| 1 | # shellcheck shell=bash |
| 2 | # bash 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; valley's |
| 7 | # own verbs fetch before acting, so stale candidates cost nothing. |
| 8 | |
| 9 | _valley_branches() { |
| 10 | # Cached topic branches on origin, with the origin/ prefix stripped. |
| 11 | # Review, approve, rebase and ready filter out merged branches; status needs |
| 12 | # those too. Silent outside a git repo — no errors mid-typing. |
| 13 | local ref |
| 14 | while IFS= read -r ref; do |
| 15 | case "$ref" in |
| 16 | '' | refs/remotes/origin/HEAD | refs/remotes/origin/main) continue ;; |
| 17 | esac |
| 18 | printf '%s\n' "${ref#refs/remotes/origin/}" |
| 19 | done < <(git for-each-ref refs/remotes/origin \ |
| 20 | --format='%(refname)' "$@" 2>/dev/null) |
| 21 | } |
| 22 | |
| 23 | _valley() { |
| 24 | local cur="${COMP_WORDS[COMP_CWORD]}" |
| 25 | COMPREPLY=() |
| 26 | if [ "$COMP_CWORD" -eq 1 ]; then |
| 27 | mapfile -t COMPREPLY < <(compgen -W 'pending changes review approve rebase status ready unready withdraw knot plan checks tail replay self-update help' -- "$cur") |
| 28 | elif [ "${COMP_WORDS[1]}" = checks ] && [ "${COMP_WORDS[COMP_CWORD - 1]}" = --cost ]; then |
| 29 | mapfile -t COMPREPLY < <(compgen -W 'quick slow' -- "$cur") |
| 30 | elif [ "${COMP_WORDS[1]}" = checks ] && [ "${cur:0:1}" = - ]; then |
| 31 | mapfile -t COMPREPLY < <(compgen -W '--schema --instance --project --names --cost' -- "$cur") |
| 32 | elif [ "${COMP_WORDS[1]}" = approve ] && [ "${cur:0:1}" = - ]; then |
| 33 | mapfile -t COMPREPLY < <(compgen -W '--head' -- "$cur") |
| 34 | elif [ "${COMP_WORDS[1]}" = approve ] && [ "${COMP_WORDS[COMP_CWORD - 1]}" = --head ]; then |
| 35 | return |
| 36 | elif [ "${COMP_WORDS[1]}" = approve ]; then |
| 37 | mapfile -t COMPREPLY < <(compgen -W "$(_valley_branches --no-merged=origin/main)" -- "$cur") |
| 38 | elif [ "${COMP_WORDS[1]}" = review ] && [ "${cur:0:1}" = - ]; then |
| 39 | mapfile -t COMPREPLY < <(compgen -W '-v' -- "$cur") |
| 40 | elif [ "${COMP_WORDS[1]}" = rebase ] && [ "${cur:0:1}" = - ]; then |
| 41 | mapfile -t COMPREPLY < <(compgen -W '--known-signers --integrator-name' -- "$cur") |
| 42 | elif [ "$COMP_CWORD" -eq 2 ] && [[ "${COMP_WORDS[1]}" =~ ^(review|rebase)$ ]]; then |
| 43 | mapfile -t COMPREPLY < <(compgen -W "$(_valley_branches --no-merged=origin/main)" -- "$cur") |
| 44 | elif [ "${COMP_WORDS[1]}" = pending ] && [ "${cur:0:1}" = - ]; then |
| 45 | mapfile -t COMPREPLY < <(compgen -W '--all --repo --checks --json -v --known-signers --integrator-name' -- "$cur") |
| 46 | elif [ "${COMP_WORDS[1]}" = changes ] && [ "${cur:0:1}" = - ]; then |
| 47 | mapfile -t COMPREPLY < <(compgen -W '--json --all --repo -v --known-signers --integrator-name' -- "$cur") |
| 48 | elif [ "${COMP_WORDS[1]}" = status ] && [ "${cur:0:1}" = - ]; then |
| 49 | mapfile -t COMPREPLY < <(compgen -W '--json -v --known-signers --integrator-name' -- "$cur") |
| 50 | elif [ "${COMP_WORDS[1]}" = status ]; then |
| 51 | mapfile -t COMPREPLY < <(compgen -W "$(_valley_branches)" -- "$cur") |
| 52 | elif [ "${COMP_WORDS[1]}" = ready ] && [ "${cur:0:1}" = - ]; then |
| 53 | mapfile -t COMPREPLY < <(compgen -W '--head --change --if-active --stamp' -- "$cur") |
| 54 | elif [ "${COMP_WORDS[1]}" = ready ] && [[ ! "${COMP_WORDS[COMP_CWORD - 1]}" =~ ^--(head|change|if-active)$ ]]; then |
| 55 | mapfile -t COMPREPLY < <(compgen -W "$(_valley_branches --no-merged=origin/main)" -- "$cur") |
| 56 | elif [ "$COMP_CWORD" -eq 2 ] && [ "${COMP_WORDS[1]}" = knot ]; then |
| 57 | mapfile -t COMPREPLY < <(compgen -W 'new show log list set link unlink edit comment claim unclaim schema' -- "$cur") |
| 58 | elif [ "${COMP_WORDS[1]}" = knot ] && { [ "$COMP_CWORD" -eq 3 ] && [ "${COMP_WORDS[2]}" = new ] || [ "${COMP_WORDS[COMP_CWORD - 1]}" = --type ]; }; then |
| 59 | mapfile -t COMPREPLY < <(compgen -W 'outcome bug idea question' -- "$cur") |
| 60 | elif [ "${COMP_WORDS[1]}" = knot ] && [ "${cur:0:1}" = - ]; then |
| 61 | mapfile -t COMPREPLY < <(compgen -W '--project --json --parent --blocked-by --owner --target --body --type --status --ready --if --force --change -m --edit --run --at' -- "$cur") |
| 62 | elif [ "${COMP_WORDS[1]}" = plan ] && [ "${cur:0:1}" = - ]; then |
| 63 | mapfile -t COMPREPLY < <(compgen -W '--project --all --owner --json --known-signers --integrator-name' -- "$cur") |
| 64 | elif [ "$COMP_CWORD" -eq 2 ] && [ "${COMP_WORDS[1]}" = replay ]; then |
| 65 | mapfile -t COMPREPLY < <(compgen -d -- "$cur") |
| 66 | elif [ "${COMP_WORDS[1]}" = self-update ] && [ "${cur:0:1}" = - ]; then |
| 67 | mapfile -t COMPREPLY < <(compgen -W '--verbose --retry --rollback --pin --unpin --repo --branch' -- "$cur") |
| 68 | elif [ "$COMP_CWORD" -eq 2 ] && [ "${COMP_WORDS[1]}" = help ]; then |
| 69 | mapfile -t COMPREPLY < <(compgen -W 'pending changes review approve rebase status ready unready withdraw knot plan checks tail replay self-update' -- "$cur") |
| 70 | fi |
| 71 | } |
| 72 | |
| 73 | complete -F _valley valley |