From 5b420204c9efa1939925a8ff62e3f5bcb971b516 Mon Sep 17 00:00:00 2001 From: Joseph Frazier <1212jtraceur@gmail.com> Date: Sun, 30 Oct 2016 21:15:47 -0400 Subject: [PATCH 1/4] git: fix `fatal: bad flag '...' after filename` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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] --- README.md | 1 + tests/rules/test_git_flag_after_filename.py | 20 ++++++++++++++ thefuck/rules/git_flag_after_filename.py | 29 +++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 tests/rules/test_git_flag_after_filename.py create mode 100644 thefuck/rules/git_flag_after_filename.py diff --git a/README.md b/README.md index aedaf1b2..0cf68448 100644 --- a/README.md +++ b/README.md @@ -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 ` commands replacing with the aliased command; * `git_not_command` – fixes wrong git commands like `git brnch`; * `git_pull` – sets upstream before executing previous `git pull`; diff --git a/tests/rules/test_git_flag_after_filename.py b/tests/rules/test_git_flag_after_filename.py new file mode 100644 index 00000000..ffb6e43c --- /dev/null +++ b/tests/rules/test_git_flag_after_filename.py @@ -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" diff --git a/thefuck/rules/git_flag_after_filename.py b/thefuck/rules/git_flag_after_filename.py new file mode 100644 index 00000000..e2a94d8e --- /dev/null +++ b/thefuck/rules/git_flag_after_filename.py @@ -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) From 9cae0bffffc82fa30475de93fa9d6fa5f6fd076e Mon Sep 17 00:00:00 2001 From: Joseph Frazier <1212jtraceur@gmail.com> Date: Mon, 31 Oct 2016 00:08:30 -0400 Subject: [PATCH 2/4] git_flag_after_filename: fix flake8 errors These were found by creating a `.flake8` file containing: [flake8] ignore = E501,W503 exclude = venv then running: flake8 $(git diff master... --name-only) See https://github.com/nvbn/thefuck/pull/563 for running `flake8` in CI --- tests/rules/test_git_flag_after_filename.py | 1 - thefuck/rules/git_flag_after_filename.py | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/rules/test_git_flag_after_filename.py b/tests/rules/test_git_flag_after_filename.py index ffb6e43c..414c0d95 100644 --- a/tests/rules/test_git_flag_after_filename.py +++ b/tests/rules/test_git_flag_after_filename.py @@ -1,4 +1,3 @@ -import pytest from thefuck.rules.git_flag_after_filename import match, get_new_command from tests.utils import Command diff --git a/thefuck/rules/git_flag_after_filename.py b/thefuck/rules/git_flag_after_filename.py index e2a94d8e..bec0591e 100644 --- a/thefuck/rules/git_flag_after_filename.py +++ b/thefuck/rules/git_flag_after_filename.py @@ -3,6 +3,7 @@ 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) @@ -24,6 +25,6 @@ def get_new_command(command): # swap them command_parts[bad_flag_index], command_parts[filename_index] = \ - command_parts[filename_index], command_parts[bad_flag_index] + command_parts[filename_index], command_parts[bad_flag_index] # noqa: E122 return u' '.join(command_parts) From fa169c686c39552bb62d0f55e7ccd1c513db0135 Mon Sep 17 00:00:00 2001 From: Joseph Frazier <1212jtraceur@gmail.com> Date: Mon, 31 Oct 2016 00:15:21 -0400 Subject: [PATCH 3/4] test_git_flag_after_filename.py: dedupe test commands --- tests/rules/test_git_flag_after_filename.py | 22 ++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/tests/rules/test_git_flag_after_filename.py b/tests/rules/test_git_flag_after_filename.py index 414c0d95..b81668b0 100644 --- a/tests/rules/test_git_flag_after_filename.py +++ b/tests/rules/test_git_flag_after_filename.py @@ -1,19 +1,23 @@ from thefuck.rules.git_flag_after_filename import match, get_new_command from tests.utils import Command +command1 = Command('git log README.md -p', + stderr="fatal: bad flag '-p' used after filename") +command2 = Command('git log README.md -p CONTRIBUTING.md', + stderr="fatal: bad flag '-p' used after filename") +command3 = Command('git log -p README.md --name-only', + stderr="fatal: bad flag '--name-only' used after filename") + 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 match(command1) + assert match(command2) + assert match(command3) 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" + assert get_new_command(command1) == "git log -p README.md" + assert get_new_command(command2) == "git log -p README.md CONTRIBUTING.md" + assert get_new_command(command3) == "git log -p --name-only README.md" From aec8fe32337e2721d198b78f0a8a37d8161d5da8 Mon Sep 17 00:00:00 2001 From: Vladimir Iakovlev Date: Tue, 8 Nov 2016 23:53:40 +0100 Subject: [PATCH 4/4] #570: Refine tests --- tests/rules/test_git_flag_after_filename.py | 28 +++++++++++++-------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/tests/rules/test_git_flag_after_filename.py b/tests/rules/test_git_flag_after_filename.py index b81668b0..4cf49957 100644 --- a/tests/rules/test_git_flag_after_filename.py +++ b/tests/rules/test_git_flag_after_filename.py @@ -1,3 +1,4 @@ +import pytest from thefuck.rules.git_flag_after_filename import match, get_new_command from tests.utils import Command @@ -9,15 +10,22 @@ command3 = Command('git log -p README.md --name-only', stderr="fatal: bad flag '--name-only' used after filename") -def test_match(): - assert match(command1) - assert match(command2) - assert match(command3) - assert not match(Command('git log README.md')) - assert not match(Command('git log -p README.md')) +@pytest.mark.parametrize('command', [ + command1, command2, command3]) +def test_match(command): + assert match(command) -def test_get_new_command(): - assert get_new_command(command1) == "git log -p README.md" - assert get_new_command(command2) == "git log -p README.md CONTRIBUTING.md" - assert get_new_command(command3) == "git log -p --name-only README.md" +@pytest.mark.parametrize('command', [ + Command('git log README.md'), + Command('git log -p README.md')]) +def test_not_match(command): + assert not match(command) + + +@pytest.mark.parametrize('command, result', [ + (command1, "git log -p README.md"), + (command2, "git log -p README.md CONTRIBUTING.md"), + (command3, "git log -p --name-only README.md")]) +def test_get_new_command(command, result): + assert get_new_command(command) == result