@@ -3,12 +3,13 @@ import { GitCli } from '.'
33import { Command , Flag } from './constants'
44import { getOptions } from './options'
55import { typeCheckCommands } from '..'
6- import { trimAndSplit } from '../../util/stdout'
6+ import { cleanUpBranchName , trimAndSplit } from '../../util/stdout'
77import { isDirectory } from '../../fileSystem'
88
99export const autoRegisteredCommands = {
1010 GIT_GET_BRANCHES : 'getBranches' ,
1111 GIT_GET_COMMIT_MESSAGES : 'getCommitMessages' ,
12+ GIT_GET_CURRENT_BRANCH : 'getCurrentBranch' ,
1213 GIT_GET_NUM_COMMITS : 'getNumCommits' ,
1314 GIT_GET_REMOTE_URL : 'getRemoteUrl' ,
1415 GIT_GET_REPOSITORY_ROOT : 'getGitRepositoryRoot' ,
@@ -52,9 +53,11 @@ export class GitReader extends GitCli {
5253 const options = getOptions (
5354 cwd ,
5455 Command . LOG ,
55- ` ${ sha } ^..HEAD` ,
56+ sha ,
5657 Flag . PRETTY_FORMAT_COMMIT_MESSAGE ,
57- Flag . SEPARATE_WITH_NULL
58+ Flag . SEPARATE_WITH_NULL ,
59+ Flag . NUMBER ,
60+ '1'
5861 )
5962 try {
6063 return await this . executeProcess ( options )
@@ -80,31 +83,39 @@ export class GitReader extends GitCli {
8083 return new Set ( [ ...files , ...dirs ] )
8184 }
8285
83- public async getNumCommits ( cwd : string ) {
84- const options = getOptions (
85- cwd ,
86- Command . REV_LIST ,
87- Flag . FULL_HISTORY ,
88- Flag . ALL
89- )
86+ public async getNumCommits ( cwd : string , branch : string ) {
87+ const options = getOptions ( cwd , Command . REV_LIST , Flag . COUNT , branch )
9088 try {
91- const revisions = await this . executeProcess ( options )
92- return trimAndSplit ( revisions ) . length
89+ const nbCommits = await this . executeProcess ( options )
90+ return Number . parseInt ( nbCommits )
9391 } catch {
94- return ''
92+ return 0
9593 }
9694 }
9795
9896 public async getBranches ( cwd : string ) : Promise < string [ ] > {
9997 const options = getOptions ( cwd , Command . BRANCH , Flag . NO_MERGE )
10098 try {
10199 const branches = await this . executeProcess ( options )
102- return trimAndSplit ( branches )
100+ return trimAndSplit ( branches ) . map ( cleanUpBranchName )
103101 } catch {
104102 return [ ]
105103 }
106104 }
107105
106+ public async getCurrentBranch ( cwd : string ) : Promise < string > {
107+ const options = getOptions ( cwd , Command . BRANCH )
108+ try {
109+ const branches = await this . executeProcess ( options )
110+ const currentBranch = trimAndSplit ( branches ) . find (
111+ branch => branch . indexOf ( '*' ) === 0
112+ )
113+ return ( currentBranch && cleanUpBranchName ( currentBranch ) ) || ''
114+ } catch {
115+ return ''
116+ }
117+ }
118+
108119 private async getUntrackedDirectories ( cwd : string ) : Promise < string [ ] > {
109120 const options = getOptions (
110121 cwd ,
0 commit comments