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 21, 2023
1 parent a55d1e9 commit bf57709
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 4 deletions.
93 changes: 93 additions & 0 deletions downloader/src/funTest/kotlin/vcs/GitFunTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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 org.ossreviewtoolkit.downloader.WorkingTree
import org.ossreviewtoolkit.model.VcsInfo
import org.ossreviewtoolkit.model.VcsType
import java.io.File

Check warning

Code scanning / detekt

Reports files that do not follow ORT's order for imports Warning

Imports in file '/home/runner/work/ort/ort/downloader/src/funTest/kotlin/vcs/GitFunTest.kt' are not sorted alphabetically or single blank lines are missing between different top-level packages

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

Check warning

Code scanning / detekt

Reports spaces around colons Warning

Unexpected spacing before ":"

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
}
}
})
13 changes: 9 additions & 4 deletions downloader/src/main/kotlin/vcs/Git.kt
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class Git : VersionControlSystem(), CommandLineTool {
Git(this).use { git ->
logger.info { "Updating working tree from '${workingTree.getRemoteUrl()}'." }

updateWorkingTreeWithoutSubmodules(git, revision).mapCatching {
updateWorkingTreeWithoutSubmodules(workingTree, git, revision).mapCatching {
// In case this throws the exception gets encapsulated as a failure.
if (recursive) updateSubmodules(workingTree)

Expand All @@ -183,7 +183,7 @@ class Git : VersionControlSystem(), CommandLineTool {
}
}

private fun updateWorkingTreeWithoutSubmodules(git: Git, revision: String): Result<String> =
private fun updateWorkingTreeWithoutSubmodules(workingTree: WorkingTree, git: Git, revision: String): Result<String> =

Check warning

Code scanning / detekt

Line detected, which is longer than the defined maximum line length in the code style. Warning

Line detected, which is longer than the defined maximum line length in the code style.
runCatching {
logger.info { "Trying to fetch only revision '$revision' with depth limited to $GIT_HISTORY_DEPTH." }

Expand Down Expand Up @@ -222,7 +222,6 @@ class Git : VersionControlSystem(), CommandLineTool {
logger.info { "Could not fetch everything using JGit: ${it.collectMessages()}" }
logger.info { "Falling back to Git CLI." }

val workingTree = GitWorkingTree(git.repository.workTree, VcsType.GIT)
if (workingTree.isShallow()) {
workingTree.runGit("fetch", "--unshallow", "--tags", "origin")
} else {
Expand All @@ -234,7 +233,13 @@ class Git : VersionControlSystem(), CommandLineTool {
logger.warn { "Failed to fetch everything: ${it.collectMessages()}" }
}.mapCatching {
// TODO: Migrate this to JGit once https://bugs.eclipse.org/bugs/show_bug.cgi?id=383772 is implemented.
run("checkout", revision, workingDir = git.repository.workTree)
run("checkout", revision, workingDir = workingTree.workingDir)

Check warning on line 236 in downloader/src/main/kotlin/vcs/Git.kt

View check run for this annotation

Codecov / codecov/patch

downloader/src/main/kotlin/vcs/Git.kt#L236

Added line #L236 was not covered by tests

// 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)

Check warning on line 241 in downloader/src/main/kotlin/vcs/Git.kt

View check run for this annotation

Codecov / codecov/patch

downloader/src/main/kotlin/vcs/Git.kt#L241

Added line #L241 was not covered by tests
}

revision
}

Check warning

Code scanning / detekt

Format signature to be single when possible, multiple lines otherwise. Warning

Newline expected after opening parenthesis

Check warning

Code scanning / detekt

Format signature to be single when possible, multiple lines otherwise. Warning

Parameter should start on a newline

Check warning

Code scanning / detekt

Format signature to be single when possible, multiple lines otherwise. Warning

Parameter should start on a newline

Check warning

Code scanning / detekt

Format signature to be single when possible, multiple lines otherwise. Warning

Newline expected before closing parenthesis
Expand Down

0 comments on commit bf57709

Please sign in to comment.