Browse Source

Judge license check rename continuity by commit ancestry

pull/16341/head
Viacheslav Klimov 2 months ago
parent
commit
38f6f30b91
Failed to extract signature
  1. 8
      tools/src/main/python/license-headers/README.md
  2. 28
      tools/src/main/python/license-headers/check_license_headers.py
  3. 17
      tools/src/main/python/license-headers/test_check_license_headers.py

8
tools/src/main/python/license-headers/README.md

@ -26,9 +26,11 @@ header equals a CE version of it, or a clean merge of its CE versions, and
A file whose path does not exist in CE but that a commit after the CE commits
renamed from a CE path is `ce-modified`. Renames come from Git's rename
detection in non-merge commits other than the relicensing commit. A chain of
renames stops where its path was added or deleted, so a file created at a path
that a CE file was once renamed to does not inherit that CE file. Git scores a
detection in non-merge commits other than the relicensing commit. A rename
counts only while its path's history is continuous: a commit that descends from
the rename and deletes or adds the renamed path breaks it, and so does a
deletion of the CE path before the rename. A file created at a path that a CE
file was once renamed to therefore does not inherit that CE file. Git scores a
rename with the header included, so the rename counts only when the content
without the header is at least 50% similar to the CE file; a weaker rename needs
a decision.

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

@ -887,21 +887,31 @@ def ce_renames(repo, head, bases, ce_paths):
# deleted CE files with similar PE files: neither is a rename.
raw = git(repo, "log", "--no-merges", "--topo-order", "--raw", "--no-abbrev", "-M", "-l0", "-z",
"--diff-filter=RAD", "--format=%x1e%H", head, "--not", *bases, "--")
names = {}
names, renames, chains, changes = {}, {}, defaultdict(list), defaultdict(list)
for change in parse_history(raw):
if change.commit == RELICENSING_COMMIT:
continue
if change.status[0] != "R":
names[change.path] = None
changes[change.path].append(change)
continue
name = names.pop(change.path, change.path)
if name is not None:
names[change.source] = name
renames = {}
for source, name in names.items():
if name is not None and source in ce_paths:
renames.setdefault(name, source)
return renames
names[change.source] = name
chains[name].append(change)
if change.source in ce_paths and name not in renames:
renames[name] = list(chains[name])
return {name: chain[-1].source for name, chain in renames.items() if continuous(repo, head, chain, changes)}
def continuous(repo, head, chain, changes):
later = head
for rename in chain:
if any(contains(repo, rename.commit, change.commit) and contains(repo, change.commit, later)
for change in changes[rename.path]):
return False
later = rename.commit
first = chain[-1]
return not any(change.status == "D" and contains(repo, change.commit, first.commit)
for change in changes[first.source])
def comparable_ce_files(comparison, names):

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

@ -555,6 +555,23 @@ class GitHistoryTests(unittest.TestCase):
git.commit("rename the reused path")
self.assertEqual({}, p.Lineage.load(git.root, git("rev-parse", "HEAD"), [base]).renames)
def test_lineage_keeps_a_rename_whose_path_changed_only_on_another_branch(self):
git = GitFixture(temporary_directory(self), "ce")
git.write("Theme.java", "".join(f"CE line {index}\n" for index in range(20)))
base = git.commit("CE base")
git("checkout", "-b", "downstream")
git("mv", "Theme.java", "Constants.java")
git.commit("rename")
git("checkout", "-b", "side", base)
git.write("Constants.java", "".join(f"unrelated line {index}\n" for index in range(20)))
git.commit("unrelated file")
git("rm", "-q", "Constants.java")
git.commit("revert")
git("checkout", "downstream")
git("merge", "--no-ff", "side", "-m", "integrate side")
self.assertEqual({"Constants.java": "Theme.java"},
p.Lineage.load(git.root, git("rev-parse", "HEAD"), [base]).renames)
@requires_merge_file_object_id
def test_merge_blobs_returns_only_clean_merges(self):
git = GitFixture(temporary_directory(self), "main")

Loading…
Cancel
Save