From 799f4127caab4e90755d5924e5f5e8239bbc955d Mon Sep 17 00:00:00 2001 From: Pablo Santiago Blum de Aguiar Date: Thu, 1 Jul 2021 13:41:07 +0200 Subject: [PATCH] #942: Improve git_branch_0flag rule - with a new name --- README.md | 2 +- tests/rules/test_git_branch_0flag.py | 70 +++++++++++++++++++ .../test_git_branch_flag_0_to_flag_dash_v.py | 22 ------ thefuck/rules/git_branch_0flag.py | 24 +++++++ .../rules/git_branch_flag_0_to_flag_dash_v.py | 36 ---------- 5 files changed, 95 insertions(+), 59 deletions(-) create mode 100644 tests/rules/test_git_branch_0flag.py delete mode 100644 tests/rules/test_git_branch_flag_0_to_flag_dash_v.py create mode 100644 thefuck/rules/git_branch_0flag.py delete mode 100644 thefuck/rules/git_branch_flag_0_to_flag_dash_v.py diff --git a/README.md b/README.md index ba83ab02..9c9d893d 100644 --- a/README.md +++ b/README.md @@ -231,7 +231,7 @@ following rules are enabled by default: * `git_branch_delete_checked_out` – changes `git branch -d` to `git checkout master && git branch -D` when trying to delete a checked out branch; * `git_branch_exists` – offers `git branch -d foo`, `git branch -D foo` or `git checkout foo` when creating a branch that already exists; * `git_branch_list` – catches `git branch list` in place of `git branch` and removes created branch; -* `git_branch_flag_0_to_flag_dash_v` – undoes `git branch 0v` and runs `git branch -v` in its place; +* `git_branch_0flag` – fixes commands such as `git branch 0v` and `git branch 0r` removing the created branch; * `git_checkout` – fixes branch name or creates new branch; * `git_clone_git_clone` – replaces `git clone git clone ...` with `git clone ...` * `git_commit_amend` – offers `git commit --amend` after previous commit; diff --git a/tests/rules/test_git_branch_0flag.py b/tests/rules/test_git_branch_0flag.py new file mode 100644 index 00000000..1c96e512 --- /dev/null +++ b/tests/rules/test_git_branch_0flag.py @@ -0,0 +1,70 @@ +import pytest + +from thefuck.rules.git_branch_0flag import get_new_command, match +from thefuck.types import Command + + +@pytest.fixture +def output_branch_exists(): + return "fatal: A branch named 'bar' already exists." + + +@pytest.mark.parametrize( + "script", + [ + "git branch 0a", + "git branch 0d", + "git branch 0f", + "git branch 0r", + "git branch 0v", + "git branch 0d foo", + "git branch 0D foo", + ], +) +def test_match(script, output_branch_exists): + assert match(Command(script, output_branch_exists)) + + +@pytest.mark.parametrize( + "script", + [ + "git branch -a", + "git branch -r", + "git branch -v", + "git branch -d foo", + "git branch -D foo", + ], +) +def test_not_match(script, output_branch_exists): + assert not match(Command(script, "")) + + +@pytest.mark.parametrize( + "script, new_command", + [ + ("git branch 0a", "git branch -D 0a && git branch -a"), + ("git branch 0v", "git branch -D 0v && git branch -v"), + ("git branch 0d foo", "git branch -D 0d && git branch -d foo"), + ("git branch 0D foo", "git branch -D 0D && git branch -D foo"), + ("git branch 0l 'maint-*'", "git branch -D 0l && git branch -l 'maint-*'"), + ("git branch 0u upstream", "git branch -D 0u && git branch -u upstream"), + ], +) +def test_get_new_command_branch_exists(script, output_branch_exists, new_command): + assert get_new_command(Command(script, output_branch_exists)) == new_command + + +@pytest.fixture +def output_not_valid_object(): + return "fatal: Not a valid object name: 'bar'." + + +@pytest.mark.parametrize( + "script, new_command", + [ + ("git branch 0l 'maint-*'", "git branch -l 'maint-*'"), + ("git branch 0u upstream", "git branch -u upstream"), + ], +) +def test_get_new_command_not_valid_object(script, output_not_valid_object, new_command): + assert get_new_command(Command(script, output_not_valid_object)) == new_command diff --git a/tests/rules/test_git_branch_flag_0_to_flag_dash_v.py b/tests/rules/test_git_branch_flag_0_to_flag_dash_v.py deleted file mode 100644 index 21c1e021..00000000 --- a/tests/rules/test_git_branch_flag_0_to_flag_dash_v.py +++ /dev/null @@ -1,22 +0,0 @@ -import pytest -from thefuck.rules.git_branch_flag_0_to_flag_dash_v import match, get_new_command -from thefuck.types import Command - - -@pytest.fixture -def output(): - return "" - - -def test_match_git_branch_0v(output): - assert match(Command('git branch 0v', output)) - - -def test_matches_no__git_branch_0_anything(output): - assert not match(Command('git branch -v', '')) - assert not match(Command('ls', output)) - - -def test_get_new_command(output): - assert get_new_command(Command('git branch 0v', output))\ - == 'git branch -D 0v && git branch -v' diff --git a/thefuck/rules/git_branch_0flag.py b/thefuck/rules/git_branch_0flag.py new file mode 100644 index 00000000..90dbdcb6 --- /dev/null +++ b/thefuck/rules/git_branch_0flag.py @@ -0,0 +1,24 @@ +from thefuck.shells import shell +from thefuck.specific.git import git_support +from thefuck.utils import memoize + + +@memoize +def first_0flag(script_parts): + return next((p for p in script_parts if len(p) == 2 and p.startswith("0")), None) + + +@git_support +def match(command): + return command.script_parts[1] == "branch" and first_0flag(command.script_parts) + + +@git_support +def get_new_command(command): + branch_name = first_0flag(command.script_parts) + fixed_flag = branch_name.replace("0", "-") + fixed_script = command.script.replace(branch_name, fixed_flag) + if "A branch named '" in command.output and "' already exists." in command.output: + delete_branch = u"git branch -D {}".format(branch_name) + return shell.and_(delete_branch, fixed_script) + return fixed_script diff --git a/thefuck/rules/git_branch_flag_0_to_flag_dash_v.py b/thefuck/rules/git_branch_flag_0_to_flag_dash_v.py deleted file mode 100644 index 205f9db1..00000000 --- a/thefuck/rules/git_branch_flag_0_to_flag_dash_v.py +++ /dev/null @@ -1,36 +0,0 @@ -from thefuck.shells import shell -from thefuck.specific.git import git_support -from thefuck.utils import memoize - -''' -keys are fatfingered entry, values are two-element tuples -where the first element is "the fix" and the second element -is "what you meant to do - ''' -# clunky when there's only one key, but as others get added, I _think_ -# this will be cleaner -flags_and_their_fixes = dict() -flags_and_their_fixes["v"] = ('git branch -D 0v', 'git branch -v') - - -@memoize -def _supported_flag_fix(command): - flag = command.script_parts[2:][0] - - if len(flag) == 2 and flag.startswith("0"): - return flags_and_their_fixes[flag[1]] - else: - return None - - -@git_support -def match(command): - return (command.script_parts - and command.script_parts[1] == 'branch' - and _supported_flag_fix(command) is not None) - - -@git_support -def get_new_command(command): - fix_parts = _supported_flag_fix(command) - return shell.and_(fix_parts[0], fix_parts[1])