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