@@ -244,21 +244,28 @@ def extract_latest_tag_from_changelog(filepath: str) -> str | None:
244244def prepare_changelog_segments (
245245 repo : git .Repo ,
246246 latest_tag_in_file : str | None = None ,
247+ current_tag : str | None = None ,
247248) -> list [tuple [str , str , str , str ]]:
248249 tags = sorted (repo .tags , key = lambda t : t .commit .committed_datetime )
249250 if not tags :
250251 print ("[yellow]No tags found in the repository.[/yellow]" )
251252 return []
252253 first_commit = get_first_commit_hash (repo )
254+
253255 points = [first_commit ] + [tag .commit .hexsha for tag in tags ]
254256 point_names = [first_commit ] + [tag .name for tag in tags ]
257+ if current_tag is not None :
258+ point_names += ["HEAD" ]
259+ points += ["HEAD" ]
255260 start_idx = 1
256261 if latest_tag_in_file and latest_tag_in_file in point_names :
257262 idx = point_names .index (latest_tag_in_file )
258263 start_idx = idx + 1
259264 segments : list [tuple [str , str , str , str ]] = [
260265 (points [i - 1 ], points [i ], point_names [i - 1 ], point_names [i ]) for i in reversed (range (start_idx , len (points )))
261266 ]
267+ if current_tag is not None :
268+ segments [0 ] = (segments [0 ][0 ], "HEAD" , segments [0 ][2 ], current_tag )
262269 return segments
263270
264271
@@ -301,8 +308,8 @@ def handle_changelog(args: ChangelogArgs, current_tag: str | None = None) -> Non
301308 if args .output and Path (args .output ).exists () and from_raw is None and to_raw is None :
302309 latest_tag_in_file = extract_latest_tag_from_changelog (args .output )
303310 # 默认:统计所有 tag 之间的差分(不包含最新 tag 到 HEAD)
304- if from_raw is None and to_raw is None and not current_tag :
305- segments = prepare_changelog_segments (repo , latest_tag_in_file )
311+ if from_raw is None and to_raw is None :
312+ segments = prepare_changelog_segments (repo , latest_tag_in_file , current_tag )
306313 changelogs = ""
307314 with Progress () as progress :
308315 task = progress .add_task ("Generating changelog..." , total = len (segments ))
@@ -324,26 +331,6 @@ def handle_changelog(args: ChangelogArgs, current_tag: str | None = None) -> Non
324331 progress .update (task , advance = 1 )
325332 print_and_write_changelog (changelogs , args .output , prepend = bool (latest_tag_in_file ))
326333 return
327- # 新增:如果 current_tag 存在,统计最新 tag 到 HEAD,to_ref 渲染为 current_tag
328- if current_tag :
329- # 获取最新 tag
330- tags = sorted (repo .tags , key = lambda t : t .commit .committed_datetime )
331- if tags :
332- latest_tag = tags [- 1 ].name
333- from_ref = latest_tag
334- else :
335- from_ref = get_first_commit_hash (repo )
336- to_ref = repo .head .commit .hexsha
337- changelog = get_changelog_by_range (repo , from_ref , to_ref )
338- # 渲染时将 to_ref 替换为 current_tag
339- changelog = changelog .replace (f"## { to_ref } " , f"## { current_tag } " )
340- changelog = changelog .replace (f"{ from_ref } ...{ to_ref } " , f"{ from_ref } ...{ current_tag } " )
341- print_and_write_changelog (changelog , args .output , prepend = False )
342- return
343- # 否则输出指定范围的 changelog
344- from_ref , to_ref = get_git_commits_range (repo , from_raw , to_raw )
345- changelog = get_changelog_by_range (repo , from_ref , to_ref )
346- print_and_write_changelog (changelog , args .output , prepend = False )
347334
348335
349336def get_changelog_by_range (repo : git .Repo , from_ref : str , to_ref : str ) -> str :
0 commit comments