Support bash completion on symmetric difference operator.
[git/jrn.git] / contrib / completion / git-completion.bash
blobe4a32b61b279a8260e4de8175cc6f8885c833937
2 # bash completion support for core Git.
4 # Copyright (C) 2006 Shawn Pearce
5 # Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
7 # The contained completion routines provide support for completing:
9 # *) local and remote branch names
10 # *) local and remote tag names
11 # *) .git/remotes file names
12 # *) git 'subcommands'
13 # *) tree paths within 'ref:path/to/file' expressions
15 # To use these routines:
17 # 1) Copy this file to somewhere (e.g. ~/.git-completion.sh).
18 # 2) Added the following line to your .bashrc:
19 # source ~/.git-completion.sh
22 __gitdir ()
24 echo "${__git_dir:-$(git rev-parse --git-dir 2>/dev/null)}"
27 __git_refs ()
29 local cmd i is_hash=y dir="${1:-$(__gitdir)}"
30 if [ -d "$dir" ]; then
31 cmd=git-peek-remote
32 else
33 cmd=git-ls-remote
35 for i in $($cmd "$dir" 2>/dev/null); do
36 case "$is_hash,$i" in
37 y,*) is_hash=n ;;
38 n,*^{}) is_hash=y ;;
39 n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
40 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
41 n,*) is_hash=y; echo "$i" ;;
42 esac
43 done
46 __git_refs2 ()
48 local cmd i is_hash=y dir="${1:-$(__gitdir)}"
49 if [ -d "$dir" ]; then
50 cmd=git-peek-remote
51 else
52 cmd=git-ls-remote
54 for i in $($cmd "$dir" 2>/dev/null); do
55 case "$is_hash,$i" in
56 y,*) is_hash=n ;;
57 n,*^{}) is_hash=y ;;
58 n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}:${i#refs/tags/}" ;;
59 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}:${i#refs/heads/}" ;;
60 n,*) is_hash=y; echo "$i:$i" ;;
61 esac
62 done
65 __git_remotes ()
67 local i ngoff IFS=$'\n' d="$(__gitdir)"
68 shopt -q nullglob || ngoff=1
69 shopt -s nullglob
70 for i in "$d/remotes"/*; do
71 echo ${i#$d/remotes/}
72 done
73 [ "$ngoff" ] && shopt -u nullglob
74 for i in $(git --git-dir="$d" repo-config --list); do
75 case "$i" in
76 remote.*.url=*)
77 i="${i#remote.}"
78 echo "${i/.url=*/}"
80 esac
81 done
84 __git_complete_file ()
86 local cur="${COMP_WORDS[COMP_CWORD]}"
87 case "$cur" in
88 ?*:*)
89 local pfx ls ref="$(echo "$cur" | sed 's,:.*$,,')"
90 cur="$(echo "$cur" | sed 's,^.*:,,')"
91 case "$cur" in
92 ?*/*)
93 pfx="$(echo "$cur" | sed 's,/[^/]*$,,')"
94 cur="$(echo "$cur" | sed 's,^.*/,,')"
95 ls="$ref:$pfx"
96 pfx="$pfx/"
99 ls="$ref"
101 esac
102 COMPREPLY=($(compgen -P "$pfx" \
103 -W "$(git --git-dir="$(__gitdir)" ls-tree "$ls" \
104 | sed '/^100... blob /s,^.* ,,
105 /^040000 tree /{
106 s,^.* ,,
107 s,$,/,
109 s/^.* //')" \
110 -- "$cur"))
113 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
115 esac
118 __git_aliases ()
120 local i IFS=$'\n'
121 for i in $(git --git-dir="$(__gitdir)" repo-config --list); do
122 case "$i" in
123 alias.*)
124 i="${i#alias.}"
125 echo "${i/=*/}"
127 esac
128 done
131 __git_aliased_command ()
133 local word cmdline=$(git --git-dir="$(__gitdir)" \
134 repo-config --get "alias.$1")
135 for word in $cmdline; do
136 if [ "${word##-*}" ]; then
137 echo $word
138 return
140 done
143 _git_branch ()
145 local cur="${COMP_WORDS[COMP_CWORD]}"
146 COMPREPLY=($(compgen -W "-l -f -d -D $(__git_refs)" -- "$cur"))
149 _git_cat_file ()
151 local cur="${COMP_WORDS[COMP_CWORD]}"
152 case "${COMP_WORDS[0]},$COMP_CWORD" in
153 git-cat-file*,1)
154 COMPREPLY=($(compgen -W "-p -t blob tree commit tag" -- "$cur"))
156 git,2)
157 COMPREPLY=($(compgen -W "-p -t blob tree commit tag" -- "$cur"))
160 __git_complete_file
162 esac
165 _git_checkout ()
167 local cur="${COMP_WORDS[COMP_CWORD]}"
168 COMPREPLY=($(compgen -W "-l -b $(__git_refs)" -- "$cur"))
171 _git_diff ()
173 __git_complete_file
176 _git_diff_tree ()
178 local cur="${COMP_WORDS[COMP_CWORD]}"
179 COMPREPLY=($(compgen -W "-r -p -M $(__git_refs)" -- "$cur"))
182 _git_fetch ()
184 local cur="${COMP_WORDS[COMP_CWORD]}"
186 case "${COMP_WORDS[0]},$COMP_CWORD" in
187 git-fetch*,1)
188 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
190 git,2)
191 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
194 case "$cur" in
195 *:*)
196 cur=$(echo "$cur" | sed 's/^.*://')
197 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
200 local remote
201 case "${COMP_WORDS[0]}" in
202 git-fetch) remote="${COMP_WORDS[1]}" ;;
203 git) remote="${COMP_WORDS[2]}" ;;
204 esac
205 COMPREPLY=($(compgen -W "$(__git_refs2 "$remote")" -- "$cur"))
207 esac
209 esac
212 _git_ls_remote ()
214 local cur="${COMP_WORDS[COMP_CWORD]}"
215 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
218 _git_ls_tree ()
220 __git_complete_file
223 _git_log ()
225 local pfx cur="${COMP_WORDS[COMP_CWORD]}"
226 case "$cur" in
227 *...*)
228 pfx="${cur%...*}..."
229 cur="${cur#*...}"
230 COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs)" -- "$cur"))
232 *..*)
233 pfx="${cur%..*}.."
234 cur="${cur#*..}"
235 COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs)" -- "$cur"))
238 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
240 esac
243 _git_merge_base ()
245 local cur="${COMP_WORDS[COMP_CWORD]}"
246 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
249 _git_pull ()
251 local cur="${COMP_WORDS[COMP_CWORD]}"
253 case "${COMP_WORDS[0]},$COMP_CWORD" in
254 git-pull*,1)
255 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
257 git,2)
258 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
261 local remote
262 case "${COMP_WORDS[0]}" in
263 git-pull) remote="${COMP_WORDS[1]}" ;;
264 git) remote="${COMP_WORDS[2]}" ;;
265 esac
266 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
268 esac
271 _git_push ()
273 local cur="${COMP_WORDS[COMP_CWORD]}"
275 case "${COMP_WORDS[0]},$COMP_CWORD" in
276 git-push*,1)
277 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
279 git,2)
280 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
283 case "$cur" in
284 *:*)
285 local remote
286 case "${COMP_WORDS[0]}" in
287 git-push) remote="${COMP_WORDS[1]}" ;;
288 git) remote="${COMP_WORDS[2]}" ;;
289 esac
290 cur=$(echo "$cur" | sed 's/^.*://')
291 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
294 COMPREPLY=($(compgen -W "$(__git_refs2)" -- "$cur"))
296 esac
298 esac
301 _git_reset ()
303 local cur="${COMP_WORDS[COMP_CWORD]}"
304 local opt="--mixed --hard --soft"
305 COMPREPLY=($(compgen -W "$opt $(__git_refs)" -- "$cur"))
308 _git_show ()
310 local cur="${COMP_WORDS[COMP_CWORD]}"
311 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
314 _git ()
316 local i c=1 command __git_dir
318 while [ $c -lt $COMP_CWORD ]; do
319 i="${COMP_WORDS[c]}"
320 case "$i" in
321 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
322 --bare) __git_dir="." ;;
323 --version|--help|-p|--paginate) ;;
324 *) command="$i"; break ;;
325 esac
326 c=$((++c))
327 done
329 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
330 COMPREPLY=($(compgen \
331 -W "--git-dir= --version \
332 $(git help -a|egrep '^ ') \
333 $(__git_aliases)" \
334 -- "${COMP_WORDS[COMP_CWORD]}"))
335 return;
338 local expansion=$(__git_aliased_command "$command")
339 [ "$expansion" ] && command="$expansion"
341 case "$command" in
342 branch) _git_branch ;;
343 cat-file) _git_cat_file ;;
344 checkout) _git_checkout ;;
345 diff) _git_diff ;;
346 diff-tree) _git_diff_tree ;;
347 fetch) _git_fetch ;;
348 log) _git_log ;;
349 ls-remote) _git_ls_remote ;;
350 ls-tree) _git_ls_tree ;;
351 merge-base) _git_merge_base ;;
352 pull) _git_pull ;;
353 push) _git_push ;;
354 reset) _git_reset ;;
355 show) _git_show ;;
356 show-branch) _git_log ;;
357 whatchanged) _git_log ;;
358 *) COMPREPLY=() ;;
359 esac
362 _gitk ()
364 local cur="${COMP_WORDS[COMP_CWORD]}"
365 COMPREPLY=($(compgen -W "--all $(__git_refs)" -- "$cur"))
368 complete -o default -o nospace -F _git git
369 complete -o default -F _gitk gitk
370 complete -o default -F _git_branch git-branch
371 complete -o default -o nospace -F _git_cat_file git-cat-file
372 complete -o default -F _git_checkout git-checkout
373 complete -o default -o nospace -F _git_diff git-diff
374 complete -o default -F _git_diff_tree git-diff-tree
375 complete -o default -o nospace -F _git_fetch git-fetch
376 complete -o default -o nospace -F _git_log git-log
377 complete -o default -F _git_ls_remote git-ls-remote
378 complete -o default -o nospace -F _git_ls_tree git-ls-tree
379 complete -o default -F _git_merge_base git-merge-base
380 complete -o default -o nospace -F _git_pull git-pull
381 complete -o default -o nospace -F _git_push git-push
382 complete -o default -F _git_reset git-reset
383 complete -o default -F _git_show git-show
384 complete -o default -o nospace -F _git_log git-show-branch
385 complete -o default -o nospace -F _git_log git-whatchanged
387 # The following are necessary only for Cygwin, and only are needed
388 # when the user has tab-completed the executable name and consequently
389 # included the '.exe' suffix.
391 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
392 complete -o default -o nospace -F _git git.exe
393 complete -o default -F _git_branch git-branch.exe
394 complete -o default -o nospace -F _git_cat_file git-cat-file.exe
395 complete -o default -o nospace -F _git_diff git-diff.exe
396 complete -o default -o nospace -F _git_diff_tree git-diff-tree.exe
397 complete -o default -o nospace -F _git_log git-log.exe
398 complete -o default -o nospace -F _git_ls_tree git-ls-tree.exe
399 complete -o default -F _git_merge_base git-merge-base.exe
400 complete -o default -o nospace -F _git_push git-push.exe
401 complete -o default -o nospace -F _git_log git-show-branch.exe
402 complete -o default -o nospace -F _git_log git-whatchanged.exe