Skip to content

Commit 05706e4

Browse files
committed
nvbn#942: Improve git_branch_0flag rule - with a new name
1 parent 24576b3 commit 05706e4

File tree

5 files changed

+95
-59
lines changed

5 files changed

+95
-59
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ following rules are enabled by default:
231231
* `git_branch_delete_checked_out` – changes `git branch -d` to `git checkout master && git branch -D` when trying to delete a checked out branch;
232232
* `git_branch_exists` – offers `git branch -d foo`, `git branch -D foo` or `git checkout foo` when creating a branch that already exists;
233233
* `git_branch_list` – catches `git branch list` in place of `git branch` and removes created branch;
234-
* `git_branch_flag_0_to_flag_dash_v` – undoes `git branch 0v` and runs `git branch -v` in its place;
234+
* `git_branch_0flag` – fixes commands such as `git branch 0v` and `git branch 0r` removing the created branch;
235235
* `git_checkout` – fixes branch name or creates new branch;
236236
* `git_clone_git_clone` – replaces `git clone git clone ...` with `git clone ...`
237237
* `git_commit_amend` – offers `git commit --amend` after previous commit;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import pytest
2+
3+
from thefuck.rules.git_branch_0flag import get_new_command, match
4+
from thefuck.types import Command
5+
6+
7+
@pytest.fixture
8+
def output_branch_exists():
9+
return "fatal: A branch named 'bar' already exists."
10+
11+
12+
@pytest.mark.parametrize(
13+
"script",
14+
[
15+
"git branch 0a",
16+
"git branch 0d",
17+
"git branch 0f",
18+
"git branch 0r",
19+
"git branch 0v",
20+
"git branch 0d foo",
21+
"git branch 0D foo",
22+
],
23+
)
24+
def test_match(script, output_branch_exists):
25+
assert match(Command(script, output_branch_exists))
26+
27+
28+
@pytest.mark.parametrize(
29+
"script",
30+
[
31+
"git branch -a",
32+
"git branch -r",
33+
"git branch -v",
34+
"git branch -d foo",
35+
"git branch -D foo",
36+
],
37+
)
38+
def test_not_match(script, output_branch_exists):
39+
assert not match(Command(script, ""))
40+
41+
42+
@pytest.mark.parametrize(
43+
"script, new_command",
44+
[
45+
("git branch 0a", "git branch -D 0a && git branch -a"),
46+
("git branch 0v", "git branch -D 0v && git branch -v"),
47+
("git branch 0d foo", "git branch -D 0d && git branch -d foo"),
48+
("git branch 0D foo", "git branch -D 0D && git branch -D foo"),
49+
("git branch 0l 'maint-*'", "git branch -D 0l && git branch -l 'maint-*'"),
50+
("git branch 0u upstream", "git branch -D 0u && git branch -u upstream"),
51+
],
52+
)
53+
def test_get_new_command_branch_exists(script, output_branch_exists, new_command):
54+
assert get_new_command(Command(script, output_branch_exists)) == new_command
55+
56+
57+
@pytest.fixture
58+
def output_not_valid_object():
59+
return "fatal: Not a valid object name: 'bar'."
60+
61+
62+
@pytest.mark.parametrize(
63+
"script, new_command",
64+
[
65+
("git branch 0l 'maint-*'", "git branch -l 'maint-*'"),
66+
("git branch 0u upstream", "git branch -u upstream"),
67+
],
68+
)
69+
def test_get_new_command_not_valid_object(script, output_not_valid_object, new_command):
70+
assert get_new_command(Command(script, output_not_valid_object)) == new_command

tests/rules/test_git_branch_flag_0_to_flag_dash_v.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

thefuck/rules/git_branch_0flag.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from thefuck.shells import shell
2+
from thefuck.specific.git import git_support
3+
from thefuck.utils import memoize
4+
5+
6+
@memoize
7+
def first_0flag(script_parts):
8+
return next((p for p in script_parts if len(p) == 2 and p.startswith("0")), None)
9+
10+
11+
@git_support
12+
def match(command):
13+
return command.script_parts[1] == "branch" and first_0flag(command.script_parts)
14+
15+
16+
@git_support
17+
def get_new_command(command):
18+
branch_name = first_0flag(command.script_parts)
19+
fixed_flag = branch_name.replace("0", "-")
20+
fixed_script = command.script.replace(branch_name, fixed_flag)
21+
if "A branch named '" in command.output and "' already exists." in command.output:
22+
delete_branch = u"git branch -D {}".format(branch_name)
23+
return shell.and_(delete_branch, fixed_script)
24+
return fixed_script

thefuck/rules/git_branch_flag_0_to_flag_dash_v.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)