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

Compare command #7104

Merged
merged 5 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat(cli): Add a command to compare ORT results
Resolves #7072.

Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed Jun 8, 2023
commit 2bfca310dc2264d1b2376dac1b59f3cf6672e926
1 change: 1 addition & 0 deletions plugins/commands/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ javaPlatform {
dependencies {
api(project(":plugins:commands:advisor-command"))
api(project(":plugins:commands:analyzer-command"))
api(project(":plugins:commands:compare-command"))
api(project(":plugins:commands:config-command"))
api(project(":plugins:commands:downloader-command"))
api(project(":plugins:commands:evaluator-command"))
Expand Down
33 changes: 33 additions & 0 deletions plugins/commands/compare/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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
*/

plugins {
// Apply core plugins.
`java-library`
}

dependencies {
api(project(":plugins:commands:command-api"))

implementation(project(":model"))
implementation(project(":utils:common-utils"))

implementation(libs.clikt)
implementation(libs.diffUtils)
}
130 changes: 130 additions & 0 deletions plugins/commands/compare/src/main/kotlin/CompareCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright (C) 2021 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.plugins.commands.compare

import com.github.ajalt.clikt.core.ProgramResult
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.arguments.convert
import com.github.ajalt.clikt.parameters.options.default
import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.types.enum
import com.github.ajalt.clikt.parameters.types.file
import com.github.difflib.DiffUtils
import com.github.difflib.UnifiedDiffUtils

import java.io.File
import java.time.Instant

import org.ossreviewtoolkit.plugins.commands.api.OrtCommand
import org.ossreviewtoolkit.utils.common.expandTilde
import org.ossreviewtoolkit.utils.common.getCommonParentFile

class CompareCommand : OrtCommand(
name = "compare",
help = "Compare two ORT results with various methods."
) {
private val fileA by argument(help = "The first ORT result file to compare.")
.convert { it.expandTilde() }
.file(mustExist = true, canBeFile = true, canBeDir = false, mustBeWritable = false, mustBeReadable = true)
.convert { it.absoluteFile.normalize() }

private val fileB by argument(help = "The second ORT result file to compare.")
.convert { it.expandTilde() }
.file(mustExist = true, canBeFile = true, canBeDir = false, mustBeWritable = false, mustBeReadable = true)
.convert { it.absoluteFile.normalize() }

private val method by option(
"--method", "-m",
help = "The method to use when comparing ORT results. Must be one of " +
"${enumValues<CompareMethod>().map { it.name }}."
).enum<CompareMethod>()
.default(CompareMethod.TEXT_DIFF)

private val ignoreTime by option(
"--ignore-time", "-t",
help = "Ignore time differences."
).flag()

override fun run() {
if (fileA == fileB) {
println("The arguments point to the same file.")
throw ProgramResult(0)
}

when (method) {
CompareMethod.TEXT_DIFF -> {
val replacements = buildMap {
if (ignoreTime) {
put("""^(\s+(?:start|end)_time:) "[^"]+"$""", "$1 \"${Instant.EPOCH}\"")
}
}

val diff = unifiedDiff(fileA, fileB, replacements)
if (diff.isEmpty()) {
println("The ORT results are the same.")
throw ProgramResult(0)
}

println("The ORT results differ:")

diff.forEach {
println(it)
}

throw ProgramResult(1)
}
}
}
}

private enum class CompareMethod {
TEXT_DIFF
}

private fun unifiedDiff(
a: File, b: File, replacements: Map<String, String> = emptyMap(), contextSize: Int = 7
): List<String> {
val replaceRegexes = replacements.mapKeys { (pattern, _) -> Regex(pattern, RegexOption.MULTILINE) }

val textA = a.readText().let {
replaceRegexes.entries.fold(it) { text, (from, to) ->
text.replace(from, to)
}
}

val textB = b.readText().let {
replaceRegexes.entries.fold(it) { text, (from, to) ->
text.replace(from, to)
}
}

val linesA = textA.lines()
val linesB = textB.lines()

val commonParent = getCommonParentFile(setOf(a, b))
return UnifiedDiffUtils.generateUnifiedDiff(
"a/${a.relativeTo(commonParent).invariantSeparatorsPath}",
"b/${b.relativeTo(commonParent).invariantSeparatorsPath}",
linesA,
DiffUtils.diff(linesA, linesB),
contextSize
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.ossreviewtoolkit.plugins.commands.compare.CompareCommand
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ include(":plugins:commands")
include(":plugins:commands:advisor")
include(":plugins:commands:analyzer")
include(":plugins:commands:api")
include(":plugins:commands:compare")
include(":plugins:commands:config")
include(":plugins:commands:downloader")
include(":plugins:commands:evaluator")
Expand Down Expand Up @@ -108,6 +109,7 @@ project(":clients:vulnerable-code").name = "vulnerable-code-client"
project(":plugins:commands:advisor").name = "advisor-command"
project(":plugins:commands:analyzer").name = "analyzer-command"
project(":plugins:commands:api").name = "command-api"
project(":plugins:commands:compare").name = "compare-command"
project(":plugins:commands:config").name = "config-command"
project(":plugins:commands:downloader").name = "downloader-command"
project(":plugins:commands:evaluator").name = "evaluator-command"
Expand Down