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

Gary Wang via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 8 05:34:36 PDT 2024


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

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.

>From 30b279586ac42c4e94952ce3963ea8f359603229 Mon Sep 17 00:00:00 2001
From: Gary Wang <wangzichong at deepin.org>
Date: Tue, 8 Oct 2024 20:32:05 +0800
Subject: [PATCH] [clang-format] convert path to Windows path if user is using
 a MSYS2 shell

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.
---
 clang/tools/clang-format/git-clang-format | 3 +++
 1 file changed, 3 insertions(+)

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)
 
 



More information about the cfe-commits mailing list