Skip to content

Commit

Permalink
fix(downloader): Fix updating the Git working tree for a branch
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
mnonnenmacher committed Sep 22, 2023
1 parent 8863163 commit c519398
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
95 changes: 95 additions & 0 deletions downloader/src/funTest/kotlin/vcs/GitFunTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (C) 2023 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
*
* 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
}
}
})
6 changes: 6 additions & 0 deletions downloader/src/main/kotlin/vcs/Git.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit c519398

Please sign in to comment.