gunk-dev/the-valley

_valley

source822cb119f57bmain's published tip history
8.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 if (( CURRENT == 2 )); then
13 local -a verbs=(
14 'pending:list the changes not yet landed, the unrecorded branches, and the drafts'
15 'changes:print every change, across the host, as valley-changes/v1 JSON'
16 'review:page through a change, or a stack bottom-up, then approve, reject, or skip'
17 'approve:approve a change without paging it, filing its integration request'
18 'rebase:rebase a change onto origin/main and push it with a lease'
19 'status:read the integration result for a change from origin'
20 'ready:mark a head of a branch ready as a new version of its change'
21 'unready:make a ready change a draft'
22 'withdraw:mark a ready change or a draft withdrawn'
23 'knot:read and write the plan graph at refs/the-valley/knots'
24 'plan:show the plan graph as a tree, and what waits on you'
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 'help:show help'
29 )
30 _describe -t verbs 'valley verb' verbs
31 elif (( CURRENT == 3 )) && [[ ${words[2]} == replay ]]; then
32 _files -/
33 elif (( CURRENT == 3 )) && [[ ${words[2]} == knot ]]; then
34 local -a knotverbs=(
35 'new:make a knot: new <type> <title>'
36 'show:show a knot, its edges both ways and its comments'
37 'list:list the knots'
38 'set:set status, title, source, owner, target or graduated_into'
39 'link:add a blocked-by, parent or change edge'
40 'unlink:drop a blocked-by, parent or change edge'
41 'edit:replace a knot'"'"'s body with stdin'
42 'comment:add a comment'
43 'claim:claim a knot for a run'
44 'unclaim:release a claim'
45 'schema:print the types, statuses, fields and edges'
46 )
47 _describe -t knotverbs 'valley knot verb' knotverbs
48 elif (( CURRENT == 4 )) && [[ ${words[2]} == knot && ${words[3]} == new ]]; then
49 compadd outcome bug idea decision
50 elif [[ ${words[2]} == knot && ${words[CURRENT-1]} == --type ]]; then
51 compadd outcome bug idea decision
52 elif [[ ${words[2]} == knot && ${words[CURRENT-1]} == --status ]]; then
53 compadd active terminal open in-progress done abandoned closed raw exploring adopted graduated superseded discarded proposed decided
54 elif [[ ${words[2]} == knot && ${words[CURRENT]} == -* ]]; then
55 local -a opts=('--project:another project on the same host' '--json:print JSON')
56 case ${words[3]} in
57 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 (-)') ;;
58 list) opts+=('--type:only this type' '--status:only this status, active or terminal' '--owner:only this owner' '--ready:only the ready knots') ;;
59 set) opts+=('--if:require <field>=<value> first' '--force:set it even if another write changed it') ;;
60 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 decision' '--if:require <field>=<value> first') ;;
61 edit) opts+=('--body:read the new body from stdin (-)' '--if:require <field>=<value> first') ;;
62 comment) opts+=('-m:the comment' '--body:read the comment from stdin (-)') ;;
63 claim) opts+=('--run:the run claiming it' '--if:require <field>=<value> first') ;;
64 unclaim) opts+=('--if:require <field>=<value> first') ;;
65 esac
66 _describe -t options 'valley knot option' opts
67 elif [[ ${words[2]} == plan && ${words[CURRENT]} == -* ]]; then
68 local -a opts=(
69 '--project:another project on the same host'
70 '--all:every project the serving host publishes'
71 '--owner:read this principal as the operator'
72 '--json:print valley-plan/v1 JSON'
73 )
74 _describe -t options 'valley plan option' opts
75 elif [[ ${words[2]} == checks && ${words[CURRENT-1]} == --cost ]]; then
76 compadd quick slow
77 elif [[ ${words[2]} == checks && ${words[CURRENT]} == -* ]]; then
78 local -a opts=(
79 '--schema:the policy schema to compose against'
80 '--instance:the instance policy layer directory'
81 '--project:the project policy layer directory'
82 '--names:print only the required check names, one per line'
83 '--cost:with --names, only the quick or the slow checks'
84 )
85 _describe -t options 'valley checks option' opts
86 elif [[ ${words[2]} == ready && ${words[CURRENT]} == -* ]]; then
87 local -a opts=(
88 '--head:the head to mark ready, already on origin'
89 '--change:the change ID to record the version under'
90 '--if-active:add the version only while the change is ready at this active head'
91 '--stamp:carry the change ID in the head as a Change-Id trailer'
92 )
93 _describe -t options 'valley ready option' opts
94 elif [[ ${words[2]} == ready && ${words[CURRENT-1]} == --(head|change|if-active) ]]; then
95 return
96 elif [[ ${words[2]} == approve && ${words[CURRENT]} == -* ]]; then
97 local -a opts=('--head:the exact head to approve, one of the change'"'"'s version heads')
98 _describe -t options 'valley approve option' opts
99 elif [[ ${words[2]} == approve && ${words[CURRENT-1]} == --head ]]; then
100 return
101 elif [[ ${words[2]} == pending && ${words[CURRENT]} == -* ]]; then
102 local -a opts=(
103 '--all:every project the serving host publishes'
104 '--repo:that one project'
105 '--checks:the checks each change requires, by cost'
106 '--json:print valley-pending/v1 JSON'
107 )
108 _describe -t options 'valley pending option' opts
109 elif [[ ${words[2]} == changes && ${words[CURRENT]} == -* ]]; then
110 local -a opts=(
111 '--json:print valley-changes/v1 JSON'
112 '--all:every project the serving host publishes'
113 '--repo:that one project'
114 )
115 _describe -t options 'valley changes option' opts
116 elif [[ ${words[2]} == status && ${words[CURRENT]} == -* ]]; then
117 local -a opts=('--json:print the status as one valley-status/v1 JSON object')
118 _describe -t options 'valley status option' opts
119 elif [[ ${words[2]} == (review|approve|rebase|status|unready|withdraw) ]] \
120 && { (( CURRENT == 3 )) || [[ ${words[2]} == (status|approve) ]]; }; then
121 _valley_change_names ${words[2]}
122 elif [[ ${words[2]} == ready ]]; then
123 _valley_branch_names ready
124 fi
125}
126
127# Cached topic branches on origin. Review, approve, rebase, unready, withdraw
128# and ready filter out merged branches; status needs those too. Silent
129# outside a git repo — no errors mid-typing.
130_valley_branch_names() {
131 local -a branches selection
132 selection=()
133 [[ $1 != status ]] && selection=(--no-merged=origin/main)
134 branches=(${(f)"$(git for-each-ref refs/remotes/origin \
135 --format='%(refname)' "${selection[@]}" 2>/dev/null)"})
136 branches=(${branches:#refs/remotes/origin/HEAD})
137 branches=(${branches:#refs/remotes/origin/main})
138 branches=(${branches#refs/remotes/origin/})
139 (( ${#branches} )) && compadd -a branches
140 return 0
141}
142
143# What a verb that takes a change offers: the handles in the cache valley
144# pending and valley changes write, each described by its title and ready
145# changes first, then the branches. Without the cache, the branches alone.
146# The cache is $(git rev-parse --git-common-dir)/valley/handles, one line
147# per change: handle, state, branch, title, tab-separated.
148_valley_change_names() {
149 local dir cache line
150 local -a fields described
151 dir=$(git rev-parse --git-common-dir 2>/dev/null)
152 cache=$dir/valley/handles
153 if [[ -n $dir && -r $cache ]]; then
154 while IFS= read -r line; do
155 fields=("${(@ps:\t:)line}")
156 [[ -n ${fields[1]} ]] || continue
157 described+=("${fields[1]//:/\\:}:${fields[4]}")
158 done < $cache
159 (( ${#described} )) && _describe -V -t handles 'change handle' described
160 fi
161 _valley_branch_names $1
162}
163
164_valley "$@"