| 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 | # (Phase 1's `valley tail` will trip this); |
| 14 | # - its behavior gets subtle enough to need real tests; |
| 15 | # - it needs nontrivial parsing or concurrency. |
| 16 | # Graduation is a deliberate act, recorded as a decision node. |
| 17 | # Precedent: klaus — born in cosmo, graduated deliberately. |
| 18 | |
| 19 | set -euo pipefail |
| 20 | |
| 21 | die() { |
| 22 | printf 'valley: %s\n' "$*" >&2 |
| 23 | exit 1 |
| 24 | } |
| 25 | |
| 26 | usage() { |
| 27 | cat <<'EOF' |
| 28 | valley — S1 integrator verbs for a valley project (direct-push mode) |
| 29 | |
| 30 | usage: valley <verb> [args] |
| 31 | |
| 32 | pending list branches on origin not merged into origin/main |
| 33 | review <branch> page through the branch's log and diff vs origin/main, |
| 34 | then choose: |
| 35 | [i]ntegrate ff-only merge onto main, push origin main |
| 36 | [r]eject delete the remote branch (confirmed first) |
| 37 | [s]kip do nothing |
| 38 | a stale branch (won't fast-forward from main) is first |
| 39 | offered: |
| 40 | [b]ase rebase it onto origin/main in a throwaway |
| 41 | worktree, push --force-with-lease, then |
| 42 | re-review the diff that would actually land |
| 43 | help show this help |
| 44 | |
| 45 | Runs from any checkout of the project; needs only git and coreutils. |
| 46 | No config, no state, no batch mode — the judgment stays with the human. |
| 47 | EOF |
| 48 | } |
| 49 | |
| 50 | fetch_origin() { |
| 51 | git fetch --prune --quiet origin |
| 52 | } |
| 53 | |
| 54 | # Resolve the pager exactly as git does — git var GIT_PAGER honors core.pager |
| 55 | # (so a configured delta renders these diffs too), then GIT_PAGER, then PAGER, |
| 56 | # then less. The value is shell-interpreted, hence sh -c; a non-tty just cats. |
| 57 | run_pager() { |
| 58 | [ -t 1 ] || { cat; return; } |
| 59 | sh -c "$(git var GIT_PAGER)" |
| 60 | } |
| 61 | |
| 62 | pending_line() { |
| 63 | local ref="$1" age author ahead stat |
| 64 | age="$(git log -1 --format='%cr' "$ref")" |
| 65 | author="$(git log -1 --format='%an' "$ref")" |
| 66 | ahead="$(git rev-list --count "origin/main..$ref")" |
| 67 | stat="$(git diff --shortstat "origin/main...$ref")" |
| 68 | stat="${stat# }" |
| 69 | printf '%s | %s | %s | ahead %s | %s\n' \ |
| 70 | "${ref#origin/}" "$age" "$author" "$ahead" "${stat:-no file changes}" |
| 71 | } |
| 72 | |
| 73 | cmd_pending() { |
| 74 | [ $# -eq 0 ] || die "pending takes no arguments" |
| 75 | fetch_origin |
| 76 | local ref found=0 |
| 77 | while IFS= read -r ref; do |
| 78 | case "$ref" in |
| 79 | origin/HEAD | origin/main) continue ;; |
| 80 | esac |
| 81 | found=1 |
| 82 | pending_line "$ref" |
| 83 | done < <(git for-each-ref --format='%(refname:short)' \ |
| 84 | --no-merged=origin/main refs/remotes/origin) |
| 85 | [ "$found" -eq 1 ] || echo "nothing pending" |
| 86 | } |
| 87 | |
| 88 | integrate() { |
| 89 | local branch="$1" local_main origin_main |
| 90 | local_main="$(git rev-parse --verify --quiet refs/heads/main)" \ |
| 91 | || die "no local main branch to fast-forward — nothing merged" |
| 92 | origin_main="$(git rev-parse refs/remotes/origin/main)" |
| 93 | [ "$local_main" = "$origin_main" ] \ |
| 94 | || die "local main is not at origin/main — sync it first; nothing merged" |
| 95 | git merge-base --is-ancestor refs/heads/main "refs/remotes/origin/$branch" \ |
| 96 | || die "$branch does not fast-forward from main — resolve by hand; nothing merged" |
| 97 | git checkout --quiet main |
| 98 | git merge --ff-only --quiet "refs/remotes/origin/$branch" |
| 99 | git push --quiet origin main |
| 100 | echo "integrated $branch: main is now $(git rev-parse --short main), pushed to origin" |
| 101 | } |
| 102 | |
| 103 | reject() { |
| 104 | local branch="$1" answer |
| 105 | printf 'really delete origin/%s? [y/N] ' "$branch" |
| 106 | read -r answer || answer="" |
| 107 | case "$answer" in |
| 108 | y | Y | yes) |
| 109 | git push --quiet origin --delete "$branch" |
| 110 | echo "rejected $branch: deleted from origin" |
| 111 | ;; |
| 112 | *) echo "kept $branch" ;; |
| 113 | esac |
| 114 | } |
| 115 | |
| 116 | show_review() { |
| 117 | local branch="$1" |
| 118 | { |
| 119 | printf 'branch %s, log against origin/main:\n\n' "$branch" |
| 120 | git --no-pager log --oneline "origin/main..origin/$branch" |
| 121 | printf '\n' |
| 122 | git --no-pager diff "origin/main...origin/$branch" |
| 123 | } | run_pager || true # quitting the pager early is not an error |
| 124 | } |
| 125 | |
| 126 | # Throwaway worktree for review's [b]ase choice; a global so the traps can |
| 127 | # always clean it up — success, conflict, or ctrl-C. Never a mid-rebase state. |
| 128 | REBASE_WT="" |
| 129 | cleanup_rebase_wt() { |
| 130 | [ -n "$REBASE_WT" ] || return 0 |
| 131 | git -C "$REBASE_WT" rebase --abort >/dev/null 2>&1 || true |
| 132 | git worktree remove --force "$REBASE_WT" >/dev/null 2>&1 || true |
| 133 | rm -rf "$REBASE_WT" |
| 134 | REBASE_WT="" |
| 135 | } |
| 136 | |
| 137 | # Rebase origin/<branch> onto origin/main in an isolated worktree — the |
| 138 | # user's checkout is never touched — and push with a lease so a concurrently |
| 139 | # updated branch is never clobbered. Mechanics only; the judgment on the |
| 140 | # rebased diff stays with the human, back at the review prompt. |
| 141 | rebase_onto_main() { |
| 142 | local branch="$1" |
| 143 | REBASE_WT="$(mktemp -d)" |
| 144 | trap cleanup_rebase_wt EXIT |
| 145 | trap 'exit 130' INT TERM |
| 146 | git worktree add --quiet --detach "$REBASE_WT" "refs/remotes/origin/$branch" |
| 147 | if ! git -C "$REBASE_WT" rebase --quiet refs/remotes/origin/main; then |
| 148 | cleanup_rebase_wt |
| 149 | die "$branch conflicts with main — resolve by hand; nothing changed" |
| 150 | fi |
| 151 | if ! git -C "$REBASE_WT" push --quiet --force-with-lease \ |
| 152 | origin "HEAD:refs/heads/$branch"; then |
| 153 | cleanup_rebase_wt |
| 154 | die "lease on origin/$branch failed (it moved since the fetch); nothing pushed" |
| 155 | fi |
| 156 | cleanup_rebase_wt |
| 157 | trap - EXIT INT TERM |
| 158 | echo "rebased $branch onto origin/main, pushed with --force-with-lease" |
| 159 | } |
| 160 | |
| 161 | cmd_review() { |
| 162 | [ $# -eq 1 ] || die "usage: valley review <branch>" |
| 163 | local branch="${1#origin/}" choice |
| 164 | fetch_origin |
| 165 | git rev-parse --verify --quiet "refs/remotes/origin/$branch" >/dev/null \ |
| 166 | || die "no such branch on origin: $branch" |
| 167 | show_review "$branch" |
| 168 | # A stale branch can't integrate, and its pre-rebase diff is not what would |
| 169 | # land; offer to rebase it mechanically, then re-review what actually would. |
| 170 | while ! git merge-base --is-ancestor refs/remotes/origin/main \ |
| 171 | "refs/remotes/origin/$branch"; do |
| 172 | printf '%s does not fast-forward from main: [b]ase onto main, [r]eject, [s]kip? ' \ |
| 173 | "$branch" |
| 174 | read -r choice || die "stdin closed before an answer; nothing done" |
| 175 | case "$choice" in |
| 176 | b) |
| 177 | rebase_onto_main "$branch" |
| 178 | fetch_origin |
| 179 | show_review "$branch" |
| 180 | ;; |
| 181 | r) reject "$branch" && return ;; |
| 182 | s) |
| 183 | echo "skipped $branch; nothing done" |
| 184 | return |
| 185 | ;; |
| 186 | *) echo "answer b, r, or s" ;; |
| 187 | esac |
| 188 | done |
| 189 | while true; do |
| 190 | printf '%s: [i]ntegrate, [r]eject, [s]kip? ' "$branch" |
| 191 | read -r choice || die "stdin closed before an answer; nothing done" |
| 192 | case "$choice" in |
| 193 | i) integrate "$branch" && return ;; |
| 194 | r) reject "$branch" && return ;; |
| 195 | s) |
| 196 | echo "skipped $branch; nothing done" |
| 197 | return |
| 198 | ;; |
| 199 | *) echo "answer i, r, or s" ;; |
| 200 | esac |
| 201 | done |
| 202 | } |
| 203 | |
| 204 | main() { |
| 205 | local verb="${1:-help}" |
| 206 | if [ $# -gt 0 ]; then shift; fi |
| 207 | # The verb set is mirrored in completions/ (valley.bash, _valley) — |
| 208 | # update both when it changes. |
| 209 | case "$verb" in |
| 210 | pending) cmd_pending "$@" ;; |
| 211 | review) cmd_review "$@" ;; |
| 212 | help | -h | --help) usage ;; |
| 213 | *) |
| 214 | printf 'valley: unknown verb: %s\n\n' "$verb" >&2 |
| 215 | usage >&2 |
| 216 | exit 1 |
| 217 | ;; |
| 218 | esac |
| 219 | } |
| 220 | |
| 221 | main "$@" |