Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record the proper clone URL in the provenance #5852

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class SubversionWorkingTreeFunTest : StringSpec({
workingTree.isValid() shouldBe true
workingTree.getInfo() shouldBe VcsInfo(
type = VcsType.SUBVERSION,
url = "https://svn.code.sf.net/p/docutils/code/trunk/docutils",
url = "https://svn.code.sf.net/p/docutils/code",
revision = "8207",
path = ""
)
Expand Down
3 changes: 2 additions & 1 deletion downloader/src/main/kotlin/Downloader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,14 @@ class Downloader(private val config: DownloaderConfiguration) {
throw e
}
}

val resolvedRevision = workingTree.getRevision()

logger.info {
"Finished downloading source code revision '$resolvedRevision' to '${outputDirectory.absolutePath}'."
}

return RepositoryProvenance(pkg.vcsProcessed, resolvedRevision)
return RepositoryProvenance(pkg.vcsProcessed.copy(url = workingTree.getRemoteUrl()), resolvedRevision)
}

/**
Expand Down
65 changes: 3 additions & 62 deletions downloader/src/main/kotlin/vcs/Subversion.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,65 +69,7 @@ class Subversion : VersionControlSystem() {

override fun getDefaultBranchName(url: String) = "trunk"

override fun getWorkingTree(vcsDirectory: File) =
object : WorkingTree(vcsDirectory, type) {
private val directoryNamespaces = listOf("branches", "tags", "trunk", "wiki")

override fun isValid(): Boolean {
if (!workingDir.isDirectory) {
return false
}

return doSvnInfo() != null
}

override fun isShallow() = false

override fun getRemoteUrl() = doSvnInfo()?.url?.toString().orEmpty()

override fun getRevision() = doSvnInfo()?.committedRevision?.number?.toString().orEmpty()

override fun getRootPath() = doSvnInfo()?.workingCopyRoot ?: workingDir

private fun listRemoteRefs(namespace: String): List<String> {
val refs = mutableListOf<String>()
val remoteUrl = getRemoteUrl()

val projectRoot = if (directoryNamespaces.any { "/$it/" in remoteUrl }) {
doSvnInfo()?.repositoryRootURL?.toString().orEmpty()
} else {
remoteUrl
}

// We assume a single project directory layout.
val svnUrl = SVNURL.parseURIEncoded("$projectRoot/$namespace")

try {
clientManager.logClient.doList(
svnUrl,
SVNRevision.HEAD,
SVNRevision.HEAD,
/* fetchLocks = */ false,
/* recursive = */ false
) { dirEntry ->
if (dirEntry.name.isNotEmpty()) refs += "$namespace/${dirEntry.relativePath}"
}
} catch (e: SVNException) {
e.showStackTrace()

logger.info { "Unable to list remote refs for $type repository at $remoteUrl." }
}

return refs
}

override fun listRemoteBranches() = listRemoteRefs("branches")

override fun listRemoteTags() = listRemoteRefs("tags")

private fun doSvnInfo() =
runCatching { clientManager.wcClient.doInfo(workingDir, SVNRevision.WORKING) }.getOrNull()
}
override fun getWorkingTree(vcsDirectory: File) = SubversionWorkingTree(vcsDirectory, type, clientManager)

override fun isApplicableUrlInternal(vcsUrl: String) =
try {
Expand Down Expand Up @@ -199,9 +141,8 @@ class Subversion : VersionControlSystem() {
)
} ?: run {
// This code path updates the working tree to a symbolic revision.
val svnUrl = SVNURL.parseURIEncoded(
"${workingTree.getRemoteUrl()}/$revision"
)
val fullUrl = (workingTree as SubversionWorkingTree).getFullUrl()
val svnUrl = SVNURL.parseURIEncoded("$fullUrl/$revision")

// First switch the (empty) working tree to the requested branch / tag.
clientManager.updateClient.doSwitch(
Expand Down
97 changes: 97 additions & 0 deletions downloader/src/main/kotlin/vcs/SubversionWorkingTree.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright (C) 2022 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 java.io.File

import org.ossreviewtoolkit.downloader.WorkingTree
import org.ossreviewtoolkit.model.VcsType
import org.ossreviewtoolkit.utils.ort.showStackTrace

import org.tmatesoft.svn.core.SVNException
import org.tmatesoft.svn.core.SVNURL
import org.tmatesoft.svn.core.wc.SVNClientManager
import org.tmatesoft.svn.core.wc.SVNRevision

class SubversionWorkingTree(
workingDir: File,
vcsType: VcsType,
private val clientManager: SVNClientManager
) : WorkingTree(workingDir, vcsType) {
private val directoryNamespaces = listOf("branches", "tags", "trunk", "wiki")

override fun isValid(): Boolean {
if (!workingDir.isDirectory) {
return false
}

return doSvnInfo() != null
}

override fun isShallow() = false

override fun getRemoteUrl() = doSvnInfo()?.repositoryRootURL?.toString().orEmpty()

override fun getRevision() = doSvnInfo()?.committedRevision?.number?.toString().orEmpty()

override fun getRootPath() = doSvnInfo()?.workingCopyRoot ?: workingDir

override fun listRemoteBranches() = listRemoteRefs("branches")

override fun listRemoteTags() = listRemoteRefs("tags")

fun getFullUrl() = doSvnInfo()?.url?.toString().orEmpty()

private fun listRemoteRefs(namespace: String): List<String> {
val refs = mutableListOf<String>()
val remoteUrl = getRemoteUrl()
val fullUrl = getFullUrl()

val projectRoot = if (directoryNamespaces.any { "/$it/" in fullUrl }) {
remoteUrl
} else {
fullUrl
}

// We assume a single project directory layout.
val svnUrl = SVNURL.parseURIEncoded("$projectRoot/$namespace")

try {
clientManager.logClient.doList(
svnUrl,
SVNRevision.HEAD,
SVNRevision.HEAD,
/* fetchLocks = */ false,
/* recursive = */ false
) { dirEntry ->
if (dirEntry.name.isNotEmpty()) refs += "$namespace/${dirEntry.relativePath}"
}
} catch (e: SVNException) {
e.showStackTrace()

Subversion.logger.info { "Unable to list remote refs for $vcsType repository at $remoteUrl." }
}

return refs
}

private fun doSvnInfo() =
runCatching { clientManager.wcClient.doInfo(workingDir, SVNRevision.WORKING) }.getOrNull()
}