diff --git a/README.md b/README.md index 470b4d35..5d9a15c9 100644 --- a/README.md +++ b/README.md @@ -188,6 +188,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..4ffea4da --- /dev/null +++ b/tests/rules/test_git_push_without_commits.py @@ -0,0 +1,27 @@ +import pytest + +from tests.utils import Command +from thefuck.rules.git_push_without_commits import ( + fix, + get_new_command, + 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..a1321319 --- /dev/null +++ b/thefuck/rules/git_push_without_commits.py @@ -0,0 +1,14 @@ +import re +from thefuck.specific.git import git_support + +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): + return bool(refspec_does_not_match.search(command.stderr)) + + +def get_new_command(command): + return fix.format(command=command.script)