[clang] [clang-format] convert path to Windows path if user is using a MSYS2 shell (PR #111526)

via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 8 05:35:32 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-format

Author: Gary Wang (BLumia)

<details>
<summary>Changes</summary>

Currently, we simply rely on the result of `git rev-parse --show-toplevel` in `cd_to_toplevel()`, which when using MSYS2 shell under Windows, can result getting a UNIX path instead of Windows path, and `os.chdir(toplevel)` would simply fail.

This patch detects if user is using MSYS2 shell (by checking `MSYSTEM` environment variable and checking if `cygpath` exists) and will use `cygpath` to convert the path to Windows path.

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


1 Files Affected:

- (modified) clang/tools/clang-format/git-clang-format (+3) 


``````````diff
diff --git a/clang/tools/clang-format/git-clang-format b/clang/tools/clang-format/git-clang-format
index bacbd8de245666..3424822ede3835 100755
--- a/clang/tools/clang-format/git-clang-format
+++ b/clang/tools/clang-format/git-clang-format
@@ -31,6 +31,7 @@ import os
 import re
 import subprocess
 import sys
+import shutil
 
 usage = ('git clang-format [OPTIONS] [<commit>] [<commit>|--staged] '
          '[--] [<file>...]')
@@ -413,6 +414,8 @@ def filter_ignored_files(dictionary, binary):
 def cd_to_toplevel():
   """Change to the top level of the git repository."""
   toplevel = run('git', 'rev-parse', '--show-toplevel')
+  if "MSYSTEM" in os.environ and shutil.which('cygpath') is not None:
+    toplevel = run('cygpath', '-w', toplevel)
   os.chdir(toplevel)
 
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/111526


More information about the cfe-commits mailing list