Skip to content

Commit 46a2b6a

Browse files
authored
Merge pull request #123 from Lee-W/look-up-config-in-git-root
Look up config in git root
2 parents 1a16328 + 84a2118 commit 46a2b6a

File tree

19 files changed

+200
-131
lines changed

19 files changed

+200
-131
lines changed

README.rst

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Commitizen
33
=============
44

5-
Python 3 command line utility to standardize commit messages and bump version
5+
Python 3 command-line utility to standardize commit messages and bump version
66

77

88
.. image:: https://github.com/Woile/commitizen/workflows/Python%20package/badge.svg
@@ -43,13 +43,13 @@ About
4343

4444
Commitizen is a tool designed for teams.
4545

46-
Its main purpose is to define a standard way of commiting rules
46+
Its main purpose is to define a standard way of committing rules
4747
and communicating it (using the cli provided by commitizen).
4848

49-
The reasoning behind it is that is easier to read, and enforces writing
49+
The reasoning behind it is that it is easier to read, and enforces writing
5050
descriptive commits.
5151

52-
Besides that, having a convention on your commits, makes it possible to
52+
Besides that, having a convention on your commits makes it possible to
5353
parse them and use them for something else, like generating automatically
5454
the version or a changelog.
5555

@@ -75,7 +75,7 @@ Installation
7575
Features
7676
========
7777

78-
- Command line utility to create commits with your rules. Defaults: `conventional commits`_
78+
- Command-line utility to create commits with your rules. Defaults: `conventional commits`_
7979
- Display information about your commit rules (commands: schema, example, info)
8080
- Bump version automatically using semantic verisoning based on the commits. `Read More <./docs/bump.md>`_
8181
- Generate a changelog using "Keep a changelog" (Planned feature)
@@ -102,7 +102,7 @@ This is an example of how the git messages history would look like:
102102
docs(README): added about, installation, creating, etc
103103
feat(config): new loads from ~/.cz and working project .cz .cz.cfg and setup.cfg
104104

105-
And then using ``cz bump`` you can change the version of your project
105+
And then, by using ``cz bump`` , you can change the version of your project.
106106

107107
``feat`` to ``MINOR``
108108
``fix`` to ``PATCH``
@@ -111,7 +111,7 @@ And then using ``cz bump`` you can change the version of your project
111111
Commitizens
112112
===========
113113

114-
These are the available commiting styles by default:
114+
These are the available committing styles by default:
115115

116116
* cz_conventional_commits: `conventional commits`_
117117
* cz_jira: `jira smart commits <https://confluence.atlassian.com/fisheye/using-smart-commits-298976812.html>`_
@@ -156,18 +156,23 @@ Usage
156156
optional arguments:
157157
-h, --help show this help message and exit
158158
--debug use debug mode
159-
-n NAME, --name NAME use the given commitizen
159+
-n NAME, --name NAME use the given commitizen (default:
160+
cz_conventional_commits)
160161
--version get the version of the installed commitizen
161162

162163
commands:
163-
{ls,commit,c,example,info,schema,bump,check}
164+
{ls,commit,c,example,info,schema,bump,version,check,init}
164165
ls show available commitizens
165166
commit (c) create new commit
166167
example show commit example
167168
info show information about the cz
168169
schema show commit schema
169170
bump bump semantic version based on the git log
170-
check enforce the project to always use conventional commits
171+
version get the version of the installed commitizen or the
172+
current project (default: installed commitizen)
173+
check validates that a commit message matches the commitizen
174+
schema
175+
init init commitizen configuration
171176

172177
Contributing
173178
============

