1 # bash completion support for StGIT -*- shell-script -*-
3 # Copyright (C) 2006, Karl Hasselström <kha@treskal.com>
4 # Based on git-completion.sh
6 # To use these routines:
8 # 1. Copy this file to somewhere (e.g. ~/.stgit-completion.bash).
10 # 2. Add the following line to your .bashrc:
11 # . ~/.stgit-completion.bash
56 # The path to .git, or empty if we're not in a repository.
59 echo "$(git rev-parse --git-dir 2>/dev/null)"
62 # Name of the current branch, or empty if there isn't one.
65 local b=$(git symbolic-ref HEAD 2>/dev/null)
69 # List of all applied patches.
73 [ "$g" ] && cat "$g/patches/$(_current_branch)/applied"
76 # List of all unapplied patches.
80 [ "$g" ] && cat "$g/patches/$(_current_branch)/unapplied"
83 # List of all patches.
86 local b=$(_current_branch)
88 [ "$g" ] && cat "$g/patches/$b/applied" "$g/patches/$b/unapplied"
91 # List of all patches except the current patch.
94 local b=$(_current_branch)
96 [ "$g" ] && cat "$g/patches/$b/applied" "$g/patches/$b/unapplied" \
97 | grep -v "^$(cat $g/patches/$b/current 2> /dev/null)$"
100 # List the command options
103 stg $1 --help 2>/dev/null | grep -e " --[A-Za-z]" | sed -e "s/.*\(--[^ =]\+\).*/\1/"
106 # Generate completions for patches and patch ranges from the given
107 # patch list function, and options from the given list.
108 _complete_patch_range ()
110 local patchlist="$1" options="$2"
111 local pfx cur="${COMP_WORDS[COMP_CWORD]}"
116 COMPREPLY=($(compgen -P "$pfx" -W "$($patchlist)" -- "$cur"))
119 COMPREPLY=($(compgen -W "$options $($patchlist)" -- "$cur"))
124 _complete_patch_range_options ()
126 local patchlist="$1" options="$2" patch_options="$3"
127 local prev="${COMP_WORDS[COMP_CWORD-1]}"
128 local cur="${COMP_WORDS[COMP_CWORD]}"
130 for popt in $patch_options; do
131 if [ $prev == $popt ]; then
132 _complete_patch_range $patchlist
136 COMPREPLY=($(compgen -W "$options" -- "$cur"))
139 # Generate completions for options from the given list.
143 COMPREPLY=($(compgen -W "$options" -- "${COMP_WORDS[COMP_CWORD]}"))
148 _complete_options "$(_cmd_options $1)"
153 _complete_patch_range "$2" "$(_cmd_options $1)"
156 _stg_patches_options ()
158 _complete_patch_range_options "$2" "$(_cmd_options $1)" "$3"
163 _complete_options "$_stg_commands"
170 while [ $c -lt $COMP_CWORD ]; do
172 command="${COMP_WORDS[c]}"
177 # Complete name of subcommand.
178 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
179 COMPREPLY=($(compgen \
180 -W "--help --version copyright help $_stg_commands" \
181 -- "${COMP_WORDS[COMP_CWORD]}"))
185 # Complete arguments to subcommands.
189 # repository commands
190 id) _stg_patches $command _all_patches ;;
192 float) _stg_patches $command _all_patches ;;
193 goto) _stg_patches $command _all_other_patches ;;
194 hide) _stg_patches $command _all_patches ;;
195 pop) _stg_patches $command _applied_patches ;;
196 push) _stg_patches $command _unapplied_patches ;;
197 series) _stg_patches $command _all_patches ;;
198 unhide) _stg_patches $command _all_patches ;;
200 delete) _stg_patches $command _all_patches ;;
201 export) _stg_patches $command _applied_patches ;;
202 files) _stg_patches $command _all_patches ;;
203 log) _stg_patches $command _all_patches ;;
204 mail) _stg_patches $command _all_patches ;;
205 pick) _stg_patches $command _unapplied_patches ;;
206 refresh)_stg_patches_options $command _applied_patches "-p --patch" ;;
207 rename) _stg_patches $command _all_patches ;;
208 show) _stg_patches $command _all_patches ;;
209 sync) _stg_patches $command _applied_patches ;;
210 # working-copy commands
211 diff) _stg_patches_options $command _applied_patches "-r --range" ;;
212 # all the other commands
213 *) _stg_common $command ;;
217 complete -o default -F _stg stg