#!/bin/bash # Oracle: the system git. Builds one fixture history in three stages, packs # each stage, and has git write the commit-graph (gitformat-commit-graph(5)) # and the multi-pack-index (gitformat-pack(5)) over it in every shape the # reader must take: one file, a chain, generation data v1 only, two chains # mixing v1 and v2 layers (either one below), and a graph that misses the last # stage. The answers the test compares against come from git with the graph # turned off. # # Every identity and date is fixed, so the commit, pack and graph names are the # same on every run. Output goes to $1 (default ../traces). set -euo pipefail command -v git >/dev/null || { echo "git not on PATH" >&2; exit 1; } out="$(cd "${1:-$(dirname "$0")/../traces}" && pwd)" work="$(mktemp -d "${TMPDIR:-/tmp}/commit-graph.XXXXXX")" trap 'rm -rf "$work"' EXIT export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=/dev/null GIT_CONFIG_NOSYSTEM=1 export GIT_AUTHOR_NAME=interop GIT_AUTHOR_EMAIL=interop@nox export GIT_COMMITTER_NAME=interop GIT_COMMITTER_EMAIL=interop@nox unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_OBJECT_DIRECTORY GIT_COMMON_DIR \ GIT_ALTERNATE_OBJECT_DIRECTORIES repo="$work/repo" g() { git -C "$repo" -c core.commitGraph=false -c gc.auto=0 -c maintenance.auto=false \ -c fetch.writeCommitGraph=false -c pack.writeReverseIndex=false "$@"; } at() { export GIT_AUTHOR_DATE="@$1 +0000" GIT_COMMITTER_DATE="@$1 +0000"; } # [c name date]: a commit on the current branch adding the file [name]. c() { at "$2"; printf '%s\n' "$1" > "$repo/$1"; g add "$1"; g commit -q -m "$1"; g tag "$1"; } # [m name date branch...]: a merge of the branches into the current one. m() { local n="$1" d="$2"; shift 2; at "$d"; g merge -q --no-ff --allow-unrelated-histories -m "$n" "$@" >/dev/null; g tag "$n"; } # [snap stage]: the branch tips as the stage leaves them. snap() { g for-each-ref --format='%(objectname)' refs/heads > "$work/$1"; } co() { g checkout -q "$@"; } git init -q -b main "$repo" t=1700000000 # Stage 1: a line, two branches off it, and a disconnected root. c m1 $t; c m2 $((t + 10)); c m3 $((t + 20)) co -b a m2; c a1 $((t + 30)); c a2 $((t + 40)) co -b b m2; c b1 $((t + 50)) co -b c m3; c c1 $((t + 60)) co --orphan o; g rm -rfq . ; c o1 $((t + 70)); c o2 $((t + 80)) co main; c m4 $((t + 90)) snap s1 g repack -q -d pack1=$(ls "$repo/.git/objects/pack" | grep '\.pack$') # Stage 2: an octopus merge (four parents, the Extra Edge List), a criss-cross # (two merge bases), and a committer date older than its parent's. m m5 $((t + 100)) a b c co -b x m5; c x1 $((t + 110)) co -b y m5; c y1 $((t + 120)) co x; m x2 $((t + 130)) y1 co y; m y2 $((t + 140)) x1 co main; c skew $((t - 100000)); c m6 $((t + 150)) snap s2 g repack -q -d pack2=$(ls "$repo/.git/objects/pack" | grep '\.pack$' | grep -v "$pack1") # Stage 3: a child dated at 1000 under a parent dated at 4e9, whose corrected # date offset overflows 31 bits (GDO2), and a commit dated past 2^32 (the # 34-bit commit time) on a branch nothing merges; the disconnected root joins # main. git 2.54.0 writes a wrong offset for a commit whose corrected date is # past 2^32 and whose own date is not, which its own [commit-graph verify] # refuses, so no such commit is made. co -b f main; c f1 4000000000; c f2 1000 co -b h main; c h1 5000000000 co main; c m7 $((t + 160)); m m8 $((t + 170)) o; m m9 $((t + 180)) f x2 y2 # Two merges of r3 and its ancestor r1, under which r2 and r3 are dated # before r1: walking by date, both sides reach r1 before r3 paints it stale, # so merge-base --all names r3 only because a candidate another candidate # reaches is removed (commit-reach.c, remove_redundant). co -b r main; c r1 $((t + 200)); c r2 $((t + 5)); c r3 $((t + 6)) q() { at "$2"; g tag "$1" "$(g commit-tree -p r3 -p r1 -m "$1" "r3^{tree}")"; } q q1 $((t + 300)); q q2 $((t + 301)) g repack -q -d packs="$repo/.git/objects/pack" info="$repo/.git/objects/info" rm -rf "$out/repo" "$out/graphs" mkdir -p "$out/repo" "$out/graphs" cp "$packs"/pack-*.pack "$packs"/pack-*.idx "$out/repo/" # The multi-pack-index covers the first two packs only, so the third is one a # reader must still search on its own. printf '%s\n' "${pack1%.pack}.idx" "${pack2%.pack}.idx" | sort > "$out/midx-packs" git -C "$repo" multi-pack-index write --stdin-packs < "$out/midx-packs" cp "$packs/multi-pack-index" "$out/repo/" rm -f "$packs/multi-pack-index" # [graph variant]: move what git wrote into objects/info to graphs/. graph() { mkdir -p "$out/graphs/$1" for f in "$info"/commit-graph "$info"/commit-graphs; do if [ -e "$f" ]; then cp -R "$f" "$out/graphs/$1/"; rm -rf "$f"; fi done } stage() { cat "$work/$1"; } git -C "$repo" commit-graph write --reachable graph single git -C "$repo" -c commitGraph.generationVersion=1 commit-graph write --reachable graph v1 stage s2 | git -C "$repo" commit-graph write --stdin-commits graph stale stage s1 | git -C "$repo" commit-graph write --stdin-commits --split=no-merge stage s2 | git -C "$repo" commit-graph write --stdin-commits --split=no-merge git -C "$repo" commit-graph write --reachable --split=no-merge graph chain stage s1 | git -C "$repo" -c commitGraph.generationVersion=1 \ commit-graph write --stdin-commits --split=no-merge git -C "$repo" commit-graph write --reachable --split=no-merge graph mixed stage s1 | git -C "$repo" commit-graph write --stdin-commits --split=no-merge git -C "$repo" -c commitGraph.generationVersion=1 \ commit-graph write --reachable --split=no-merge graph v1-top # The answers, from git with the graph off. names=$(g log --all --format=%s | sort) { echo "name,hash,tree,parents,date" g log --all --format='%s,%H,%T,%P,%ct' | sort } > "$out/commits.csv" { echo "stage,name" for s in s1 s2; do g log --format="$s,%s" $(stage "$s") | sort; done } > "$out/stages.csv" subjects() { while read -r h; do awk -F, -v h="$h" '$2 == h { print $1 }' "$out/commits.csv"; done } hash() { awk -F, -v n="$1" '$1 == n { print $2 }' "$out/commits.csv"; } { echo "ancestor,descendant,expected" for a in $names; do for d in $names; do if g merge-base --is-ancestor "$(hash "$a")" "$(hash "$d")"; then r=true; else r=false; fi echo "$a,$d,$r" done; done } > "$out/ancestry.csv" { echo "left,right,best,all" for l in $names; do for r in $names; do best=$({ g merge-base "$(hash "$l")" "$(hash "$r")" || true; } | subjects) all=$({ g merge-base --all "$(hash "$l")" "$(hash "$r")" || true; } | subjects | sort | tr '\n' ' ') echo "$l,$r,$best,${all% }" done; done } > "$out/merge_bases.csv" { echo "base,head,commits" for b in $names; do for h in $names; do range=$(g rev-list "$(hash "$b")..$(hash "$h")" | subjects | tr '\n' ' ') echo "$b,$h,${range% }" done; done } > "$out/ranges.csv" { echo "hash,pack,offset" for idx in "$packs"/pack-*.idx; do git show-index < "$idx" | awk -v p="$(basename "$idx")" '{print $2 "," p "," $1}' done | sort } > "$out/objects.csv" git --version > "$out/git-version"