@@ -28,6 +28,7 @@ import org.ossreviewtoolkit.model.config.CopyrightGarbage
2828import org.ossreviewtoolkit.model.config.LicenseFilePatterns
2929import org.ossreviewtoolkit.model.config.PathExclude
3030import org.ossreviewtoolkit.model.utils.PathLicenseMatcher
31+ import org.ossreviewtoolkit.model.utils.vcsPath
3132import org.ossreviewtoolkit.utils.common.FileMatcher
3233import org.ossreviewtoolkit.utils.ort.CopyrightStatementsProcessor
3334import org.ossreviewtoolkit.utils.spdx.SpdxExpression
@@ -85,38 +86,31 @@ data class ResolvedLicenseInfo(
8586 */
8687 fun mainLicense (): SpdxExpression ? {
8788 val licenseFilePatterns = LicenseFilePatterns .getInstance()
88- val fileMatcher = FileMatcher (licenseFilePatterns.allLicenseFilenames, ignoreCase = true )
8989 val licenseMatcher = PathLicenseMatcher (licenseFilePatterns)
9090
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- }
91+ val declaredLicenses = filter(LicenseView .ONLY_DECLARED , filterSources = true )
92+ val detectedLicenses = filter(LicenseView .ONLY_DETECTED , filterSources = true )
9893
99- val licensePaths = relevantResolvedLicenses.flatMap { resolvedLicense ->
94+ val licensePaths = detectedLicenses.flatMapTo( mutableSetOf ()) { resolvedLicense ->
10095 resolvedLicense.locations.map { it.location.path }
10196 }
10297
103- val detectedLicenses = relevantResolvedLicenses.filterTo(mutableSetOf ()) { resolvedLicense ->
104- resolvedLicense.locations.any {
105- val packageRoot = (it.provenance as ? RepositoryProvenance )?.vcsInfo?.path.orEmpty()
98+ val packageRoot = detectedLicenses.firstOrNull()?.locations?.firstOrNull()?.provenance.vcsPath
10699
107- val applicableLicensePaths = licenseMatcher.getApplicableLicenseFilesForDirectories(
108- licensePaths,
109- listOf (packageRoot)
110- )
100+ val applicableLicensePaths = licenseMatcher.getApplicableLicenseFilesForDirectories(
101+ licensePaths,
102+ listOf (packageRoot)
103+ )
111104
112- val applicableLicenseFiles = applicableLicensePaths[packageRoot].orEmpty()
105+ val applicableLicenseFiles = applicableLicensePaths[packageRoot].orEmpty()
113106
107+ val relevantDetectedLicenses = detectedLicenses.filterTo(mutableSetOf ()) { resolvedLicense ->
108+ resolvedLicense.locations.any {
114109 it.location.path in applicableLicenseFiles
115110 }
116111 }
117112
118- val declaredLicenses = filter(LicenseView .ONLY_DECLARED )
119- val mainLicenses = (detectedLicenses + declaredLicenses.licenses)
113+ val mainLicenses = (declaredLicenses.licenses + relevantDetectedLicenses)
120114 .flatMap { it.originalExpressions }
121115 .map { it.expression }
122116
0 commit comments