From 932a7c5db5e63735ba5e3dcc77d24a95284810bd Mon Sep 17 00:00:00 2001 From: mcarton Date: Fri, 8 May 2015 01:46:00 +0200 Subject: [PATCH 1/3] Add a don't repeat yourself rule --- README.md | 1 + thefuck/rules/dry.py | 12 ++++++++++++ 2 files changed, 13 insertions(+) create mode 100644 thefuck/rules/dry.py diff --git a/README.md b/README.md index ee52858f..e903d76b 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,7 @@ using matched rule and run it. Rules enabled by default: * `cd_parent` – changes `cd..` to `cd ..`; * `cd_mkdir` – creates directories before cd'ing into them; * `cp_omitting_directory` – adds `-a` when you `cp` directory; +* `dry` – fix repetitions like "git git push"; * `fix_alt_space` – replaces Alt+Space with Space character; * `git_add` – fix *"Did you forget to 'git add'?"*; * `git_no_command` – fixes wrong git commands like `git brnch`; diff --git a/thefuck/rules/dry.py b/thefuck/rules/dry.py new file mode 100644 index 00000000..86759b32 --- /dev/null +++ b/thefuck/rules/dry.py @@ -0,0 +1,12 @@ +def match(command, settings): + split_command = command.script.split() + + return len(split_command) >= 2 and split_command[0] == split_command[1] + + +def get_new_command(command, settings): + return command.script[command.script.find(' '):] + +# it should be rare enough to actually have to type twice the same word, so +# this rule can have a higher priority to come before things like "cd cd foo" +priority = 900 From 56f636f3d8efbabb0942dd751f567484c67f3fbe Mon Sep 17 00:00:00 2001 From: mcarton Date: Fri, 8 May 2015 11:41:26 +0200 Subject: [PATCH 2/3] Remove unnecessary space in the DRY rule --- thefuck/rules/dry.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/thefuck/rules/dry.py b/thefuck/rules/dry.py index 86759b32..f0954ea6 100644 --- a/thefuck/rules/dry.py +++ b/thefuck/rules/dry.py @@ -5,7 +5,7 @@ def match(command, settings): def get_new_command(command, settings): - return command.script[command.script.find(' '):] + return command.script[command.script.find(' ')+1:] # it should be rare enough to actually have to type twice the same word, so # this rule can have a higher priority to come before things like "cd cd foo" From 95007220fbe4c5554a92b2b688fb9724ec949ebd Mon Sep 17 00:00:00 2001 From: mcarton Date: Fri, 8 May 2015 11:42:00 +0200 Subject: [PATCH 3/3] Add a test for the DRY rule --- tests/rules/test_dry.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 tests/rules/test_dry.py diff --git a/tests/rules/test_dry.py b/tests/rules/test_dry.py new file mode 100644 index 00000000..757866df --- /dev/null +++ b/tests/rules/test_dry.py @@ -0,0 +1,17 @@ +import pytest +from thefuck.rules.dry import match, get_new_command +from tests.utils import Command + + +@pytest.mark.parametrize('command', [ + Command(script='cd cd foo'), + Command(script='git git push origin/master')]) +def test_match(command): + assert match(command, None) + + +@pytest.mark.parametrize('command, new_command', [ + (Command('cd cd foo'), 'cd foo'), + (Command('git git push origin/master'), 'git push origin/master')]) +def test_get_new_command(command, new_command): + assert get_new_command(command, None) == new_command