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 git-clang-format and parse its output.""" 17 18import argparse 19import os 20import sys 21 22_path = os.path.realpath(__file__ + '/../..') 23if sys.path[0] != _path: 24 sys.path.insert(0, _path) 25del _path 26 27# We have to import our local modules after the sys.path tweak. We can't use 28# relative imports because this is an executable program, not a module. 29# pylint: disable=wrong-import-position,import-error 30import rh.shell 31import rh.utils 32 33 34# Since we're asking git-clang-format to print a diff, all modified filenames 35# that have formatting errors are printed with this prefix. 36DIFF_MARKER_PREFIX = '+++ b/' 37 38 39def get_parser(): 40 """Return a command line parser.""" 41 parser = argparse.ArgumentParser(description=__doc__) 42 parser.add_argument('--clang-format', default='clang-format', 43 help='The path of the clang-format executable.') 44 parser.add_argument('--git-clang-format', default='git-clang-format', 45 help='The path of the git-clang-format executable.') 46 parser.add_argument('--style', metavar='STYLE', type=str, 47 help='The style that clang-format will use.') 48 parser.add_argument('--extensions', metavar='EXTENSIONS', type=str, 49 help='Comma-separated list of file extensions to ' 50 'format.') 51 parser.add_argument('--fix', action='store_true', 52 help='Fix any formatting errors automatically.') 53 54 scope = parser.add_mutually_exclusive_group(required=True) 55 scope.add_argument('--commit', type=str, default='HEAD', 56 help='Specify the commit to validate.') 57 scope.add_argument('--working-tree', action='store_true', 58 help='Validates the files that have changed from ' 59 'HEAD in the working directory.') 60 61 parser.add_argument('files', type=str, nargs='*', 62 help='If specified, only consider differences in ' 63 'these files.') 64 return parser 65 66 67def main(argv): 68 """The main entry.""" 69 parser = get_parser() 70 opts = parser.parse_args(argv) 71 72 cmd = [opts.git_clang_format, '--binary', opts.clang_format, '--diff'] 73 if opts.style: 74 cmd.extend(['--style', opts.style]) 75 if opts.extensions: 76 cmd.extend(['--extensions', opts.extensions]) 77 if not opts.working_tree: 78 cmd.extend([f'{opts.commit}^', opts.commit]) 79 cmd.extend(['--'] + opts.files) 80 81 # Fail gracefully if clang-format itself aborts/fails. 82 result = rh.utils.run(cmd, capture_output=True, check=False) 83 # Newer versions of git-clang-format will exit 1 when it worked. Assume a 84 # real failure is any exit code above 1, or any time stderr is used, or if 85 # it exited 1 and produce useful format diffs to stdout. If it exited 0, 86 # then assume all is well and we'll attempt to parse its output below. 87 ret_code = None 88 if (result.returncode > 1 or result.stderr or 89 (result.stdout and result.returncode)): 90 # Apply fix if the flag is set and clang-format shows it is fixible. 91 if opts.fix and result.stdout and result.returncode: 92 result = rh.utils.run(['git', 'apply'], input=result.stdout, 93 check=False) 94 ret_code = result.returncode 95 if ret_code: 96 print('Error: Unable to automatically fix things.\n' 97 ' Make sure your checkout is clean first.\n' 98 ' If you have multiple commits, you might have to ' 99 'manually rebase your tree first.', 100 file=sys.stderr) 101 102 else: # Regular clang-format aborts/fails. 103 print(f'clang-format failed:\ncmd: {result.cmdstr}\n' 104 f'stdout:\n{result.stdout}\n', file=sys.stderr) 105 if result.returncode > 1 or result.stderr: 106 print('\nPlease report this to the clang team.\n', 107 f'stderr:\n{result.stderr}', file=sys.stderr) 108 ret_code = 1 109 110 return ret_code 111 112 stdout = result.stdout 113 if stdout.rstrip('\n') == 'no modified files to format': 114 # This is always printed when only files that clang-format does not 115 # understand were modified. 116 return 0 117 118 diff_filenames = [] 119 for line in stdout.splitlines(): 120 if line.startswith(DIFF_MARKER_PREFIX): 121 diff_filenames.append(line[len(DIFF_MARKER_PREFIX):].rstrip()) 122 123 if diff_filenames: 124 if opts.fix: 125 result = rh.utils.run(['git', 'apply'], input=stdout, check=False) 126 if result.returncode: 127 print('Error: Unable to automatically fix things.\n' 128 ' Make sure your checkout is clean first.\n' 129 ' If you have multiple commits, you might have to ' 130 'manually rebase your tree first.', 131 file=sys.stderr) 132 return result.returncode 133 else: 134 print('The following files have formatting errors:') 135 for filename in diff_filenames: 136 print(f'\t{filename}') 137 print('You can try to fix this by running:\n' 138 f'{sys.argv[0]} --fix {rh.shell.cmd_to_str(argv)}') 139 return 1 140 141 return 0 142 143 144if __name__ == '__main__': 145 sys.exit(main(sys.argv[1:])) 146