| 1 | # shellcheck shell=bash |
| 2 | # The plan graph through the real host hook: bare repositories wired with |
| 3 | # the pre-receive hook the protected host's valley-init renders, written by |
| 4 | # the real valley knot as each principal, and pushed crafted commits that |
| 5 | # break each rule the hook holds a push to refs/the-valley/knots to. Every |
| 6 | # refused push must leave the ref where it was. |
| 7 | # Paths and fixtures come from the derivation environment. |
| 8 | # shellcheck disable=SC2154 |
| 9 | set -euo pipefail |
| 10 | |
| 11 | w="$TMPDIR/knots-e2e" |
| 12 | mkdir -p "$w" |
| 13 | export HOME="$w" |
| 14 | export XDG_CACHE_HOME="$w/cache" |
| 15 | export GIT_CONFIG_NOSYSTEM=1 |
| 16 | git config --global user.name knots-e2e |
| 17 | git config --global user.email knots-e2e@localhost |
| 18 | git config --global init.defaultBranch main |
| 19 | git config --global advice.detachedHead false |
| 20 | |
| 21 | fail() { |
| 22 | printf 'knots-e2e: %s\n' "$*" >&2 |
| 23 | if [ -s "$w/err" ]; then cat "$w/err" >&2; fi |
| 24 | exit 1 |
| 25 | } |
| 26 | |
| 27 | # valley is bin/valley, run by bash, with the valley-go helper on PATH. |
| 28 | mkdir -p "$w/bin" |
| 29 | printf '#!%s\nexec %q %q "$@"\n' "$BASH" "$BASH" "$cliScript" > "$w/bin/valley" |
| 30 | chmod +x "$w/bin/valley" |
| 31 | export PATH="$w/bin:$PATH" |
| 32 | |
| 33 | # A bare repo wired as valley-init wires it, with the hook followed from |
| 34 | # the rendered init script, as protect-e2e wires it. No verifier keys are |
| 35 | # named: no attestation is accepted here. git runs the hook through a |
| 36 | # wrapper that adds a line to <name>.runs for each run. |
| 37 | serve() { |
| 38 | local hook |
| 39 | hook="$(grep -o "/nix/store/[^ ]*-valley-protect-$1" "$initScriptPath" | head -n1)" |
| 40 | test -x "$hook" |
| 41 | git init --quiet --bare "$w/$1.git" |
| 42 | sed "s|/var/lib/valley-instance/known_signers|$w/no_such_keys|" "$hook" > "$w/$1.git/hooks/valley-protect" |
| 43 | chmod +x "$w/$1.git/hooks/valley-protect" |
| 44 | printf '#!%s\necho run >> %q\nexec %q\n' "$BASH" "$w/$1.runs" "$w/$1.git/hooks/valley-protect" \ |
| 45 | > "$w/$1.git/hooks/pre-receive" |
| 46 | chmod +x "$w/$1.git/hooks/pre-receive" |
| 47 | } |
| 48 | serve guarded |
| 49 | serve open |
| 50 | |
| 51 | # Pushing as a principal is pushing with the tag on, as protect-e2e does. |
| 52 | as() { |
| 53 | local who="$1" |
| 54 | shift |
| 55 | if [ "$who" = anonymous ]; then |
| 56 | env -u VALLEY_PRINCIPAL "$@" |
| 57 | else |
| 58 | VALLEY_PRINCIPAL="$who" "$@" |
| 59 | fi |
| 60 | } |
| 61 | tip() { git -C "$w/$1.git" rev-parse --verify --quiet refs/the-valley/knots || true; } |
| 62 | |
| 63 | git init --quiet "$w/work" |
| 64 | cd "$w/work" |
| 65 | git remote add origin "$w/guarded.git" |
| 66 | git remote add open "$w/open.git" |
| 67 | |
| 68 | # ---------------------------------------------------------------------- |
| 69 | # The real CLI, as each principal: patrick creates the graph, klaus-env |
| 70 | # adds to it. The hook admits every write. |
| 71 | git config valley.principal patrick |
| 72 | as patrick valley knot new outcome "Slice one lands" > "$w/out" 2> "$w/err" || fail "patrick could not create the graph" |
| 73 | milestone="oc-$(printf '%s' slice-one-lands | sha256sum | cut -c1-7)" |
| 74 | created="$(tip guarded)" |
| 75 | [ -n "$created" ] || fail "the create did not land" |
| 76 | as patrick valley knot new bug "The hook reads too much" --parent "$milestone" > "$w/out" 2> "$w/err" \ |
| 77 | || fail "patrick could not update the graph" |
| 78 | bug="bd-$(printf '%s' the-hook-reads-too-much | sha256sum | cut -c1-7)" |
| 79 | as patrick valley knot comment "$bug" -m "Seen on a large graph." > "$w/out" 2> "$w/err" || fail "patrick could not comment" |
| 80 | git config valley.principal klaus-env |
| 81 | as klaus-env valley knot new outcome "Write the hook case" --parent "$milestone" --blocked-by "$bug" \ |
| 82 | > "$w/out" 2> "$w/err" || fail "klaus-env could not add a knot" |
| 83 | work="oc-$(printf '%s' write-the-hook-case | sha256sum | cut -c1-7)" |
| 84 | as klaus-env valley knot claim "$work" --run 20261015-0058-feb0a926 > "$w/out" 2> "$w/err" || fail "klaus-env could not claim" |
| 85 | as klaus-env valley knot set "$bug" status=closed > "$w/out" 2> "$w/err" || fail "klaus-env could not change patrick's knot" |
| 86 | as klaus-env valley knot comment "$work" -m "Claimed." > "$w/out" 2> "$w/err" || fail "klaus-env could not comment" |
| 87 | # A question waits on its owner, and links the change that records its |
| 88 | # answer in the source. |
| 89 | as klaus-env valley knot new question "Where does the graph live" --owner patrick > "$w/out" 2> "$w/err" \ |
| 90 | || fail "klaus-env could not add a question" |
| 91 | question="qn-$(printf '%s' where-does-the-graph-live | sha256sum | cut -c1-7)" |
| 92 | as klaus-env valley knot set "$question" status=answered > "$w/out" 2> "$w/err" || fail "klaus-env could not answer the question" |
| 93 | as klaus-env valley knot link "$question" --change valley:the-valley/kkkkllllmmmmnnnnooooppppqqqqrrrr > "$w/out" 2> "$w/err" \ |
| 94 | || fail "klaus-env could not link a change to a question" |
| 95 | git -C "$w/guarded.git" cat-file blob "refs/the-valley/knots:questions/$question-where-does-the-graph-live.md" > "$w/question" |
| 96 | grep -qx 'status: answered' "$w/question" || fail "the question's answer did not land" |
| 97 | grep -qx 'owner: "patrick"' "$w/question" || fail "the question's owner did not land" |
| 98 | grep -qx 'changes: \["valley:the-valley/kkkkllllmmmmnnnnooooppppqqqqrrrr"\]' "$w/question" || fail "the question's change did not land" |
| 99 | [ "$(git -C "$w/guarded.git" log --format=%cn refs/the-valley/knots | sort -u | tr '\n' ' ')" = "klaus-env patrick " ] \ |
| 100 | || fail "the graph's commits are not both principals'" |
| 101 | |
| 102 | # The CLI, written as a principal its key does not push as, is refused by |
| 103 | # the hook, and says what the hook said. A refusal is the hook's answer, |
| 104 | # not a race, so the CLI pushes once and does not try again. |
| 105 | git config valley.principal patrick |
| 106 | : > "$w/guarded.runs" |
| 107 | as klaus-env valley knot comment "$bug" -m "Forged." > "$w/out" 2> "$w/err" && fail "a write by a principal other than the pusher landed" |
| 108 | grep -q 'origin refused the write' "$w/err" || fail "the CLI did not report the hook's refusal" |
| 109 | grep -q 'patrick as its committer, and klaus-env is pushing it' "$w/err" || fail "the refusal does not name the committer" |
| 110 | [ "$(wc -l < "$w/guarded.runs")" -eq 1 ] || fail "a refused write ran the hook $(wc -l < "$w/guarded.runs") times, not once" |
| 111 | |
| 112 | # ---------------------------------------------------------------------- |
| 113 | # Crafted commits. Each is made on a scratch index from the current tip, |
| 114 | # with the edits given, by the committer given, and pushed as a principal. |
| 115 | git fetch --quiet origin refs/the-valley/knots |
| 116 | good="$(tip guarded)" |
| 117 | # blob writes a file's content, given as command substitution leaves it: |
| 118 | # without the one newline every canonical file ends with. |
| 119 | blob() { printf '%s\n' "$1" | git hash-object -w --stdin; } |
| 120 | # craft <parent|-> <committer> <message|-> <edit…>, where each edit is |
| 121 | # +PATH=CONTENT, -PATH, or MODE:PATH=OID. |
| 122 | craft() { |
| 123 | local parent="$1" committer="$2" message="$3" edit tree |
| 124 | shift 3 |
| 125 | export GIT_INDEX_FILE="$w/scratch.index" |
| 126 | rm -f "$GIT_INDEX_FILE" |
| 127 | if [ "$parent" = - ]; then git read-tree --empty; else git read-tree "$parent"; fi |
| 128 | for edit in "$@"; do |
| 129 | case "$edit" in |
| 130 | +*) git update-index --add --cacheinfo "100644,$(blob "${edit#*=}"),$(p="${edit#+}" && echo "${p%%=*}")" ;; |
| 131 | -*) git update-index --force-remove -- "${edit#-}" ;; |
| 132 | *:*) git update-index --add --cacheinfo "${edit%%:*},${edit#*=},$(p="${edit#*:}" && echo "${p%%=*}")" ;; |
| 133 | esac |
| 134 | done |
| 135 | tree="$(git write-tree)" |
| 136 | unset GIT_INDEX_FILE |
| 137 | if [ "$message" = - ]; then message="$(printf 'crafted\n\nValley-Op: %016x' "$RANDOM$RANDOM")"; fi |
| 138 | local parents=() |
| 139 | if [ "$parent" != - ]; then parents=(-p "$parent"); fi |
| 140 | GIT_COMMITTER_NAME="$committer" git commit-tree "$tree" "${parents[@]}" -m "$message" |
| 141 | } |
| 142 | # refused <what> <ERE> <who> <git push args…>: the push fails, saying |
| 143 | # something matching the pattern, and the ref stays where it was. |
| 144 | refused() { |
| 145 | local what="$1" says="$2" who="$3" before |
| 146 | shift 3 |
| 147 | before="$(tip guarded)" |
| 148 | if as "$who" git push --quiet "$@" 2> "$w/err"; then fail "$what was admitted"; fi |
| 149 | grep -qE -- "$says" "$w/err" || fail "$what was refused without saying $says" |
| 150 | [ "$(tip guarded)" = "$before" ] || fail "$what moved the ref" |
| 151 | } |
| 152 | push_graph() { echo "$1:refs/the-valley/knots"; } |
| 153 | |
| 154 | knot_path() { git ls-tree -r --name-only "$good" | grep -m1 -- "/$1-"; } |
| 155 | bug_path="$(knot_path "$bug")" |
| 156 | work_path="$(knot_path "$work")" |
| 157 | comment_path="$(git ls-tree -r --name-only "$good" -- "comments/$bug/" | head -n1)" |
| 158 | bug_text="$(git cat-file blob "$good:$bug_path")" |
| 159 | work_text="$(git cat-file blob "$good:$work_path")" |
| 160 | stamp="20261015T120000Z" |
| 161 | comment() { printf -- '---\nauthor: "%s"\n---\n\n%s\n' "$1" "$2"; } |
| 162 | idea() { # idea <title> <slug> <creator> [body] |
| 163 | local id |
| 164 | id="ida-$(printf '%s' "$2" | sha256sum | cut -c1-7)" |
| 165 | printf -- '---\ntype: idea\nid: %s\nstatus: raw\ntitle: "%s"\ncreated: 2026-10-15\ncreated_by: "%s"\n---\n' "$id" "$1" "$3" |
| 166 | if [ -n "${4:-}" ]; then printf '\n%s\n' "$4"; fi |
| 167 | } |
| 168 | idea_path() { echo "ideas/ida-$(printf '%s' "$1" | sha256sum | cut -c1-7)-$1.md"; } |
| 169 | |
| 170 | # A well-formed next commit is admitted, so every refusal below is the |
| 171 | # hook's answer to the one thing each breaks. |
| 172 | next="$(craft "$good" patrick - "+$(idea_path a-crafted-idea)=$(idea "A crafted idea" a-crafted-idea patrick)")" |
| 173 | cp /dev/null "$w/err" |
| 174 | |
| 175 | # Who: an untagged key is no principal. |
| 176 | refused "an untagged key's write" 'untagged key.*named principal' anonymous origin "$(push_graph "$next")" |
| 177 | |
| 178 | # Shape: one commit whose only parent is the old tip. |
| 179 | older="$(git rev-parse "$good~1")" |
| 180 | rewind="$(craft "$older" patrick - "+$(idea_path rewound)=$(idea Rewound rewound patrick)")" |
| 181 | refused "a non-fast-forward update" 'only parent is the tip it replaces' patrick --force origin "$(push_graph "$rewind")" |
| 182 | side="$(craft "$older" patrick - "+$(idea_path side)=$(idea Side side patrick)")" |
| 183 | merge="$(GIT_COMMITTER_NAME=patrick git commit-tree "$next^{tree}" -p "$good" -p "$side" -m "$(printf 'merge\n\nValley-Op: 0123456789abcdef')")" |
| 184 | refused "a merge" 'only parent is the tip it replaces' patrick origin "$(push_graph "$merge")" |
| 185 | two="$(craft "$next" patrick - "+$(idea_path second)=$(idea Second second patrick)")" |
| 186 | refused "two commits at once" 'only parent is the tip it replaces' patrick origin "$(push_graph "$two")" |
| 187 | git commit --quiet --allow-empty -m unrelated |
| 188 | stranger="$(craft "$(git rev-parse HEAD)" patrick - "+$(idea_path stranger)=$(idea Stranger stranger patrick)")" |
| 189 | refused "a parent that is not the old tip" 'only parent is the tip it replaces' patrick --force origin "$(push_graph "$stranger")" |
| 190 | tagged="$(printf 'object %s\ntype commit\ntag t\ntagger t <t> 0 +0000\n\nt\n' "$next" | git mktag)" |
| 191 | refused "a tag object as the tip" 'points at a tag' patrick origin "$tagged:refs/the-valley/knots" |
| 192 | |
| 193 | # Every commit: committed by the pusher, and carrying its operation. |
| 194 | forged="$(craft "$good" klaus-env - "+$(idea_path forged)=$(idea Forged forged klaus-env)")" |
| 195 | refused "a commit by another committer" 'names klaus-env as its committer, and patrick is pushing it' patrick origin "$(push_graph "$forged")" |
| 196 | untrailed="$(craft "$good" patrick "no trailer" "+$(idea_path untrailed)=$(idea Untrailed untrailed patrick)")" |
| 197 | refused "a commit without its trailer" 'Valley-Op' patrick origin "$(push_graph "$untrailed")" |
| 198 | |
| 199 | # Transitions: comments stay, knots stay, and what never changes does not. |
| 200 | refused "a deleted comment" 'a comment is never deleted' patrick origin "$(push_graph "$(craft "$good" patrick - "-$comment_path")")" |
| 201 | refused "a changed comment" 'stays exactly as it was written' patrick origin \ |
| 202 | "$(push_graph "$(craft "$good" patrick - "+$comment_path=$(comment patrick 'Rewritten.')")")" |
| 203 | refused "a deleted knot" 'a knot is never deleted' patrick origin "$(push_graph "$(craft "$good" patrick - "-$bug_path")")" |
| 204 | refused "a changed id" 'filename says id' patrick origin \ |
| 205 | "$(push_graph "$(craft "$good" patrick - "+$bug_path=${bug_text/id: $bug/id: bd-0000000}")")" |
| 206 | refused "a changed type" "id is \"$bug\", and an outcome's id is oc-" patrick origin \ |
| 207 | "$(push_graph "$(craft "$good" patrick - "+$bug_path=${bug_text/type: bug/type: outcome}")")" |
| 208 | refused "a changed created_by" 'changes created_by from "patrick" to "klaus-env"' klaus-env origin \ |
| 209 | "$(push_graph "$(craft "$good" klaus-env - "+$bug_path=${bug_text/created_by: \"patrick\"/created_by: \"klaus-env\"}")")" |
| 210 | |
| 211 | # Provenance: a new comment's author and a new knot's creator are the |
| 212 | # pusher. |
| 213 | refused "a comment with a forged author" 'names klaus-env as its author, and patrick is pushing it' patrick origin \ |
| 214 | "$(push_graph "$(craft "$good" patrick - "+comments/$bug/$stamp-0123456789abcdef.md=$(comment klaus-env 'Not mine.')")")" |
| 215 | refused "a knot with a forged creator" 'names klaus-env in created_by, and patrick is pushing it' patrick origin \ |
| 216 | "$(push_graph "$(craft "$good" patrick - "+$(idea_path forged)=$(idea Forged forged klaus-env)")")" |
| 217 | |
| 218 | # Layout: plain files, in the layout, within the bounds. |
| 219 | knot_blob="$(blob "$(idea Linked linked patrick)")" |
| 220 | refused "a symlink" 'mode 120000' patrick origin "$(push_graph "$(craft "$good" patrick - "120000:$(idea_path linked)=$knot_blob")")" |
| 221 | refused "an executable" 'mode 100755' patrick origin "$(push_graph "$(craft "$good" patrick - "100755:$(idea_path linked)=$knot_blob")")" |
| 222 | refused "a file outside the layout" 'outside the graph.s layout' patrick origin "$(push_graph "$(craft "$good" patrick - "+README.md=hello")")" |
| 223 | refused "a directory outside the layout" 'no directory a graph holds' patrick origin \ |
| 224 | "$(push_graph "$(craft "$good" patrick - "+tasks/$(basename "$(idea_path tasked)")=$(idea Tasked tasked patrick)")")" |
| 225 | # A knot is an outcome, a bug, an idea or a question, and the hook names |
| 226 | # the directories each lives in. |
| 227 | decision="dcr-$(printf '%s' one-ref | sha256sum | cut -c1-7)" |
| 228 | decision_text="$(printf -- '---\ntype: decision\nid: %s\nstatus: decided\ntitle: "One ref"\ncreated: 2026-10-15\ncreated_by: "patrick"\n---' "$decision")" |
| 229 | refused "a decision" 'decisions: is no directory a graph holds; the root holds outcomes, bugs, ideas, questions, comments' patrick origin \ |
| 230 | "$(push_graph "$(craft "$good" patrick - "+decisions/$decision-one-ref.md=$decision_text")")" |
| 231 | refused "a non-canonical knot" 'not in canonical form' patrick origin \ |
| 232 | "$(push_graph "$(craft "$good" patrick - "+$(idea_path loose)=$(idea Loose loose patrick | sed 's/^title: "Loose"/title: Loose/')")")" |
| 233 | big="$(head -c 70000 /dev/zero | tr '\0' x)" |
| 234 | refused "an oversized knot" 'more than the 65536' patrick origin \ |
| 235 | "$(push_graph "$(craft "$good" patrick - "+$(idea_path big)=$(idea Big big patrick "$big")")")" |
| 236 | |
| 237 | # A tree that names one subtree many times over is refused at a bound, |
| 238 | # before it is walked. |
| 239 | leaf_lines="" |
| 240 | for i in $(seq 1 64); do |
| 241 | leaf_lines+="$(printf '100644 blob %s\t%s-%016x.md' "$(blob "$(comment patrick x)")" "$stamp" "$i")"$'\n' |
| 242 | done |
| 243 | leaf="$(printf '%s' "$leaf_lines" | git mktree)" |
| 244 | comments_tree="$(for i in $(seq 1 1000); do printf '040000 tree %s\toc-%07x\n' "$leaf" "$i"; done | git mktree)" |
| 245 | root="$(git ls-tree "$good" | grep -v $'\tcomments$' | { cat; printf '040000 tree %s\tcomments\n' "$comments_tree"; } | git mktree)" |
| 246 | bomb="$(GIT_COMMITTER_NAME=patrick git commit-tree "$root" -p "$good" -m "$(printf 'bomb\n\nValley-Op: 0123456789abcdef')")" |
| 247 | start="$(date +%s)" |
| 248 | refused "a shared-subtree bomb" 'entries in all' patrick origin "$(push_graph "$bomb")" |
| 249 | [ $(($(date +%s) - start)) -lt 10 ] || fail "the bomb took more than 10 seconds to refuse" |
| 250 | |
| 251 | # Trees written by hand. A tree object can hold any bytes, and the host |
| 252 | # runs no fsck on what a push brings, so the hook refuses every entry git |
| 253 | # does not write. A name with a slash in it would be joined into a path |
| 254 | # that is no directory's, and a directory in the mode 040000, which git |
| 255 | # reads as a tree, would be a subtree the hook never walked. mktree and |
| 256 | # update-index refuse to write such trees, so raw_tree writes the entries |
| 257 | # it is given, each a mode, a name and an object, in the order given. |
| 258 | raw_tree() { |
| 259 | local f="$w/raw-tree" |
| 260 | : > "$f" |
| 261 | while [ "$#" -gt 0 ]; do |
| 262 | printf '%s %s\0' "$1" "$2" >> "$f" |
| 263 | # shellcheck disable=SC2059 # the format is the object id as \x escapes |
| 264 | printf "$(printf '%s' "$3" | sed 's/../\\x&/g')" >> "$f" |
| 265 | shift 3 |
| 266 | done |
| 267 | git hash-object -t tree --literally -w "$f" |
| 268 | } |
| 269 | # entries_of <tree> [-r]: the tree's entries, or with -r every file below |
| 270 | # it at its full path, as raw_tree takes them. |
| 271 | entries_of() { |
| 272 | git ls-tree "${@:2}" "$1" | while IFS=$' \t' read -r mode _ oid name; do |
| 273 | printf '%s\n%s\n%s\n' "${mode#0}" "$name" "$oid" |
| 274 | done |
| 275 | } |
| 276 | on_good() { GIT_COMMITTER_NAME=patrick git commit-tree "$1" -p "$good" -m "$(printf 'raw\n\nValley-Op: %016x' "$RANDOM$RANDOM")"; } |
| 277 | mapfile -t root_entries < <(entries_of "$good") |
| 278 | mapfile -t flat_entries < <(entries_of "$good" -r) |
| 279 | named_idea="$(blob "$(idea Named named patrick)")" |
| 280 | refused "a root file named ideas/<id>-<slug>.md" 'named "ideas/ida-[0-9a-f]{7}-named\.md", and git writes no name with a slash' \ |
| 281 | patrick origin "$(push_graph "$(on_good "$(raw_tree "${root_entries[@]}" 100644 "$(idea_path named)" "$named_idea")")")" |
| 282 | refused "every knot and comment flattened into names with slashes" 'git writes no name with a slash' \ |
| 283 | patrick origin "$(push_graph "$(on_good "$(raw_tree "${flat_entries[@]}")")")" |
| 284 | # comments/, in the mode 040000, naming a subtree with a forged comment. |
| 285 | hidden="$(raw_tree 40000 "$bug" "$(raw_tree 100644 "$stamp-0123456789abcdef.md" "$(blob "$(comment klaus-env 'Never read.')")")")" |
| 286 | hiding=() |
| 287 | for ((i = 0; i < ${#root_entries[@]}; i += 3)); do |
| 288 | if [ "${root_entries[i + 1]}" = comments ]; then |
| 289 | hiding+=(040000 comments "$hidden") |
| 290 | else |
| 291 | hiding+=("${root_entries[@]:i:3}") |
| 292 | fi |
| 293 | done |
| 294 | refused "a directory in the mode 040000" 'holds "comments" with mode 040000, and git writes only the modes' \ |
| 295 | patrick origin "$(push_graph "$(on_good "$(raw_tree "${hiding[@]}")")")" |
| 296 | for name in . ..; do |
| 297 | refused "an entry named $name" "holds an entry named \"${name//./\\.}\", which git does not write" \ |
| 298 | patrick origin "$(push_graph "$(on_good "$(raw_tree "${root_entries[@]}" 100644 "$name" "$named_idea")")")" |
| 299 | done |
| 300 | # git push walks every tree it sends, and will not send one that holds an |
| 301 | # entry with an empty name. So the update is sent to receive-pack as any |
| 302 | # client could send it: the command, then a pack of exactly the objects |
| 303 | # named. git refuses the update itself, since its own walk cannot read the |
| 304 | # tree, and the hook, which git still runs, refuses it too. |
| 305 | nameless_tree="$(raw_tree "${root_entries[@]}" 100644 "" "$named_idea")" |
| 306 | nameless="$(on_good "$nameless_tree")" |
| 307 | update_line="$good $nameless refs/the-valley/knots" |
| 308 | capabilities=report-status |
| 309 | { |
| 310 | printf '%04x%s\0%s\n' $((4 + ${#update_line} + 1 + ${#capabilities} + 1)) "$update_line" "$capabilities" |
| 311 | printf 0000 |
| 312 | printf '%s\n' "$nameless" "$nameless_tree" "$named_idea" | git pack-objects --quiet --stdout |
| 313 | } > "$w/raw.request" |
| 314 | as patrick git receive-pack "$w/guarded.git" < "$w/raw.request" > "$w/raw.report" 2> "$w/err" || true |
| 315 | grep -q 'ng refs/the-valley/knots' "$w/raw.report" || fail "an entry with an empty name was admitted" |
| 316 | grep -q 'its tree: it holds an entry with an empty name' "$w/err" || fail "the hook did not refuse an entry with an empty name" |
| 317 | [ "$(tip guarded)" = "$good" ] || fail "an entry with an empty name moved the ref" |
| 318 | |
| 319 | # Edges: every edge resolves, a parent is an outcome, and no edge forms a |
| 320 | # cycle. A cycle is crafted, since the CLI refuses to write one. |
| 321 | refused "a blocker that is no knot" 'is no knot in the graph' patrick origin \ |
| 322 | "$(push_graph "$(craft "$good" patrick - "+$work_path=${work_text/blocked_by: \[$bug\]/blocked_by: [$bug, oc-0000000]}")")" |
| 323 | loop_bug="${bug_text/blocked_by: \[\]/blocked_by: [$work]}" |
| 324 | [ "$loop_bug" != "$bug_text" ] || fail "the bug's blocked_by could not be edited" |
| 325 | refused "a cycle" 'blocked_by edges form a cycle' patrick origin "$(push_graph "$(craft "$good" patrick - "+$bug_path=$loop_bug")")" |
| 326 | |
| 327 | # The ref: never deleted, and matched exactly. |
| 328 | refused "a deletion of the graph" 'never deleted' patrick origin --delete refs/the-valley/knots |
| 329 | refused "a ref that only begins with the graph's name" 'written only by the machinery' patrick origin "$next:refs/the-valley/knots-x" |
| 330 | refused "a ref below the graph's name" 'written only by the machinery' patrick origin "$next:refs/the-valley/knots/x" |
| 331 | |
| 332 | # Create: one commit with no parent, on a project that has no graph yet. |
| 333 | first="$(craft - patrick - "+$(idea_path first)=$(idea First first patrick)")" |
| 334 | child="$(craft "$first" patrick - "+$(idea_path second)=$(idea Second second patrick)")" |
| 335 | if as patrick git push --quiet open "$(push_graph "$child")" 2> "$w/err"; then fail "a create with a parent was admitted"; fi |
| 336 | grep -q 'creates the graph has no parent' "$w/err" || fail "a create with a parent was refused for another reason" |
| 337 | [ -z "$(tip open)" ] || fail "a refused create made the ref" |
| 338 | as patrick git push --quiet open "$(push_graph "$first")" 2> "$w/err" || fail "a parentless create was refused" |
| 339 | as patrick git push --quiet open "$(push_graph "$child")" 2> "$w/err" || fail "the update after it was refused" |
| 340 | [ "$(tip open)" = "$child" ] || fail "the open project's graph is not at the update" |
| 341 | |
| 342 | # A push that carries a valid graph update with an invalid write to |
| 343 | # another namespace is refused whole, atomic or not: a change record that |
| 344 | # is no record, an attestation no key here accepts, and an integration |
| 345 | # request from a principal without the request grant. |
| 346 | record="refs/the-valley/changes/main/$(printf 'k%.0s' $(seq 1 32))" |
| 347 | attestation="refs/the-valley/attestations/$(printf '%064d' 0)/00000000" |
| 348 | request="refs/the-valley/integration-requests/main/topic" |
| 349 | for other in "$next:$record" "$next:$attestation" "HEAD:$request"; do |
| 350 | for mode in --atomic --no-atomic; do |
| 351 | refused "a graph update beside $other ($mode)" 'refs/the-valley/(changes|attestations|integration-requests)/' \ |
| 352 | patrick "$mode" origin "$(push_graph "$next")" "$other" |
| 353 | if git -C "$w/guarded.git" rev-parse --verify --quiet "${other#*:}" > /dev/null; then fail "$other landed"; fi |
| 354 | done |
| 355 | done |
| 356 | |
| 357 | # The well-formed commit itself is admitted. |
| 358 | as patrick git push --quiet origin "$(push_graph "$next")" 2> "$w/err" || fail "a well-formed update was refused" |
| 359 | [ "$(tip guarded)" = "$next" ] || fail "the well-formed update did not land" |
| 360 | touch "$out" |