gunk-dev/the-valley

knots-e2e.sh

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