1
0
mirror of https://github.com/nvbn/thefuck.git synced 2024-10-05 18:31:10 +01:00

#942: Improve git_branch_0flag rule - with a new name

This commit is contained in:
Pablo Santiago Blum de Aguiar 2021-07-01 13:41:07 +02:00 committed by Pablo Aguiar
parent fe1942866b
commit 799f4127ca
5 changed files with 95 additions and 59 deletions

View File

@ -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;

View File

@ -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

View File

@ -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'

View File

@ -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

View File

@ -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])