radn-dev/xmetrics/commits.py

42 lines
1.5 KiB
Python

import pickle
from subprocess import check_output
from common import counter
args = []
# args = ["--topo-order"]
original_commits = check_output(["git", "log", "--pretty=%H", *args], text=True).splitlines()
N = len(original_commits)
filtered_commits = set(original_commits[:: N // min(N, 4096)])
total_changes: dict[str, int] = {}
chosen_parents: dict[str, str | None] = {}
for i, commit in enumerate(reversed(original_commits)):
print(f"P={i / N:6f}", flush=True)
parents = check_output(["git", "log", "--pretty=%P", "-n", "1", commit], text=True).split()
chosen_parent = None
if commit in filtered_commits:
ctr = counter(commit, True)
changes = ctr.total()
for parent in parents:
pctr = counter(parent, True)
maybe_changes = (pctr - ctr).total() + (ctr - pctr).total() + total_changes[parent]
if maybe_changes > changes:
changes = maybe_changes
chosen_parent = parent
else:
changes = 0
for parent in parents:
maybe_changes = total_changes[parent]
if maybe_changes > changes:
changes = maybe_changes
chosen_parent = parent
total_changes[commit] = changes
chosen_parents[commit] = chosen_parent
commit = original_commits[0]
commits = []
while commit is not None:
commits.append(commit)
commit = chosen_parents[commit]
with open("/code/commits.dat", "wb") as file:
pickle.dump(commits, file)