Skip to content
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

Improve clang-format-diff help output #73491

Conversation

serge-sans-paille
Copy link
Collaborator

It is quite common to symlink clang-format-diff.py to clang-format-diff, and in that case the help output still refers to the .py version. Compute it instead to work in both setup.

@llvmbot
Copy link
Collaborator

llvmbot commented Nov 27, 2023

@llvm/pr-subscribers-clang-format

Author: None (serge-sans-paille)

Changes

It is quite common to symlink clang-format-diff.py to clang-format-diff, and in that case the help output still refers to the .py version. Compute it instead to work in both setup.


Full diff: https://github.com/llvm/llvm-project/pull/73491.diff

1 Files Affected:

  • (modified) clang/tools/clang-format/clang-format-diff.py (+6-3)
diff --git a/clang/tools/clang-format/clang-format-diff.py b/clang/tools/clang-format/clang-format-diff.py
index 892c1e38a462ff4..facaaf96dd8f90a 100755
--- a/clang/tools/clang-format/clang-format-diff.py
+++ b/clang/tools/clang-format/clang-format-diff.py
@@ -13,8 +13,8 @@
 lines. This is useful to reformat all the lines touched by a specific patch.
 Example usage for git/svn users:
 
-  git diff -U0 --no-color --relative HEAD^ | clang-format-diff.py -p1 -i
-  svn diff --diff-cmd=diff -x-U0 | clang-format-diff.py -i
+  git diff -U0 --no-color --relative HEAD^ | {clang_format_diff} -p1 -i
+  svn diff --diff-cmd=diff -x-U0 | {clang_format_diff} -i
 
 It should be noted that the filename contained in the diff is used unmodified
 to determine the source file to update. Users calling this script directly
@@ -25,6 +25,7 @@
 
 import argparse
 import difflib
+import os
 import re
 import subprocess
 import sys
@@ -36,8 +37,10 @@
 
 
 def main():
+    basename = os.path.basename(sys.argv[0])
     parser = argparse.ArgumentParser(
-        description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
+        description=__doc__.format(clang_format_diff=basename),
+        formatter_class=argparse.RawDescriptionHelpFormatter
     )
     parser.add_argument(
         "-i",

Copy link

github-actions bot commented Nov 27, 2023

✅ With the latest revision this PR passed the Python code formatter.

@owenca
Copy link
Contributor

owenca commented Nov 28, 2023

See below. Seems it's already working as expected without this patch.

$ clang-format -version
clang-format version 17.0.5
$ clang-format-diff.py foo
usage: clang-format-diff.py [-h] [-i] [-p NUM] [-regex PATTERN] [-iregex PATTERN]
                            [-sort-includes] [-v] [-style STYLE]
                            [-fallback-style FALLBACK_STYLE] [-binary BINARY]
clang-format-diff.py: error: unrecognized arguments: foo
$ ln -s clang-format-diff.py cfd
$ cfd foo
usage: cfd [-h] [-i] [-p NUM] [-regex PATTERN] [-iregex PATTERN] [-sort-includes] [-v]
           [-style STYLE] [-fallback-style FALLBACK_STYLE] [-binary BINARY]
cfd: error: unrecognized arguments: foo

@serge-sans-paille
Copy link
Collaborator Author

See below. Seems it's already working as expected without this patch.

$ clang-format -version
clang-format version 17.0.5
$ clang-format-diff.py foo
usage: clang-format-diff.py [-h] [-i] [-p NUM] [-regex PATTERN] [-iregex PATTERN]
                            [-sort-includes] [-v] [-style STYLE]
                            [-fallback-style FALLBACK_STYLE] [-binary BINARY]
clang-format-diff.py: error: unrecognized arguments: foo
$ ln -s clang-format-diff.py cfd
$ cfd foo
usage: cfd [-h] [-i] [-p NUM] [-regex PATTERN] [-iregex PATTERN] [-sort-includes] [-v]
           [-style STYLE] [-fallback-style FALLBACK_STYLE] [-binary BINARY]
cfd: error: unrecognized arguments: foo

try with clang-format-diff.py --help vs cfd --help ;-)

@owenca
Copy link
Contributor

owenca commented Nov 28, 2023

Please open a github issue to show us what you are trying to accomplish and reference the issue number here.

It is quite common to symlink clang-format-diff.py to
clang-format-diff, and in that case the help output still refers to the
.py version. Compute it instead to work in both setup.

Fix llvm#73635
@serge-sans-paille serge-sans-paille force-pushed the feature/improve-clang-format-diff-help branch from 4ddec8f to 864f9a1 Compare November 28, 2023 10:59
@serge-sans-paille
Copy link
Collaborator Author

@owenca bug opened as #73635 and commit message updated to link to it.

@serge-sans-paille serge-sans-paille force-pushed the feature/improve-clang-format-diff-help branch from 864f9a1 to 24cb032 Compare November 28, 2023 13:46
@serge-sans-paille serge-sans-paille merged commit ced0f28 into llvm:main Nov 28, 2023
2 of 3 checks passed
owenca added a commit to owenca/llvm-project that referenced this pull request Dec 5, 2023
serge-sans-paille pushed a commit that referenced this pull request Dec 5, 2023
sstipanovic added a commit to sstipanovic/llvm-project that referenced this pull request Dec 22, 2023
sstipanovic added a commit to sstipanovic/llvm-project that referenced this pull request Dec 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants