Skip to content

Commit 6ea6554

Browse files
committed
Fix SshAgentSessionFactory setting
1 parent ba447d2 commit 6ea6554

File tree

5 files changed

+38
-21
lines changed

5 files changed

+38
-21
lines changed

build.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ lazy val gitsupport = (project in file("cli-git"))
142142
jsch,
143143
jschSshAgent,
144144
jschConnectorFactory,
145+
jgitJsch,
145146
commonsIo,
146147
scalatest % Test,
147148
scalamock % Test

cli-git/src/main/scala/ConsoleCredentialsProvider.scala

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,29 @@ object ConsoleCredentialsProvider extends CredentialsProvider {
2626
def supports(items: CredentialItem*) = true
2727

2828
def get(uri: URIish, items: CredentialItem*): Boolean = {
29-
items foreach {
30-
case i: CredentialItem.Username =>
31-
val username = System.console.readLine("%s: ", i.getPromptText)
32-
i.setValue(username)
33-
34-
case i: CredentialItem.Password =>
35-
val password = System.console.readPassword("%s: ", i.getPromptText)
36-
i.setValueNoCopy(password)
37-
38-
case i: CredentialItem.InformationalMessage =>
39-
System.console.printf("%s\n", i.getPromptText)
40-
41-
case i: CredentialItem.YesNoType =>
42-
i.setValue(askYesNo(i.getPromptText))
43-
44-
case i: CredentialItem.StringType if uri.getScheme == "ssh" =>
45-
val password = String.valueOf(System.console.readPassword("%s: ", i.getPromptText))
46-
i.setValue(password)
29+
if (System.console == null) false
30+
else {
31+
items foreach {
32+
case i: CredentialItem.Username =>
33+
val username = System.console.readLine("%s: ", i.getPromptText)
34+
i.setValue(username)
35+
36+
case i: CredentialItem.Password =>
37+
val password = System.console.readPassword("%s: ", i.getPromptText)
38+
i.setValueNoCopy(password)
39+
40+
case i: CredentialItem.InformationalMessage =>
41+
System.console.printf("%s\n", i.getPromptText)
42+
43+
case i: CredentialItem.YesNoType =>
44+
i.setValue(askYesNo(i.getPromptText))
45+
46+
case i: CredentialItem.StringType if uri.getScheme == "ssh" =>
47+
val password = String.valueOf(System.console.readPassword("%s: ", i.getPromptText))
48+
i.setValue(password)
49+
}
50+
true
4751
}
48-
true
4952
}
5053

5154
@scala.annotation.tailrec

cli-git/src/main/scala/GitInteractor.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import java.io.File
2121

2222
import giter8.GitInteractor.TransportError
2323
import org.eclipse.jgit.api.errors.TransportException
24-
import org.eclipse.jgit.transport.{CredentialsProvider, SshSessionFactory}
24+
import org.eclipse.jgit.transport.{CredentialsProvider, SshSessionFactory, SshTransport}
2525
import org.eclipse.jgit.api.{Git => JGit}
2626

2727
import scala.util.{Failure, Success, Try}
@@ -42,14 +42,17 @@ object GitInteractor {
4242

4343
class JGitInteractor(knownHosts: Option[String]) extends GitInteractor {
4444
CredentialsProvider.setDefault(ConsoleCredentialsProvider)
45-
SshSessionFactory.setInstance(new SshAgentSessionFactory(knownHosts))
4645

4746
override def cloneRepository(url: String, dest: File): Try[Unit] = Try {
4847
JGit
4948
.cloneRepository()
5049
.setURI(url)
5150
.setDirectory(dest)
5251
.setCredentialsProvider(ConsoleCredentialsProvider)
52+
.setTransportConfigCallback({
53+
case sshTransport: SshTransport => sshTransport.setSshSessionFactory(new SshAgentSessionFactory(knownHosts))
54+
case x => x
55+
})
5356
.call()
5457
.close()
5558
}

launcher/src/test/scala/LauncherTest.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,13 @@ object LauncherTest extends BasicTestSuite {
2121
assert((dir / "hello" / "build.sbt").exists)
2222
}
2323
}
24+
25+
/*
26+
test("runs git@github.com:scala/scala-seed.g8.git") {
27+
IO.withTemporaryDirectory { dir =>
28+
launcher.run(Array("git@github.com:scala/scala-seed.g8.git", "--name=hello"), dir)
29+
assert((dir / "hello" / "build.sbt").exists)
30+
}
31+
}
32+
*/
2433
}

project/Dependencies.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ object Dependencies {
1414
ExclusionRule("com.sun.jdmk", "jmxtools"),
1515
ExclusionRule("com.sun.jmx", "jmxri")
1616
)
17+
val jgitJsch = "org.eclipse.jgit" % "org.eclipse.jgit.ssh.jsch" % "5.8.0.202006091008-r"
1718
val jsch = "com.jcraft" % "jsch.agentproxy.jsch" % "0.0.9"
1819
val jschSshAgent = "com.jcraft" % "jsch.agentproxy.sshagent" % "0.0.9"
1920
val jschConnectorFactory = "com.jcraft" % "jsch.agentproxy.connector-factory" % "0.0.9"

0 commit comments

Comments
 (0)