4
4
# This module is part of GitPython and is released under
5
5
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
6
6
7
- from contextlib import contextmanager
7
+ import contextlib
8
8
import io
9
9
import logging
10
10
import os
19
19
import threading
20
20
from collections import OrderedDict
21
21
from textwrap import dedent
22
+ import unittest .mock
22
23
23
24
from git .compat import (
24
25
string_types ,
@@ -705,11 +706,15 @@ def execute(self, command,
705
706
cmd_not_found_exception = OSError
706
707
if kill_after_timeout :
707
708
raise GitCommandError (command , '"kill_after_timeout" feature is not supported on Windows.' )
709
+
710
+ # Only search PATH, not CWD. This must be in the *caller* environment. The "1" can be any value.
711
+ patch_caller_env = unittest .mock .patch .dict (os .environ , {"NoDefaultCurrentDirectoryInExePath" : "1" })
708
712
else :
709
713
if sys .version_info [0 ] > 2 :
710
714
cmd_not_found_exception = FileNotFoundError # NOQA # exists, flake8 unknown @UndefinedVariable
711
715
else :
712
716
cmd_not_found_exception = OSError
717
+ patch_caller_env = contextlib .nullcontext ()
713
718
# end handle
714
719
715
720
stdout_sink = (PIPE
@@ -721,19 +726,21 @@ def execute(self, command,
721
726
log .debug ("Popen(%s, cwd=%s, universal_newlines=%s, shell=%s, istream=%s)" ,
722
727
command , cwd , universal_newlines , shell , istream_ok )
723
728
try :
724
- proc = Popen (command ,
725
- env = env ,
726
- cwd = cwd ,
727
- bufsize = - 1 ,
728
- stdin = istream ,
729
- stderr = PIPE ,
730
- stdout = stdout_sink ,
731
- shell = shell is not None and shell or self .USE_SHELL ,
732
- close_fds = is_posix , # unsupported on windows
733
- universal_newlines = universal_newlines ,
734
- creationflags = PROC_CREATIONFLAGS ,
735
- ** subprocess_kwargs
736
- )
729
+ with patch_caller_env :
730
+ proc = Popen (
731
+ command ,
732
+ env = env ,
733
+ cwd = cwd ,
734
+ bufsize = - 1 ,
735
+ stdin = istream ,
736
+ stderr = PIPE ,
737
+ stdout = stdout_sink ,
738
+ shell = shell is not None and shell or self .USE_SHELL ,
739
+ close_fds = is_posix , # unsupported on windows
740
+ universal_newlines = universal_newlines ,
741
+ creationflags = PROC_CREATIONFLAGS ,
742
+ ** subprocess_kwargs ,
743
+ )
737
744
except cmd_not_found_exception as err :
738
745
raise GitCommandNotFound (command , err )
739
746
@@ -862,7 +869,7 @@ def update_environment(self, **kwargs):
862
869
del self ._environment [key ]
863
870
return old_env
864
871
865
- @contextmanager
872
+ @contextlib . contextmanager
866
873
def custom_environment (self , ** kwargs ):
867
874
"""
868
875
A context manager around the above ``update_environment`` method to restore the
0 commit comments