Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[google-java-format-diff] Add mode that verifies a diff is formated #516

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions scripts/google-java-format-diff.py
Expand Up @@ -62,9 +62,15 @@ def main():
parser.add_argument('-b', '--binary', help='path to google-java-format binary')
parser.add_argument('--google-java-format-jar', metavar='ABSOLUTE_PATH', default=None,
help='use a custom google-java-format jar')
parser.add_argument('--verify', action='store_true',
help="Only verify that the diff is formated and skip reformatting")

args = parser.parse_args()

if args.i and args.verify:
sys.stderr.write("Cannot use -i and --verify simultaneously")
sys.exit(-1)

# Extract changed lines for each file.
filename = None
lines_by_file = {}
Expand Down Expand Up @@ -116,15 +122,23 @@ def main():
command.append('--skip-sorting-imports')
if args.skip_removing_unused_imports:
command.append('--skip-removing-unused-imports')
if args.verify:
command.append("--dry-run")
command.append("--set-exit-if-changed")
command.extend(lines)
command.append(filename)
p = subprocess.Popen(command, stdout=subprocess.PIPE,
stderr=None, stdin=subprocess.PIPE)
stdout, stderr = p.communicate()
if p.returncode != 0:
sys.exit(p.returncode);
if args.verify:
sys.stderr.write(
"The following file is not formated: " + filename + "\n")
continue
else:
sys.exit(p.returncode)

if not args.i:
if not args.i and not args.verify:
with open(filename) as f:
code = f.readlines()
formatted_code = StringIO.StringIO(stdout).readlines()
Expand Down