Skip to content

Commit fe1de71

Browse files
committed
Update DependencyManager.kt
1 parent 9ffa789 commit fe1de71

1 file changed

Lines changed: 30 additions & 18 deletions

File tree

src/main/kotlin/file/DependencyManager.kt

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ import org.eclipse.jgit.api.Git
88
import org.eclipse.jgit.api.ResetCommand
99
import org.eclipse.jgit.internal.storage.file.FileRepository
1010
import org.eclipse.jgit.lib.Constants
11-
import org.eclipse.jgit.lib.Ref
12-
import org.eclipse.jgit.transport.URIish
1311
import java.io.File
12+
import java.io.ByteArrayOutputStream
1413
import java.io.IOException
1514
import java.nio.file.Files
1615
import java.nio.file.Path
@@ -125,10 +124,8 @@ object DependencyManager {
125124
FileRepository(depFolder.resolve(".git").toFile()).use { repository ->
126125
try {
127126
Git(repository).use { git ->
128-
git.remoteSetUrl()
129-
.setName("origin")
130-
.setUri(URIish(depUri))
131-
.call()
127+
repository.config.setString("remote", "origin", "url", depUri)
128+
repository.config.save()
132129
git.fetch()
133130
.setRemote("origin")
134131
.setRemoveDeletedRefs(true)
@@ -193,21 +190,20 @@ object DependencyManager {
193190

194191
private fun getDefaultBranch(depUri: String): String? {
195192
return try {
193+
val fromGit = getDefaultBranchFromGit(depUri)
194+
if (!fromGit.isNullOrBlank()) {
195+
return fromGit
196+
}
196197
val refs = Git.lsRemoteRepository()
197198
.setRemote(depUri)
198199
.setHeads(true)
199200
.setTags(false)
200-
.setSymrefs(true)
201201
.call()
202202

203-
val symbolicHead = refs.firstOrNull { it.name == Constants.HEAD && it.isSymbolic }
204-
?.target
205-
?.name
206-
if (symbolicHead != null && symbolicHead.startsWith(Constants.R_HEADS)) {
207-
return symbolicHead.removePrefix(Constants.R_HEADS)
208-
}
209-
210-
val branchNames = refs.mapNotNull { refToBranchName(it) }.toSet()
203+
val branchNames = refs.mapNotNull { ref ->
204+
val name = ref.name
205+
if (name.startsWith(Constants.R_HEADS)) name.removePrefix(Constants.R_HEADS) else null
206+
}.toSet()
211207
when {
212208
branchNames.contains("main") -> "main"
213209
branchNames.contains("master") -> "master"
@@ -220,9 +216,25 @@ object DependencyManager {
220216
}
221217
}
222218

223-
private fun refToBranchName(ref: Ref): String? {
224-
val name = ref.name
225-
return if (name.startsWith(Constants.R_HEADS)) name.removePrefix(Constants.R_HEADS) else null
219+
private fun getDefaultBranchFromGit(depUri: String): String? {
220+
return try {
221+
val out = ByteArrayOutputStream()
222+
val err = ByteArrayOutputStream()
223+
val p = ProcessBuilder("git", "ls-remote", "--symref", depUri, "HEAD")
224+
.redirectErrorStream(false)
225+
.start()
226+
p.inputStream.copyTo(out)
227+
p.errorStream.copyTo(err)
228+
if (p.waitFor() != 0) {
229+
return null
230+
}
231+
val line = out.toString(Charsets.UTF_8.name())
232+
.lineSequence()
233+
.firstOrNull { it.startsWith("ref: refs/heads/") && it.endsWith("\tHEAD") }
234+
line?.substringAfter("ref: refs/heads/")?.substringBefore("\tHEAD")
235+
} catch (_: Exception) {
236+
null
237+
}
226238
}
227239

228240
private fun isGitRepoUpToDate(depFolder: Path): Boolean {

0 commit comments

Comments
 (0)