From f700b23f5725e14f8ee6ffb0a5c44ab9eaf42b53 Mon Sep 17 00:00:00 2001 From: David Hart Date: Tue, 2 Jan 2018 16:47:48 +0000 Subject: [PATCH] Add git merge rule (#755) This fixes https://github.com/nvbn/thefuck/issues/629 --- README.md | 1 + tests/rules/test_git_merge.py | 26 ++++++++++++++++++++++++++ thefuck/rules/git_merge.py | 18 ++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 tests/rules/test_git_merge.py create mode 100644 thefuck/rules/git_merge.py diff --git a/README.md b/README.md index 26ba5ab3..e686d063 100644 --- a/README.md +++ b/README.md @@ -191,6 +191,7 @@ using the matched rule and runs it. Rules enabled by default are as follows: * `git_fix_stash` – fixes `git stash` commands (misspelled subcommand and missing `save`); * `git_flag_after_filename` – fixes `fatal: bad flag '...' after filename` * `git_help_aliased` – fixes `git help ` commands replacing with the aliased command; +* `git_merge` – adds remote to branch names; * `git_not_command` – fixes wrong git commands like `git brnch`; * `git_pull` – sets upstream before executing previous `git pull`; * `git_pull_clone` – clones instead of pulling when the repo does not exist; diff --git a/tests/rules/test_git_merge.py b/tests/rules/test_git_merge.py new file mode 100644 index 00000000..c347a59f --- /dev/null +++ b/tests/rules/test_git_merge.py @@ -0,0 +1,26 @@ +import pytest +from thefuck.rules.git_merge import match, get_new_command +from thefuck.types import Command + + +@pytest.fixture +def output(): + return 'merge: local - not something we can merge\n\n' \ + 'Did you mean this?\n\tremote/local' + + +def test_match(output): + assert match(Command('git merge test', output)) + assert not match(Command('git merge master', '')) + assert not match(Command('ls', output)) + + +@pytest.mark.parametrize('command, new_command', [ + (Command('git merge local', output()), + 'git merge remote/local'), + (Command('git merge -m "test" local', output()), + 'git merge -m "test" remote/local'), + (Command('git merge -m "test local" local', output()), + 'git merge -m "test local" remote/local')]) +def test_get_new_command(command, new_command): + assert get_new_command(command) == new_command diff --git a/thefuck/rules/git_merge.py b/thefuck/rules/git_merge.py new file mode 100644 index 00000000..2420b67d --- /dev/null +++ b/thefuck/rules/git_merge.py @@ -0,0 +1,18 @@ +import re +from thefuck.utils import replace_argument +from thefuck.specific.git import git_support + + +@git_support +def match(command): + return ('merge' in command.script + and ' - not something we can merge' in command.output + and 'Did you mean this?' in command.output) + + +@git_support +def get_new_command(command): + unknown_branch = re.findall(r'merge: (.+) - not something we can merge', command.output)[0] + remote_branch = re.findall(r'Did you mean this\?\n\t([^\n]+)', command.output)[0] + + return replace_argument(command.script, unknown_branch, remote_branch)