Skip to content

Commit 85ab727

Browse files
committed
fix(vcs): Properly configure URL replacements for submodules
The configuration must be applied when initializing submodules, not afterward. Resolves #8918. Signed-off-by: Sebastian Schuberth <sebastian@doubleopen.org>
1 parent 90031fc commit 85ab727

File tree

2 files changed

+77
-11
lines changed

2 files changed

+77
-11
lines changed

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ import io.kotest.core.annotation.Tags
2525
import io.kotest.core.spec.style.WordSpec
2626
import io.kotest.engine.spec.tempdir
2727
import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder
28+
import io.kotest.matchers.concurrent.shouldCompleteWithin
2829
import io.kotest.matchers.shouldBe
2930

3031
import java.io.File
32+
import java.util.concurrent.TimeUnit
3133

3234
import org.ossreviewtoolkit.downloader.DownloadException
3335
import org.ossreviewtoolkit.model.Identifier
@@ -149,5 +151,73 @@ class GitFunTest : WordSpec({
149151
"specs/dep_graph_spec.js"
150152
)
151153
}
154+
155+
"apply URL replacements recursively" {
156+
val url = "https://github.com/oss-review-toolkit/ort-test-data-git-submodules.git"
157+
val pkg = Package.EMPTY.copy(vcsProcessed = VcsInfo(VcsType.GIT, url, "git-protocol-submodule-urls"))
158+
159+
// Using GitHub with the deprecated "git://" protocol actually hangs instead of failing explicitly.
160+
val workingTree = shouldCompleteWithin(40, TimeUnit.SECONDS) {
161+
git.download(pkg, outputDir, allowMovingRevisions = true)
162+
}
163+
164+
workingTree.isValid() shouldBe true
165+
workingTree.getRootPath().walk().onEnter {
166+
it == outputDir || it.parentFile.resolve(".gitmodules").isFile
167+
}.mapNotNullTo(mutableListOf()) { file ->
168+
file.toRelativeString(outputDir).takeIf { it.isNotEmpty() && !it.startsWith('.') }
169+
}.shouldContainExactlyInAnyOrder(
170+
"commons-text",
171+
"test-data-npm",
172+
"LICENSE",
173+
"README.md",
174+
175+
"commons-text/.git",
176+
"commons-text/.gitignore",
177+
"commons-text/.travis.yml",
178+
"commons-text/CONTRIBUTING.md",
179+
"commons-text/LICENSE.txt",
180+
"commons-text/NOTICE.txt",
181+
"commons-text/README.md",
182+
"commons-text/RELEASE-NOTES.txt",
183+
"commons-text/checkstyle-suppressions.xml",
184+
"commons-text/checkstyle.xml",
185+
"commons-text/license-header.txt",
186+
"commons-text/pom.xml",
187+
"commons-text/sb-excludes.xml",
188+
189+
"test-data-npm/isarray",
190+
"test-data-npm/long.js",
191+
"test-data-npm/.git",
192+
"test-data-npm/.gitignore",
193+
"test-data-npm/.gitmodules",
194+
"test-data-npm/LICENSE",
195+
"test-data-npm/README.md",
196+
"test-data-npm/package-lock.json",
197+
"test-data-npm/package.json",
198+
199+
"test-data-npm/isarray/.git",
200+
"test-data-npm/isarray/.gitignore",
201+
"test-data-npm/isarray/.travis.yml",
202+
"test-data-npm/isarray/LICENSE",
203+
"test-data-npm/isarray/Makefile",
204+
"test-data-npm/isarray/README.md",
205+
"test-data-npm/isarray/component.json",
206+
"test-data-npm/isarray/index.js",
207+
"test-data-npm/isarray/package-lock.json",
208+
"test-data-npm/isarray/package.json",
209+
"test-data-npm/isarray/test.js",
210+
211+
"test-data-npm/long.js/.git",
212+
"test-data-npm/long.js/.gitignore",
213+
"test-data-npm/long.js/.travis.yml",
214+
"test-data-npm/long.js/LICENSE",
215+
"test-data-npm/long.js/README.md",
216+
"test-data-npm/long.js/bower.json",
217+
"test-data-npm/long.js/index.js",
218+
"test-data-npm/long.js/package.json",
219+
"test-data-npm/long.js/webpack.config.js"
220+
)
221+
}
152222
}
153223
})

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -315,26 +315,22 @@ class Git(
315315
private fun updateSubmodules(workingTree: GitWorkingTree) {
316316
if (!workingTree.getRootPath().resolve(".gitmodules").isFile) return
317317

318-
val insteadOf = REPOSITORY_URL_PREFIX_REPLACEMENTS.map { (prefix, replacement) ->
319-
"url.$replacement.insteadOf $prefix"
318+
val configArgs = REPOSITORY_URL_PREFIX_REPLACEMENTS.flatMap { (prefix, replacement) ->
319+
listOf("-c", "url.$replacement.insteadOf=$prefix")
320320
}
321321

322322
val recursive = "--recursive".takeIf { config.updateNestedSubmodules }
323323
val updateArgs = listOfNotNull("submodule", "update", "--init", recursive)
324324

325325
runCatching {
326326
// TODO: Migrate this to JGit once https://bugs.eclipse.org/bugs/show_bug.cgi?id=580731 is implemented.
327-
workingTree.runGit(*updateArgs.toTypedArray(), "--depth", "${config.historyDepth}")
328-
329-
insteadOf.forEach {
330-
val foreachArgs = listOfNotNull(
331-
"submodule", "foreach", recursive, "git config $it"
332-
)
333-
workingTree.runGit(*foreachArgs.toTypedArray())
334-
}
327+
workingTree.runGit(
328+
*configArgs.toTypedArray(), *updateArgs.toTypedArray(),
329+
"--depth", "${config.historyDepth}"
330+
)
335331
}.recover {
336332
// As Git's dumb HTTP transport does not support shallow capabilities, also try to not limit the depth.
337-
workingTree.runGit(*updateArgs.toTypedArray())
333+
workingTree.runGit(*configArgs.toTypedArray(), *updateArgs.toTypedArray())
338334
}
339335
}
340336

0 commit comments

Comments
 (0)