• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    # While the formatter defaults to sorting imports, in the Android codebase,
48    # the standard import order doesn't match the formatter's, so flip the
49    # default to not sort imports, while letting callers override as desired.
50    parser.add_argument('--sort-imports', action='store_true',
51                        help='If true, imports will be sorted.')
52    parser.add_argument('files', nargs='*',
53                        help='If specified, only consider differences in '
54                             'these files.')
55    return parser
56
57
58def main(argv):
59    """The main entry."""
60    parser = get_parser()
61    opts = parser.parse_args(argv)
62
63    format_path = shutil.which(opts.google_java_format)
64    if not format_path:
65        print(
66            f'Unable to find google-java-format at: {opts.google_java_format}',
67            file=sys.stderr
68        )
69        return 1
70
71    # TODO: Delegate to the tool once this issue is resolved:
72    # https://github.com/google/google-java-format/issues/107
73    diff_cmd = ['git', 'diff', '--no-ext-diff', '-U0', f'{opts.commit}^!']
74    diff_cmd.extend(['--'] + opts.files)
75    diff = rh.utils.run(diff_cmd, capture_output=True).stdout
76
77    cmd = [opts.google_java_format_diff, '-p1', '--aosp', '-b', format_path]
78    if opts.fix:
79        cmd.extend(['-i'])
80    if not opts.sort_imports:
81        cmd.extend(['--skip-sorting-imports'])
82
83    stdout = rh.utils.run(cmd, input=diff, capture_output=True).stdout
84    if stdout:
85        print('One or more files in your commit have Java formatting errors.')
86        print(f'You can run: {sys.argv[0]} --fix {rh.shell.cmd_to_str(argv)}')
87        return 1
88
89    return 0
90
91
92if __name__ == '__main__':
93    sys.exit(main(sys.argv[1:]))
94