omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tools / pinned_checkout.sh
| 1 | #!/usr/bin/env bash |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 4 | # |
| 5 | # Materialize or verify an exact, read-only checkout from a full commit id. |
| 6 | # Replace refs and grafts are disabled, object identities are re-hashed, and the |
| 7 | # worktree is compared as raw bytes and modes without checkout filters. |
| 8 | set -euo pipefail |
| 9 | unset GIT_CONFIG GIT_CONFIG_COUNT GIT_CONFIG_PARAMETERS GIT_DIR GIT_WORK_TREE GIT_COMMON_DIR |
| 10 | unset GIT_INDEX_FILE GIT_OBJECT_DIRECTORY GIT_ALTERNATE_OBJECT_DIRECTORIES GIT_NAMESPACE |
| 11 | unset GIT_TEMPLATE_DIR GIT_SSH_COMMAND GIT_EXTERNAL_DIFF |
| 12 | export GIT_NO_REPLACE_OBJECTS=1 |
| 13 | export GIT_GRAFT_FILE=/nonexistent/omega-pinned-checkout-grafts |
| 14 | export GIT_CONFIG_NOSYSTEM=1 GIT_CONFIG_GLOBAL=/dev/null GIT_ATTR_NOSYSTEM=1 |
| 15 | export LC_ALL=C |
| 16 | |
| 17 | # Command-line config beats the repository's. The source checkout is the thing |
| 18 | # this tool refuses to trust: its hooks, fsmonitor, commit-graph and upload-pack |
| 19 | # hook must not run, and a commit-graph must not choose a different tree than |
| 20 | # the one stored in the commit object. |
| 21 | gitc() { |
| 22 | command git \ |
| 23 | -c core.commitGraph=false \ |
| 24 | -c core.multiPackIndex=false \ |
| 25 | -c core.hooksPath=/dev/null \ |
| 26 | -c core.fsmonitor= \ |
| 27 | -c core.fsmonitorHookVersion= \ |
| 28 | -c uploadpack.packObjectsHook= \ |
| 29 | -c protocol.file.allow=always \ |
| 30 | "$@" |
| 31 | } |
| 32 | |
| 33 | PROG=${0##*/} |
| 34 | tmpd="" |
| 35 | created="" |
| 36 | empty="" |
| 37 | |
| 38 | die() { echo "$PROG: REFUSED: $*" >&2; exit 1; } |
| 39 | |
| 40 | cleanup() { |
| 41 | local rc=$? |
| 42 | if [ -n "$tmpd" ] && [ -d "$tmpd" ]; then rm -rf -- "$tmpd"; fi |
| 43 | if [ "$rc" -ne 0 ] && [ -n "$created" ] && [ -d "$created" ]; then |
| 44 | chmod -R u+w -- "$created" 2>/dev/null || true |
| 45 | rm -rf -- "$created" |
| 46 | fi |
| 47 | if [ -n "$empty" ] && [ -d "$empty" ]; then rmdir -- "$empty" 2>/dev/null || true; fi |
| 48 | } |
| 49 | trap cleanup EXIT |
| 50 | |
| 51 | full_commit() { |
| 52 | [[ "$1" =~ ^[0-9a-f]{40}$ ]] || die "commit must be one full lowercase 40-hex id: '$1'" |
| 53 | } |
| 54 | |
| 55 | rehash() { |
| 56 | local repo=$1 type=$2 id=$3 got |
| 57 | got=$(gitc -C "$repo" cat-file "$type" "$id" 2>/dev/null | |
| 58 | gitc hash-object -t "$type" --stdin 2>/dev/null) || |
| 59 | die "cannot read $type object $id from $repo" |
| 60 | [ "$got" = "$id" ] || die "$type object $id fails its content hash in $repo" |
| 61 | } |
| 62 | |
| 63 | # The root tree is the first line of the commit object, not a peeled rev-parse, |
| 64 | # so a rewritten commit-graph cannot point this check at a different tree. |
| 65 | commit_tree() { |
| 66 | local repo=$1 commit=$2 line |
| 67 | line=$(gitc -C "$repo" cat-file commit "$commit" | awk 'NR==1 { print; exit }') || |
| 68 | die "cannot read commit $commit" |
| 69 | [[ "$line" =~ ^tree\ [0-9a-f]{40}$ ]] || die "commit $commit has no literal root tree" |
| 70 | printf '%s\n' "${line#tree }" |
| 71 | } |
| 72 | |
| 73 | # verify_checkout <directory> <commit>: print the verified root tree id. |
| 74 | verify_checkout() { |
| 75 | local dir=$1 commit=$2 top actual tree ent meta path mode type id file got gitlink_entries |
| 76 | local bad="" |
| 77 | full_commit "$commit" |
| 78 | [ -d "$dir" ] || die "$dir is not a directory" |
| 79 | dir=$(cd "$dir" && pwd -P) || die "cannot resolve $dir" |
| 80 | top=$(gitc -C "$dir" rev-parse --show-toplevel 2>/dev/null) || die "$dir is not a Git worktree" |
| 81 | top=$(cd "$top" && pwd -P) || die "cannot resolve worktree root $top" |
| 82 | [ "$top" = "$dir" ] || die "$dir is not the root of its Git worktree ($top)" |
| 83 | |
| 84 | actual=$(gitc -C "$dir" rev-parse --verify -q "$commit^{commit}" 2>/dev/null) || |
| 85 | die "$dir does not hold commit $commit" |
| 86 | [ "$actual" = "$commit" ] || die "$commit resolves to unexpected commit $actual in $dir" |
| 87 | actual=$(gitc -C "$dir" rev-parse --verify -q HEAD 2>/dev/null) || die "cannot read HEAD in $dir" |
| 88 | [ "$actual" = "$commit" ] || die "$dir is at $actual, not locked commit $commit" |
| 89 | |
| 90 | rehash "$dir" commit "$commit" |
| 91 | tree=$(commit_tree "$dir" "$commit") |
| 92 | rehash "$dir" tree "$tree" |
| 93 | |
| 94 | tmpd=$(mktemp -d "${TMPDIR:-/tmp}/omega-pinned-checkout.XXXXXX") || die "mktemp failed" |
| 95 | : > "$tmpd/want0" |
| 96 | : > "$tmpd/regular" |
| 97 | : > "$tmpd/regular.ids" |
| 98 | |
| 99 | # Re-hash every nested tree. Blob ids are proved below from the exact bytes |
| 100 | # in the worktree, binding what consumers read to the locked tree. |
| 101 | gitc -C "$dir" ls-tree -r -t -z --full-tree "$tree" > "$tmpd/objects" || |
| 102 | die "cannot enumerate objects at $commit" |
| 103 | while IFS= read -r -d '' ent; do |
| 104 | meta=${ent%%$'\t'*} |
| 105 | path=${ent#*$'\t'} |
| 106 | read -r mode type id <<< "$meta" |
| 107 | case "$path" in *$'\n'*) die "path with a newline at $commit is unsupported" ;; esac |
| 108 | case "$type" in |
| 109 | tree) rehash "$dir" tree "$id" ;; |
| 110 | blob) ;; |
| 111 | commit) [ "$mode" = 160000 ] || die "$path has unexpected commit mode $mode" ;; |
| 112 | *) die "$path has unsupported object type $type" ;; |
| 113 | esac |
| 114 | done < "$tmpd/objects" |
| 115 | |
| 116 | gitc -C "$dir" ls-tree -r -z --full-tree "$tree" > "$tmpd/entries" || |
| 117 | die "cannot enumerate paths at $commit" |
| 118 | while IFS= read -r -d '' ent; do |
| 119 | meta=${ent%%$'\t'*} |
| 120 | path=${ent#*$'\t'} |
| 121 | read -r mode type id <<< "$meta" |
| 122 | case "$path" in *$'\n'*) die "path with a newline at $commit is unsupported" ;; esac |
| 123 | file=$dir/$path |
| 124 | case "$mode:$type" in |
| 125 | 100644:blob|100755:blob) |
| 126 | [ -f "$file" ] && [ ! -L "$file" ] || { bad+="$path(type) "; continue; } |
| 127 | if [ "$mode" = 100755 ]; then |
| 128 | [ -x "$file" ] || { bad+="$path(mode) "; continue; } |
| 129 | else |
| 130 | [ ! -x "$file" ] || { bad+="$path(mode) "; continue; } |
| 131 | fi |
| 132 | printf '%s\n' "$file" >> "$tmpd/regular" |
| 133 | printf '%s\n' "$id" >> "$tmpd/regular.ids" |
| 134 | printf '%s\0' "$path" >> "$tmpd/want0" ;; |
| 135 | 120000:blob) |
| 136 | if [ -L "$file" ]; then |
| 137 | # readlink -z preserves embedded/trailing newlines; remove only its NUL delimiter. |
| 138 | got=$(readlink -z -- "$file" | head -c -1 | gitc hash-object --stdin 2>/dev/null) || got="" |
| 139 | [ "$got" = "$id" ] || bad+="$path(symlink) " |
| 140 | else |
| 141 | bad+="$path(type) " |
| 142 | fi |
| 143 | printf '%s\0' "$path" >> "$tmpd/want0" ;; |
| 144 | 160000:commit) |
| 145 | if [ -e "$file" ] || [ -L "$file" ]; then |
| 146 | if [ -d "$file" ] && [ ! -L "$file" ]; then |
| 147 | gitlink_entries=$(find "$file" -mindepth 1 -print -quit 2>/dev/null) || |
| 148 | { bad+="$path(unreadable-gitlink) "; continue; } |
| 149 | [ -z "$gitlink_entries" ] || bad+="$path(gitlink) " |
| 150 | else |
| 151 | bad+="$path(gitlink) " |
| 152 | fi |
| 153 | fi ;; |
| 154 | *) die "$path has unsupported mode and type $mode $type at $commit" ;; |
| 155 | esac |
| 156 | done < "$tmpd/entries" |
| 157 | |
| 158 | if [ -s "$tmpd/regular" ]; then |
| 159 | gitc hash-object --no-filters --stdin-paths < "$tmpd/regular" > "$tmpd/regular.got" 2>/dev/null || |
| 160 | die "cannot hash regular files under $dir" |
| 161 | if ! cmp -s "$tmpd/regular.ids" "$tmpd/regular.got"; then |
| 162 | bad+="regular-file-content " |
| 163 | fi |
| 164 | fi |
| 165 | |
| 166 | (cd "$dir" && find . -path './.git' -prune -o ! -type d -printf '%P\0') > "$tmpd/have0" || |
| 167 | die "cannot enumerate every worktree path under $dir" |
| 168 | sort -z -o "$tmpd/want0" "$tmpd/want0" |
| 169 | sort -z -o "$tmpd/have0" "$tmpd/have0" |
| 170 | if ! cmp -s "$tmpd/want0" "$tmpd/have0"; then bad+="missing-or-extra-path "; fi |
| 171 | [ -z "$bad" ] || die "$dir differs from $commit: ${bad:0:300}" |
| 172 | |
| 173 | rm -rf -- "$tmpd" |
| 174 | tmpd="" |
| 175 | printf '%s\n' "$tree" |
| 176 | } |
| 177 | |
| 178 | cmd=${1:-} |
| 179 | case "$cmd" in |
| 180 | verify) |
| 181 | [ $# = 3 ] || die "usage: $PROG verify CHECKOUT COMMIT" |
| 182 | verify_checkout "$2" "$3" ;; |
| 183 | materialize) |
| 184 | [ $# = 4 ] || die "usage: $PROG materialize SOURCE_REPOSITORY COMMIT DESTINATION" |
| 185 | source_repo=$2 |
| 186 | commit=$3 |
| 187 | destination=$4 |
| 188 | full_commit "$commit" |
| 189 | [ -d "$source_repo" ] || die "$source_repo is not a repository directory" |
| 190 | source_repo=$(cd "$source_repo" && pwd -P) || die "cannot resolve $source_repo" |
| 191 | gitc -C "$source_repo" rev-parse --git-dir >/dev/null 2>&1 || die "$source_repo is not a Git repository" |
| 192 | [ ! -e "$destination" ] && [ ! -L "$destination" ] || die "destination already exists: $destination" |
| 193 | mkdir -p -- "$(dirname "$destination")" || die "cannot create destination parent" |
| 194 | empty=$(mktemp -d "${TMPDIR:-/tmp}/omega-pin-template.XXXXXX") || die "mktemp failed" |
| 195 | gitc init --quiet --template="$empty" "$destination" || die "cannot initialize $destination" |
| 196 | rmdir -- "$empty" || true |
| 197 | empty="" |
| 198 | created=$destination |
| 199 | # Copy the commit with pack-objects in this process. Fetching the source |
| 200 | # repository would execute its upload-pack, including a configured |
| 201 | # packObjectsHook. A raw commit id is not a bundle tip (git refuses an |
| 202 | # "empty" bundle), and a shared clone would keep the source object store. |
| 203 | tmpd=$(mktemp -d "${TMPDIR:-/tmp}/omega-pinned-checkout.XXXXXX") || die "mktemp failed" |
| 204 | printf '%s\n' "$commit" | gitc -C "$source_repo" pack-objects --revs --quiet --stdout > "$tmpd/pin.pack" || |
| 205 | die "cannot copy locked commit $commit from $source_repo" |
| 206 | gitc -C "$destination" unpack-objects -q < "$tmpd/pin.pack" || |
| 207 | die "cannot import locked commit $commit" |
| 208 | gitc -C "$destination" checkout --quiet --detach "$commit" || die "cannot check out $commit" |
| 209 | rm -rf -- "$tmpd" |
| 210 | tmpd="" |
| 211 | tree=$(verify_checkout "$destination" "$commit") |
| 212 | chmod -R a-w -- "$destination" || die "cannot make $destination read-only" |
| 213 | created="" |
| 214 | printf '%s\n' "$tree" ;; |
| 215 | *) |
| 216 | die "usage: $PROG verify CHECKOUT COMMIT | materialize SOURCE_REPOSITORY COMMIT DESTINATION" ;; |
| 217 | esac |