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
53 # The path to .git, or empty if we're not in a repository.
56 echo "$(git rev-parse --git-dir 2>/dev/null)"
59 # Name of the current branch, or empty if there isn't one.
62 local b
=$
(git symbolic-ref HEAD
2>/dev
/null
)
66 # List of all applied patches.
70 [ "$g" ] && cat "$g/patches/$(_current_branch)/applied"
73 # List of all unapplied patches.
77 [ "$g" ] && cat "$g/patches/$(_current_branch)/unapplied"
80 # List of all patches.
83 local b
=$
(_current_branch
)
85 [ "$g" ] && cat "$g/patches/$b/applied" "$g/patches/$b/unapplied"
88 # List of all patches except the current patch.
91 local b
=$
(_current_branch
)
93 [ "$g" ] && cat "$g/patches/$b/applied" "$g/patches/$b/unapplied" \
94 |
grep -v
"^$(cat $g/patches/$b/current 2> /dev/null)$"
97 # List the command options
100 stg
$1 --help |
grep -e
" --[A-Za-z]" |
sed -e
"s/.*\(--[^ =]\+\).*/\1/"
103 # Generate completions for patches and patch ranges from the given
104 # patch list function, and options from the given list.
105 _complete_patch_range
()
107 local patchlist
="$1" options
="$2"
108 local pfx cur
="${COMP_WORDS[COMP_CWORD]}"
113 COMPREPLY
=($
(compgen
-P
"$pfx" -W
"$($patchlist)" -- "$cur"))
116 COMPREPLY
=($
(compgen
-W
"$options $($patchlist)" -- "$cur"))
121 _complete_patch_range_options
()
123 local patchlist
="$1" options
="$2" patch_options
="$3"
124 local prev
="${COMP_WORDS[COMP_CWORD-1]}"
125 local cur
="${COMP_WORDS[COMP_CWORD]}"
127 for popt
in $patch_options; do
128 if [ $prev == $popt ]; then
129 _complete_patch_range
$patchlist
133 COMPREPLY
=($
(compgen
-W
"$options" -- "$cur"))
136 # Generate completions for options from the given list.
140 COMPREPLY
=($
(compgen
-W
"$options" -- "${COMP_WORDS[COMP_CWORD]}"))
145 _complete_options
"$(_cmd_options $1)"
150 _complete_patch_range
"$2" "$(_cmd_options $1)"
153 _stg_patches_options
()
155 _complete_patch_range_options
"$2" "$(_cmd_options $1)" "$3"
160 _complete_options
"$_stg_commands"
167 while [ $c -lt
$COMP_CWORD ]; do
169 command="${COMP_WORDS[c]}"
174 # Complete name of subcommand.
175 if [ $c -eq
$COMP_CWORD -a
-z
"$command" ]; then
176 COMPREPLY
=($
(compgen \
177 -W
"--help --version copyright help $_stg_commands" \
178 -- "${COMP_WORDS[COMP_CWORD]}"))
182 # Complete arguments to subcommands.
186 # repository commands
187 id
) _stg_patches
$command _all_patches
;;
189 float
) _stg_patches
$command _all_patches
;;
190 goto
) _stg_patches
$command _all_other_patches
;;
191 pop
) _stg_patches
$command _applied_patches
;;
192 push
) _stg_patches
$command _unapplied_patches
;;
194 delete
) _stg_patches
$command _all_patches
;;
195 export) _stg_patches
$command _applied_patches
;;
196 files
) _stg_patches
$command _all_patches
;;
197 log
) _stg_patches
$command _all_patches
;;
198 mail) _stg_patches
$command _all_patches
;;
199 pick
) _stg_patches
$command _unapplied_patches
;;
200 refresh
)_stg_patches_options
$command _applied_patches
"-p --patch" ;;
201 rename
) _stg_patches
$command _all_patches
;;
202 show
) _stg_patches
$command _all_patches
;;
203 sync
) _stg_patches
$command _applied_patches
;;
204 # working-copy commands
205 diff) _stg_patches_options
$command _applied_patches
"-r --range" ;;
206 # all the other commands
207 *) _stg_common
$command ;;
211 complete
-o default
-F _stg stg