Skip to content

Commit 2d093db

Browse files
authored
Merge pull request #237 from commitizen-tools/next
Next
2 parents 16e832e + ff45e19 commit 2d093db

15 files changed

+211
-343
lines changed

commitizen/cli.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import argparse
22
import logging
33
import sys
4-
import warnings
54
from functools import partial
65

76
from decli import cli
@@ -24,16 +23,10 @@
2423
"name": ["-n", "--name"],
2524
"help": "use the given commitizen (default: cz_conventional_commits)",
2625
},
27-
{
28-
"name": ["--version"],
29-
"action": "store_true",
30-
"help": "get the version of the installed commitizen",
31-
},
3226
],
3327
"subcommands": {
3428
"title": "commands",
35-
# TODO: Add this constraint back in 2.0
36-
# "required": True,
29+
"required": True,
3730
"commands": [
3831
{
3932
"name": ["init"],
@@ -278,26 +271,11 @@ def main():
278271
elif not args.name and not conf.path:
279272
conf.update({"name": "cz_conventional_commits"})
280273

281-
if args.version:
282-
warnings.warn(
283-
(
284-
"'cz --version' will be deprecated in next major version. "
285-
"Please use 'cz version' command from your scripts"
286-
),
287-
category=DeprecationWarning,
288-
)
289-
args.func = commands.Version
290-
291274
if args.debug:
292275
logging.getLogger("commitizen").setLevel(logging.DEBUG)
293276
sys.excepthook = commitizen_debug_excepthook
294277

295-
# TODO: This try block can be removed after command is required in 2.0
296-
# Handle the case that argument is given, but no command is provided
297-
try:
298-
args.func(conf, vars(args))()
299-
except AttributeError:
300-
raise NoCommandFoundError()
278+
args.func(conf, vars(args))()
301279

302280

303281
if __name__ == "__main__":

commitizen/commands/init.py

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import os
2+
13
import questionary
4+
import yaml
25
from packaging.version import Version
36

4-
from commitizen import factory, out
5-
from commitizen.config import BaseConfig, IniConfig, TomlConfig
7+
from commitizen import cmd, factory, out
8+
from commitizen.__version__ import __version__
9+
from commitizen.config import BaseConfig, TomlConfig
610
from commitizen.cz import registry
7-
from commitizen.defaults import long_term_support_config_files
11+
from commitizen.defaults import config_files
812
from commitizen.exceptions import NoAnswersError
913
from commitizen.git import get_latest_tag_name, get_tag_names
1014

@@ -20,11 +24,8 @@ def __call__(self):
2024
# No config for commitizen exist
2125
if not self.config.path:
2226
config_path = self._ask_config_path()
23-
2427
if "toml" in config_path:
2528
self.config = TomlConfig(data="", path=config_path)
26-
else:
27-
self.config = IniConfig(data="", path=config_path)
2829

2930
self.config.init_empty_config_content()
3031

@@ -33,17 +34,20 @@ def __call__(self):
3334
values_to_add["version"] = Version(tag).public
3435
values_to_add["tag_format"] = self._ask_tag_format(tag)
3536
self._update_config_file(values_to_add)
37+
38+
if questionary.confirm("Do you want to install pre-commit hook?").ask():
39+
self._install_pre_commit_hook()
40+
3641
out.write("You can bump the version and create changelog running:\n")
3742
out.info("cz bump --changelog")
3843
out.success("The configuration are all set.")
3944
else:
40-
# TODO: handle the case that config file exist but no value
4145
out.line(f"Config file {self.config.path} already exists")
4246

4347
def _ask_config_path(self) -> str:
4448
name = questionary.select(
4549
"Please choose a supported config file: (default: pyproject.toml)",
46-
choices=long_term_support_config_files,
50+
choices=config_files,
4751
default="pyproject.toml",
4852
style=self.cz.style,
4953
).ask()
@@ -101,6 +105,50 @@ def _ask_tag_format(self, latest_tag) -> str:
101105
tag_format = "$version"
102106
return tag_format
103107

108+
def _install_pre_commit_hook(self):
109+
pre_commit_config_filename = ".pre-commit-config.yaml"
110+
cz_hook_config = {
111+
"repo": "https://github.com/commitizen-tools/commitizen",
112+
"rev": f"v{__version__}",
113+
"hooks": [{"id": "commitizen", "stages": ["commit-msg"]}],
114+
}
115+
116+
config_data = {}
117+
if not os.path.isfile(pre_commit_config_filename):
118+
# .pre-commit-config does not exist
119+
config_data["repos"] = [cz_hook_config]
120+
else:
121+
# breakpoint()
122+
with open(pre_commit_config_filename) as config_file:
123+
yaml_data = yaml.safe_load(config_file)
124+
if yaml_data:
125+
config_data = yaml_data
126+
127+
if "repos" in config_data:
128+
for pre_commit_hook in config_data["repos"]:
129+
if "commitizen" in pre_commit_hook["repo"]:
130+
out.write("commitizen already in pre-commit config")
131+
break
132+
else:
133+
config_data["repos"].append(cz_hook_config)
134+
else:
135+
# .pre-commit-config exists but there's no "repos" key
136+
config_data["repos"] = [cz_hook_config]
137+
138+
with open(pre_commit_config_filename, "w") as config_file:
139+
yaml.safe_dump(config_data, stream=config_file)
140+
141+
c = cmd.run("pre-commit install --hook-type commit-msg")
142+
if c.return_code == 127:
143+
out.error(
144+
"pre-commit is not installed in current environement.\n"
145+
"Run 'pre-commit install --hook-type commit-msg' again after it's installed"
146+
)
147+
elif c.return_code != 0:
148+
out.error(c.err)
149+
else:
150+
out.write("commitizen pre-commit hook is now installed in your '.git'\n")
151+
104152
def _update_config_file(self, values):
105153
for key, value in values.items():
106154
self.config.set_key(key, value)

commitizen/config/__init__.py

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,11 @@
1-
import warnings
21
from pathlib import Path
3-
from typing import Optional, Union
42

53
from commitizen import defaults, git
64

75
from .base_config import BaseConfig
8-
from .ini_config import IniConfig
96
from .toml_config import TomlConfig
107

118

12-
def load_global_conf() -> Optional[IniConfig]:
13-
home = Path.home()
14-
global_cfg = home / Path(".cz")
15-
if not global_cfg.exists():
16-
return None
17-
18-
# global conf doesn't make sense with commitizen bump
19-
# so I'm deprecating it and won't test it
20-
message = (
21-
"Global conf will be deprecated in next major version. "
22-
"Use per project configuration. "
23-
"Remove '~/.cz' file from your conf folder."
24-
)
25-
warnings.simplefilter("always", DeprecationWarning)
26-
warnings.warn(message, category=DeprecationWarning)
27-
28-
with open(global_cfg, "r") as f:
29-
data = f.read()
30-
31-
conf = IniConfig(data=data, path=global_cfg)
32-
return conf
33-
34-
359
def read_cfg() -> BaseConfig:
3610
conf = BaseConfig()
3711

@@ -52,21 +26,14 @@ def read_cfg() -> BaseConfig:
5226
with open(filename, "r") as f:
5327
data: str = f.read()
5428

55-
_conf: Union[TomlConfig, IniConfig]
29+
_conf: TomlConfig
5630
if "toml" in filename.suffix:
5731
_conf = TomlConfig(data=data, path=filename)
58-
else:
59-
_conf = IniConfig(data=data, path=filename)
6032

6133
if _conf.is_empty_config:
6234
continue
6335
else:
6436
conf = _conf
6537
break
6638

67-
if not conf.path:
68-
global_conf = load_global_conf()
69-
if global_conf:
70-
conf = global_conf
71-
7239
return conf

commitizen/config/base_config.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import warnings
21
from pathlib import Path
32
from typing import Any, Dict, Optional, Union
43

@@ -34,16 +33,3 @@ def add_path(self, path: Union[str, Path]):
3433

3534
def _parse_setting(self, data: str) -> dict:
3635
raise NotImplementedError()
37-
38-
# TODO: remove "files" supported in 2.0
39-
@classmethod
40-
def _show_files_column_deprecated_warning(cls):
41-
warnings.simplefilter("always", DeprecationWarning)
42-
warnings.warn(
43-
(
44-
'"files" is renamed as "version_files" '
45-
"and will be deprecated in next major version\n"
46-
'Please repalce "files" with "version_files"'
47-
),
48-
category=DeprecationWarning,
49-
)

commitizen/config/ini_config.py

Lines changed: 0 additions & 76 deletions
This file was deleted.

commitizen/config/toml_config.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,3 @@ def _parse_setting(self, data: str):
4444
self.settings.update(doc["tool"]["commitizen"])
4545
except exceptions.NonExistentKey:
4646
self.is_empty_config = True
47-
48-
if "files" in self.settings:
49-
self.settings["version_files"] = self.settings["files"]
50-
TomlConfig._show_files_column_deprecated_warning

commitizen/defaults.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
from collections import OrderedDict
2-
from typing import Any, Dict
2+
from typing import Any, Dict, List
33

44
name: str = "cz_conventional_commits"
5-
# TODO: .cz, setup.cfg, .cz.cfg should be removed in 2.0
6-
long_term_support_config_files: list = ["pyproject.toml", ".cz.toml"]
7-
deprcated_config_files: list = [".cz", "setup.cfg", ".cz.cfg"]
8-
config_files: list = long_term_support_config_files + deprcated_config_files
5+
config_files: List[str] = ["pyproject.toml", ".cz.toml"]
96

107
DEFAULT_SETTINGS: Dict[str, Any] = {
118
"name": "cz_conventional_commits",

docs/bump.md

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ However, it will still update `pyproject.toml` and `src/__version__.py`.
131131
To fix it, you'll first `git checkout .` to reset to the status before trying to bump and update
132132
the version in `setup.py` to `1.21.0`
133133

134-
135134
## Configuration
136135

137136
### `tag_format`
@@ -155,13 +154,6 @@ In your `pyproject.toml` or `.cz.toml`
155154
tag_format = "v$minor.$major.$patch$prerelease"
156155
```
157156

158-
Or in your `.cz` (TO BE DEPRECATED)
159-
160-
```ini
161-
[commitizen]
162-
tag_format = v$minor.$major.$patch$prerelease
163-
```
164-
165157
The variables must be preceded by a `$` sign.
166158

167159
Supported variables:
@@ -198,16 +190,6 @@ version_files = [
198190
]
199191
```
200192

201-
`.cz` (TO BE DEPRECATED)
202-
203-
```ini
204-
[commitizen]
205-
version_files = [
206-
"src/__version__.py",
207-
"setup.py:version"
208-
]
209-
```
210-
211193
In the example above, we can see the reference `"setup.py:version"`.
212194
This means that it will find a file `setup.py` and will only make a change
213195
in a line containing the `version` substring.
@@ -234,13 +216,6 @@ Some examples
234216
bump_message = "release $current_version → $new_version [skip-ci]"
235217
```
236218

237-
`.cz` (TO BE DEPRECATED)
238-
239-
```ini
240-
[commitizen]
241-
bump_message = release $current_version → $new_version [skip-ci]
242-
```
243-
244219
## Custom bump
245220

246221
Read the [customizing section](./customization.md).

0 commit comments

Comments
 (0)