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