gunk-dev/the-valley

_valley

source66e2b574bf3cmain's published tip history
8.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 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 and its whole stack onto origin/main'
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 'self-update:install the tip of a branch of the-valley into your nix profile'
29 'help:show help'
30 )
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 local -a topics=('self-update:every step self-update takes, and its flags')
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 '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 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 comment) opts+=('-m:the comment' '--body:read the comment from stdin (-)') ;;
78 claim) opts+=('--run:the run claiming it' '--if:require <field>=<value> first') ;;
79 unclaim) opts+=('--if:require <field>=<value> first') ;;
80 esac
81 _describe -t options 'valley knot option' opts
82 elif [[ ${words[2]} == plan && ${words[CURRENT]} == -* ]]; then
83 local -a opts=(
84 '--project:another project on the same host'
85 '--all:every project the serving host publishes'
86 '--owner:read this principal as the operator'
87 '--json:print valley-plan/v1 JSON'
88 )
89 _describe -t options 'valley plan option' opts
90 elif [[ ${words[2]} == checks && ${words[CURRENT-1]} == --cost ]]; then
91 compadd quick slow
92 elif [[ ${words[2]} == checks && ${words[CURRENT]} == -* ]]; then
93 local -a opts=(
94 '--schema:the policy schema to compose against'
95 '--instance:the instance policy layer directory'
96 '--project:the project policy layer directory'
97 '--names:print only the required check names, one per line'
98 '--cost:with --names, only the quick or the slow checks'
99 )
100 _describe -t options 'valley checks option' opts
101 elif [[ ${words[2]} == ready && ${words[CURRENT]} == -* ]]; then
102 local -a opts=(
103 '--head:the head to mark ready, already on origin'
104 '--change:the change ID to record the version under'
105 '--if-active:add the version only while the change is ready at this active head'
106 '--stamp:carry the change ID in the head as a Change-Id trailer'
107 )
108 _describe -t options 'valley ready option' opts
109 elif [[ ${words[2]} == ready && ${words[CURRENT-1]} == --(head|change|if-active) ]]; then
110 return
111 elif [[ ${words[2]} == approve && ${words[CURRENT]} == -* ]]; then
112 local -a opts=('--head:the exact head to approve, one of the change'"'"'s version heads')
113 _describe -t options 'valley approve option' opts
114 elif [[ ${words[2]} == approve && ${words[CURRENT-1]} == --head ]]; then
115 return
116 elif [[ ${words[2]} == pending && ${words[CURRENT]} == -* ]]; then
117 local -a opts=(
118 '--all:every project the serving host publishes'
119 '--repo:that one project'
120 '--checks:the checks each change requires, by cost'
121 '--json:print valley-pending/v1 JSON'
122 )
123 _describe -t options 'valley pending option' opts
124 elif [[ ${words[2]} == changes && ${words[CURRENT]} == -* ]]; then
125 local -a opts=(
126 '--json:print valley-changes/v1 JSON'
127 '--all:every project the serving host publishes'
128 '--repo:that one project'
129 )
130 _describe -t options 'valley changes option' opts
131 elif [[ ${words[2]} == status && ${words[CURRENT]} == -* ]]; then
132 local -a opts=('--json:print the status as one valley-status/v1 JSON object')
133 _describe -t options 'valley status option' opts
134 elif [[ ${words[2]} == (review|approve|rebase|status|unready|withdraw) ]] \
135 && { (( CURRENT == 3 )) || [[ ${words[2]} == (status|approve) ]]; }; then
136 _valley_change_names ${words[2]}
137 elif [[ ${words[2]} == ready ]]; then
138 _valley_branch_names ready
139 fi
140}
141
142# Cached topic branches on origin. Review, approve, rebase, unready, withdraw
143# and ready filter out merged branches; status needs those too. Silent
144# outside a git repo — no errors mid-typing.
145_valley_branch_names() {
146 local -a branches selection
147 selection=()
148 [[ $1 != status ]] && selection=(--no-merged=origin/main)
149 branches=(${(f)"$(git for-each-ref refs/remotes/origin \
150 --format='%(refname)' "${selection[@]}" 2>/dev/null)"})
151 branches=(${branches:#refs/remotes/origin/HEAD})
152 branches=(${branches:#refs/remotes/origin/main})
153 branches=(${branches#refs/remotes/origin/})
154 (( ${#branches} )) && compadd -a branches
155 return 0
156}
157
158# What a verb that takes a change offers: the handles in the cache valley
159# pending and valley changes write, each described by its title and ready
160# changes first, then the branches. Without the cache, the branches alone.
161# The cache is $(git rev-parse --git-common-dir)/valley/handles, one line
162# per change: handle, state, branch, title, tab-separated.
163_valley_change_names() {
164 local dir cache line
165 local -a fields described
166 dir=$(git rev-parse --git-common-dir 2>/dev/null)
167 cache=$dir/valley/handles
168 if [[ -n $dir && -r $cache ]]; then
169 while IFS= read -r line; do
170 fields=("${(@ps:\t:)line}")
171 [[ -n ${fields[1]} ]] || continue
172 described+=("${fields[1]//:/\\:}:${fields[4]}")
173 done < $cache
174 (( ${#described} )) && _describe -V -t handles 'change handle' described
175 fi
176 _valley_branch_names $1
177}
178
179_valley "$@"