gunk-dev/the-valley

_valley

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