From 5efe4cc00e88ff7888c95908e25d742f3ded3f24 Mon Sep 17 00:00:00 2001 From: Wei Lee Date: Sat, 9 Nov 2019 09:02:21 +0800 Subject: [PATCH 1/4] feat(git): add is_staging_clean to check if there is any file in git staging --- commitizen/git.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/commitizen/git.py b/commitizen/git.py index c04206a906..db307d4510 100644 --- a/commitizen/git.py +++ b/commitizen/git.py @@ -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)) From c43d6d8df8dcfadbc1d2bd8dcae6069aeef131b3 Mon Sep 17 00:00:00 2001 From: Wei Lee Date: Sat, 9 Nov 2019 09:03:29 +0800 Subject: [PATCH 2/4] feat(commands/commit): abort commit if there is nothing to commit --- commitizen/commands/commit.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/commitizen/commands/commit.py b/commitizen/commands/commit.py index cda2abcefd..6be8886e60 100644 --- a/commitizen/commands/commit.py +++ b/commitizen/commands/commit.py @@ -9,6 +9,7 @@ NO_ANSWERS = 5 COMMIT_ERROR = 6 NO_COMMIT_BACKUP = 7 +NOTHING_TO_COMMIT = 8 class Commit: @@ -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: From da131a44db8d90c91c52a19a46b015cd6a6eb1b8 Mon Sep 17 00:00:00 2001 From: Wei Lee Date: Sat, 9 Nov 2019 09:04:18 +0800 Subject: [PATCH 3/4] test(commands/commit): test aborting commit when there is nothing to commit #45 --- tests/test_commands.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/test_commands.py b/tests/test_commands.py index 3dda7202f5..0c8b640231 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -72,6 +72,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)() From 373ec25d9841f06500ce18ac908e86ef0ab7938a Mon Sep 17 00:00:00 2001 From: Wei Lee Date: Sat, 9 Nov 2019 10:02:46 +0800 Subject: [PATCH 4/4] test(commands/commit): set staging to clean for most commit tests --- tests/test_commands.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/test_commands.py b/tests/test_commands.py index 0c8b640231..6c7434aa0d 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -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 = { @@ -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", "", "", "") @@ -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 = {