44# This module is part of GitPython and is released under
55# the BSD License: http://www.opensource.org/licenses/bsd-license.php
66
7- from contextlib import contextmanager
7+ import contextlib
88import io
99import logging
1010import os
1919import threading
2020from collections import OrderedDict
2121from textwrap import dedent
22+ import unittest .mock
2223
2324from git .compat import (
2425 string_types ,
@@ -705,11 +706,15 @@ def execute(self, command,
705706 cmd_not_found_exception = OSError
706707 if kill_after_timeout :
707708 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" })
708712 else :
709713 if sys .version_info [0 ] > 2 :
710714 cmd_not_found_exception = FileNotFoundError # NOQA # exists, flake8 unknown @UndefinedVariable
711715 else :
712716 cmd_not_found_exception = OSError
717+ patch_caller_env = contextlib .nullcontext ()
713718 # end handle
714719
715720 stdout_sink = (PIPE
@@ -721,19 +726,21 @@ def execute(self, command,
721726 log .debug ("Popen(%s, cwd=%s, universal_newlines=%s, shell=%s, istream=%s)" ,
722727 command , cwd , universal_newlines , shell , istream_ok )
723728 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+ )
737744 except cmd_not_found_exception as err :
738745 raise GitCommandNotFound (command , err )
739746
@@ -862,7 +869,7 @@ def update_environment(self, **kwargs):
862869 del self ._environment [key ]
863870 return old_env
864871
865- @contextmanager
872+ @contextlib . contextmanager
866873 def custom_environment (self , ** kwargs ):
867874 """
868875 A context manager around the above ``update_environment`` method to restore the
0 commit comments