99
1010import itertools
1111import re
12+ import typing as t
1213from pathlib import Path
1314
1415import tomllib
1516from git import GitCommandError , Repo
17+ from packaging .version import Version
1618from packaging .version import parse as parse_version
1719
1820# Read Towncrier settings
5153)
5254
5355
54- def get_changelog (repo , branch ) :
56+ def get_changelog (repo : Repo , branch : str ) -> str :
5557 branch_tc_settings = tomllib .loads (repo .git .show (f"{ branch } :pyproject.toml" ))["tool" ][
5658 "towncrier"
5759 ]
5860 branch_changelog_file = branch_tc_settings .get ("filename" , "NEWS.rst" )
5961 return repo .git .show (f"{ branch } :{ branch_changelog_file } " ) + "\n "
6062
6163
62- def _tokenize_changes (splits ) :
64+ def _tokenize_changes (splits : list [ str ]) -> t . Iterator [ list [ Version | str ]] :
6365 assert len (splits ) % 3 == 0
6466 for i in range (len (splits ) // 3 ):
6567 title = splits [3 * i ]
6668 version = parse_version (splits [3 * i + 1 ])
6769 yield [version , title + splits [3 * i + 2 ]]
6870
6971
70- def split_changelog (changelog ) :
72+ def split_changelog (changelog : str ) -> tuple [ str , list [ list [ Version | str ]]] :
7173 preamble , rest = changelog .split (START_STRING , maxsplit = 1 )
7274 split_rest = re .split (TITLE_REGEX , rest )
7375 return preamble + START_STRING + split_rest [0 ], list (_tokenize_changes (split_rest [1 :]))
7476
7577
76- def main ():
78+ def main () -> None :
7779 repo = Repo (Path .cwd ())
7880 remote = repo .remotes [0 ]
7981 branches = [ref for ref in remote .refs if re .match (r"^([0-9]+)\.([0-9]+)$" , ref .remote_head )]
@@ -91,7 +93,7 @@ def main():
9193 except GitCommandError :
9294 print ("No changelog found on this branch." )
9395 continue
94- dummy , changes = split_changelog (changelog )
96+ _dummy , changes = split_changelog (changelog )
9597 new_changes = sorted (main_changes + changes , key = lambda x : x [0 ], reverse = True )
9698 # Now remove duplicates (retain the first one)
9799 main_changes = [new_changes [0 ]]
0 commit comments