Skip to content

Commit ddd220a

Browse files
committed
feat: custom cz plugins now support bumping version
1 parent ad17acf commit ddd220a

File tree

5 files changed

+30
-11
lines changed

5 files changed

+30
-11
lines changed

commitizen/bump.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,13 @@
44
from string import Template
55
from packaging.version import Version
66
from typing import List, Optional, Union
7-
8-
MAJOR = "MAJOR"
9-
MINOR = "MINOR"
10-
PATCH = "PATCH"
11-
conventional_commits_pattern = r"^(BREAKING CHANGE|feat)"
12-
conventional_commits_map = {"BREAKING CHANGE": MAJOR, "feat": MINOR}
7+
from commitizen.defaults import MAJOR, MINOR, PATCH, bump_pattern, bump_map
138

149

1510
def find_increment(
1611
messages: List[str],
17-
regex: str = conventional_commits_pattern,
18-
increments_map: dict = conventional_commits_map,
12+
regex: str = bump_pattern,
13+
increments_map: dict = bump_map,
1914
) -> str:
2015

2116
# Most important cases are major and minor.

commitizen/commands/bump.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
from typing import Optional
33

44
import questionary
5-
from commitizen import bump, git, config, out
5+
from commitizen import bump, git, config, out, factory
66

77
NO_COMMITS_FOUND = 3
88
NO_VERSION_SPECIFIED = 4
9+
NO_PATTERN_MAP = 7
910

1011

1112
class Bump:
@@ -22,6 +23,7 @@ def __init__(self, config: dict, arguments: dict):
2223
if arguments[key] is not None
2324
},
2425
}
26+
self.cz = factory.commiter_factory(self.config)
2527

2628
def __call__(self):
2729
"""Steps executed to bump."""
@@ -32,6 +34,7 @@ def __call__(self):
3234
out.error("Check if current version is specified in config file, like:")
3335
out.error("version = 0.4.3")
3436
raise SystemExit(NO_VERSION_SPECIFIED)
37+
3538
current_version: str = self.config["version"]
3639
tag_format: str = self.parameters["tag_format"]
3740
current_tag_version: str = bump.create_tag(
@@ -66,7 +69,14 @@ def __call__(self):
6669
raise SystemExit(NO_COMMITS_FOUND)
6770

6871
if increment is None:
69-
increment = bump.find_increment(commits)
72+
bump_pattern = self.cz.bump_pattern
73+
bump_map = self.cz.bump_map
74+
if not bump_map or not bump_pattern:
75+
out.error(f"'{self.config['name']}' rule does not support bump")
76+
raise SystemExit(NO_PATTERN_MAP)
77+
increment = bump.find_increment(
78+
commits, regex=bump_pattern, increments_map=bump_map
79+
)
7080

7181
# Increment is removed when current and next version
7282
# are expected to be prereleases.

commitizen/cz/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
from typing import Optional
12
from abc import ABCMeta, abstractmethod
23

34

45
class BaseCommitizen(metaclass=ABCMeta):
6+
bump_pattern: Optional[str] = None
7+
bump_map: Optional[dict] = None
8+
59
def __init__(self, config: dict):
610
self.config = config
711

commitizen/cz/conventional_commits/conventional_commits.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
from commitizen.cz.base import BaseCommitizen
3-
3+
from commitizen import defaults
44
__all__ = ["ConventionalCommitsCz"]
55

66

@@ -30,6 +30,9 @@ def parse_subject(text):
3030

3131

3232
class ConventionalCommitsCz(BaseCommitizen):
33+
bump_pattern = defaults.bump_pattern
34+
bump_map = defaults.bump_map
35+
3336
def questions(self) -> list:
3437
questions = [
3538
{

commitizen/defaults.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,10 @@
88
"files": [],
99
"tag_format": None, # example v$version
1010
}
11+
12+
MAJOR = "MAJOR"
13+
MINOR = "MINOR"
14+
PATCH = "PATCH"
15+
16+
bump_pattern = r"^(BREAKING CHANGE|feat)"
17+
bump_map = {"BREAKING CHANGE": MAJOR, "feat": MINOR}

0 commit comments

Comments
 (0)