From b22a3ac891b2610d596225ba05dbd0729b707fb0 Mon Sep 17 00:00:00 2001 From: Adam Barnes Date: Wed, 22 Mar 2017 10:23:35 +0000 Subject: [PATCH] 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)