1#!/usr/bin/python 2# -*- coding:utf-8 -*- 3# Copyright 2016 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17"""Wrapper to run git-clang-format and parse its output.""" 18 19from __future__ import print_function 20 21import argparse 22import os 23import sys 24 25_path = os.path.realpath(__file__ + '/../..') 26if sys.path[0] != _path: 27 sys.path.insert(0, _path) 28del _path 29 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(['%s^' % opts.commit, opts.commit]) 79 cmd.extend(['--'] + opts.files) 80 81 stdout = rh.utils.run_command(cmd, capture_output=True).output 82 if stdout.rstrip('\n') == 'no modified files to format': 83 # This is always printed when only files that clang-format does not 84 # understand were modified. 85 return 0 86 87 diff_filenames = [] 88 for line in stdout.splitlines(): 89 if line.startswith(DIFF_MARKER_PREFIX): 90 diff_filenames.append(line[len(DIFF_MARKER_PREFIX):].rstrip()) 91 92 if diff_filenames: 93 if opts.fix: 94 rh.utils.run_command(['git', 'apply'], input=stdout) 95 else: 96 print('The following files have formatting errors:') 97 for filename in diff_filenames: 98 print('\t%s' % filename) 99 print('You can run `%s --fix %s` to fix this' % 100 (sys.argv[0], rh.shell.cmd_to_str(argv))) 101 return 1 102 103 return 0 104 105 106if __name__ == '__main__': 107 sys.exit(main(sys.argv[1:])) 108