diff --git a/git_cl.py b/git_cl.py index d9e2d9803b..b1cf7fa269 100755 --- a/git_cl.py +++ b/git_cl.py @@ -2303,6 +2303,7 @@ def CMDowners(parser, args): disable_color=options.no_color).run() +@subcommand.usage('[files or directories to diff]') def CMDformat(parser, args): """Runs clang-format on the diff.""" CLANG_EXTS = ['.cc', '.cpp', '.h'] @@ -2311,8 +2312,6 @@ def CMDformat(parser, args): parser.add_option('--dry-run', action='store_true', help='Don\'t modify any file on disk.') opts, args = parser.parse_args(args) - if args: - parser.error('Unrecognized args: %s' % ' '.join(args)) # git diff generates paths against the root of the repository. Change # to that directory so clang-format can find files even within subdirs. @@ -2348,7 +2347,16 @@ def CMDformat(parser, args): # Handle source file filtering. diff_cmd.append('--') - diff_cmd += ['*' + ext for ext in CLANG_EXTS] + if args: + for arg in args: + if os.path.isdir(arg): + diff_cmd += [os.path.join(arg, '*' + ext) for ext in CLANG_EXTS] + elif os.path.isfile(arg): + diff_cmd.append(arg) + else: + DieWithError('Argument "%s" is not a file or a directory' % arg) + else: + diff_cmd += ['*' + ext for ext in CLANG_EXTS] diff_output = RunGit(diff_cmd) top_dir = os.path.normpath( diff --git a/presubmit_canned_checks.py b/presubmit_canned_checks.py index d52ad9582d..bc8c3934d6 100644 --- a/presubmit_canned_checks.py +++ b/presubmit_canned_checks.py @@ -1045,8 +1045,8 @@ def PanProjectChecks(input_api, output_api, def CheckPatchFormatted(input_api, output_api): import git_cl - code, _ = git_cl.RunGitWithCode(['cl', 'format', '--dry-run'], - suppress_stderr=True) + cmd = ['cl', 'format', '--dry-run', input_api.PresubmitLocalPath()] + code, _ = git_cl.RunGitWithCode(cmd, suppress_stderr=True) if code == 2: return [output_api.PresubmitPromptWarning( 'Your patch is not formatted, please run git cl format.')]