Commit 7c5efb2a authored by Tibor Goldschwendt's avatar Tibor Goldschwendt Committed by LUCI CQ

Add format string option to git cl archive

This option lets users specify a format of the archive tags. E.g.
  git cl archive -p 'archived/{issue}-{branch}'
makes tags in the format
  archived/1234-foo-feature

Change-Id: Icb74cc68781cda21a70c802bd640543e92ae97a9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2116723Reviewed-by: 's avatarEdward Lesmes <ehmaldonado@chromium.org>
Commit-Queue: Tibor Goldschwendt <tiborg@chromium.org>
parent cba46082
......@@ -3515,12 +3515,12 @@ def upload_branch_deps(cl, args):
return 0
def GetArchiveTagForBranch(issue_num, branch_name, existing_tags):
def GetArchiveTagForBranch(issue_num, branch_name, existing_tags, pattern):
"""Given a proposed tag name, returns a tag name that is guaranteed to be
unique. If 'foo' is proposed but already exists, then 'foo-2' is used,
or 'foo-3', and so on."""
proposed_tag = 'git-cl-archived-%s-%s' % (issue_num, branch_name)
proposed_tag = pattern.format(**{'issue': issue_num, 'branch': branch_name})
for suffix_num in itertools.count(1):
if suffix_num == 1:
to_check = proposed_tag
......@@ -3547,6 +3547,12 @@ def CMDarchive(parser, args):
'-t', '--notags', action='store_true',
help='Do not tag archived branches. '
'Note: local commit history may be lost.')
parser.add_option(
'-p',
'--pattern',
default='git-cl-archived-{issue}-{branch}',
help='Format string for archive tags. '
'E.g. \'archived-{issue}-{branch}\'.')
options, args = parser.parse_args(args)
if args:
......@@ -3568,8 +3574,8 @@ def CMDarchive(parser, args):
fine_grained=True,
max_processes=options.maxjobs)
proposal = [(cl.GetBranch(),
GetArchiveTagForBranch(cl.GetIssue(), cl.GetBranch(),
tags))
GetArchiveTagForBranch(cl.GetIssue(), cl.GetBranch(), tags,
options.pattern))
for cl, status in statuses
if status in ('closed', 'rietveld-not-supported')]
proposal.sort()
......
......@@ -2201,6 +2201,22 @@ class TestGitCl(unittest.TestCase):
self.assertEqual(0, git_cl.main(['archive', '-f']))
def test_archive_with_format(self):
self.calls = [
((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'], ),
'refs/heads/master\nrefs/heads/foo\nrefs/heads/bar'),
((['git', 'for-each-ref', '--format=%(refname)', 'refs/tags'], ), ''),
((['git', 'tag', 'archived/12-foo', 'foo'], ), ''),
((['git', 'branch', '-D', 'foo'], ), ''),
]
mock.patch('git_cl.get_cl_statuses',
lambda branches, fine_grained, max_processes:
[(MockChangelistWithBranchAndIssue('foo', 12), 'closed')]).start()
self.assertEqual(
0, git_cl.main(['archive', '-f', '-p', 'archived/{issue}-{branch}']))
def test_cmd_issue_erase_existing(self):
self.mockGit.config['branch.master.gerritissue'] = '123'
self.mockGit.config['branch.master.gerritserver'] = (
......
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