@@ -18,37 +18,64 @@ export async function isRepoShallow() {
1818 return ( await execCommand ( 'git' , [ 'rev-parse' , '--is-shallow-repository' ] ) ) . trim ( ) === 'true'
1919}
2020
21- export async function getGitTags ( ) {
22- return ( await execCommand ( 'git' , [ '--no-pager' , 'tag' , '-l' , '--sort=creatordate' ] ) . then ( r => r . split ( '\n' ) ) )
23- . reverse ( )
21+ export function getSafeTagTemplate ( template : string ) {
22+ return template . includes ( '%s' ) ? template : `${ template } %s`
2423}
2524
26- function getTagWithoutPrefix ( tag : string ) {
27- return tag . replace ( / ^ v / , '' )
25+ function getVersionString ( template : string , tag : string ) {
26+ const pattern = template . replace ( / % s / g, '(.+)' )
27+ const regex = new RegExp ( `^${ pattern } $` )
28+ const match = regex . exec ( tag )
29+ return match ? match [ 1 ] : tag
2830}
2931
30- export async function getLastMatchingTag ( inputTag : string , tagFilter : ( tag : string ) => boolean ) {
31- const inputTagWithoutPrefix = getTagWithoutPrefix ( inputTag )
32- const isVersion = semver . valid ( inputTagWithoutPrefix ) !== null
33- const isPrerelease = semver . prerelease ( inputTag ) !== null
32+ export async function getGitTags ( ) {
33+ const output = await execCommand ( 'git' , [
34+ 'log' ,
35+ '--simplify-by-decoration' ,
36+ '--pretty=format:"%d"' ,
37+ ] )
38+
39+ const tagRegex = / t a g : ( [ ^ , ) ] + ) / g
40+ const tagList : string [ ] = [ ]
41+ let match
42+
43+ while ( match !== null ) {
44+ const tag = match ?. [ 1 ] . trim ( )
45+ if ( tag ) {
46+ tagList . push ( tag )
47+ }
48+ match = tagRegex . exec ( output )
49+ }
50+
51+ return tagList
52+ }
53+
54+ export async function getLastMatchingTag (
55+ inputTag : string ,
56+ tagFilter : ( tag : string ) => boolean ,
57+ tagTemplate : string ,
58+ ) {
59+ const inputVersionString = getVersionString ( tagTemplate , inputTag )
60+ const isVersion = semver . valid ( inputVersionString ) !== null
61+ const isPrerelease = semver . prerelease ( inputVersionString ) !== null
3462 const tags = await getGitTags ( )
3563 const filteredTags = tags . filter ( tagFilter )
3664
3765 let tag : string | undefined
3866 // Doing a stable release, find the last stable release to compare with
3967 if ( ! isPrerelease && isVersion ) {
4068 tag = filteredTags . find ( ( tag ) => {
41- const tagWithoutPrefix = getTagWithoutPrefix ( tag )
69+ const versionString = getVersionString ( tagTemplate , tag )
4270
43- return tagWithoutPrefix !== inputTagWithoutPrefix
44- && semver . valid ( tagWithoutPrefix ) !== null
45- && semver . prerelease ( tagWithoutPrefix ) === null
71+ return versionString !== inputVersionString
72+ && semver . valid ( versionString ) !== null
73+ && semver . prerelease ( versionString ) === null
4674 } )
4775 }
4876
4977 // Fallback to the last tag, that are not the input tag
5078 tag ||= filteredTags . find ( tag => tag !== inputTag )
51-
5279 return tag
5380}
5481
0 commit comments