commitizen/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
{
9090
"name": "--tag-format",
9191
"help": (
92-
"format used to tag the commmit and read it, "
92+
"the format used to tag the commit and read it, "
9393
"use it in existing projects, "
9494
"wrap around simple quotes"
9595
),

commitizen/commands/bump.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55

66
from commitizen import bump, factory, git, out
77
from commitizen.config import BaseConfig
8-
9-
NO_COMMITS_FOUND = 3
10-
NO_VERSION_SPECIFIED = 4
11-
NO_PATTERN_MAP = 7
12-
COMMIT_FAILED = 8
13-
TAG_FAILED = 9
8+
from commitizen.error_codes import (
9+
NO_COMMITS_FOUND,
10+
NO_VERSION_SPECIFIED,
11+
NO_PATTERN_MAP,
12+
COMMIT_FAILED,
13+
TAG_FAILED,
14+
)
1415

1516

1617
class Bump:

commitizen/commands/check.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33

44
from commitizen import factory, out
55
from commitizen.config import BaseConfig
6-
7-
8-
NO_COMMIT_MSG = 3
9-
INVALID_COMMIT_MSG = 5
6+
from commitizen.error_codes import INVALID_COMMIT_MSG
107

118

129
class Check:

commitizen/commands/commit.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
from commitizen import factory, git, out
88
from commitizen.cz.exceptions import CzException
99
from commitizen.config import BaseConfig
10-
11-
12-
NO_ANSWERS = 5
13-
COMMIT_ERROR = 6
14-
NO_COMMIT_BACKUP = 7
15-
NOTHING_TO_COMMIT = 8
16-
CUSTOM_ERROR = 9
10+
from commitizen.error_codes import (
11+
NO_ANSWERS,
12+
COMMIT_ERROR,
13+
NO_COMMIT_BACKUP,
14+
NOTHING_TO_COMMIT,
15+
CUSTOM_ERROR,
16+
)
1717

1818

1919
class Commit:

commitizen/config/__init__.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import os
21
import warnings
32
from pathlib import Path
43
from typing import Optional
54

6-
from commitizen import defaults
5+
from commitizen import defaults, git, out
6+
from commitizen.error_codes import NOT_A_GIT_PROJECT
77
from .base_config import BaseConfig
88
from .toml_config import TomlConfig
99
from .ini_config import IniConfig
1010

1111

1212
def load_global_conf() -> Optional[IniConfig]:
13-
home = str(Path.home())
14-
global_cfg = os.path.join(home, ".cz")
15-
if not os.path.exists(global_cfg):
13+
home = Path.home()
14+
global_cfg = home / Path(".cz")
15+
if not global_cfg.exists():
1616
return None
1717

1818
# global conf doesnt make sense with commitizen bump
@@ -35,16 +35,27 @@ def load_global_conf() -> Optional[IniConfig]:
3535
def read_cfg() -> BaseConfig:
3636
conf = BaseConfig()
3737

38+
git_project_root = git.find_git_project_root()
39+
if not git_project_root:
40+
out.error(
41+
"fatal: not a git repository (or any of the parent directories): .git"
42+
)
43+
raise SystemExit(NOT_A_GIT_PROJECT)
44+
3845
allowed_cfg_files = defaults.config_files
39-
for filename in allowed_cfg_files:
40-
config_file_exists = os.path.exists(filename)
41-
if not config_file_exists:
46+
cfg_paths = (
47+
path / Path(filename)
48+
for path in [Path("."), git_project_root]
49+
for filename in allowed_cfg_files
50+
)
51+
for filename in cfg_paths:
52+
if not filename.exists():
4253
continue
4354

4455
with open(filename, "r") as f:
4556
data: str = f.read()
4657

47-
if "toml" in filename:
58+
if "toml" in filename.suffix:
4859
_conf = TomlConfig(data=data, path=filename)
4960
else:
5061
warnings.warn(

commitizen/config/base_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def set_key(self, key, value):
2323
For now only strings are supported.
2424
We use to update the version number.
2525
"""
26-
return self
26+
raise NotImplementedError()
2727

2828
def update(self, data: dict):
2929
self._settings.update(data)

commitizen/error_codes.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Commitizen factory
2+
NO_COMMITIZEN_FOUND = 1
3+
4+
# Config
5+
NOT_A_GIT_PROJECT = 2
6+
7+
# Bump
8+
NO_COMMITS_FOUND = 3
9+
NO_VERSION_SPECIFIED = 4
10+
NO_PATTERN_MAP = 5
11+
COMMIT_FAILED = 6
12+
TAG_FAILED = 7
13+
14+
# Commit
15+
NO_ANSWERS = 8
16+
COMMIT_ERROR = 9
17+
NO_COMMIT_BACKUP = 10
18+
NOTHING_TO_COMMIT = 11
19+
CUSTOM_ERROR = 12
20+
21+
# Check
22+
NO_COMMIT_MSG = 13
23+
INVALID_COMMIT_MSG = 14

commitizen/factory.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from commitizen import BaseCommitizen, out
22
from commitizen.cz import registry
33
from commitizen.config import BaseConfig
4-
5-
NO_COMMITIZEN_FOUND = 2
4+
from commitizen.error_codes import NO_COMMITIZEN_FOUND
65

76

87
def commiter_factory(config: BaseConfig) -> BaseCommitizen:

commitizen/git.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
from pathlib import Path
23
from tempfile import NamedTemporaryFile
34
from typing import Optional, List
45

@@ -55,3 +56,10 @@ def get_all_tags() -> Optional[List[str]]:
5556
if c.err:
5657
return []
5758
return [tag.strip() for tag in c.out.split("\n") if tag.strip()]
59+
60+
61+
def find_git_project_root() -> Optional[Path]:
62+
c = cmd.run("git rev-parse --show-toplevel")
63+
if not c.err:
64+
return Path(c.out.strip())
65+
return None

0 commit comments

Comments
 (0)