gunk-dev/the-valley

_valley

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