gunk-dev/the-valley

valley

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