Skip to content

Commit b824841

Browse files
committed
feat(vcs): Allow to replace Git SSH / SCP URLs with HTTPS
Relates to #6698. Note that still #8556 needs to be implemented to make this configurable for ORT CLI users. Signed-off-by: Sebastian Schuberth <[email protected]>
1 parent 9219ba7 commit b824841

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

plugins/version-control-systems/git/src/funTest/kotlin/GitFunTest.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private const val REPO_PATH_FOR_VERSION = "specs"
5252

5353
@Tags("RequiresExternalTool")
5454
class GitFunTest : WordSpec({
55-
val git = GitFactory().create(PluginConfig.EMPTY)
55+
val git = GitFactory().create(PluginConfig(options = mapOf("replaceSshWithHttps" to "true")))
5656
lateinit var outputDir: File
5757

5858
beforeEach {
@@ -163,6 +163,16 @@ class GitFunTest : WordSpec({
163163
}
164164
}
165165

166+
"apply SSH URL replacements" {
167+
val pkg = Package.EMPTY.copy(
168+
vcsProcessed = VcsInfo(VcsType.GIT, REPO_URL.replace("https://", "ssh://git@"), REPO_REV)
169+
)
170+
171+
shouldNotThrow<DownloadException> {
172+
git.download(pkg, outputDir)
173+
}
174+
}
175+
166176
"apply URL replacements recursively" {
167177
val url = "https://github.com/oss-review-toolkit/ort-test-data-git-submodules.git"
168178
val pkg = Package.EMPTY.copy(vcsProcessed = VcsInfo(VcsType.GIT, url, "git-protocol-submodule-urls"))

plugins/version-control-systems/git/src/main/kotlin/Git.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,6 @@ import org.ossreviewtoolkit.utils.ort.showStackTrace
6464
import org.semver4j.range.RangeList
6565
import org.semver4j.range.RangeListFactory
6666

67-
// Replace prefixes of Git submodule repository URLs.
68-
private val REPOSITORY_URL_PREFIX_REPLACEMENTS = listOf(
69-
"git://" to "https://"
70-
)
71-
7267
object GitCommand : CommandLineTool {
7368
private val versionRegex = Regex("[Gg]it [Vv]ersion (?<version>[\\d.a-z-]+)(\\s.+)?")
7469

@@ -143,6 +138,16 @@ class Git(
143138
override val priority = 100
144139
override val latestRevisionNames = listOf("HEAD", "@")
145140

141+
private val repositoryUrlPrefixReplacements = buildList {
142+
// Replace prefixes of Git submodule repository URLs.
143+
add("git://" to "https://")
144+
145+
if (config.replaceSshWithHttps) {
146+
add("ssh://git@" to "https://")
147+
add("[email protected]:" to "https://github.com/")
148+
}
149+
}
150+
146151
override fun getVersion() = GitCommand.getVersion()
147152

148153
override fun getDefaultBranchName(url: String): String {
@@ -182,7 +187,7 @@ class Git(
182187
gitInfoDir.resolve("sparse-checkout").writeText(globPatterns.joinToString("\n"))
183188
}
184189

185-
REPOSITORY_URL_PREFIX_REPLACEMENTS.groupBy(
190+
repositoryUrlPrefixReplacements.groupBy(
186191
{ it.second }, { it.first }
187192
).forEach { (replacement, prefixes) ->
188193
git.repository.config.setStringList("url", replacement, "insteadOf", prefixes)
@@ -321,7 +326,7 @@ class Git(
321326
private fun updateSubmodules(workingTree: GitWorkingTree) {
322327
if (!workingTree.getRootPath().resolve(".gitmodules").isFile) return
323328

324-
val configArgs = REPOSITORY_URL_PREFIX_REPLACEMENTS.flatMap { (prefix, replacement) ->
329+
val configArgs = repositoryUrlPrefixReplacements.flatMap { (prefix, replacement) ->
325330
listOf("-c", "url.$replacement.insteadOf=$prefix")
326331
}
327332

plugins/version-control-systems/git/src/main/kotlin/GitConfig.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,12 @@ data class GitConfig(
3232

3333
/** Whether nested submodules should be updated, or if only top-level submodules should be considered. */
3434
@OrtPluginOption(defaultValue = "true")
35-
val updateNestedSubmodules: Boolean
35+
val updateNestedSubmodules: Boolean,
36+
37+
/**
38+
* Whether to force SSH / SCP URLs (which always require authentication) to use HTTPS instead (which might work
39+
* without authentication).
40+
*/
41+
@OrtPluginOption(defaultValue = "false")
42+
val replaceSshWithHttps: Boolean
3643
)

0 commit comments

Comments
 (0)