'From Pharo5.0 of 16 April 2015 [Latest update: #50380] on 13 October 2015 at 1:16:17.804323 am'!

!MCFileTreeGitRepository class methodsFor: 'accessing' stamp: 'HernanMoralesDurand 10/13/2015 01:13'!
sshAgentPid
	" Answer a <String> with the pid of a running ssh-agent process 
	ToDo: if exist '%PROGRAMFILES(X86)%' "

	^ self runProcessWrapperCommand: 'wmic process where ExecutablePath=' , '''c:\\Program Files (x86)\\Git\\bin\\ssh-agent.exe ''' , ' get ProcessId'
! !

!MCFileTreeGitRepository class methodsFor: 'accessing' stamp: 'HernanMoralesDurand 10/10/2015 17:05'!
processWrapperClass
	" Answer the ProcessWrapper class or signal an exception if not found "

	^ Smalltalk
		at: #ProcessWrapper
		ifAbsent: [ self error: 'Please load ProcessWrapper' ]! !

!MCFileTreeGitRepository class methodsFor: 'accessing' stamp: 'HernanMoralesDurand 10/13/2015 01:14'!
sshAgentAuthSock
	" Answer a <String> with the pid of a running ssh-agent process.
	$SSH_AUTH_SOCK contains the path of the unix file socket that the agent uses for communication with other processes. This is essential for ssh-add. "
	"  '/tmp/ssh-Jpgwx10860/agent.10860' -> '10860'  "
	" find.exe and head.exe (MSYS commands) location should be in PATH environment variable "

	^ self runProcessWrapperCommand: 'find /tmp/ssh-* -name agent.\* -uid $(id -u)| head -n 1'! !

!MCFileTreeGitRepository class methodsFor: 'accessing' stamp: 'HernanMoralesDurand 10/13/2015 01:15'!
buildProcessWrapperGitCommand: aCommandString in: aDirectory
	" Answer a <String> with the git command to be used "
	
	^ self buildSSHAgentEnvVars ,
		' && ' ,
		self gitCommand , ' -C "' , (MCFileTreeFileUtils current directoryPathString: aDirectory) , '" ' , 
		aCommandString! !

!MCFileTreeGitRepository class methodsFor: 'accessing' stamp: 'HernanMoralesDurand 10/10/2015 17:23'!
buildSSHAgentEnvVars
	" Answer a <String> with DOS environment commands for connecting a runnning ssh-agent "

	^ String streamContents: [ : stream | 
		stream
			nextPutAll: 'set SSH_AGENT_PID=';
			nextPutAll: self sshAgentPid;
			cr;
			nextPutAll: 	'SSH_AUTH_SOCK=';
			nextPutAll: self sshAgentAuthSock
			cr ]
! !

!MCFileTreeGitRepository class methodsFor: 'accessing' stamp: 'HernanMoralesDurand 10/10/2015 16:55'!
runProcessWrapperGitCommand: anArrayOfStrings in: aDirectory
	"Enclose all parameters with double quotes to protect."

	| r aCommandString |
	aCommandString := String
		streamContents:
			[ :stream | 
			anArrayOfStrings
				do:
					[ :e | 
					stream
						nextPut: $";
						nextPutAll: e;
						nextPutAll: '" ' ] ].
	r := self
		runProcessWrapperGitCommandString: aCommandString
		in: aDirectory.
	^ r! !

!MCFileTreeGitRepository class methodsFor: 'accessing' stamp: 'HernanMoralesDurand 10/10/2015 17:18'!
runProcessWrapperGitCommandString: aCommandString in: aDirectory

	| r command |

	command := self processWrapperClass new
				useStdout;
				useStderr;
				startWithCommand: (self buildProcessWrapperGitCommand: aCommandString in: aDirectory);
				yourself.
	command waitForExit.
	self assert: command isRunning not.
	r := command upToEnd.
	command exitCode > 0
		ifTrue: [ 
			self signalGitCommandError: command.
			r := '' ].
	^ r! !

!MCFileTreeGitRepository class methodsFor: 'accessing' stamp: 'HernanMoralesDurand 10/10/2015 17:00'!
signalGitCommandError: command
	| errorString |
	errorString := command errorUpToEnd.
	errorString notEmpty
		ifTrue:
			[ MCFileTreeGitError new signal: 'Git error: ' , errorString ]! !

!MCFileTreeGitRepository class methodsFor: 'accessing' stamp: 'HernanMoralesDurand 10/13/2015 01:15'!
runProcessWrapperCommand: aCommandString
	| r command |

	command := self processWrapperClass new
				useStdout;
				useStderr;
				startWithCommand: aCommandString;
				yourself.
	command waitForExit.
	self assert: command isRunning not.
	r := command upToEnd.
	command exitCode > 0
		ifTrue: [ 
			self error: command.
			r := '' ].
	^ r! !

MCFileTreeGitRepository class removeSelector: #buildGitCommand:in:!
