33from operator import itemgetter
44from typing import Callable , Dict , List , Optional
55
6- from commitizen import changelog , factory , git , out
6+ from commitizen import bump , changelog , factory , git , out
77from commitizen .config import BaseConfig
88from commitizen .exceptions import (
99 DryRunExit ,
1010 NoCommitsFoundError ,
1111 NoPatternMapError ,
1212 NoRevisionError ,
1313 NotAGitProjectError ,
14+ NotAllowed ,
1415)
1516from commitizen .git import GitTag
1617
@@ -46,6 +47,10 @@ def __init__(self, config: BaseConfig, args):
4647 self .change_type_order = (
4748 self .config .settings .get ("change_type_order" ) or self .cz .change_type_order
4849 )
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+ )
4954
5055 def _find_incremental_rev (self , latest_version : str , tags : List [GitTag ]) -> str :
5156 """Try to find the 'start_rev'.
@@ -73,6 +78,30 @@ def _find_incremental_rev(self, latest_version: str, tags: List[GitTag]) -> str:
7378 start_rev = tag .name
7479 return start_rev
7580
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+
76105 def __call__ (self ):
77106 commit_parser = self .cz .commit_parser
78107 changelog_pattern = self .cz .changelog_pattern
@@ -83,23 +112,38 @@ def __call__(self):
83112 changelog_message_builder_hook : Optional [
84113 Callable
85114 ] = self .cz .changelog_message_builder_hook
86- changelog_hook : Optional [ Callable ] = self . cz . changelog_hook
115+
87116 if not changelog_pattern or not commit_parser :
88117 raise NoPatternMapError (
89118 f"'{ self .config .settings ['name' ]} ' rule does not support changelog"
90119 )
91120
121+ if self .incremental and self .rev_range :
122+ raise NotAllowed ("--incremental cannot be combined with a rev_range" )
123+
92124 tags = git .get_tags ()
93125 if not tags :
94126 tags = []
95127
128+ end_rev = "HEAD"
129+
96130 if self .incremental :
97131 changelog_meta = changelog .get_metadata (self .file_name )
98132 latest_version = changelog_meta .get ("latest_version" )
99133 if latest_version :
100134 start_rev = self ._find_incremental_rev (latest_version , tags )
101135
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+ )
103147 if not commits :
104148 raise NoCommitsFoundError ("No commits found" )
105149
@@ -126,15 +170,4 @@ def __call__(self):
126170 with open (self .file_name , "r" ) as changelog_file :
127171 lines = changelog_file .readlines ()
128172
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