Skip to content

Commit

Permalink
[google-java-format-diff] Add mode that verifies a diff is formated
Browse files Browse the repository at this point in the history
This change adds a flag to google-java-format-diff.py to verify that a
diff is formatted instread of reformating the files. Useful to enforce a
styleguide as part of presubmit checks.
  • Loading branch information
Yannic committed Sep 2, 2020
1 parent 1614b6a commit 37c478b
Showing 1 changed file with 16 additions and 2 deletions.
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

0 comments on commit 37c478b

Please sign in to comment.