@@ -985,23 +985,31 @@ def execute(
985985if inline_env is not None :
986986env .update (inline_env )
987987
988+ if shell is None :
989+ shell = self .USE_SHELL
990+
991+ cmd_not_found_exception = FileNotFoundError
992+ maybe_patch_caller_env = contextlib .nullcontext ()
993+
988994if os .name == "nt" :
989- cmd_not_found_exception = OSError
990995if kill_after_timeout is not None :
991996raise GitCommandError (
992997redacted_command ,
993998'"kill_after_timeout" feature is not supported on Windows.' ,
994999 )
995- # Only search PATH, not CWD. This must be in the *caller* environment. The "1" can be any value.
996- maybe_patch_caller_env = patch_env ("NoDefaultCurrentDirectoryInExePath" ,"1" )
997- else :
998- cmd_not_found_exception = FileNotFoundError
999- maybe_patch_caller_env = contextlib .nullcontext ()
1000+
1001+ cmd_not_found_exception = OSError
1002+
1003+ # Search PATH, but do not search CWD. The "1" can be any value.
1004+ if shell :
1005+ # If the direct subprocess is a shell, this must go in its environment.
1006+ env ["NoDefaultCurrentDirectoryInExePath" ]= "1"
1007+ else :
1008+ # If we're not using a shell, the variable goes in our own environment.
1009+ maybe_patch_caller_env = patch_env ("NoDefaultCurrentDirectoryInExePath" ,"1" )
10001010# END handle
10011011
10021012stdout_sink = PIPE if with_stdout else getattr (subprocess ,"DEVNULL" ,None )or open (os .devnull ,"wb" )
1003- if shell is None :
1004- shell = self .USE_SHELL
10051013log .debug (
10061014"Popen(%s, cwd=%s, stdin=%s, shell=%s, universal_newlines=%s)" ,
10071015redacted_command ,