diff --git a/clang/tools/clang-format/git-clang-format b/clang/tools/clang-format/git-clang-format index da271bbe6e3a07d..e14c6311b599f2c 100755 --- a/clang/tools/clang-format/git-clang-format +++ b/clang/tools/clang-format/git-clang-format @@ -149,6 +149,15 @@ def main(): action="store_true", help="print a diff instead of applying the changes", ) + p.add_argument( + "--diff_from_common_commit", + action="store_true", + help=( + "diff from the last common commit for commits in " + "separate branches rather than the exact point of the " + "commits" + ), + ) p.add_argument( "--diffstat", action="store_true", @@ -171,6 +180,11 @@ def main(): p.add_argument( "-p", "--patch", action="store_true", help="select hunks interactively" ) + p.add_argument( + "--print0", + action="store_true", + help="end each printed filename with a null character", + ) p.add_argument( "-q", "--quiet", @@ -196,15 +210,6 @@ def main(): default=0, help="print extra information", ) - p.add_argument( - "--diff_from_common_commit", - action="store_true", - help=( - "diff from the last common commit for commits in " - "separate branches rather than the exact point of the " - "commits" - ), - ) # We gather all the remaining positional arguments into 'args' since we need # to use some heuristics to determine whether or not was present. # However, to print pretty messages, we make use of metavar and help. @@ -260,15 +265,13 @@ def main(): "Ignoring the following files (wrong extension, symlink, or " "ignored by clang-format):" ) - for filename in ignored_files: - print(" %s" % filename) + print_filenames(ignored_files, opts.print0) if changed_lines: print("Running clang-format on the following files:") - for filename in changed_lines: - print(" %s" % filename) + print_filenames(changed_lines, opts.print0) if not changed_lines: - if opts.verbose >= 0: + if opts.verbose >= 0 and not opts.print0: print("no modified files to format") return 0 @@ -289,7 +292,7 @@ def main(): print("new tree: %s" % new_tree) if old_tree == new_tree: - if opts.verbose >= 0: + if opts.verbose >= 0 and not opts.print0: print("clang-format did not modify any files") return 0 @@ -302,9 +305,9 @@ def main(): old_tree, new_tree, force=opts.force, patch_mode=opts.patch ) if (opts.verbose >= 0 and not opts.patch) or opts.verbose >= 1: - print("changed files:") - for filename in changed_files: - print(" %s" % filename) + if not opts.print0: + print("changed files:") + print_filenames(changed_files, opts.print0) return 1 @@ -869,5 +872,13 @@ def convert_string(bytes_input): return str(bytes_input) +def print_filenames(filenames, print0): + for filename in filenames: + if print0: + print(filename, end="\0") + else: + print(" " * 4 + filename) + + if __name__ == "__main__": sys.exit(main())