From 80a9ef1e126c0f65388a771877b799a3cd6933c1 Mon Sep 17 00:00:00 2001 From: "maruel@chromium.org" Date: Tue, 13 Dec 2011 20:44:10 +0000 Subject: [PATCH] Enforce cwd for GIT functions GIT.CaptureStatus was assuming os.getcwd() was using all the time, which isn't true when using trychange.py --sub_rep XXX. R=dpranke@chromium.org TEST= BUG= Review URL: http://codereview.chromium.org/8930002 git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@114264 0039d316-1c4b-4281-b951-d872f2087c98 --- git_cl.py | 2 +- git_try.py | 8 +++++--- scm.py | 19 ++++++++++--------- trychange.py | 14 +++++++++----- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/git_cl.py b/git_cl.py index 387aa9b01..a8669a373 100755 --- a/git_cl.py +++ b/git_cl.py @@ -500,7 +500,7 @@ or verify this branch is set up to track another (via the --track argument to name = RunCommand(['git', 'rev-parse', 'HEAD']).strip() # Need to pass a relative path for msysgit. try: - files = scm.GIT.CaptureStatus([root], upstream_branch) + files = scm.GIT.CaptureStatus([root], '.', upstream_branch) except subprocess2.CalledProcessError: DieWithError( ('\nFailed to diff against upstream branch %s!\n\n' diff --git a/git_try.py b/git_try.py index a36a19f24..45a5217e6 100755 --- a/git_try.py +++ b/git_try.py @@ -19,7 +19,8 @@ import git_cl def GetRietveldIssueNumber(): try: return GIT.Capture( - ['config', 'branch.%s.rietveldissue' % GIT.GetBranch(None)]).strip() + ['config', 'branch.%s.rietveldissue' % GIT.GetBranch('.')], + '.').strip() except subprocess2.CalledProcessError: return None @@ -27,14 +28,15 @@ def GetRietveldIssueNumber(): def GetRietveldPatchsetNumber(): try: return GIT.Capture( - ['config', 'branch.%s.rietveldpatchset' % GIT.GetBranch(None)]).strip() + ['config', 'branch.%s.rietveldpatchset' % GIT.GetBranch('.')], + '.').strip() except subprocess2.CalledProcessError: return None def GetRietveldServerUrl(): try: - return GIT.Capture(['config', 'rietveld.server']).strip() + return GIT.Capture(['config', 'rietveld.server'], '.').strip() except subprocess2.CalledProcessError: return None diff --git a/scm.py b/scm.py index 56e795fcf..b1f2e653e 100644 --- a/scm.py +++ b/scm.py @@ -97,19 +97,19 @@ class GIT(object): current_version = None @staticmethod - def Capture(args, **kwargs): + def Capture(args, cwd, **kwargs): return subprocess2.check_output( - ['git'] + args, stderr=subprocess2.PIPE, **kwargs) + ['git'] + args, cwd=cwd, stderr=subprocess2.PIPE, **kwargs) @staticmethod - def CaptureStatus(files, upstream_branch=None): + def CaptureStatus(files, cwd, upstream_branch): """Returns git status. @files can be a string (one file) or a list of files. Returns an array of (status, file) tuples.""" if upstream_branch is None: - upstream_branch = GIT.GetUpstreamBranch(os.getcwd()) + upstream_branch = GIT.GetUpstreamBranch(cwd) if upstream_branch is None: raise gclient_utils.Error('Cannot determine upstream branch') command = ['diff', '--name-status', '-r', '%s...' % upstream_branch] @@ -119,7 +119,7 @@ class GIT(object): command.append(files) else: command.extend(files) - status = GIT.Capture(command).rstrip() + status = GIT.Capture(command, cwd).rstrip() results = [] if status: for statusline in status.splitlines(): @@ -226,7 +226,7 @@ class GIT(object): # pipe at a time. # The -100 is an arbitrary limit so we don't search forever. cmd = ['git', 'log', '-100', '--pretty=medium'] - proc = subprocess2.Popen(cmd, stdout=subprocess2.PIPE) + proc = subprocess2.Popen(cmd, cwd, stdout=subprocess2.PIPE) url = None for line in proc.stdout: match = git_svn_re.match(line) @@ -237,8 +237,9 @@ class GIT(object): if url: svn_remote_re = re.compile(r'^svn-remote\.([^.]+)\.url (.*)$') - remotes = GIT.Capture(['config', '--get-regexp', - r'^svn-remote\..*\.url'], cwd=cwd).splitlines() + remotes = GIT.Capture( + ['config', '--get-regexp', r'^svn-remote\..*\.url'], + cwd=cwd).splitlines() for remote in remotes: match = svn_remote_re.match(remote) if match: @@ -383,7 +384,7 @@ class GIT(object): def AssertVersion(cls, min_version): """Asserts git's version is at least min_version.""" if cls.current_version is None: - cls.current_version = cls.Capture(['--version']).split()[-1] + cls.current_version = cls.Capture(['--version'], '.').split()[-1] current_version_list = map(only_int, cls.current_version.split('.')) for min_ver in map(int, min_version.split('.')): ver = current_version_list.pop(0) diff --git a/trychange.py b/trychange.py index cdd17ad32..d227472e7 100755 --- a/trychange.py +++ b/trychange.py @@ -275,13 +275,17 @@ class GIT(SCM): logging.info("GIT(%s)" % self.checkout_root) def CaptureStatus(self): - return scm.GIT.CaptureStatus(self.checkout_root.replace(os.sep, '/'), - self.diff_against) + return scm.GIT.CaptureStatus( + [], + self.checkout_root.replace(os.sep, '/'), + self.diff_against) def GenerateDiff(self): - return scm.GIT.GenerateDiff(self.checkout_root, files=self.files, - full_move=True, - branch=self.diff_against) + return scm.GIT.GenerateDiff( + self.checkout_root, + files=self.files, + full_move=True, + branch=self.diff_against) def _ParseSendChangeOptions(options):