Skip to content

Commit 5e70dfa

Browse files
scorphusnvbn
authored andcommitted
#N/A: Add new git_branch_delete_checked_out rule (nvbn#985)
1 parent 5f292e9 commit 5e70dfa

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ following rules are enabled by default:
201201
* `git_add_force` &ndash; adds `--force` to `git add <pathspec>...` when paths are .gitignore'd;
202202
* `git_bisect_usage` &ndash; fixes `git bisect strt`, `git bisect goood`, `git bisect rset`, etc. when bisecting;
203203
* `git_branch_delete` &ndash; changes `git branch -d` to `git branch -D`;
204+
* `git_branch_delete_checked_out` &ndash; changes `git branch -d` to `git checkout master && git branch -D` when trying to delete a checked out branch;
204205
* `git_branch_exists` &ndash; offers `git branch -d foo`, `git branch -D foo` or `git checkout foo` when creating a branch that already exists;
205206
* `git_branch_list` &ndash; catches `git branch list` in place of `git branch` and removes created branch;
206207
* `git_checkout` &ndash; fixes branch name or creates new branch;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import pytest
2+
from thefuck.rules.git_branch_delete_checked_out import match, get_new_command
3+
from thefuck.types import Command
4+
5+
6+
@pytest.fixture
7+
def output():
8+
return "error: Cannot delete branch 'foo' checked out at '/bar/foo'"
9+
10+
11+
@pytest.mark.parametrize("script", ["git branch -d foo", "git branch -D foo"])
12+
def test_match(script, output):
13+
assert match(Command(script, output))
14+
15+
16+
@pytest.mark.parametrize("script", ["git branch -d foo", "git branch -D foo"])
17+
def test_not_match(script):
18+
assert not match(Command(script, "Deleted branch foo (was a1b2c3d)."))
19+
20+
21+
@pytest.mark.parametrize(
22+
"script, new_command",
23+
[
24+
("git branch -d foo", "git checkout master && git branch -D foo"),
25+
("git branch -D foo", "git checkout master && git branch -D foo"),
26+
],
27+
)
28+
def test_get_new_command(script, new_command, output):
29+
assert get_new_command(Command(script, output)) == new_command
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from thefuck.shells import shell
2+
from thefuck.specific.git import git_support
3+
from thefuck.utils import replace_argument
4+
5+
6+
@git_support
7+
def match(command):
8+
return (
9+
("branch -d" in command.script or "branch -D" in command.script)
10+
and "error: Cannot delete branch '" in command.output
11+
and "' checked out at '" in command.output
12+
)
13+
14+
15+
@git_support
16+
def get_new_command(command):
17+
return shell.and_("git checkout master", "{}").format(
18+
replace_argument(command.script, "-d", "-D")
19+
)

0 commit comments

Comments
 (0)