Browse Source

Fail the license header check on Git without merge-file --object-id

pull/16341/head
Viacheslav Klimov 2 months ago
parent
commit
ccc4a2acd0
Failed to extract signature
  1. 8
      tools/src/main/python/license-headers/check_license_headers.py
  2. 18
      tools/src/main/python/license-headers/test_check_license_headers.py

8
tools/src/main/python/license-headers/check_license_headers.py

@ -95,6 +95,9 @@ STRICT_BLOCK = Style("strict_block", tuple(sorted(suffix[1:] for suffix in BLOCK
)) ))
MERGE_FILE_REQUIREMENT = "The license header check needs Git 2.43 or later"
class Failure(Exception): class Failure(Exception):
pass pass
@ -194,7 +197,10 @@ def merged_ce_bases(repo, head, baseline):
def merge_blobs(repo, ours, base, theirs): def merge_blobs(repo, ours, base, theirs):
result = run(["git", "merge-file", "-p", "--object-id", ours, base, theirs], repo, ok=range(0, 256)) 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()}.")
return result.stdout if result.returncode == 0 else None return result.stdout if result.returncode == 0 else None

18
tools/src/main/python/license-headers/test_check_license_headers.py

@ -8,6 +8,7 @@ import io
import json import json
import os import os
from pathlib import Path from pathlib import Path
import subprocess
import tempfile import tempfile
import textwrap import textwrap
import unittest import unittest
@ -27,6 +28,11 @@ def this_repository():
return Path(p.git(Path(__file__).parent, "rev-parse", "--show-toplevel").decode().strip()) 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)
def fixture_plugin(version="0"): def fixture_plugin(version="0"):
return p.Plugin(version, ET.Element("configuration")) return p.Plugin(version, ET.Element("configuration"))
@ -519,6 +525,7 @@ class GitHistoryTests(unittest.TestCase):
with patch.object(p, "RELICENSING_COMMIT", squash): with patch.object(p, "RELICENSING_COMMIT", squash):
self.assertEqual({"Final.java": "Base.java"}, p.Lineage.load(git.root, head, [base]).renames) self.assertEqual({"Final.java": "Base.java"}, p.Lineage.load(git.root, head, [base]).renames)
@requires_merge_file_object_id
def test_merge_blobs_returns_only_clean_merges(self): def test_merge_blobs_returns_only_clean_merges(self):
git = GitFixture(temporary_directory(self), "main") git = GitFixture(temporary_directory(self), "main")
git.write("File.java", "one\ntwo\nthree\nfour\nfive\n") git.write("File.java", "one\ntwo\nthree\nfour\nfive\n")
@ -534,6 +541,12 @@ class GitHistoryTests(unittest.TestCase):
self.assertEqual(b"ONE\ntwo\nthree\nfour\nFIVE\n", p.merge_blobs(git.root, ours, base, theirs)) self.assertEqual(b"ONE\ntwo\nthree\nfour\nFIVE\n", p.merge_blobs(git.root, ours, base, theirs))
self.assertIsNone(p.merge_blobs(git.root, ours, base, blob("uno\ntwo\nthree\nfour\nfive\n"))) self.assertIsNone(p.merge_blobs(git.root, ours, base, blob("uno\ntwo\nthree\nfour\nfive\n")))
def test_merge_blobs_fails_without_object_id_support(self):
usage = subprocess.CompletedProcess([], 129, b"", b"error: unknown option `object-id'")
with (patch.object(p, "run", side_effect=[usage, subprocess.CompletedProcess([], 0, b"git version 2.39.5\n", b"")]),
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_ignore_filter_keeps_tracked_and_unignored_files(self): def test_ignore_filter_keeps_tracked_and_unignored_files(self):
repo = temporary_directory(self) repo = temporary_directory(self)
p.git(repo, "init") p.git(repo, "init")
@ -679,6 +692,7 @@ class CheckCommandTests(unittest.TestCase):
def rows(self, report): def rows(self, report):
return {row["path"]: row for row in report["files"]} return {row["path"]: row for row in report["files"]}
@requires_merge_file_object_id
def test_relicensed_branch_passes(self): def test_relicensed_branch_passes(self):
code, report, _ = self.check() code, report, _ = self.check()
self.assertEqual(0, code) self.assertEqual(0, code)
@ -693,6 +707,7 @@ class CheckCommandTests(unittest.TestCase):
self.assertEqual({"path": "Fixed.java", "ref": self.fix, "blob": self.git("rev-parse", f"{self.fix}:Fixed.java")}, self.assertEqual({"path": "Fixed.java", "ref": self.fix, "blob": self.git("rev-parse", f"{self.fix}:Fixed.java")},
rows["Fixed.java"]["identicalTo"]) rows["Fixed.java"]["identicalTo"])
@requires_merge_file_object_id
def test_fix_restamps_decided_headers_only(self): def test_fix_restamps_decided_headers_only(self):
self.git.write("Base.java", FOREIGN_LINE.decode() + "base implementation\n") self.git.write("Base.java", FOREIGN_LINE.decode() + "base implementation\n")
self.git.write("New.java", fixture("apache") + "TB feature\n") self.git.write("New.java", fixture("apache") + "TB feature\n")
@ -719,6 +734,7 @@ class CheckCommandTests(unittest.TestCase):
self.assertEqual([], report["restamped"]) self.assertEqual([], report["restamped"])
self.assertEqual({"match": 5, "mismatch": 1}, report["summary"]) self.assertEqual({"match": 5, "mismatch": 1}, report["summary"])
@requires_merge_file_object_id
def test_ce_evidence_for_new_paths_needs_a_decision(self): def test_ce_evidence_for_new_paths_needs_a_decision(self):
self.git.write("Copy.java", fixture(p.NON_CE) + self.body) self.git.write("Copy.java", fixture(p.NON_CE) + self.body)
self.git.write("Deleted.java", fixture(p.NON_CE) + "removed CE source\n") self.git.write("Deleted.java", fixture(p.NON_CE) + "removed CE source\n")
@ -732,6 +748,7 @@ class CheckCommandTests(unittest.TestCase):
rows["Deleted.java"]["historicalCandidate"]) rows["Deleted.java"]["historicalCandidate"])
self.assertIn("Needs a decision (2)\n", output) self.assertIn("Needs a decision (2)\n", output)
@requires_merge_file_object_id
def test_renamed_ce_file_is_ce_modified(self): def test_renamed_ce_file_is_ce_modified(self):
self.git("mv", "Source.java", "Moved.java") self.git("mv", "Source.java", "Moved.java")
self.git.write("Moved.java", fixture("ce-modified") + self.body + "TB change\n") self.git.write("Moved.java", fixture("ce-modified") + self.body + "TB change\n")
@ -742,6 +759,7 @@ class CheckCommandTests(unittest.TestCase):
(row["status"], row["expectedHeader"], row["reason"])) (row["status"], row["expectedHeader"], row["reason"]))
self.assertEqual({"Source.java"}, {version["path"] for version in row["renamedFrom"]}) self.assertEqual({"Source.java"}, {version["path"] for version in row["renamedFrom"]})
@requires_merge_file_object_id
def test_fix_removes_redundant_and_orphaned_curations(self): def test_fix_removes_redundant_and_orphaned_curations(self):
self.git.write("Copy.java", fixture(p.NON_CE) + self.body) self.git.write("Copy.java", fixture(p.NON_CE) + self.body)

Loading…
Cancel
Save