gunk-dev/the-valley

knots-e2e.sh

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