gunk-dev/the-valley

valley.bash

source79a631efa975main's published tip history
1.5 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_review_branches() {
10 # Branches on origin not merged into origin/main, with the origin/ prefix
11 # stripped and origin/HEAD + origin/main excluded. Silent outside a git
12 # repo — no errors mid-typing.
13 local ref
14 while IFS= read -r ref; do
15 case "$ref" in
16 '' | origin/HEAD | origin/main) continue ;;
17 esac
18 printf '%s\n' "${ref#origin/}"
19 done < <(git for-each-ref refs/remotes/origin \
20 --format='%(refname:short)' --no-merged=origin/main 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 review checks tail replay help' -- "$cur")
28 elif [ "${COMP_WORDS[1]}" = checks ] && [ "${cur:0:1}" = - ]; then
29 mapfile -t COMPREPLY < <(compgen -W '--schema --instance --project' -- "$cur")
30 elif [ "$COMP_CWORD" -eq 2 ] && [ "${COMP_WORDS[1]}" = review ]; then
31 mapfile -t COMPREPLY < <(compgen -W "$(_valley_review_branches)" -- "$cur")
32 elif [ "$COMP_CWORD" -eq 2 ] && [ "${COMP_WORDS[1]}" = replay ]; then
33 mapfile -t COMPREPLY < <(compgen -d -- "$cur")
34 fi
35}
36
37complete -F _valley valley