Something went wrong. Try again.
Git format in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199#!/usr/bin/env python3"""Oracle: the system git's line counts (diff.c, builtin_diffstat) and renamedetection (diffcore-rename.c), read off git diff-tree -r -M --numstat -z.
A history is built below, one commit per rule: lines edited, added andremoved, a file deleted, exact and inexact renames, a rename below git's 50 percent, a rename found by its basename, binary files edited and renamed, emptyfiles renamed (git diff's --rename-empty), a mode change alone, a symbolic linkturned into a file, a file turned into a directory, a last line losing itsnewline, CR LF lines, a submodule, and a merge, whose counts are against itsfirst parent. For each commit git diff-tree compares it with its first parent,or with the empty tree for the root.
history.pack every object of the history history.idx its index numstat for each commit, oldest first, a line "<commit>\\t<name>\\t<bytes>\\n" then that many bytes of git diff-tree -r -M --numstat -z git-version the git that counted
Output goes to argv[1]."""import osimport subprocessimport sysimport tempfile
ENV = dict(os.environ, GIT_AUTHOR_NAME="A U Thor", GIT_AUTHOR_EMAIL="author@example.com", GIT_COMMITTER_NAME="C O Mitter", GIT_COMMITTER_EMAIL="committer@example.com")
def run(args, cwd=None, input=None, date=None): env = ENV if date is not None: stamp = "%d +0000" % (1700000000 + date * 60) env = dict(ENV, GIT_AUTHOR_DATE=stamp, GIT_COMMITTER_DATE=stamp) r = subprocess.run(args, cwd=cwd, input=input, capture_output=True, env=env) if r.returncode != 0: sys.exit("%s: exit %d\n%s" % (" ".join(args), r.returncode, r.stderr.decode())) return r.stdout
def lines(word, n, start=1): return "".join("%s %d\n" % (word, i) for i in range(start, start + n)).encode()
def replace(text, k, step=1): """[text] with every [step]th of its first lines rewritten, [k] of them.""" ls = text.splitlines(keepends=True) out, done = [], 0 for i, l in enumerate(ls): if done < k and i % step == 0: out.append(b"changed " + l) done += 1 else: out.append(l) return b"".join(out)
A = lines("alpha", 40)C = lines("gamma", 20)UTIL = lines("let util", 30)BIN = bytes(range(256)) * 3CRLF = lines("crlf", 12).replace(b"\n", b"\r\n")
def main(out): with tempfile.TemporaryDirectory(prefix="numstat-interop.") as work: repo = os.path.join(work, "r") run(["git", "init", "-q", "-b", "main", repo]) g = lambda *a, input=None, date=None: run(["git"] + list(a), cwd=repo, input=input, date=date)
def write(path, data, exe=False): full = os.path.join(repo, path) os.makedirs(os.path.dirname(full) or repo, exist_ok=True) with open(full, "wb") as f: f.write(data) os.chmod(full, 0o755 if exe else 0o644) g("add", path)
def remove(path): g("rm", "-q", path)
def move(src, dst, data=None): os.makedirs(os.path.dirname(os.path.join(repo, dst)) or repo, exist_ok=True) g("mv", src, dst) if data is not None: write(dst, data)
commits = []
def commit(name): g("commit", "-q", "--allow-empty", "-m", name, date=len(commits)) commits.append((g("rev-parse", "HEAD").decode().strip(), name))
write("a.txt", A) write("b.txt", lines("beta", 10)) write("bin.dat", BIN) write("empty1", b"") write("dir/c.txt", C) write("x", b"a file that becomes a directory\n") write("script.sh", b"#!/bin/sh\necho hello\n") write("old/util.ml", UTIL) write("old/other.ml", lines("let other", 30)) write("crlf.txt", CRLF) os.symlink("a.txt", os.path.join(repo, "link")) g("add", "link") commit("root")
write("a.txt", replace(A, 3, step=7) + lines("alpha", 2, start=41)) remove("b.txt") commit("lines edited and a file deleted")
move("dir/c.txt", "dir2/c.txt") commit("exact rename")
a = open(os.path.join(repo, "a.txt"), "rb").read() move("a.txt", "src/main.txt", replace(a, 10, step=4)) commit("inexact rename")
move("dir2/c.txt", "other/d.txt", replace(C, 15)) commit("rename below the threshold")
move("old/util.ml", "new/util.ml", replace(UTIL, 5, step=6)) remove("old/other.ml") write("new/other.ml", replace(lines("let other", 30), 25)) commit("renames by basename")
write("bin.dat", BIN[:500] + b"\0\1\2" * 30) commit("binary edited")
move("bin.dat", "assets/bin.dat") commit("binary renamed")
move("empty1", "empty2") commit("empty file renamed")
os.chmod(os.path.join(repo, "script.sh"), 0o755) g("add", "script.sh") commit("mode change")
os.remove(os.path.join(repo, "link")) write("link", b"no longer a link\n") commit("link turned into a file")
remove("x") write("x/y", b"now a directory\n") commit("file turned into a directory")
write("src/main.txt", open(os.path.join(repo, "src/main.txt"), "rb").read()[:-1]) commit("last newline removed")
write("crlf.txt", replace(CRLF, 4, step=3)) commit("CR LF lines edited")
g("update-index", "--add", "--cacheinfo", "160000,%s,sub" % commits[0][0]) commit("submodule added")
g("checkout", "-q", "-b", "side") write("side.txt", lines("side", 5)) write("crlf.txt", CRLF) commit("side branch") g("checkout", "-q", "main") write("main.txt", lines("main", 3)) commit("main branch") g("merge", "-q", "--no-ff", "-m", "merge", "side", date=len(commits)) commits.append((g("rev-parse", "HEAD").decode().strip(), "merge"))
with open(os.path.join(out, "numstat"), "wb") as f: for c, name in commits: parents = g("rev-list", "--parents", "-n", "1", c).decode().split()[1:] if parents: stat = g("diff-tree", "-r", "-M", "--numstat", "-z", parents[0], c) else: stat = g("diff-tree", "-r", "-M", "--numstat", "-z", "--root", "--no-commit-id", c) f.write(("%s\t%s\t%d\n" % (c, name, len(stat))).encode()) f.write(stat)
objects = g("rev-list", "--objects", "--all").decode() pack = g("pack-objects", "--stdout", "--threads=1", input=("\n".join(l.split()[0] for l in objects.splitlines()) + "\n") .encode()) with open(os.path.join(out, "history.pack"), "wb") as f: f.write(pack) run(["git", "index-pack", "--no-rev-index", "-o", os.path.join(out, "history.idx"), os.path.join(out, "history.pack")]) with open(os.path.join(out, "git-version"), "wb") as f: f.write(run(["git", "--version"]))
if __name__ == "__main__": main(os.path.abspath(sys.argv[1] if len(sys.argv) > 1 else os.path.join(os.path.dirname(__file__), "..", "traces")))