|
| 1 | +#!/usr/bin/env bash |
| 2 | +# extract-history.sh: filter the Python SDK history out of a NVIDIA/OpenShell |
| 3 | +# clone at the freeze commit. |
| 4 | +# |
| 5 | +# Usage: extract-history.sh <core-clone> <freeze-sha> <out-dir> |
| 6 | +# |
| 7 | +# <out-dir> must not exist. Afterwards it is a standalone repository with one |
| 8 | +# branch, main, whose tip tree is the paths in paths.txt at <freeze-sha>, |
| 9 | +# mapped to this repository's layout. Every commit that touched those paths |
| 10 | +# keeps its author, date, and trailers. Bare #N references in commit messages |
| 11 | +# become NVIDIA/OpenShell#N (see message-rules.txt). |
| 12 | +# The last output line is the resulting HEAD. |
| 13 | +set -euo pipefail |
| 14 | + |
| 15 | +if [ $# -ne 3 ]; then |
| 16 | + echo "usage: $0 <core-clone> <freeze-sha> <out-dir>" >&2 |
| 17 | + exit 2 |
| 18 | +fi |
| 19 | + |
| 20 | +core=$1 |
| 21 | +freeze=$2 |
| 22 | +out=$3 |
| 23 | +here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 24 | +rules="$here/message-rules.txt" |
| 25 | +paths="$here/paths.txt" |
| 26 | + |
| 27 | +command -v git-filter-repo >/dev/null 2>&1 || { |
| 28 | + echo "ERROR: git-filter-repo is not installed" >&2 |
| 29 | + exit 1 |
| 30 | +} |
| 31 | +for f in "$rules" "$paths"; do |
| 32 | + [ -f "$f" ] || { |
| 33 | + echo "ERROR: $f is missing" >&2 |
| 34 | + exit 1 |
| 35 | + } |
| 36 | +done |
| 37 | +[ ! -e "$out" ] || { |
| 38 | + echo "ERROR: $out exists; pass a path that does not exist yet" >&2 |
| 39 | + exit 1 |
| 40 | +} |
| 41 | +git -C "$core" cat-file -e "${freeze}^{commit}" 2>/dev/null || { |
| 42 | + echo "ERROR: $freeze is not a commit in $core" >&2 |
| 43 | + exit 1 |
| 44 | +} |
| 45 | +freeze=$(git -C "$core" rev-parse "${freeze}^{commit}") |
| 46 | + |
| 47 | +# A real copy (no hardlinks, no tags, no checkout) that filter-repo may rewrite. |
| 48 | +git clone --quiet --no-local --no-checkout --no-tags "$core" "$out" |
| 49 | +# A freeze commit that only a remote-tracking ref reaches is not cloned. |
| 50 | +git -C "$out" cat-file -e "${freeze}^{commit}" 2>/dev/null || |
| 51 | + git -C "$out" fetch --quiet --no-tags "$core" "$freeze" || { |
| 52 | + echo "ERROR: $freeze is not reachable from a branch of $core" >&2 |
| 53 | + exit 1 |
| 54 | + } |
| 55 | +git -C "$out" remote remove origin |
| 56 | + |
| 57 | +# Keep exactly one branch, main, at the freeze commit. |
| 58 | +git -C "$out" branch --force main "$freeze" |
| 59 | +git -C "$out" symbolic-ref HEAD refs/heads/main |
| 60 | +git -C "$out" for-each-ref --format='%(refname)' refs/heads | |
| 61 | + grep -v '^refs/heads/main$' | |
| 62 | + while read -r ref; do git -C "$out" update-ref -d "$ref"; done |
| 63 | +git -C "$out" checkout --quiet main |
| 64 | + |
| 65 | +# --force: <out-dir> is a throwaway copy without an origin, so filter-repo's |
| 66 | +# fresh-clone check does not apply. |
| 67 | +# The global and system config are hidden because filter-repo parses |
| 68 | +# `git config --list` and chokes on keys without a value. |
| 69 | +GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=/dev/null \ |
| 70 | + git -C "$out" filter-repo --force --quiet \ |
| 71 | + --paths-from-file "$paths" \ |
| 72 | + --replace-message "$rules" |
| 73 | + |
| 74 | +echo "commits: $(git -C "$out" rev-list --count HEAD)" |
| 75 | +echo "authors: $(git -C "$out" log --format='%an <%ae>' | sort -u | wc -l | tr -d ' ')" |
| 76 | +echo "tree: $(git -C "$out" rev-parse 'HEAD^{tree}')" |
| 77 | +git -C "$out" rev-parse HEAD |
0 commit comments