#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
+24
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
@@ -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])