Skip to content

Commit faa27ec

Browse files
authored
Bootstrap using Git CLI (#148)
1 parent 7d82b3c commit faa27ec

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

modules/core/src/main/scala/com/indoorvivants/vcpkg/VcpkgBootstrap.scala

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import java.nio.file.Files
77
import java.util.stream.Collectors
88
import com.indoorvivants.detective.Platform
99
import Platform.OS._
10+
import scala.util.control.NonFatal
1011

1112
object VcpkgBootstrap {
1213
private val REMOTE_URI = "https://github.com/microsoft/vcpkg"
@@ -23,17 +24,32 @@ object VcpkgBootstrap {
2324
case _ => "vcpkg"
2425
}
2526

26-
def clone(directory: File) = {
27-
import org.eclipse.jgit.api.Git
27+
private def cloneWithGit(directory: File, log: ExternalLogger) = {
28+
val cmd = Seq(
29+
"git",
30+
"clone",
31+
"--single-branch",
32+
"--recurse-submodules",
33+
REMOTE_URI,
34+
directory.toString()
35+
)
36+
37+
sys.process.Process(cmd).! == 0
38+
}
2839

29-
val repo = Git
30-
.cloneRepository()
31-
.setURI(REMOTE_URI)
32-
.setDirectory(directory)
33-
.setBranchesToClone(Arrays.asList("refs/heads/master"))
34-
.setCloneAllBranches(false)
35-
.setCloneSubmodules(true)
36-
.call()
40+
def clone(directory: File, log: ExternalLogger) = {
41+
if (!gitAvailable(log) || !cloneWithGit(directory, log)) {
42+
import org.eclipse.jgit.api.Git
43+
44+
val repo = Git
45+
.cloneRepository()
46+
.setURI(REMOTE_URI)
47+
.setDirectory(directory)
48+
.setBranchesToClone(Arrays.asList("refs/heads/master"))
49+
.setCloneAllBranches(false)
50+
.setCloneSubmodules(true)
51+
.call()
52+
}
3753

3854
}
3955

@@ -73,4 +89,22 @@ object VcpkgBootstrap {
7389

7490
new Vcpkg(config, logger)
7591
}
92+
93+
private def gitAvailable(log: ExternalLogger): Boolean = {
94+
val cmd = Seq("git", "--version")
95+
96+
try {
97+
val contents = sys.process.Process(cmd).!!
98+
contents.startsWith("git version")
99+
} catch {
100+
case NonFatal(_) =>
101+
log.warn(
102+
"`git` command is not available, falling back to JGit which is slower"
103+
)
104+
105+
false
106+
107+
}
108+
109+
}
76110
}

modules/core/src/main/scala/com/indoorvivants/vcpkg/VcpkgPluginImpl.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ trait VcpkgPluginImpl {
197197
binary
198198
} else {
199199
logger.info(s"Cloning microsoft/vcpkg into $destination")
200-
VcpkgBootstrap.clone(destination)
200+
VcpkgBootstrap.clone(destination, logger)
201201
VcpkgBootstrap.launchBootstrap(destination, logger)
202202

203203
binary

0 commit comments

Comments
 (0)