11const utils = require ( './utils' ) ;
22const { spawn } = require ( 'child_process' ) ;
3+ const commandExists = require ( 'command-exists' ) ;
4+ const { stat} = require ( 'fs' ) ;
35
46let showAllLogs = false ;
57
@@ -38,9 +40,58 @@ let git = {
3840 abortMerge,
3941 testGit,
4042 searchForHash,
41- searchForCommitMessage
43+ searchForCommitMessage,
44+ getSettings
4245} ;
4346
47+ let gitExecutablePath = 'git' ;
48+
49+ function getSettings ( { req, res} ) {
50+ // 1. get git path from local storage.
51+ // 2a. if git path, check if the command exists.
52+ // 2b. if not git path, check if git exists in the PATH
53+ // 3. Either local storage git path OR git in PATH must exist.
54+
55+ let reqBody = req . body ;
56+ let gitFnName = reqBody && reqBody . gitExecutablePath ? reqBody . gitExecutablePath + '/git.exe' : 'git' ;
57+
58+ gitFnName = gitFnName . replace ( / \\ / g, '/' ) ; // replace back slashes with forward slashes
59+
60+
61+ if ( gitFnName === 'git' ) {
62+ commandExists ( gitFnName , callback ) ;
63+ }
64+ else {
65+ stat ( gitFnName , callback )
66+ }
67+
68+ return ;
69+
70+ function callback ( err , exists ) {
71+ if ( ! exists ) {
72+ sendResponse ( res , {
73+ errorCode : 1 ,
74+ msg : 'I could not find GIT' ,
75+ description : 'You have two options here.. Add your git installation directory OR Add git to your PATH.'
76+ } ) ;
77+ return ;
78+ }
79+
80+ setGitFn ( gitFnName ) ;
81+ sendResponse ( res , {
82+ msg : 'Everything is fine'
83+ } ) ;
84+ } ;
85+ }
86+
87+ function setGitFn ( gitFnName ) {
88+ gitExecutablePath = gitFnName ;
89+ }
90+
91+ function getGitFn ( ) {
92+ return gitExecutablePath ;
93+ }
94+
4495function searchForCommitMessage ( { req, res, repo} ) {
4596 return logRepo ( { req, res, repo, options : {
4697 searchFor : 'commitmessage' ,
@@ -305,7 +356,6 @@ function getLocalProgressStatus(repo) {
305356 // no way yet to see stash in progress.
306357 // we'll detect these by checking UU on file status.
307358 return Promise . all ( [ rebaseHeadPromise , mergeHeadPromise , revertHeadPromise , interactiveRebaseHeadPromise ] ) . then ( ( heads ) => {
308- console . log ( 'heads = ' + heads ) ;
309359 if ( heads [ 0 ] || heads [ 1 ] || heads [ 2 ] ) {
310360 // there is something in progress!
311361 if ( heads [ 0 ] ) {
@@ -419,8 +469,6 @@ function unstageFile({req, res, repo}) {
419469
420470 gitOptions . push ( fileName ) ;
421471
422- console . log ( gitOptions ) ;
423-
424472 const child = spawnGitProcess ( repo , gitOptions ) ;
425473 redirectIO ( child , req , res ) ;
426474}
@@ -440,8 +488,6 @@ function stageFile({req, res, repo}) {
440488
441489 gitOptions . push ( fileName ) ;
442490
443- console . log ( gitOptions ) ;
444-
445491 const child = spawnGitProcess ( repo , gitOptions ) ;
446492 redirectIO ( child , req , res ) ;
447493}
@@ -512,8 +558,10 @@ function getCommit(options) {
512558}
513559
514560function spawnGitProcess ( repo , processOptions ) {
515- // console.log('path = ' + utils.decodePath(repo));
516- return spawn ( 'git' , processOptions , {
561+ if ( showAllLogs ) {
562+ console . log ( 'git arguments' , processOptions ) ;
563+ }
564+ return spawn ( gitExecutablePath , processOptions , {
517565 cwd : _getCwd ( repo ) ,
518566 stdio : [ 0 , 'pipe' , 'pipe' ]
519567 } ) ;
@@ -548,7 +596,7 @@ function logRepo3({repo, req, res}) {
548596 let logFormat = `--format=format:%H%n%an%n%ae%n%aD%n%s%n%P` ;
549597 let logArgs = [ 'log' , '-n 100' , logFormat , '--branches' ] ;
550598
551- const child = spawn ( 'git' , logArgs , {
599+ const child = spawn ( gitExecutablePath , logArgs , {
552600 cwd : utils . getCheckoutsDir ( ) + '/' + repo ,
553601 stdio : [ 0 , 'pipe' , 'pipe' ]
554602 } ) ;
@@ -604,9 +652,7 @@ log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset)
604652 logArgs . push ( '--skip=' + ( ( page - 1 ) * commitsInOnePageCount ) ) ;
605653 }
606654
607- console . log ( logArgs ) ;
608-
609- const child = spawn ( 'git' , logArgs , {
655+ const child = spawn ( gitExecutablePath , logArgs , {
610656 cwd : _getCwd ( repo ) ,
611657 stdio : [ 0 , 'pipe' , 'pipe' ]
612658 } ) ;
@@ -625,7 +671,7 @@ function clone({req, res}) {
625671 cloneOptions . push ( cloneSubdirectoryName ) ;
626672 }
627673
628- const child = spawn ( 'git' , cloneOptions , {
674+ const child = spawn ( gitExecutablePath , cloneOptions , {
629675 cwd : destinationDir ,
630676 stdio : [ 0 , 'pipe' , 'pipe' ]
631677 } ) ;
0 commit comments