_stg () { local cur cmd cmds cur=${COMP_WORDS[COMP_CWORD]} COMPREPLY=() if [ $COMP_CWORD -eq 1 ]; then cmds=$(stg help|tail +4|awk '{print $1}') COMPREPLY=( $(compgen -W "${cmds}" -- $cur) ) else local cmd=${COMP_WORDS[1]} local prev=${COMP_WORDS[COMP_CWORD-1]} #if [ $COMP_CWORD -eq 2 -o "$prev" = "-t" -o "$prev" = "--to" -o "$prev" = "-r" ]; then local patches #Add -b support - pass "-b branch" to unapplied and applied. case $cmd in push) patches=$(stg unapplied) ;; pop|mail) patches=$(stg applied) ;; diff|id) patches=$((stg applied; stg unapplied)|while read i; do echo $i/; done) ;; esac case $cmd in push) if [ "$prev" = "-t" -o "$prev" = "--to" ]; then if [ "${cur#*:}" != "${cur}" ]; then COMPREPLY=( $(compgen -W "${patches}" -- ${cur#*:}) ) else COMPREPLY=( $(compgen -W "${patches}" -- $cur) ) fi else cmds="-a --all -n --number -t --to --reverse --undo -h --help" COMPREPLY=( $(compgen -W "${cmds} ${patches}" -- $cur) ) fi ;; pop) if [ "$prev" = "-t" -o "$prev" = "--to" ]; then COMPREPLY=( $(compgen -W "${patches}" -- $cur) ) else cmds="-a --all -n --number -t --to -h --help" COMPREPLY=( $(compgen -W "${cmds}" -- $cur) ) fi ;; mail) if [ "$prev" = "-r" -o "$prev" = "--range" ]; then if [ "${cur#*:}" != "${cur}" ]; then COMPREPLY=( $(compgen -W "${patches}" -- ${cur#*:}) ) else COMPREPLY=( $(compgen -W "${patches}" -- $cur) ) fi else cmds="-a --all -r --range --to --cc --bcc -v --version \ -t --template -f --first -s --sleep --refid -u --smtp-user \ -p --smtp-password -b --branch -h --help" COMPREPLY=( $(compgen -o bashdefault -W "${cmds} ${patches}" -- $cur) ) fi ;; diff) if [ "$prev" = "-r" ]; then if [ "${cur#*:}" != "${cur}" ]; then COMPREPLY=( $(compgen -W "${patches}" -- ${cur#*:}) ) else COMPREPLY=( $(compgen -W "${patches}" -- $cur) ) fi else cmds="-r -s --stat -h --help" COMPREPLY=( $(compgen -W "${cmds}" -- $cur) ) fi ;; id) cmds="-b --branch -h --help" COMPREPLY=( $(compgen -W "${cmds} ${patches}" -- $cur) ) ;; *) COMPREPLY=( $(compgen -f -- $cur) ) ;; esac #else #COMPREPLY=( $(compgen -f -- $cur) ) #fi fi } complete -o default -F _stg stg # vi: set ft=sh: