Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cb1dd20

Browse files
committed
feat: new working bump command
1 parent 9c7450f commit cb1dd20

File tree

17 files changed

+200
-82
lines changed

17 files changed

+200
-82
lines changed

‎.cz.cfg‎

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

‎commitizen/bump.py‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from itertools import zip_longest
44
from string import Template
55
from packaging.version import Version
6-
from typing import List, Optional
6+
from typing import List, Optional, Union
77

88
MAJOR = "MAJOR"
99
MINOR = "MINOR"
@@ -86,7 +86,7 @@ def semver_generator(current_version: str, increment: str = None) -> str:
8686

8787

8888
def generate_version(
89-
current_version: str, increment: str=None, prerelease: Optional[str] = None
89+
current_version: str, increment: str, prerelease: Optional[str] = None
9090
) -> Version:
9191
"""Based on the given increment a proper semver will be generated.
9292
@@ -106,7 +106,7 @@ def generate_version(
106106
return Version(f"{semver}{pre_version}")
107107

108108

109-
def update_version_in_files(old_version: str, new_version: str, files: list):
109+
def update_version_in_files(current_version: str, new_version: str, files: list):
110110
"""Change old version to the new one in every file given.
111111
112112
Note that this version is not the tag formatted one.
@@ -119,21 +119,38 @@ def update_version_in_files(old_version: str, new_version: str, files: list):
119119
filedata = file.read()
120120

121121
# Replace the target string
122-
filedata = filedata.replace(old_version, new_version)
122+
filedata = filedata.replace(current_version, new_version)
123123

124124
# Write the file out again
125125
with open(filepath, "w") as file:
126126
file.write(filedata)
127127

128128

129-
def create_tag(version: Version, tag_format: str):
129+
def create_tag(version: Union[Version, str], tag_format: Optional[str] = None):
130+
"""The tag and the software version might be different.
131+
132+
That's why this function exists.
133+
134+
Example:
135+
136+
| tag | version (PEP 0440) |
137+
| --- | ------- |
138+
| v0.9.0 | 0.9.0 |
139+
| ver1.0.0 | 1.0.0 |
140+
| ver1.0.0.a0 | 1.0.0a0 |
141+
142+
"""
143+
if isinstance(version, str):
144+
version = Version(version)
145+
130146
if not tag_format:
131147
return version.public
132148

133149
major, minor, patch = version.release
134150
prerelease = ""
135151
if version.is_prerelease:
136152
prerelease = f"{version.pre[0]}{version.pre[1]}"
153+
137154
t = Template(tag_format)
138155
return t.safe_substitute(
139156
version=version, major=major, minor=minor, patch=patch, prerelease=prerelease

‎commitizen/cli.py‎

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -61,34 +61,26 @@
6161
{
6262
"name": "--dry-run",
6363
"action": "store_true",
64-
"help": "show output to stdout",
64+
"help": "show output to stdout, no commit, no modified files",
6565
},
6666
{
67-
"name": "--branch",
68-
"help": "brach used to compare latest tag"
67+
"name": "--tag-format",
68+
"help": (
69+
"format used to tag the commmit and read it, "
70+
"use it in existing projects, "
71+
"wrap around simple quotes."
72+
),
6973
},
7074
{
71-
"name": "--tag_format",
72-
"help": "format used to tag the commmit and read it"
75+
"name": ["--prerelease", "-pr"],
76+
"help": "choose type of prerelease",
77+
"choices": ["alpha", "beta", "rc"],
7378
},
7479
{
75-
"name": ["--alpha", "-a"],
76-
"help": "tag the version as alpha",
77-
"action": "store_true",
78-
"exclusive_group": "prerelease"
79-
},
80-
{
81-
"name": ["--beta", "-b"],
82-
"help": "tag the version as beta",
83-
"action": "store_true",
84-
"exclusive_group": "prerelease"
80+
"name": ["--increment"],
81+
"help": "manually specify the desired increment",
82+
"choices": ["MAJOR", "MINOR", "PATCH"],
8583
},
86-
{
87-
"name": "--rc",
88-
"help": "tag the version as release candidate",
89-
"action": "store_true",
90-
"exclusive_group": "prerelease"
91-
}
9284
],
9385
},
9486
],
@@ -133,7 +125,7 @@ def main():
133125
# Show help if no arg provided
134126
if len(sys.argv) == 1:
135127
parser.print_help(sys.stderr)
136-
raise SystemExit(1)
128+
raise SystemExit()
137129

138130
args = parser.parse_args()
139131

@@ -151,4 +143,4 @@ def main():
151143
out.line(__version__)
152144
raise SystemExit()
153145

154-
args.func(conf)(args)
146+
args.func(conf, vars(args))()

‎commitizen/commands/bump.py‎

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,95 @@
1-
# from commitizen import bump
1+
from packaging.version import Version
2+
from typing import Optional
3+
4+
import questionary
5+
from commitizen import bump, git, config, out
6+
7+
NO_COMMITS_FOUND = 3
8+
NO_VERSION_SPECIFIED = 4
29

310

411
class Bump:
512
"""Show prompt for the user to create a guided commit."""
613

