From c51939831ef44d2f9eae57662f04121755f0ef69 Mon Sep 17 00:00:00 2001 From: Martin Nonnenmacher Date: Thu, 21 Sep 2023 15:33:06 +0200 Subject: [PATCH] fix(downloader): Fix updating the Git working tree for a branch The `Git.updateWorkingTree` function called only `git checkout $revision` after fetching. In case of a local branch that was already cloned before, this did not update the local branch. Fix this by calling `git reset --hard FETCH_HEAD` after the checkout if the revision is not a fixed revision. Signed-off-by: Martin Nonnenmacher --- .../src/funTest/kotlin/vcs/GitFunTest.kt | 95 +++++++++++++++++++ downloader/src/main/kotlin/vcs/Git.kt | 6 ++ 2 files changed, 101 insertions(+) create mode 100644 downloader/src/funTest/kotlin/vcs/GitFunTest.kt diff --git a/downloader/src/funTest/kotlin/vcs/GitFunTest.kt b/downloader/src/funTest/kotlin/vcs/GitFunTest.kt new file mode 100644 index 0000000000000..fe47eb1f8436f --- /dev/null +++ b/downloader/src/funTest/kotlin/vcs/GitFunTest.kt @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2023 The ORT Project Authors (see ) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * License-Filename: LICENSE + */ + +package org.ossreviewtoolkit.downloader.vcs + +import io.kotest.core.spec.style.WordSpec +import io.kotest.engine.spec.tempdir +import io.kotest.matchers.result.shouldBeSuccess +import io.kotest.matchers.shouldBe + +import java.io.File + +import org.ossreviewtoolkit.downloader.WorkingTree +import org.ossreviewtoolkit.model.VcsInfo +import org.ossreviewtoolkit.model.VcsType + +private val branches = mapOf( + "main" to "6f09f276c4426c387c6663f54bbd45aea8d81dac", + "branch1" to "0c58ea81d5c8112affab7a9cd6308deb4bc51589", + "branch2" to "7a05ad3ad30b4ddbfac22e0b768fb91383f16d8d", + "branch3" to "b798693a551e4d0e96d09409948327178a9abbce" +) + +private val tags = mapOf( + "tag1" to "0c58ea81d5c8112affab7a9cd6308deb4bc51589", + "tag2" to "7a05ad3ad30b4ddbfac22e0b768fb91383f16d8d", + "tag3" to "b798693a551e4d0e96d09409948327178a9abbce" +) + +class GitFunTest : WordSpec({ + val git = Git() + val vcsInfo = VcsInfo( + type = VcsType.GIT, + url = "https://github.com/oss-review-toolkit/ort-test-data-git.git", + revision = "main" + ) + + lateinit var repoDir: File + lateinit var workingTree: WorkingTree + + beforeEach { + repoDir = tempdir() + workingTree = git.initWorkingTree(repoDir, vcsInfo) + } + + "updateWorkingTree" should { + "update the working tree to the correct revision" { + branches.values.forEach { revision -> + git.updateWorkingTree(workingTree, revision) shouldBeSuccess revision + workingTree.getRevision() shouldBe revision + } + } + + "update the working tree to the correct tag" { + tags.forEach { (tag, revision) -> + git.updateWorkingTree(workingTree, tag) shouldBeSuccess tag + workingTree.getRevision() shouldBe revision + } + } + + "update the working tree to the correct branch" { + branches.forEach { (branch, revision) -> + git.updateWorkingTree(workingTree, branch) shouldBeSuccess branch + workingTree.getRevision() shouldBe revision + } + } + + "update an outdated local branch" { + val branch = "branch1" + val revision = branches.getValue(branch) + + git.updateWorkingTree(workingTree, branch) + git.run("reset", "--hard", "HEAD~1", workingDir = repoDir) + + git.updateWorkingTree(workingTree, branch) shouldBeSuccess branch + workingTree.getRevision() shouldBe revision + } + } +}) diff --git a/downloader/src/main/kotlin/vcs/Git.kt b/downloader/src/main/kotlin/vcs/Git.kt index 7c3b6771e3168..7953f8839123c 100644 --- a/downloader/src/main/kotlin/vcs/Git.kt +++ b/downloader/src/main/kotlin/vcs/Git.kt @@ -239,6 +239,12 @@ class Git : VersionControlSystem(), CommandLineTool { // TODO: Migrate this to JGit once https://bugs.eclipse.org/bugs/show_bug.cgi?id=383772 is implemented. run("checkout", revision, workingDir = workingTree.workingDir) + // In case of a non-fixed revision (branch or tag) reset the working tree to ensure that the previously + // fetched changes are applied. + if (!isFixedRevision(workingTree, revision).getOrThrow()) { + run("reset", "--hard", "FETCH_HEAD", workingDir = workingTree.workingDir) + } + revision }