diff --git a/thefuck/rules/git_push.py b/thefuck/rules/git_push.py index ba4a0875..85f59fe1 100644 --- a/thefuck/rules/git_push.py +++ b/thefuck/rules/git_push.py @@ -8,29 +8,29 @@ def match(command): and 'set-upstream' in command.stderr) +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 - upstream_option_index = -1 command_parts = command.script_parts[:] + upstream_option_index = _get_upstream_option_index(command_parts) - try: - upstream_option_index = command_parts.index('--set-upstream') - except ValueError: - pass - try: - upstream_option_index = command_parts.index('-u') - except ValueError: - pass - if upstream_option_index is not -1: + if upstream_option_index is not None: command_parts.pop(upstream_option_index) - try: + + # 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) - except IndexError: - # This happens for `git push -u` - pass push_upstream = command.stderr.split('\n')[-3].strip().partition('git ')[2] return replace_argument(" ".join(command_parts), 'push', push_upstream)