#!/usr/bin/env python3
"""Emit a git fast-import stream: N commits over F files, B branches, T tags.
Deterministic (seeded) so every run builds the same history. Usage:
mem-bench-gen.py COMMITS FILES BRANCHES TAGS | git fast-import
"""
import random
import sys
commits, nfiles, nbranches, ntags = (int(a) for a in sys.argv[1:5])
rnd = random.Random(42)
dirs = ["src", "src/core", "src/net", "src/ui", "lib", "docs", "tests", "cmd", "internal/a", "internal/b"]
paths = ["%s/file%04d.%s" % (dirs[i % len(dirs)], i, ("go", "c", "md", "txt")[i % 4]) for i in range(nfiles)]
words = "alpha beta gamma delta index timeline search diff branch merge fix add remove refactor cache sort query".split()
out = sys.stdout.buffer
body = {}
t0 = 1_500_000_000
branches = ["main"] + ["feature-%03d" % i for i in range(1, nbranches)]
tips = {}
mark = 0
tagged = set(rnd.sample(range(1, commits + 1), min(ntags, commits)))
authors = ["Ada Lovelace <[email protected]>", "Alan Turing <[email protected]>", "Grace Hopper <[email protected]>", "Edsger Dijkstra <[email protected]>"]
def w(s):
out.write(s.encode())
for n in range(1, commits + 1):
br = "main" if rnd.random() < 0.7 else rnd.choice(branches)
mark += 1
msg = "%s %s %s (%d)\n\nbody line %d\n" % (rnd.choice(words), rnd.choice(words), rnd.choice(words), n, n)
a = rnd.choice(authors)
ts = t0 + n * 600
w("commit refs/heads/%s\nmark :%d\nauthor %s %d +0000\ncommitter %s %d +0000\n" % (br, mark, a, ts, a, ts))
w("data %d\n%s\n" % (len(msg), msg))
if br in tips:
w("from :%d\n" % tips[br])
elif br != "main" and "main" in tips:
w("from :%d\n" % tips["main"])
for p in rnd.sample(paths, min(3, len(paths))):
v = body.get(p, 0) + 1
body[p] = v
content = "".join("line %d of %s rev %d %s\n" % (i, p, v, rnd.choice(words)) for i in range(40 + (v % 20)))
w("M 100644 inline %s\ndata %d\n%s\n" % (p, len(content), content))
tips[br] = mark
if n in tagged:
w("reset refs/tags/v%d.%d\nfrom :%d\n\n" % (n // 1000, n % 1000, mark))