Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checkout to default branch instead of master #1362

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 18 additions & 2 deletions tests/rules/test_git_branch_delete_checked_out.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from mock import patch

import pytest

from thefuck.rules.git_branch_delete_checked_out import match, get_new_command
from thefuck.types import Command

Expand All @@ -25,5 +28,18 @@ def test_not_match(script):
("git branch -D foo", "git checkout master && git branch -D foo"),
],
)
def test_get_new_command(script, new_command, output):
assert get_new_command(Command(script, output)) == new_command
def test_get_new_command_deletion_flag(script, new_command, output):
with patch('thefuck.rules.git_branch_delete_checked_out.get_sp_stdout', return_value='master'):
assert get_new_command(Command(script, output)) == new_command


@pytest.mark.parametrize(
"script, default_branch",
[
("git branch -d foo", "main"),
("git branch -d foo", "bar"),
],
)
def test_get_new_command_default_branch(script, default_branch, output):
with patch('thefuck.rules.git_branch_delete_checked_out.get_sp_stdout', return_value=default_branch):
assert get_new_command(Command(script, output)) == "git checkout {default_branch} && git branch -D foo".format(default_branch=default_branch)
13 changes: 12 additions & 1 deletion thefuck/rules/git_branch_delete_checked_out.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import subprocess as sp

from thefuck.shells import shell
from thefuck.specific.git import git_support
from thefuck.utils import replace_argument


STDOUT_INDEX = 0


@git_support
def match(command):
return (
Expand All @@ -12,8 +17,14 @@ def match(command):
)


def get_sp_stdout(command):
return sp.Popen(command, stdout=sp.PIPE, shell=True).communicate()[STDOUT_INDEX].strip().decode("utf-8")


@git_support
def get_new_command(command):
return shell.and_("git checkout master", "{}").format(
remote_name = get_sp_stdout("git remote")
default_branch_name = get_sp_stdout("git remote show {remote} | sed -n '/HEAD branch/s/.*: //p'".format(remote=remote_name))
return shell.and_("git checkout {default_branch}".format(default_branch=default_branch_name), "{}").format(
replace_argument(command.script, "-d", "-D")
)