1#!/usr/bin/env python3 2# Copyright 2016 The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16"""Wrapper to run google-java-format to check for any malformatted changes.""" 17 18import argparse 19import os 20import shutil 21import sys 22 23_path = os.path.realpath(__file__ + '/../..') 24if sys.path[0] != _path: 25 sys.path.insert(0, _path) 26del _path 27 28# We have to import our local modules after the sys.path tweak. We can't use 29# relative imports because this is an executable program, not a module. 30# pylint: disable=wrong-import-position 31import rh.shell 32import rh.utils 33 34 35def get_parser(): 36 """Return a command line parser.""" 37 parser = argparse.ArgumentParser(description=__doc__) 38 parser.add_argument('--google-java-format', default='google-java-format', 39 help='The path of the google-java-format executable.') 40 parser.add_argument('--google-java-format-diff', 41 default='google-java-format-diff.py', 42 help='The path of the google-java-format-diff script.') 43 parser.add_argument('--fix', action='store_true', 44 help='Fix any formatting errors automatically.') 45 parser.add_argument('--commit', type=str, default='HEAD', 46 help='Specify the commit to validate.') 47 parser.add_argument('--skip-sorting-imports', action='store_true', 48 help='If true, imports will not be sorted.') 49 parser.add_argument('files', nargs='*', 50 help='If specified, only consider differences in ' 51 'these files.') 52 return parser 53 54 55def main(argv): 56 """The main entry.""" 57 parser = get_parser() 58 opts = parser.parse_args(argv) 59 60 format_path = shutil.which(opts.google_java_format) 61 if not format_path: 62 print( 63 f'Unable to find google-java-format at: {opts.google_java_format}', 64 file=sys.stderr 65 ) 66 return 1 67 68 # TODO: Delegate to the tool once this issue is resolved: 69 # https://github.com/google/google-java-format/issues/107 70 diff_cmd = ['git', 'diff', '--no-ext-diff', '-U0', f'{opts.commit}^!'] 71 diff_cmd.extend(['--'] + opts.files) 72 diff = rh.utils.run(diff_cmd, capture_output=True).stdout 73 74 cmd = [opts.google_java_format_diff, '-p1', '--aosp', '-b', format_path] 75 if opts.fix: 76 cmd.extend(['-i']) 77 if opts.skip_sorting_imports: 78 cmd.extend(['--skip-sorting-imports']) 79 80 stdout = rh.utils.run(cmd, input=diff, capture_output=True).stdout 81 if stdout: 82 print('One or more files in your commit have Java formatting errors.') 83 print(f'You can run: {sys.argv[0]} --fix {rh.shell.cmd_to_str(argv)}') 84 return 1 85 86 return 0 87 88 89if __name__ == '__main__': 90 sys.exit(main(sys.argv[1:])) 91