gunk-dev/the-valley

_valley

source9067b6d5ac52main's published tip history
5.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 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 'checks:print the checks the composed policy requires of a change'
24 'tail:watch bus events scroll by'
25 'replay:publish ref-updated events for the current refs of a repo'
26 'help:show help'
27 )
28 _describe -t verbs 'valley verb' verbs
29 elif (( CURRENT == 3 )) && [[ ${words[2]} == replay ]]; then
30 _files -/
31 elif [[ ${words[2]} == checks && ${words[CURRENT-1]} == --cost ]]; then
32 compadd quick slow
33 elif [[ ${words[2]} == checks && ${words[CURRENT]} == -* ]]; then
34 local -a opts=(
35 '--schema:the policy schema to compose against'
36 '--instance:the instance policy layer directory'
37 '--project:the project policy layer directory'
38 '--names:print only the required check names, one per line'
39 '--cost:with --names, only the quick or the slow checks'
40 )
41 _describe -t options 'valley checks option' opts
42 elif [[ ${words[2]} == ready && ${words[CURRENT]} == -* ]]; then
43 local -a opts=(
44 '--head:the head to mark ready, already on origin'
45 '--change:the change ID to record the version under'
46 '--if-active:add the version only while the change is ready at this active head'
47 '--stamp:carry the change ID in the head as a Change-Id trailer'
48 )
49 _describe -t options 'valley ready option' opts
50 elif [[ ${words[2]} == ready && ${words[CURRENT-1]} == --(head|change|if-active) ]]; then
51 return
52 elif [[ ${words[2]} == approve && ${words[CURRENT]} == -* ]]; then
53 local -a opts=('--head:the exact head to approve, one of the change'"'"'s version heads')
54 _describe -t options 'valley approve option' opts
55 elif [[ ${words[2]} == approve && ${words[CURRENT-1]} == --head ]]; then
56 return
57 elif [[ ${words[2]} == pending && ${words[CURRENT]} == -* ]]; then
58 local -a opts=(
59 '--all:every project the serving host publishes'
60 '--repo:that one project'
61 '--checks:the checks each change requires, by cost'
62 '--json:print valley-pending/v1 JSON'
63 )
64 _describe -t options 'valley pending option' opts
65 elif [[ ${words[2]} == changes && ${words[CURRENT]} == -* ]]; then
66 local -a opts=(
67 '--json:print valley-changes/v1 JSON'
68 '--all:every project the serving host publishes'
69 '--repo:that one project'
70 )
71 _describe -t options 'valley changes option' opts
72 elif [[ ${words[2]} == status && ${words[CURRENT]} == -* ]]; then
73 local -a opts=('--json:print the status as one valley-status/v1 JSON object')
74 _describe -t options 'valley status option' opts
75 elif [[ ${words[2]} == (review|approve|rebase|status|unready|withdraw) ]] \
76 && { (( CURRENT == 3 )) || [[ ${words[2]} == (status|approve) ]]; }; then
77 _valley_change_names ${words[2]}
78 elif [[ ${words[2]} == ready ]]; then
79 _valley_branch_names ready
80 fi
81}
82
83# Cached topic branches on origin. Review, approve, rebase, unready, withdraw
84# and ready filter out merged branches; status needs those too. Silent
85# outside a git repo — no errors mid-typing.
86_valley_branch_names() {
87 local -a branches selection
88 selection=()
89 [[ $1 != status ]] && selection=(--no-merged=origin/main)
90 branches=(${(f)"$(git for-each-ref refs/remotes/origin \
91 --format='%(refname)' "${selection[@]}" 2>/dev/null)"})
92 branches=(${branches:#refs/remotes/origin/HEAD})
93 branches=(${branches:#refs/remotes/origin/main})
94 branches=(${branches#refs/remotes/origin/})
95 (( ${#branches} )) && compadd -a branches
96 return 0
97}
98
99# What a verb that takes a change offers: the handles in the cache valley
100# pending and valley changes write, each described by its title and ready
101# changes first, then the branches. Without the cache, the branches alone.
102# The cache is $(git rev-parse --git-common-dir)/valley/handles, one line
103# per change: handle, state, branch, title, tab-separated.
104_valley_change_names() {
105 local dir cache line
106 local -a fields described
107 dir=$(git rev-parse --git-common-dir 2>/dev/null)
108 cache=$dir/valley/handles
109 if [[ -n $dir && -r $cache ]]; then
110 while IFS= read -r line; do
111 fields=("${(@ps:\t:)line}")
112 [[ -n ${fields[1]} ]] || continue
113 described+=("${fields[1]//:/\\:}:${fields[4]}")
114 done < $cache
115 (( ${#described} )) && _describe -V -t handles 'change handle' described
116 fi
117 _valley_branch_names $1
118}
119
120_valley "$@"