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
54 # The path to .git, or empty if we're not in a repository.
57 echo "$(git rev-parse --git-dir 2>/dev/null)"
60 # Name of the current branch, or empty if there isn't one.
63 local b
=$
(git symbolic-ref HEAD
2>/dev
/null
)
67 # List of all applied patches.
71 [ "$g" ] && cat "$g/patches/$(_current_branch)/applied"
74 # List of all unapplied patches.
78 [ "$g" ] && cat "$g/patches/$(_current_branch)/unapplied"
81 # List of all applied patches.
85 [ "$g" ] && cat "$g/patches/$(_current_branch)/hidden"
88 # List of all patches.
91 local b
=$
(_current_branch
)
93 [ "$g" ] && cat "$g/patches/$b/applied" "$g/patches/$b/unapplied"
96 # List of all patches except the current patch.
99 local b
=$
(_current_branch
)
101 [ "$g" ] && cat "$g/patches/$b/applied" "$g/patches/$b/unapplied" \
102 |
grep -v
"^$(cat $g/patches/$b/current 2> /dev/null)$"
108 [ "$g" ] && (cd $g/patches
/ && echo *)
111 _conflicting_files
()
114 [ "$g" ] && stg status
--conflict
120 [ "$g" ] && stg status
--modified
--new
--deleted
126 [ "$g" ] && stg status
--unknown
132 [ "$g" ] && git ls-files
135 # List the command options
138 stg
$1 --help 2>/dev
/null |
grep -e
" --[A-Za-z]" |
sed -e
"s/.*\(--[^ =]\+\).*/\1/"
141 # Generate completions for patches and patch ranges from the given
142 # patch list function, and options from the given list.
143 _complete_patch_range
()
145 local patchlist
="$1" options
="$2"
146 local pfx cur
="${COMP_WORDS[COMP_CWORD]}"
151 COMPREPLY
=($
(compgen
-P
"$pfx" -W
"$($patchlist)" -- "$cur"))
154 COMPREPLY
=($
(compgen
-W
"$options $($patchlist)" -- "$cur"))
159 _complete_patch_range_options
()
161 local patchlist
="$1" options
="$2" patch_options
="$3"
162 local prev
="${COMP_WORDS[COMP_CWORD-1]}"
163 local cur
="${COMP_WORDS[COMP_CWORD]}"
165 for popt
in $patch_options; do
166 if [ $prev == $popt ]; then
167 _complete_patch_range
$patchlist
171 COMPREPLY
=($
(compgen
-W
"$options" -- "$cur"))
176 COMPREPLY
=($
(compgen
-W
"$(_cmd_options $1) $($2)" -- "${COMP_WORDS[COMP_CWORD]}"))
179 # Generate completions for options from the given list.
183 COMPREPLY
=($
(compgen
-W
"$options" -- "${COMP_WORDS[COMP_CWORD]}"))
188 COMPREPLY
=($
(compgen
-W
"$(_cmd_options $1) $2" -- "${COMP_WORDS[COMP_CWORD]}"))
193 _complete_options
"$(_cmd_options $1)"
198 _complete_patch_range
"$2" "$(_cmd_options $1)"
201 _stg_patches_options
()
203 _complete_patch_range_options
"$2" "$(_cmd_options $1)" "$3"
208 _complete_options
"$_stg_commands"
215 while [ $c -lt
$COMP_CWORD ]; do
217 command="${COMP_WORDS[c]}"
222 # Complete name of subcommand.
223 if [ $c -eq
$COMP_CWORD -a
-z
"$command" ]; then
224 COMPREPLY
=($
(compgen \
225 -W
"--help --version copyright help $_stg_commands" \
226 -- "${COMP_WORDS[COMP_CWORD]}"))
230 # Complete arguments to subcommands.
234 # repository commands
235 id
) _stg_patches
$command _all_patches
;;
237 coalesce
) _stg_patches
$command _applied_patches
;;
238 float
) _stg_patches
$command _all_patches
;;
239 goto
) _stg_patches
$command _all_other_patches
;;
240 hide
) _stg_patches
$command _unapplied_patches
;;
241 pop
) _stg_patches
$command _applied_patches
;;
242 push
) _stg_patches
$command _unapplied_patches
;;
243 series
) _stg_patches
$command _all_patches
;;
244 sink
) _stg_patches
$command _all_patches
;;
245 unhide
) _stg_patches
$command _hidden_patches
;;
247 delete
) _stg_patches
$command _all_patches
;;
248 edit
) _stg_patches
$command _applied_patches
;;
249 export) _stg_patches
$command _all_patches
;;
250 files
) _stg_patches
$command _all_patches
;;
251 log
) _stg_patches
$command _all_patches
;;
252 mail) _stg_patches
$command _all_patches
;;
253 pick
) _stg_patches
$command _unapplied_patches
;;
254 # refresh)_stg_patches_options $command _applied_patches "-p --patch" ;;
255 refresh
) _complete_files
$command "$(_dirty_files)" ;;
256 rename
) _stg_patches
$command _all_patches
;;
257 show
) _stg_patches
$command _all_patches
;;
258 sync
) _stg_patches
$command _applied_patches
;;
259 # working-copy commands
260 diff) _stg_patches_options
$command _applied_patches
"-r --range" ;;
261 resolved
) _complete_files
$command "$(_conflicting_files)" ;;
262 # commands that usually raher accept branches
263 branch
) _complete_branch
$command _all_branches
;;
264 rebase
) _complete_branch
$command _all_branches
;;
265 # all the other commands
266 *) _stg_common
$command ;;
270 complete
-o default
-F _stg stg