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
51 # The path to .git, or empty if we're not in a repository.
54 echo "$(git rev-parse --git-dir 2>/dev/null)"
57 # Name of the current branch, or empty if there isn't one.
60 local b=$(git symbolic-ref HEAD 2>/dev/null)
64 # List of all applied patches.
68 [ "$g" ] && cat "$g/patches/$(_current_branch)/applied"
71 # List of all unapplied patches.
75 [ "$g" ] && cat "$g/patches/$(_current_branch)/unapplied"
78 # List of all patches.
81 local b=$(_current_branch)
83 [ "$g" ] && cat "$g/patches/$b/applied" "$g/patches/$b/unapplied"
86 # List of all patches except the current patch.
89 local b=$(_current_branch)
91 [ "$g" ] && cat "$g/patches/$b/applied" "$g/patches/$b/unapplied" \
92 | grep -v "^$(< $g/patches/$b/current)$"
95 # List the command options
98 stg $1 --help | grep -e " --[A-Za-z]" | sed -e "s/.*\(--[^ =]\+\).*/\1/"
101 # Generate completions for patches and patch ranges from the given
102 # patch list function, and options from the given list.
103 _complete_patch_range ()
105 local patchlist="$1" options="$2"
106 local pfx cur="${COMP_WORDS[COMP_CWORD]}"
111 COMPREPLY=($(compgen -P "$pfx" -W "$($patchlist)" -- "$cur"))
114 COMPREPLY=($(compgen -W "$options $($patchlist)" -- "$cur"))
119 # Generate completions for options from the given list.
123 COMPREPLY=($(compgen -W "$options" -- "${COMP_WORDS[COMP_CWORD]}"))
128 _complete_options "$(_cmd_options $1)"
133 _complete_patch_range _all_patches "$(_cmd_options $1)"
136 _stg_other_patches ()
138 _complete_patch_range _all_other_patches "$(_cmd_options $1)"
141 _stg_applied_patches ()
143 _complete_patch_range _applied_patches "$(_cmd_options $1)"
146 _stg_unapplied_patches ()
148 _complete_patch_range _unapplied_patches "$(_cmd_options $1)"
153 _complete_options "$_stg_commands"
160 while [ $c -lt $COMP_CWORD ]; do
162 command="${COMP_WORDS[c]}"
167 # Complete name of subcommand.
168 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
169 COMPREPLY=($(compgen \
170 -W "--help --version copyright help $_stg_commands" \
171 -- "${COMP_WORDS[COMP_CWORD]}"))
175 # Complete arguments to subcommands.
179 # repository commands
180 id) _stg_all_patches $command ;;
182 float) _stg_all_patches $command ;;
183 goto) _stg_other_patches $command ;;
184 pop) _stg_applied_patches $command ;;
185 push) _stg_unapplied_patches $command ;;
187 delete) _stg_all_patches $command ;;
188 export) _stg_applied_patches $command ;;
189 files) _stg_all_patches $command ;;
190 log) _stg_all_patches $command ;;
191 mail) _stg_applied_patches $command ;;
192 pick) _stg_unapplied_patches $command ;;
193 rename) _stg_all_patches $command ;;
194 show) _stg_all_patches $command ;;
195 # all the other commands
196 *) _stg_common $command ;;
200 complete -o default -F _stg stg