import json import os from collections import Counter from pathlib import Path from subprocess import check_output commits = check_output(["git", "log", "--pretty=%H", "--topo-order"], text=True).splitlines() entries = [] last_ctr = Counter() last_cor = [] cors = [] for i, commit in enumerate(reversed(commits)): print("running", commit) check_output(["git", "checkout", commit], text=True) current_ctr = Counter() for path in Path(os.getenv("SRCDIR", "src")).rglob(os.getenv("SRCPATTERN", "*.rs")): lines = path.read_bytes().splitlines() lines = (line.strip() for line in lines) lines = (line for line in lines if line) current_ctr.update(lines) added = current_ctr - last_ctr deleted = last_ctr - current_ctr entries.append((i, current_ctr.total(), added.total(), deleted.total())) current_cor = [] common_ctr = current_ctr & last_ctr for j, line in reversed(last_cor): if common_ctr[line]: common_ctr[line] -= 1 current_cor.append((j, line)) current_cor.reverse() for line in added.elements(): current_cor.append((i, line)) cor_ctr = Counter(j for j, _ in current_cor) assert len(current_cor) == cor_ctr.total() == current_ctr.total() cors.append([0] * i) for j, cor in enumerate(cors): cor.append(cor_ctr[j]) last_ctr = current_ctr last_cor = current_cor print(sum(sum(cor) for cor in cors)) with open("/code/metrics.json", "w") as file: json.dump({"entries": entries, "cors": cors}, file)