@@ -7,6 +7,8 @@ interface DescribeRefOptions {
77 cwd ?: string | URL ;
88 // Glob passed to `--match` flag
99 match ?: string ;
10+ // Separator used within independent version tags, defaults to @
11+ separator ?: string ;
1012}
1113
1214// When annotated release tags are missing
@@ -60,7 +62,7 @@ export function describeRef(
6062 // TODO: refactor based on TS feedback
6163 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
6264 // @ts -ignore
63- const result = parse ( stdout , options . cwd ) ;
65+ const result = parse ( stdout , options . cwd , options . separator ) ;
6466
6567 log . verbose ( "git-describe" , "%j => %j" , options && options . match , stdout ) ;
6668 log . silly ( "git-describe" , "parsed => %j" , result ) ;
@@ -69,12 +71,15 @@ export function describeRef(
6971 } ) ;
7072}
7173
72- export function describeRefSync ( options = { } , includeMergedTags ?: boolean ) {
74+ export function describeRefSync (
75+ options : DescribeRefOptions = { } ,
76+ includeMergedTags ?: boolean
77+ ) : DescribeRefFallbackResult | DescribeRefDetailedResult {
7378 const stdout = childProcess . execSync ( "git" , getArgs ( options , includeMergedTags ) , options ) ;
7479 // TODO: refactor based on TS feedback
7580 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
7681 // @ts -ignore
77- const result = parse ( stdout , options . cwd ) ;
82+ const result = parse ( stdout , options . cwd , options . separator ) ;
7883
7984 // only called by collect-updates with no matcher
8085 log . silly ( "git-describe.sync" , "%j => %j" , stdout , result ) ;
@@ -86,8 +91,15 @@ export function describeRefSync(options = {}, includeMergedTags?: boolean) {
8691 * Parse git output and return relevant metadata.
8792 * @param stdout Result of `git describe`
8893 * @param [cwd] Defaults to `process.cwd()`
94+ * @param [separator] Separator used within independent version tags, defaults to @
8995 */
90- function parse ( stdout : string , cwd : string ) : DescribeRefFallbackResult | DescribeRefDetailedResult {
96+ function parse (
97+ stdout : string ,
98+ cwd : string ,
99+ separator : string
100+ ) : DescribeRefFallbackResult | DescribeRefDetailedResult {
101+ separator = separator || "@" ;
102+
91103 const minimalShaRegex = / ^ ( [ 0 - 9 a - f ] { 7 , 40 } ) ( - d i r t y ) ? $ / ;
92104 // when git describe fails to locate tags, it returns only the minimal sha
93105 if ( minimalShaRegex . test ( stdout ) ) {
@@ -103,8 +115,11 @@ function parse(stdout: string, cwd: string): DescribeRefFallbackResult | Describ
103115 return { refCount, sha, isDirty : Boolean ( isDirty ) } ;
104116 }
105117
106- const [ , lastTagName , lastVersion , refCount , sha , isDirty ] =
107- / ^ ( (?: .* @ ) ? ( .* ) ) - ( \d + ) - g ( [ 0 - 9 a - f ] + ) ( - d i r t y ) ? $ / . exec ( stdout ) || [ ] ;
118+ // If the user has specified a custom separator, it may not be regex-safe, so escape it
119+ const escapedSeparator = separator . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, "\\$&" ) ;
120+ const regexPattern = new RegExp ( `^((?:.*${ escapedSeparator } )?(.*))-(\\d+)-g([0-9a-f]+)(-dirty)?$` ) ;
121+
122+ const [ , lastTagName , lastVersion , refCount , sha , isDirty ] = regexPattern . exec ( stdout ) || [ ] ;
108123
109124 return { lastTagName, lastVersion, refCount, sha, isDirty : Boolean ( isDirty ) } ;
110125}
0 commit comments