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 sys
21from distutils.spawn import find_executable
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    # google-java-format-diff.py looks for google-java-format in $PATH, so find
64    # the parent dir up front and inject it into $PATH when launching it.
65    # TODO: Pass the path in directly once this issue is resolved:
66    # https://github.com/google/google-java-format/issues/108
67    format_path = find_executable(opts.google_java_format)
68    if not format_path:
69        print('Unable to find google-java-format at %s' %
70              opts.google_java_format)
71        return 1
72
73    extra_env = {
74        'PATH': '%s%s%s' % (os.path.dirname(format_path),
75                            os.pathsep,
76                            os.environ['PATH'])
77    }
78
79    # TODO: Delegate to the tool once this issue is resolved:
80    # https://github.com/google/google-java-format/issues/107
81    diff_cmd = ['git', 'diff', '--no-ext-diff', '-U0', '%s^!' % opts.commit]
82    diff_cmd.extend(['--'] + opts.files)
83    diff = rh.utils.run(diff_cmd, capture_output=True).stdout
84
85    cmd = [opts.google_java_format_diff, '-p1', '--aosp']
86    if opts.fix:
87        cmd.extend(['-i'])
88    if not opts.sort_imports:
89        cmd.extend(['--skip-sorting-imports'])
90
91    stdout = rh.utils.run(cmd, input=diff, capture_output=True,
92                          extra_env=extra_env).stdout
93    if stdout:
94        print('One or more files in your commit have Java formatting errors.')
95        print('You can run `%s --fix %s` to fix this' %
96              (sys.argv[0], rh.shell.cmd_to_str(argv)))
97        return 1
98
99    return 0
100
101
102if __name__ == '__main__':
103    sys.exit(main(sys.argv[1:]))
104