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