diff --git a/gclient_scm.py b/gclient_scm.py index 007c5164d..a10a56d21 100644 --- a/gclient_scm.py +++ b/gclient_scm.py @@ -1259,10 +1259,18 @@ class GitWrapper(SCMWrapper): return branch def _Capture(self, args, **kwargs): + set_git_dir = 'cwd' not in kwargs kwargs.setdefault('cwd', self.checkout_path) kwargs.setdefault('stderr', subprocess2.PIPE) strip = kwargs.pop('strip', True) env = scm.GIT.ApplyEnvVars(kwargs) + # If an explicit cwd isn't set, then default to the .git/ subdir so we get + # stricter behavior. This can be useful in cases of slight corruption -- + # we don't accidentally go corrupting parent git checks too. See + # https://crbug.com/1000825 for an example. + if set_git_dir: + env.setdefault('GIT_DIR', os.path.abspath(os.path.join( + self.checkout_path, '.git'))) ret = subprocess2.check_output( ['git'] + args, env=env, **kwargs).decode('utf-8') if strip: