1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-03-19 00:58:56 +00:00
thefuck/thefuck/rules/git_push.py

45 lines
1.7 KiB
Python
Raw Normal View History

import re
2016-08-19 22:08:30 +01:00
from thefuck.utils import replace_argument
from thefuck.specific.git import git_support
2015-07-17 11:55:44 +02:00
@git_support
def match(command):
return ('push' in command.script
and 'set-upstream' in command.output)
2015-04-08 18:15:49 +02:00
2016-10-08 12:24:48 +02:00
def _get_upstream_option_index(command_parts):
if '--set-upstream' in command_parts:
return command_parts.index('--set-upstream')
elif '-u' in command_parts:
return command_parts.index('-u')
else:
return None
@git_support
def get_new_command(command):
# If --set-upstream or -u are passed, remove it and its argument. This is
# because the remaining arguments are concatenated onto the command suggested
# by git, which includes --set-upstream and its argument
command_parts = command.script_parts[:]
2016-10-08 12:24:48 +02:00
upstream_option_index = _get_upstream_option_index(command_parts)
2016-10-08 12:24:48 +02:00
if upstream_option_index is not None:
command_parts.pop(upstream_option_index)
2016-10-08 12:24:48 +02:00
# In case of `git push -u` we don't have next argument:
if len(command_parts) > upstream_option_index:
command_parts.pop(upstream_option_index)
else:
# the only non-qualified permitted options are the repository and refspec; git's
# suggestion include them, so they won't be lost, but would be duplicated otherwise.
push_idx = command_parts.index('push') + 1
while len(command_parts) > push_idx and command_parts[len(command_parts) - 1][0] != '-':
command_parts.pop(len(command_parts) - 1)
2018-01-02 23:17:19 +00:00
arguments = re.findall(r'git push (.*)', command.output)[0].replace("'", r"\'").strip()
return replace_argument(" ".join(command_parts), 'push',
'push {}'.format(arguments))