https://github.com/selimkeles created 
https://github.com/llvm/llvm-project/pull/135779

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! 🚀

>From 606802b8703fa3c1affcc3e52afc22bc3422dcad Mon Sep 17 00:00:00 2001
From: "selim.keles" <selim.ke...@landisgyr.com>
Date: Tue, 15 Apr 2025 14:24:08 +0300
Subject: [PATCH] Fix filename parsing in clang-format-diff.py for files or
 paths with spaces

---
 clang/tools/clang-format/clang-format-diff.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

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
 

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to