@@ -739,6 +739,17 @@ export class Git {
739739 return data . length === 0 ? undefined : data . trim ( ) ;
740740 }
741741
742+ static async log__recent ( repoPath : string ) {
743+ const data = await git < string > (
744+ { cwd : repoPath , errors : GitErrorHandling . Ignore } ,
745+ 'log' ,
746+ '-n1' ,
747+ '--format=%H' ,
748+ '--'
749+ ) ;
750+ return data . length === 0 ? undefined : data . trim ( ) ;
751+ }
752+
742753 static log__search ( repoPath : string , search : string [ ] = emptyArray , { maxCount } : { maxCount ?: number } = { } ) {
743754 const params = [ 'log' , '--name-status' , `--format=${ GitLogParser . defaultFormat } ` ] ;
744755 if ( maxCount ) {
@@ -825,49 +836,32 @@ export class Git {
825836 }
826837
827838 static async rev_parse__currentBranch ( repoPath : string ) : Promise < [ string , string | undefined ] | undefined > {
828- const params = [ 'rev-parse' , '--abbrev-ref' , '--symbolic-full-name' , '@' , '@{u}' ] ;
829-
830- const opts : GitCommandOptions = {
831- cwd : repoPath ,
832- errors : GitErrorHandling . Throw
833- } ;
834-
835839 try {
836- const data = await git < string > ( opts , ...params ) ;
840+ const data = await git < string > (
841+ { cwd : repoPath , errors : GitErrorHandling . Throw } ,
842+ 'rev-parse' ,
843+ '--abbrev-ref' ,
844+ '--symbolic-full-name' ,
845+ '@' ,
846+ '@{u}' ,
847+ '--'
848+ ) ;
837849 return [ data , undefined ] ;
838850 }
839851 catch ( ex ) {
840852 const msg = ex && ex . toString ( ) ;
841- if ( GitWarnings . headNotABranch . test ( msg ) ) {
842- const data = await git < string > (
843- { ...opts , errors : GitErrorHandling . Ignore } ,
844- 'log' ,
845- '-n1' ,
846- '--format=%H' ,
847- '--'
848- ) ;
849- if ( data . length === 0 ) return undefined ;
850-
851- // Matches output of `git branch -vv`
852- const sha = data . trim ( ) ;
853- return [ `(HEAD detached at ${ this . shortenSha ( sha ) } )` , sha ] ;
853+ if ( GitErrors . badRevision . test ( msg ) || GitWarnings . noUpstream . test ( msg ) ) {
854+ return [ ex . stdout , undefined ] ;
854855 }
855856
856- const result = GitWarnings . noUpstream . exec ( msg ) ;
857- if ( result !== null ) return [ result [ 1 ] , undefined ] ;
857+ if ( GitWarnings . headNotABranch . test ( msg ) ) {
858+ const sha = await this . log__recent ( repoPath ) ;
859+ if ( sha === undefined ) return undefined ;
858860
859- if ( GitWarnings . unknownRevision . test ( msg ) ) {
860- const data = await git < string > (
861- { ...opts , errors : GitErrorHandling . Ignore } ,
862- 'symbolic-ref' ,
863- '-q' ,
864- '--short' ,
865- 'HEAD'
866- ) ;
867- return data . length === 0 ? undefined : [ data . trim ( ) , undefined ] ;
861+ return [ `(HEAD detached at ${ this . shortenSha ( sha ) } )` , sha ] ;
868862 }
869863
870- defaultExceptionHandler ( ex , opts . cwd ) ;
864+ defaultExceptionHandler ( ex , repoPath ) ;
871865 return undefined ;
872866 }
873867 }
0 commit comments