gunk-dev/the-valley

_valley

source6a7605f70901main's published tip history
11.4 KiB
1#compdef valley
2# zsh completion for valley. The verb list mirrors valley's public verbs
3# (helpTopics in status/help.go) — update both together.
4#
5# NEVER fetch from here: completion runs on every <TAB> and must be instant.
6# Candidates come from the remote-tracking refs of the last fetch and from
7# the handle cache the last valley pending or valley changes wrote; valley's
8# own verbs resolve a name against origin before acting, so stale candidates
9# cost nothing.
10
11_valley() {
12 local -a topics=(
13 'pending:list what needs an approval, in landing order, and what moves'
14 'changes:print every change, across the host, as valley-changes/v1 JSON'
15 'review:page through a change, or a stack bottom-up, then approve, reject, or skip'
16 'approve:approve a change without paging it, filing its integration request'
17 'rebase:rebase a change and its whole stack onto origin/main'
18 'status:read the integration result for a change from origin'
19 'ready:mark a head of a branch ready as a new version of its change'
20 'unready:make a ready change a draft'
21 'withdraw:mark a ready change or a draft withdrawn'
22 'knot:read and write the plan graph at refs/the-valley/knots'
23 'plan:show the plan graph as a tree, and what waits on you'
24 'flow:run valley-flow, the client that launches and follows agent runs'
25 'checks:print the checks the composed policy requires of a change'
26 'tail:watch bus events scroll by'
27 'replay:publish ref-updated events for the current refs of a repo'
28 'self-update:install the tip of a branch of the-valley into your nix profile'
29 'telemetry:read a valley host'"'"'s diagnostics over ssh'
30 )
31 if (( CURRENT == 2 )); then
32 local -a verbs=("${topics[@]}" 'help:show help')
33 _describe -t verbs 'valley verb' verbs
34 elif (( CURRENT == 3 )) && [[ ${words[2]} == replay ]]; then
35 _files -/
36 elif (( CURRENT == 3 )) && [[ ${words[2]} == help ]]; then
37 _describe -t topics 'valley help topic' topics
38 elif [[ ${words[2]} == self-update && ${words[CURRENT]} == -* ]]; then
39 local -a opts=(
40 '--verbose:also say so when valley is up to date'
41 '--retry:build the branch'"'"'s tip even if it failed before'
42 '--rollback:reinstall the previous valley'
43 '--pin:leave the current install alone until --unpin'
44 '--unpin:let self-update install the branch'"'"'s tip again'
45 '--repo:the repository, in place of valley.selfUpdate.repo'
46 '--branch:the branch, in place of valley.selfUpdate.branch'
47 )
48 _describe -t options 'valley self-update option' opts
49 elif (( CURRENT == 3 )) && [[ ${words[2]} == knot ]]; then
50 local -a knotverbs=(
51 'new:make a knot: new <type> <title>'
52 'show:show a knot, its edges both ways and its comments'
53 'log:list the commits that touched a knot or its comments'
54 'list:list the knots'
55 'set:set status, title, source, owner, target or graduated_into'
56 'link:add a blocked-by, parent or change edge'
57 'unlink:drop a blocked-by, parent or change edge'
58 'edit:replace a knot'"'"'s body with stdin'
59 'comment:add or edit a comment'
60 'claim:claim a knot for a run'
61 'unclaim:release a claim'
62 'schema:print the types, statuses, fields and edges'
63 )
64 _describe -t knotverbs 'valley knot verb' knotverbs
65 elif (( CURRENT == 4 )) && [[ ${words[2]} == knot && ${words[3]} == new ]]; then
66 compadd outcome bug idea question
67 elif [[ ${words[2]} == knot && ${words[CURRENT-1]} == --type ]]; then
68 compadd outcome bug idea question
69 elif [[ ${words[2]} == knot && ${words[CURRENT-1]} == --status ]]; then
70 compadd active terminal open in-progress done abandoned closed raw exploring adopted graduated superseded discarded answered dropped
71 elif [[ ${words[2]} == knot && ${words[CURRENT]} == -* ]]; then
72 local -a opts=('--project:another project on the same host' '--json:print JSON')
73 case ${words[3]} in
74 new) opts+=('--parent:the outcome it belongs to' '--blocked-by:a knot it waits on' '--owner:the principal it waits on' '--target:the project the work lands in' '--body:read the body from stdin (-)') ;;
75 list) opts+=('--type:only this type' '--status:only this status, active or terminal' '--owner:only this owner' '--ready:only the ready knots') ;;
76 set) opts+=('--if:require <field>=<value> first' '--force:set it even if another write changed it') ;;
77 link | unlink) opts+=('--blocked-by:a knot it waits on' '--parent:the outcome it belongs to' '--change:a change that does the work or records the answer' '--if:require <field>=<value> first') ;;
78 edit) opts+=('--body:read the new body from stdin (-)' '--if:require <field>=<value> first') ;;
79 show) opts+=('--at:show it as this commit of the graph left it') ;;
80 comment) opts+=('-m:the comment' '--body:read the comment from stdin (-)' '--edit:replace the text of the comment with this id') ;;
81 claim) opts+=('--run:the run claiming it' '--if:require <field>=<value> first') ;;
82 unclaim) opts+=('--if:require <field>=<value> first') ;;
83 esac
84 _describe -t options 'valley knot option' opts
85 elif [[ ${words[2]} == plan && ${words[CURRENT]} == -* ]]; then
86 local -a opts=(
87 '--project:another project on the same host'
88 '--all:every project the serving host publishes'
89 '--owner:read this principal as the operator'
90 '--json:print valley-plan/v1 JSON'
91 '--known-signers:the known-signers file a transfer statement is verified with'
92 '--integrator-name:the name the integrator signs transfer statements under'
93 )
94 _describe -t options 'valley plan option' opts
95 elif [[ ${words[2]} == checks && ${words[CURRENT-1]} == --cost ]]; then
96 compadd quick slow
97 elif [[ ${words[2]} == checks && ${words[CURRENT]} == -* ]]; then
98 local -a opts=(
99 '--schema:the policy schema to compose against'
100 '--instance:the instance policy layer directory'
101 '--project:the project policy layer directory'
102 '--names:print only the required check names, one per line'
103 '--cost:with --names, only the quick or the slow checks'
104 )
105 _describe -t options 'valley checks option' opts
106 elif [[ ${words[2]} == ready && ${words[CURRENT]} == -* ]]; then
107 local -a opts=(
108 '--head:the head to mark ready, already on origin'
109 '--change:the change ID to record the version under'
110 '--if-active:add the version only while the change is ready at this active head'
111 '--stamp:carry the change ID in the head as a Change-Id trailer'
112 )
113 _describe -t options 'valley ready option' opts
114 elif [[ ${words[2]} == ready && ${words[CURRENT-1]} == --(head|change|if-active) ]]; then
115 return
116 elif [[ ${words[2]} == approve && ${words[CURRENT]} == -* ]]; then
117 local -a opts=(
118 '--head:the exact head to approve, one of the change'"'"'s version heads'
119 '--build-here:build the nix checks origin holds no evidence for on this machine'
120 '--verbose:also print the refs, evidence, signers and digest approve read and published'
121 '--approval-key:sign the approval with this ed25519 key file'
122 '--approval-piv:sign the approval with the key in a YubiKey'"'"'s PIV slot'
123 )
124 _describe -t options 'valley approve option' opts
125 elif [[ ${words[2]} == (approve|review) && ${words[CURRENT-1]} == --approval-key ]]; then
126 _files
127 elif [[ ${words[2]} == approve && ${words[CURRENT-1]} == --head ]]; then
128 return
129 elif [[ ${words[2]} == pending && ${words[CURRENT]} == -* ]]; then
130 local -a opts=(
131 '--all:every project the serving host publishes'
132 '--repo:that one project'
133 '--checks:the checks each change requires, by cost, and their state'
134 '--drafts:list the drafts in full'
135 '--branches:list the branches no change is on in full'
136 '--json:print valley-pending/v1 JSON'
137 '-v:each row in full, and why no change shows a knot'
138 '--known-signers:the known-signers file a transfer statement is verified with'
139 '--integrator-name:the name the integrator signs transfer statements under'
140 )
141 _describe -t options 'valley pending option' opts
142 elif [[ ${words[2]} == changes && ${words[CURRENT]} == -* ]]; then
143 local -a opts=(
144 '--json:print valley-changes/v1 JSON'
145 '--checks:the state of the checks each ready change requires'
146 '--all:every project the serving host publishes'
147 '--repo:that one project'
148 '-v:say why no change shows a knot'
149 '--known-signers:the known-signers file a transfer statement is verified with'
150 '--integrator-name:the name the integrator signs transfer statements under'
151 )
152 _describe -t options 'valley changes option' opts
153 elif [[ ${words[2]} == status && ${words[CURRENT]} == -* ]]; then
154 local -a opts=(
155 '--json:print the status as one valley-status/v1 JSON object'
156 '-v:say why the object lists no knot'
157 '--known-signers:the known-signers file a transfer statement is verified with'
158 '--integrator-name:the name the integrator signs transfer statements under'
159 )
160 _describe -t options 'valley status option' opts
161 elif [[ ${words[2]} == rebase && ${words[CURRENT]} == -* ]]; then
162 local -a opts=(
163 '--known-signers:the known-signers file a transfer statement is verified with'
164 '--integrator-name:the name the integrator signs transfer statements under'
165 )
166 _describe -t options 'valley rebase option' opts
167 elif [[ ${words[2]} == review && ${words[CURRENT]} == -* ]]; then
168 local -a opts=(
169 '-v:say why no knot is shown'
170 '--build-here:let approve build the nix checks origin holds no evidence for on this machine'
171 '--repo:with no change, walk one project'
172 '--approval-key:sign each approval with this ed25519 key file'
173 '--approval-piv:sign each approval with the key in a YubiKey'"'"'s PIV slot'
174 )
175 _describe -t options 'valley review option' opts
176 elif [[ ${words[2]} == (review|approve|rebase|status|unready|withdraw) ]] \
177 && { (( CURRENT == 3 )) || [[ ${words[2]} == (status|approve) ]]; }; then
178 _valley_change_names ${words[2]}
179 elif [[ ${words[2]} == ready ]]; then
180 _valley_branch_names ready
181 fi
182}
183
184# Cached topic branches on origin. Review, approve, rebase, unready, withdraw
185# and ready filter out merged branches; status needs those too. Silent
186# outside a git repo — no errors mid-typing.
187_valley_branch_names() {
188 local -a branches selection
189 selection=()
190 [[ $1 != status ]] && selection=(--no-merged=origin/main)
191 branches=(${(f)"$(git for-each-ref refs/remotes/origin \
192 --format='%(refname)' "${selection[@]}" 2>/dev/null)"})
193 branches=(${branches:#refs/remotes/origin/HEAD})
194 branches=(${branches:#refs/remotes/origin/main})
195 branches=(${branches#refs/remotes/origin/})
196 (( ${#branches} )) && compadd -a branches
197 return 0
198}
199
200# What a verb that takes a change offers: the handles in the cache valley
201# pending and valley changes write, each described by its title and ready
202# changes first, then the branches. Without the cache, the branches alone.
203# The cache is $(git rev-parse --git-common-dir)/valley/handles, one line
204# per change: handle, state, branch, title, tab-separated.
205_valley_change_names() {
206 local dir cache line
207 local -a fields described
208 dir=$(git rev-parse --git-common-dir 2>/dev/null)
209 cache=$dir/valley/handles
210 if [[ -n $dir && -r $cache ]]; then
211 while IFS= read -r line; do
212 fields=("${(@ps:\t:)line}")
213 [[ -n ${fields[1]} ]] || continue
214 described+=("${fields[1]//:/\\:}:${fields[4]}")
215 done < $cache
216 (( ${#described} )) && _describe -V -t handles 'change handle' described
217 fi
218 _valley_branch_names $1
219}
220
221_valley "$@"