Merge branch 'stable'
[stgit] / contrib / stgit-completion.bash
CommitLineData
8ebae56b
KH
1# bash completion support for StGIT -*- shell-script -*-
2#
3# Copyright (C) 2006, Karl Hasselström <kha@treskal.com>
4# Based on git-completion.sh
5#
6# To use these routines:
7#
8# 1. Copy this file to somewhere (e.g. ~/.stgit-completion.bash).
9#
10# 2. Add the following line to your .bashrc:
11# . ~/.stgit-completion.bash
12
1aaf55c9
CM
13_stg_commands="
14 add
15 applied
1aaf55c9
CM
16 branch
17 delete
18 diff
19 clean
20 clone
48b209cd 21 coalesce
1aaf55c9 22 commit
d5ae2173 23 cp
ed60fdae 24 edit
1aaf55c9
CM
25 export
26 files
27 float
28 fold
29 goto
841c7b2a 30 hide
1aaf55c9
CM
31 id
32 import
33 init
34 log
35 mail
36 new
37 patches
38 pick
39 pop
40 pull
41 push
22037590 42 rebase
1aaf55c9
CM
43 refresh
44 rename
051090dd 45 repair
1aaf55c9
CM
46 resolved
47 rm
48 series
49 show
6f1c5e3c 50 sink
1aaf55c9 51 status
06848fab 52 sync
1aaf55c9
CM
53 top
54 unapplied
55 uncommit
841c7b2a 56 unhide
1aaf55c9
CM
57"
58
8ebae56b
KH
59# The path to .git, or empty if we're not in a repository.
60_gitdir ()
61{
62 echo "$(git rev-parse --git-dir 2>/dev/null)"
63}
64
65# Name of the current branch, or empty if there isn't one.
66_current_branch ()
67{
68 local b=$(git symbolic-ref HEAD 2>/dev/null)
69 echo ${b#refs/heads/}
70}
71
72# List of all applied patches.
73_applied_patches ()
74{
75 local g=$(_gitdir)
76 [ "$g" ] && cat "$g/patches/$(_current_branch)/applied"
77}
78
79# List of all unapplied patches.
80_unapplied_patches ()
81{
82 local g=$(_gitdir)
83 [ "$g" ] && cat "$g/patches/$(_current_branch)/unapplied"
84}
85
ca8b854c
CM
86# List of all applied patches.
87_hidden_patches ()
88{
89 local g=$(_gitdir)
90 [ "$g" ] && cat "$g/patches/$(_current_branch)/hidden"
91}
92
8ebae56b
KH
93# List of all patches.
94_all_patches ()
95{
96 local b=$(_current_branch)
97 local g=$(_gitdir)
98 [ "$g" ] && cat "$g/patches/$b/applied" "$g/patches/$b/unapplied"
99}
100
101# List of all patches except the current patch.
102_all_other_patches ()
103{
104 local b=$(_current_branch)
105 local g=$(_gitdir)
106 [ "$g" ] && cat "$g/patches/$b/applied" "$g/patches/$b/unapplied" \
7ae2e706 107 | grep -v "^$(cat $g/patches/$b/current 2> /dev/null)$"
8ebae56b
KH
108}
109
450ae43a
YD
110_all_branches ()
111{
112 local g=$(_gitdir)
f1dc6c26 113 [ "$g" ] && (cd $g/patches/ && echo *)
450ae43a
YD
114}
115
1c804cba
YD
116_conflicting_files ()
117{
118 local g=$(_gitdir)
119 [ "$g" ] && stg status --conflict
120}
121
122_dirty_files ()
123{
124 local g=$(_gitdir)
125 [ "$g" ] && stg status --modified --new --deleted
126}
127
128_unknown_files ()
129{
130 local g=$(_gitdir)
131 [ "$g" ] && stg status --unknown
132}
133
134_known_files ()
135{
136 local g=$(_gitdir)
137 [ "$g" ] && git ls-files
138}
139
1aaf55c9
CM
140# List the command options
141_cmd_options ()
142{
21b1cc55 143 stg $1 --help 2>/dev/null | grep -e " --[A-Za-z]" | sed -e "s/.*\(--[^ =]\+\).*/\1/"
1aaf55c9
CM
144}
145
8ebae56b
KH
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 ()
149{
150 local patchlist="$1" options="$2"
151 local pfx cur="${COMP_WORDS[COMP_CWORD]}"
152 case "$cur" in
153 *..*)
154 pfx="${cur%..*}.."
155 cur="${cur#*..}"
156 COMPREPLY=($(compgen -P "$pfx" -W "$($patchlist)" -- "$cur"))
157 ;;
158 *)
159 COMPREPLY=($(compgen -W "$options $($patchlist)" -- "$cur"))
160 ;;
161 esac
162}
163
ebbd6f00
CM
164_complete_patch_range_options ()
165{
166 local patchlist="$1" options="$2" patch_options="$3"
167 local prev="${COMP_WORDS[COMP_CWORD-1]}"
168 local cur="${COMP_WORDS[COMP_CWORD]}"
169 local popt
170 for popt in $patch_options; do
171 if [ $prev == $popt ]; then
172 _complete_patch_range $patchlist
173 return
174 fi
175 done
176 COMPREPLY=($(compgen -W "$options" -- "$cur"))
177}
178
450ae43a
YD
179_complete_branch ()
180{
181 COMPREPLY=($(compgen -W "$(_cmd_options $1) $($2)" -- "${COMP_WORDS[COMP_CWORD]}"))
182}
183
8ebae56b
KH
184# Generate completions for options from the given list.
185_complete_options ()
186{
187 local options="$1"
188 COMPREPLY=($(compgen -W "$options" -- "${COMP_WORDS[COMP_CWORD]}"))
189}
190
1c804cba
YD
191_complete_files ()
192{
193 COMPREPLY=($(compgen -W "$(_cmd_options $1) $2" -- "${COMP_WORDS[COMP_CWORD]}"))
194}
195
1aaf55c9 196_stg_common ()
8ebae56b 197{
1aaf55c9 198 _complete_options "$(_cmd_options $1)"
8ebae56b
KH
199}
200
ebbd6f00 201_stg_patches ()
8ebae56b 202{
ebbd6f00 203 _complete_patch_range "$2" "$(_cmd_options $1)"
8ebae56b
KH
204}
205
ebbd6f00 206_stg_patches_options ()
8ebae56b 207{
ebbd6f00 208 _complete_patch_range_options "$2" "$(_cmd_options $1)" "$3"
8ebae56b
KH
209}
210
1aaf55c9 211_stg_help ()
8ebae56b 212{
1aaf55c9 213 _complete_options "$_stg_commands"
8ebae56b
KH
214}
215
216_stg ()
217{
218 local i c=1 command
219
220 while [ $c -lt $COMP_CWORD ]; do
221 if [ $c == 1 ]; then
222 command="${COMP_WORDS[c]}"
223 fi
224 c=$((++c))
225 done
226
227 # Complete name of subcommand.
228 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
229 COMPREPLY=($(compgen \
1aaf55c9 230 -W "--help --version copyright help $_stg_commands" \
8ebae56b
KH
231 -- "${COMP_WORDS[COMP_CWORD]}"))
232 return;
233 fi
234
235 # Complete arguments to subcommands.
236 case "$command" in
1aaf55c9
CM
237 # generic commands
238 help) _stg_help ;;
239 # repository commands
ebbd6f00 240 id) _stg_patches $command _all_patches ;;
1aaf55c9 241 # stack commands
48b209cd 242 coalesce) _stg_patches $command _applied_patches ;;
ebbd6f00
CM
243 float) _stg_patches $command _all_patches ;;
244 goto) _stg_patches $command _all_other_patches ;;
ca8b854c 245 hide) _stg_patches $command _unapplied_patches ;;
ebbd6f00
CM
246 pop) _stg_patches $command _applied_patches ;;
247 push) _stg_patches $command _unapplied_patches ;;
0aebdb85 248 series) _stg_patches $command _all_patches ;;
6f1c5e3c 249 sink) _stg_patches $command _all_patches ;;
ca8b854c 250 unhide) _stg_patches $command _hidden_patches ;;
1aaf55c9 251 # patch commands
ebbd6f00 252 delete) _stg_patches $command _all_patches ;;
ed60fdae 253 edit) _stg_patches $command _applied_patches ;;
340793d1 254 export) _stg_patches $command _all_patches ;;
ebbd6f00
CM
255 files) _stg_patches $command _all_patches ;;
256 log) _stg_patches $command _all_patches ;;
b4f656f0 257 mail) _stg_patches $command _all_patches ;;
ebbd6f00 258 pick) _stg_patches $command _unapplied_patches ;;
1c804cba
YD
259# refresh)_stg_patches_options $command _applied_patches "-p --patch" ;;
260 refresh) _complete_files $command "$(_dirty_files)" ;;
ebbd6f00
CM
261 rename) _stg_patches $command _all_patches ;;
262 show) _stg_patches $command _all_patches ;;
06848fab 263 sync) _stg_patches $command _applied_patches ;;
ebbd6f00
CM
264 # working-copy commands
265 diff) _stg_patches_options $command _applied_patches "-r --range" ;;
1c804cba
YD
266 resolved) _complete_files $command "$(_conflicting_files)" ;;
267 add) _complete_files $command "$(_unknown_files)" ;;
268# rm) _complete_files $command "$(_known_files)" ;;
450ae43a
YD
269 # commands that usually raher accept branches
270 branch) _complete_branch $command _all_branches ;;
271 rebase) _complete_branch $command _all_branches ;;
1aaf55c9
CM
272 # all the other commands
273 *) _stg_common $command ;;
8ebae56b
KH
274 esac
275}
276
277complete -o default -F _stg stg