diff --git a/tools/src/main/python/license-headers/check_license_headers.py b/tools/src/main/python/license-headers/check_license_headers.py index 11c3e1aa57..a01d776e43 100644 --- a/tools/src/main/python/license-headers/check_license_headers.py +++ b/tools/src/main/python/license-headers/check_license_headers.py @@ -903,6 +903,7 @@ class Comparison: copies: dict merges: dict ce_directories: frozenset + size_filter: bool | None = None def classify(self, name, curation): key = ("head", name) @@ -1073,6 +1074,34 @@ def could_be_copy(size, destinations): return any(100 * min(size, other) >= COPY_SIMILARITY * max(size, other) for other in destinations) +def git_finds_copy(repo, plugin, source, destination): + """Whether the copy search, normalized_copies itself, matches destination, a new path, to source, a CE file.""" + with tempfile.TemporaryDirectory(prefix="license-provenance-") as temporary: + staging = Mycila(repo, Path(temporary), plugin) + staging.add(("Source.java", "probe"), "Source.java", source) + staging.add(("head", "Destination.java"), "Destination.java", destination) + found = normalized_copies(staging, {("Source.java", "probe"): "probe"}, {"Destination.java"}, staging.nonempty()) + return "Destination.java" in found + + +def size_filter_holds(repo, plugin): + """Whether the installed Git still never matches a source that could_be_copy rules out. The filter relies on Git + measuring similarity against the larger blob (estimate_similarity divides by the larger size, and diffcore-delta.c + counts at most the smaller blob as copied), so a source below COPY_SIMILARITY % of a destination can never reach + the threshold. The probe asks the real copy search twice: a source of exactly COPY_SIMILARITY % of a destination + must be found, the same source one byte shorter must not. That catches a Git that measures against anything but + the larger blob; nothing else in this repository would notice such a change, and the filter would then drop CE + files Git matches. Both answers are compared with could_be_copy itself, so a change of the Python rule that no + longer matches Git turns the filter off as well. Each case runs in its own repository, so a found boundary pair + cannot hide the other answer.""" + destination = b"".join(b"line %05d\n" % index for index in range(100)) + boundary = destination[:len(destination) * COPY_SIMILARITY // 100] + sources = [boundary, boundary[:-1]] + rule = [could_be_copy(len(source), [len(destination)]) for source in sources] + answers = parallel(lambda source: git_finds_copy(repo, plugin, source, destination), sources) + return rule == answers == [True, False] + + def could_shrink_to_copy(raw_size, destinations): """Normalizing never grows a file: below COPY_SIMILARITY % of the smallest destination it stays out of Git's reach.""" return 100 * raw_size >= COPY_SIMILARITY * min(destinations) @@ -1093,10 +1122,10 @@ def compare(repo, plugin, lineage, contents, cache, in_scope): for key in comparable_ce_files(files, copy_source_types).items(): sources.setdefault(key, base) versions = {(name, oid) for name in names for _, oid in lineage.versions(name)} - # Every input keeps the slot it has among all inputs, so the copy search sees the same paths, and Git breaks - # ties between equally similar sources the same way, however many sources the size filter drops. - slots = {("head", name): index for index, name in enumerate(names)} - slots |= {key: len(names) + index for index, key in enumerate(sorted(sources))} + # Git breaks ties between equally similar copy sources by path. Sources are staged in key order, padded so that + # paths sort like ranks: --since, a full run and the size filter all present any two sources in the same order. + slots = {("head", name): f"{index:08d}" for index, name in enumerate(sorted(names))} + slots |= {key: f"{index:08d}" for index, key in enumerate(sorted(sources))} bytes_by_id = blobs(repo, [oid for _, oid in sources]) with tempfile.TemporaryDirectory(prefix="license-provenance-") as temporary: mycila = Mycila(repo, Path(temporary), plugin) @@ -1113,8 +1142,8 @@ def compare(repo, plugin, lineage, contents, cache, in_scope): destinations = {name: mycila.contents[("head", name)] for name in sorted(new_paths) if mycila.contents[("head", name)].strip()} normalized = head_hashes | {key: comparison_hash(content) for key, content in copy_sources.items()} - copies, searched = copy_candidates(repo, plugin, cache, sources, versions, slots, bytes_by_id, - destinations, copy_sources) + copies, searched, size_filter = copy_candidates(repo, plugin, cache, sources, versions, slots, bytes_by_id, + destinations, copy_sources) normalized |= searched merges = clean_merges(repo, plugin, cache, lineage, names, normalized) console.end(f"done ({len(normalized)} file versions, {len(searched)} of {len(sources) - len(versions)} " @@ -1128,17 +1157,21 @@ def compare(repo, plugin, lineage, contents, cache, in_scope): merges=merges, ce_directories=frozenset(str(parent) for files in lineage.trees.values() for path in files for parent in PurePosixPath(path).parents), + size_filter=size_filter, ) def copy_candidates(repo, plugin, cache, sources, versions, slots, bytes_by_id, destinations, copy_sources): - """CE files similar to the paths that CE does not have, normalized only when Git could match them by size, and - the comparison hashes of the other CE files searched for them.""" + """CE files similar to the paths that CE does not have, normalized only when Git could match them by size; the + comparison hashes of the other CE files searched for them; and whether the size filter was used, None when there + was nothing to search for.""" if not destinations: - return {}, {} + return {}, {}, None sizes = [staged_size(content) for content in destinations.values()] + # Slower, never wrong: when the installed Git disagrees with the size rule, every CE file is searched. + size_filter = size_filter_holds(repo, plugin) others = sorted(key for key in sources if key not in versions - and could_shrink_to_copy(len(bytes_by_id[key[1]]), sizes)) + and (not size_filter or could_shrink_to_copy(len(bytes_by_id[key[1]]), sizes))) found = normalize(repo, plugin, cache, {key: bytes_by_id[key[1]] for key in others}) searched = {key: comparison_hash(content) for key, content in found.items()} with tempfile.TemporaryDirectory(prefix="license-provenance-") as temporary: @@ -1148,8 +1181,8 @@ def copy_candidates(repo, plugin, cache, sources, versions, slots, bytes_by_id, for name, content in destinations.items(): staging.add(("head", name), name, content, slots[("head", name)]) reachable = {key: sources[key] for key in [*others, *copy_sources] - if could_be_copy(staged_size(staging.contents[key]), sizes)} - return normalized_copies(staging, reachable, set(destinations), staging.nonempty()), searched + if not size_filter or could_be_copy(staged_size(staging.contents[key]), sizes)} + return normalized_copies(staging, reachable, set(destinations), staging.nonempty()), searched, size_filter def clean_merges(repo, plugin, cache, lineage, names, normalized): @@ -1268,6 +1301,10 @@ def select_changes(repo, since, bases, ce_bases, plugin, curations): return Selection(since, base, paths, f"{len(paths)} changed paths") +def git_version(): + return run(["git", "--version"], Path.cwd()).stdout.decode().strip().removeprefix("git version ") + + def current_branch(repo): branch = git(repo, "rev-parse", "--abbrev-ref", "HEAD").decode().strip() return "" if branch == "HEAD" else branch @@ -1523,6 +1560,7 @@ def check(args): console.line(f"Checking {len(checked)} of {len(scope.eligible)} files in scope", "dim") contents = {name: (repo / name).read_bytes() for name in checked} rows = [] + size_filter = None if contents: cache = (NoCache() if args.no_cache else NormalizationCache((repo / args.cache).resolve(), cache_signature(repo, plugin))) @@ -1531,7 +1569,12 @@ def check(args): finally: cache.close() rows = [comparison.classify(name, curations.get(name)) for name in checked] + size_filter = comparison.size_filter console.line() + if size_filter is False: + console.line(f"Git's copy detection (git {git_version()}) and the size rule of the copy search disagree; " + "every CE file was searched for copies. Report this to the maintainers of the check.", "red") + console.line() orphaned = [{"path": name, "status": "stale-curation", "reason": "file is no longer in scope"} for name in sorted(set(curations) - set(scope.eligible))] ensure_inputs_unchanged(repo, lineage, curation_path, curation_bytes, contents) @@ -1547,6 +1590,7 @@ def check(args): "ce": ce | {"bases": list(lineage.bases)}, "scope": {"supported": len(scope.eligible), "unknown": scope.unknown, "modules": scope.modules}, "selection": selection.report() if selection is not None else None, + "copySearch": {"sizeFilter": size_filter}, "summary": dict(summary), "restamped": sorted(row["path"] for row in rows if "previousHeader" in row), "removedCurations": removed, diff --git a/tools/src/main/python/license-headers/test_check_license_headers.py b/tools/src/main/python/license-headers/test_check_license_headers.py index 5cd9d1b311..9989f5b2ec 100644 --- a/tools/src/main/python/license-headers/test_check_license_headers.py +++ b/tools/src/main/python/license-headers/test_check_license_headers.py @@ -1237,8 +1237,8 @@ class CopySearchTests(unittest.TestCase): for category, name in p.TEMPLATES.items(): git.write(name, f"fixture {category}\n") git.write(p.CURATIONS, '{"schemaVersion": 1, "curations": []}') - # Sorted CE sources: a/ (small), b/Alpha, c/ (small), e/Gamma. Among all inputs Gamma takes slot 12 and - # Alpha slot 3, so Gamma comes first in the copy search and wins the tie between the identical sources. + # Sorted CE sources: a/ (small), b/Alpha, c/ (small), e/Gamma. Alpha and Gamma are identical: Alpha ranks first + # among all CE files and wins the tie, whichever sources the size filter drops and whatever else is checked. for name in ("a/Small0.java", "a/Small1.java", *(f"c/Small{index}.java" for index in range(2, 10))): git.write(name, fixture("apache") + "x\n") git.write("b/Alpha.java", fixture("apache") + self.BODY) @@ -1251,16 +1251,108 @@ class CopySearchTests(unittest.TestCase): def test_size_filter_keeps_the_copy_search_result(self): _, filtered, output = self.check() - with (patch.object(p, "could_shrink_to_copy", return_value=True), - patch.object(p, "could_be_copy", return_value=True)): + with patch.object(p, "size_filter_holds", return_value=False): _, unfiltered, unfiltered_output = self.check() - self.assertEqual(unfiltered, filtered) + self.assertEqual(unfiltered["files"], filtered["files"]) + self.assertEqual(({"sizeFilter": True}, {"sizeFilter": False}), + (filtered["copySearch"], unfiltered["copySearch"])) + self.assertNotIn("and the size rule of the copy search disagree", output) + self.assertIn("and the size rule of the copy search disagree", unfiltered_output) row = self.rows(filtered)["d/Delta.java"] self.assertEqual(("unresolved", "apache"), (row["status"], row["suggestedHeader"])) - self.assertEqual(["e/Gamma.java"], [copy["path"] for copy in row["copyCandidates"]]) + self.assertEqual(["b/Alpha.java"], [copy["path"] for copy in row["copyCandidates"]]) self.assertIn("2 of 12 other CE files searched for copies", output) self.assertIn("12 of 12 other CE files searched for copies", unfiltered_output) + def test_since_reports_the_copy_a_full_run_reports_among_equally_similar_sources(self): + # TB files beside the new one shift where the checked inputs sit among all inputs of a full run. + others = [f"t/Tb{index}.java" for index in range(7)] + self.git("reset", "-q", "--hard", "HEAD~1") + for name in others: + self.git.write(name, fixture(p.NON_CE) + f"TB source {name}\n") + self.relicensing = self.git.commit("relicensing with TB files") + self.git("checkout", "-q", "-b", "feature") + self.git.write("d/Delta.java", fixture(p.NON_CE) + self.BODY) + self.git.commit("copy of a CE file under a new path") + scope = sorted(others + ["d/Delta.java"]) + _, full, _ = self.check(scope=scope) + _, since, _ = self.check("--since", "master", scope=scope) + self.assertEqual(["d/Delta.java"], [row["path"] for row in since["files"]]) + row = self.rows(full)["d/Delta.java"] + self.assertEqual(("unresolved", 1), (row["status"], len(row["copyCandidates"]))) + self.assertEqual("b/Alpha.java", row["copyCandidates"][0]["path"]) + self.assertEqual(row, self.rows(since)["d/Delta.java"]) + + def test_without_the_size_filter_git_searches_every_ce_file(self): + searched = [] + copies = p.normalized_copies + + def spy(mycila, source_refs, names, nonempty): + if "d/Delta.java" in names: # not the size probe + searched.append(sorted(key[0] for key in source_refs)) + return copies(mycila, source_refs, names, nonempty) + + with patch.object(p, "normalized_copies", spy): + self.check() + with patch.object(p, "size_filter_holds", return_value=False): + self.check() + filtered, unfiltered = searched + self.assertEqual(["b/Alpha.java", "e/Gamma.java"], filtered) + self.assertEqual(12, len(unfiltered)) + + def probe(self): + return p.size_filter_holds(this_repository(), fixture_plugin()) + + def test_size_filter_holds_for_the_installed_git(self): + self.assertTrue(self.probe()) + + def test_a_python_rule_that_no_longer_matches_git_turns_the_size_filter_off(self): + stricter = lambda size, destinations: any(100 * min(size, other) > p.COPY_SIMILARITY * max(size, other) + for other in destinations) + with patch.object(p, "could_be_copy", stricter): + self.assertFalse(self.probe()) + + def test_report_marks_the_size_filter_only_when_copies_were_searched(self): + _, report, _ = self.check() + self.assertEqual({"sizeFilter": True}, report["copySearch"]) + self.git.write("d/Delta.java", fixture("apache") + "base implementation\n") + self.git.commit("Delta in CE as well") + with patch.object(p, "RELICENSING_COMMIT", "0" * 40), patch.object(p, "relicensed_ce_bases", + return_value=[self.git("rev-parse", "HEAD")]): + _, report, output = self.check() + self.assertEqual({"sizeFilter": None}, report["copySearch"]) + self.assertNotIn("and the size rule of the copy search disagree", output) + + def test_size_filter_is_off_when_git_answers_otherwise_on_either_side_of_the_boundary(self): + boundary = 1000 * p.COPY_SIMILARITY // 100 + 100 * p.COPY_SIMILARITY // 100 # 100 lines of 11 bytes + + def git(at_boundary, below): + return lambda repo, plugin, source, destination: at_boundary if len(source) == boundary else below + + for at_boundary, below, holds in ((True, False, True), # Git follows the rule + (True, True, False), # looser: a CE file below the boundary matches + (False, False, False), # the boundary pair does not match either + (False, True, False)): + with patch.object(p, "git_finds_copy", git(at_boundary, below)): + self.assertEqual(holds, self.probe(), (at_boundary, below)) + + def test_probe_pairs_sit_on_both_sides_of_the_boundary(self): + sizes = [] + def finds(repo, plugin, source, destination): + sizes.append((len(source), len(destination))) + return len(source) % 2 == 0 + + with patch.dict(os.environ, {p.JOBS_VARIABLE: "1"}), patch.object(p, "git_finds_copy", finds): + self.assertTrue(self.probe()) + (boundary, destination), (below, _) = sizes + self.assertEqual((boundary * 100, below), (p.COPY_SIMILARITY * destination, boundary - 1)) + + def test_size_filter_follows_the_configured_similarity(self): + with patch.object(p, "COPY_SIMILARITY", 60): + self.assertTrue(self.probe()) + self.assertTrue(p.could_be_copy(600, [1000])) + self.assertFalse(p.could_be_copy(599, [1000])) + def test_a_file_grown_by_normalizing_fails_the_check(self): def strip_headers(tool): tool.removed, tool.stacked = {key: b"" for key in tool.contents}, set()