diff --git a/tests/rules/test_git_checkout.py b/tests/rules/test_git_checkout.py index c54be16c..c72b1dcb 100644 --- a/tests/rules/test_git_checkout.py +++ b/tests/rules/test_git_checkout.py @@ -56,18 +56,18 @@ def test_get_branches(branches, branch_list, git_branch): @pytest.mark.parametrize('branches, command, new_command', [ (b'', Command('git checkout unknown', did_not_match('unknown')), - 'git checkout -b unknown'), + ['git checkout -b unknown']), (b'', Command('git commit unknown', did_not_match('unknown')), - 'git branch unknown && git commit unknown'), + ['git branch unknown && git commit unknown']), (b' test-random-branch-123', Command('git checkout tst-rdm-brnch-123', did_not_match('tst-rdm-brnch-123')), - 'git checkout test-random-branch-123'), + ['git checkout test-random-branch-123', 'git checkout -b tst-rdm-brnch-123']), (b' test-random-branch-123', Command('git commit tst-rdm-brnch-123', did_not_match('tst-rdm-brnch-123')), - 'git commit test-random-branch-123')]) + ['git commit test-random-branch-123'])]) def test_get_new_command(branches, command, new_command, git_branch): git_branch(branches) assert get_new_command(command) == new_command diff --git a/thefuck/rules/git_checkout.py b/thefuck/rules/git_checkout.py index 6345cbc3..e4879731 100644 --- a/thefuck/rules/git_checkout.py +++ b/thefuck/rules/git_checkout.py @@ -34,10 +34,16 @@ def get_new_command(command): r"did not match any file\(s\) known to git", command.output)[0] closest_branch = utils.get_closest(missing_file, get_branches(), fallback_to_first=False) + + new_commands = [] + if closest_branch: - return replace_argument(command.script, missing_file, closest_branch) - elif command.script_parts[1] == 'checkout': - return replace_argument(command.script, 'checkout', 'checkout -b') - else: - return shell.and_('git branch {}', '{}').format( - missing_file, command.script) + new_commands.append(replace_argument(command.script, missing_file, closest_branch)) + if command.script_parts[1] == 'checkout': + new_commands.append(replace_argument(command.script, 'checkout', 'checkout -b')) + + if not new_commands: + new_commands.append(shell.and_('git branch {}', '{}').format( + missing_file, command.script)) + + return new_commands