Allow a patch range to be specified for 'series'
[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
16 assimilate
17 branch
18 delete
19 diff
20 clean
21 clone
22 commit
23 export
24 files
25 float
26 fold
27 goto
28 id
29 import
30 init
31 log
32 mail
33 new
34 patches
35 pick
36 pop
37 pull
38 push
22037590 39 rebase
1aaf55c9
CM
40 refresh
41 rename
42 resolved
43 rm
44 series
45 show
46 status
06848fab 47 sync
1aaf55c9
CM
48 top
49 unapplied
50 uncommit
51"
52
8ebae56b
KH
53# The path to .git, or empty if we're not in a repository.
54_gitdir ()
55{
56 echo "$(git rev-parse --git-dir 2>/dev/null)"
57}
58
59# Name of the current branch, or empty if there isn't one.
60_current_branch ()
61{
62 local b=$(git symbolic-ref HEAD 2>/dev/null)
63 echo ${b#refs/heads/}
64}
65
66# List of all applied patches.
67_applied_patches ()
68{
69 local g=$(_gitdir)
70 [ "$g" ] && cat "$g/patches/$(_current_branch)/applied"
71}
72
73# List of all unapplied patches.
74_unapplied_patches ()
75{
76 local g=$(_gitdir)
77 [ "$g" ] && cat "$g/patches/$(_current_branch)/unapplied"
78}
79
80# List of all patches.
81_all_patches ()
82{
83 local b=$(_current_branch)
84 local g=$(_gitdir)
85 [ "$g" ] && cat "$g/patches/$b/applied" "$g/patches/$b/unapplied"
86}
87
88# List of all patches except the current patch.
89_all_other_patches ()
90{
91 local b=$(_current_branch)
92 local g=$(_gitdir)
93 [ "$g" ] && cat "$g/patches/$b/applied" "$g/patches/$b/unapplied" \
7ae2e706 94 | grep -v "^$(cat $g/patches/$b/current 2> /dev/null)$"
8ebae56b
KH
95}
96
1aaf55c9
CM
97# List the command options
98_cmd_options ()
99{
100 stg $1 --help | grep -e " --[A-Za-z]" | sed -e "s/.*\(--[^ =]\+\).*/\1/"
101}
102
8ebae56b
KH
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 ()
106{
107 local patchlist="$1" options="$2"
108 local pfx cur="${COMP_WORDS[COMP_CWORD]}"
109 case "$cur" in
110 *..*)
111 pfx="${cur%..*}.."
112 cur="${cur#*..}"
113 COMPREPLY=($(compgen -P "$pfx" -W "$($patchlist)" -- "$cur"))
114 ;;
115 *)
116 COMPREPLY=($(compgen -W "$options $($patchlist)" -- "$cur"))
117 ;;
118 esac
119}
120
ebbd6f00
CM
121_complete_patch_range_options ()
122{
123 local patchlist="$1" options="$2" patch_options="$3"
124 local prev="${COMP_WORDS[COMP_CWORD-1]}"
125 local cur="${COMP_WORDS[COMP_CWORD]}"
126 local popt
127 for popt in $patch_options; do
128 if [ $prev == $popt ]; then
129 _complete_patch_range $patchlist
130 return
131 fi
132 done
133 COMPREPLY=($(compgen -W "$options" -- "$cur"))
134}
135
8ebae56b
KH
136# Generate completions for options from the given list.
137_complete_options ()
138{
139 local options="$1"
140 COMPREPLY=($(compgen -W "$options" -- "${COMP_WORDS[COMP_CWORD]}"))
141}
142
1aaf55c9 143_stg_common ()
8ebae56b 144{
1aaf55c9 145 _complete_options "$(_cmd_options $1)"
8ebae56b
KH
146}
147
ebbd6f00 148_stg_patches ()
8ebae56b 149{
ebbd6f00 150 _complete_patch_range "$2" "$(_cmd_options $1)"
8ebae56b
KH
151}
152
ebbd6f00 153_stg_patches_options ()
8ebae56b 154{
ebbd6f00 155 _complete_patch_range_options "$2" "$(_cmd_options $1)" "$3"
8ebae56b
KH
156}
157
1aaf55c9 158_stg_help ()
8ebae56b 159{
1aaf55c9 160 _complete_options "$_stg_commands"
8ebae56b
KH
161}
162
163_stg ()
164{
165 local i c=1 command
166
167 while [ $c -lt $COMP_CWORD ]; do
168 if [ $c == 1 ]; then
169 command="${COMP_WORDS[c]}"
170 fi
171 c=$((++c))
172 done
173
174 # Complete name of subcommand.
175 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
176 COMPREPLY=($(compgen \
1aaf55c9 177 -W "--help --version copyright help $_stg_commands" \
8ebae56b
KH
178 -- "${COMP_WORDS[COMP_CWORD]}"))
179 return;
180 fi
181
182 # Complete arguments to subcommands.
183 case "$command" in
1aaf55c9
CM
184 # generic commands
185 help) _stg_help ;;
186 # repository commands
ebbd6f00 187 id) _stg_patches $command _all_patches ;;
1aaf55c9 188 # stack commands
ebbd6f00
CM
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 ;;
0aebdb85 193 series) _stg_patches $command _all_patches ;;
1aaf55c9 194 # patch commands
ebbd6f00 195 delete) _stg_patches $command _all_patches ;;
8560c67b 196 export) _stg_patches $command _applied_patches ;;
ebbd6f00
CM
197 files) _stg_patches $command _all_patches ;;
198 log) _stg_patches $command _all_patches ;;
b4f656f0 199 mail) _stg_patches $command _all_patches ;;
ebbd6f00
CM
200 pick) _stg_patches $command _unapplied_patches ;;
201 refresh)_stg_patches_options $command _applied_patches "-p --patch" ;;
202 rename) _stg_patches $command _all_patches ;;
203 show) _stg_patches $command _all_patches ;;
06848fab 204 sync) _stg_patches $command _applied_patches ;;
ebbd6f00
CM
205 # working-copy commands
206 diff) _stg_patches_options $command _applied_patches "-r --range" ;;
1aaf55c9
CM
207 # all the other commands
208 *) _stg_common $command ;;
8ebae56b
KH
209 esac
210}
211
212complete -o default -F _stg stg