Skip to content

Commit

Permalink
Make Git.execute a bit simpler and very slightly more robust
Browse files Browse the repository at this point in the history
This covers the rare unpatched python/cpython#101283 case the
previous commit added tests for, that only applies in the unusual
situation that the ComSpec environment variable is unset and an old
build (but this includes downloadable builds and current
actions/setup-python builds) of Python <=3.9 for Windows is in use.

The main benefit of this change is really to slightly simplify the
code under test. (It might even be justified to remove the
use_shell_impostor=True test cases at some point.)
  • Loading branch information
EliahKagan committed Jan 9, 2024
1 parent 865c6e8 commit d2506c7
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions git/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -988,25 +988,22 @@ def execute(
if shell is None:
shell = self.USE_SHELL

cmd_not_found_exception = FileNotFoundError
maybe_patch_caller_env = contextlib.nullcontext()

if os.name == "nt":
cmd_not_found_exception = OSError
if kill_after_timeout is not None:
raise GitCommandError(
redacted_command,
'"kill_after_timeout" feature is not supported on Windows.',
)

cmd_not_found_exception = OSError

# Search PATH, but do not search CWD. The "1" can be any value.
# Search PATH but not CWD. The "1" can be any value. We'll patch just before
# the Popen call and unpatch just after, or we get a worse race condition.
maybe_patch_caller_env = patch_env("NoDefaultCurrentDirectoryInExePath", "1")
if shell:
# If the direct subprocess is a shell, this must go in its environment.
# Modify the direct shell subprocess's own search behavior accordingly.
env["NoDefaultCurrentDirectoryInExePath"] = "1"
else:
# If we're not using a shell, the variable goes in our own environment.
maybe_patch_caller_env = patch_env("NoDefaultCurrentDirectoryInExePath", "1")
else:
cmd_not_found_exception = FileNotFoundError
maybe_patch_caller_env = contextlib.nullcontext()
# END handle

stdout_sink = PIPE if with_stdout else getattr(subprocess, "DEVNULL", None) or open(os.devnull, "wb")
Expand Down

0 comments on commit d2506c7

Please sign in to comment.