From 7c5efb2a7e2bd90a212b184f1ae441bc24a842f1 Mon Sep 17 00:00:00 2001 From: Tibor Goldschwendt Date: Wed, 25 Mar 2020 01:23:54 +0000 Subject: [PATCH] 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/+/2116723 Reviewed-by: Edward Lesmes Commit-Queue: Tibor Goldschwendt --- git_cl.py | 14 ++++++++++---- tests/git_cl_test.py | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/git_cl.py b/git_cl.py index 84eaa9726..36fd989e3 100755 --- a/git_cl.py +++ b/git_cl.py @@ -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() diff --git a/tests/git_cl_test.py b/tests/git_cl_test.py index 1cf288058..721719fe6 100755 --- a/tests/git_cl_test.py +++ b/tests/git_cl_test.py @@ -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'] = (