gunk-dev/the-valley

valley

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