From b22a3ac891b2610d596225ba05dbd0729b707fb0 Mon Sep 17 00:00:00 2001 From: Adam Barnes Date: Wed, 22 Mar 2017 10:23:35 +0000 Subject: [PATCH 1/4] Created a rule for trying to push a new repository with no commits. --- README.md | 1 + tests/rules/test_git_push_without_commits.py | 28 ++++++++++++++++++++ thefuck/rules/git_push_without_commits.py | 17 ++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 tests/rules/test_git_push_without_commits.py create mode 100644 thefuck/rules/git_push_without_commits.py diff --git a/README.md b/README.md index 3e84a35a..1d61d743 100644 --- a/README.md +++ b/README.md @@ -183,6 +183,7 @@ using the matched rule and runs it. Rules enabled by default are as follows: * `git_pull_uncommitted_changes` – stashes changes before pulling and pops them afterwards; * `git_push` – adds `--set-upstream origin $branch` to previous failed `git push`; * `git_push_pull` – runs `git pull` when `push` was rejected; +* `git_push_without_commits` – Creates an initial commit if you forget and only `git add .`, when setting up a new project; * `git_rebase_no_changes` – runs `git rebase --skip` instead of `git rebase --continue` when there are no changes; * `git_rm_local_modifications` – adds `-f` or `--cached` when you try to `rm` a locally modified file; * `git_rm_recursive` – adds `-r` when you try to `rm` a directory; diff --git a/tests/rules/test_git_push_without_commits.py b/tests/rules/test_git_push_without_commits.py new file mode 100644 index 00000000..4c3add58 --- /dev/null +++ b/tests/rules/test_git_push_without_commits.py @@ -0,0 +1,28 @@ +import pytest + +from tests.utils import Command +from thefuck.rules.git_push_without_commits import ( + fix, + get_new_command, + match, + refspec_does_not_match, +) + +command = 'git push -u origin master' +expected_error = ''' +error: src refspec master does not match any. +error: failed to push some refs to 'git@github.com:User/repo.git' +''' + + +@pytest.mark.parametrize('command', [Command(command, stderr=expected_error)]) +def test_match(command): + assert match(command) + + +@pytest.mark.parametrize('command, result', [( + Command(command, stderr=expected_error), + fix.format(command=command), +)]) +def test_get_new_command(command, result): + assert get_new_command(command) == result diff --git a/thefuck/rules/git_push_without_commits.py b/thefuck/rules/git_push_without_commits.py new file mode 100644 index 00000000..119c903c --- /dev/null +++ b/thefuck/rules/git_push_without_commits.py @@ -0,0 +1,17 @@ +import re + +from thefuck.utils import replace_command + +fix = 'git commit -m "Initial commit." && {command}' +refspec_does_not_match = re.compile(r'src refspec \w+ does not match any\.') + + +def match(command): + if refspec_does_not_match.search(command.stderr): + return True + + return False + + +def get_new_command(command): + return fix.format(command=command.script) From bddb43b98768b0ab0c7218180f24802733007abf Mon Sep 17 00:00:00 2001 From: Adam Barnes Date: Wed, 22 Mar 2017 10:29:50 +0000 Subject: [PATCH 2/4] Removed an unused import. --- thefuck/rules/git_push_without_commits.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/thefuck/rules/git_push_without_commits.py b/thefuck/rules/git_push_without_commits.py index 119c903c..b9c4ae2c 100644 --- a/thefuck/rules/git_push_without_commits.py +++ b/thefuck/rules/git_push_without_commits.py @@ -1,7 +1,5 @@ import re -from thefuck.utils import replace_command - fix = 'git commit -m "Initial commit." && {command}' refspec_does_not_match = re.compile(r'src refspec \w+ does not match any\.') From e61271dae3549c98e8ccf83ae23372fc60d2d185 Mon Sep 17 00:00:00 2001 From: Adam Barnes Date: Wed, 22 Mar 2017 10:59:27 +0000 Subject: [PATCH 3/4] Removed another unused import. Goodness. --- tests/rules/test_git_push_without_commits.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/rules/test_git_push_without_commits.py b/tests/rules/test_git_push_without_commits.py index 4c3add58..4ffea4da 100644 --- a/tests/rules/test_git_push_without_commits.py +++ b/tests/rules/test_git_push_without_commits.py @@ -5,7 +5,6 @@ from thefuck.rules.git_push_without_commits import ( fix, get_new_command, match, - refspec_does_not_match, ) command = 'git push -u origin master' From b0965604695c1e28e9b3632e27ec6131d8aad13e Mon Sep 17 00:00:00 2001 From: Vladimir Iakovlev Date: Wed, 22 Mar 2017 14:00:03 +0100 Subject: [PATCH 4/4] #618: Refine `git_push_without_commits` rule --- thefuck/rules/git_push_without_commits.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/thefuck/rules/git_push_without_commits.py b/thefuck/rules/git_push_without_commits.py index b9c4ae2c..a1321319 100644 --- a/thefuck/rules/git_push_without_commits.py +++ b/thefuck/rules/git_push_without_commits.py @@ -1,14 +1,13 @@ import re +from thefuck.specific.git import git_support -fix = 'git commit -m "Initial commit." && {command}' +fix = u'git commit -m "Initial commit." && {command}' refspec_does_not_match = re.compile(r'src refspec \w+ does not match any\.') +@git_support def match(command): - if refspec_does_not_match.search(command.stderr): - return True - - return False + return bool(refspec_does_not_match.search(command.stderr)) def get_new_command(command):