Browse Source

Check for Git merge-file --object-id at the start of the license check

pull/16341/head
Viacheslav Klimov 2 months ago
parent
commit
b63ac94da1
Failed to extract signature
  1. 4
      tools/src/main/python/license-headers/README.md
  2. 18
      tools/src/main/python/license-headers/check_license_headers.py
  3. 19
      tools/src/main/python/license-headers/test_check_license_headers.py

4
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

18
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}")

19
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()

Loading…
Cancel
Save