From cb0ca84654f8ae11e629e503b06455e3b531169c Mon Sep 17 00:00:00 2001 From: Tomas Gallucci Date: Fri, 29 Mar 2019 09:44:17 -0500 Subject: [PATCH 1/7] fix fuckup `branch 0v` by... ...deleting branch `0v` and running `git branch -v` as the user intended --- README.md | 1 + tests/rules/test_get_branch_0v_to_dash_v.py | 19 +++++++++++++++++++ thefuck/rules/git_branch_0v_to_dash_v.py | 13 +++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 tests/rules/test_get_branch_0v_to_dash_v.py create mode 100644 thefuck/rules/git_branch_0v_to_dash_v.py diff --git a/README.md b/README.md index 53cfebae9..8363c386d 100644 --- a/README.md +++ b/README.md @@ -202,6 +202,7 @@ following rules are enabled by default: * `git_branch_delete` – changes `git branch -d` to `git branch -D`; * `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_0v_to_dash_v` – undoes `git branch 0v` and runs `git branch -v` in its place; * `git_checkout` – fixes branch name or creates new branch; * `git_commit_amend` – offers `git commit --amend` after previous commit; * `git_commit_reset` – offers `git reset HEAD~` after previous commit; diff --git a/tests/rules/test_get_branch_0v_to_dash_v.py b/tests/rules/test_get_branch_0v_to_dash_v.py new file mode 100644 index 000000000..8eaaade71 --- /dev/null +++ b/tests/rules/test_get_branch_0v_to_dash_v.py @@ -0,0 +1,19 @@ +import pytest +from thefuck.rules.git_branch_0v_to_dash_v import match, get_new_command +from thefuck.types import Command + + +@pytest.fixture +def output(): + return "" + + +def test_match(output): + assert match(Command('git branch 0v', 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_0v_to_dash_v.py b/thefuck/rules/git_branch_0v_to_dash_v.py new file mode 100644 index 000000000..77b0916bb --- /dev/null +++ b/thefuck/rules/git_branch_0v_to_dash_v.py @@ -0,0 +1,13 @@ +from thefuck.shells import shell +from thefuck.specific.git import git_support + + +@git_support +def match(command): + return (command.script_parts + and command.script_parts[1:] == 'branch 0v'.split()) + + +@git_support +def get_new_command(command): + return shell.and_('git branch -D 0v', 'git branch -v') From d42e45f09b27bd38d3afc5a3e935826679e0bfcc Mon Sep 17 00:00:00 2001 From: Tomas Gallucci Date: Mon, 29 Jun 2020 19:46:13 -0500 Subject: [PATCH 2/7] use quotes consistently --- tests/rules/test_get_branch_0v_to_dash_v.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/rules/test_get_branch_0v_to_dash_v.py b/tests/rules/test_get_branch_0v_to_dash_v.py index 8eaaade71..de7ca3a95 100644 --- a/tests/rules/test_get_branch_0v_to_dash_v.py +++ b/tests/rules/test_get_branch_0v_to_dash_v.py @@ -16,4 +16,4 @@ def test_match(output): def test_get_new_command(output): assert get_new_command(Command('git branch 0v', output))\ - == "git branch -D 0v && git branch -v" + == 'git branch -D 0v && git branch -v' From 8151723f8703549a1a3294f36c0f2cef1591b84c Mon Sep 17 00:00:00 2001 From: Tomas Gallucci Date: Tue, 30 Jun 2020 18:37:49 -0500 Subject: [PATCH 3/7] provide new solution implementation based on feedback on PR --- thefuck/rules/git_branch_0v_to_dash_v.py | 27 ++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/thefuck/rules/git_branch_0v_to_dash_v.py b/thefuck/rules/git_branch_0v_to_dash_v.py index 77b0916bb..205f9db17 100644 --- a/thefuck/rules/git_branch_0v_to_dash_v.py +++ b/thefuck/rules/git_branch_0v_to_dash_v.py @@ -1,13 +1,36 @@ 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 0v'.split()) + and command.script_parts[1] == 'branch' + and _supported_flag_fix(command) is not None) @git_support def get_new_command(command): - return shell.and_('git branch -D 0v', 'git branch -v') + fix_parts = _supported_flag_fix(command) + return shell.and_(fix_parts[0], fix_parts[1]) From 9005aff1bd2a1ab0f5ea132391c9927f30e965c6 Mon Sep 17 00:00:00 2001 From: Tomas Gallucci Date: Tue, 30 Jun 2020 18:43:49 -0500 Subject: [PATCH 4/7] rename files to more accurately reflect their more generic contents --- ...h_0v_to_dash_v.py => test_git_branch_flag_0_to_flag_dash_v.py} | 0 ...branch_0v_to_dash_v.py => git_branch_flag_0_to_flag_dash_v.py} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename tests/rules/{test_get_branch_0v_to_dash_v.py => test_git_branch_flag_0_to_flag_dash_v.py} (100%) rename thefuck/rules/{git_branch_0v_to_dash_v.py => git_branch_flag_0_to_flag_dash_v.py} (100%) diff --git a/tests/rules/test_get_branch_0v_to_dash_v.py b/tests/rules/test_git_branch_flag_0_to_flag_dash_v.py similarity index 100% rename from tests/rules/test_get_branch_0v_to_dash_v.py rename to tests/rules/test_git_branch_flag_0_to_flag_dash_v.py diff --git a/thefuck/rules/git_branch_0v_to_dash_v.py b/thefuck/rules/git_branch_flag_0_to_flag_dash_v.py similarity index 100% rename from thefuck/rules/git_branch_0v_to_dash_v.py rename to thefuck/rules/git_branch_flag_0_to_flag_dash_v.py From f66cc0736754d76c3e02093495c2c2cf541c6e4b Mon Sep 17 00:00:00 2001 From: Tomas Gallucci Date: Tue, 30 Jun 2020 18:52:11 -0500 Subject: [PATCH 5/7] update import statement to match new file name --- tests/rules/test_git_branch_flag_0_to_flag_dash_v.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index de7ca3a95..3f3c8c791 100644 --- 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 @@ -1,5 +1,5 @@ import pytest -from thefuck.rules.git_branch_0v_to_dash_v import match, get_new_command +from thefuck.rules.git_branch_flag_0_to_flag_dash_v import match, get_new_command from thefuck.types import Command From e1f0ff44184c6468335aea176dc08fd0ae4d0c1a Mon Sep 17 00:00:00 2001 From: Tomas Gallucci Date: Tue, 30 Jun 2020 18:52:33 -0500 Subject: [PATCH 6/7] update command name in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index eb8e92ced..070ee9759 100644 --- a/README.md +++ b/README.md @@ -203,7 +203,7 @@ following rules are enabled by default: * `git_branch_delete` – changes `git branch -d` to `git branch -D`; * `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_0v_to_dash_v` – undoes `git branch 0v` and runs `git branch -v` in its place; +* `git_branch_flag_0_to_flag_dash_v` – undoes `git branch 0v` and runs `git branch -v` in its place; * `git_checkout` – fixes branch name or creates new branch; * `git_commit_amend` – offers `git commit --amend` after previous commit; * `git_commit_reset` – offers `git reset HEAD~` after previous commit; From a72845be2329dcffd51106ed19ff999293e44359 Mon Sep 17 00:00:00 2001 From: Tomas Gallucci Date: Sat, 4 Jul 2020 01:45:14 -0500 Subject: [PATCH 7/7] separate out matching tests so the pattern is clear for those who add matches in the future --- tests/rules/test_git_branch_flag_0_to_flag_dash_v.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 index 3f3c8c791..21c1e021d 100644 --- 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 @@ -8,8 +8,11 @@ def output(): return "" -def test_match(output): +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))