#!/usr/bin/env python3
"""Synthetic repo: 3,000 small changed files plus a few large generated schema files whose
declarations are reordered on the feature branch (the shape `--minimal` is slow on)."""
import os, random, subprocess, sys

root = sys.argv[1]
big_files = int(sys.argv[2]) if len(sys.argv) > 2 else 3
decls = int(sys.argv[3]) if len(sys.argv) > 3 else 4000
os.makedirs(root, exist_ok=True)
run = lambda *a: subprocess.run(["git", *a], cwd=root, check=True, stdout=subprocess.DEVNULL)
run("init", "-q", "-b", "main")
run("config", "user.email", "t@example.com"); run("config", "user.name", "t")

def schema(order):
    out = []
    for i in order:
        out.append(f"export const Type{i} = Schema.Struct({{")
        for f in range(6):
            out.append(f"  field{f}: Schema.String,")
        out.append("});")
        out.append("")
    return "\n".join(out) + "\n"

rng = random.Random(1)
for b in range(big_files):
    with open(f"{root}/schema{b}.gen.ts", "w") as fh: fh.write(schema(range(decls)))
os.makedirs(f"{root}/src", exist_ok=True)
for i in range(3000):
    with open(f"{root}/src/m{i}.ts", "w") as fh: fh.write("".join(f"export const v{j} = {j};\n" for j in range(40)))
run("add", "-A"); run("commit", "-qm", "base")
run("checkout", "-q", "-b", "feature")
for b in range(big_files):
    order = list(range(decls)); rng.shuffle(order)
    with open(f"{root}/schema{b}.gen.ts", "w") as fh: fh.write(schema(order))
for i in range(3000):
    with open(f"{root}/src/m{i}.ts", "a") as fh: fh.write(f"export const added = {i};\n")
run("add", "-A"); run("commit", "-qm", "feature")
