gunk-dev/the-valley

valley-changes.sh

source71e69b2e4721main's published tip history
24.8 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# The host: guarded, open, released and sealed, with sealed the instance
56# repository. valley-init creates them, wires their hooks, and publishes
57# the project list in sealed.
58data="$w/srv/git"
59mkdir -p "$data"
60sed "s|/srv/git|$data|g" "$initScriptPath" > "$w/init.sh"
61bash "$w/init.sh" 2> "$w/init.err" || fail "valley-init failed: $(cat "$w/init.err")"
62instance="$data/sealed.git"
63# The integrator's repositories are group-shared, and git then sets the
64# setgid bit on every directory it makes, which this sandbox refuses. The
65# setting is dropped so the pushes below can write objects; it decides
66# nothing this check reads.
67for name in guarded open released sealed; do
68 git -C "$data/$name.git" config --unset core.sharedRepository || true
69done
70[ "$(git -C "$instance" cat-file blob refs/the-valley/projects)" = "$(printf '%s\n' guarded open released sealed)" ] ||
71 fail "valley-init published another project list"
72
73# A change's ID, by the letter it repeats. Each is 32 letters from k to z.
74id() { printf '%s%s' "$1" "$(printf 'k%.0s' $(seq $((32 - ${#1}))))"; }
75A="$(id nxvpwlkm)" B="$(id mmmm)" C="$(id pppp)" D="$(id qqqq)" E="$(id rrrr)"
76W="$(id ssss)" F="$(id tttt)" G="$(id uuuu)" U="$(id vvvv)" H="$(id wwww)"
77X="$(id oooo)" I="$(id zzzz)" J="$(id lmlm)" X2="$(id pqpq)"
78R="$(id nxvpwlkz)" D2="$(id xxxx)" E2="$(id yyyy)"
79
80# A checkout of a project, whose main names sealed as its owning valley.
81# Every push carries the principal, as the key sshd authenticated would.
82export VALLEY_PRINCIPAL=klaus-env
83checkout() {
84 local name="$1" dir="$w/$1"
85 git clone --quiet "$data/$name.git" "$dir" 2> /dev/null
86 git -C "$dir" config valley.principal klaus-env
87 mkdir -p "$dir/policy"
88 printf '# the owning valley\n%s\n' "$instance" > "$dir/policy/valley"
89 git -C "$dir" add policy/valley
90 git -C "$dir" commit --quiet -m base
91 VALLEY_PRINCIPAL=integrator git -C "$dir" push --quiet origin main
92}
93
94# branch <name> <from> <subject…>: a branch from a commit, with a commit
95# per subject, each adding a file of its own, pushed to origin.
96branch() {
97 local name="$1" from="$2" s
98 shift 2
99 git checkout --quiet -b "$name" "$from"
100 for s in "$@"; do
101 echo "$s" > "$s.txt"
102 git add "$s.txt"
103 git commit --quiet -m "$s"
104 done
105 git push --quiet origin "$name"
106}
107
108# ready <id> <branch>: the branch's head readied as the change.
109ready() {
110 ok valley ready --change "$1" "$2"
111}
112
113# ----------------------------------------------------------------------
114# open: a stack of three, a draft, a withdrawn change with a ready change
115# on top of it, a change landed by fast-forward, one landed as a new
116# commit with its transfer statement, one whose request was consumed with
117# no evidence stored, one whose transfer statement another key signed,
118# and a branch no record is on.
119checkout open
120cd "$w/open"
121base="$(git rev-parse HEAD)"
122branch stack-a "$base" stack-a
123ready "$A" stack-a
124branch stack-b stack-a stack-b
125ready "$B" stack-b
126branch stack-c stack-b stack-c
127ready "$C" stack-c
128branch paused "$base" paused
129ready "$D" paused
130ok valley unready "$D"
131branch gone "$base" gone
132ready "$E" gone
133ok valley withdraw "$E"
134branch atop-gone gone atop-gone
135ready "$W" atop-gone
136branch ff "$base" ff
137ready "$F" ff
138branch reparent "$base" reparent
139ready "$G" reparent
140branch lost "$base" lost
141ready "$U" lost
142branch forged "$base" forged
143ready "$H" forged
144branch loose "$base" loose
145
146# Before anything lands, ff names its change.
147ok valley-go resolve ff
148line "^change"$'\t'"$F"$'\t'
149
150# The integrator's keys: the integrator's own, and another a known
151# signer holds, which may sign evidence and not a landing.
152ssh-keygen -q -t ed25519 -N '' -C integrator -f "$w/integrator.key"
153ssh-keygen -q -t ed25519 -N '' -C mallory -f "$w/mallory.key"
154{
155 attest key --key "$w/integrator.key" --name integrator
156 attest key --key "$w/mallory.key" --name mallory
157} > "$w/known-signers"
158
159# F lands by fast-forward.
160VALLEY_PRINCIPAL=integrator git push --quiet origin ff:main
161fhead="$(git rev-parse ff)"
162
163# G lands as a new commit on main, and the integrator records it: main
164# moves, the outcome record says land, the request is consumed, and the
165# transfer statement is stored under the landed tree.
166git checkout --quiet -b landing ff
167git cherry-pick reparent > /dev/null
168landed="$(git rev-parse HEAD)"
169VALLEY_PRINCIPAL=integrator git push --quiet origin landing:main
170tree_digest() { attest digest --repo "$w/open" --rev "$1" | cut -d: -f2; }
171
172# memo <name> <head> <decision…>: the integrator's outcome record.
173memo() {
174 local name="$1" head="$2" blob
175 shift 2
176 blob="$(printf '%s %s %s %s\n' "$head" "$fhead" 0123456789abcdef "$*" | git -C "$data/open.git" hash-object -w --stdin)"
177 git -C "$data/open.git" update-ref "refs/the-valley/integration-outcomes/main/$name" "$blob"
178}
179
180# transfer <change> <head> <landed> <key> <name>: a transfer statement,
181# as the integrator composes, signs and stores one.
182transfer() {
183 local change="$1" head="$2" at="$3" key="$4" name="$5" ldigest blob tree
184 ldigest="$(tree_digest "$at")"
185 jq -n --arg change "$change" --arg base "$(tree_digest "$head")" --arg landed "$ldigest" \
186 --arg commit "$at" --arg tip "$fhead" '{
187 statementType: "the-valley/attestation/v1",
188 subject: {primary: "valley-tree-v1", digest: {"valley-tree-v1": $landed, "git-sha1": $commit}},
189 predicateType: "the-valley/integration/transfer/v1",
190 predicate: {change: $change, target: "refs/heads/main", base: {"valley-tree-v1": $base},
191 policy: {sha256: ("ab" * 32), "git-sha1": $tip}, required: "none"}
192 }' > "$w/statement.json"
193 attest render "$w/statement.json" | attest sign --key "$key" --name "$name" > "$w/transfer.note"
194 blob="$(git -C "$data/open.git" hash-object -w "$w/transfer.note")"
195 tree="$(printf '100644 blob %s\ttransfer.note\n' "$blob" | git -C "$data/open.git" mktree)"
196 git -C "$data/open.git" update-ref "refs/the-valley/attestations/$ldigest/$(printf '%08x' "${#name}")" "$tree"
197}
198memo reparent "$(git rev-parse reparent)" land
199transfer reparent "$(git rev-parse reparent)" "$landed" "$w/integrator.key" integrator
200
201# U's request was consumed, and no evidence was stored.
202memo lost "$(git rev-parse lost)" land
203# H's request was consumed too, and its transfer statement is signed by a
204# known key that is not the integrator's: it is no record of a landing.
205memo forged "$(git rev-parse forged)" land
206git checkout --quiet -b landing-h landing
207git cherry-pick forged > /dev/null
208VALLEY_PRINCIPAL=integrator git push --quiet origin landing-h:main
209transfer forged "$(git rev-parse forged)" "$(git rev-parse HEAD)" "$w/mallory.key" mallory
210# A's request is pending.
211git -C "$data/open.git" update-ref refs/the-valley/integration-requests/main/stack-a "$(git rev-parse stack-a)"
212
213# I lands as a new commit, L2, filed as c-<I>, and the integrator records
214# it. Then I's next version, H', has H's tree with L2 as its parent. Its
215# name and its tree digest match the transfer statement, but it is another
216# delta: merged onto L2's parent it does not give L2's tree. I is then made
217# a draft.
218branch again "$base" again
219ready "$I" again
220hhead="$(git rev-parse again)"
221git checkout --quiet -b landing-i landing-h
222git cherry-pick again > /dev/null
223landed2="$(git rev-parse HEAD)"
224VALLEY_PRINCIPAL=integrator git push --quiet origin landing-i:main
225memo "c-$I" "$hhead" land
226transfer "c-$I" "$hhead" "$landed2" "$w/integrator.key" integrator
227hprime="$(git commit-tree "again^{tree}" -p "$landed2" -m again)"
228git push --quiet --force origin "$hprime:refs/heads/again"
229ready "$I" again
230ok valley unready "$I"
231
232# J lands by fast-forward, and the integrator records it: its outcome
233# record says land at J's head, and its transfer statement is about J's
234# tree. Then main moves on to Q, which adds a file, and J's next version,
235# J', has J's tree with Q as its parent: it deletes Q's file. J' matches
236# the statement's name and tree, and merged onto the parent of the commit
237# that landed it gives that commit's tree. But the outcome record names
238# J's head, not J', so J' has not landed. J is then made a draft.
239branch ffrec landing-i ffrec
240ready "$J" ffrec
241jhead="$(git rev-parse ffrec)"
242VALLEY_PRINCIPAL=integrator git push --quiet origin ffrec:main
243memo ffrec "$jhead" land
244transfer ffrec "$jhead" "$jhead" "$w/integrator.key" integrator
245git checkout --quiet -b moved-on ffrec
246echo q > q.txt
247git add q.txt
248git commit --quiet -m q
249VALLEY_PRINCIPAL=integrator git push --quiet origin moved-on:main
250jprime="$(git commit-tree "ffrec^{tree}" -p moved-on -m ffrec)"
251git push --quiet --force origin "$jprime:refs/heads/ffrec"
252ready "$J" ffrec
253ok valley unready "$J"
254
255# ----------------------------------------------------------------------
256# guarded: a ready change whose ID shares seven letters with A's, a draft
257# and a withdrawn change.
258checkout guarded
259cd "$w/guarded"
260gbase="$(git rev-parse HEAD)"
261branch g-ready "$gbase" g-ready
262ready "$R" g-ready
263branch g-draft "$gbase" g-draft
264ready "$D2" g-draft
265ok valley unready "$D2"
266branch g-gone "$gbase" g-gone
267ready "$E2" g-gone
268ok valley withdraw "$E2"
269# B's change ID, recorded here too.
270branch g-dup "$gbase" g-dup
271ready "$B" g-dup
272
273# A merge on main can drop the content of either parent, so no head below
274# one has landed by ancestry. X is ready, and a merge of it whose tree
275# drops its changes lands on main by fast-forward: M = (main, X), with
276# main's tree. Then X2, branched from that main, is dropped by a merge with
277# its parents reversed: M2 = (X2, main), with main's tree. X2 is on main's
278# first-parent history, below the merge that dropped it.
279branch dropped "$gbase" dropped
280ready "$X" dropped
281merge="$(git commit-tree "$gbase^{tree}" -p "$gbase" -p dropped -m 'merge, dropping X')"
282VALLEY_PRINCIPAL=integrator git push --quiet origin "$merge:refs/heads/main"
283branch reversed "$merge" reversed
284ready "$X2" reversed
285merge2="$(git commit-tree "$merge^{tree}" -p reversed -p "$merge" -m 'merge, dropping X2')"
286VALLEY_PRINCIPAL=integrator git push --quiet origin "$merge2:refs/heads/main"
287
288# origin: a project named origin, recording B's change ID too. A host that
289# declared it would list it, so it is added to the list on disk, as
290# valley-init writes the list.
291git init --quiet --bare "$data/origin.git"
292checkout origin
293cd "$w/origin"
294branch o-dup "$(git rev-parse HEAD)" o-dup
295ready "$B" o-dup
296projects="$(printf '%s\n' guarded open origin released sealed | git -C "$instance" hash-object -w --stdin)"
297git -C "$instance" update-ref refs/the-valley/projects "$projects"
298
299# released is listed and cannot be read.
300mv "$data/released.git" "$data/released.moved"
301
302export VALLEY_KNOWN_SIGNERS="$w/known-signers" VALLEY_INTEGRATOR_SIGNER=integrator
303
304# ----------------------------------------------------------------------
305# One repository: every record in every state, as valley-changes/v1.
306cd "$w/open"
307ok valley changes --json
308[ "$(wc -l < "$w/out")" = 1 ] || fail "changes printed more than one line"
309holds '.schema == "valley-changes/v1"' '.host == "localhost"' '(.repos | length) == 1' \
310 '.repos[0].repo == "open"' ".repos[0].url == \"$data/open.git\"" '.repos[0].error == null' \
311 '(.repos[0].changes | length) == 12' '.repos[0] | has("drafts") | not'
312c() { printf '.repos[0].changes[] | select(.change_id == "%s")' "$1"; }
313holds "$(c "$A") | .handle == \"nxvp\" and .state == \"ready\" and .owner == \"klaus-env\" and .target == \"main\" and .active == 1 and .seq == 1" \
314 "$(c "$A") | .record == \"$(git -C "$data/open.git" rev-parse "refs/the-valley/changes/main/$A")\"" \
315 "$(c "$A") | .versions[0] | .n == 1 and .title == \"stack-a\" and .branch == \"stack-a\" and .uploader == \"klaus-env\"" \
316 "$(c "$A") | .versions[0] | .head == \"$(git rev-parse stack-a)\" and .landing == \"not-landed\" and .landed_commit == null" \
317 "$(c "$A") | .versions[0].request == {ref: \"refs/the-valley/integration-requests/main/stack-a\", commit: \"$(git rev-parse stack-a)\"}" \
318 "$(c "$A") | .versions[0].outcome == null" \
319 "$(c "$D") | .state == \"draft\" and .seq == 2" \
320 "$(c "$E") | .state == \"withdrawn\""
321# Landing evidence, one version each way.
322holds "$(c "$F") | .versions[0] | .landing == \"landed\" and .landed_commit == \"$fhead\"" \
323 "$(c "$G") | .versions[0] | .landing == \"landed-recorded\" and .landed_commit == \"$landed\"" \
324 "$(c "$G") | .versions[0].outcome == {decision: \"land\", head: \"$(git rev-parse reparent)\", tip: \"$fhead\"}" \
325 "$(c "$G") | .versions[0].request == null" \
326 "$(c "$U") | .versions[0] | .landing == \"uncertain\" and .landed_commit == null" \
327 "$(c "$H") | .versions[0].landing == \"uncertain\"" \
328 "$(c "$J") | .versions[0] | .landing == \"landed\" and .landed_commit == \"$jhead\"" \
329 "$(c "$J") | .versions[1] | .head == \"$jprime\" and .landing == \"not-landed\" and .landed_commit == null" \
330 "$(c "$J") | .state == \"draft\" and .active == 2" \
331 "$(c "$I") | .versions[0] | .landing == \"landed-recorded\" and .landed_commit == \"$landed2\"" \
332 "$(c "$I") | .versions[1] | .head == \"$hprime\" and .landing == \"not-landed\" and .landed_commit == null" \
333 "$(c "$I") | .state == \"draft\" and .active == 2"
334# includes is ancestry, nearest only.
335holds "$(c "$A") | .includes == []" "$(c "$B") | .includes == [\"$A\"]" \
336 "$(c "$C") | .includes == [\"$B\"]" "$(c "$W") | .includes == [\"$E\"]"
337# The one branch no record is on, and no branch main reaches.
338holds '.repos[0].branches == [{branch: "loose", head: "'"$(git rev-parse loose)"'"}]'
339
340# Without the registry's keys, the transfer statement is not believed, and
341# that is said.
342env -u VALLEY_KNOWN_SIGNERS valley changes --json > "$w/out" 2> "$w/err" || fail "changes failed without the keys"
343holds "$(c "$G") | .versions[0].landing == \"uncertain\""
344grep -q 'could not be verified' "$w/err" || fail "an unverified transfer statement was not reported"
345
346# ----------------------------------------------------------------------
347# Every project the host publishes. Handles are unique across both
348# repositories, the repository that cannot be read carries its error, and
349# the others are listed all the same.
350ok valley changes --json --all
351holds '[.repos[].repo] == ["guarded", "open", "origin", "released", "sealed"]' \
352 '.repos[] | select(.repo == "released") | .error != null and .changes == [] and .url == "'"$data/released.git"'"' \
353 '.repos[] | select(.repo != "released") | .error == null' \
354 '.repos[] | select(.repo == "sealed") | .changes == []' \
355 '[.repos[].changes[].handle] | length == (unique | length)' \
356 "[.repos[].changes[] | select(.change_id == \"$A\" or .change_id == \"$R\") | .handle] == [\"nxvpwlkz\", \"nxvpwlkm\"]" \
357 "[.repos[].changes[] | select(.change_id == \"$B\") | .handle] == [\"guarded:mmmm\", \"open:mmmm\", \"origin:mmmm\"]" \
358 '.repos[] | select(.repo == "guarded") | [.changes[].state] == ["ready", "ready", "ready", "ready", "draft", "withdrawn"]'
359# ls-remote matches a ref's tail, so refs whose names end like the
360# refs discovery reads must not stand in for them: one in open whose
361# policy/valley names another instance, and one in the instance listing
362# another project.
363git checkout --quiet -b decoy "$base"
364printf '%s\n' "$w/elsewhere.git" > policy/valley
365git commit --quiet -am decoy
366git push --quiet origin decoy:refs/heads/a/refs/heads/main
367decoy_list="$(printf 'decoy\n' | git -C "$instance" hash-object -w --stdin)"
368git -C "$instance" update-ref refs/tags/a/refs/the-valley/projects "$decoy_list"
369git checkout --quiet landing
370ok valley changes --json --all
371holds '[.repos[].repo] == ["guarded", "open", "origin", "released", "sealed"]'
372git push --quiet origin :refs/heads/a/refs/heads/main
373git -C "$instance" update-ref -d refs/tags/a/refs/the-valley/projects
374# --repo limits the listing to one repository, read through its clone.
375ok valley changes --json --repo guarded
376holds '[.repos[].repo] == ["guarded"]' "[.repos[0].changes[].change_id] == [\"$B\", \"$R\", \"$X\", \"$X2\", \"$D2\", \"$E2\"]" \
377 '[.repos[0].changes[].handle] == ["mmmm", "nxvp", "oooo", "pqpq", "xxxx", "yyyy"]' \
378 "[.repos[0].changes[] | select(.change_id == \"$X\" or .change_id == \"$X2\") | .versions[0] | [.landing, .landed_commit]] == [[\"not-landed\", null], [\"not-landed\", null]]"
379cd "$w/guarded"
380ok valley changes --json
381holds '[.repos[].repo] == ["guarded"]' '[.repos[0].changes[].handle] == ["mmmm", "nxvp", "oooo", "pqpq", "xxxx", "yyyy"]'
382# A qualified handle names the change in the repository it qualifies, and
383# a qualifier naming another repository is refused.
384ok valley-go resolve guarded:mmmm
385line "^change"$'\t'"$B"$'\t'"mmmm"$'\t'"1"$'\t'"$(git rev-parse g-dup)"$'\t'
386cd "$w/open"
387ok valley-go resolve open:mmmm
388line "^change"$'\t'"$B"$'\t'"mmmm"$'\t'"1"$'\t'"$(git rev-parse stack-b)"$'\t'
389if valley-go resolve origin:mmmm > "$w/out" 2> "$w/err"; then fail "open resolved a handle qualified by origin"; fi
390grep -q 'origin:mmmm names a change in origin, and this repository is open' "$w/err" || fail "origin:mmmm was refused without saying why"
391cd "$w/origin"
392ok valley-go resolve origin:mmmm
393line "^change"$'\t'"$B"$'\t'"mmmm"$'\t'"1"$'\t'"$(git rev-parse o-dup)"$'\t'
394ok valley status origin:mmmm
395cd "$w/guarded"
396if valley changes --json --repo nowhere > "$w/out" 2> "$w/err"; then fail "--repo took a project the host does not list"; fi
397grep -q 'lists no project nowhere' "$w/err" || fail "--repo nowhere was refused without saying why"
398cd "$w/open"
399
400# A repository whose main names no owning valley lists itself alone, and
401# says discovery is unavailable.
402git init --quiet --bare "$w/solo.git"
403git clone --quiet "$w/solo.git" "$w/solo" 2> /dev/null
404git -C "$w/solo" commit --quiet --allow-empty -m solo
405git -C "$w/solo" push --quiet origin main
406(cd "$w/solo" && valley changes --json --all) > "$w/out" 2> "$w/err" || fail "changes --all failed without discovery"
407holds '[.repos[].repo] == ["solo"]'
408grep -q 'discovery is unavailable' "$w/err" || fail "changes --all did not say discovery is unavailable"
409
410# A policy/valley that git would read as an option, or that names a remote
411# helper, is refused before git sees it, and nothing it names runs, even
412# where the caller's own configuration allows ext::.
413for url in "--upload-pack=touch $w/pwned; git-upload-pack" "ext::sh -c touch% $w/pwned"; do
414 rm -rf "$w/evil.git" "$w/evil"
415 git init --quiet --bare "$w/evil.git"
416 git clone --quiet "$w/evil.git" "$w/evil" 2> /dev/null
417 mkdir -p "$w/evil/policy"
418 printf '%s\n' "$url" > "$w/evil/policy/valley"
419 git -C "$w/evil" add policy
420 git -C "$w/evil" commit --quiet -m evil
421 git -C "$w/evil" push --quiet origin main
422 export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=protocol.ext.allow GIT_CONFIG_VALUE_0=always
423 (cd "$w/evil" && valley changes --json --all) > "$w/out" 2> "$w/err" || fail "changes --all failed over policy/valley $url"
424 holds '[.repos[].repo] == ["evil"]'
425 grep -q 'discovery is unavailable' "$w/err" || fail "policy/valley $url was not refused"
426 if (cd "$w/evil" && valley changes --json --repo other) > "$w/out" 2> "$w/err"; then
427 fail "--repo read through policy/valley $url"
428 fi
429 unset GIT_CONFIG_COUNT GIT_CONFIG_KEY_0 GIT_CONFIG_VALUE_0
430 [ ! -e "$w/pwned" ] || fail "policy/valley $url ran a command"
431done
432
433# ----------------------------------------------------------------------
434# pending: the ready changes that have not landed, by handle, version and
435# title, then the branches no record is on, then the drafts.
436ok valley pending
437line '^nxvp v1 \| stack-a$'
438line '^mmmm v1 \| stack-b \| includes nxvp$'
439line '^pppp v1 \| stack-c \| includes mmmm$'
440line '^ssss v1 \| atop-gone \| includes rrrr$'
441line '^vvvv v1 \| lost \| uncertain$'
442line '^wwww v1 \| forged \| uncertain$'
443no_line '^(tttt|uuuu|rrrr|qqqq) v1 \|'
444line '^branches$'
445line '^loose \| loose \| .* \| ahead 1 \| 1 file changed'
446line '^drafts$'
447line '^qqqq v1 on paused \| paused \| .* \| ahead 1 \| '
448line '^zzzz v2 on again \| again \| '
449line '^lmlm v2 on ffrec \| ffrec \| '
450[ "$(grep -n '^branches$' "$w/out" | cut -d: -f1)" -lt "$(grep -n '^drafts$' "$w/out" | cut -d: -f1)" ] ||
451 fail "pending listed the drafts before the branches"
452# --json is the same listing in valley-changes/v1's shape: the pending
453# changes alone in changes, and the drafts in drafts.
454ok valley pending --json
455holds '.schema == "valley-pending/v1"' \
456 '[.repos[0].changes[].handle] == ["mmmm", "nxvp", "pppp", "ssss", "vvvv", "wwww"]' \
457 '[.repos[0].changes[].state] | unique == ["ready"]' \
458 '[.repos[0].drafts[] | [.handle, .state]] == [["lmlm", "draft"], ["qqqq", "draft"], ["zzzz", "draft"]]' \
459 '.repos[0].branches == [{branch: "loose", head: "'"$(git rev-parse loose)"'"}]'
460# Across the host, each repository is headed by its name.
461ok valley pending --all
462line '^guarded$'
463line '^nxvpwlkz v1 \| g-ready$'
464line '^guarded drafts$'
465line '^open$'
466line '^nxvpwlkm v1 \| stack-a$'
467line '^open:mmmm v1 \| stack-b \| includes nxvpwlkm$'
468line '^pppp v1 \| stack-c \| includes open:mmmm$'
469line '^guarded:mmmm v1 \| g-dup$'
470line '^oooo v1 \| dropped$'
471line '^pqpq v1 \| reversed \| includes oooo$'
472line '^origin$'
473line '^origin:mmmm v1 \| o-dup$'
474line '^released: could not read '
475
476# --owes ends each change's line with the checks it owes, by cost, as
477# valley checks derives them, here and in a repository read through its
478# clone. The instance gets a floor for that, written to its main on disk
479# as the integrator would.
480git init --quiet "$w/floor"
481mkdir -p "$w/floor/policy/instance"
482cp "$floor"/*.cue "$w/floor/policy/instance/"
483git -C "$w/floor" add policy
484git -C "$w/floor" commit --quiet -m floor
485git -C "$w/floor" push --quiet "$instance" HEAD:refs/heads/floor
486git -C "$instance" update-ref refs/heads/main "$(git -C "$w/floor" rev-parse HEAD)"
487ok valley pending --owes --all
488line '^nxvpwlkm v1 \| stack-a \| owes 1 quick, 0 slow$'
489line '^open:mmmm v1 \| stack-b \| includes nxvpwlkm \| owes 1 quick, 0 slow$'
490line '^nxvpwlkz v1 \| g-ready \| owes 1 quick, 0 slow$'
491
492# ----------------------------------------------------------------------
493# A branch name is used again once its change has landed: ff now names no
494# change, and reparent none either, once its transfer statement verifies.
495ok valley-go resolve ff
496line "^branch"$'\t'"ff$"
497ok valley-go resolve reparent
498line "^branch"$'\t'"reparent$"
499env -u VALLEY_KNOWN_SIGNERS valley-go resolve reparent > "$w/out" 2> "$w/err" || fail "resolve failed"
500line "^change"$'\t'"$G"$'\t'
501ok valley-go resolve stack-a
502line "^change"$'\t'"$A"$'\t'
503
504# H' has not landed, so its branch still names I, which is a draft, and
505# approving the branch is refused for I's state rather than taken as a
506# plain branch.
507ok valley-go resolve again
508line "^change"$'\t'"$I"$'\t'"zzzz"$'\t'"2"$'\t'"$hprime"$'\t'"again"$'\t'"draft$"
509if valley approve again > "$w/out" 2> "$w/err"; then fail "approve took the branch of a draft"; fi
510grep -q "change zzzz v2 is draft, not ready" "$w/err" || fail "approve did not refuse the draft for its state"
511
512# J' is not J's landing replayed, so its branch names J, a draft, too.
513ok valley-go resolve ffrec
514line "^change"$'\t'"$J"$'\t'"lmlm"$'\t'"2"$'\t'"$jprime"$'\t'"ffrec"$'\t'"draft$"
515if valley approve ffrec > "$w/out" 2> "$w/err"; then fail "approve took the branch of a replayed landing"; fi
516grep -q "change lmlm v2 is draft, not ready" "$w/err" || fail "approve did not refuse J' for its state"
517
518# A landed change names its branch no more once the branch moves on to new
519# work: ff is a branch, free for that work.
520git checkout --quiet -b ff-again ff
521echo more > more.txt
522git add more.txt
523git commit --quiet -m more
524git push --quiet origin ff-again:ff
525ok valley-go resolve ff
526line "^branch"$'\t'"ff$"
527
528# valley help names the verbs klaus looks for.
529valley help > "$w/out"
530line '^ ready '
531line '^ changes '
532
533touch "$out"