Skip to content

Commit b319c77

Browse files
authored
Merge pull request #12 from intersystems/fix-git-push-tim
Properly use ssh when possible
2 parents eef1b4b + d632b42 commit b319c77

File tree

2 files changed

+36
-32
lines changed

2 files changed

+36
-32
lines changed

cls/SourceControl/Git/Utils.cls

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ ClassMethod GitUserEmail() As %String
7979
Quit $Get(@..#Storage@("settings","user",$username,"gitUserEmail"))
8080
}
8181

82+
ClassMethod PrivateKeyFile() As %String
83+
{
84+
Quit $Get(@..#Storage@("settings","ssh","privateKeyFile"))
85+
}
86+
8287
ClassMethod NeedSettings() As %Boolean [ CodeMode = expression ]
8388
{
8489
(..DefaultTemp() = "") || (..GitBinPath() = "") || (..GitBinPath() = """")
@@ -110,6 +115,7 @@ ClassMethod UpdateSettings(ByRef settings) As %Status
110115
set @..#Storage@("settings","groupByFolder") = $case(settings("groupByFolder"), "":"", :1)
111116
set @..#Storage@("settings","user",$username,"gitUserName") = settings("gitUserName")
112117
set @..#Storage@("settings","user",$username,"gitUserEmail") = settings("gitUserEmail")
118+
set @..#Storage@("settings","ssh","privateKeyFile") = settings("privateKeyFile")
113119

114120
quit $$$OK
115121
}
@@ -282,10 +288,11 @@ ClassMethod Commit(InternalName As %String, Message As %String = "example commit
282288
quit $$$OK
283289
}
284290

285-
ClassMethod Push() As %Status
291+
ClassMethod Push(remote As %String = "origin") As %Status
286292
{
287-
do ..RunGitCommand("push", .errStream, .outStream)
288-
w "git push"
293+
do ##class(SourceControl.Git.Utils).RunGitCommandWithInput("branch",,.errStream,.outstream,"--show-current")
294+
set branchName = outstream.ReadLine(outstream.Size)
295+
do ..RunGitWithArgs(.errStream, .outStream, "push", remote, branchName)
289296
do errStream.OutputToDevice()
290297
do outStream.OutputToDevice()
291298
quit $$$OK
@@ -303,6 +310,8 @@ ClassMethod Fetch(ByRef diffFiles) As %Status
303310
while (outStream.AtEnd = 0) {
304311
set diffFiles = diffFiles_$lb(outStream.ReadLine())
305312
}
313+
do outStream.Rewind()
314+
do outStream.OutputToDevice()
306315
quit $$$OK
307316
}
308317

@@ -311,33 +320,6 @@ ClassMethod Pull() As %Status
311320
quit $$$OK
312321
}
313322

314-
ClassMethod ConfigureGitAuthentication(Username As %String, Password As %String)
315-
{
316-
//git config credential.helper store
317-
//write !,"git config --system credential.helper manager"
318-
//git config credential.helper '!f() { printf "%s\n" "username=$USER" "password=$PASS"; };f'
319-
do ..RunGitCommand("config", .errStream, .outStream, "--global", "credential.helper", "store")
320-
do errStream.OutputToDevice()
321-
do outStream.OutputToDevice()
322-
/*
323-
write !,"git config --global credential.modalPrompt false"
324-
do ..RunGitCommand("config", .errStream, .outStream, "--global","credential.modalPrompt", "false")
325-
do errStream.OutputToDevice()
326-
do outStream.OutputToDevice()
327-
*/
328-
/*
329-
set file = ##class(%Stream.FileCharacter).%New()
330-
do file.WriteLine(Username)
331-
do file.WriteLine(Password)
332-
$$$ThrowOnError(file.%Save())
333-
set file.RemoveOnClose = 1
334-
*/
335-
write !,"git fetch"
336-
do ..RunGitCommand("fetch", .errStream, .outStream)
337-
do errStream.OutputToDevice()
338-
do outStream.OutputToDevice()
339-
}
340-
341323
ClassMethod IsNamespaceInGit() As %Boolean [ CodeMode = expression ]
342324
{
343325
##class(%File).Exists(..TempFolder()_".git")
@@ -1093,6 +1075,17 @@ ClassMethod RunGitCommandWithInput(command As %String, inFile As %String = "", O
10931075
set newArgs($i(newArgs)) = "-C"
10941076
set newArgs($i(newArgs)) = ..TempFolder()
10951077

1078+
set privateKeyFile = ..PrivateKeyFile()
1079+
if (privateKeyFile '= "") {
1080+
if $$$isWINDOWS {
1081+
// Escape slashes
1082+
set privateKeyFile = $Replace(privateKeyFile,"\","\\")
1083+
}
1084+
set newArgs($i(newArgs)) = "-c"
1085+
// StrictHostKeyChecking=accept-new for good behavior on first connection
1086+
set newArgs($i(newArgs)) = "core.sshCommand=ssh -F /dev/null -o StrictHostKeyChecking=accept-new -i "_privateKeyFile
1087+
}
1088+
10961089
set username = ..GitUserName()
10971090
set email = ..GitUserEmail()
10981091

@@ -1110,10 +1103,12 @@ ClassMethod RunGitCommandWithInput(command As %String, inFile As %String = "", O
11101103
}
11111104

11121105
}
1113-
1106+
11141107
set outLog = ##class(%Library.File).TempFilename()
11151108
set errLog = ##class(%Library.File).TempFilename()
1109+
11161110
set returnCode = $zf(-100,"/STDOUT="_$$$QUOTE(outLog)_" /STDERR="_$$$QUOTE(errLog)_$Case(inFile, "":"", :" /STDIN="_inFile),"git",newArgs...)
1111+
11171112
set errStream = ##class(%Stream.FileCharacter).%OpenId(errLog,,.sc)
11181113
set outStream = ##class(%Stream.FileCharacter).%OpenId(outLog,,.sc)
11191114
for stream=errStream,outStream {
@@ -1306,3 +1301,4 @@ ClassMethod NameToInternalName(Name, IgnorePercent = 1, IgnoreNonexistent = 1) A
13061301
}
13071302

13081303
}
1304+

csp/gitprojectsettings.csp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
set $namespace = namespace
2121
if $Data(%request.Data("gitsettings",1)) {
2222
kill settings
23-
for param="gitBinPath","defaultTemp","namespaceTemp","groupByFolder","gitUserName","gitUserEmail" {
23+
for param="gitBinPath","defaultTemp","namespaceTemp","groupByFolder","privateKeyFile","gitUserName","gitUserEmail" {
2424
set settings(param) = $Get(%request.Data(param,1))
2525
}
2626
do ##class(SourceControl.Git.Utils).UpdateSettings(.settings)
@@ -29,6 +29,7 @@
2929
set defaultTemp = ##class(SourceControl.Git.Utils).DefaultTemp()
3030
set namespaceTemp = ##class(SourceControl.Git.Utils).TempFolder()
3131
set groupByFolder = ##class(SourceControl.Git.Utils).GroupByFolder()
32+
set privateKeyFile = ##class(SourceControl.Git.Utils).PrivateKeyFile()
3233
set gitUserName = ##class(SourceControl.Git.Utils).GitUserName()
3334
set gitUserEmail = ##class(SourceControl.Git.Utils).GitUserEmail()
3435
set:defaultTemp="" defaultTemp = "c:\temp\"
@@ -65,6 +66,13 @@
6566
</div>
6667
</div>
6768

69+
<div class="form-group row">
70+
<label for="privateKeyFile" class="col-sm-4 col-form-label">SSH Private Key File</label>
71+
<div class="col-sm-8">
72+
<input type="text" class="form-control" id="privateKeyFile" name="privateKeyFile" value='#(..EscapeHTML(privateKeyFile))#'/>
73+
</div>
74+
</div>
75+
6876
<!--
6977
<div class="form-group row">
7078
<label for="groupByFolder" class="col-sm-4 col-form-label">Group all items by folders</label>

0 commit comments

Comments
 (0)