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
59 # The path to .git, or empty if we're not in a repository.
62 echo "$(git rev-parse --git-dir 2>/dev/null)"
65 # Name of the current branch, or empty if there isn't one.
68 local b
=$
(git symbolic-ref HEAD
2>/dev
/null
)
72 # List of all applied patches.
76 [ "$g" ] && cat "$g/patches/$(_current_branch)/applied"
79 # List of all unapplied patches.
83 [ "$g" ] && cat "$g/patches/$(_current_branch)/unapplied"
86 # List of all applied patches.
90 [ "$g" ] && cat "$g/patches/$(_current_branch)/hidden"
93 # List of all patches.
96 local b
=$
(_current_branch
)
98 [ "$g" ] && cat "$g/patches/$b/applied" "$g/patches/$b/unapplied"
101 # List of all patches except the current patch.
102 _all_other_patches
()
104 local b
=$
(_current_branch
)
106 [ "$g" ] && cat "$g/patches/$b/applied" "$g/patches/$b/unapplied" \
107 |
grep -v "^$(cat $g/patches/$b/current 2> /dev/null)$"
113 [ "$g" ] && (cd $g/patches
/ && echo *)
116 _conflicting_files
()
119 [ "$g" ] && stg status
--conflict
125 [ "$g" ] && stg status
--modified --new --deleted
131 [ "$g" ] && stg status
--unknown
137 [ "$g" ] && git ls-files
140 # List the command options
143 stg
$1 --help 2>/dev
/null |
grep -e " --[A-Za-z]" |
sed -e "s/.*\(--[^ =]\+\).*/\1/"
146 # Generate completions for patches and patch ranges from the given
147 # patch list function, and options from the given list.
148 _complete_patch_range
()
150 local patchlist
="$1" options
="$2"
151 local pfx cur
="${COMP_WORDS[COMP_CWORD]}"
156 COMPREPLY
=($
(compgen
-P "$pfx" -W "$($patchlist)" -- "$cur"))
159 COMPREPLY
=($
(compgen
-W "$options $($patchlist)" -- "$cur"))
164 _complete_patch_range_options
()
166 local patchlist
="$1" options
="$2" patch_options
="$3"
167 local prev
="${COMP_WORDS[COMP_CWORD-1]}"
168 local cur
="${COMP_WORDS[COMP_CWORD]}"
170 for popt
in $patch_options; do
171 if [ $prev == $popt ]; then
172 _complete_patch_range
$patchlist
176 COMPREPLY
=($
(compgen
-W "$options" -- "$cur"))
181 COMPREPLY
=($
(compgen
-W "$(_cmd_options $1) $($2)" -- "${COMP_WORDS[COMP_CWORD]}"))
184 # Generate completions for options from the given list.
188 COMPREPLY
=($
(compgen
-W "$options" -- "${COMP_WORDS[COMP_CWORD]}"))
193 COMPREPLY
=($
(compgen
-W "$(_cmd_options $1) $2" -- "${COMP_WORDS[COMP_CWORD]}"))
198 _complete_options
"$(_cmd_options $1)"
203 _complete_patch_range
"$2" "$(_cmd_options $1)"
206 _stg_patches_options
()
208 _complete_patch_range_options
"$2" "$(_cmd_options $1)" "$3"
213 _complete_options
"$_stg_commands"
220 while [ $c -lt $COMP_CWORD ]; do
222 command="${COMP_WORDS[c]}"
227 # Complete name of subcommand.
228 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
229 COMPREPLY
=($
(compgen \
230 -W "--help --version copyright help $_stg_commands" \
231 -- "${COMP_WORDS[COMP_CWORD]}"))
235 # Complete arguments to subcommands.
239 # repository commands
240 id
) _stg_patches
$command _all_patches
;;
242 coalesce
) _stg_patches
$command _applied_patches
;;
243 float
) _stg_patches
$command _all_patches
;;
244 goto
) _stg_patches
$command _all_other_patches
;;
245 hide
) _stg_patches
$command _unapplied_patches
;;
246 pop
) _stg_patches
$command _applied_patches
;;
247 push
) _stg_patches
$command _unapplied_patches
;;
248 series
) _stg_patches
$command _all_patches
;;
249 sink
) _stg_patches
$command _all_patches
;;
250 unhide
) _stg_patches
$command _hidden_patches
;;
252 delete
) _stg_patches
$command _all_patches
;;
253 edit
) _stg_patches
$command _applied_patches
;;
254 export) _stg_patches
$command _all_patches
;;
255 files
) _stg_patches
$command _all_patches
;;
256 log
) _stg_patches
$command _all_patches
;;
257 mail) _stg_patches
$command _all_patches
;;
258 pick
) _stg_patches
$command _unapplied_patches
;;
259 # refresh)_stg_patches_options $command _applied_patches "-p --patch" ;;
260 refresh
) _complete_files
$command "$(_dirty_files)" ;;
261 rename
) _stg_patches
$command _all_patches
;;
262 show
) _stg_patches
$command _all_patches
;;
263 sync
) _stg_patches
$command _applied_patches
;;
264 # working-copy commands
265 diff) _stg_patches_options
$command _applied_patches
"-r --range" ;;
266 resolved
) _complete_files
$command "$(_conflicting_files)" ;;
267 add
) _complete_files
$command "$(_unknown_files)" ;;
268 # rm) _complete_files $command "$(_known_files)" ;;
269 # commands that usually raher accept branches
270 branch
) _complete_branch
$command _all_branches
;;
271 rebase
) _complete_branch
$command _all_branches
;;
272 # all the other commands
273 *) _stg_common
$command ;;
277 complete
-o default
-F _stg stg