From 027b41da593a5a6369243bb1328e0f613ac1863d Mon Sep 17 00:00:00 2001 From: Joseph Frazier <1212jtraceur@gmail.com> Date: Tue, 16 Jan 2018 20:03:56 -0500 Subject: [PATCH] Add `git_merge_unrelated` rule for `git merge --allow-unrelated-histories` (#773) From https://git-scm.com/docs/merge-options#merge-options---allow-unrelated-histories > By default, `git merge` command refuses to merge histories that do not share a common ancestor. This option can be used to override this safety when merging histories of two projects that started their lives independently. --- README.md | 1 + tests/rules/test_git_merge_unrelated.py | 25 +++++++++++++++++++++++++ thefuck/rules/git_merge_unrelated.py | 12 ++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 tests/rules/test_git_merge_unrelated.py create mode 100644 thefuck/rules/git_merge_unrelated.py diff --git a/README.md b/README.md index ec6549b..27d2d4a 100644 --- a/README.md +++ b/README.md @@ -200,6 +200,7 @@ using the matched rule and runs it. Rules enabled by default are as follows: * `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_merge_unrelated` – adds `--allow-unrelated-histories` when required * `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_unrelated.py b/tests/rules/test_git_merge_unrelated.py new file mode 100644 index 0000000..c9a2b9e --- /dev/null +++ b/tests/rules/test_git_merge_unrelated.py @@ -0,0 +1,25 @@ +import pytest +from thefuck.rules.git_merge_unrelated import match, get_new_command +from thefuck.types import Command + + +@pytest.fixture +def output(): + return 'fatal: refusing to merge unrelated histories' + + +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 local --allow-unrelated-histories'), + (Command('git merge -m "test" local', output()), + 'git merge -m "test" local --allow-unrelated-histories'), + (Command('git merge -m "test local" local', output()), + 'git merge -m "test local" local --allow-unrelated-histories')]) +def test_get_new_command(command, new_command): + assert get_new_command(command) == new_command diff --git a/thefuck/rules/git_merge_unrelated.py b/thefuck/rules/git_merge_unrelated.py new file mode 100644 index 0000000..8e32642 --- /dev/null +++ b/thefuck/rules/git_merge_unrelated.py @@ -0,0 +1,12 @@ +from thefuck.specific.git import git_support + + +@git_support +def match(command): + return ('merge' in command.script + and 'fatal: refusing to merge unrelated histories' in command.output) + + +@git_support +def get_new_command(command): + return command.script + ' --allow-unrelated-histories'