Skip to content

Commit 2fdce2f

Browse files
committed
fix: Raise NotAGitProjectError only in git related command
Only Running `cz commit`, `cz bump` and `cz changelog` in a non git project will raise NotAGitProjectError Other commands like `cz --help` will work now #206
1 parent 3f91066 commit 2fdce2f

File tree

9 files changed

+56
-14
lines changed

9 files changed

+56
-14
lines changed

‎commitizen/commands/bump.py

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
ExpectedExit,
1414
NoCommitsFoundError,
1515
NoPatternMapError,
16+
NotAGitProjectError,
1617
NoVersionSpecifiedError,
1718
)
1819

@@ -21,6 +22,9 @@ class Bump:
2122
"""Show prompt for the user to create a guided commit."""
2223

2324
def __init__(self, config: BaseConfig, arguments: dict):
25+
if not git.is_git_project():
26+
raise NotAGitProjectError()
27+
2428
self.config: BaseConfig = config
2529
self.arguments: dict = arguments
2630
self.bump_settings: dict = {

‎commitizen/commands/changelog.py

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
NoCommitsFoundError,
1111
NoPatternMapError,
1212
NoRevisionError,
13+
NotAGitProjectError,
1314
)
1415
from commitizen.git import GitTag
1516

@@ -22,6 +23,9 @@ class Changelog:
2223
"""Generate a changelog based on the commit history."""
2324

2425
def __init__(self, config: BaseConfig, args):
26+
if not git.is_git_project():
27+
raise NotAGitProjectError()
28+
2529
self.config: BaseConfig = config
2630
self.cz = factory.commiter_factory(self.config)
2731

‎commitizen/commands/commit.py

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
DryRunExit,
1414
NoAnswersError,
1515
NoCommitBackupError,
16+
NotAGitProjectError,
1617
NothingToCommitError,
1718
)
1819

@@ -21,6 +22,9 @@ class Commit:
2122
"""Show prompt for the user to create a guided commit."""
2223

2324
def __init__(self, config: BaseConfig, arguments: dict):
25+
if not git.is_git_project():
26+
raise NotAGitProjectError()
27+
2428
self.config: BaseConfig = config
2529
self.cz = factory.commiter_factory(self.config)
2630
self.arguments = arguments

‎commitizen/config/__init__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from typing import Optional, Union
44

55
from commitizen import defaults, git
6-
from commitizen.exceptions import NotAGitProjectError
76

87
from .base_config import BaseConfig
98
from .ini_config import IniConfig
@@ -37,12 +36,13 @@ def read_cfg() -> BaseConfig:
3736
conf = BaseConfig()
3837

3938
git_project_root = git.find_git_project_root()
40-
if not git_project_root:
41-
raise NotAGitProjectError()
39+
cfg_search_paths = [Path(".")]
40+
if git_project_root:
41+
cfg_search_paths.append(git_project_root)
4242

4343
cfg_paths = (
4444
path / Path(filename)
45-
for path in [Path("."), git_project_root]
45+
for path in cfg_search_paths
4646
for filename in defaults.config_files
4747
)
4848
for filename in cfg_paths:

‎commitizen/git.py

+7
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,10 @@ def is_staging_clean() -> bool:
135135
c = cmd.run("git diff --no-ext-diff --name-only")
136136
c_cached = cmd.run("git diff --no-ext-diff --cached --name-only")
137137
return not (bool(c.out) or bool(c_cached.out))
138+
139+
140+
def is_git_project() -> bool:
141+
c = cmd.run("git rev-parse --is-inside-work-tree")
142+
if c.out.strip() == "true":
143+
return True
144+
return False

‎tests/commands/test_bump_command.py

+11
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
ExpectedExit,
1111
NoCommitsFoundError,
1212
NoPatternMapError,
13+
NotAGitProjectError,
1314
NoVersionSpecifiedError,
1415
)
1516
from tests.utils import create_file_and_commit
@@ -206,3 +207,13 @@ def test_bump_dry_run(mocker, capsys, tmp_commitizen_project):
206207

207208
tag_exists = git.tag_exist("0.2.0")
208209
assert tag_exists is False
210+
211+
212+
def test_bump_in_non_git_project(tmpdir, config, mocker):
213+
testargs = ["cz", "bump", "--yes"]
214+
mocker.patch.object(sys, "argv", testargs)
215+
216+
with tmpdir.as_cwd():
217+
with pytest.raises(NotAGitProjectError):
218+
with pytest.raises(ExpectedExit):
219+
cli.main()

‎tests/commands/test_changelog_command.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66

77
from commitizen import cli, git
88
from commitizen.commands.changelog import Changelog
9-
from commitizen.exceptions import DryRunExit, NoCommitsFoundError, NoRevisionError
9+
from commitizen.exceptions import (
10+
DryRunExit,
11+
NoCommitsFoundError,
12+
NoRevisionError,
13+
NotAGitProjectError,
14+
)
1015
from tests.utils import create_file_and_commit
1116

1217

@@ -335,3 +340,12 @@ def test_changelog_with_different_tag_name_and_changelog_content(
335340

336341
with pytest.raises(NoRevisionError):
337342
cli.main()
343+
344+
345+
def test_changelog_in_non_git_project(tmpdir, config, mocker):
346+
testargs = ["cz", "changelog", "--incremental"]
347+
mocker.patch.object(sys, "argv", testargs)
348+
349+
with tmpdir.as_cwd():
350+
with pytest.raises(NotAGitProjectError):
351+
cli.main()

‎tests/commands/test_commit_command.py

+7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
DryRunExit,
1111
NoAnswersError,
1212
NoCommitBackupError,
13+
NotAGitProjectError,
1314
NothingToCommitError,
1415
)
1516

@@ -151,3 +152,9 @@ def test_commit_when_no_user_answer(config, mocker, capsys):
151152
with pytest.raises(NoAnswersError):
152153
commit_cmd = commands.Commit(config, {})
153154
commit_cmd()
155+
156+
157+
def test_commit_in_non_git_project(tmpdir, config):
158+
with tmpdir.as_cwd():
159+
with pytest.raises(NotAGitProjectError):
160+
commands.Commit(config, {})

‎tests/test_conf.py

-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import pytest
55

66
from commitizen import config, defaults, git
7-
from commitizen.exceptions import NotAGitProjectError
87

98
PYPROJECT = """
109
[tool.commitizen]
@@ -150,14 +149,6 @@ def test_find_git_project_root(tmpdir):
150149
assert git.find_git_project_root() is None
151150

152151

153-
def test_read_cfg_when_not_in_a_git_project(tmpdir):
154-
with tmpdir.as_cwd() as _:
155-
with pytest.raises(NotAGitProjectError) as excinfo:
156-
config.read_cfg()
157-
158-
assert NotAGitProjectError.message in str(excinfo.value)
159-
160-
161152
class TestInilConfig:
162153
def test_read_setup_cfg_without_commitizen_config(self, tmpdir):
163154
path = tmpdir.mkdir("commitizen").join("setup.cfg")

0 commit comments

Comments
 (0)