gunk-dev/the-valley

_valley

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