From b63ac94da130822b97d6d3ab499b0a44ce478bb2 Mon Sep 17 00:00:00 2001 From: Viacheslav Klimov Date: Fri, 16 Oct 2026 14:53:11 +0300 Subject: [PATCH] Check for Git merge-file --object-id at the start of the license check --- .../src/main/python/license-headers/README.md | 4 +++- .../license-headers/check_license_headers.py | 18 ++++++++++++++++-- .../test_check_license_headers.py | 19 ++++++++++++++++--- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/tools/src/main/python/license-headers/README.md b/tools/src/main/python/license-headers/README.md index 99137a4426..7dd2386381 100644 --- a/tools/src/main/python/license-headers/README.md +++ b/tools/src/main/python/license-headers/README.md @@ -45,7 +45,9 @@ packaging define the new artifact. ./check-license-headers.sh ``` -The check needs the full Git history of the clone. +The check needs Git 2.43 or later, for `git merge-file --object-id`, and the +full Git history of the clone. It stops at startup on an older Git, such as the +Git that ships with macOS. ## Fix automatically 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 a8759c4d47..c82dce92c2 100644 --- a/tools/src/main/python/license-headers/check_license_headers.py +++ b/tools/src/main/python/license-headers/check_license_headers.py @@ -200,11 +200,23 @@ def merged_ce_bases(repo, head, baseline): def merge_blobs(repo, ours, base, theirs): result = run(["git", "merge-file", "-p", "--object-id", ours, base, theirs], repo, ok=(*range(0, 128), 129)) if result.returncode == 129: - raise Failure(f"git merge-file does not support --object-id. {MERGE_FILE_REQUIREMENT}, " - f"found {git(repo, 'version').decode().strip()}.") + raise unsupported_merge_file(repo) return result.stdout if result.returncode == 0 else None +def merge_file_object_id_supported(): + with tempfile.TemporaryDirectory(prefix="license-provenance-") as temporary: + git(temporary, "init", "--quiet") + blob = run(["git", "hash-object", "-w", "--stdin"], temporary, data=b"probe\n").stdout.decode().strip() + result = run(["git", "merge-file", "-p", "--object-id", blob, blob, blob], temporary, ok=(0, 129)) + return result.returncode == 0 + + +def unsupported_merge_file(directory): + return Failure(f"git merge-file does not support --object-id. {MERGE_FILE_REQUIREMENT}, " + f"found {git(directory, 'version').decode().strip()}.") + + def tree(repo, revision): result = {} for record in git(repo, "ls-tree", "-r", "-z", "--full-tree", revision).split(b"\0"): @@ -1224,6 +1236,8 @@ def prune_curations(path, rows, orphaned): def check(args): repo = repository_root() + if not merge_file_object_id_supported(): + raise unsupported_merge_file(repo) head = resolve(repo, "HEAD") if args.ce_ref: console.begin(f"Fetching {args.ce_ref} from {args.ce_remote}") 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 335809ae33..a108aee2c5 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 @@ -28,9 +28,8 @@ def this_repository(): return Path(p.git(Path(__file__).parent, "rev-parse", "--show-toplevel").decode().strip()) -requires_merge_file_object_id = unittest.skipUnless( - b"object-id" in subprocess.run(["git", "merge-file", "-h"], capture_output=True).stdout, - p.MERGE_FILE_REQUIREMENT) +MERGE_FILE_OBJECT_ID = b"object-id" in subprocess.run(["git", "merge-file", "-h"], capture_output=True).stdout +requires_merge_file_object_id = unittest.skipUnless(MERGE_FILE_OBJECT_ID, p.MERGE_FILE_REQUIREMENT) def fixture_plugin(version="0"): @@ -578,6 +577,9 @@ class GitHistoryTests(unittest.TestCase): self.assertRaisesRegex(p.Failure, "needs Git 2.43 or later, found git version 2.39.5")): p.merge_blobs(Path("."), "a", "b", "c") + def test_merge_file_probe_detects_object_id_support(self): + self.assertEqual(MERGE_FILE_OBJECT_ID, p.merge_file_object_id_supported()) + def test_ignore_filter_keeps_tracked_and_unignored_files(self): repo = temporary_directory(self) p.git(repo, "init") @@ -652,6 +654,9 @@ class CheckCommandTests(unittest.TestCase): LINES = "".join(f"line {index}\n" for index in range(10)) def setUp(self): + probe = patch.object(p, "merge_file_object_id_supported", return_value=True) + probe.start() + self.addCleanup(probe.stop) self.repo = temporary_directory(self) self.git = git = GitFixture(self.repo, "master") self.body = "CE source implementation\n" * 20 @@ -842,6 +847,14 @@ class CheckCommandTests(unittest.TestCase): code, report, _ = self.check("--fix", scope=scope) self.assertEqual((0, []), (code, report["removedCurations"])) + def test_git_without_merge_file_object_id_fails_at_startup(self): + with patch.object(p, "merge_file_object_id_supported", return_value=False): + code, report, output = self.check("--ce-ref", "lts-4.3", "--ce-remote", str(self.repo)) + self.assertEqual((2, None), (code, report)) + self.assertNotIn("Fetching", output) + self.assertIn(f"Error: git merge-file does not support --object-id. {p.MERGE_FILE_REQUIREMENT}, found git version", + output) + def test_missing_relicensing_commit_fails_before_the_scan(self): self.relicensing = "0" * 40 code, report, output = self.check()