| 1 | #!/usr/bin/env bash |
| 2 | # valley — the S1 integrator's verbs, as a plain shell script. |
| 3 | # |
| 4 | # EVICTION CLAUSE (owner-ratified; the decision node is |
| 5 | # .the-valley/decisions/dcr-74c3158-valley-cli-lifecycle.md). |
| 6 | # This tool is deliberately a shell script: it runs from any checkout with |
| 7 | # nothing but git and coreutils — no machinery, the portability stance of |
| 8 | # ida-b9f646c. It GRADUATES to its own repo and a real language the moment |
| 9 | # ANY tripwire is crossed: |
| 10 | # - it grows past ~300 lines; |
| 11 | # - it needs persistent state, config, or a daemon; |
| 12 | # - it needs dependencies beyond git + coreutils |
| 13 | # (CROSSED by Phase 1's tail/replay, which need the nats CLI — |
| 14 | # graduation is now owed; recording the decision node is part of taking it.); |
| 15 | # - its behavior gets subtle enough to need real tests; |
| 16 | # - it needs nontrivial parsing or concurrency. |
| 17 | # Graduation is a deliberate act, recorded as a decision node. |
| 18 | # Precedent: klaus — born in cosmo, graduated deliberately. |
| 19 | |
| 20 | set -euo pipefail |
| 21 | |
| 22 | die() { |
| 23 | printf 'valley: %s\n' "$*" >&2 |
| 24 | exit 1 |
| 25 | } |
| 26 | |
| 27 | usage() { |
| 28 | cat <<'EOF' |
| 29 | valley — S1 integrator verbs for a valley project (direct-push mode) |
| 30 | |
| 31 | usage: valley <verb> [args] |
| 32 | |
| 33 | pending list branches on origin not merged into origin/main |
| 34 | review <branch> page through the branch's log and diff vs origin/main, |
| 35 | then choose: |
| 36 | [i]ntegrate ff-only merge onto main, push origin main, |
| 37 | delete the merged branch from origin |
| 38 | [r]eject delete the remote branch (confirmed first) |
| 39 | [s]kip do nothing |
| 40 | a stale branch (won't fast-forward from main) is first |
| 41 | offered: |
| 42 | [b]ase rebase it onto origin/main in a throwaway |
| 43 | worktree, push --force-with-lease, then |
| 44 | re-review the diff that would actually land |
| 45 | tail [subject] watch bus events scroll by (default subject: valley.>); |
| 46 | the server comes from the nats CLI's own config |
| 47 | (NATS_URL or a nats context; default localhost) |
| 48 | replay [dir] publish one ref-updated event per current ref of the |
| 49 | repo at dir (default: .) — rebuilds the stream's |
| 50 | projection, e.g. after nats stream purge valley |
| 51 | help show this help |
| 52 | |
| 53 | Runs from any checkout of the project; needs only git and coreutils, |
| 54 | except tail and replay, which also need the nats CLI (natscli). |
| 55 | No config, no state, no batch mode — the judgment stays with the human. |
| 56 | EOF |
| 57 | } |
| 58 | |
| 59 | fetch_origin() { |
| 60 | git fetch --prune --quiet origin |
| 61 | } |
| 62 | |
| 63 | # Resolve the pager as git would for a diff: pager.diff first (per-command |
| 64 | # pager configs — e.g. home-manager's delta module — set pager.diff and leave |
| 65 | # core.pager alone), then git var GIT_PAGER (core.pager, GIT_PAGER, PAGER, |
| 66 | # less). pager.<cmd> may legally be a boolean; treat true/false — and an |
| 67 | # empty or missing value — as unset and fall back. The value is |
| 68 | # shell-interpreted, hence sh -c; a non-tty just cats. |
| 69 | run_pager() { |
| 70 | [ -t 1 ] || { cat; return; } |
| 71 | local pager |
| 72 | pager="$(git config --get pager.diff || true)" |
| 73 | case "$pager" in ''|true|false) pager="$(git var GIT_PAGER)" ;; esac |
| 74 | sh -c "$pager" |
| 75 | } |
| 76 | |
| 77 | pending_line() { |
| 78 | local ref="$1" age author ahead stat |
| 79 | age="$(git log -1 --format='%cr' "$ref")" |
| 80 | author="$(git log -1 --format='%an' "$ref")" |
| 81 | ahead="$(git rev-list --count "origin/main..$ref")" |
| 82 | stat="$(git diff --shortstat "origin/main...$ref")" |
| 83 | stat="${stat# }" |
| 84 | printf '%s | %s | %s | ahead %s | %s\n' \ |
| 85 | "${ref#origin/}" "$age" "$author" "$ahead" "${stat:-no file changes}" |
| 86 | } |
| 87 | |
| 88 | cmd_pending() { |
| 89 | [ $# -eq 0 ] || die "pending takes no arguments" |
| 90 | fetch_origin |
| 91 | local ref found=0 |
| 92 | while IFS= read -r ref; do |
| 93 | case "$ref" in |
| 94 | origin/HEAD | origin/main) continue ;; |
| 95 | esac |
| 96 | found=1 |
| 97 | pending_line "$ref" |
| 98 | done < <(git for-each-ref --format='%(refname:short)' \ |
| 99 | --no-merged=origin/main refs/remotes/origin) |
| 100 | [ "$found" -eq 1 ] || echo "nothing pending" |
| 101 | } |
| 102 | |
| 103 | integrate() { |
| 104 | local branch="$1" local_main origin_main |
| 105 | local_main="$(git rev-parse --verify --quiet refs/heads/main)" \ |
| 106 | || die "no local main branch to fast-forward — nothing merged" |
| 107 | origin_main="$(git rev-parse refs/remotes/origin/main)" |
| 108 | [ "$local_main" = "$origin_main" ] \ |
| 109 | || die "local main is not at origin/main — sync it first; nothing merged" |
| 110 | git merge-base --is-ancestor refs/heads/main "refs/remotes/origin/$branch" \ |
| 111 | || die "$branch does not fast-forward from main — resolve by hand; nothing merged" |
| 112 | git checkout --quiet main |
| 113 | git merge --ff-only --quiet "refs/remotes/origin/$branch" |
| 114 | git push --quiet origin main |
| 115 | echo "integrated $branch: main is now $(git rev-parse --short main), pushed to origin" |
| 116 | # The branch tip is now an ancestor of the main just pushed, so deleting it |
| 117 | # loses nothing; without this every integration leaves a fully-merged ghost |
| 118 | # ref that later reads as a confusing "empty" branch. Best-effort: a failed |
| 119 | # delete must not report the integration as failed. |
| 120 | if git push --quiet origin --delete "$branch"; then |
| 121 | echo "deleted merged branch $branch from origin" |
| 122 | else |
| 123 | printf 'valley: warning: could not delete origin/%s — delete it by hand\n' \ |
| 124 | "$branch" >&2 |
| 125 | fi |
| 126 | } |
| 127 | |
| 128 | reject() { |
| 129 | local branch="$1" answer |
| 130 | printf 'really delete origin/%s? [y/N] ' "$branch" |
| 131 | read -r answer || answer="" |
| 132 | case "$answer" in |
| 133 | y | Y | yes) |
| 134 | git push --quiet origin --delete "$branch" |
| 135 | echo "rejected $branch: deleted from origin" |
| 136 | ;; |
| 137 | *) echo "kept $branch" ;; |
| 138 | esac |
| 139 | } |
| 140 | |
| 141 | show_review() { |
| 142 | local branch="$1" |
| 143 | { |
| 144 | printf 'branch %s, log against origin/main:\n\n' "$branch" |
| 145 | git --no-pager log --oneline "origin/main..origin/$branch" |
| 146 | printf '\n' |
| 147 | git --no-pager diff "origin/main...origin/$branch" |
| 148 | } | run_pager || true # quitting the pager early is not an error |
| 149 | } |
| 150 | |
| 151 | # Throwaway worktree for review's [b]ase choice; a global so the traps can |
| 152 | # always clean it up — success, conflict, or ctrl-C. Never a mid-rebase state. |
| 153 | REBASE_WT="" |
| 154 | cleanup_rebase_wt() { |
| 155 | [ -n "$REBASE_WT" ] || return 0 |
| 156 | git -C "$REBASE_WT" rebase --abort >/dev/null 2>&1 || true |
| 157 | git worktree remove --force "$REBASE_WT" >/dev/null 2>&1 || true |
| 158 | rm -rf "$REBASE_WT" |
| 159 | REBASE_WT="" |
| 160 | } |
| 161 | |
| 162 | # Rebase origin/<branch> onto origin/main in an isolated worktree — the |
| 163 | # user's checkout is never touched — and push with a lease so a concurrently |
| 164 | # updated branch is never clobbered. Mechanics only; the judgment on the |
| 165 | # rebased diff stays with the human, back at the review prompt. |
| 166 | rebase_onto_main() { |
| 167 | local branch="$1" |
| 168 | REBASE_WT="$(mktemp -d)" |
| 169 | trap cleanup_rebase_wt EXIT |
| 170 | trap 'exit 130' INT TERM |
| 171 | git worktree add --quiet --detach "$REBASE_WT" "refs/remotes/origin/$branch" |
| 172 | if ! git -C "$REBASE_WT" rebase --quiet refs/remotes/origin/main; then |
| 173 | cleanup_rebase_wt |
| 174 | die "$branch conflicts with main — resolve by hand; nothing changed" |
| 175 | fi |
| 176 | if ! git -C "$REBASE_WT" push --quiet --force-with-lease \ |
| 177 | origin "HEAD:refs/heads/$branch"; then |
| 178 | cleanup_rebase_wt |
| 179 | die "lease on origin/$branch failed (it moved since the fetch); nothing pushed" |
| 180 | fi |
| 181 | cleanup_rebase_wt |
| 182 | trap - EXIT INT TERM |
| 183 | echo "rebased $branch onto origin/main, pushed with --force-with-lease" |
| 184 | } |
| 185 | |
| 186 | cmd_review() { |
| 187 | [ $# -eq 1 ] || die "usage: valley review <branch>" |
| 188 | local branch="${1#origin/}" choice |
| 189 | fetch_origin |
| 190 | git rev-parse --verify --quiet "refs/remotes/origin/$branch" >/dev/null \ |
| 191 | || die "no such branch on origin: $branch" |
| 192 | show_review "$branch" |
| 193 | # A stale branch can't integrate, and its pre-rebase diff is not what would |
| 194 | # land; offer to rebase it mechanically, then re-review what actually would. |
| 195 | while ! git merge-base --is-ancestor refs/remotes/origin/main \ |
| 196 | "refs/remotes/origin/$branch"; do |
| 197 | printf '%s does not fast-forward from main: [b]ase onto main, [r]eject, [s]kip? ' \ |
| 198 | "$branch" |
| 199 | read -r choice || die "stdin closed before an answer; nothing done" |
| 200 | case "$choice" in |
| 201 | b) |
| 202 | rebase_onto_main "$branch" |
| 203 | fetch_origin |
| 204 | show_review "$branch" |
| 205 | ;; |
| 206 | r) reject "$branch" && return ;; |
| 207 | s) |
| 208 | echo "skipped $branch; nothing done" |
| 209 | return |
| 210 | ;; |
| 211 | *) echo "answer b, r, or s" ;; |
| 212 | esac |
| 213 | done |
| 214 | while true; do |
| 215 | printf '%s: [i]ntegrate, [r]eject, [s]kip? ' "$branch" |
| 216 | read -r choice || die "stdin closed before an answer; nothing done" |
| 217 | case "$choice" in |
| 218 | i) integrate "$branch" && return ;; |
| 219 | r) reject "$branch" && return ;; |
| 220 | s) |
| 221 | echo "skipped $branch; nothing done" |
| 222 | return |
| 223 | ;; |
| 224 | *) echo "answer i, r, or s" ;; |
| 225 | esac |
| 226 | done |
| 227 | } |
| 228 | |
| 229 | # tail and replay are thin wrappers over the nats CLI — anything they do, |
| 230 | # plain nats and git can do. They are the only verbs needing more than git |
| 231 | # and coreutils. |
| 232 | need_nats() { |
| 233 | command -v nats >/dev/null 2>&1 \ |
| 234 | || die "this verb needs the nats CLI (natscli); nix run .#valley bundles it" |
| 235 | } |
| 236 | |
| 237 | cmd_tail() { |
| 238 | [ $# -le 1 ] || die "usage: valley tail [subject]" |
| 239 | need_nats |
| 240 | exec nats sub "${1:-valley.>}" |
| 241 | } |
| 242 | |
| 243 | # One ref-updated event per existing ref, from git for-each-ref alone. A |
| 244 | # ref's prior value is not derivable from the repository, so `old` is the |
| 245 | # all-zero id — the same payload a push creating the ref would produce. |
| 246 | # The payload construction mirrors the host module's publisher hook |
| 247 | # (nix/valley-host.nix); change one only with the other. |
| 248 | cmd_replay() { |
| 249 | [ $# -le 1 ] || die "usage: valley replay [repo-dir]" |
| 250 | need_nats |
| 251 | local dir="${1:-.}" repo sha ref event count=0 |
| 252 | git -C "$dir" rev-parse --git-dir >/dev/null 2>&1 \ |
| 253 | || die "not a git repository: $dir" |
| 254 | if [ "$(git -C "$dir" rev-parse --is-bare-repository)" = true ]; then |
| 255 | repo="$(basename "$(git -C "$dir" rev-parse --absolute-git-dir)" .git)" |
| 256 | else |
| 257 | repo="$(basename "$(git -C "$dir" rev-parse --show-toplevel)")" |
| 258 | fi |
| 259 | while read -r sha ref; do |
| 260 | # " is the only JSON-significant character a refname can contain (git |
| 261 | # forbids \ and control characters); names and object ids are safe. |
| 262 | event="$(printf '{"event":"ref-updated","repo":"%s","ref":"%s","old":"%s","new":"%s"}' \ |
| 263 | "$repo" "${ref//\"/\\\"}" "${sha//?/0}" "$sha")" |
| 264 | nats pub "valley.git.$repo.ref-updated" "$event" >/dev/null |
| 265 | count=$((count + 1)) |
| 266 | done < <(git -C "$dir" for-each-ref --format='%(objectname) %(refname)') |
| 267 | echo "replayed $count refs of $repo onto the bus" |
| 268 | } |
| 269 | |
| 270 | main() { |
| 271 | local verb="${1:-help}" |
| 272 | if [ $# -gt 0 ]; then shift; fi |
| 273 | # The verb set is mirrored in completions/ (valley.bash, _valley) — |
| 274 | # update both when it changes. |
| 275 | case "$verb" in |
| 276 | pending) cmd_pending "$@" ;; |
| 277 | review) cmd_review "$@" ;; |
| 278 | tail) cmd_tail "$@" ;; |
| 279 | replay) cmd_replay "$@" ;; |
| 280 | help | -h | --help) usage ;; |
| 281 | *) |
| 282 | printf 'valley: unknown verb: %s\n\n' "$verb" >&2 |
| 283 | usage >&2 |
| 284 | exit 1 |
| 285 | ;; |
| 286 | esac |
| 287 | } |
| 288 | |
| 289 | main "$@" |