Skip to content

Commit 16bf895

Browse files
committed
perf(model): Filter down to relevant files before getting the main license
As an alternative to adding caching, which also included lookups for irrelevant files, use another approach to only look for relevant files in the first place. As only configured license file patterns can contribute to the main license, add a preceding step to only keep findings from those files before determining the main license. Signed-off-by: Sebastian Schuberth <[email protected]>
1 parent 43735c0 commit 16bf895

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

model/src/main/kotlin/licenses/ResolvedLicenseInfo.kt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import org.ossreviewtoolkit.model.config.CopyrightGarbage
2828
import org.ossreviewtoolkit.model.config.LicenseFilePatterns
2929
import org.ossreviewtoolkit.model.config.PathExclude
3030
import org.ossreviewtoolkit.model.utils.PathLicenseMatcher
31+
import org.ossreviewtoolkit.utils.common.FileMatcher
3132
import org.ossreviewtoolkit.utils.ort.CopyrightStatementsProcessor
3233
import org.ossreviewtoolkit.utils.spdx.SpdxExpression
3334
import org.ossreviewtoolkit.utils.spdx.SpdxLicenseChoice
@@ -83,12 +84,23 @@ data class ResolvedLicenseInfo(
8384
* in any of the configured [LicenseFilePatterns] matched against the root path of the package (or project).
8485
*/
8586
fun mainLicense(): SpdxExpression? {
86-
val licenseMatcher = PathLicenseMatcher(LicenseFilePatterns.getInstance())
87-
val licensePaths = flatMap { resolvedLicense ->
87+
val licenseFilePatterns = LicenseFilePatterns.getInstance()
88+
val fileMatcher = FileMatcher(licenseFilePatterns.allLicenseFilenames, ignoreCase = true)
89+
val licenseMatcher = PathLicenseMatcher(licenseFilePatterns)
90+
91+
// Only keep those resolved licenses that can contribute to the main license as they match the configured
92+
// license file patterns. This vastly reduces the search for applicable license files for scan results with a
93+
// lot of detected license findings, like from file headers in a large code base.
94+
val relevantResolvedLicenses = mapNotNull { resolvedLicense ->
95+
val locations = resolvedLicense.locations.filterTo(mutableSetOf()) { fileMatcher.matches(it.location.path) }
96+
if (locations.isNotEmpty()) resolvedLicense.copy(locations = locations) else null
97+
}
98+
99+
val licensePaths = relevantResolvedLicenses.flatMap { resolvedLicense ->
88100
resolvedLicense.locations.map { it.location.path }
89101
}
90102

91-
val detectedLicenses = filterTo(mutableSetOf()) { resolvedLicense ->
103+
val detectedLicenses = relevantResolvedLicenses.filterTo(mutableSetOf()) { resolvedLicense ->
92104
resolvedLicense.locations.any {
93105
val rootPath = (it.provenance as? RepositoryProvenance)?.vcsInfo?.path.orEmpty()
94106

0 commit comments

Comments
 (0)