Skip to content
47 changes: 29 additions & 18 deletions clang/tools/clang-format/git-clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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 <commit> was present.
# However, to print pretty messages, we make use of metavar and help.
Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is verbose and null a combination that should be supported?

Copy link
Author

@createyourpersonalaccount createyourpersonalaccount Feb 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have a good point, should they be mutually exclusive? I.e. if both are specified an error message is printed. I don't think --verbose is meaningful because --null does not have extra information to print. I will try to write a patch for that.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HazardyKnusperkeks Do you have an input on this? Right now it's just ignoring --verbose.

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

Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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())
Loading