#!/usr/bin/env python3
# Builds a repo whose checked-out branch has drifted far from a stale local `main`:
# no remote, no gh-merge-base, ~55k files different from the merge base.
import os, subprocess, sys, random

root = sys.argv[1]
n_base = int(sys.argv[2]) if len(sys.argv) > 2 else 30000
n_new = int(sys.argv[3]) if len(sys.argv) > 3 else 25000
lines = int(sys.argv[4]) if len(sys.argv) > 4 else 200

def git(*a):
    subprocess.run(["git", "-C", root, *a], check=True, stdout=subprocess.DEVNULL)

def write(path, seed):
    rnd = random.Random(seed)
    os.makedirs(os.path.dirname(path), exist_ok=True)
    with open(path, "w") as f:
        for i in range(lines):
            f.write(f"line {i} {rnd.getrandbits(64):x} {rnd.getrandbits(64):x}\n")

os.makedirs(root, exist_ok=True)
git("init", "-q", "-b", "main")
git("config", "user.email", "t@example.com"); git("config", "user.name", "t")
for i in range(n_base):
    write(f"{root}/src/d{i % 300}/f{i}.txt", i)
git("add", "-A"); git("commit", "-qm", "base")
git("checkout", "-qb", "feature")
for i in range(n_base):
    write(f"{root}/src/d{i % 300}/f{i}.txt", 10**7 + i)
for i in range(n_new):
    write(f"{root}/new/d{i % 300}/n{i}.txt", 2 * 10**7 + i)
git("add", "-A"); git("commit", "-qm", "feature work")
git("checkout", "-q", "main")
with open(f"{root}/main-only.txt", "w") as f: f.write("stale main\n")
git("add", "-A"); git("commit", "-qm", "stale main commit")
git("checkout", "-q", "feature")
print("done")
