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