aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuis Hector Chavez <lhchavez@google.com>2016-10-18 16:28:11 -0700
committerLuis Hector Chavez <lhchavez@google.com>2016-10-18 17:01:32 -0700
commit4913e578e816b5a6178b61dc934553c176780dce (patch)
tree8f922d30e2ebe29e79a10171819de4cdea6c84f7
parent5c4c293174bb61f0f39035a71acd9084abfa743d (diff)
downloadrepohooks-4913e578e816b5a6178b61dc934553c176780dce.tar.gz
hooks: Improve the clang-format wrapper script
This change leverages upstream changes in git-clang-format to restrict diffs to a single commit instead of diffing from a commit to whatever is in the working tree. That mode of operation does not lend itself to an easy invocation to automatically fix any violations (since it requires the use of the --diff flag), so a --fix flag was also added to the wrapper script in order to automatically apply any changes that are needed to comply. Bug: 31305183 Test: repo upload Change-Id: I835f2e1923803ff8ac9aee451078e6c1c1dcc070
-rwxr-xr-xtools/clang-format.py55
1 files changed, 39 insertions, 16 deletions
diff --git a/tools/clang-format.py b/tools/clang-format.py
index b4fbd31..ac9e826 100755
--- a/tools/clang-format.py
+++ b/tools/clang-format.py
@@ -43,23 +43,42 @@ def get_parser():
help='The path of the clang-format executable.')
parser.add_argument('--git-clang-format', default='git-clang-format',
help='The path of the git-clang-format executable.')
- parser.add_argument('--commit', type=str, default='HEAD',
- help='Specify the commit to validate.')
+ parser.add_argument('--style', metavar='STYLE', type=str,
+ help='The style that clang-format will use.')
+ parser.add_argument('--extensions', metavar='EXTENSIONS', type=str,
+ help='Comma-separated list of file extensions to '
+ 'format.')
+ parser.add_argument('--fix', action='store_true',
+ help='Fix any formatting errors automatically.')
+
+ scope = parser.add_mutually_exclusive_group(required=True)
+ scope.add_argument('--commit', type=str, default='HEAD',
+ help='Specify the commit to validate.')
+ scope.add_argument('--working-tree', action='store_true',
+ help='Validates the files that have changed from '
+ 'HEAD in the working directory.')
+
+ parser.add_argument('files', type=str, nargs='*',
+ help='If specified, only consider differences in '
+ 'these files.')
return parser
def main(argv):
"""The main entry."""
parser = get_parser()
- opts, unknown = parser.parse_known_args(argv)
-
- # TODO(b/31305183): Avoid false positives by limiting git-clang-format's
- # diffs to just that commit instead of from the parent of the commit against
- # the working tree.
- cmd = [opts.git_clang_format, '--binary',
- opts.clang_format, '--commit=%s^' % opts.commit] + unknown
-
- stdout = rh.utils.run_command(cmd + ['--diff'], capture_output=True).output
+ opts = parser.parse_args(argv)
+
+ cmd = [opts.git_clang_format, '--binary', opts.clang_format, '--diff']
+ if opts.style:
+ cmd.extend(['--style', opts.style])
+ if opts.extensions:
+ cmd.extend(['--extensions', opts.extensions])
+ if not opts.working_tree:
+ cmd.extend(['%s^' % opts.commit, opts.commit])
+ cmd.extend(['--'] + opts.files)
+
+ stdout = rh.utils.run_command(cmd, capture_output=True).output
if stdout.rstrip('\n') == 'no modified files to format':
# This is always printed when only files that clang-format does not
# understand were modified.
@@ -71,11 +90,15 @@ def main(argv):
diff_filenames.append(line[len(DIFF_MARKER_PREFIX):].rstrip())
if diff_filenames:
- print('The following files have formatting errors:')
- for filename in diff_filenames:
- print('\t%s' % filename)
- print('You can run `%s` to fix this' % rh.shell.cmd_to_str(cmd))
- return 1
+ if opts.fix:
+ rh.utils.run_command(['git', 'apply'], input=stdout)
+ else:
+ print('The following files have formatting errors:')
+ for filename in diff_filenames:
+ print('\t%s' % filename)
+ print('You can run `%s --fix %s` to fix this' %
+ (sys.argv[0], rh.shell.cmd_to_str(argv)))
+ return 1
return 0