Commit 5df290f6 authored by tandrii@chromium.org's avatar tandrii@chromium.org

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
parent c9f2c3fa
......@@ -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('<codereview url or issue id>')
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
......
......@@ -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 <issue>."""
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 <issue>."""
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 <issue>."""
self.calls = self._checkout_calls()
self.assertEqual(1, git_cl.main(['checkout', '99999']))
if __name__ == '__main__':
git_cl.logging.basicConfig(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment