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