Commit d343c635 authored by Aaron Gable's avatar Aaron Gable Committed by Commit Bot

git-cl patch: set metadata before (failing to) apply patch

If the commit or cherry-pick command fails, git automatically drops
the user into a state where they can try to manually fix it. That's
great. But with the old call order, it would also leave the metadata
unset. This sets the issue and patchset number before doing the
potentially-failing heavy lifting, so that the branch will be in a
consistent state after the user finishes fixing up the failed command.

BUG=701130

Change-Id: I792b9fb9e61ba62626c19aa1837d21f8cd8f594e
Reviewed-on: https://chromium-review.googlesource.com/456039Reviewed-by: 's avatarAndrii Shyshkalov <tandrii@chromium.org>
Commit-Queue: Aaron Gable <agable@chromium.org>
parent 837cb3ae
...@@ -2070,12 +2070,12 @@ class _RietveldChangelistImpl(_ChangelistCodereviewBase): ...@@ -2070,12 +2070,12 @@ class _RietveldChangelistImpl(_ChangelistCodereviewBase):
# If we had an issue, commit the current state and register the issue. # If we had an issue, commit the current state and register the issue.
if not nocommit: if not nocommit:
self.SetIssue(self.GetIssue())
self.SetPatchset(patchset)
RunGit(['commit', '-m', (self.GetDescription() + '\n\n' + RunGit(['commit', '-m', (self.GetDescription() + '\n\n' +
'patch from issue %(i)s at patchset ' 'patch from issue %(i)s at patchset '
'%(p)s (http://crrev.com/%(i)s#ps%(p)s)' '%(p)s (http://crrev.com/%(i)s#ps%(p)s)'
% {'i': self.GetIssue(), 'p': patchset})]) % {'i': self.GetIssue(), 'p': patchset})])
self.SetIssue(self.GetIssue())
self.SetPatchset(patchset)
print('Committed patch locally.') print('Committed patch locally.')
else: else:
print('Patch applied to index.') print('Patch applied to index.')
...@@ -2635,9 +2635,9 @@ class _GerritChangelistImpl(_ChangelistCodereviewBase): ...@@ -2635,9 +2635,9 @@ class _GerritChangelistImpl(_ChangelistCodereviewBase):
fetch_info = revision_info['fetch']['http'] fetch_info = revision_info['fetch']['http']
RunGit(['fetch', fetch_info['url'], fetch_info['ref']]) RunGit(['fetch', fetch_info['url'], fetch_info['ref']])
RunGit(['cherry-pick', 'FETCH_HEAD'])
self.SetIssue(self.GetIssue()) self.SetIssue(self.GetIssue())
self.SetPatchset(patchset) self.SetPatchset(patchset)
RunGit(['cherry-pick', 'FETCH_HEAD'])
print('Committed patch for change %i patchset %i locally' % print('Committed patch for change %i patchset %i locally' %
(self.GetIssue(), self.GetPatchset())) (self.GetIssue(), self.GetPatchset()))
return 0 return 0
......
...@@ -1815,16 +1815,16 @@ class TestGitCl(TestCase): ...@@ -1815,16 +1815,16 @@ class TestGitCl(TestCase):
def _common_patch_successful(self, new_branch=False): def _common_patch_successful(self, new_branch=False):
self._patch_common(new_branch=new_branch) self._patch_common(new_branch=new_branch)
self.calls += [ self.calls += [
((['git', 'commit', '-m',
'Description\n\n' +
'patch from issue 123456 at patchset 60001 ' +
'(http://crrev.com/123456#ps60001)'],), ''),
((['git', 'config', 'branch.master.rietveldissue', '123456'],), ((['git', 'config', 'branch.master.rietveldissue', '123456'],),
''), ''),
((['git', 'config', 'branch.master.rietveldserver', ((['git', 'config', 'branch.master.rietveldserver',
'https://codereview.example.com'],), ''), 'https://codereview.example.com'],), ''),
((['git', 'config', 'branch.master.rietveldpatchset', '60001'],), ((['git', 'config', 'branch.master.rietveldpatchset', '60001'],),
''), ''),
((['git', 'commit', '-m',
'Description\n\n' +
'patch from issue 123456 at patchset 60001 ' +
'(http://crrev.com/123456#ps60001)'],), ''),
] ]
def test_patch_successful(self): def test_patch_successful(self):
...@@ -1846,12 +1846,12 @@ class TestGitCl(TestCase): ...@@ -1846,12 +1846,12 @@ class TestGitCl(TestCase):
self.calls += [ self.calls += [
((['git', 'fetch', 'https://chromium.googlesource.com/my/repo', ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo',
'refs/changes/56/123456/7'],), ''), 'refs/changes/56/123456/7'],), ''),
((['git', 'cherry-pick', 'FETCH_HEAD'],), ''),
((['git', 'config', 'branch.master.gerritissue', '123456'],), ((['git', 'config', 'branch.master.gerritissue', '123456'],),
''), ''),
((['git', 'config', 'branch.master.gerritserver', ((['git', 'config', 'branch.master.gerritserver',
'https://chromium-review.googlesource.com'],), ''), 'https://chromium-review.googlesource.com'],), ''),
((['git', 'config', 'branch.master.gerritpatchset', '7'],), ''), ((['git', 'config', 'branch.master.gerritpatchset', '7'],), ''),
((['git', 'cherry-pick', 'FETCH_HEAD'],), ''),
] ]
self.assertEqual(git_cl.main(['patch', '123456']), 0) self.assertEqual(git_cl.main(['patch', '123456']), 0)
...@@ -1861,12 +1861,12 @@ class TestGitCl(TestCase): ...@@ -1861,12 +1861,12 @@ class TestGitCl(TestCase):
self.calls += [ self.calls += [
((['git', 'fetch', 'https://host.googlesource.com/my/repo', ((['git', 'fetch', 'https://host.googlesource.com/my/repo',
'refs/changes/56/123456/7'],), ''), 'refs/changes/56/123456/7'],), ''),
((['git', 'cherry-pick', 'FETCH_HEAD'],), ''),
((['git', 'config', 'branch.master.gerritissue', '123456'],), ((['git', 'config', 'branch.master.gerritissue', '123456'],),
''), ''),
((['git', 'config', 'branch.master.gerritserver', ((['git', 'config', 'branch.master.gerritserver',
'https://host-review.googlesource.com'],), ''), 'https://host-review.googlesource.com'],), ''),
((['git', 'config', 'branch.master.gerritpatchset', '7'],), ''), ((['git', 'config', 'branch.master.gerritpatchset', '7'],), ''),
((['git', 'cherry-pick', 'FETCH_HEAD'],), ''),
] ]
self.assertEqual(git_cl.main(['patch', '--gerrit', '123456']), 0) self.assertEqual(git_cl.main(['patch', '--gerrit', '123456']), 0)
...@@ -1876,12 +1876,12 @@ class TestGitCl(TestCase): ...@@ -1876,12 +1876,12 @@ class TestGitCl(TestCase):
self.calls += [ self.calls += [
((['git', 'fetch', 'https://else.googlesource.com/my/repo', ((['git', 'fetch', 'https://else.googlesource.com/my/repo',
'refs/changes/56/123456/1'],), ''), 'refs/changes/56/123456/1'],), ''),
((['git', 'cherry-pick', 'FETCH_HEAD'],), ''),
((['git', 'config', 'branch.master.gerritissue', '123456'],), ((['git', 'config', 'branch.master.gerritissue', '123456'],),
''), ''),
((['git', 'config', 'branch.master.gerritserver', ((['git', 'config', 'branch.master.gerritserver',
'https://else-review.googlesource.com'],), ''), 'https://else-review.googlesource.com'],), ''),
((['git', 'config', 'branch.master.gerritpatchset', '1'],), ''), ((['git', 'config', 'branch.master.gerritpatchset', '1'],), ''),
((['git', 'cherry-pick', 'FETCH_HEAD'],), ''),
] ]
self.assertEqual(git_cl.main( self.assertEqual(git_cl.main(
['patch', 'https://else-review.googlesource.com/#/c/123456/1']), 0) ['patch', 'https://else-review.googlesource.com/#/c/123456/1']), 0)
...@@ -1892,6 +1892,11 @@ class TestGitCl(TestCase): ...@@ -1892,6 +1892,11 @@ class TestGitCl(TestCase):
self.calls += [ self.calls += [
((['git', 'fetch', 'https://chromium.googlesource.com/my/repo', ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo',
'refs/changes/56/123456/1'],), ''), 'refs/changes/56/123456/1'],), ''),
((['git', 'config', 'branch.master.gerritissue', '123456'],),
''),
((['git', 'config', 'branch.master.gerritserver',
'https://chromium-review.googlesource.com'],), ''),
((['git', 'config', 'branch.master.gerritpatchset', '1'],), ''),
((['git', 'cherry-pick', 'FETCH_HEAD'],), CERR1), ((['git', 'cherry-pick', 'FETCH_HEAD'],), CERR1),
((['DieWithError', 'Command "git cherry-pick FETCH_HEAD" failed.\n'],), ((['DieWithError', 'Command "git cherry-pick FETCH_HEAD" failed.\n'],),
SystemExitMock()), SystemExitMock()),
......
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