gunk-dev/the-valley

valley-changes.sh

sourcee612ba14a52bmain's published tip history
34.0 KiB
1# shellcheck shell=bash
2# valley changes and valley pending, end to end and unmocked, over the
3# repositories one test host serves. The host is made by the integrator
4# host's rendered valley-init, relocated as init-e2e relocates it, so the
5# project list discovery reads is the one a host publishes, and every
6# record is pushed through the hook that host installs. What the
7# integrator writes on disk when it lands a change — main, the outcome
8# record, the transfer statement — is written here the same way, with a
9# key generated for the run.
10# Paths and fixtures come from the derivation environment.
11# shellcheck disable=SC2154
12set -euo pipefail
13
14w="$TMPDIR/valley-changes"
15mkdir -p "$w"
16export HOME="$w"
17export XDG_CACHE_HOME="$w/cache"
18export GIT_CONFIG_NOSYSTEM=1
19git config --global user.name valley-changes-check
20git config --global user.email valley-changes-check@localhost
21git config --global init.defaultBranch main
22git config --global advice.detachedHead false
23unset VALLEY_KNOWN_SIGNERS VALLEY_INTEGRATOR_SIGNER
24
25fail() {
26 printf 'valley-changes: %s\n' "$*" >&2
27 for f in "$w"/out "$w"/err; do
28 if [ -s "$f" ]; then cat "$f" >&2; fi
29 done
30 exit 1
31}
32
33# ok <command…>: the command succeeds, and its output is kept.
34ok() {
35 "$@" > "$w/out" 2> "$w/err" || fail "$* failed"
36}
37
38# holds <jq condition…>: the last JSON printed holds each.
39holds() {
40 local cond
41 for cond in "$@"; do
42 jq -e "$cond" "$w/out" > /dev/null || fail "$cond does not hold"
43 done
44}
45
46# line <ERE>: the last output has a line matching it.
47line() {
48 grep -qE -- "$1" "$w/out" || fail "no line matches $1"
49}
50
51no_line() {
52 if grep -qE -- "$1" "$w/out"; then fail "a line matches $1"; fi
53}
54
55# after <ERE> <ERE…>: the first line of the last output that matches the
56# first is followed, line by line, by lines matching the rest.
57after() {
58 local n re
59 n="$(grep -nE -m 1 -- "$1" "$w/out" | cut -d: -f1 || true)"
60 [ -n "$n" ] || fail "no line matches $1"
61 for re in "${@:2}"; do
62 n=$((n + 1))
63 sed -n "${n}p" "$w/out" | grep -qE -- "$re" || fail "line $n, after $1, does not match $re"
64 done
65}
66
67# The host: guarded, open, released and sealed, with sealed the instance
68# repository. valley-init creates them, wires their hooks, and publishes
69# the project list in sealed.
70data="$w/srv/git"
71mkdir -p "$data"
72sed "s|/srv/git|$data|g" "$initScriptPath" > "$w/init.sh"
73bash "$w/init.sh" 2> "$w/init.err" || fail "valley-init failed: $(cat "$w/init.err")"
74instance="$data/sealed.git"
75# The integrator's repositories are group-shared, and git then sets the
76# setgid bit on every directory it makes, which this sandbox refuses. The
77# setting is dropped so the pushes below can write objects; it decides
78# nothing this check reads.
79for name in guarded open released sealed; do
80 git -C "$data/$name.git" config --unset core.sharedRepository || true
81done
82[ "$(git -C "$instance" cat-file blob refs/the-valley/projects)" = "$(printf '%s\n' guarded open released sealed)" ] ||
83 fail "valley-init published another project list"
84
85# A change's ID, by the letter it repeats. Each is 32 letters from k to z.
86id() { printf '%s%s' "$1" "$(printf 'k%.0s' $(seq $((32 - ${#1}))))"; }
87A="$(id nxvpwlkm)" B="$(id mmmm)" C="$(id pppp)" D="$(id qqqq)" E="$(id rrrr)"
88W="$(id ssss)" F="$(id tttt)" G="$(id uuuu)" U="$(id vvvv)" H="$(id wwww)"
89X="$(id oooo)" I="$(id zzzz)" J="$(id lmlm)" X2="$(id pqpq)"
90R="$(id nxvpwlkz)" D2="$(id xxxx)" E2="$(id yyyy)"
91
92# A checkout of a project, whose main names sealed as its owning valley.
93# Every push carries the principal, as the key sshd authenticated would.
94export VALLEY_PRINCIPAL=alice
95checkout() {
96 local name="$1" dir="$w/$1"
97 git clone --quiet "$data/$name.git" "$dir" 2> /dev/null
98 git -C "$dir" config valley.principal alice
99 mkdir -p "$dir/policy"
100 printf '# the owning valley\n%s\n' "$instance" > "$dir/policy/valley"
101 git -C "$dir" add policy/valley
102 git -C "$dir" commit --quiet -m base
103 VALLEY_PRINCIPAL=integrator git -C "$dir" push --quiet origin main
104}
105
106# branch <name> <from> <subject…>: a branch from a commit, with a commit
107# per subject, each adding a file of its own, pushed to origin.
108branch() {
109 local name="$1" from="$2" s
110 shift 2
111 git checkout --quiet -b "$name" "$from"
112 for s in "$@"; do
113 echo "$s" > "$s.txt"
114 git add "$s.txt"
115 git commit --quiet -m "$s"
116 done
117 git push --quiet origin "$name"
118}
119
120# ready <id> <branch>: the branch's head readied as the change.
121ready() {
122 ok valley ready --change "$1" "$2"
123}
124
125# ----------------------------------------------------------------------
126# open: a stack of three, a draft, a withdrawn change with a ready change
127# on top of it, a change landed by fast-forward, one landed as a new
128# commit with its transfer statement, one whose request was consumed with
129# no evidence stored, one whose transfer statement another key signed,
130# and a branch no record is on.
131checkout open
132cd "$w/open"
133base="$(git rev-parse HEAD)"
134branch stack-a "$base" stack-a
135ready "$A" stack-a
136branch stack-b stack-a stack-b
137ready "$B" stack-b
138branch stack-c stack-b stack-c
139ready "$C" stack-c
140branch paused "$base" paused
141ready "$D" paused
142ok valley unready "$D"
143branch gone "$base" gone
144ready "$E" gone
145ok valley withdraw "$E"
146branch atop-gone gone atop-gone
147ready "$W" atop-gone
148branch ff "$base" ff
149ready "$F" ff
150branch reparent "$base" reparent
151ready "$G" reparent
152branch lost "$base" lost
153ready "$U" lost
154branch forged "$base" forged
155ready "$H" forged
156branch loose "$base" loose
157
158# Before anything lands, ff names its change.
159ok valley-go resolve ff
160line "^change"$'\t'"$F"$'\t'
161
162# The integrator's keys: the integrator's own, and another a known
163# signer holds, which may sign evidence and not a landing.
164ssh-keygen -q -t ed25519 -N '' -C integrator -f "$w/integrator.key"
165ssh-keygen -q -t ed25519 -N '' -C mallory -f "$w/mallory.key"
166{
167 attest key --key "$w/integrator.key" --name integrator
168 attest key --key "$w/mallory.key" --name mallory
169} > "$w/known-signers"
170
171# F lands by fast-forward.
172VALLEY_PRINCIPAL=integrator git push --quiet origin ff:main
173fhead="$(git rev-parse ff)"
174
175# G lands as a new commit on main, and the integrator records it: main
176# moves, the outcome record says land, the request is consumed, and the
177# transfer statement is stored under the landed tree.
178git checkout --quiet -b landing ff
179git cherry-pick reparent > /dev/null
180landed="$(git rev-parse HEAD)"
181VALLEY_PRINCIPAL=integrator git push --quiet origin landing:main
182tree_digest() { attest digest --repo "$w/open" --rev "$1" | cut -d: -f2; }
183
184# memo <name> <head> <decision…>: the integrator's outcome record.
185memo() {
186 local name="$1" head="$2" blob
187 shift 2
188 blob="$(printf '%s %s %s %s\n' "$head" "$fhead" 0123456789abcdef "$*" | git -C "$data/open.git" hash-object -w --stdin)"
189 git -C "$data/open.git" update-ref "refs/the-valley/integration-outcomes/main/$name" "$blob"
190}
191
192# transfer <change> <head> <landed> <key> <name>: a transfer statement,
193# as the integrator composes, signs and stores one.
194transfer() {
195 local change="$1" head="$2" at="$3" key="$4" name="$5" ldigest blob tree
196 ldigest="$(tree_digest "$at")"
197 jq -n --arg change "$change" --arg base "$(tree_digest "$head")" --arg landed "$ldigest" \
198 --arg commit "$at" --arg tip "$fhead" '{
199 statementType: "the-valley/attestation/v1",
200 subject: {primary: "valley-tree-v1", digest: {"valley-tree-v1": $landed, "git-sha1": $commit}},
201 predicateType: "the-valley/integration/transfer/v1",
202 predicate: {change: $change, target: "refs/heads/main", base: {"valley-tree-v1": $base},
203 policy: {sha256: ("ab" * 32), "git-sha1": $tip}, required: "none"}
204 }' > "$w/statement.json"
205 attest render "$w/statement.json" | attest sign --key "$key" --name "$name" > "$w/transfer.note"
206 blob="$(git -C "$data/open.git" hash-object -w "$w/transfer.note")"
207 tree="$(printf '100644 blob %s\ttransfer.note\n' "$blob" | git -C "$data/open.git" mktree)"
208 git -C "$data/open.git" update-ref "refs/the-valley/attestations/$ldigest/$(printf '%08x' "${#name}")" "$tree"
209}
210memo reparent "$(git rev-parse reparent)" land
211transfer reparent "$(git rev-parse reparent)" "$landed" "$w/integrator.key" integrator
212
213# U's request was consumed, and no evidence was stored.
214memo lost "$(git rev-parse lost)" land
215# H's request was consumed too, and its transfer statement is signed by a
216# known key that is not the integrator's: it is no record of a landing.
217memo forged "$(git rev-parse forged)" land
218git checkout --quiet -b landing-h landing
219git cherry-pick forged > /dev/null
220VALLEY_PRINCIPAL=integrator git push --quiet origin landing-h:main
221transfer forged "$(git rev-parse forged)" "$(git rev-parse HEAD)" "$w/mallory.key" mallory
222# A's request is pending.
223git -C "$data/open.git" update-ref refs/the-valley/integration-requests/main/stack-a "$(git rev-parse stack-a)"
224
225# I lands as a new commit, L2, filed as c-<I>, and the integrator records
226# it. Then I's next version, H', has H's tree with L2 as its parent. Its
227# name and its tree digest match the transfer statement, but it is another
228# delta: merged onto L2's parent it does not give L2's tree. I is then made
229# a draft.
230branch again "$base" again
231ready "$I" again
232hhead="$(git rev-parse again)"
233git checkout --quiet -b landing-i landing-h
234git cherry-pick again > /dev/null
235landed2="$(git rev-parse HEAD)"
236VALLEY_PRINCIPAL=integrator git push --quiet origin landing-i:main
237memo "c-$I" "$hhead" land
238transfer "c-$I" "$hhead" "$landed2" "$w/integrator.key" integrator
239hprime="$(git commit-tree "again^{tree}" -p "$landed2" -m again)"
240git push --quiet --force origin "$hprime:refs/heads/again"
241ready "$I" again
242ok valley unready "$I"
243
244# J lands by fast-forward, and the integrator records it: its outcome
245# record says land at J's head, and its transfer statement is about J's
246# tree. Then main moves on to Q, which adds a file, and J's next version,
247# J', has J's tree with Q as its parent: it deletes Q's file. J' matches
248# the statement's name and tree, and merged onto the parent of the commit
249# that landed it gives that commit's tree. But the outcome record names
250# J's head, not J', so J' has not landed. J is then made a draft.
251branch ffrec landing-i ffrec
252ready "$J" ffrec
253jhead="$(git rev-parse ffrec)"
254VALLEY_PRINCIPAL=integrator git push --quiet origin ffrec:main
255memo ffrec "$jhead" land
256transfer ffrec "$jhead" "$jhead" "$w/integrator.key" integrator
257git checkout --quiet -b moved-on ffrec
258echo q > q.txt
259git add q.txt
260git commit --quiet -m q
261VALLEY_PRINCIPAL=integrator git push --quiet origin moved-on:main
262jprime="$(git commit-tree "ffrec^{tree}" -p moved-on -m ffrec)"
263git push --quiet --force origin "$jprime:refs/heads/ffrec"
264ready "$J" ffrec
265ok valley unready "$J"
266
267# ----------------------------------------------------------------------
268# guarded: a ready change whose ID shares seven letters with A's, a draft
269# and a withdrawn change.
270checkout guarded
271cd "$w/guarded"
272gbase="$(git rev-parse HEAD)"
273branch g-ready "$gbase" g-ready
274ready "$R" g-ready
275branch g-draft "$gbase" g-draft
276ready "$D2" g-draft
277ok valley unready "$D2"
278branch g-gone "$gbase" g-gone
279ready "$E2" g-gone
280ok valley withdraw "$E2"
281# B's change ID, recorded here too.
282branch g-dup "$gbase" g-dup
283ready "$B" g-dup
284
285# A merge on main can drop the content of either parent, so no head below
286# one has landed by ancestry. X is ready, and a merge of it whose tree
287# drops its changes lands on main by fast-forward: M = (main, X), with
288# main's tree. Then X2, branched from that main, is dropped by a merge with
289# its parents reversed: M2 = (X2, main), with main's tree. X2 is on main's
290# first-parent history, below the merge that dropped it.
291branch dropped "$gbase" dropped
292ready "$X" dropped
293merge="$(git commit-tree "$gbase^{tree}" -p "$gbase" -p dropped -m 'merge, dropping X')"
294VALLEY_PRINCIPAL=integrator git push --quiet origin "$merge:refs/heads/main"
295branch reversed "$merge" reversed
296ready "$X2" reversed
297merge2="$(git commit-tree "$merge^{tree}" -p reversed -p "$merge" -m 'merge, dropping X2')"
298VALLEY_PRINCIPAL=integrator git push --quiet origin "$merge2:refs/heads/main"
299
300# origin: a project named origin, recording B's change ID too. A host that
301# declared it would list it, so it is added to the list on disk, as
302# valley-init writes the list.
303git init --quiet --bare "$data/origin.git"
304checkout origin
305cd "$w/origin"
306branch o-dup "$(git rev-parse HEAD)" o-dup
307ready "$B" o-dup
308projects="$(printf '%s\n' guarded open origin released sealed | git -C "$instance" hash-object -w --stdin)"
309git -C "$instance" update-ref refs/the-valley/projects "$projects"
310
311# released is listed and cannot be read.
312mv "$data/released.git" "$data/released.moved"
313
314export VALLEY_KNOWN_SIGNERS="$w/known-signers" VALLEY_INTEGRATOR_SIGNER=integrator
315
316# ----------------------------------------------------------------------
317# One repository: every record in every state, as valley-changes/v1.
318cd "$w/open"
319ok valley changes --json
320[ "$(wc -l < "$w/out")" = 1 ] || fail "changes printed more than one line"
321holds '.schema == "valley-changes/v1"' '.host == "localhost"' '(.repos | length) == 1' \
322 '.repos[0].repo == "open"' ".repos[0].url == \"$data/open.git\"" '.repos[0].error == null' \
323 '(.repos[0].changes | length) == 12' '.repos[0] | has("drafts") | not'
324c() { printf '.repos[0].changes[] | select(.change_id == "%s")' "$1"; }
325holds "$(c "$A") | .handle == \"nxvp\" and .state == \"ready\" and .owner == \"alice\" and .target == \"main\" and .active == 1 and .seq == 1" \
326 "$(c "$A") | .record == \"$(git -C "$data/open.git" rev-parse "refs/the-valley/changes/main/$A")\"" \
327 "$(c "$A") | .versions[0] | .n == 1 and .title == \"stack-a\" and .branch == \"stack-a\" and .uploader == \"alice\"" \
328 "$(c "$A") | .versions[0] | .head == \"$(git rev-parse stack-a)\" and .landing == \"not-landed\" and .landed_commit == null" \
329 "$(c "$A") | .versions[0].request == {ref: \"refs/the-valley/integration-requests/main/stack-a\", commit: \"$(git rev-parse stack-a)\"}" \
330 "$(c "$A") | .versions[0].outcome == null" \
331 "$(c "$D") | .state == \"draft\" and .seq == 2" \
332 "$(c "$E") | .state == \"withdrawn\""
333# Landing evidence, one version each way.
334holds "$(c "$F") | .versions[0] | .landing == \"landed\" and .landed_commit == \"$fhead\"" \
335 "$(c "$G") | .versions[0] | .landing == \"landed-recorded\" and .landed_commit == \"$landed\"" \
336 "$(c "$G") | .versions[0].outcome == {decision: \"land\", head: \"$(git rev-parse reparent)\", tip: \"$fhead\"}" \
337 "$(c "$G") | .versions[0].request == null" \
338 "$(c "$U") | .versions[0] | .landing == \"uncertain\" and .landed_commit == null" \
339 "$(c "$H") | .versions[0].landing == \"uncertain\"" \
340 "$(c "$J") | .versions[0] | .landing == \"landed\" and .landed_commit == \"$jhead\"" \
341 "$(c "$J") | .versions[1] | .head == \"$jprime\" and .landing == \"not-landed\" and .landed_commit == null" \
342 "$(c "$J") | .state == \"draft\" and .active == 2" \
343 "$(c "$I") | .versions[0] | .landing == \"landed-recorded\" and .landed_commit == \"$landed2\"" \
344 "$(c "$I") | .versions[1] | .head == \"$hprime\" and .landing == \"not-landed\" and .landed_commit == null" \
345 "$(c "$I") | .state == \"draft\" and .active == 2"
346# includes is ancestry, nearest only.
347holds "$(c "$A") | .includes == []" "$(c "$B") | .includes == [\"$A\"]" \
348 "$(c "$C") | .includes == [\"$B\"]" "$(c "$W") | .includes == [\"$E\"]"
349# The one branch no record is on, and no branch main reaches.
350holds '.repos[0].branches == [{branch: "loose", head: "'"$(git rev-parse loose)"'"}]'
351
352# Without the registry's keys, the transfer statement is not believed, and
353# that is said.
354env -u VALLEY_KNOWN_SIGNERS valley changes --json > "$w/out" 2> "$w/err" || fail "changes failed without the keys"
355holds "$(c "$G") | .versions[0].landing == \"uncertain\""
356grep -q 'could not be verified' "$w/err" || fail "an unverified transfer statement was not reported"
357
358# ----------------------------------------------------------------------
359# Every project the host publishes. Handles are unique across both
360# repositories, the repository that cannot be read carries its error, and
361# the others are listed all the same.
362ok valley changes --json --all
363holds '[.repos[].repo] == ["guarded", "open", "origin", "released", "sealed"]' \
364 '.repos[] | select(.repo == "released") | .error != null and .changes == [] and .url == "'"$data/released.git"'"' \
365 '.repos[] | select(.repo != "released") | .error == null' \
366 '.repos[] | select(.repo == "sealed") | .changes == []' \
367 '[.repos[].changes[].handle] | length == (unique | length)' \
368 "[.repos[].changes[] | select(.change_id == \"$A\" or .change_id == \"$R\") | .handle] == [\"nxvpwlkz\", \"nxvpwlkm\"]" \
369 "[.repos[].changes[] | select(.change_id == \"$B\") | .handle] == [\"guarded:mmmm\", \"open:mmmm\", \"origin:mmmm\"]" \
370 '.repos[] | select(.repo == "guarded") | [.changes[].state] == ["ready", "ready", "ready", "ready", "draft", "withdrawn"]'
371# ls-remote matches a ref's tail, so refs whose names end like the
372# refs discovery reads must not stand in for them: one in open whose
373# policy/valley names another instance, and one in the instance listing
374# another project.
375git checkout --quiet -b decoy "$base"
376printf '%s\n' "$w/elsewhere.git" > policy/valley
377git commit --quiet -am decoy
378git push --quiet origin decoy:refs/heads/a/refs/heads/main
379decoy_list="$(printf 'decoy\n' | git -C "$instance" hash-object -w --stdin)"
380git -C "$instance" update-ref refs/tags/a/refs/the-valley/projects "$decoy_list"
381git checkout --quiet landing
382ok valley changes --json --all
383holds '[.repos[].repo] == ["guarded", "open", "origin", "released", "sealed"]'
384git push --quiet origin :refs/heads/a/refs/heads/main
385git -C "$instance" update-ref -d refs/tags/a/refs/the-valley/projects
386# --repo limits the listing to one repository, read through its clone.
387ok valley changes --json --repo guarded
388holds '[.repos[].repo] == ["guarded"]' "[.repos[0].changes[].change_id] == [\"$B\", \"$R\", \"$X\", \"$X2\", \"$D2\", \"$E2\"]" \
389 '[.repos[0].changes[].handle] == ["mmmm", "nxvp", "oooo", "pqpq", "xxxx", "yyyy"]' \
390 "[.repos[0].changes[] | select(.change_id == \"$X\" or .change_id == \"$X2\") | .versions[0] | [.landing, .landed_commit]] == [[\"not-landed\", null], [\"not-landed\", null]]"
391cd "$w/guarded"
392ok valley changes --json
393holds '[.repos[].repo] == ["guarded"]' '[.repos[0].changes[].handle] == ["mmmm", "nxvp", "oooo", "pqpq", "xxxx", "yyyy"]'
394# A qualified handle names the change in the repository it qualifies, and
395# a qualifier naming another repository is refused.
396ok valley-go resolve guarded:mmmm
397line "^change"$'\t'"$B"$'\t'"mmmm"$'\t'"1"$'\t'"$(git rev-parse g-dup)"$'\t'
398cd "$w/open"
399ok valley-go resolve open:mmmm
400line "^change"$'\t'"$B"$'\t'"mmmm"$'\t'"1"$'\t'"$(git rev-parse stack-b)"$'\t'
401if valley-go resolve origin:mmmm > "$w/out" 2> "$w/err"; then fail "open resolved a handle qualified by origin"; fi
402grep -q 'origin:mmmm names a change in origin, and this repository is open' "$w/err" || fail "origin:mmmm was refused without saying why"
403cd "$w/origin"
404ok valley-go resolve origin:mmmm
405line "^change"$'\t'"$B"$'\t'"mmmm"$'\t'"1"$'\t'"$(git rev-parse o-dup)"$'\t'
406ok valley status origin:mmmm
407cd "$w/guarded"
408if valley changes --json --repo nowhere > "$w/out" 2> "$w/err"; then fail "--repo took a project the host does not list"; fi
409grep -q 'lists no project nowhere' "$w/err" || fail "--repo nowhere was refused without saying why"
410cd "$w/open"
411
412# A repository whose main names no owning valley lists itself alone, and
413# says discovery is unavailable.
414git init --quiet --bare "$w/solo.git"
415git clone --quiet "$w/solo.git" "$w/solo" 2> /dev/null
416git -C "$w/solo" commit --quiet --allow-empty -m solo
417git -C "$w/solo" push --quiet origin main
418(cd "$w/solo" && valley changes --json --all) > "$w/out" 2> "$w/err" || fail "changes --all failed without discovery"
419holds '[.repos[].repo] == ["solo"]'
420grep -q 'discovery is unavailable' "$w/err" || fail "changes --all did not say discovery is unavailable"
421
422# A policy/valley that git would read as an option, or that names a remote
423# helper, is refused before git sees it, and nothing it names runs, even
424# where the caller's own configuration allows ext::.
425for url in "--upload-pack=touch $w/pwned; git-upload-pack" "ext::sh -c touch% $w/pwned"; do
426 rm -rf "$w/evil.git" "$w/evil"
427 git init --quiet --bare "$w/evil.git"
428 git clone --quiet "$w/evil.git" "$w/evil" 2> /dev/null
429 mkdir -p "$w/evil/policy"
430 printf '%s\n' "$url" > "$w/evil/policy/valley"
431 git -C "$w/evil" add policy
432 git -C "$w/evil" commit --quiet -m evil
433 git -C "$w/evil" push --quiet origin main
434 export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=protocol.ext.allow GIT_CONFIG_VALUE_0=always
435 (cd "$w/evil" && valley changes --json --all) > "$w/out" 2> "$w/err" || fail "changes --all failed over policy/valley $url"
436 holds '[.repos[].repo] == ["evil"]'
437 grep -q 'discovery is unavailable' "$w/err" || fail "policy/valley $url was not refused"
438 if (cd "$w/evil" && valley changes --json --repo other) > "$w/out" 2> "$w/err"; then
439 fail "--repo read through policy/valley $url"
440 fi
441 unset GIT_CONFIG_COUNT GIT_CONFIG_KEY_0 GIT_CONFIG_VALUE_0
442 [ ! -e "$w/pwned" ] || fail "policy/valley $url ran a command"
443done
444
445# ----------------------------------------------------------------------
446# pending: a block per entry, in three sections. A stack is its top
447# entry, then every entry the top contains, as one list, each counted from
448# the entry it contains. atop-lost is a branch no record is on, holding U,
449# a ready change. a-parked is a branch last committed to eleven days ago.
450# deep-4 tops a chain of four branches. fork-left and fork-right are two
451# branches on one base, fork-base. The chain and the fork were last
452# committed to twenty days ago.
453branch atop-lost lost atop-lost
454parked="@$(($(date +%s) - 11 * 86400))"
455GIT_AUTHOR_DATE="$parked" GIT_COMMITTER_DATE="$parked" branch a-parked "$base" a-parked
456old="@$(($(date +%s) - 20 * 86400))"
457export GIT_AUTHOR_DATE="$old" GIT_COMMITTER_DATE="$old"
458branch deep-1 "$base" deep-1
459branch deep-2 deep-1 deep-2
460branch deep-3 deep-2 deep-3
461branch deep-4 deep-3 deep-4
462branch fork-base "$base" fork-base
463branch fork-left fork-base fork-left
464branch fork-right fork-base fork-right
465unset GIT_AUTHOR_DATE GIT_COMMITTER_DATE
466ok valley pending
467no_line $'\x1b'
468# pending also writes the handle cache completion reads: one line for each
469# change not landed or withdrawn, ready changes first.
470cache="$(git rev-parse --git-common-dir)/valley/handles"
471[ -f "$cache" ] || fail "pending wrote no handle cache at $cache"
472cut -f1,2 "$cache" > "$w/out"
473[ "$(cat "$w/out")" = "$(printf '%s\t%s\n' mmmm ready nxvp ready pppp ready ssss ready vvvv ready wwww ready lmlm draft qqqq draft zzzz draft)" ] ||
474 fail "the handle cache lists other changes than the ready and the drafts"
475grep -qE "^pppp"$'\t'"ready"$'\t'"[^"$'\t'"]*"$'\t'"stack-c$" "$cache" || fail "the handle cache has no line for pppp with its title"
476grep -qE "^qqqq"$'\t'"draft"$'\t'"paused"$'\t' "$cache" || fail "the handle cache has no line for qqqq with its branch"
477ok valley pending
478line '^ready$'
479# The stack of three, outermost first, each layer counted on its own.
480after '^pppp v1 +[0-9]+ [a-z]+ ago · valley-changes-check$' '^ stack-c$' '^ 1 commit · 1 file \+1 −0$' \
481 '^ approving it lands the 2 below too$' \
482 '^ ├ mmmm v1 +.* ago · valley-changes-check$' '^ │ stack-b$' '^ │ 1 commit · 1 file \+1 −0$' \
483 '^ └ nxvp v1 +.* ago · valley-changes-check$' '^ stack-a$' '^ 1 commit · 1 file \+1 −0$' '^$'
484# W includes E, which is withdrawn, so it is counted from main.
485after '^ssss v1 ' '^ atop-gone$' '^ 2 commits · 2 files \+2 −0$' '^ includes rrrr \(withdrawn\)$' '^$'
486after '^wwww v1 ' '^ forged$' '^ 1 commit · 1 file \+1 −0$' '^ uncertain: it may have landed without its evidence being stored$'
487no_line '(tttt|uuuu|rrrr) v1'
488no_line 'qqqq v1 '
489# The branches, latest first, and atop-lost with U listed under it,
490# marked as ready.
491after '^branches$' '^atop-lost +.* ago · valley-changes-check$' '^ atop-lost$' '^ 1 commit · 1 file \+1 −0$' \
492 '^ approving it lands the one below too$' \
493 '^ └ vvvv v1 \(ready\) +.* ago · valley-changes-check$' '^ lost$' '^ 1 commit · 1 file \+1 −0$' \
494 '^ uncertain: ' '^$' '^loose +.* ago · valley-changes-check$' '^ loose$' '^ 1 commit · 1 file \+1 −0$' '^$' \
495 '^a-parked +11 days ago · valley-changes-check$' '^ a-parked$' '^ 1 commit · 1 file \+1 −0$' '^$' '^deep-4 '
496# A chain four deep is one flat list, with no on line: each entry is on
497# the one listed after it.
498after '^deep-4 +3 weeks ago · valley-changes-check$' '^ deep-4$' '^ 1 commit · 1 file \+1 −0$' \
499 '^ approving it lands the 3 below too$' \
500 '^ ├ deep-3 +3 weeks ago · valley-changes-check$' '^ │ deep-3$' '^ │ 1 commit · 1 file \+1 −0$' \
501 '^ ├ deep-2 +3 weeks ago · valley-changes-check$' '^ │ deep-2$' '^ │ 1 commit · 1 file \+1 −0$' \
502 '^ └ deep-1 +3 weeks ago · valley-changes-check$' '^ deep-1$' '^ 1 commit · 1 file \+1 −0$' '^$'
503no_line '^ *(│ *)?on deep-'
504# Of two branches on one base, one lists the base, and the other says it
505# is on it. The base is listed once.
506[ "$(grep -cE '(^|├ |└ )fork-base ' "$w/out")" = 1 ] || fail "pending did not list fork-base exactly once"
507[ "$(grep -cx ' on fork-base' "$w/out")" = 1 ] || fail "not exactly one of the fork says it is on fork-base"
508after '^fork-left ' '^ fork-left$' '^ 1 commit · 1 file \+1 −0$' '^ approving it lands the one below too$' \
509 '^ └ fork-base ' '^ fork-base$' '^ 1 commit · 1 file \+1 −0$' '^$'
510after '^fork-right ' '^ fork-right$' '^ 1 commit · 1 file \+1 −0$' '^ on fork-base$' '^$' '^drafts$'
511after '^qqqq v1 on paused +.* ago · valley-changes-check$' '^ paused$' '^ 1 commit · 1 file \+1 −0$'
512after '^zzzz v2 on again ' '^ again$' '^ 1 commit · '
513after '^lmlm v2 on ffrec ' '^ ffrec$' '^ 1 commit · 1 file \+0 −1$'
514[ "$(grep -n '^ready$' "$w/out" | cut -d: -f1)" -lt "$(grep -n '^branches$' "$w/out" | cut -d: -f1)" ] ||
515 fail "pending listed the branches before the ready changes"
516# --json is the same listing in valley-changes/v1's shape: the pending
517# changes alone in changes, and the drafts in drafts.
518ok valley pending --json
519holds '.schema == "valley-pending/v1"' \
520 '[.repos[0].changes[].handle] == ["mmmm", "nxvp", "pppp", "ssss", "vvvv", "wwww"]' \
521 '[.repos[0].changes[].state] | unique == ["ready"]' \
522 '[.repos[0].drafts[] | [.handle, .state]] == [["lmlm", "draft"], ["qqqq", "draft"], ["zzzz", "draft"]]' \
523 '[.repos[0].branches[].branch] == ["a-parked", "atop-lost", "deep-1", "deep-2", "deep-3", "deep-4", "fork-base", "fork-left", "fork-right", "loose"]'
524# Across the host, each repository is headed by its name, and handles
525# are qualified where a change ID is recorded in more than one. In origin,
526# a branch no record is on holds the one ready change, so the ready
527# section says where it is.
528(cd "$w/origin" && branch atop-dup o-dup atop-dup)
529ok valley pending --all
530after '^guarded$' '^(─)+$' '^ready$'
531after '^nxvpwlkz v1 ' '^ g-ready$' '^ 1 commit · 1 file \+1 −0$'
532after '^guarded:mmmm v1 ' '^ g-dup$'
533after '^pqpq v1 ' '^ reversed$' '^ no commits · ' '^ approving it lands the one below too$' '^ └ oooo v1 ' \
534 '^ dropped$' '^ no commits · no file changes$'
535line '^xxxx v1 on g-draft '
536line '^open$'
537after '^pppp v1 ' '^ stack-c$' '^ 1 commit · ' '^ approving it lands the 2 below too$' '^ ├ open:mmmm v1 ' \
538 '^ │ stack-b$' '^ │ 1 commit · ' '^ └ nxvpwlkm v1 ' '^ stack-a$'
539after '^origin$' '^(─)+$' '^nothing is ready on its own; 1 ready change is in a stack under branches$' '^$' \
540 '^branches$' '^atop-dup ' '^ atop-dup$' '^ 1 commit · ' '^ approving it lands the one below too$' \
541 '^ └ origin:mmmm v1 \(ready\) ' '^ o-dup$'
542line '^released: could not read '
543after '^sealed$' '^(─)+$' '^nothing is ready$'
544
545# --checks adds a line to each change with the checks it requires, by
546# cost, as valley checks derives them, here and in a repository read
547# through its clone. The instance gets a floor for that, written to its
548# main on disk as the integrator would.
549git init --quiet "$w/floor"
550mkdir -p "$w/floor/policy/instance"
551cp "$floor"/*.cue "$w/floor/policy/instance/"
552git -C "$w/floor" add policy
553git -C "$w/floor" commit --quiet -m floor
554git -C "$w/floor" push --quiet "$instance" HEAD:refs/heads/floor
555git -C "$instance" update-ref refs/heads/main "$(git -C "$w/floor" rev-parse HEAD)"
556ok valley pending --checks --all
557after '^pppp v1 ' '^ stack-c$' '^ 1 commit · ' '^ requires 1 quick check, 0 slow$' '^ approving it lands the 2 below too$' \
558 '^ ├ open:mmmm v1 ' '^ │ stack-b$' '^ │ 1 commit · ' '^ │ requires 1 quick check, 0 slow$' '^ └ nxvpwlkm v1 ' \
559 '^ stack-a$' '^ 1 commit · ' '^ requires 1 quick check, 0 slow$'
560after '^nxvpwlkz v1 ' '^ g-ready$' '^ 1 commit · ' '^ requires 1 quick check, 0 slow$'
561
562# ----------------------------------------------------------------------
563# A branch name is used again once its change has landed: ff now names no
564# change, and reparent none either, once its transfer statement verifies.
565ok valley-go resolve ff
566line "^branch"$'\t'"ff$"
567ok valley-go resolve reparent
568line "^branch"$'\t'"reparent$"
569env -u VALLEY_KNOWN_SIGNERS valley-go resolve reparent > "$w/out" 2> "$w/err" || fail "resolve failed"
570line "^change"$'\t'"$G"$'\t'
571ok valley-go resolve stack-a
572line "^change"$'\t'"$A"$'\t'
573
574# H' has not landed, so its branch still names I, which is a draft, and
575# approving the branch is refused for I's state rather than taken as a
576# plain branch.
577ok valley-go resolve again
578line "^change"$'\t'"$I"$'\t'"zzzz"$'\t'"2"$'\t'"$hprime"$'\t'"again"$'\t'"draft"$'\t'"alice$"
579if valley approve again > "$w/out" 2> "$w/err"; then fail "approve took the branch of a draft"; fi
580grep -q "change zzzz v2 is draft, not ready" "$w/err" || fail "approve did not refuse the draft for its state"
581
582# J' is not J's landing replayed, so its branch names J, a draft, too.
583ok valley-go resolve ffrec
584line "^change"$'\t'"$J"$'\t'"lmlm"$'\t'"2"$'\t'"$jprime"$'\t'"ffrec"$'\t'"draft"$'\t'"alice$"
585if valley approve ffrec > "$w/out" 2> "$w/err"; then fail "approve took the branch of a replayed landing"; fi
586grep -q "change lmlm v2 is draft, not ready" "$w/err" || fail "approve did not refuse J' for its state"
587
588# A landed change names its branch no more once the branch moves on to new
589# work: ff is a branch, free for that work.
590git checkout --quiet -b ff-again ff
591echo more > more.txt
592git add more.txt
593git commit --quiet -m more
594git push --quiet origin ff-again:ff
595ok valley-go resolve ff
596line "^branch"$'\t'"ff$"
597
598# ----------------------------------------------------------------------
599# The listings name the knots that link each change: every knot in its
600# project's plan graph whose changes lists valley:<project>/<change ID>,
601# active knots first. The graph is written through the hook. nxvp is
602# linked from two knots, one of them closed, mmmm from one, pppp from
603# none, and the draft qqqq from the closed one. mmmm's change ID is linked
604# in guarded's name too, and guarded has no graph, so its mmmm has none.
605ok valley knot new outcome "The stack lands" --json
606goal="$(jq -r .knot.id "$w/out")"
607ok valley knot new bug "The stack reads the wrong base" --json
608bug="$(jq -r .knot.id "$w/out")"
609ok valley knot link "$goal" --change "valley:open/$A" --change "valley:open/$B"
610ok valley knot link "$bug" --change "valley:open/$A" --change "valley:open/$D" --change "valley:guarded/$B"
611ok valley knot set "$bug" status=closed
612goal_ref="[\"$goal\", \"outcome\", \"open\", \"The stack lands\"]"
613bug_ref="[\"$bug\", \"bug\", \"closed\", \"The stack reads the wrong base\"]"
614k() { printf '[.repos[0].%s[] | select(.change_id == "%s") | .knots[] | [.id, .type, .status, .title]]' "$1" "$2"; }
615ok valley changes --json
616holds "$(k changes "$A") == [$goal_ref, $bug_ref]" "$(k changes "$B") == [$goal_ref]" "$(k changes "$C") == []" \
617 "$(k changes "$D") == [$bug_ref]" '[.repos[0].changes[] | .knots | type] | unique == ["array"]'
618if grep -q 'plan graph' "$w/err"; then fail 'changes spoke of the plan graph without -v'; fi
619ok valley pending --json
620holds "$(k changes "$A") == [$goal_ref, $bug_ref]" "$(k changes "$C") == []" "$(k drafts "$D") == [$bug_ref]"
621# pending shows the first knot's ID after the title.
622ok valley pending
623after '^pppp v1 ' '^ stack-c$' '^ 1 commit · ' '^ approving it lands the 2 below too$' '^ ├ mmmm v1 ' \
624 "^ │ stack-b · $goal\$" '^ │ 1 commit · ' '^ └ nxvp v1 ' "^ stack-a · $goal\$"
625after '^qqqq v1 on paused ' "^ paused · $bug\$"
626ok valley status --json nxvp
627holds "[.knots[] | [.id, .type, .status, .title]] == [$goal_ref, $bug_ref]"
628ok valley status --json pppp
629holds '.knots == []'
630ok valley status --json loose
631holds '.change_id == null and .knots == []'
632ok valley changes --json --all
633holds "[.repos[] | select(.repo == \"open\") | .changes[] | select(.change_id == \"$B\") | .knots[].id] == [\"$goal\"]" \
634 "[.repos[] | select(.repo == \"guarded\") | .changes[] | select(.change_id == \"$B\") | .knots] == [[]]"
635if grep -q 'plan graph' "$w/err"; then fail 'changes --all spoke of a missing plan graph without -v'; fi
636ok valley changes --json --repo guarded -v
637grep -qF 'guarded has no plan graph at refs/the-valley/knots, so no change shows a knot' "$w/err" \
638 || fail 'changes -v did not say guarded has no plan graph'
639# A graph that does not read links no change, and the listings go on as
640# before. Only -v says why.
641graph_tip="$(git -C "$data/open.git" rev-parse refs/the-valley/knots)"
642junk="$(printf 'junk\n' | git -C "$data/open.git" hash-object -w --stdin)"
643junk_tree="$(printf '100644 blob %s\tjunk.txt\n' "$junk" | git -C "$data/open.git" mktree)"
644git -C "$data/open.git" update-ref refs/the-valley/knots "$(git -C "$data/open.git" commit-tree "$junk_tree" -m junk)"
645ok valley changes --json
646holds '(.repos[0].changes | length) == 12' '[.repos[0].changes[].knots] | unique == [[]]'
647if grep -q 'plan graph' "$w/err"; then fail 'changes spoke of a broken plan graph without -v'; fi
648ok valley pending
649line '^ stack-a$'
650no_line " · ($goal|$bug)\$"
651ok valley status --json nxvp
652holds '.knots == []'
653ok valley changes --json -v
654grep -qF "open's plan graph could not be read, so no change shows a knot" "$w/err" \
655 || fail 'changes -v did not say why it shows no knot'
656git -C "$data/open.git" update-ref refs/the-valley/knots "$graph_tip"
657
658# valley help names the verbs front ends look for.
659valley help > "$w/out"
660line '^ ready '
661line '^ changes '
662
663touch "$out"