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
57 # The path to .git, or empty if we're not in a repository.
60 echo "$(git rev-parse --git-dir 2>/dev/null)"
63 # Name of the current branch, or empty if there isn't one.
66 local b=$(git symbolic-ref HEAD 2>/dev/null)
70 # List of all applied patches.
74 [ "$g" ] && cat "$g/patches/$(_current_branch)/applied"
77 # List of all unapplied patches.
81 [ "$g" ] && cat "$g/patches/$(_current_branch)/unapplied"
84 # List of all applied patches.
88 [ "$g" ] && cat "$g/patches/$(_current_branch)/hidden"
91 # List of all patches.
94 local b=$(_current_branch)
96 [ "$g" ] && cat "$g/patches/$b/applied" "$g/patches/$b/unapplied"
99 # List of all patches except the current patch.
100 _all_other_patches ()
102 local b=$(_current_branch)
104 [ "$g" ] && cat "$g/patches/$b/applied" "$g/patches/$b/unapplied" \
105 | grep -v "^$(cat $g/patches/$b/current 2> /dev/null)$"
111 [ "$g" ] && (cd $g/patches/ && echo *)
114 _conflicting_files ()
117 [ "$g" ] && stg status --conflict
123 [ "$g" ] && stg status --modified --new --deleted
129 [ "$g" ] && stg status --unknown
135 [ "$g" ] && git ls-files
138 # List the command options
141 stg $1 --help 2>/dev/null | grep -e " --[A-Za-z]" | sed -e "s/.*\(--[^ =]\+\).*/\1/"
144 # Generate completions for patches and patch ranges from the given
145 # patch list function, and options from the given list.
146 _complete_patch_range ()
148 local patchlist="$1" options="$2"
149 local pfx cur="${COMP_WORDS[COMP_CWORD]}"
154 COMPREPLY=($(compgen -P "$pfx" -W "$($patchlist)" -- "$cur"))
157 COMPREPLY=($(compgen -W "$options $($patchlist)" -- "$cur"))
162 _complete_patch_range_options ()
164 local patchlist="$1" options="$2" patch_options="$3"
165 local prev="${COMP_WORDS[COMP_CWORD-1]}"
166 local cur="${COMP_WORDS[COMP_CWORD]}"
168 for popt in $patch_options; do
169 if [ $prev == $popt ]; then
170 _complete_patch_range $patchlist
174 COMPREPLY=($(compgen -W "$options" -- "$cur"))
179 COMPREPLY=($(compgen -W "$(_cmd_options $1) $($2)" -- "${COMP_WORDS[COMP_CWORD]}"))
182 # Generate completions for options from the given list.
186 COMPREPLY=($(compgen -W "$options" -- "${COMP_WORDS[COMP_CWORD]}"))
191 COMPREPLY=($(compgen -W "$(_cmd_options $1) $2" -- "${COMP_WORDS[COMP_CWORD]}"))
196 _complete_options "$(_cmd_options $1)"
201 _complete_patch_range "$2" "$(_cmd_options $1)"
204 _stg_patches_options ()
206 _complete_patch_range_options "$2" "$(_cmd_options $1)" "$3"
211 _complete_options "$_stg_commands"
218 while [ $c -lt $COMP_CWORD ]; do
220 command="${COMP_WORDS[c]}"
225 # Complete name of subcommand.
226 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
227 COMPREPLY=($(compgen \
228 -W "--help --version copyright help $_stg_commands" \
229 -- "${COMP_WORDS[COMP_CWORD]}"))
233 # Complete arguments to subcommands.
237 # repository commands
238 id) _stg_patches $command _all_patches ;;
240 float) _stg_patches $command _all_patches ;;
241 goto) _stg_patches $command _all_other_patches ;;
242 hide) _stg_patches $command _unapplied_patches ;;
243 pop) _stg_patches $command _applied_patches ;;
244 push) _stg_patches $command _unapplied_patches ;;
245 series) _stg_patches $command _all_patches ;;
246 sink) _stg_patches $command _all_patches ;;
247 unhide) _stg_patches $command _hidden_patches ;;
249 delete) _stg_patches $command _all_patches ;;
250 export) _stg_patches $command _applied_patches ;;
251 files) _stg_patches $command _all_patches ;;
252 log) _stg_patches $command _all_patches ;;
253 mail) _stg_patches $command _all_patches ;;
254 pick) _stg_patches $command _unapplied_patches ;;
255 # refresh)_stg_patches_options $command _applied_patches "-p --patch" ;;
256 refresh) _complete_files $command "$(_dirty_files)" ;;
257 rename) _stg_patches $command _all_patches ;;
258 show) _stg_patches $command _all_patches ;;
259 sync) _stg_patches $command _applied_patches ;;
260 # working-copy commands
261 diff) _stg_patches_options $command _applied_patches "-r --range" ;;
262 resolved) _complete_files $command "$(_conflicting_files)" ;;
263 add) _complete_files $command "$(_unknown_files)" ;;
264 # rm) _complete_files $command "$(_known_files)" ;;
265 # commands that usually raher accept branches
266 branch) _complete_branch $command _all_branches ;;
267 rebase) _complete_branch $command _all_branches ;;
268 # all the other commands
269 *) _stg_common $command ;;
273 complete -o default -F _stg stg