@@ -603,33 +603,47 @@ export class Git {
603603 }
604604 }
605605
606- static async revparse_currentBranch ( repoPath : string ) : Promise < string | undefined > {
606+ static async revparse_currentBranch ( repoPath : string ) : Promise < [ string , string ? ] | undefined > {
607607 const params = [ 'rev-parse' , '--abbrev-ref' , '--symbolic-full-name' , '@' , '@{u}' ] ;
608608
609609 const opts = { cwd : repoPath } as CommandOptions ;
610610 try {
611611 const data = await gitCommandCore ( opts , ...params ) ;
612- return data ;
612+ return [ data , undefined ] ;
613613 }
614614 catch ( ex ) {
615615 const msg = ex && ex . toString ( ) ;
616- if ( GitWarnings . headNotABranch . test ( msg ) ) return undefined ;
616+ if ( GitWarnings . headNotABranch . test ( msg ) ) {
617+ try {
618+ const params = [ 'log' , '-n1' , '--format=%H' , '--' ] ;
619+ const data = await gitCommandCore ( opts , ...params ) ;
620+ if ( data === undefined ) return undefined ;
621+
622+ // Matches output of `git branch -vv`
623+ const sha = data . trim ( ) ;
624+ return [ `(HEAD detached at ${ this . shortenSha ( sha ) } )` , sha ] ;
625+ }
626+ catch {
627+ return undefined ;
628+ }
629+ }
617630
618631 const result = GitWarnings . noUpstream . exec ( msg ) ;
619- if ( result !== null ) return result [ 1 ] ;
632+ if ( result !== null ) return [ result [ 1 ] , undefined ] ;
620633
621634 if ( GitWarnings . unknownRevision . test ( msg ) ) {
622635 try {
623636 const params = [ 'symbolic-ref' , '-q' , '--short' , 'HEAD' ] ;
624637 const data = await gitCommandCore ( opts , ...params ) ;
625- return data ;
638+ return [ data , undefined ] ;
626639 }
627640 catch {
628641 return undefined ;
629642 }
630643 }
631644
632- return gitCommandDefaultErrorHandler ( ex , opts , ...params ) ;
645+ gitCommandDefaultErrorHandler ( ex , opts , ...params ) ;
646+ return undefined ;
633647 }
634648 }
635649
0 commit comments