Skip to content

Fix filename parsing in clang-format-diff.py for paths with spaces #135779

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

Merged
merged 2 commits into from
Apr 19, 2025

Conversation

selimkeles
Copy link
Contributor

@selimkeles selimkeles commented Apr 15, 2025

Summary: This PR resolves an issue in clang-format-diff.py where filenames containing spaces were not correctly extracted from Git diffs. Due to the previous regex implementation, filenames were being truncated, causing the script to fail when processing diffs with such filenames.

Fixes #135619.

Details:

  • Adjusted the regex pattern to correctly capture the filename, including spaces.
  • Ensured compatibility across Linux and Windows environments (tested on WSL and Windows).

Steps to Reproduce:

Modify a file with spaces in its name, stage the changes, and generate a diff.

Run git diff --cached -U0 --no-color | python3 clang-format-diff.py -p1.

Before the fix, filenames with spaces are incorrectly extracted or cause errors.

After the fix, filenames with spaces are correctly recognized, and formatting differences are processed properly.

Impact:

Users relying on clang-format-diff.py in their automated workflows and pre-commit hooks will now be able to format files without filename parsing issues.

No changes are made to formatting behavior—only improved filename handling in diffs.

Maintainer Mentions: Tagging @mydeveloperday and @owenca for review since they maintain clang-format and also @owenca reviewed my issue earlier.

Note: Also attached the patch file clang-format-space-fix.patch

Would love any feedback or suggestions for further refinements! 🚀

Copy link

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 @ followed by their GitHub username.

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.

@llvmbot
Copy link
Member

llvmbot commented Apr 15, 2025

@llvm/pr-subscribers-lldb

@llvm/pr-subscribers-clang-format

Author: Selim Keles (selimkeles)

Changes

Summary: This PR resolves an issue in clang-format-diff.py where filenames containing spaces were not correctly extracted from Git diffs. Due to the previous regex implementation, filenames were being truncated, causing the script to fail when processing diffs with such filenames.

Details:

  • Adjusted the regex pattern to correctly capture the filename, including spaces.
  • Ensured compatibility across Linux and Windows environments (tested on WSL and Windows).

Steps to Reproduce:

Modify a file with spaces in its name, stage the changes, and generate a diff.

Run git diff --cached -U0 --no-color | python3 clang-format-diff.py -p1.

Before the fix, filenames with spaces are incorrectly extracted or cause errors.

After the fix, filenames with spaces are correctly recognized, and formatting differences are processed properly.

Impact:

Users relying on clang-format-diff.py in their automated workflows and pre-commit hooks will now be able to format files without filename parsing issues.

No changes are made to formatting behavior—only improved filename handling in diffs.

Maintainer Mentions: Tagging @mydeveloperday and @owenca for review since they maintain clang-format and also @owenca reviewed my issue earlier.

Note: Also attached the patch file clang-format-space-fix.patch

Would love any feedback or suggestions for further refinements! 🚀


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

1 Files Affected:

  • (modified) clang/tools/clang-format/clang-format-diff.py (+2-2)
diff --git a/clang/tools/clang-format/clang-format-diff.py b/clang/tools/clang-format/clang-format-diff.py
index c82b41e8bd031..e1d635fc85ffb 100755
--- a/clang/tools/clang-format/clang-format-diff.py
+++ b/clang/tools/clang-format/clang-format-diff.py
@@ -102,9 +102,9 @@ def main():
     filename = None
     lines_by_file = {}
     for line in sys.stdin:
-        match = re.search(r"^\+\+\+\ (.*?/){%s}(\S*)" % args.p, line)
+        match = re.search(r"^\+\+\+\s+(?:.*?/){%s}(.+)$" % args.p, line)
         if match:
-            filename = match.group(2)
+            filename = match.group(1).strip()
         if filename is None:
             continue
 

@selimkeles selimkeles force-pushed the clang-format-space-fix branch from 606802b to 235ef7b Compare April 18, 2025 05:52
@llvmbot llvmbot added the lldb label Apr 18, 2025
@selimkeles selimkeles closed this Apr 18, 2025
@selimkeles selimkeles force-pushed the clang-format-space-fix branch from 235ef7b to 81499ed Compare April 18, 2025 06:00
@selimkeles selimkeles reopened this Apr 18, 2025
@owenca owenca removed the lldb label Apr 19, 2025
@owenca owenca merged commit 0c0b5ab into llvm:main Apr 19, 2025
11 checks passed
Copy link

@selimkeles Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@llvm llvm deleted a comment from llvm-ci Apr 19, 2025
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
llvm#135779)

This PR resolves an issue in clang-format-diff.py where
filenames containing spaces were not correctly extracted from Git diffs.
Due to the previous regex implementation, filenames were being
truncated, causing the script to fail when processing diffs with such
filenames.

Fixes llvm#135619.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
llvm#135779)

This PR resolves an issue in clang-format-diff.py where
filenames containing spaces were not correctly extracted from Git diffs.
Due to the previous regex implementation, filenames were being
truncated, causing the script to fail when processing diffs with such
filenames.

Fixes llvm#135619.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
llvm#135779)

This PR resolves an issue in clang-format-diff.py where
filenames containing spaces were not correctly extracted from Git diffs.
Due to the previous regex implementation, filenames were being
truncated, causing the script to fail when processing diffs with such
filenames.

Fixes llvm#135619.
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.

clang-format-diff.py Fails to Correctly Handle Filenames with Spaces
3 participants