mirror of
https://github.com/nvbn/thefuck.git
synced 2025-01-31 02:01:13 +00:00
git: fix fatal: bad flag '...' after filename
For example: $ git log README.md -p fatal: bad flag '-p' used after filename $ fuck git log -p README.md [enter/↑/↓/ctrl+c] Aborted $ git log -p README.md --name-only fatal: bad flag '--name-only' used after filename $ fuck git log -p --name-only README.md [enter/↑/↓/ctrl+c] Aborted $ git log README.md -p CONTRIBUTING.md fatal: bad flag '-p' used after filename $ fuck git log -p README.md CONTRIBUTING.md [enter/↑/↓/ctrl+c]
This commit is contained in:
parent
cb99e42e02
commit
5b420204c9
@ -173,6 +173,7 @@ using the matched rule and runs it. Rules enabled by default are as follows:
|
||||
* `git_diff_no_index` – adds `--no-index` to previous `git diff` on untracked files;
|
||||
* `git_diff_staged` – adds `--staged` to previous `git diff` with unexpected output;
|
||||
* `git_fix_stash` – fixes `git stash` commands (misspelled subcommand and missing `save`);
|
||||
* `git_flag_after_filename` – fixes `fatal: bad flag '...' after filename`
|
||||
* `git_help_aliased` – fixes `git help <alias>` commands replacing <alias> with the aliased command;
|
||||
* `git_not_command` – fixes wrong git commands like `git brnch`;
|
||||
* `git_pull` – sets upstream before executing previous `git pull`;
|
||||
|
20
tests/rules/test_git_flag_after_filename.py
Normal file
20
tests/rules/test_git_flag_after_filename.py
Normal file
@ -0,0 +1,20 @@
|
||||
import pytest
|
||||
from thefuck.rules.git_flag_after_filename import match, get_new_command
|
||||
from tests.utils import Command
|
||||
|
||||
|
||||
def test_match():
|
||||
assert match(Command('git log README.md -p', stderr="fatal: bad flag '-p' used after filename"))
|
||||
assert match(Command('git log README.md -p CONTRIBUTING.md', stderr="fatal: bad flag '-p' used after filename"))
|
||||
assert match(Command('git log -p README.md --name-only', stderr="fatal: bad flag '--name-only' used after filename"))
|
||||
assert not match(Command('git log README.md'))
|
||||
assert not match(Command('git log -p README.md'))
|
||||
|
||||
|
||||
def test_get_new_command():
|
||||
assert get_new_command(Command('git log README.md -p', stderr="fatal: bad flag '-p' used after filename"))\
|
||||
== "git log -p README.md"
|
||||
assert get_new_command(Command('git log README.md -p CONTRIBUTING.md', stderr="fatal: bad flag '-p' used after filename"))\
|
||||
== "git log -p README.md CONTRIBUTING.md"
|
||||
assert get_new_command(Command('git log -p README.md --name-only', stderr="fatal: bad flag '--name-only' used after filename"))\
|
||||
== "git log -p --name-only README.md"
|
29
thefuck/rules/git_flag_after_filename.py
Normal file
29
thefuck/rules/git_flag_after_filename.py
Normal file
@ -0,0 +1,29 @@
|
||||
import re
|
||||
from thefuck.specific.git import git_support
|
||||
|
||||
error_pattern = "fatal: bad flag '(.*?)' used after filename"
|
||||
|
||||
@git_support
|
||||
def match(command):
|
||||
return re.search(error_pattern, command.stderr)
|
||||
|
||||
|
||||
@git_support
|
||||
def get_new_command(command):
|
||||
command_parts = command.script_parts[:]
|
||||
|
||||
# find the bad flag
|
||||
bad_flag = re.search(error_pattern, command.stderr).group(1)
|
||||
bad_flag_index = command_parts.index(bad_flag)
|
||||
|
||||
# find the filename
|
||||
for index in reversed(range(bad_flag_index)):
|
||||
if command_parts[index][0] != '-':
|
||||
filename_index = index
|
||||
break
|
||||
|
||||
# swap them
|
||||
command_parts[bad_flag_index], command_parts[filename_index] = \
|
||||
command_parts[filename_index], command_parts[bad_flag_index]
|
||||
|
||||
return u' '.join(command_parts)
|
Loading…
x
Reference in New Issue
Block a user