gunk-dev/the-valley

valley.bash

sourcee534e27c2fbemain's published tip history
2.6 KiB
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 checks tail replay 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_CWORD" -eq 2 ] && [[ "${COMP_WORDS[1]}" =~ ^(review|approve|rebase)$ ]]; then
33 mapfile -t COMPREPLY < <(compgen -W "$(_valley_branches --no-merged=origin/main)" -- "$cur")
34 elif [ "${COMP_WORDS[1]}" = pending ] && [ "${cur:0:1}" = - ]; then
35 mapfile -t COMPREPLY < <(compgen -W '--all --repo --owes --json' -- "$cur")
36 elif [ "${COMP_WORDS[1]}" = changes ] && [ "${cur:0:1}" = - ]; then
37 mapfile -t COMPREPLY < <(compgen -W '--json --all --repo' -- "$cur")
38 elif [ "${COMP_WORDS[1]}" = status ] && [ "${cur:0:1}" = - ]; then
39 mapfile -t COMPREPLY < <(compgen -W '--json' -- "$cur")
40 elif [ "${COMP_WORDS[1]}" = status ]; then
41 mapfile -t COMPREPLY < <(compgen -W "$(_valley_branches)" -- "$cur")
42 elif [ "${COMP_WORDS[1]}" = ready ] && [ "${cur:0:1}" = - ]; then
43 mapfile -t COMPREPLY < <(compgen -W '--head --change --stamp' -- "$cur")
44 elif [ "${COMP_WORDS[1]}" = ready ] && [[ ! "${COMP_WORDS[COMP_CWORD - 1]}" =~ ^--(head|change)$ ]]; then
45 mapfile -t COMPREPLY < <(compgen -W "$(_valley_branches --no-merged=origin/main)" -- "$cur")
46 elif [ "$COMP_CWORD" -eq 2 ] && [ "${COMP_WORDS[1]}" = replay ]; then
47 mapfile -t COMPREPLY < <(compgen -d -- "$cur")
48 fi
49}
50
51complete -F _valley valley