@@ -5,41 +5,47 @@ const rangeRegex = /^([\w\-/]+(?:\.[\w\-/]+)*)?(\.\.\.?)([\w\-/]+(?:\.[\w\-/]+)*
55const qualifiedRangeRegex = / ^ ( [ \w \- / ] + (?: \. [ \w \- / ] + ) * ) ( \. \. \. ? ) ( [ \w \- / ] + (?: \. [ \w \- / ] + ) * ) $ / ;
66const qualifiedDoubleDotRange = / ^ ( [ \w \- / ] + (?: \. [ \w \- / ] + ) * ) ( \. \. ) ( [ \w \- / ] + (?: \. [ \w \- / ] + ) * ) $ / ;
77const qualifiedTripleDotRange = / ^ ( [ \w \- / ] + (?: \. [ \w \- / ] + ) * ) ( \. \. \. ) ( [ \w \- / ] + (?: \. [ \w \- / ] + ) * ) $ / ;
8- const shaLikeRegex = / ( ^ [ 0 - 9 a - f ] { 40 } ( [ \^ @ ~ : ] \S * ) ? $ ) | ( ^ [ 0 ] { 40 } ( : | - ) $ ) / ;
8+ const shaWithOptionalRevisionSuffixRegex = / ( ^ [ 0 - 9 a - f ] { 40 } ( [ \^ @ ~ : ] \S * ) ? $ ) | ( ^ [ 0 ] { 40 } ( : | - ) $ ) / ;
99const shaRegex = / ( ^ [ 0 - 9 a - f ] { 40 } $ ) | ( ^ [ 0 ] { 40 } ( : | - ) $ ) / ;
10+ const shaShortRegex = / ( ^ [ 0 - 9 a - f ] { 7 , 40 } $ ) | ( ^ [ 0 ] { 40 } ( : | - ) $ ) / ;
1011const shaParentRegex = / ( ^ [ 0 - 9 a - f ] { 40 } ) \^ [ 0 - 3 ] ? $ / ;
1112const shaShortenRegex = / ^ ( .* ?) ( [ \^ @ ~ : ] .* ) ? $ / ;
1213const uncommittedRegex = / ^ [ 0 ] { 40 } (?: [ \^ @ ~ : ] \S * ) ? : ? $ / ;
1314const uncommittedStagedRegex = / ^ [ 0 ] { 40 } ( [ \^ @ ~ ] \S * ) ? : $ / ;
1415
15- function isMatch ( regex : RegExp , ref : string | undefined ) {
16- return ! ref ? false : regex . test ( ref ) ;
16+ function isMatch ( regex : RegExp , rev : string | undefined ) {
17+ return ! rev ? false : regex . test ( rev ) ;
1718}
1819
19- export function isSha ( ref : string ) : boolean {
20- return isMatch ( shaRegex , ref ) ;
20+ /** Checks if the rev looks like a SHA-1 hash
21+ * @param allowShort If true, allows short SHAs (7-40 characters)
22+ */
23+ export function isSha ( rev : string , allowShort : boolean = false ) : boolean {
24+ return isMatch ( allowShort ? shaShortRegex : shaRegex , rev ) ;
2125}
2226
23- export function isShaLike ( ref : string ) : boolean {
24- return isMatch ( shaLikeRegex , ref ) ;
27+ /** Checks if the rev looks like a SHA-1 hash with an optional revision navigation suffixes (like ^, @, ~, or :) */
28+ export function isShaWithOptionalRevisionSuffix ( rev : string ) : boolean {
29+ return isMatch ( shaWithOptionalRevisionSuffixRegex , rev ) ;
2530}
2631
27- export function isShaParent ( ref : string ) : boolean {
28- return isMatch ( shaParentRegex , ref ) ;
32+ /** Checks if the rev looks like a SHA-1 hash with a ^ parent suffix */
33+ export function isShaWithParentSuffix ( rev : string ) : boolean {
34+ return isMatch ( shaParentRegex , rev ) ;
2935}
3036
31- export function isUncommitted ( ref : string | undefined , exact : boolean = false ) : boolean {
32- return ref === uncommitted || ref === uncommittedStaged || ( ! exact && isMatch ( uncommittedRegex , ref ) ) ;
37+ export function isUncommitted ( rev : string | undefined , exact : boolean = false ) : boolean {
38+ return rev === uncommitted || rev === uncommittedStaged || ( ! exact && isMatch ( uncommittedRegex , rev ) ) ;
3339}
3440
35- export function isUncommittedParent (
36- ref : string | undefined ,
37- ) : ref is '0000000000000000000000000000000000000000^' | '0000000000000000000000000000000000000000:^' {
38- return ref === `${ uncommitted } ^` || ref === `${ uncommittedStaged } ^` ;
41+ export function isUncommittedStaged ( rev : string | undefined , exact : boolean = false ) : boolean {
42+ return rev === uncommittedStaged || ( ! exact && isMatch ( uncommittedStagedRegex , rev ) ) ;
3943}
4044
41- export function isUncommittedStaged ( ref : string | undefined , exact : boolean = false ) : boolean {
42- return ref === uncommittedStaged || ( ! exact && isMatch ( uncommittedStagedRegex , ref ) ) ;
45+ export function isUncommittedWithParentSuffix (
46+ rev : string | undefined ,
47+ ) : rev is '0000000000000000000000000000000000000000^' | '0000000000000000000000000000000000000000:^' {
48+ return rev === `${ uncommitted } ^` || rev === `${ uncommittedStaged } ^` ;
4349}
4450
4551let abbreviatedShaLength = 7 ;
@@ -52,25 +58,25 @@ export function setAbbreviatedShaLength(length: number): void {
5258}
5359
5460export function shortenRevision (
55- ref : string | undefined ,
61+ rev : string | undefined ,
5662 options ?: {
5763 strings ?: { uncommitted ?: string ; uncommittedStaged ?: string ; working ?: string } ;
5864 } ,
5965) : string {
60- if ( ref === deletedOrMissing ) return '(deleted)' ;
61- if ( ! ref ) return options ?. strings ?. working ?? '' ;
62- if ( isUncommitted ( ref ) ) {
63- return isUncommittedStaged ( ref )
66+ if ( rev === deletedOrMissing ) return '(deleted)' ;
67+ if ( ! rev ) return options ?. strings ?. working ?? '' ;
68+ if ( isUncommitted ( rev ) ) {
69+ return isUncommittedStaged ( rev )
6470 ? options ?. strings ?. uncommittedStaged ?? 'Index'
6571 : options ?. strings ?. uncommitted ?? 'Working Tree' ;
6672 }
67- if ( isRevisionRange ( ref ) || ! isShaLike ( ref ) ) return ref ;
73+ if ( isRevisionRange ( rev ) || ! isShaWithOptionalRevisionSuffix ( rev ) ) return rev ;
6874
6975 // Don't allow shas to be shortened to less than 5 characters
7076 const len = Math . max ( 5 , getAbbreviatedShaLength ( ) ) ;
7177
7278 // If we have a suffix, append it
73- const match = shaShortenRegex . exec ( ref ) ;
79+ const match = shaShortenRegex . exec ( rev ) ;
7480 if ( match != null ) {
7581 const [ , rev , suffix ] = match ;
7682
@@ -79,7 +85,7 @@ export function shortenRevision(
7985 }
8086 }
8187
82- return ref . substring ( 0 , len ) ;
88+ return rev . substring ( 0 , len ) ;
8389}
8490
8591export function createRevisionRange (
@@ -91,9 +97,9 @@ export function createRevisionRange(
9197}
9298
9399export function getRevisionRangeParts (
94- ref : GitRevisionRange ,
100+ revRange : GitRevisionRange ,
95101) : { left : string | undefined ; right : string | undefined ; notation : GitRevisionRangeNotation } | undefined {
96- const match = rangeRegex . exec ( ref ) ;
102+ const match = rangeRegex . exec ( revRange ) ;
97103 if ( match == null ) return undefined ;
98104
99105 const [ , left , notation , right ] = match ;
@@ -105,19 +111,19 @@ export function getRevisionRangeParts(
105111}
106112
107113export function isRevisionRange (
108- ref : string | undefined ,
114+ rev : string | undefined ,
109115 rangeType : 'any' | 'qualified' | 'qualified-double-dot' | 'qualified-triple-dot' = 'any' ,
110- ) : ref is GitRevisionRange {
111- if ( ref == null ) return false ;
116+ ) : rev is GitRevisionRange {
117+ if ( rev == null ) return false ;
112118
113119 switch ( rangeType ) {
114120 case 'qualified' :
115- return qualifiedRangeRegex . test ( ref ) ;
121+ return qualifiedRangeRegex . test ( rev ) ;
116122 case 'qualified-double-dot' :
117- return qualifiedDoubleDotRange . test ( ref ) ;
123+ return qualifiedDoubleDotRange . test ( rev ) ;
118124 case 'qualified-triple-dot' :
119- return qualifiedTripleDotRange . test ( ref ) ;
125+ return qualifiedTripleDotRange . test ( rev ) ;
120126 default :
121- return rangeRegex . test ( ref ) ;
127+ return rangeRegex . test ( rev ) ;
122128 }
123129}
0 commit comments