Skip to content

Commit 6585a7f

Browse files
committed
feat(changelog): add support for single version and version range
Closes #399 #225
1 parent ec38ce3 commit 6585a7f

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

commitizen/changelog.py

+46-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import re
3030
from collections import OrderedDict, defaultdict
3131
from datetime import date
32-
from typing import Callable, Dict, Iterable, List, Optional
32+
from typing import Callable, Dict, Iterable, List, Optional, Tuple
3333

3434
from jinja2 import Environment, PackageLoader
3535

@@ -281,3 +281,48 @@ def incremental_build(new_content: str, lines: List, metadata: Dict) -> List:
281281
if not isinstance(latest_version_position, int):
282282
output_lines.append(new_content)
283283
return output_lines
284+
285+
286+
def get_tag_range(tags: List[GitTag], start: str, end: Optional[str]) -> List[GitTag]:
287+
accumulator = []
288+
keep = False
289+
if not end:
290+
end = start
291+
for index, tag in enumerate(tags):
292+
if tag.name == start:
293+
keep = True
294+
if keep:
295+
accumulator.append(tag)
296+
if tag.name == end:
297+
keep = False
298+
try:
299+
accumulator.append(tags[index + 1])
300+
except IndexError:
301+
pass
302+
break
303+
return accumulator
304+
305+
306+
def get_star_and_end_rev(
307+
tags: List[GitTag], version: str, tag_format: str, create_tag: Callable
308+
) -> Tuple[Optional[str], Optional[str]]:
309+
start: Optional[str] = None
310+
end: Optional[str] = None
311+
312+
try:
313+
start, end = version.split("..")
314+
except ValueError:
315+
end = version
316+
317+
end_tag = create_tag(end, tag_format=tag_format)
318+
319+
start_tag = None
320+
if start:
321+
start_tag = create_tag(start, tag_format=tag_format)
322+
323+
tags = get_tag_range(tags, start=end_tag, end=start_tag)
324+
if len(tags) == 0:
325+
return None, None
326+
start_rev = tags[-1].name
327+
end_rev = end_tag
328+
return start_rev, end_rev

commitizen/cli.py

+6
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@
183183
"useful if the changelog has been manually modified"
184184
),
185185
},
186+
{
187+
"name": "rev_range",
188+
"type": str,
189+
"nargs": "?",
190+
"help": "generates changelog for the given version range",
191+
},
186192
{
187193
"name": "--start-rev",
188194
"default": None,

commitizen/commands/changelog.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from operator import itemgetter
44
from typing import Callable, Dict, List, Optional
55

6-
from commitizen import changelog, factory, git, out
6+
from commitizen import bump, changelog, factory, git, out
77
from commitizen.config import BaseConfig
88
from commitizen.exceptions import (
99
DryRunExit,
@@ -46,6 +46,10 @@ def __init__(self, config: BaseConfig, args):
4646
self.change_type_order = (
4747
self.config.settings.get("change_type_order") or self.cz.change_type_order
4848
)
49+
self.rev_range = args.get("rev_range")
50+
self.tag_format = args.get("tag_format") or self.config.settings.get(
51+
"tag_format"
52+
)
4953

5054
def _find_incremental_rev(self, latest_version: str, tags: List[GitTag]) -> str:
5155
"""Try to find the 'start_rev'.
@@ -93,13 +97,24 @@ def __call__(self):
9397
if not tags:
9498
tags = []
9599

100+
end_rev = "HEAD"
101+
if self.rev_range and self.tag_format:
102+
start_rev, end_rev = changelog.get_star_and_end_rev(
103+
tags,
104+
version=self.rev_range,
105+
tag_format=self.tag_format,
106+
create_tag=bump.create_tag,
107+
)
108+
96109
if self.incremental:
97110
changelog_meta = changelog.get_metadata(self.file_name)
98111
latest_version = changelog_meta.get("latest_version")
99112
if latest_version:
100113
start_rev = self._find_incremental_rev(latest_version, tags)
101114

102-
commits = git.get_commits(start=start_rev, args="--author-date-order")
115+
commits = git.get_commits(
116+
start=start_rev, end=end_rev, args="--author-date-order"
117+
)
103118
if not commits:
104119
raise NoCommitsFoundError("No commits found")
105120

docs/customization.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ commitizen:
170170
When the [`use_shortcuts`](https://commitizen-tools.github.io/commitizen/config/#settings) config option is enabled, commitizen can show and use keyboard shortcuts to select items from lists directly.
171171
For example, when using the `cz_conventional_commits` commitizen template, shortcut keys are shown when selecting the commit type. Unless otherwise defined, keyboard shortcuts will be numbered automatically.
172172
To specify keyboard shortcuts for your custom choices, provide the shortcut using the `key` parameter in dictionary form for each choice you would like to customize.
173-
173+
174174
## 2. Customize through customizing a class
175175

176176
The basic steps are:

0 commit comments

Comments
 (0)