-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[clang-format] Add null-terminated path option (#123921) #123926
base: main
Are you sure you want to change the base?
[clang-format] Add null-terminated path option (#123921) #123926
Conversation
This makes the `git clang-format` tool compose nicely with other shell utilities in case of maliciously (or innocently) crafted filenames.
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang-format Author: Nikolaos Chatzikonstantinou (createyourpersonalaccount) ChangesThis makes the Full diff: https://github.com/llvm/llvm-project/pull/123926.diff 1 Files Affected:
diff --git a/clang/tools/clang-format/git-clang-format b/clang/tools/clang-format/git-clang-format
index da271bbe6e3a07..04c49e8910d0ac 100755
--- a/clang/tools/clang-format/git-clang-format
+++ b/clang/tools/clang-format/git-clang-format
@@ -205,6 +205,12 @@ def main():
"commits"
),
)
+ p.add_argument(
+ "-0",
+ "--null",
+ action="store_true",
+ help="print the affected paths with null-terminated characters",
+ )
# 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.
@@ -261,11 +267,11 @@ def main():
"ignored by clang-format):"
)
for filename in ignored_files:
- print(" %s" % filename)
+ print_filename(filename, opts.null)
if changed_lines:
print("Running clang-format on the following files:")
for filename in changed_lines:
- print(" %s" % filename)
+ print_filename(filename, opts.null)
if not changed_lines:
if opts.verbose >= 0:
@@ -304,7 +310,7 @@ def main():
if (opts.verbose >= 0 and not opts.patch) or opts.verbose >= 1:
print("changed files:")
for filename in changed_files:
- print(" %s" % filename)
+ print_filename(filename, opts.null)
return 1
@@ -869,5 +875,12 @@ def convert_string(bytes_input):
return str(bytes_input)
+def print_filename(filename, null=False):
+ if null:
+ print(filename + "\0", end="")
+ else:
+ print(" %s" % filename)
+
+
if __name__ == "__main__":
sys.exit(main())
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems reasonable to me, but wait for the others, maybe @owenca
I'm wondering if So right now the usage would be (example) git clang-format --staged -0 | tail -n +2 | xargs -0 git add but perhaps it should not print informational messages and have the usage be simplified: git clang-format --staged -0 | xargs -0 git add |
With that I can see why you don't indent. But you changed only the format of the file name output. |
What else should change? |
Aren't you really asking, just give me a list of all the files that have been changed by clang format, in which case -0 isn't really that descriptive, what not
or |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
on reflection, I think better named arguments might be better
From #123921, it seems that you only want the new option to work with |
I don't know; I only use What worries me is conditions where more than one list of files is printed, e.g. unchanged + changed files. It's difficult to parse these messages from the shell; when a null-separated list is printed from other command line utilities, it has one meaning, e.g. "here's the list of files you asked for", whereas If someone is familiar with the usages of this tool maybe they can tell me what is the logic that must be followed in terms of which list is the important one according to the options passed, so that only that list is printed with |
Co-authored-by: Owen Pan <[email protected]>
Co-authored-by: Owen Pan <[email protected]>
@owenca @mydeveloperday @HazardyKnusperkeks I'll ping now that it's the weekend, perhaps you have some time to look at this more; in particular now I got the |
@@ -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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
It seems that |
No, the approach of printing to |
This makes the
git clang-format
tool compose nicely with other shell utilities in case of maliciously (or innocently) crafted filenames.Closes #123921.