diff --git a/README.md b/README.md index 5f5e310..4a8b5bf 100644 --- a/README.md +++ b/README.md @@ -196,6 +196,7 @@ using the matched rule and runs it. Rules enabled by default are as follows: * `git_pull_clone` – clones instead of pulling when the repo does not exist; * `git_pull_uncommitted_changes` – stashes changes before pulling and pops them afterwards; * `git_push` – adds `--set-upstream origin $branch` to previous failed `git push`; +* `git_push_different_branch_names` – fixes pushes when local brach name does not match remote branch name; * `git_push_pull` – runs `git pull` when `push` was rejected; * `git_push_without_commits` – Creates an initial commit if you forget and only `git add .`, when setting up a new project; * `git_rebase_no_changes` – runs `git rebase --skip` instead of `git rebase --continue` when there are no changes; diff --git a/tests/rules/test_git_push_different_branch_names.py b/tests/rules/test_git_push_different_branch_names.py new file mode 100644 index 0000000..626c47e --- /dev/null +++ b/tests/rules/test_git_push_different_branch_names.py @@ -0,0 +1,39 @@ +import pytest +from thefuck.rules.git_push_different_branch_names import get_new_command, match +from thefuck.types import Command + + +output = """fatal: The upstream branch of your current branch does not match +the name of your current branch. To push to the upstream branch +on the remote, use + + git push origin HEAD:%s + +To push to the branch of the same name on the remote, use + + git push origin %s + +To choose either option permanently, see push.default in 'git help config'. +""" + + +def error_msg(localbranch, remotebranch): + return output % (remotebranch, localbranch) + + +def test_match(): + assert match(Command('git push', error_msg('foo', 'bar'))) + + +@pytest.mark.parametrize('command', [ + Command('vim', ''), + Command('git status', error_msg('foo', 'bar')), + Command('git push', '') +]) +def test_not_match(command): + assert not match(command) + + +def test_get_new_command(): + new_command = get_new_command(Command('git push', error_msg('foo', 'bar'))) + assert new_command == 'git push origin HEAD:bar' diff --git a/thefuck/rules/git_push_different_branch_names.py b/thefuck/rules/git_push_different_branch_names.py new file mode 100644 index 0000000..3ec5a40 --- /dev/null +++ b/thefuck/rules/git_push_different_branch_names.py @@ -0,0 +1,12 @@ +import re +from thefuck.specific.git import git_support + + +@git_support +def match(command): + return "push" in command.script and "The upstream branch of your current branch does not match" in command.output + + +@git_support +def get_new_command(command): + return re.findall(r'^ +(git push [^\s]+ [^\s]+)', command.output, re.MULTILINE)[0]