Teach bash about git log/show/whatchanged options.
[git/git.git] / contrib / completion / git-completion.bash
CommitLineData
690d8824
JH
1#
2# bash completion support for core Git.
3#
4# Copyright (C) 2006 Shawn Pearce
5# Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
6#
7# The contained completion routines provide support for completing:
8#
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
14#
15# To use these routines:
16#
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
20#
d3d717a4
SP
21# 3) Consider changing your PS1 to also show the current branch:
22# PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
23#
24# The argument to __git_ps1 will be displayed only if you
25# are currently in a git repository. The %s token will be
26# the name of the current branch.
27#
690d8824 28
873537fa
SP
29__gitdir ()
30{
31 echo "${__git_dir:-$(git rev-parse --git-dir 2>/dev/null)}"
32}
33
d3d717a4
SP
34__git_ps1 ()
35{
36 local b="$(git symbolic-ref HEAD 2>/dev/null)"
37 if [ -n "$b" ]; then
38 if [ -n "$1" ]; then
39 printf "$1" "${b##refs/heads/}"
40 else
41 printf " (%s)" "${b##refs/heads/}"
42 fi
43 fi
44}
45
690d8824
JH
46__git_refs ()
47{
873537fa
SP
48 local cmd i is_hash=y dir="${1:-$(__gitdir)}"
49 if [ -d "$dir" ]; then
690d8824
JH
50 cmd=git-peek-remote
51 else
52 cmd=git-ls-remote
53 fi
873537fa 54 for i in $($cmd "$dir" 2>/dev/null); do
690d8824
JH
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/}" ;;
59 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
60 n,*) is_hash=y; echo "$i" ;;
61 esac
62 done
63}
64
65__git_refs2 ()
66{
873537fa
SP
67 local cmd i is_hash=y dir="${1:-$(__gitdir)}"
68 if [ -d "$dir" ]; then
690d8824
JH
69 cmd=git-peek-remote
70 else
71 cmd=git-ls-remote
72 fi
873537fa 73 for i in $($cmd "$dir" 2>/dev/null); do
690d8824
JH
74 case "$is_hash,$i" in
75 y,*) is_hash=n ;;
76 n,*^{}) is_hash=y ;;
77 n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}:${i#refs/tags/}" ;;
78 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}:${i#refs/heads/}" ;;
79 n,*) is_hash=y; echo "$i:$i" ;;
80 esac
81 done
82}
83
84__git_remotes ()
85{
873537fa 86 local i ngoff IFS=$'\n' d="$(__gitdir)"
56fc25f2 87 shopt -q nullglob || ngoff=1
690d8824 88 shopt -s nullglob
873537fa
SP
89 for i in "$d/remotes"/*; do
90 echo ${i#$d/remotes/}
690d8824 91 done
56fc25f2 92 [ "$ngoff" ] && shopt -u nullglob
873537fa 93 for i in $(git --git-dir="$d" repo-config --list); do
56fc25f2
SP
94 case "$i" in
95 remote.*.url=*)
96 i="${i#remote.}"
97 echo "${i/.url=*/}"
98 ;;
99 esac
100 done
690d8824
JH
101}
102
4ad91321
SP
103__git_merge_strategies ()
104{
105 sed -n "/^all_strategies='/{
106 s/^all_strategies='//
107 s/'//
108 p
109 q
110 }" "$(git --exec-path)/git-merge"
111}
112
690d8824
JH
113__git_complete_file ()
114{
a79c6551 115 local pfx ls ref cur="${COMP_WORDS[COMP_CWORD]}"
690d8824
JH
116 case "$cur" in
117 ?*:*)
a79c6551
SP
118 ref="${cur%%:*}"
119 cur="${cur#*:}"
690d8824
JH
120 case "$cur" in
121 ?*/*)
a79c6551
SP
122 pfx="${cur%/*}"
123 cur="${cur##*/}"
690d8824
JH
124 ls="$ref:$pfx"
125 pfx="$pfx/"
126 ;;
127 *)
128 ls="$ref"
129 ;;
130 esac
131 COMPREPLY=($(compgen -P "$pfx" \
873537fa 132 -W "$(git --git-dir="$(__gitdir)" ls-tree "$ls" \
690d8824
JH
133 | sed '/^100... blob /s,^.* ,,
134 /^040000 tree /{
135 s,^.* ,,
136 s,$,/,
137 }
138 s/^.* //')" \
139 -- "$cur"))
140 ;;
141 *)
873537fa 142 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
690d8824
JH
143 ;;
144 esac
145}
146
f53352fb
SP
147__git_complete_revlist ()
148{
149 local pfx cur="${COMP_WORDS[COMP_CWORD]}"
150 case "$cur" in
151 *...*)
152 pfx="${cur%...*}..."
153 cur="${cur#*...}"
154 COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs)" -- "$cur"))
155 ;;
156 *..*)
157 pfx="${cur%..*}.."
158 cur="${cur#*..}"
159 COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs)" -- "$cur"))
160 ;;
161 *)
162 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
163 ;;
164 esac
165}
166
f2bb9f88
SP
167__git_commands ()
168{
169 local i IFS=" "$'\n'
170 for i in $(git help -a|egrep '^ ')
171 do
172 case $i in
173 check-ref-format) : plumbing;;
174 commit-tree) : plumbing;;
175 convert-objects) : plumbing;;
176 cvsserver) : daemon;;
177 daemon) : daemon;;
178 fetch-pack) : plumbing;;
179 hash-object) : plumbing;;
180 http-*) : transport;;
181 index-pack) : plumbing;;
182 local-fetch) : plumbing;;
183 mailinfo) : plumbing;;
184 mailsplit) : plumbing;;
185 merge-*) : plumbing;;
186 mktree) : plumbing;;
187 mktag) : plumbing;;
188 pack-objects) : plumbing;;
189 pack-redundant) : plumbing;;
190 pack-refs) : plumbing;;
191 parse-remote) : plumbing;;
192 patch-id) : plumbing;;
193 peek-remote) : plumbing;;
194 read-tree) : plumbing;;
195 receive-pack) : plumbing;;
196 rerere) : plumbing;;
197 rev-list) : plumbing;;
198 rev-parse) : plumbing;;
199 runstatus) : plumbing;;
200 sh-setup) : internal;;
201 shell) : daemon;;
202 send-pack) : plumbing;;
203 show-index) : plumbing;;
204 ssh-*) : transport;;
205 stripspace) : plumbing;;
206 symbolic-ref) : plumbing;;
207 unpack-file) : plumbing;;
208 unpack-objects) : plumbing;;
209 update-ref) : plumbing;;
210 update-server-info) : daemon;;
211 upload-archive) : plumbing;;
212 upload-pack) : plumbing;;
213 write-tree) : plumbing;;
214 *) echo $i;;
215 esac
216 done
217}
218
367dce2a
DS
219__git_aliases ()
220{
56fc25f2 221 local i IFS=$'\n'
873537fa 222 for i in $(git --git-dir="$(__gitdir)" repo-config --list); do
56fc25f2
SP
223 case "$i" in
224 alias.*)
225 i="${i#alias.}"
226 echo "${i/=*/}"
227 ;;
228 esac
229 done
367dce2a
DS
230}
231
232__git_aliased_command ()
233{
873537fa
SP
234 local word cmdline=$(git --git-dir="$(__gitdir)" \
235 repo-config --get "alias.$1")
367dce2a
DS
236 for word in $cmdline; do
237 if [ "${word##-*}" ]; then
238 echo $word
239 return
240 fi
241 done
242}
243
690d8824
JH
244_git_branch ()
245{
246 local cur="${COMP_WORDS[COMP_CWORD]}"
873537fa 247 COMPREPLY=($(compgen -W "-l -f -d -D $(__git_refs)" -- "$cur"))
690d8824
JH
248}
249
250_git_cat_file ()
251{
252 local cur="${COMP_WORDS[COMP_CWORD]}"
253 case "${COMP_WORDS[0]},$COMP_CWORD" in
254 git-cat-file*,1)
255 COMPREPLY=($(compgen -W "-p -t blob tree commit tag" -- "$cur"))
256 ;;
257 git,2)
258 COMPREPLY=($(compgen -W "-p -t blob tree commit tag" -- "$cur"))
259 ;;
260 *)
261 __git_complete_file
262 ;;
263 esac
264}
265
266_git_checkout ()
267{
268 local cur="${COMP_WORDS[COMP_CWORD]}"
873537fa 269 COMPREPLY=($(compgen -W "-l -b $(__git_refs)" -- "$cur"))
690d8824
JH
270}
271
1273231e
SP
272_git_cherry_pick ()
273{
274 local cur="${COMP_WORDS[COMP_CWORD]}"
275 case "$cur" in
276 --*)
277 COMPREPLY=($(compgen -W "
278 --edit --no-commit
279 " -- "$cur"))
280 ;;
281 *)
282 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
283 ;;
284 esac
285}
286
690d8824
JH
287_git_diff ()
288{
289 __git_complete_file
290}
291
292_git_diff_tree ()
293{
294 local cur="${COMP_WORDS[COMP_CWORD]}"
873537fa 295 COMPREPLY=($(compgen -W "-r -p -M $(__git_refs)" -- "$cur"))
690d8824
JH
296}
297
298_git_fetch ()
299{
300 local cur="${COMP_WORDS[COMP_CWORD]}"
301
302 case "${COMP_WORDS[0]},$COMP_CWORD" in
303 git-fetch*,1)
304 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
305 ;;
306 git,2)
307 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
308 ;;
309 *)
310 case "$cur" in
311 *:*)
a79c6551 312 cur="${cur#*:}"
873537fa 313 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
690d8824
JH
314 ;;
315 *)
316 local remote
317 case "${COMP_WORDS[0]}" in
318 git-fetch) remote="${COMP_WORDS[1]}" ;;
319 git) remote="${COMP_WORDS[2]}" ;;
320 esac
321 COMPREPLY=($(compgen -W "$(__git_refs2 "$remote")" -- "$cur"))
322 ;;
323 esac
324 ;;
325 esac
326}
327
f53352fb
SP
328_git_format_patch ()
329{
330 local cur="${COMP_WORDS[COMP_CWORD]}"
331 case "$cur" in
332 --*)
333 COMPREPLY=($(compgen -W "
334 --stdout --attach --thread
335 --output-directory
336 --numbered --start-number
337 --keep-subject
338 --signoff
339 --in-reply-to=
340 --full-index --binary
341 " -- "$cur"))
342 return
343 ;;
344 esac
345 __git_complete_revlist
346}
347
690d8824
JH
348_git_ls_remote ()
349{
350 local cur="${COMP_WORDS[COMP_CWORD]}"
351 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
352}
353
354_git_ls_tree ()
355{
356 __git_complete_file
357}
358
359_git_log ()
360{
6e31b866
SP
361 local cur="${COMP_WORDS[COMP_CWORD]}"
362 case "$cur" in
363 --pretty=*)
364 COMPREPLY=($(compgen -W "
365 oneline short medium full fuller email raw
366 " -- "${cur##--pretty=}"))
367 return
368 ;;
369 --*)
370 COMPREPLY=($(compgen -W "
371 --max-count= --max-age= --since= --after=
372 --min-age= --before= --until=
373 --root --not --topo-order --date-order
374 --no-merges
375 --abbrev-commit --abbrev=
376 --relative-date
377 --author= --committer= --grep=
378 --all-match
379 --pretty= --name-status --name-only
380 " -- "$cur"))
381 return
382 ;;
383 esac
f53352fb 384 __git_complete_revlist
690d8824
JH
385}
386
4ad91321
SP
387_git_merge ()
388{
389 local cur="${COMP_WORDS[COMP_CWORD]}"
390 case "$cur" in
391 --*)
392 COMPREPLY=($(compgen -W "
61d926a3 393 --no-commit --no-summary --squash --strategy
4ad91321
SP
394 " -- "$cur"))
395 return
396 esac
61d926a3
SP
397 case "${COMP_WORDS[COMP_CWORD-1]}" in
398 -s|--strategy)
4ad91321 399 COMPREPLY=($(compgen -W "$(__git_merge_strategies)" -- "$cur"))
61d926a3
SP
400 return
401 esac
402 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
4ad91321
SP
403}
404
690d8824
JH
405_git_merge_base ()
406{
407 local cur="${COMP_WORDS[COMP_CWORD]}"
873537fa 408 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
690d8824
JH
409}
410
d33909bf
SP
411_git_name_rev ()
412{
413 local cur="${COMP_WORDS[COMP_CWORD]}"
414 COMPREPLY=($(compgen -W "--tags --all --stdin" -- "$cur"))
415}
416
690d8824
JH
417_git_pull ()
418{
419 local cur="${COMP_WORDS[COMP_CWORD]}"
420
421 case "${COMP_WORDS[0]},$COMP_CWORD" in
422 git-pull*,1)
423 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
424 ;;
425 git,2)
426 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
427 ;;
428 *)
429 local remote
430 case "${COMP_WORDS[0]}" in
431 git-pull) remote="${COMP_WORDS[1]}" ;;
432 git) remote="${COMP_WORDS[2]}" ;;
433 esac
434 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
435 ;;
436 esac
437}
438
439_git_push ()
440{
441 local cur="${COMP_WORDS[COMP_CWORD]}"
442
443 case "${COMP_WORDS[0]},$COMP_CWORD" in
444 git-push*,1)
445 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
446 ;;
447 git,2)
448 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
449 ;;
450 *)
451 case "$cur" in
452 *:*)
453 local remote
454 case "${COMP_WORDS[0]}" in
455 git-push) remote="${COMP_WORDS[1]}" ;;
456 git) remote="${COMP_WORDS[2]}" ;;
457 esac
a79c6551 458 cur="${cur#*:}"
690d8824
JH
459 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
460 ;;
461 *)
873537fa 462 COMPREPLY=($(compgen -W "$(__git_refs2)" -- "$cur"))
690d8824
JH
463 ;;
464 esac
465 ;;
466 esac
467}
468
61d926a3
SP
469_git_rebase ()
470{
471 local cur="${COMP_WORDS[COMP_CWORD]}"
472 if [ -d .dotest ]; then
473 COMPREPLY=($(compgen -W "
474 --continue --skip --abort
475 " -- "$cur"))
476 return
477 fi
478 case "$cur" in
479 --*)
480 COMPREPLY=($(compgen -W "
481 --onto --merge --strategy
482 " -- "$cur"))
483 return
484 esac
485 case "${COMP_WORDS[COMP_CWORD-1]}" in
486 -s|--strategy)
487 COMPREPLY=($(compgen -W "$(__git_merge_strategies)" -- "$cur"))
488 return
489 esac
490 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
491}
492
67e78c3b
SP
493_git_reset ()
494{
495 local cur="${COMP_WORDS[COMP_CWORD]}"
496 local opt="--mixed --hard --soft"
873537fa 497 COMPREPLY=($(compgen -W "$opt $(__git_refs)" -- "$cur"))
67e78c3b
SP
498}
499
690d8824
JH
500_git ()
501{
873537fa
SP
502 local i c=1 command __git_dir
503
504 while [ $c -lt $COMP_CWORD ]; do
505 i="${COMP_WORDS[c]}"
506 case "$i" in
507 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
508 --bare) __git_dir="." ;;
509 --version|--help|-p|--paginate) ;;
510 *) command="$i"; break ;;
511 esac
512 c=$((++c))
513 done
514
515 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
f2bb9f88
SP
516 COMPREPLY=($(compgen -W "
517 --git-dir= --version --exec-path
518 $(__git_commands)
519 $(__git_aliases)
520 " -- "${COMP_WORDS[COMP_CWORD]}"))
873537fa
SP
521 return;
522 fi
367dce2a 523
873537fa
SP
524 local expansion=$(__git_aliased_command "$command")
525 [ "$expansion" ] && command="$expansion"
367dce2a 526
873537fa
SP
527 case "$command" in
528 branch) _git_branch ;;
529 cat-file) _git_cat_file ;;
530 checkout) _git_checkout ;;
1273231e 531 cherry-pick) _git_cherry_pick ;;
873537fa
SP
532 diff) _git_diff ;;
533 diff-tree) _git_diff_tree ;;
534 fetch) _git_fetch ;;
f53352fb 535 format-patch) _git_format_patch ;;
873537fa
SP
536 log) _git_log ;;
537 ls-remote) _git_ls_remote ;;
538 ls-tree) _git_ls_tree ;;
4ad91321 539 merge) _git_merge;;
873537fa 540 merge-base) _git_merge_base ;;
d33909bf 541 name-rev) _git_name_rev ;;
873537fa
SP
542 pull) _git_pull ;;
543 push) _git_push ;;
61d926a3 544 rebase) _git_rebase ;;
873537fa 545 reset) _git_reset ;;
6e31b866 546 show) _git_log ;;
873537fa
SP
547 show-branch) _git_log ;;
548 whatchanged) _git_log ;;
549 *) COMPREPLY=() ;;
550 esac
690d8824
JH
551}
552
553_gitk ()
554{
555 local cur="${COMP_WORDS[COMP_CWORD]}"
873537fa 556 COMPREPLY=($(compgen -W "--all $(__git_refs)" -- "$cur"))
690d8824
JH
557}
558
559complete -o default -o nospace -F _git git
560complete -o default -F _gitk gitk
561complete -o default -F _git_branch git-branch
562complete -o default -o nospace -F _git_cat_file git-cat-file
563complete -o default -F _git_checkout git-checkout
1273231e 564complete -o default -F _git_cherry_pick git-cherry-pick
690d8824
JH
565complete -o default -o nospace -F _git_diff git-diff
566complete -o default -F _git_diff_tree git-diff-tree
567complete -o default -o nospace -F _git_fetch git-fetch
f53352fb 568complete -o default -o nospace -F _git_format_patch git-format-patch
690d8824
JH
569complete -o default -o nospace -F _git_log git-log
570complete -o default -F _git_ls_remote git-ls-remote
571complete -o default -o nospace -F _git_ls_tree git-ls-tree
4ad91321 572complete -o default -F _git_merge git-merge
690d8824 573complete -o default -F _git_merge_base git-merge-base
d33909bf 574complete -o default -F _git_name_rev git-name-rev
690d8824
JH
575complete -o default -o nospace -F _git_pull git-pull
576complete -o default -o nospace -F _git_push git-push
61d926a3 577complete -o default -F _git_rebase git-rebase
67e78c3b 578complete -o default -F _git_reset git-reset
6e31b866 579complete -o default -F _git_log git-show
144d33de 580complete -o default -o nospace -F _git_log git-show-branch
690d8824
JH
581complete -o default -o nospace -F _git_log git-whatchanged
582
583# The following are necessary only for Cygwin, and only are needed
584# when the user has tab-completed the executable name and consequently
585# included the '.exe' suffix.
586#
76c3eb51 587if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
144d33de 588complete -o default -o nospace -F _git git.exe
dfb96092 589complete -o default -F _git_branch git-branch.exe
690d8824
JH
590complete -o default -o nospace -F _git_cat_file git-cat-file.exe
591complete -o default -o nospace -F _git_diff git-diff.exe
592complete -o default -o nospace -F _git_diff_tree git-diff-tree.exe
f53352fb 593complete -o default -o nospace -F _git_format_patch git-format-patch.exe
690d8824
JH
594complete -o default -o nospace -F _git_log git-log.exe
595complete -o default -o nospace -F _git_ls_tree git-ls-tree.exe
596complete -o default -F _git_merge_base git-merge-base.exe
d33909bf 597complete -o default -F _git_name_rev git-name-rev.exe
690d8824 598complete -o default -o nospace -F _git_push git-push.exe
6e31b866 599complete -o default -o nospace -F _git_log git-show.exe
144d33de 600complete -o default -o nospace -F _git_log git-show-branch.exe
690d8824 601complete -o default -o nospace -F _git_log git-whatchanged.exe
76c3eb51 602fi