7-
def __init__(self, config: dict):
14+
def __init__(self, config: dict, arguments: dict):
815
self.config: dict = config
9-
# self.cz = factory.commiter_factory(self.config)
16+
self.arguments: dict = arguments
17+
self.parameters: dict = {
18+
**config,
19+
**{
20+
key: arguments[key]
21+
for key in ["dry_run", "tag_format", "prerelease", "increment"]
22+
if arguments[key] is not None
23+
},
24+
}
25+
26+
def __call__(self):
27+
"""Steps executed to bump."""
28+
try:
29+
current_version_instance: Version = Version(self.parameters["version"])
30+
except TypeError:
31+
out.error("[NO_VERSION_SPECIFIED]")
32+
out.error("Check if current version is specified in config file, like:")
33+
out.error("version = 0.4.3")
34+
raise SystemExit(NO_VERSION_SPECIFIED)
35+
current_version: str = self.config["version"]
36+
tag_format: str = self.parameters["tag_format"]
37+
current_tag_version: str = bump.create_tag(
38+
current_version, tag_format=tag_format
39+
)
40+
files: list = self.parameters["files"]
41+
dry_run: bool = self.parameters["dry_run"]
42+
43+
prerelease: str = self.arguments["prerelease"]
44+
increment: Optional[str] = self.arguments["increment"]
45+
is_initial: bool = False
46+
47+
# Check if reading the whole git tree up to HEAD is needed.
48+
if not git.tag_exist(current_tag_version):
49+
out.info(f"Tag {current_tag_version} could not be found. ")
50+
out.info(
51+
(
52+
"Possible causes:\n"
53+
"- version in configuration is not the current version\n"
54+
"- tag_format is missing, check them using 'git tag --list'\n"
55+
)
56+
)
57+
is_initial = questionary.confirm("Is this the first tag created?").ask()
58+
59+
commits = git.get_commits(current_tag_version, from_beginning=is_initial)
60+
61+
# No commits, there is no need to create an empty tag.
62+
# Unless we previously had a prerelease.
63+
if not commits and not current_version_instance.is_prerelease:
64+
out.error("[NO_COMMITS_FOUND]")
65+
out.error("No new commits found.")
66+
raise SystemExit(NO_COMMITS_FOUND)
67+
68+
if increment is None:
69+
increment = bump.find_increment(commits)
70+
71+
# Increment is removed when current and next version
72+
# are expected to be prereleases.
73+
if prerelease and current_version_instance.is_prerelease:
74+
increment = None
75+
76+
new_version = bump.generate_version(
77+
current_version, increment, prerelease=prerelease
78+
)
79+
new_tag_version = bump.create_tag(new_version, tag_format=tag_format)
80+
message = f"Bump version {current_version}{new_version}"
81+
82+
# Report found information
83+
out.write(message)
84+
out.write(f"Tag to create: {new_tag_version}")
85+
out.write(f"Increment detected: {increment}")
86+
87+
# Do not perform operations over files or git.
88+
if dry_run:
89+
raise SystemExit()
1090

11-
def __call__(self, *args, **kwargs):
12-
# self.cz.run(*args, **kwargs)
13-
print(*args, **kwargs)
91+
config.set_key("version", new_version.public)
92+
bump.update_version_in_files(current_version, new_version.public, files)
93+
git.commit(message, args="-a")
94+
git.tag(new_tag_version)
95+
out.success("Done!")

‎commitizen/commands/commit.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
class Commit:
55
"""Show prompt for the user to create a guided commit."""
66

7-
def __init__(self, config: dict):
7+
def __init__(self, config: dict, *args):
88
self.config: dict = config
99
self.cz = factory.commiter_factory(self.config)
1010

11-
def __call__(self, *args, **kwargs):
12-
self.cz.run(*args, **kwargs)
11+
def __call__(self):
12+
self.cz.run()

‎commitizen/commands/example.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
class Example:
55
"""Show an example so people understands the rules."""
66

7-
def __init__(self, config: dict):
7+
def __init__(self, config: dict, *args):
88
self.config: dict = config
99
self.cz = factory.commiter_factory(self.config)
1010

11-
def __call__(self, *args, **kwargs):
11+
def __call__(self):
1212
self.cz.show_example()

‎commitizen/commands/info.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
class Info:
55
"""Show in depth explanation of your rules."""
66

7-
def __init__(self, config: dict):
7+
def __init__(self, config: dict, *args):
88
self.config: dict = config
99
self.cz = factory.commiter_factory(self.config)
1010

11-
def __call__(self, *args, **kwargs):
11+
def __call__(self):
1212
self.cz.show_info()

‎commitizen/commands/list_cz.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
class ListCz:
66
"""List currently installed rules."""
77

8-
def __init__(self, config: dict):
8+
def __init__(self, config: dict, *args):
99
self.config: dict = config
1010

11-
def __call__(self, *args, **kwargs):
11+
def __call__(self):
1212
out.write("\n".join(registry.keys()))

‎commitizen/commands/schema.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
class Schema:
55
"""Show structure of the rule."""
66

7-
def __init__(self, config: dict):
7+
def __init__(self, config: dict, *args):
88
self.config: dict = config
99
self.cz = factory.commiter_factory(self.config)
1010

11-
def __call__(self, *args, **kwargs):
11+
def __call__(self):
1212
self.cz.show_schema()

‎commitizen/config.py‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ def __init__(self):
2121
def config(self):
2222
return self._config
2323

24-
@property
25-
def configparser_dict(self):
26-
c = self._config.copy()
27-
if "files" in c:
28-
c.update({"files": json.dumps(c["files"])})
29-
return c
30-
3124
@property
3225
def path(self):
3326
return self._path

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /