Skip to content

Commit

Permalink
fix(git): Do not rely on FETCH_HEAD to list the current branch first
Browse files Browse the repository at this point in the history
The `.git/FETCH_HEAD` file does not only contain a single ref for the
current branch, but all refs that have been fetched last. Do not assume
the entry for the current branch to be the first one, and use JGit's
`FetchResult` to search for the correct resolved revision to reset to.

Fixes #7725.

Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed Jan 24, 2024
1 parent 34859a6 commit b2262a1
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions plugins/version-control-systems/git/src/main/kotlin/Git.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ import org.apache.logging.log4j.kotlin.logger

import org.eclipse.jgit.api.Git
import org.eclipse.jgit.api.LsRemoteCommand
import org.eclipse.jgit.api.ResetCommand
import org.eclipse.jgit.api.errors.GitAPIException
import org.eclipse.jgit.errors.UnsupportedCredentialItem
import org.eclipse.jgit.lib.Constants
import org.eclipse.jgit.lib.ObjectIdRef
import org.eclipse.jgit.lib.SymbolicRef
import org.eclipse.jgit.transport.CredentialItem
import org.eclipse.jgit.transport.CredentialsProvider
Expand All @@ -51,6 +53,7 @@ import org.ossreviewtoolkit.utils.common.CommandLineTool
import org.ossreviewtoolkit.utils.common.Os
import org.ossreviewtoolkit.utils.common.collectMessages
import org.ossreviewtoolkit.utils.common.safeMkdirs
import org.ossreviewtoolkit.utils.common.withoutPrefix
import org.ossreviewtoolkit.utils.ort.requestPasswordAuthentication
import org.ossreviewtoolkit.utils.ort.showStackTrace

Expand Down Expand Up @@ -230,19 +233,34 @@ class Git : VersionControlSystem(), CommandLineTool {
} else {
workingTree.runGit("fetch", "--tags", "origin")
}

// Do a dummy fetch call to create a JGit FetchResult based on the Git CLI call.
git.fetch().call()
}.onFailure {
it.showStackTrace()

logger.warn { "Failed to fetch everything: ${it.collectMessages()}" }
}.mapCatching {
}.mapCatching { fetchResult ->
// TODO: Migrate this to JGit once sparse checkout (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)
val refs = fetchResult.advertisedRefs.filterIsInstance<ObjectIdRef>()

val tags = refs.mapNotNull { ref ->
ref.name.withoutPrefix(Constants.R_TAGS)?.let { it to ref.objectId.name }
}.toMap()

val branches = refs.mapNotNull { ref ->
ref.name.withoutPrefix(Constants.R_HEADS)?.let { it to ref.objectId.name }
}.toMap()

(tags[revision] ?: branches[revision])?.also { resolvedRevision ->
git.reset().setMode(ResetCommand.ResetType.HARD).setRef(resolvedRevision).call()
}
}

revision
Expand Down

0 comments on commit b2262a1

Please sign in to comment.