diff --git a/git_cl.py b/git_cl.py index 8a5dc73ec..3acfdbbdf 100755 --- a/git_cl.py +++ b/git_cl.py @@ -4409,6 +4409,8 @@ def CMDpatch(parser, args): RunGit(['branch', '-D', options.newbranch], stderr=subprocess2.PIPE, error_ok=True) RunGit(['new-branch', options.newbranch]) + elif not GetCurrentBranch(): + DieWithError('A branch is required to apply patch. Hint: use -b option.') cl = Changelist(auth_config=auth_config, codereview=options.forced_codereview) diff --git a/tests/git_cl_test.py b/tests/git_cl_test.py index dc689fdcc..d4603ed3d 100755 --- a/tests/git_cl_test.py +++ b/tests/git_cl_test.py @@ -1237,7 +1237,8 @@ class TestGitCl(TestCase): self.mock(git_common, 'is_dirty_git_tree', lambda x: True) self.assertNotEqual(git_cl.main(['diff']), 0) - def _patch_common(self, is_gerrit=False, force_codereview=False): + def _patch_common(self, is_gerrit=False, force_codereview=False, + new_branch=False): self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) self.mock(git_cl._RietveldChangelistImpl, 'GetMostRecentPatchset', lambda x: '60001') @@ -1267,7 +1268,10 @@ class TestGitCl(TestCase): lambda *args: 'Description') self.mock(git_cl, 'IsGitVersionAtLeast', lambda *args: True) - self.calls = self.calls or [] + if new_branch: + self.calls = [((['git', 'new-branch', 'master'],), ''),] + else: + self.calls = [((['git', 'symbolic-ref', 'HEAD'],), 'master')] if not force_codereview: # These calls detect codereview to use. self.calls += [ @@ -1290,8 +1294,8 @@ class TestGitCl(TestCase): ((['sed', '-e', 's|^--- a/|--- |; s|^+++ b/|+++ |'],), ''), ] - def _common_patch_successful(self): - self._patch_common() + def _common_patch_successful(self, new_branch=False): + self._patch_common(new_branch=new_branch) self.calls += [ ((['git', 'apply', '--index', '-p0', '--3way'],), ''), ((['git', 'commit', '-m', @@ -1312,8 +1316,7 @@ class TestGitCl(TestCase): 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._common_patch_successful(new_branch=True) self.assertEqual(git_cl.main(['patch', '-b', 'master', '123456']), 0) def test_patch_conflict(self):