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
58 # The path to .git, or empty if we're not in a repository.
61 echo "$(git rev-parse --git-dir 2>/dev/null)"
64 # Name of the current branch, or empty if there isn't one.
67 local b
=$
(git symbolic-ref HEAD
2>/dev
/null
)
71 # List of all applied patches.
75 [ "$g" ] && cat "$g/patches/$(_current_branch)/applied"
78 # List of all unapplied patches.
82 [ "$g" ] && cat "$g/patches/$(_current_branch)/unapplied"
85 # List of all applied patches.
89 [ "$g" ] && cat "$g/patches/$(_current_branch)/hidden"
92 # List of all patches.
95 local b
=$
(_current_branch
)
97 [ "$g" ] && cat "$g/patches/$b/applied" "$g/patches/$b/unapplied"
100 # List of all patches except the current patch.
101 _all_other_patches
()
103 local b
=$
(_current_branch
)
105 [ "$g" ] && cat "$g/patches/$b/applied" "$g/patches/$b/unapplied" \
106 |
grep -v "^$(cat $g/patches/$b/current 2> /dev/null)$"
112 [ "$g" ] && (cd $g/patches
/ && echo *)
115 _conflicting_files
()
118 [ "$g" ] && stg status
--conflict
124 [ "$g" ] && stg status
--modified --new --deleted
130 [ "$g" ] && stg status
--unknown
136 [ "$g" ] && git ls-files
139 # List the command options
142 stg
$1 --help 2>/dev
/null |
grep -e " --[A-Za-z]" |
sed -e "s/.*\(--[^ =]\+\).*/\1/"
145 # Generate completions for patches and patch ranges from the given
146 # patch list function, and options from the given list.
147 _complete_patch_range
()
149 local patchlist
="$1" options
="$2"
150 local pfx cur
="${COMP_WORDS[COMP_CWORD]}"
155 COMPREPLY
=($
(compgen
-P "$pfx" -W "$($patchlist)" -- "$cur"))
158 COMPREPLY
=($
(compgen
-W "$options $($patchlist)" -- "$cur"))
163 _complete_patch_range_options
()
165 local patchlist
="$1" options
="$2" patch_options
="$3"
166 local prev
="${COMP_WORDS[COMP_CWORD-1]}"
167 local cur
="${COMP_WORDS[COMP_CWORD]}"
169 for popt
in $patch_options; do
170 if [ $prev == $popt ]; then
171 _complete_patch_range
$patchlist
175 COMPREPLY
=($
(compgen
-W "$options" -- "$cur"))
180 COMPREPLY
=($
(compgen
-W "$(_cmd_options $1) $($2)" -- "${COMP_WORDS[COMP_CWORD]}"))
183 # Generate completions for options from the given list.
187 COMPREPLY
=($
(compgen
-W "$options" -- "${COMP_WORDS[COMP_CWORD]}"))
192 COMPREPLY
=($
(compgen
-W "$(_cmd_options $1) $2" -- "${COMP_WORDS[COMP_CWORD]}"))
197 _complete_options
"$(_cmd_options $1)"
202 _complete_patch_range
"$2" "$(_cmd_options $1)"
205 _stg_patches_options
()
207 _complete_patch_range_options
"$2" "$(_cmd_options $1)" "$3"
212 _complete_options
"$_stg_commands"
219 while [ $c -lt $COMP_CWORD ]; do
221 command="${COMP_WORDS[c]}"
226 # Complete name of subcommand.
227 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
228 COMPREPLY
=($
(compgen \
229 -W "--help --version copyright help $_stg_commands" \
230 -- "${COMP_WORDS[COMP_CWORD]}"))
234 # Complete arguments to subcommands.
238 # repository commands
239 id
) _stg_patches
$command _all_patches
;;
241 float
) _stg_patches
$command _all_patches
;;
242 goto
) _stg_patches
$command _all_other_patches
;;
243 hide
) _stg_patches
$command _unapplied_patches
;;
244 pop
) _stg_patches
$command _applied_patches
;;
245 push
) _stg_patches
$command _unapplied_patches
;;
246 series
) _stg_patches
$command _all_patches
;;
247 sink
) _stg_patches
$command _all_patches
;;
248 unhide
) _stg_patches
$command _hidden_patches
;;
250 delete
) _stg_patches
$command _all_patches
;;
251 edit
) _stg_patches
$command _applied_patches
;;
252 export) _stg_patches
$command _applied_patches
;;
253 files
) _stg_patches
$command _all_patches
;;
254 log
) _stg_patches
$command _all_patches
;;
255 mail) _stg_patches
$command _all_patches
;;
256 pick
) _stg_patches
$command _unapplied_patches
;;
257 # refresh)_stg_patches_options $command _applied_patches "-p --patch" ;;
258 refresh
) _complete_files
$command "$(_dirty_files)" ;;
259 rename
) _stg_patches
$command _all_patches
;;
260 show
) _stg_patches
$command _all_patches
;;
261 sync
) _stg_patches
$command _applied_patches
;;
262 # working-copy commands
263 diff) _stg_patches_options
$command _applied_patches
"-r --range" ;;
264 resolved
) _complete_files
$command "$(_conflicting_files)" ;;
265 add
) _complete_files
$command "$(_unknown_files)" ;;
266 # rm) _complete_files $command "$(_known_files)" ;;
267 # commands that usually raher accept branches
268 branch
) _complete_branch
$command _all_branches
;;
269 rebase
) _complete_branch
$command _all_branches
;;
270 # all the other commands
271 *) _stg_common
$command ;;
275 complete
-o default
-F _stg stg