Fix & refactor git cl patch.

* Fixes a bug when issue was incorrectly over-written
  in another branch. Add a test case.
* Refactor for better flow.
* Update outdated errors on wrong arguments.

BUG=616105
R=andybons@chromium.org,sergiyb@chromium.org

Review-Url: https://codereview.chromium.org/2022183003

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@300681 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/tests/git_cl_test.py b/tests/git_cl_test.py
index 82a404c..3924b22 100755
--- a/tests/git_cl_test.py
+++ b/tests/git_cl_test.py
@@ -1109,14 +1109,6 @@
   def test_patch_when_dirty(self):
     # Patch when local tree is dirty
     self.mock(git_common, 'is_dirty_git_tree', lambda x: True)
-    self.calls = [
-      ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
-      ((['git', 'config', 'branch.master.rietveldissue'],), ''),
-      ((['git', 'config', 'branch.master.gerritissue'],), ''),
-      ((['git', 'config', 'rietveld.autoupdate'],), ''),
-      ((['git', 'config', 'gerrit.host'],), ''),
-      ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'),
-    ]
     self.assertNotEqual(git_cl.main(['patch', '123456']), 0)
 
   def test_diff_when_dirty(self):
@@ -1154,16 +1146,15 @@
               lambda *args: 'Description')
     self.mock(git_cl, 'IsGitVersionAtLeast', lambda *args: True)
 
+    self.calls = self.calls or []
     if not force_codereview:
       # These calls detect codereview to use.
-      self.calls = [
+      self.calls += [
         ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
         ((['git', 'config', 'branch.master.rietveldissue'],), ''),
         ((['git', 'config', 'branch.master.gerritissue'],), ''),
         ((['git', 'config', 'rietveld.autoupdate'],), ''),
       ]
-    else:
-      self.calls = []
 
     if is_gerrit:
       if not force_codereview:
@@ -1178,7 +1169,7 @@
         ((['sed', '-e', 's|^--- a/|--- |; s|^+++ b/|+++ |'],), ''),
       ]
 
-  def test_patch_successful(self):
+  def _common_patch_successful(self):
     self._patch_common()
     self.calls += [
       ((['git', 'apply', '--index', '-p0', '--3way'],), ''),
@@ -1192,8 +1183,16 @@
          'https://codereview.example.com'],), ''),
       ((['git', 'config', 'branch.master.rietveldpatchset', '60001'],), ''),
     ]
+
+  def test_patch_successful(self):
+    self._common_patch_successful()
     self.assertEqual(git_cl.main(['patch', '123456']), 0)
 
+  def test_patch_successful_new_branch(self):
+    self.calls = [ ((['git', 'new-branch', 'master'],), ''), ]
+    self._common_patch_successful()
+    self.assertEqual(git_cl.main(['patch', '-b', 'master', '123456']), 0)
+
   def test_patch_conflict(self):
     self._patch_common()
     self.calls += [