3
3
from operator import itemgetter
4
4
from typing import Callable , Dict , List , Optional
5
5
6
- from commitizen import changelog , factory , git , out
6
+ from commitizen import bump , changelog , factory , git , out
7
7
from commitizen .config import BaseConfig
8
8
from commitizen .exceptions import (
9
9
DryRunExit ,
10
10
NoCommitsFoundError ,
11
11
NoPatternMapError ,
12
12
NoRevisionError ,
13
13
NotAGitProjectError ,
14
+ NotAllowed ,
14
15
)
15
16
from commitizen .git import GitTag
16
17
@@ -46,6 +47,10 @@ def __init__(self, config: BaseConfig, args):
46
47
self .change_type_order = (
47
48
self .config .settings .get ("change_type_order" ) or self .cz .change_type_order
48
49
)
50
+ self .rev_range = args .get ("rev_range" )
51
+ self .tag_format = args .get ("tag_format" ) or self .config .settings .get (
52
+ "tag_format"
53
+ )
49
54
50
55
def _find_incremental_rev (self , latest_version : str , tags : List [GitTag ]) -> str :
51
56
"""Try to find the 'start_rev'.
@@ -73,6 +78,30 @@ def _find_incremental_rev(self, latest_version: str, tags: List[GitTag]) -> str:
73
78
start_rev = tag .name
74
79
return start_rev
75
80
81
+ def write_changelog (
82
+ self , changelog_out : str , lines : List [str ], changelog_meta : Dict
83
+ ):
84
+ if not isinstance (self .file_name , str ):
85
+ raise NotAllowed (
86
+ "Changelog file name is broken.\n "
87
+ "Check the flag `--file-name` in the terminal "
88
+ f"or the setting `changelog_file` in { self .config .path } "
89
+ )
90
+
91
+ changelog_hook : Optional [Callable ] = self .cz .changelog_hook
92
+ with open (self .file_name , "w" ) as changelog_file :
93
+ partial_changelog : Optional [str ] = None
94
+ if self .incremental :
95
+ new_lines = changelog .incremental_build (
96
+ changelog_out , lines , changelog_meta
97
+ )
98
+ changelog_out = "" .join (new_lines )
99
+ partial_changelog = changelog_out
100
+
101
+ if changelog_hook :
102
+ changelog_out = changelog_hook (changelog_out , partial_changelog )
103
+ changelog_file .write (changelog_out )
104
+
76
105
def __call__ (self ):
77
106
commit_parser = self .cz .commit_parser
78
107
changelog_pattern = self .cz .changelog_pattern
@@ -83,23 +112,38 @@ def __call__(self):
83
112
changelog_message_builder_hook : Optional [
84
113
Callable
85
114
] = self .cz .changelog_message_builder_hook
86
- changelog_hook : Optional [ Callable ] = self . cz . changelog_hook
115
+
87
116
if not changelog_pattern or not commit_parser :
88
117
raise NoPatternMapError (
89
118
f"'{ self .config .settings ['name' ]} ' rule does not support changelog"
90
119
)
91
120
121
+ if self .incremental and self .rev_range :
122
+ raise NotAllowed ("--incremental cannot be combined with a rev_range" )
123
+
92
124
tags = git .get_tags ()
93
125
if not tags :
94
126
tags = []
95
127
128
+ end_rev = "HEAD"
129
+
96
130
if self .incremental :
97
131
changelog_meta = changelog .get_metadata (self .file_name )
98
132
latest_version = changelog_meta .get ("latest_version" )
99
133
if latest_version :
100
134
start_rev = self ._find_incremental_rev (latest_version , tags )
101
135
102
- commits = git .get_commits (start = start_rev , args = "--author-date-order" )
136
+ if self .rev_range and self .tag_format :
137
+ start_rev , end_rev = changelog .get_start_and_end_rev (
138
+ tags ,
139
+ version = self .rev_range ,
140
+ tag_format = self .tag_format ,
141
+ create_tag = bump .create_tag ,
142
+ )
143
+
144
+ commits = git .get_commits (
145
+ start = start_rev , end = end_rev , args = "--author-date-order"
146
+ )
103
147
if not commits :
104
148
raise NoCommitsFoundError ("No commits found" )
105
149
@@ -126,15 +170,4 @@ def __call__(self):
126
170
with open (self .file_name , "r" ) as changelog_file :
127
171
lines = changelog_file .readlines ()
128
172
129
- with open (self .file_name , "w" ) as changelog_file :
130
- partial_changelog : Optional [str ] = None
131
- if self .incremental :
132
- new_lines = changelog .incremental_build (
133
- changelog_out , lines , changelog_meta
134
- )
135
- changelog_out = "" .join (new_lines )
136
- partial_changelog = changelog_out
137
-
138
- if changelog_hook :
139
- changelog_out = changelog_hook (changelog_out , partial_changelog )
140
- changelog_file .write (changelog_out )
173
+ self .write_changelog (changelog_out , lines , changelog_meta )
0 commit comments