Skip to content

Commit 0637a6b

Browse files
authored
Merge pull request #49 from Lee-W/abort-if-no-change
Abort if no change
2 parents 1f6ffb0 + 373ec25 commit 0637a6b

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

commitizen/commands/commit.py

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
NO_ANSWERS = 5
1010
COMMIT_ERROR = 6
1111
NO_COMMIT_BACKUP = 7
12+
NOTHING_TO_COMMIT = 8
1213

1314

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

2324
def __call__(self):
25+
if git.is_staging_clean():
26+
out.write("No files added to staging!")
27+
raise SystemExit(NOTHING_TO_COMMIT)
28+
2429
retry: bool = self.arguments.get("retry")
2530

2631
if retry:

commitizen/git.py

+7
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,10 @@ def get_commits(start: str, end: str = "HEAD", from_beginning: bool = False) ->
3333
def tag_exist(tag: str) -> bool:
3434
c = cmd.run(f"git tag --list {tag}")
3535
return tag in c.out
36+
37+
38+
def is_staging_clean() -> bool:
39+
"""Check if staing is clean"""
40+
c = cmd.run("git diff --no-ext-diff --name-only")
41+
c_cached = cmd.run("git diff --no-ext-diff --cached --name-only")
42+
return not (bool(c.out) or bool(c_cached.out))

tests/test_commands.py

+20
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
config = {"name": defaults.name}
99

1010

11+
@pytest.fixture
12+
def staging_is_clean(mocker):
13+
is_staging_clean_mock = mocker.patch("commitizen.git.is_staging_clean")
14+
is_staging_clean_mock.return_value = False
15+
16+
17+
@pytest.mark.usefixtures("staging_is_clean")
1118
def test_commit(mocker):
1219
prompt_mock = mocker.patch("questionary.prompt")
1320
prompt_mock.return_value = {
@@ -27,6 +34,7 @@ def test_commit(mocker):
2734
success_mock.assert_called_once()
2835

2936

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

3745

46+
@pytest.mark.usefixtures("staging_is_clean")
3847
def test_commit_retry_works(mocker):
3948
prompt_mock = mocker.patch("questionary.prompt")
4049
prompt_mock.return_value = {
@@ -72,6 +81,17 @@ def test_commit_retry_works(mocker):
7281
assert not os.path.isfile(temp_file)
7382

7483

84+
def test_commit_when_nothing_to_commit(mocker):
85+
is_staging_clean_mock = mocker.patch("commitizen.git.is_staging_clean")
86+
is_staging_clean_mock.return_value = True
87+
88+
with pytest.raises(SystemExit) as err:
89+
commit_cmd = commands.Commit(config, {})
90+
commit_cmd()
91+
92+
assert err.value.code == commands.commit.NOTHING_TO_COMMIT
93+
94+
7595
def test_example():
7696
with mock.patch("commitizen.out.write") as write_mock:
7797
commands.Example(config)()

0 commit comments

Comments
 (0)