| 1 | # shellcheck shell=bash |
| 2 | # The status command reads an asynchronous integration result. Exercise it |
| 3 | # against real refs, including the gaps between updating main, recording an |
| 4 | # outcome, and consuming a request. No controller or timing assumption is |
| 5 | # needed to make those states observable. Every state is read twice: as |
| 6 | # the sentences an operator reads, and through --json, the valley-status/v1 |
| 7 | # object front ends parse. |
| 8 | # The derivation supplies its output path. |
| 9 | # shellcheck disable=SC2154 |
| 10 | set -euo pipefail |
| 11 | |
| 12 | w="$TMPDIR/valley-status" |
| 13 | mkdir -p "$w" |
| 14 | export GIT_CONFIG_GLOBAL="$w/gitconfig" |
| 15 | export GIT_CONFIG_NOSYSTEM=1 |
| 16 | git config --global user.name valley-status-check |
| 17 | git config --global user.email valley-status-check@localhost |
| 18 | git config --global init.defaultBranch main |
| 19 | git config --global advice.detachedHead false |
| 20 | |
| 21 | origin="$w/project.git" |
| 22 | repo="$w/project" |
| 23 | git init --quiet --bare "$origin" |
| 24 | git init --quiet "$repo" |
| 25 | git -C "$repo" remote add origin "$origin" |
| 26 | printf 'base\n' > "$repo/readme.txt" |
| 27 | git -C "$repo" add readme.txt |
| 28 | git -C "$repo" commit --quiet -m base |
| 29 | git -C "$repo" push --quiet origin main |
| 30 | base="$(git -C "$repo" rev-parse HEAD)" |
| 31 | git -C "$repo" checkout --quiet -b topic |
| 32 | printf 'topic\n' > "$repo/readme.txt" |
| 33 | git -C "$repo" commit --quiet -am topic |
| 34 | head="$(git -C "$repo" rev-parse HEAD)" |
| 35 | git -C "$repo" push --quiet origin topic |
| 36 | git -C "$repo" checkout --quiet main |
| 37 | git -C "$repo" fetch --quiet origin |
| 38 | |
| 39 | request=refs/the-valley/integration-requests/main/topic |
| 40 | memo=refs/the-valley/integration-outcomes/main/topic |
| 41 | evidence_ref="refs/the-valley/attestations/$(printf '%064d' 0)/test" |
| 42 | |
| 43 | fail() { |
| 44 | printf 'valley-status: %s\n' "$*" >&2 |
| 45 | if [ -f "$w/status.out" ]; then cat "$w/status.out" >&2; fi |
| 46 | if [ -f "$w/status.json" ]; then cat "$w/status.json" >&2; fi |
| 47 | exit 1 |
| 48 | } |
| 49 | |
| 50 | contains() { |
| 51 | grep -qiF -- "$1" "$w/status.out" || fail "$2" |
| 52 | } |
| 53 | |
| 54 | lacks() { |
| 55 | if grep -qiF -- "$1" "$w/status.out"; then fail "$2"; fi |
| 56 | } |
| 57 | |
| 58 | matches() { |
| 59 | grep -qiE -- "$1" "$w/status.out" || fail "$2" |
| 60 | } |
| 61 | |
| 62 | evidence_state() { |
| 63 | git -C "$origin" for-each-ref --format='%(refname) %(objectname)' \ |
| 64 | refs/the-valley/attestations | sha256sum | cut -c1-16 |
| 65 | } |
| 66 | |
| 67 | record_outcome() { |
| 68 | local blob |
| 69 | blob="$(printf '%s %s %s %s\n' "$1" "$2" "$(evidence_state)" "$3" | |
| 70 | git -C "$origin" hash-object -w --stdin)" |
| 71 | git -C "$origin" update-ref "$memo" "$blob" |
| 72 | } |
| 73 | |
| 74 | reset_remote() { |
| 75 | git -C "$origin" update-ref refs/heads/main "$base" |
| 76 | git -C "$origin" update-ref refs/heads/topic "$head" |
| 77 | git -C "$origin" update-ref -d "$request" |
| 78 | git -C "$origin" update-ref -d "$memo" |
| 79 | git -C "$origin" update-ref -d "$evidence_ref" |
| 80 | } |
| 81 | |
| 82 | # A read must preserve every local branch and both kinds of uncommitted |
| 83 | # change. FETCH_HEAD is included because scripts and people may be using |
| 84 | # the preceding fetch result independently of status. |
| 85 | local_state() { |
| 86 | git -C "$repo" symbolic-ref -q HEAD || true |
| 87 | git -C "$repo" rev-parse HEAD |
| 88 | git -C "$repo" for-each-ref --format='%(refname) %(objectname)' refs/heads |
| 89 | git -C "$repo" status --porcelain=v1 |
| 90 | git -C "$repo" diff --binary |
| 91 | git -C "$repo" diff --cached --binary |
| 92 | if [ -f "$repo/.git/FETCH_HEAD" ]; then |
| 93 | sha256sum "$repo/.git/FETCH_HEAD" |
| 94 | fi |
| 95 | } |
| 96 | |
| 97 | status() { |
| 98 | local_state > "$w/before" |
| 99 | (cd "$repo" && valley status "${1:-topic}") > "$w/status.out" 2>&1 \ |
| 100 | || fail "status could not inspect the remote" |
| 101 | local_state > "$w/after" |
| 102 | diff -u "$w/before" "$w/after" || fail "status changed the local checkout or FETCH_HEAD" |
| 103 | } |
| 104 | |
| 105 | # The same read through --json: one object on one line, on stdout alone, |
| 106 | # and the local checkout left as it was. |
| 107 | status_json() { |
| 108 | local_state > "$w/before" |
| 109 | (cd "$repo" && valley status --json "${1:-topic}") > "$w/status.json" 2> "$w/status.err" \ |
| 110 | || fail "status --json could not inspect the remote" |
| 111 | local_state > "$w/after" |
| 112 | diff -u "$w/before" "$w/after" || fail "status --json changed the local checkout or FETCH_HEAD" |
| 113 | [ "$(wc -l < "$w/status.json")" = 1 ] || fail "status --json printed other than one line" |
| 114 | jq -e 'type == "object"' "$w/status.json" > /dev/null \ |
| 115 | || fail "status --json printed something other than one JSON object" |
| 116 | } |
| 117 | |
| 118 | # json <state> [<jq condition>...]: topic's state through --json, the |
| 119 | # fields every object carries, and each condition given. The ids in a |
| 120 | # condition are hex, so they are written into it directly. |
| 121 | json() { |
| 122 | local want="$1" tip got cond |
| 123 | shift |
| 124 | status_json |
| 125 | tip="$(git -C "$origin" rev-parse refs/heads/main)" |
| 126 | got="$(jq -r .state "$w/status.json")" |
| 127 | [ "$got" = "$want" ] || fail "status --json reported $got where $want was expected" |
| 128 | for cond in '.schema == "valley-status/v1"' '.change == "topic"' \ |
| 129 | '.request_name == "topic"' '.target == "main"' ".target_tip == \"$tip\"" "$@"; do |
| 130 | jq -e "$cond" "$w/status.json" > /dev/null || fail "status --json: $cond does not hold" |
| 131 | done |
| 132 | } |
| 133 | |
| 134 | # A read that cannot give a status prints no object at all: a front end |
| 135 | # must never parse half of one, or an empty one, as an answer. |
| 136 | json_refused() { |
| 137 | if (cd "$repo" && valley status --json "${1:-topic}") > "$w/status.json" 2> "$w/status.err"; then |
| 138 | fail "status --json succeeded where it had to refuse" |
| 139 | fi |
| 140 | [ ! -s "$w/status.json" ] || fail "a refused status --json printed to stdout" |
| 141 | [ -s "$w/status.err" ] || fail "a refused status --json did not say why" |
| 142 | } |
| 143 | |
| 144 | # No request is different from a request waiting to be judged. |
| 145 | reset_remote |
| 146 | status |
| 147 | contains 'not approved' 'an unapproved branch was not identified' |
| 148 | lacks 'integrated into origin/main' 'an unapproved branch was reported integrated' |
| 149 | json not-requested ".head == \"$head\"" '.request == null' '.outcome == null' |
| 150 | git -C "$origin" update-ref "$request" "$head" |
| 151 | status |
| 152 | contains 'awaiting' 'a new request was not reported as waiting' |
| 153 | contains 'decision' 'the pending message did not say what is awaited' |
| 154 | json awaiting-decision ".head == \"$head\"" \ |
| 155 | ".request == {\"ref\": \"$request\", \"commit\": \"$head\"}" '.outcome == null' |
| 156 | |
| 157 | # A current refusal is useful, but a verdict for an earlier input is not |
| 158 | # the current answer. Changing the target or adding evidence schedules a |
| 159 | # new controller decision without requiring another request. |
| 160 | record_outcome "$head" "$base" stale |
| 161 | status |
| 162 | contains 'needs attention' 'current staleness did not call for attention' |
| 163 | contains 'stale' 'current staleness lost the recorded outcome' |
| 164 | json stale ".outcome == {\"decision\": \"stale\", \"head\": \"$head\", \"tip\": \"$base\", \"failed_at\": null}" |
| 165 | record_outcome "$head" "$base" reject |
| 166 | status |
| 167 | contains 'rejected' 'a current rejection was not reported' |
| 168 | json rejected '.outcome.decision == "reject"' ".request.commit == \"$head\"" |
| 169 | # The integrator's refusals: a head that is a version of a change that is |
| 170 | # not ready, and commits that hold a merge. The request stands for both. |
| 171 | record_outcome "$head" "$base" refused-not-ready |
| 172 | status |
| 173 | contains 'did not say ready' 'a refusal for a record that is not ready was not reported' |
| 174 | json refused-not-ready '.outcome.decision == "refused-not-ready"' ".request.commit == \"$head\"" \ |
| 175 | '.change_id == null and .handle == null and .landing == null' |
| 176 | record_outcome "$head" "$base" refused-merge |
| 177 | status |
| 178 | contains 'include a merge' 'a refusal for a merge was not reported' |
| 179 | json refused-merge '.outcome.decision == "refused-merge"' |
| 180 | # A request the integrator voided for a withdrawal is gone, and its outcome |
| 181 | # says why. A request filed again at that head is a fresh approval. |
| 182 | git -C "$origin" update-ref -d "$request" |
| 183 | record_outcome "$head" "$base" voided-withdrawn |
| 184 | status |
| 185 | contains 'voided its request because the change was withdrawn' 'a voided request was not reported' |
| 186 | json voided-withdrawn '.outcome.decision == "voided-withdrawn"' '.request == null' |
| 187 | git -C "$origin" update-ref "$request" "$head" |
| 188 | json awaiting-decision '.outcome.decision == "voided-withdrawn"' |
| 189 | # A stored record that does not read, and that may name the request's |
| 190 | # head, is warned of: the integrator cannot tell whether it holds the head |
| 191 | # back. Only corrupt storage holds one, since the hook refuses it. |
| 192 | broken="$(printf '{}\n' | git -C "$origin" hash-object -w --stdin)" |
| 193 | broken="$(printf '100644 blob %s\trecord.json\n' "$broken" | git -C "$origin" mktree)" |
| 194 | broken="$(git -C "$origin" commit-tree "$broken" -p "$head" -m 'a broken record')" |
| 195 | git -C "$origin" update-ref refs/the-valley/changes/main/kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk "$broken" |
| 196 | status |
| 197 | contains 'changes/main/kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk does not read' 'a record that does not read was not warned of' |
| 198 | contains 'awaiting' 'a record that does not read stopped the status read' |
| 199 | git -C "$origin" update-ref -d refs/the-valley/changes/main/kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk |
| 200 | record_outcome "$head" "$base" 'failed 2026-09-21T12:00:00Z' |
| 201 | status |
| 202 | contains 'failed' 'a controller failure was not reported' |
| 203 | matches 'may.*land|might.*land|cannot.*(rule out|confirm|determine)|does not.*(prove|mean|establish).*land|not.*confirm.*land' \ |
| 204 | 'a failure was presented as proof that nothing landed' |
| 205 | json failed '.outcome.decision == "failed"' '.outcome.failed_at == "2026-09-21T12:00:00Z"' |
| 206 | |
| 207 | record_outcome "$head" "$base" stale |
| 208 | other_tree="$(git -C "$origin" rev-parse "$base^{tree}")" |
| 209 | advanced="$(printf 'another landing\n' | git -C "$origin" commit-tree "$other_tree" -p "$base")" |
| 210 | git -C "$origin" update-ref refs/heads/main "$advanced" |
| 211 | status |
| 212 | contains 'awaiting' 'a moved target retained the previous refusal' |
| 213 | contains 'new decision' 'a moved target did not explain re-evaluation' |
| 214 | json awaiting-new-decision ".target_tip == \"$advanced\"" ".outcome.tip == \"$base\"" \ |
| 215 | '.outcome.decision == "stale"' |
| 216 | |
| 217 | reset_remote |
| 218 | git -C "$origin" update-ref "$request" "$head" |
| 219 | record_outcome "$head" "$base" stale |
| 220 | evidence="$(printf 'new evidence\n' | git -C "$origin" hash-object -w --stdin)" |
| 221 | git -C "$origin" update-ref "$evidence_ref" "$evidence" |
| 222 | status |
| 223 | contains 'awaiting' 'new evidence retained the previous refusal' |
| 224 | contains 'new decision' 'new evidence did not explain re-evaluation' |
| 225 | json awaiting-new-decision ".target_tip == \"$base\"" '.outcome.decision == "stale"' |
| 226 | |
| 227 | # A reused change name must not carry an earlier commit's rejection or |
| 228 | # landing forward to its replacement. |
| 229 | reset_remote |
| 230 | git -C "$origin" update-ref "$request" "$head" |
| 231 | record_outcome "$base" "$base" reject |
| 232 | status |
| 233 | contains 'awaiting' 'an earlier head verdict was applied to the current request' |
| 234 | lacks 'needs attention' 'an earlier rejection was treated as current' |
| 235 | json awaiting-decision ".head == \"$head\"" ".outcome.head == \"$base\"" \ |
| 236 | '.outcome.decision == "reject"' |
| 237 | git -C "$origin" update-ref -d "$request" |
| 238 | record_outcome "$base" "$base" land |
| 239 | status |
| 240 | contains 'not approved' 'an old landing was applied to a newer branch head' |
| 241 | lacks 'integrated into origin/main' 'the newer branch inherited an old landing' |
| 242 | json not-requested ".head == \"$head\"" '.request == null' \ |
| 243 | ".outcome.head == \"$base\"" '.outcome.decision == "land"' |
| 244 | |
| 245 | # Main is authoritative for direct landings. A post-commit failure may |
| 246 | # leave both a failed memo and an unconsumed request behind. |
| 247 | reset_remote |
| 248 | git -C "$origin" update-ref refs/heads/main "$head" |
| 249 | git -C "$origin" update-ref "$request" "$head" |
| 250 | record_outcome "$head" "$base" 'failed 2026-09-21T12:00:00Z' |
| 251 | status |
| 252 | contains 'integrated into origin/main' 'a failed bookkeeping step hid an actual landing' |
| 253 | contains 'git pull --ff-only origin main' 'a behind local main had no update instruction' |
| 254 | json integrated ".head == \"$head\"" ".request.commit == \"$head\"" \ |
| 255 | '.outcome.decision == "failed"' |
| 256 | git -C "$origin" update-ref -d "$request" |
| 257 | record_outcome "$head" "$base" land |
| 258 | status |
| 259 | contains 'integrated into origin/main' 'a consumed request hid a direct landing' |
| 260 | json integrated ".head == \"$head\"" '.request == null' '.outcome.decision == "land"' |
| 261 | |
| 262 | # Reparenting changes the landed commit id. The matching durable outcome |
| 263 | # still identifies the change that was approved, including after topic |
| 264 | # cleanup. |
| 265 | reset_remote |
| 266 | topic_tree="$(git -C "$origin" rev-parse "$head^{tree}")" |
| 267 | landed="$(printf 'reparented landing\n' | git -C "$origin" commit-tree "$topic_tree" -p "$advanced")" |
| 268 | git -C "$origin" update-ref refs/heads/main "$landed" |
| 269 | record_outcome "$head" "$advanced" land |
| 270 | status |
| 271 | contains 'integrated into origin/main' 'a reparented landing was missed' |
| 272 | json integrated-recorded ".head == \"$head\"" ".target_tip == \"$landed\"" \ |
| 273 | ".outcome.head == \"$head\"" |
| 274 | git -C "$origin" update-ref -d refs/heads/topic |
| 275 | status |
| 276 | contains 'integrated into origin/main' 'deleting a landed topic hid its outcome' |
| 277 | json integrated-recorded ".head == \"$head\"" '.request == null' |
| 278 | |
| 279 | # A disappeared request without a landing record cannot establish success. |
| 280 | reset_remote |
| 281 | git -C "$origin" update-ref -d refs/heads/topic |
| 282 | if (cd "$repo" && valley status topic) > "$w/status.out" 2>&1; then |
| 283 | fail 'a nonexistent branch and request were accepted' |
| 284 | fi |
| 285 | matches 'no.*recorded.*request|no.*request.*record|no.*branch.*request' \ |
| 286 | 'a missing branch and request had no actionable explanation' |
| 287 | lacks 'integrated into origin/main' 'absence was treated as a landing' |
| 288 | json_refused |
| 289 | |
| 290 | # A corrupted memo must not turn a valid pending request into success or |
| 291 | # abort the status read without explaining which record is unreadable. |
| 292 | reset_remote |
| 293 | git -C "$origin" update-ref "$request" "$head" |
| 294 | bad="$(printf 'not an outcome\n' | git -C "$origin" hash-object -w --stdin)" |
| 295 | git -C "$origin" update-ref "$memo" "$bad" |
| 296 | local_state > "$w/before" |
| 297 | (cd "$repo" && valley status topic) > "$w/status.out" 2>&1 || true |
| 298 | local_state > "$w/after" |
| 299 | diff -u "$w/before" "$w/after" || fail 'a malformed memo changed local state' |
| 300 | matches 'malformed|unreadable|invalid|unrecognized|not recognized' \ |
| 301 | 'a malformed memo was silently accepted' |
| 302 | lacks 'integrated into origin/main' 'a malformed memo became a successful landing' |
| 303 | # An unreadable record is present and says nothing: no head, tip or time |
| 304 | # from it is reported as though it had been read. |
| 305 | json outcome-unreadable ".head == \"$head\"" \ |
| 306 | '.outcome == {"decision": "unknown", "head": null, "tip": null, "failed_at": null}' |
| 307 | |
| 308 | # Status must remain useful with uncommitted work, and its suggestion to |
| 309 | # update main must disappear once the local main already has the landing. |
| 310 | reset_remote |
| 311 | git -C "$origin" update-ref refs/heads/main "$head" |
| 312 | record_outcome "$head" "$base" land |
| 313 | printf 'staged\n' > "$repo/staged.txt" |
| 314 | git -C "$repo" add staged.txt |
| 315 | printf 'unstaged\n' >> "$repo/readme.txt" |
| 316 | status |
| 317 | contains 'integrated into origin/main' 'dirty local work obscured the remote result' |
| 318 | json integrated ".head == \"$head\"" |
| 319 | git -C "$repo" reset --quiet --hard "$head" |
| 320 | status |
| 321 | lacks 'git pull --ff-only origin main' 'an up-to-date local main was told to pull' |
| 322 | |
| 323 | # On another branch, advice must describe local main rather than assume |
| 324 | # main is at the commit currently checked out. |
| 325 | git -C "$repo" checkout --quiet -b feature "$base" |
| 326 | status |
| 327 | contains 'git switch main' 'an up-to-date local main was not offered for viewing' |
| 328 | lacks 'git pull --ff-only origin main' 'an up-to-date local main was told to pull from a feature checkout' |
| 329 | git -C "$repo" branch --force main "$advanced" >/dev/null |
| 330 | status |
| 331 | contains 'diverged' 'a divergent local main was overlooked from a feature checkout' |
| 332 | lacks 'git pull --ff-only origin main' 'a divergent local main was told to fast-forward' |
| 333 | git -C "$repo" branch --force main "$head" >/dev/null |
| 334 | |
| 335 | # A newer topic commit must not be mistaken for the older pending request. |
| 336 | reset_remote |
| 337 | git -C "$origin" update-ref "$request" "$base" |
| 338 | status |
| 339 | contains 'latest commit not approved' 'a newer topic inherited an older request' |
| 340 | contains 'older request' 'an outstanding older request was hidden' |
| 341 | json outdated-request ".head == \"$head\"" ".request.commit == \"$base\"" |
| 342 | |
| 343 | # A change name is carried exactly. A branch name may hold quotes, the |
| 344 | # characters a shell reads specially, and any space but ASCII's — here a |
| 345 | # no-break space: JSON escapes them, the request name maps only the slash, |
| 346 | # and the command the sentences suggest reads back, in a shell, as the |
| 347 | # same name. |
| 348 | odd='odd/"quoted"&<name>;$(x)'$'\302\240''nbsp' |
| 349 | oddrequest='odd-"quoted"&<name>;$(x)'$'\302\240''nbsp' |
| 350 | git -C "$origin" update-ref "refs/heads/$odd" "$head" |
| 351 | git -C "$origin" update-ref "refs/the-valley/integration-requests/main/$oddrequest" "$head" |
| 352 | status "$odd" |
| 353 | suggested="$(sed -n 's/^Check again: valley status //p' "$w/status.out")" |
| 354 | eval "set -- $suggested" |
| 355 | [ "$#" = 1 ] && [ "$1" = "$odd" ] || fail "the suggested command names something other than $odd" |
| 356 | status_json "$odd" |
| 357 | jq -e --arg change "$odd" --arg name "$oddrequest" --arg head "$head" ' |
| 358 | .change == $change and .request_name == $name and .state == "awaiting-decision" |
| 359 | and .request == {ref: ("refs/the-valley/integration-requests/main/" + $name), commit: $head} |
| 360 | ' "$w/status.json" > /dev/null || fail "status --json did not carry the name $odd exactly" |
| 361 | git -C "$origin" update-ref -d "refs/heads/$odd" |
| 362 | git -C "$origin" update-ref -d "refs/the-valley/integration-requests/main/$oddrequest" |
| 363 | |
| 364 | # A change's restack report. status shows the latest report the host |
| 365 | # restacker wrote about a change, and says whether it is current: every ref |
| 366 | # it observed is still what it observed. Those are main, each layer's |
| 367 | # record and branch, a branch it saw missing, and each request that held it |
| 368 | # back. A report whose refs have changed is historical, and pending leaves |
| 369 | # it out. |
| 370 | reset_remote |
| 371 | restack_id=qqqqrrrrssssttttuuuuvvvvwwwwxxxx |
| 372 | record_ref="refs/the-valley/changes/main/$restack_id" |
| 373 | report_ref="refs/the-valley/restack/main/$restack_id" |
| 374 | # one_file <file> <json> <parent…>: a commit on origin holding the JSON as |
| 375 | # the one file given. |
| 376 | one_file() { |
| 377 | local file="$1" json="$2" blob tree parent |
| 378 | local -a parents=() |
| 379 | shift 2 |
| 380 | for parent in "$@"; do parents+=(-p "$parent"); done |
| 381 | blob="$(printf '%s' "$json" | git -C "$origin" hash-object -w --stdin)" |
| 382 | tree="$(printf '100644 blob %s\t%s\n' "$blob" "$file" | git -C "$origin" mktree)" |
| 383 | git -C "$origin" commit-tree "$tree" "${parents[@]}" -m "$file" |
| 384 | } |
| 385 | version_json() { |
| 386 | printf '{"n": %s, "head": "git-sha1:%s", "title": "topic", "branch": "topic", "uploader": "%s"}' "$@" |
| 387 | } |
| 388 | record_v1="$(one_file record.json "{\"schema\": \"valley-change-record/v1\", \"change_id\": \"$restack_id\", \"target\": \"main\", \"owner\": \"author\", \"state\": \"ready\", \"seq\": 1, \"versions\": [$(version_json 1 "$head" author)], \"active\": 1}" "$head")" |
| 389 | git -C "$origin" update-ref "$record_ref" "$record_v1" |
| 390 | # report_json <result> <layer's published object or null>: a report about |
| 391 | # the change, observed at main's base, its first record, and topic at its |
| 392 | # head. |
| 393 | report_json() { |
| 394 | local failing=null paths='[]' |
| 395 | if [ "$1" = conflict ]; then failing="\"$restack_id\"" paths='["readme.txt"]'; fi |
| 396 | printf '{"schema": "valley-restack-report/v1", "change_id": "%s", "target": "main", "target_tip": "git-sha1:%s", "operation": "op-1", "result": "%s", "layers": [{"change_id": "%s", "record": "git-sha1:%s", "version": 1, "head": "git-sha1:%s", "branch": "topic", "branch_tip": "git-sha1:%s", "published": %s}], "failing_layer": %s, "conflict_paths": %s, "blocking_requests": [], "refusal": null}' \ |
| 397 | "$restack_id" "$base" "$1" "$restack_id" "$record_v1" "$head" "$head" "$2" "$failing" "$paths" |
| 398 | } |
| 399 | conflict="$(one_file report.json "$(report_json conflict null)")" |
| 400 | git -C "$origin" update-ref "$report_ref" "$conflict" |
| 401 | |
| 402 | # restack_json <current> [<jq condition>...]: the change's status, through |
| 403 | # --json, carries the report and says whether it is current. |
| 404 | restack_json() { |
| 405 | local want="$1" cond |
| 406 | shift |
| 407 | status_json "$restack_id" |
| 408 | for cond in ".restack.ref == \"$report_ref\"" ".restack.current == $want" ".change_id == \"$restack_id\"" "$@"; do |
| 409 | jq -e "$cond" "$w/status.json" > /dev/null || fail "status --json: $cond does not hold" |
| 410 | done |
| 411 | } |
| 412 | restack_json true ".restack.commit == \"$conflict\"" '.restack.report.result == "conflict"' \ |
| 413 | ".restack.report.failing_layer == \"$restack_id\"" '.restack.report.conflict_paths == ["readme.txt"]' \ |
| 414 | ".restack.report.layers[0].record == \"git-sha1:$record_v1\"" |
| 415 | status "$restack_id" |
| 416 | contains "Its restack report: a conflict rebasing qqqq onto main at ${base:0:7}, in readme.txt." \ |
| 417 | 'a current conflict was not reported' |
| 418 | contains "valley rebase qqqq" 'a current conflict did not say how to rebase by hand' |
| 419 | lacks 'historical' 'a current report was called historical' |
| 420 | (cd "$repo" && valley changes --json) > "$w/changes.json" || fail 'changes --json failed' |
| 421 | jq -e --arg id "$restack_id" --arg commit "$conflict" ' |
| 422 | .repos[0].changes[] | select(.change_id == $id) |
| 423 | | .restack.current == true and .restack.commit == $commit and .restack.report.result == "conflict" |
| 424 | ' "$w/changes.json" > /dev/null || fail 'changes --json did not carry the current report' |
| 425 | (cd "$repo" && valley pending) > "$w/status.out" || fail 'pending failed' |
| 426 | contains 'restack: a conflict rebasing qqqq onto main' 'pending did not show a current report' |
| 427 | grep -qx 'moving (1)' "$w/status.out" || fail 'pending did not list the change a restack holds as moving' |
| 428 | grep -q '^ ✗ qqqq v1 restack: a conflict rebasing qqqq onto main' "$w/status.out" || fail 'pending did not give a restack conflict its own row' |
| 429 | contains 'nothing needs you' 'pending listed a change a restack holds as needing an approval' |
| 430 | |
| 431 | # Each ref it observed changing makes it historical: the branch, the |
| 432 | # branch gone, main, and the record. Status says so, and pending leaves it |
| 433 | # out. historical <what> [<what the report says>] |
| 434 | historical() { |
| 435 | restack_json false |
| 436 | status "$restack_id" |
| 437 | contains "Its last restack report is historical: ${2:-a conflict rebasing}" "$1: a historical report was not called historical" |
| 438 | lacks 'valley rebase' "$1: a historical conflict was given as one to resolve" |
| 439 | (cd "$repo" && valley pending) > "$w/status.out" || fail 'pending failed' |
| 440 | lacks 'restack:' "$1: pending showed a historical report" |
| 441 | } |
| 442 | git -C "$origin" update-ref refs/heads/topic "$base" |
| 443 | historical 'a moved branch' |
| 444 | git -C "$origin" update-ref refs/heads/topic "$head" |
| 445 | git -C "$origin" update-ref refs/heads/main "$advanced" |
| 446 | historical 'a moved main' |
| 447 | git -C "$origin" update-ref refs/heads/main "$base" |
| 448 | record_v1_draft="$(one_file record.json "{\"schema\": \"valley-change-record/v1\", \"change_id\": \"$restack_id\", \"target\": \"main\", \"owner\": \"author\", \"state\": \"draft\", \"seq\": 2, \"versions\": [$(version_json 1 "$head" author)], \"active\": 1}" "$record_v1")" |
| 449 | git -C "$origin" update-ref "$record_ref" "$record_v1_draft" |
| 450 | historical 'a moved record' |
| 451 | git -C "$origin" update-ref "$record_ref" "$record_v1" |
| 452 | restack_json true |
| 453 | git -C "$origin" update-ref -d refs/heads/topic |
| 454 | restack_json false |
| 455 | git -C "$origin" update-ref refs/heads/topic "$head" |
| 456 | |
| 457 | # A complete report is matched against what it published: the head it |
| 458 | # moved the branch to, and the record that recorded it. It is current once |
| 459 | # both stand on origin, and historical while the branch is at the head it |
| 460 | # was moved from. |
| 461 | rebased="$(git -C "$origin" commit-tree "$head^{tree}" -p "$base" -m 'topic, rebased')" |
| 462 | record_v2="$(one_file record.json "{\"schema\": \"valley-change-record/v1\", \"change_id\": \"$restack_id\", \"target\": \"main\", \"owner\": \"author\", \"state\": \"ready\", \"seq\": 2, \"versions\": [$(version_json 1 "$head" author), $(version_json 2 "$rebased" restacker)], \"active\": 2}" "$record_v1" "$rebased")" |
| 463 | complete="$(one_file report.json "$(report_json complete "{\"head\": \"git-sha1:$rebased\", \"version\": 2, \"record\": \"git-sha1:$record_v2\"}")" "$conflict")" |
| 464 | git -C "$origin" update-ref "$report_ref" "$complete" |
| 465 | git -C "$origin" update-ref "$record_ref" "$record_v2" |
| 466 | restack_json false ".restack.commit == \"$complete\"" '.restack.report.result == "complete"' |
| 467 | git -C "$origin" update-ref refs/heads/topic "$rebased" |
| 468 | restack_json true '.restack.report.layers[0].published.version == 2' ".version == 2" |
| 469 | status "$restack_id" |
| 470 | contains "Its restack report: rebased onto main at ${base:0:7}." 'a current complete report was not reported' |
| 471 | git -C "$origin" update-ref "$record_ref" "$record_v1" |
| 472 | restack_json false |
| 473 | git -C "$origin" update-ref refs/heads/topic "$head" |
| 474 | |
| 475 | # A report that saw the branch missing records a null branch_tip. It stays |
| 476 | # current while the branch is missing, and is historical once anyone |
| 477 | # creates the branch, even at the head the record names. |
| 478 | git -C "$origin" update-ref -d refs/heads/topic |
| 479 | missing="$(one_file report.json "$(report_json blocked null | jq -c ' |
| 480 | .layers[0].branch_tip = null |
| 481 | | .refusal = "qqqq is on the branch topic, and origin has no such branch; nothing rebased"')" "$complete")" |
| 482 | git -C "$origin" update-ref "$report_ref" "$missing" |
| 483 | restack_json true '.restack.report.layers[0].branch == "topic"' '.restack.report.layers[0].branch_tip == null' |
| 484 | status "$restack_id" |
| 485 | contains "Its restack report: held before rebasing onto main at ${base:0:7}: qqqq is on the branch topic, and origin has no such branch; nothing rebased." \ |
| 486 | 'a current report of a missing branch was not reported' |
| 487 | (cd "$repo" && valley pending) > "$w/status.out" || fail 'pending failed' |
| 488 | contains 'restack: held before rebasing onto main' 'pending did not show a current report of a missing branch' |
| 489 | grep -q '^ ◇ qqqq v1 restack: held before rebasing onto main' "$w/status.out" || fail 'pending did not give a held restack its own row' |
| 490 | git -C "$origin" update-ref refs/heads/topic "$head" |
| 491 | historical 'a missing branch created' 'held before rebasing' |
| 492 | |
| 493 | # A version that names no branch has no branch to observe. Its layer's |
| 494 | # branch is empty and its branch_tip null, and the report is current while |
| 495 | # main and the record stand, wherever any branch is. |
| 496 | bare_version="{\"n\": 1, \"head\": \"git-sha1:$head\", \"title\": \"topic\", \"branch\": \"\", \"uploader\": \"author\"}" |
| 497 | record_bare="$(one_file record.json "{\"schema\": \"valley-change-record/v1\", \"change_id\": \"$restack_id\", \"target\": \"main\", \"owner\": \"author\", \"state\": \"ready\", \"seq\": 1, \"versions\": [$bare_version], \"active\": 1}" "$head")" |
| 498 | git -C "$origin" update-ref "$record_ref" "$record_bare" |
| 499 | bare="$(one_file report.json "$(report_json blocked null | jq -c --arg record "git-sha1:$record_bare" ' |
| 500 | .layers[0].record = $record | .layers[0].branch = "" | .layers[0].branch_tip = null |
| 501 | | .refusal = "qqqq records no branch; nothing rebased"')" "$missing")" |
| 502 | git -C "$origin" update-ref "$report_ref" "$bare" |
| 503 | restack_json true '.restack.report.layers[0].branch == ""' '.restack.report.layers[0].branch_tip == null' \ |
| 504 | ".restack.report.layers[0].record == \"git-sha1:$record_bare\"" |
| 505 | status "$restack_id" |
| 506 | contains "Its restack report: held before rebasing onto main at ${base:0:7}: qqqq records no branch; nothing rebased." \ |
| 507 | 'a current report of a version with no branch was not reported' |
| 508 | git -C "$origin" update-ref -d refs/heads/topic |
| 509 | restack_json true |
| 510 | git -C "$origin" update-ref refs/heads/topic "$head" |
| 511 | git -C "$origin" update-ref "$record_ref" "$record_v1" |
| 512 | historical 'the record of a version with no branch moved' 'held before rebasing' |
| 513 | |
| 514 | # A block names each request that held it back, at the tip it read. It is |
| 515 | # current while every one of them stays there, and historical once one is |
| 516 | # deleted or moved to another head. |
| 517 | request_ref=refs/the-valley/integration-requests/main/topic |
| 518 | git -C "$origin" update-ref "$request_ref" "$head" |
| 519 | blocked="$(one_file report.json "$(report_json blocked null | jq -c --arg ref "$request_ref" --arg tip "git-sha1:$head" ' |
| 520 | .blocking_requests = [{ref: $ref, tip: $tip}]')" "$bare")" |
| 521 | git -C "$origin" update-ref "$report_ref" "$blocked" |
| 522 | restack_json true ".restack.report.blocking_requests == [{\"ref\": \"$request_ref\", \"tip\": \"git-sha1:$head\"}]" |
| 523 | status "$restack_id" |
| 524 | contains "Its restack report: held before rebasing onto main at ${base:0:7} by the request topic." \ |
| 525 | 'a current block was not reported with its request' |
| 526 | git -C "$origin" update-ref -d "$request_ref" |
| 527 | historical 'a deleted blocking request' "held before rebasing onto main at ${base:0:7} by the request topic" |
| 528 | git -C "$origin" update-ref "$request_ref" "$head" |
| 529 | restack_json true |
| 530 | git -C "$origin" update-ref "$request_ref" "$advanced" |
| 531 | historical 'a retargeted blocking request' "held before rebasing onto main at ${base:0:7} by the request topic" |
| 532 | git -C "$origin" update-ref -d "$request_ref" |
| 533 | git -C "$origin" update-ref -d "$record_ref" |
| 534 | git -C "$origin" update-ref -d "$report_ref" |
| 535 | |
| 536 | # A branch no record is on has no restack report to show. |
| 537 | status_json topic |
| 538 | jq -e '.restack == null' "$w/status.json" > /dev/null || fail 'a branch status carried a restack report' |
| 539 | |
| 540 | # A target with no main is no answer either. |
| 541 | git -C "$origin" update-ref -d refs/heads/main |
| 542 | json_refused |
| 543 | grep -q 'no main branch' "$w/status.err" || fail 'a missing main had no useful diagnostic' |
| 544 | git -C "$origin" update-ref refs/heads/main "$base" |
| 545 | |
| 546 | # Connectivity failures are not an empty pending queue. These cases do not |
| 547 | # require one particular exit convention, only a diagnostic and no claim |
| 548 | # about a successfully inspected remote. |
| 549 | git -C "$repo" remote set-url origin "$w/missing.git" |
| 550 | (cd "$repo" && valley status topic) > "$w/status.out" 2>&1 || true |
| 551 | matches 'origin|remote|fetch|repository' 'an unavailable origin had no useful diagnostic' |
| 552 | lacks 'integrated into origin/main' 'a failed fetch reported cached success' |
| 553 | lacks 'not approved' 'a failed fetch was mistaken for an absent request' |
| 554 | json_refused |
| 555 | grep -q 'could not read origin' "$w/status.err" || fail 'an unreadable origin was not named as such' |
| 556 | git -C "$repo" remote remove origin |
| 557 | (cd "$repo" && valley status topic) > "$w/status.out" 2>&1 || true |
| 558 | matches 'origin|remote' 'a missing origin had no useful diagnostic' |
| 559 | lacks 'integrated into origin/main' 'a missing origin reported cached success' |
| 560 | json_refused |
| 561 | |
| 562 | echo 'valley-status: remote outcomes and local state checks passed' |
| 563 | touch "$out" |