gunk-dev/the-valley

_valley

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