11import * as cp from 'child_process' ;
22import { Config } from './config' ;
3- import { GitCommandStatus , GitCommit , GitCommitDetails , GitCommitNode , GitFileChangeType , GitRef , GitResetMode , GitUnsavedChanges } from './types' ;
3+ import { GitCommandStatus , GitCommit , GitCommitDetails , GitCommitNode , GitFileChangeType , GitRefData , GitResetMode , GitUnsavedChanges } from './types' ;
44
55const eolRegex = / \r \n | \r | \n / g;
6+ const headRegex = / ^ \( H E A D d e t a c h e d a t [ 0 - 9 A - Z a - z ] + \) / g;
7+
68const gitLogSeparator = 'XX7Nal-YARtTpjCikii9nJxER19D6diSyk-AWkPb' ;
79const gitLogFormat = [ '%H' , '%P' , '%an' , '%ae' , '%at' , '%s' ] . join ( gitLogSeparator ) ;
810const gitCommitDetailsFormat = [ '%H' , '%P' , '%an' , '%ae' , '%at' , '%cn' , '%B' ] . join ( gitLogSeparator ) ;
@@ -37,9 +39,10 @@ export class DataSource {
3739 let lines = stdout . split ( eolRegex ) ;
3840 let branches : string [ ] = [ ] ;
3941 for ( let i = 0 ; i < lines . length - 1 ; i ++ ) {
40- let active = lines [ i ] [ 0 ] === '*' ;
41- let name = lines [ i ] . substring ( 2 ) . split ( ' ' ) [ 0 ] ;
42- if ( active ) {
42+ let name = lines [ i ] . substring ( 2 ) . split ( ' -> ' ) [ 0 ] ;
43+ if ( name . match ( headRegex ) !== null ) continue ;
44+
45+ if ( lines [ i ] [ 0 ] === '*' ) {
4346 branches . unshift ( name ) ;
4447 } else {
4548 branches . push ( name ) ;
@@ -53,30 +56,22 @@ export class DataSource {
5356 } ) ;
5457 }
5558
56- public async getCommits ( branch : string , maxCommits : number , showRemoteBranches : boolean , currentBranch : string | null ) {
57- let i , j ;
59+ public async getCommits ( branch : string , maxCommits : number , showRemoteBranches : boolean ) {
5860 let commits = await this . getGitLog ( branch , maxCommits + 1 , showRemoteBranches ) ;
59- let refs = await this . getRefs ( showRemoteBranches ) ;
60- let unsavedChanges = null ;
61+ let refData = await this . getRefs ( showRemoteBranches ) ;
62+ let i , unsavedChanges = null ;
6163
6264 let moreCommitsAvailable = commits . length === maxCommits + 1 ;
6365 if ( moreCommitsAvailable ) commits . pop ( ) ;
6466
65- let currentBranchHash = null ;
66- for ( i = 0 ; i < refs . length ; i ++ ) {
67- if ( refs [ i ] . name === currentBranch && refs [ i ] . type === 'head' ) {
68- currentBranchHash = refs [ i ] . hash ;
69- break ;
70- }
71- }
72- if ( currentBranchHash !== null && ( branch === '' || branch === currentBranch ) ) {
73- unsavedChanges = ( new Config ( ) ) . showUncommittedChanges ( ) ? await this . getGitUnsavedChanges ( ) : null ;
74- if ( unsavedChanges !== null ) {
75- for ( j = 0 ; j < commits . length ; j ++ ) {
76- if ( currentBranchHash === commits [ j ] . hash ) {
77- commits . unshift ( { hash : '*' , parentHashes : [ currentBranchHash ] , author : '*' , email : '' , date : Math . round ( ( new Date ( ) ) . getTime ( ) / 1000 ) , message : 'Uncommitted Changes (' + unsavedChanges . changes + ')' } ) ;
78- break ;
67+ if ( refData . head !== null ) {
68+ for ( i = 0 ; i < commits . length ; i ++ ) {
69+ if ( refData . head === commits [ i ] . hash ) {
70+ unsavedChanges = ( new Config ( ) ) . showUncommittedChanges ( ) ? await this . getGitUnsavedChanges ( ) : null ;
71+ if ( unsavedChanges !== null ) {
72+ commits . unshift ( { hash : '*' , parentHashes : [ refData . head ] , author : '*' , email : '' , date : Math . round ( ( new Date ( ) ) . getTime ( ) / 1000 ) , message : 'Uncommitted Changes (' + unsavedChanges . changes + ')' } ) ;
7973 }
74+ break ;
8075 }
8176 }
8277 }
@@ -88,16 +83,16 @@ export class DataSource {
8883 commitLookup [ commits [ i ] . hash ] = i ;
8984 commitNodes . push ( { hash : commits [ i ] . hash , parentHashes : commits [ i ] . parentHashes , author : commits [ i ] . author , email : commits [ i ] . email , date : commits [ i ] . date , message : commits [ i ] . message , refs : [ ] , current : false } ) ;
9085 }
91- for ( i = 0 ; i < refs . length ; i ++ ) {
92- if ( typeof commitLookup [ refs [ i ] . hash ] === 'number' ) {
93- commitNodes [ commitLookup [ refs [ i ] . hash ] ] . refs . push ( refs [ i ] ) ;
86+ for ( i = 0 ; i < refData . refs . length ; i ++ ) {
87+ if ( typeof commitLookup [ refData . refs [ i ] . hash ] === 'number' ) {
88+ commitNodes [ commitLookup [ refData . refs [ i ] . hash ] ] . refs . push ( refData . refs [ i ] ) ;
9489 }
9590 }
9691
9792 if ( unsavedChanges !== null ) {
9893 commitNodes [ 0 ] . current = true ;
99- } else if ( currentBranchHash !== null && typeof commitLookup [ currentBranchHash ] === 'number' ) {
100- commitNodes [ commitLookup [ currentBranchHash ] ] . current = true ;
94+ } else if ( refData . head !== null && typeof commitLookup [ refData . head ] === 'number' ) {
95+ commitNodes [ commitLookup [ refData . head ] ] . current = true ;
10196 }
10297
10398 return { commits : commitNodes , moreCommitsAvailable : moreCommitsAvailable } ;
@@ -214,11 +209,11 @@ export class DataSource {
214209 }
215210
216211 private getRefs ( showRemoteBranches : boolean ) {
217- return new Promise < GitRef [ ] > ( ( resolve ) => {
218- this . execGit ( 'show-ref ' + ( showRemoteBranches ? '' : '--heads --tags' ) + ' -d' , ( err , stdout ) => {
212+ return new Promise < GitRefData > ( ( resolve ) => {
213+ this . execGit ( 'show-ref ' + ( showRemoteBranches ? '' : '--heads --tags' ) + ' -d --head' , ( err , stdout ) => {
214+ let refData : GitRefData = { head : null , refs : [ ] } ;
219215 if ( ! err ) {
220216 let lines = stdout . split ( eolRegex ) ;
221- let refs : GitRef [ ] = [ ] ;
222217 for ( let i = 0 ; i < lines . length - 1 ; i ++ ) {
223218 let line = lines [ i ] . split ( ' ' ) ;
224219 if ( line . length < 2 ) continue ;
@@ -227,17 +222,17 @@ export class DataSource {
227222 let ref = line . join ( ' ' ) ;
228223
229224 if ( ref . startsWith ( 'refs/heads/' ) ) {
230- refs . push ( { hash : hash , name : ref . substring ( 11 ) , type : 'head' } ) ;
225+ refData . refs . push ( { hash : hash , name : ref . substring ( 11 ) , type : 'head' } ) ;
231226 } else if ( ref . startsWith ( 'refs/tags/' ) ) {
232- refs . push ( { hash : hash , name : ( ref . endsWith ( '^{}' ) ? ref . substring ( 10 , ref . length - 3 ) : ref . substring ( 10 ) ) , type : 'tag' } ) ;
227+ refData . refs . push ( { hash : hash , name : ( ref . endsWith ( '^{}' ) ? ref . substring ( 10 , ref . length - 3 ) : ref . substring ( 10 ) ) , type : 'tag' } ) ;
233228 } else if ( ref . startsWith ( 'refs/remotes/' ) ) {
234- refs . push ( { hash : hash , name : ref . substring ( 13 ) , type : 'remote' } ) ;
229+ refData . refs . push ( { hash : hash , name : ref . substring ( 13 ) , type : 'remote' } ) ;
230+ } else if ( ref === 'HEAD' ) {
231+ refData . head = hash ;
235232 }
236233 }
237- resolve ( refs ) ;
238- } else {
239- resolve ( [ ] ) ;
240234 }
235+ resolve ( refData ) ;
241236 } ) ;
242237 } ) ;
243238 }
0 commit comments