Skip to content

Abort if no change #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions commitizen/commands/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
NO_ANSWERS = 5
COMMIT_ERROR = 6
NO_COMMIT_BACKUP = 7
NOTHING_TO_COMMIT = 8


class Commit:
Expand All @@ -21,6 +22,10 @@ def __init__(self, config: dict, arguments: dict):
self.temp_file: str = os.path.join(tempfile.gettempdir(), "cz.commit.backup")

def __call__(self):
if git.is_staging_clean():
out.write("No files added to staging!")
raise SystemExit(NOTHING_TO_COMMIT)

retry: bool = self.arguments.get("retry")

if retry:
Expand Down
7 changes: 7 additions & 0 deletions commitizen/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,10 @@ def get_commits(start: str, end: str = "HEAD", from_beginning: bool = False) ->
def tag_exist(tag: str) -> bool:
c = cmd.run(f"git tag --list {tag}")
return tag in c.out


def is_staging_clean() -> bool:
"""Check if staing is clean"""
c = cmd.run("git diff --no-ext-diff --name-only")
c_cached = cmd.run("git diff --no-ext-diff --cached --name-only")
return not (bool(c.out) or bool(c_cached.out))
20 changes: 20 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
config = {"name": defaults.name}


@pytest.fixture
def staging_is_clean(mocker):
is_staging_clean_mock = mocker.patch("commitizen.git.is_staging_clean")
is_staging_clean_mock.return_value = False


@pytest.mark.usefixtures("staging_is_clean")
def test_commit(mocker):
prompt_mock = mocker.patch("questionary.prompt")
prompt_mock.return_value = {
Expand All @@ -27,6 +34,7 @@ def test_commit(mocker):
success_mock.assert_called_once()


@pytest.mark.usefixtures("staging_is_clean")
def test_commit_retry_fails_no_backup(mocker):
commit_mock = mocker.patch("commitizen.git.commit")
commit_mock.return_value = cmd.Command("success", "", "", "")
Expand All @@ -35,6 +43,7 @@ def test_commit_retry_fails_no_backup(mocker):
commands.Commit(config, {"retry": True})()


@pytest.mark.usefixtures("staging_is_clean")
def test_commit_retry_works(mocker):
prompt_mock = mocker.patch("questionary.prompt")
prompt_mock.return_value = {
Expand Down Expand Up @@ -72,6 +81,17 @@ def test_commit_retry_works(mocker):
assert not os.path.isfile(temp_file)


def test_commit_when_nothing_to_commit(mocker):
is_staging_clean_mock = mocker.patch("commitizen.git.is_staging_clean")
is_staging_clean_mock.return_value = True

with pytest.raises(SystemExit) as err:
commit_cmd = commands.Commit(config, {})
commit_cmd()

assert err.value.code == commands.commit.NOTHING_TO_COMMIT


def test_example():
with mock.patch("commitizen.out.write") as write_mock:
commands.Example(config)()
Expand Down