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/456039
Reviewed-by: Andrii Shyshkalov <tandrii@chromium.org>
Commit-Queue: Aaron Gable <agable@chromium.org>
diff --git a/git_cl.py b/git_cl.py
index b0f169f..8dea2d2 100755
--- a/git_cl.py
+++ b/git_cl.py
@@ -2070,12 +2070,12 @@
 
     # If we had an issue, commit the current state and register the issue.
     if not nocommit:
+      self.SetIssue(self.GetIssue())
+      self.SetPatchset(patchset)
       RunGit(['commit', '-m', (self.GetDescription() + '\n\n' +
                                'patch from issue %(i)s at patchset '
                                '%(p)s (http://crrev.com/%(i)s#ps%(p)s)'
                                % {'i': self.GetIssue(), 'p': patchset})])
-      self.SetIssue(self.GetIssue())
-      self.SetPatchset(patchset)
       print('Committed patch locally.')
     else:
       print('Patch applied to index.')
@@ -2635,9 +2635,9 @@
 
     fetch_info = revision_info['fetch']['http']
     RunGit(['fetch', fetch_info['url'], fetch_info['ref']])
-    RunGit(['cherry-pick', 'FETCH_HEAD'])
     self.SetIssue(self.GetIssue())
     self.SetPatchset(patchset)
+    RunGit(['cherry-pick', 'FETCH_HEAD'])
     print('Committed patch for change %i patchset %i locally' %
           (self.GetIssue(), self.GetPatchset()))
     return 0