Skip to content
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
4 changes: 4 additions & 0 deletions commitizen/commands/bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
ExpectedExit,
NoCommitsFoundError,
NoPatternMapError,
NotAGitProjectError,
NoVersionSpecifiedError,
)

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

def __init__(self, config: BaseConfig, arguments: dict):
if not git.is_git_project():
raise NotAGitProjectError()

self.config: BaseConfig = config
self.arguments: dict = arguments
self.bump_settings: dict = {
Expand Down
4 changes: 4 additions & 0 deletions commitizen/commands/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
NoCommitsFoundError,
NoPatternMapError,
NoRevisionError,
NotAGitProjectError,
)
from commitizen.git import GitTag

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

def __init__(self, config: BaseConfig, args):
if not git.is_git_project():
raise NotAGitProjectError()

self.config: BaseConfig = config
self.cz = factory.commiter_factory(self.config)

Expand Down
4 changes: 4 additions & 0 deletions commitizen/commands/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
DryRunExit,
NoAnswersError,
NoCommitBackupError,
NotAGitProjectError,
NothingToCommitError,
)

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

def __init__(self, config: BaseConfig, arguments: dict):
if not git.is_git_project():
raise NotAGitProjectError()

self.config: BaseConfig = config
self.cz = factory.commiter_factory(self.config)
self.arguments = arguments
Expand Down
8 changes: 4 additions & 4 deletions commitizen/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from typing import Optional, Union

from commitizen import defaults, git
from commitizen.exceptions import NotAGitProjectError

from .base_config import BaseConfig
from .ini_config import IniConfig
Expand Down Expand Up @@ -37,12 +36,13 @@ def read_cfg() -> BaseConfig:
conf = BaseConfig()

git_project_root = git.find_git_project_root()
if not git_project_root:
raise NotAGitProjectError()
cfg_search_paths = [Path(".")]
if git_project_root:
cfg_search_paths.append(git_project_root)

cfg_paths = (
path / Path(filename)
for path in [Path("."), git_project_root]
for path in cfg_search_paths
for filename in defaults.config_files
)
for filename in cfg_paths:
Expand Down
7 changes: 7 additions & 0 deletions commitizen/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,10 @@ def is_staging_clean() -> bool:
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))


def is_git_project() -> bool:
c = cmd.run("git rev-parse --is-inside-work-tree")
if c.out.strip() == "true":
return True
return False
11 changes: 11 additions & 0 deletions tests/commands/test_bump_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
ExpectedExit,
NoCommitsFoundError,
NoPatternMapError,
NotAGitProjectError,
NoVersionSpecifiedError,
)
from tests.utils import create_file_and_commit
Expand Down Expand Up @@ -206,3 +207,13 @@ def test_bump_dry_run(mocker, capsys, tmp_commitizen_project):

tag_exists = git.tag_exist("0.2.0")
assert tag_exists is False


def test_bump_in_non_git_project(tmpdir, config, mocker):
testargs = ["cz", "bump", "--yes"]
mocker.patch.object(sys, "argv", testargs)

with tmpdir.as_cwd():
with pytest.raises(NotAGitProjectError):
with pytest.raises(ExpectedExit):
cli.main()
16 changes: 15 additions & 1 deletion tests/commands/test_changelog_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@

from commitizen import cli, git
from commitizen.commands.changelog import Changelog
from commitizen.exceptions import DryRunExit, NoCommitsFoundError, NoRevisionError
from commitizen.exceptions import (
DryRunExit,
NoCommitsFoundError,
NoRevisionError,
NotAGitProjectError,
)
from tests.utils import create_file_and_commit


Expand Down Expand Up @@ -335,3 +340,12 @@ def test_changelog_with_different_tag_name_and_changelog_content(

with pytest.raises(NoRevisionError):
cli.main()


def test_changelog_in_non_git_project(tmpdir, config, mocker):
testargs = ["cz", "changelog", "--incremental"]
mocker.patch.object(sys, "argv", testargs)

with tmpdir.as_cwd():
with pytest.raises(NotAGitProjectError):
cli.main()
7 changes: 7 additions & 0 deletions tests/commands/test_commit_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
DryRunExit,
NoAnswersError,
NoCommitBackupError,
NotAGitProjectError,
NothingToCommitError,
)

Expand Down Expand Up @@ -151,3 +152,9 @@ def test_commit_when_no_user_answer(config, mocker, capsys):
with pytest.raises(NoAnswersError):
commit_cmd = commands.Commit(config, {})
commit_cmd()


def test_commit_in_non_git_project(tmpdir, config):
with tmpdir.as_cwd():
with pytest.raises(NotAGitProjectError):
commands.Commit(config, {})
9 changes: 0 additions & 9 deletions tests/test_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import pytest

from commitizen import config, defaults, git
from commitizen.exceptions import NotAGitProjectError

PYPROJECT = """
[tool.commitizen]
Expand Down Expand Up @@ -150,14 +149,6 @@ def test_find_git_project_root(tmpdir):
assert git.find_git_project_root() is None


def test_read_cfg_when_not_in_a_git_project(tmpdir):
with tmpdir.as_cwd() as _:
with pytest.raises(NotAGitProjectError) as excinfo:
config.read_cfg()

assert NotAGitProjectError.message in str(excinfo.value)


class TestInilConfig:
def test_read_setup_cfg_without_commitizen_config(self, tmpdir):
path = tmpdir.mkdir("commitizen").join("setup.cfg")
Expand Down