llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-format

Author: Selim Keles (selimkeles)

<details>
<summary>Changes</summary>

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](https://github.com/llvm/llvm-project/issues/135619#issuecomment-2804148179)
 earlier.

_**Note: Also attached the patch file**_ 
[clang-format-space-fix.patch](https://github.com/user-attachments/files/19757014/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
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
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/135779
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to