#!/bin/bash # Oracle: the system git's upload-pack, the advertisement it sends a client # (gitprotocol-pack(5), "Reference Discovery"), for a bare repository whose # refs are made in an order other than their names', some packed and some # loose, git talking through $2, ssh_relay.exe. Output goes to $1 (default # ../traces): # # - layout: each ref in the order it was made, with how it is stored, # "packed REF" or "loose REF"; HEAD names refs/heads/main. # - upload-pack.pkt: the bytes of `git upload-pack --advertise-refs`. # - push-ssh.*, delete-ssh.*: git push over ssh of a branch, then of its # deletion alone, through ssh_relay.exe, a stand-in for ssh that runs # git-receive-pack here and records what each side sent (.client, # .server) and how many of the server's bytes git had been sent when it # ended its standard input, as ssh ends the channel then (.eof). # - fetch-ssh.*: git fetch over ssh of that branch into an empty # repository, in protocol v0, recorded the same way through # git-upload-pack. # - git-version: the git that wrote them. # # Every identity and date is fixed, so the object names are the same on # every run. set -euo pipefail command -v git >/dev/null || { echo "git not on PATH" >&2; exit 1; } here="$(dirname "$0")" out="${1:-$here/../traces}" work="$(mktemp -d "${TMPDIR:-/tmp}/advertise.XXXXXX")" trap 'rm -rf "$work"' EXIT export HOME="$work" GIT_CONFIG_GLOBAL=/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 export GIT_AUTHOR_DATE='@1700000000 +0000' GIT_COMMITTER_DATE='@1700000000 +0000' export TZ=UTC LC_ALL=C repo="$work/repo.git" git init -q --bare --initial-branch=main "$repo" tree=$(git -C "$repo" mktree < /dev/null) commit=$(git -C "$repo" commit-tree -m advertise "$tree") # Packed: names that sort after loose ones, and a directory beside names # that share its prefix. packed="refs/tags/v1.0 refs/heads/zeta refs/heads/a/b" # Loose, made after the pack: "B" sorts before "a", and "-" before "/" # before "0", in byte order. loose="refs/heads/main refs/heads/a0 refs/heads/a-b refs/heads/B" : > "$out/layout" for r in $packed; do git -C "$repo" update-ref "$r" "$commit" echo "packed $r" >> "$out/layout" done git -C "$repo" pack-refs --all for r in $loose; do git -C "$repo" update-ref "$r" "$commit" echo "loose $r" >> "$out/layout" done git upload-pack --advertise-refs "$repo" > "$out/upload-pack.pkt" # Absolute, since git runs the stand-in from its working tree. relay="${2:?the stand-in for ssh, ssh_relay.exe, is the second argument}" ssh="$(cd "$(dirname "$relay")" && pwd)/$(basename "$relay")" records="$(cd "$out" && pwd)" git init -q --bare --initial-branch=main "$work/push.git" git init -q --initial-branch=main "$work/w" printf 'pushed\n' > "$work/w/F" git -C "$work/w" add F git -C "$work/w" commit -q -m push for push in "push-ssh main:refs/heads/topic" "delete-ssh :refs/heads/topic"; do set -- $push RECORD="$records/$1" GIT_SSH_COMMAND="$ssh" \ git -C "$work/w" push -q "ssh://host$work/push.git" "$2" done git init -q --bare "$work/fetch.git" RECORD="$records/fetch-ssh" GIT_SSH_COMMAND="$ssh" \ git -C "$work/fetch.git" -c protocol.version=0 fetch -q \ "ssh://host$work/w" main git --version > "$out/git-version"