From 5df290f6afcd7413bd7c4517544960c006d3fcb1 Mon Sep 17 00:00:00 2001 From: "tandrii@chromium.org" Date: Mon, 11 Apr 2016 16:12:29 +0000 Subject: [PATCH] git cl checkout: implement for Gerrit + add test. R=sergiyb@chromium.org,andybons@chromium.org BUG=579182 Review URL: https://codereview.chromium.org/1873233002 git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@299833 0039d316-1c4b-4281-b951-d872f2087c98 --- git_cl.py | 40 +++++++++++++++++++++++----------------- tests/git_cl_test.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 17 deletions(-) diff --git a/git_cl.py b/git_cl.py index 47c646c20..ad6fe01a2 100755 --- a/git_cl.py +++ b/git_cl.py @@ -1368,8 +1368,12 @@ class _ChangelistCodereviewBase(object): """Returns git config setting for the codereview server.""" raise NotImplementedError() - @staticmethod - def IssueSetting(branch): + @classmethod + def IssueSetting(cls, branch): + return 'branch.%s.%s' % (branch, cls.IssueSettingPrefix()) + + @classmethod + def IssueSettingPrefix(cls): """Returns name of git config setting which stores issue number for a given branch.""" raise NotImplementedError() @@ -1576,9 +1580,9 @@ class _RietveldChangelistImpl(_ChangelistCodereviewBase): self._auth_config or auth.make_auth_config()) return self._rpc_server - @staticmethod - def IssueSetting(branch): - return 'branch.%s.rietveldissue' % branch + @classmethod + def IssueSettingPrefix(cls): + return 'rietveldissue' def PatchsetSetting(self): """Return the git setting that stores this change's most recent patchset.""" @@ -1717,9 +1721,9 @@ class _GerritChangelistImpl(_ChangelistCodereviewBase): self._gerrit_server = 'https://%s' % self._gerrit_host return self._gerrit_server - @staticmethod - def IssueSetting(branch): - return 'branch.%s.gerritissue' % branch + @classmethod + def IssueSettingPrefix(cls): + return 'gerritissue' def PatchsetSetting(self): """Return the git setting that stores this change's most recent patchset.""" @@ -4532,8 +4536,7 @@ def CMDformat(parser, args): @subcommand.usage('') def CMDcheckout(parser, args): - """Checks out a branch associated with a given Rietveld issue.""" - # TODO(tandrii): consider adding this for Gerrit? + """Checks out a branch associated with a given Rietveld or Gerrit issue.""" _, args = parser.parse_args(args) if len(args) != 1: @@ -4546,14 +4549,17 @@ def CMDcheckout(parser, args): return 1 target_issue = str(issue_arg.issue) - key_and_issues = [x.split() for x in RunGit( - ['config', '--local', '--get-regexp', r'branch\..*\.rietveldissue']) - .splitlines()] - branches = [] - for key, issue in key_and_issues: - if issue == target_issue: - branches.append(re.sub(r'branch\.(.*)\.rietveldissue', r'\1', key)) + def find_issues(issueprefix): + key_and_issues = [x.split() for x in RunGit( + ['config', '--local', '--get-regexp', r'branch\..*\.%s' % issueprefix]) + .splitlines()] + for key, issue in key_and_issues: + if issue == target_issue: + yield re.sub(r'branch\.(.*)\.%s' % issueprefix, r'\1', key) + branches = [] + for cls in _CODEREVIEW_IMPLEMENTATIONS.values(): + branches.extend(find_issues(cls.IssueSettingPrefix())) if len(branches) == 0: print 'No branch found for issue %s.' % target_issue return 1 diff --git a/tests/git_cl_test.py b/tests/git_cl_test.py index b8b694fa5..e94e9129a 100755 --- a/tests/git_cl_test.py +++ b/tests/git_cl_test.py @@ -1151,6 +1151,35 @@ class TestGitCl(TestCase): git_cl.main(['patch', 'https://chromium-review.googlesource.com/#/c/123456/1']) + def _checkout_calls(self): + return [ + ((['git', 'config', '--local', '--get-regexp', + 'branch\\..*\\.rietveldissue'], ), + ('branch.retrying.rietveldissue 1111111111\n' + 'branch.some-fix.rietveldissue 2222222222\n')), + ((['git', 'config', '--local', '--get-regexp', + 'branch\\..*\\.gerritissue'], ), + ('branch.ger-branch.gerritissue 123456\n' + 'branch.gbranch654.gerritissue 654321\n')), + ] + + def test_checkout_gerrit(self): + """Tests git cl checkout .""" + self.calls = self._checkout_calls() + self.calls += [((['git', 'checkout', 'ger-branch'], ), '')] + self.assertEqual(0, git_cl.main(['checkout', '123456'])) + + def test_checkout_rietveld(self): + """Tests git cl checkout .""" + self.calls = self._checkout_calls() + self.calls += [((['git', 'checkout', 'some-fix'], ), '')] + self.assertEqual(0, git_cl.main(['checkout', '2222222222'])) + + def test_checkout_not_found(self): + """Tests git cl checkout .""" + self.calls = self._checkout_calls() + self.assertEqual(1, git_cl.main(['checkout', '99999'])) + if __name__ == '__main__': git_cl.logging.basicConfig(