Skip to content

Commit 66ff4c1

Browse files
committed
Omit CWD in search for bash.exe to run hooks on Windows
This uses the same NoDefaultCurrentDirectoryInExePath technique for the Popen call in git.index.fun.run_commit_hook on Windows as is already used in git.cmd.Git.execute. The code is simpler in run_commit_hook because a shell is never used to run bash.exe. (bash.exe is itself a shell, but we never run it *via* a shell by passing shell=True to Popen.) Nonetheless, it may make sense to extract out a helper function both can call. This commit does not do that, so there is some code duplication.
1 parent 61b4dda commit 66ff4c1

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

git/index/fun.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
"""Standalone functions to accompany the index implementation and make it more versatile."""
55

6+
import contextlib
67
from io import BytesIO
78
import os
89
import os.path as osp
@@ -26,7 +27,7 @@
2627
traverse_trees_recursive,
2728
tree_to_stream,
2829
)
29-
from git.util import IndexFileSHA1Writer, finalize_process
30+
from git.util import IndexFileSHA1Writer, finalize_process, patch_env
3031
from gitdb.base import IStream
3132
from gitdb.typ import str_tree_type
3233

@@ -90,6 +91,10 @@ def run_commit_hook(name: str, index: "IndexFile", *args: str) -> None:
9091
env = os.environ.copy()
9192
env["GIT_INDEX_FILE"] = safe_decode(str(index.path))
9293
env["GIT_EDITOR"] = ":"
94+
if os.name == "nt":
95+
maybe_patch_caller_env = patch_env("NoDefaultCurrentDirectoryInExePath", "1")
96+
else:
97+
maybe_patch_caller_env = contextlib.nullcontext()
9398
cmd = [hp]
9499
try:
95100
if os.name == "nt" and not _has_file_extension(hp):
@@ -98,14 +103,15 @@ def run_commit_hook(name: str, index: "IndexFile", *args: str) -> None:
98103
relative_hp = Path(hp).relative_to(index.repo.working_dir).as_posix()
99104
cmd = ["bash.exe", relative_hp]
100105

101-
process = subprocess.Popen(
102-
cmd + list(args),
103-
env=env,
104-
stdout=subprocess.PIPE,
105-
stderr=subprocess.PIPE,
106-
cwd=index.repo.working_dir,
107-
creationflags=PROC_CREATIONFLAGS,
108-
)
106+
with maybe_patch_caller_env:
107+
process = subprocess.Popen(
108+
cmd + list(args),
109+
env=env,
110+
stdout=subprocess.PIPE,
111+
stderr=subprocess.PIPE,
112+
cwd=index.repo.working_dir,
113+
creationflags=PROC_CREATIONFLAGS,
114+
)
109115
except Exception as ex:
110116
raise HookExecutionError(hp, ex) from ex
111117
else:

0 commit comments

Comments
 (0)