@@ -8,23 +8,31 @@ const gitLogFormat = ['%H', '%P', '%an', '%ae', '%at', '%s'].join(gitLogSeparato
88const gitCommitDetailsFormat = [ '%H' , '%P' , '%an' , '%ae' , '%at' , '%cn' , '%B' ] . join ( gitLogSeparator ) ;
99
1010export class DataSource {
11- private workspaceDir : string ;
11+ private execOptions : cp . ExecOptions ;
12+ private gitPath ! : string ;
13+ private gitExecPath ! : string ;
1214
1315 constructor ( workspaceDir : string ) {
14- this . workspaceDir = workspaceDir ;
16+ this . execOptions = { cwd : workspaceDir } ;
17+ this . registerGitPath ( ) ;
18+ }
19+
20+ public registerGitPath ( ) {
21+ this . gitPath = ( new Config ( ) ) . gitPath ( ) ;
22+ this . gitExecPath = this . gitPath . indexOf ( ' ' ) > - 1 ? '"' + this . gitPath + '"' : this . gitPath ;
1523 }
1624
1725 public isGitRepository ( ) {
1826 return new Promise < boolean > ( ( resolve ) => {
19- cp . exec ( 'git rev-parse --git-dir', { cwd : this . workspaceDir } , ( err ) => {
27+ cp . exec ( this . gitExecPath + ' rev-parse --git-dir', this . execOptions , ( err ) => {
2028 resolve ( ! err ) ;
2129 } ) ;
2230 } ) ;
2331 }
2432
2533 public getBranches ( showRemoteBranches : boolean ) {
2634 return new Promise < string [ ] > ( ( resolve ) => {
27- cp . exec ( 'git branch' + ( showRemoteBranches ? ' -a' : '' ) , { cwd : this . workspaceDir } , ( err , stdout ) => {
35+ cp . exec ( this . gitExecPath + ' branch' + ( showRemoteBranches ? ' -a' : '' ) , this . execOptions , ( err , stdout ) => {
2836 if ( ! err ) {
2937 let lines = stdout . split ( eolRegex ) ;
3038 let branches : string [ ] = [ ] ;
@@ -98,7 +106,7 @@ export class DataSource {
98106 public async commitDetails ( commitHash : string ) {
99107 try {
100108 let details = await new Promise < GitCommitDetails > ( ( resolve , reject ) => {
101- cp . exec ( 'git show --quiet ' + commitHash + ' --format="' + gitCommitDetailsFormat + '"' , { cwd : this . workspaceDir } , ( err , stdout ) => {
109+ cp . exec ( this . gitExecPath + ' show --quiet ' + commitHash + ' --format="' + gitCommitDetailsFormat + '"' , this . execOptions , ( err , stdout ) => {
102110 if ( ! err ) {
103111 let lines = stdout . split ( eolRegex ) ;
104112 let commitInfo = lines [ 0 ] . split ( gitLogSeparator ) ;
@@ -119,7 +127,7 @@ export class DataSource {
119127 } ) ;
120128 let fileLookup : { [ file : string ] : number } = { } ;
121129 await new Promise ( ( resolve , reject ) => {
122- cp . exec ( 'git diff-tree --name-status -r -m --root --find-renames --diff-filter=AMDR ' + commitHash , { cwd : this . workspaceDir } , ( err , stdout ) => {
130+ cp . exec ( this . gitExecPath + ' diff-tree --name-status -r -m --root --find-renames --diff-filter=AMDR ' + commitHash , this . execOptions , ( err , stdout ) => {
123131 if ( ! err ) {
124132 let lines = stdout . split ( eolRegex ) ;
125133 for ( let i = 1 ; i < lines . length - 1 ; i ++ ) {
@@ -136,7 +144,7 @@ export class DataSource {
136144 } ) ;
137145 } ) ;
138146 await new Promise ( ( resolve , reject ) => {
139- cp . exec ( 'git diff-tree --numstat -r -m --root --find-renames --diff-filter=AMDR ' + commitHash , { cwd : this . workspaceDir } , ( err , stdout ) => {
147+ cp . exec ( this . gitExecPath + ' diff-tree --numstat -r -m --root --find-renames --diff-filter=AMDR ' + commitHash , this . execOptions , ( err , stdout ) => {
140148 if ( ! err ) {
141149 let lines = stdout . split ( eolRegex ) ;
142150 for ( let i = 1 ; i < lines . length - 1 ; i ++ ) {
@@ -164,7 +172,7 @@ export class DataSource {
164172 public async getCommitFile ( commitHash : string , filePath : string ) {
165173 return new Promise < string > ( ( resolve ) => {
166174 let args = [ 'show' , commitHash + ':' + filePath ] , stdout = '' , err = false ;
167- const cmd = cp . spawn ( 'git' , args , { cwd : this . workspaceDir } ) ;
175+ const cmd = cp . spawn ( this . gitPath , args , this . execOptions ) ;
168176 cmd . stdout . on ( 'data' , ( d ) => { stdout += d ; } ) ;
169177 cmd . on ( 'error' , ( ) => {
170178 resolve ( '' ) ;
@@ -178,48 +186,48 @@ export class DataSource {
178186 }
179187
180188 public addTag ( tagName : string , commitHash : string ) {
181- return this . runGitCommand ( 'git tag -a ' + escapeRefName ( tagName ) + ' -m "" ' + commitHash ) ;
189+ return this . runGitCommand ( 'tag -a ' + escapeRefName ( tagName ) + ' -m "" ' + commitHash ) ;
182190 }
183191
184192 public deleteTag ( tagName : string ) {
185- return this . runGitCommand ( 'git tag -d ' + escapeRefName ( tagName ) ) ;
193+ return this . runGitCommand ( 'tag -d ' + escapeRefName ( tagName ) ) ;
186194 }
187195
188196 public createBranch ( branchName : string , commitHash : string ) {
189- return this . runGitCommand ( 'git branch ' + escapeRefName ( branchName ) + ' ' + commitHash ) ;
197+ return this . runGitCommand ( 'branch ' + escapeRefName ( branchName ) + ' ' + commitHash ) ;
190198 }
191199
192200 public checkoutBranch ( branchName : string , remoteBranch : string | null ) {
193- return this . runGitCommand ( 'git checkout ' + ( remoteBranch === null ? escapeRefName ( branchName ) : ' -b ' + escapeRefName ( branchName ) + ' ' + escapeRefName ( remoteBranch ) ) ) ;
201+ return this . runGitCommand ( 'checkout ' + ( remoteBranch === null ? escapeRefName ( branchName ) : ' -b ' + escapeRefName ( branchName ) + ' ' + escapeRefName ( remoteBranch ) ) ) ;
194202 }
195203
196204 public deleteBranch ( branchName : string , forceDelete : boolean ) {
197- return this . runGitCommand ( 'git branch --delete' + ( forceDelete ? ' --force' : '' ) + ' ' + escapeRefName ( branchName ) ) ;
205+ return this . runGitCommand ( 'branch --delete' + ( forceDelete ? ' --force' : '' ) + ' ' + escapeRefName ( branchName ) ) ;
198206 }
199207
200208 public renameBranch ( oldName : string , newName : string ) {
201- return this . runGitCommand ( 'git branch -m ' + escapeRefName ( oldName ) + ' ' + escapeRefName ( newName ) ) ;
209+ return this . runGitCommand ( 'branch -m ' + escapeRefName ( oldName ) + ' ' + escapeRefName ( newName ) ) ;
202210 }
203211
204- public mergeBranch ( branchName : string ) {
205- return this . runGitCommand ( 'git merge ' + escapeRefName ( branchName ) ) ;
212+ public mergeBranch ( branchName : string ) {
213+ return this . runGitCommand ( 'merge ' + escapeRefName ( branchName ) ) ;
206214 }
207215
208216 public cherrypickCommit ( commitHash : string , parentIndex : number ) {
209- return this . runGitCommand ( 'git cherry-pick ' + commitHash + ( parentIndex > 0 ? ' -m ' + parentIndex : '' ) ) ;
217+ return this . runGitCommand ( 'cherry-pick ' + commitHash + ( parentIndex > 0 ? ' -m ' + parentIndex : '' ) ) ;
210218 }
211219
212220 public revertCommit ( commitHash : string , parentIndex : number ) {
213- return this . runGitCommand ( 'git revert --no-edit ' + commitHash + ( parentIndex > 0 ? ' -m ' + parentIndex : '' ) ) ;
221+ return this . runGitCommand ( 'revert --no-edit ' + commitHash + ( parentIndex > 0 ? ' -m ' + parentIndex : '' ) ) ;
214222 }
215223
216224 public resetToCommit ( commitHash : string , resetMode : GitResetMode ) {
217- return this . runGitCommand ( 'git reset --' + resetMode + ' ' + commitHash ) ;
225+ return this . runGitCommand ( 'reset --' + resetMode + ' ' + commitHash ) ;
218226 }
219227
220228 private async runGitCommand ( command : string ) {
221229 return new Promise < GitCommandStatus > ( ( resolve ) => {
222- cp . exec ( command , { cwd : this . workspaceDir } , ( err ) => {
230+ cp . exec ( this . gitExecPath + ' ' + command , this . execOptions , ( err ) => {
223231 if ( ! err ) {
224232 resolve ( null ) ;
225233 } else {
@@ -232,7 +240,7 @@ export class DataSource {
232240
233241 private async getRefs ( showRemoteBranches : boolean ) {
234242 return new Promise < GitRef [ ] > ( ( resolve ) => {
235- cp . exec ( 'git show-ref ' + ( showRemoteBranches ? '' : '--heads --tags' ) + ' -d' , { cwd : this . workspaceDir } , ( err , stdout ) => {
243+ cp . exec ( this . gitExecPath + ' show-ref ' + ( showRemoteBranches ? '' : '--heads --tags' ) + ' -d' , this . execOptions , ( err , stdout ) => {
236244 if ( ! err ) {
237245 let lines = stdout . split ( eolRegex ) ;
238246 let refs : GitRef [ ] = [ ] ;
@@ -269,7 +277,7 @@ export class DataSource {
269277 if ( showRemoteBranches ) args . push ( '--remotes' ) ;
270278 }
271279
272- const cmd = cp . spawn ( 'git' , args , { cwd : this . workspaceDir } ) ;
280+ const cmd = cp . spawn ( this . gitPath , args , this . execOptions ) ;
273281 cmd . stdout . on ( 'data' , ( d ) => { stdout += d ; } ) ;
274282 cmd . on ( 'error' , ( ) => {
275283 resolve ( [ ] ) ;
@@ -296,7 +304,7 @@ export class DataSource {
296304 private getGitUnsavedChanges ( ) {
297305 try {
298306 return new Promise < GitUnsavedChangesCmdResp > ( ( resolve , reject ) => {
299- cp . exec ( 'git status -s --branch --untracked-files --porcelain', { cwd : this . workspaceDir } , ( err , stdout ) => {
307+ cp . exec ( this . gitExecPath + ' status -s --branch --untracked-files --porcelain', this . execOptions , ( err , stdout ) => {
300308 if ( ! err ) {
301309 let lines = stdout . split ( eolRegex ) ;
302310 resolve ( lines . length > 2 ? { branch : lines [ 0 ] . substring ( 3 ) . split ( '...' ) [ 0 ] , changes : lines . length - 2 } : null ) ;
0 commit comments