gunk-dev/the-valley

valley

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