| 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 | help show this help |
| 39 | |
| 40 | Runs from any checkout of the project; needs only git and coreutils. |
| 41 | No config, no state, no batch mode — the judgment stays with the human. |
| 42 | EOF |
| 43 | } |
| 44 | |
| 45 | fetch_origin() { |
| 46 | git fetch --prune --quiet origin |
| 47 | } |
| 48 | |
| 49 | # $PAGER may carry arguments ("less -FRX"), so run it through sh like git does. |
| 50 | run_pager() { |
| 51 | sh -c "${PAGER:-less -R}" |
| 52 | } |
| 53 | |
| 54 | pending_line() { |
| 55 | local ref="$1" age author ahead stat |
| 56 | age="$(git log -1 --format='%cr' "$ref")" |
| 57 | author="$(git log -1 --format='%an' "$ref")" |
| 58 | ahead="$(git rev-list --count "origin/main..$ref")" |
| 59 | stat="$(git diff --shortstat "origin/main...$ref")" |
| 60 | stat="${stat# }" |
| 61 | printf '%s | %s | %s | ahead %s | %s\n' \ |
| 62 | "${ref#origin/}" "$age" "$author" "$ahead" "${stat:-no file changes}" |
| 63 | } |
| 64 | |
| 65 | cmd_pending() { |
| 66 | [ $# -eq 0 ] || die "pending takes no arguments" |
| 67 | fetch_origin |
| 68 | local ref found=0 |
| 69 | while IFS= read -r ref; do |
| 70 | case "$ref" in |
| 71 | origin/HEAD | origin/main) continue ;; |
| 72 | esac |
| 73 | found=1 |
| 74 | pending_line "$ref" |
| 75 | done < <(git for-each-ref --format='%(refname:short)' \ |
| 76 | --no-merged=origin/main refs/remotes/origin) |
| 77 | [ "$found" -eq 1 ] || echo "nothing pending" |
| 78 | } |
| 79 | |
| 80 | integrate() { |
| 81 | local branch="$1" local_main origin_main |
| 82 | local_main="$(git rev-parse --verify --quiet refs/heads/main)" \ |
| 83 | || die "no local main branch to fast-forward — nothing merged" |
| 84 | origin_main="$(git rev-parse refs/remotes/origin/main)" |
| 85 | [ "$local_main" = "$origin_main" ] \ |
| 86 | || die "local main is not at origin/main — sync it first; nothing merged" |
| 87 | git merge-base --is-ancestor refs/heads/main "refs/remotes/origin/$branch" \ |
| 88 | || die "$branch does not fast-forward from main — resolve by hand; nothing merged" |
| 89 | git checkout --quiet main |
| 90 | git merge --ff-only --quiet "refs/remotes/origin/$branch" |
| 91 | git push --quiet origin main |
| 92 | echo "integrated $branch: main is now $(git rev-parse --short main), pushed to origin" |
| 93 | } |
| 94 | |
| 95 | reject() { |
| 96 | local branch="$1" answer |
| 97 | printf 'really delete origin/%s? [y/N] ' "$branch" |
| 98 | read -r answer || answer="" |
| 99 | case "$answer" in |
| 100 | y | Y | yes) |
| 101 | git push --quiet origin --delete "$branch" |
| 102 | echo "rejected $branch: deleted from origin" |
| 103 | ;; |
| 104 | *) echo "kept $branch" ;; |
| 105 | esac |
| 106 | } |
| 107 | |
| 108 | cmd_review() { |
| 109 | [ $# -eq 1 ] || die "usage: valley review <branch>" |
| 110 | local branch="${1#origin/}" choice |
| 111 | fetch_origin |
| 112 | git rev-parse --verify --quiet "refs/remotes/origin/$branch" >/dev/null \ |
| 113 | || die "no such branch on origin: $branch" |
| 114 | { |
| 115 | printf 'branch %s, log against origin/main:\n\n' "$branch" |
| 116 | git --no-pager log --oneline "origin/main..origin/$branch" |
| 117 | printf '\n' |
| 118 | git --no-pager diff "origin/main...origin/$branch" |
| 119 | } | run_pager || true # quitting the pager early is not an error |
| 120 | while true; do |
| 121 | printf '%s: [i]ntegrate, [r]eject, [s]kip? ' "$branch" |
| 122 | read -r choice || die "stdin closed before an answer; nothing done" |
| 123 | case "$choice" in |
| 124 | i) integrate "$branch" && return ;; |
| 125 | r) reject "$branch" && return ;; |
| 126 | s) |
| 127 | echo "skipped $branch; nothing done" |
| 128 | return |
| 129 | ;; |
| 130 | *) echo "answer i, r, or s" ;; |
| 131 | esac |
| 132 | done |
| 133 | } |
| 134 | |
| 135 | main() { |
| 136 | local verb="${1:-help}" |
| 137 | if [ $# -gt 0 ]; then shift; fi |
| 138 | case "$verb" in |
| 139 | pending) cmd_pending "$@" ;; |
| 140 | review) cmd_review "$@" ;; |
| 141 | help | -h | --help) usage ;; |
| 142 | *) |
| 143 | printf 'valley: unknown verb: %s\n\n' "$verb" >&2 |
| 144 | usage >&2 |
| 145 | exit 1 |
| 146 | ;; |
| 147 | esac |
| 148 | } |
| 149 | |
| 150 | main "$@" |