bash: complete full refs
[git] / contrib / completion / git-completion.bash
1 #
2 # bash completion support for core Git.
3 #
4 # Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
5 # Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
6 # Distributed under the GNU General Public License, version 2.0.
7 #
8 # The contained completion routines provide support for completing:
9 #
10 #    *) local and remote branch names
11 #    *) local and remote tag names
12 #    *) .git/remotes file names
13 #    *) git 'subcommands'
14 #    *) tree paths within 'ref:path/to/file' expressions
15 #    *) common --long-options
16 #
17 # To use these routines:
18 #
19 #    1) Copy this file to somewhere (e.g. ~/.git-completion.sh).
20 #    2) Added the following line to your .bashrc:
21 #        source ~/.git-completion.sh
22 #
23 #    3) You may want to make sure the git executable is available
24 #       in your PATH before this script is sourced, as some caching
25 #       is performed while the script loads.  If git isn't found
26 #       at source time then all lookups will be done on demand,
27 #       which may be slightly slower.
28 #
29 #    4) Consider changing your PS1 to also show the current branch:
30 #        PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
31 #
32 #       The argument to __git_ps1 will be displayed only if you
33 #       are currently in a git repository.  The %s token will be
34 #       the name of the current branch.
35 #
36 # To submit patches:
37 #
38 #    *) Read Documentation/SubmittingPatches
39 #    *) Send all patches to the current maintainer:
40 #
41 #       "Shawn O. Pearce" <spearce@spearce.org>
42 #
43 #    *) Always CC the Git mailing list:
44 #
45 #       git@vger.kernel.org
46 #
47
48 case "$COMP_WORDBREAKS" in
49 *:*) : great ;;
50 *)   COMP_WORDBREAKS="$COMP_WORDBREAKS:"
51 esac
52
53 __gitdir ()
54 {
55         if [ -z "$1" ]; then
56                 if [ -n "$__git_dir" ]; then
57                         echo "$__git_dir"
58                 elif [ -d .git ]; then
59                         echo .git
60                 else
61                         git rev-parse --git-dir 2>/dev/null
62                 fi
63         elif [ -d "$1/.git" ]; then
64                 echo "$1/.git"
65         else
66                 echo "$1"
67         fi
68 }
69
70 __git_ps1 ()
71 {
72         local g="$(git rev-parse --git-dir 2>/dev/null)"
73         if [ -n "$g" ]; then
74                 local r
75                 local b
76                 if [ -d "$g/rebase-apply" ]
77                 then
78                         if test -f "$g/rebase-apply/rebasing"
79                         then
80                                 r="|REBASE"
81                         elif test -f "$g/rebase-apply/applying"
82                         then
83                                 r="|AM"
84                         else
85                                 r="|AM/REBASE"
86                         fi
87                         b="$(git symbolic-ref HEAD 2>/dev/null)"
88                 elif [ -f "$g/rebase-merge/interactive" ]
89                 then
90                         r="|REBASE-i"
91                         b="$(cat "$g/rebase-merge/head-name")"
92                 elif [ -d "$g/rebase-merge" ]
93                 then
94                         r="|REBASE-m"
95                         b="$(cat "$g/rebase-merge/head-name")"
96                 elif [ -f "$g/MERGE_HEAD" ]
97                 then
98                         r="|MERGING"
99                         b="$(git symbolic-ref HEAD 2>/dev/null)"
100                 else
101                         if [ -f "$g/BISECT_LOG" ]
102                         then
103                                 r="|BISECTING"
104                         fi
105                         if ! b="$(git symbolic-ref HEAD 2>/dev/null)"
106                         then
107                                 if ! b="$(git describe --exact-match HEAD 2>/dev/null)"
108                                 then
109                                         b="$(cut -c1-7 "$g/HEAD")..."
110                                 fi
111                         fi
112                 fi
113
114                 if [ -n "$1" ]; then
115                         printf "$1" "${b##refs/heads/}$r"
116                 else
117                         printf " (%s)" "${b##refs/heads/}$r"
118                 fi
119         fi
120 }
121
122 __gitcomp_1 ()
123 {
124         local c IFS=' '$'\t'$'\n'
125         for c in $1; do
126                 case "$c$2" in
127                 --*=*) printf %s$'\n' "$c$2" ;;
128                 *.)    printf %s$'\n' "$c$2" ;;
129                 *)     printf %s$'\n' "$c$2 " ;;
130                 esac
131         done
132 }
133
134 __gitcomp ()
135 {
136         local cur="${COMP_WORDS[COMP_CWORD]}"
137         if [ $# -gt 2 ]; then
138                 cur="$3"
139         fi
140         case "$cur" in
141         --*=)
142                 COMPREPLY=()
143                 ;;
144         *)
145                 local IFS=$'\n'
146                 COMPREPLY=($(compgen -P "$2" \
147                         -W "$(__gitcomp_1 "$1" "$4")" \
148                         -- "$cur"))
149                 ;;
150         esac
151 }
152
153 __git_heads ()
154 {
155         local cmd i is_hash=y dir="$(__gitdir "$1")"
156         if [ -d "$dir" ]; then
157                 git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
158                         refs/heads
159                 return
160         fi
161         for i in $(git ls-remote "$1" 2>/dev/null); do
162                 case "$is_hash,$i" in
163                 y,*) is_hash=n ;;
164                 n,*^{}) is_hash=y ;;
165                 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
166                 n,*) is_hash=y; echo "$i" ;;
167                 esac
168         done
169 }
170
171 __git_tags ()
172 {
173         local cmd i is_hash=y dir="$(__gitdir "$1")"
174         if [ -d "$dir" ]; then
175                 git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
176                         refs/tags
177                 return
178         fi
179         for i in $(git ls-remote "$1" 2>/dev/null); do
180                 case "$is_hash,$i" in
181                 y,*) is_hash=n ;;
182                 n,*^{}) is_hash=y ;;
183                 n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
184                 n,*) is_hash=y; echo "$i" ;;
185                 esac
186         done
187 }
188
189 __git_refs ()
190 {
191         local i is_hash=y dir="$(__gitdir "$1")"
192         local cur="${COMP_WORDS[COMP_CWORD]}" format refs
193         if [ -d "$dir" ]; then
194                 case "$cur" in
195                 refs|refs/*)
196                         format="refname"
197                         refs="${cur%/*}"
198                         ;;
199                 *)
200                         if [ -e "$dir/HEAD" ]; then echo HEAD; fi
201                         format="refname:short"
202                         refs="refs/tags refs/heads refs/remotes"
203                         ;;
204                 esac
205                 git --git-dir="$dir" for-each-ref --format="%($format)" \
206                         $refs
207                 return
208         fi
209         for i in $(git ls-remote "$dir" 2>/dev/null); do
210                 case "$is_hash,$i" in
211                 y,*) is_hash=n ;;
212                 n,*^{}) is_hash=y ;;
213                 n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
214                 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
215                 n,refs/remotes/*) is_hash=y; echo "${i#refs/remotes/}" ;;
216                 n,*) is_hash=y; echo "$i" ;;
217                 esac
218         done
219 }
220
221 __git_refs2 ()
222 {
223         local i
224         for i in $(__git_refs "$1"); do
225                 echo "$i:$i"
226         done
227 }
228
229 __git_refs_remotes ()
230 {
231         local cmd i is_hash=y
232         for i in $(git ls-remote "$1" 2>/dev/null); do
233                 case "$is_hash,$i" in
234                 n,refs/heads/*)
235                         is_hash=y
236                         echo "$i:refs/remotes/$1/${i#refs/heads/}"
237                         ;;
238                 y,*) is_hash=n ;;
239                 n,*^{}) is_hash=y ;;
240                 n,refs/tags/*) is_hash=y;;
241                 n,*) is_hash=y; ;;
242                 esac
243         done
244 }
245
246 __git_remotes ()
247 {
248         local i ngoff IFS=$'\n' d="$(__gitdir)"
249         shopt -q nullglob || ngoff=1
250         shopt -s nullglob
251         for i in "$d/remotes"/*; do
252                 echo ${i#$d/remotes/}
253         done
254         [ "$ngoff" ] && shopt -u nullglob
255         for i in $(git --git-dir="$d" config --list); do
256                 case "$i" in
257                 remote.*.url=*)
258                         i="${i#remote.}"
259                         echo "${i/.url=*/}"
260                         ;;
261                 esac
262         done
263 }
264
265 __git_merge_strategies ()
266 {
267         if [ -n "$__git_merge_strategylist" ]; then
268                 echo "$__git_merge_strategylist"
269                 return
270         fi
271         git merge -s help 2>&1 |
272         sed -n -e '/[Aa]vailable strategies are: /,/^$/{
273                 s/\.$//
274                 s/.*://
275                 s/^[    ]*//
276                 s/[     ]*$//
277                 p
278         }'
279 }
280 __git_merge_strategylist=
281 __git_merge_strategylist=$(__git_merge_strategies 2>/dev/null)
282
283 __git_complete_file ()
284 {
285         local pfx ls ref cur="${COMP_WORDS[COMP_CWORD]}"
286         case "$cur" in
287         ?*:*)
288                 ref="${cur%%:*}"
289                 cur="${cur#*:}"
290                 case "$cur" in
291                 ?*/*)
292                         pfx="${cur%/*}"
293                         cur="${cur##*/}"
294                         ls="$ref:$pfx"
295                         pfx="$pfx/"
296                         ;;
297                 *)
298                         ls="$ref"
299                         ;;
300             esac
301
302                 case "$COMP_WORDBREAKS" in
303                 *:*) : great ;;
304                 *)   pfx="$ref:$pfx" ;;
305                 esac
306
307                 local IFS=$'\n'
308                 COMPREPLY=($(compgen -P "$pfx" \
309                         -W "$(git --git-dir="$(__gitdir)" ls-tree "$ls" \
310                                 | sed '/^100... blob /{
311                                            s,^.*        ,,
312                                            s,$, ,
313                                        }
314                                        /^120000 blob /{
315                                            s,^.*        ,,
316                                            s,$, ,
317                                        }
318                                        /^040000 tree /{
319                                            s,^.*        ,,
320                                            s,$,/,
321                                        }
322                                        s/^.*    //')" \
323                         -- "$cur"))
324                 ;;
325         *)
326                 __gitcomp "$(__git_refs)"
327                 ;;
328         esac
329 }
330
331 __git_complete_revlist ()
332 {
333         local pfx cur="${COMP_WORDS[COMP_CWORD]}"
334         case "$cur" in
335         *...*)
336                 pfx="${cur%...*}..."
337                 cur="${cur#*...}"
338                 __gitcomp "$(__git_refs)" "$pfx" "$cur"
339                 ;;
340         *..*)
341                 pfx="${cur%..*}.."
342                 cur="${cur#*..}"
343                 __gitcomp "$(__git_refs)" "$pfx" "$cur"
344                 ;;
345         *)
346                 __gitcomp "$(__git_refs)"
347                 ;;
348         esac
349 }
350
351 __git_all_commands ()
352 {
353         if [ -n "$__git_all_commandlist" ]; then
354                 echo "$__git_all_commandlist"
355                 return
356         fi
357         local i IFS=" "$'\n'
358         for i in $(git help -a|egrep '^ ')
359         do
360                 case $i in
361                 *--*)             : helper pattern;;
362                 *) echo $i;;
363                 esac
364         done
365 }
366 __git_all_commandlist=
367 __git_all_commandlist="$(__git_all_commands 2>/dev/null)"
368
369 __git_porcelain_commands ()
370 {
371         if [ -n "$__git_porcelain_commandlist" ]; then
372                 echo "$__git_porcelain_commandlist"
373                 return
374         fi
375         local i IFS=" "$'\n'
376         for i in "help" $(__git_all_commands)
377         do
378                 case $i in
379                 *--*)             : helper pattern;;
380                 applymbox)        : ask gittus;;
381                 applypatch)       : ask gittus;;
382                 archimport)       : import;;
383                 cat-file)         : plumbing;;
384                 check-attr)       : plumbing;;
385                 check-ref-format) : plumbing;;
386                 checkout-index)   : plumbing;;
387                 commit-tree)      : plumbing;;
388                 count-objects)    : infrequent;;
389                 cvsexportcommit)  : export;;
390                 cvsimport)        : import;;
391                 cvsserver)        : daemon;;
392                 daemon)           : daemon;;
393                 diff-files)       : plumbing;;
394                 diff-index)       : plumbing;;
395                 diff-tree)        : plumbing;;
396                 fast-import)      : import;;
397                 fast-export)      : export;;
398                 fsck-objects)     : plumbing;;
399                 fetch-pack)       : plumbing;;
400                 fmt-merge-msg)    : plumbing;;
401                 for-each-ref)     : plumbing;;
402                 hash-object)      : plumbing;;
403                 http-*)           : transport;;
404                 index-pack)       : plumbing;;
405                 init-db)          : deprecated;;
406                 local-fetch)      : plumbing;;
407                 lost-found)       : infrequent;;
408                 ls-files)         : plumbing;;
409                 ls-remote)        : plumbing;;
410                 ls-tree)          : plumbing;;
411                 mailinfo)         : plumbing;;
412                 mailsplit)        : plumbing;;
413                 merge-*)          : plumbing;;
414                 mktree)           : plumbing;;
415                 mktag)            : plumbing;;
416                 pack-objects)     : plumbing;;
417                 pack-redundant)   : plumbing;;
418                 pack-refs)        : plumbing;;
419                 parse-remote)     : plumbing;;
420                 patch-id)         : plumbing;;
421                 peek-remote)      : plumbing;;
422                 prune)            : plumbing;;
423                 prune-packed)     : plumbing;;
424                 quiltimport)      : import;;
425                 read-tree)        : plumbing;;
426                 receive-pack)     : plumbing;;
427                 reflog)           : plumbing;;
428                 repo-config)      : deprecated;;
429                 rerere)           : plumbing;;
430                 rev-list)         : plumbing;;
431                 rev-parse)        : plumbing;;
432                 runstatus)        : plumbing;;
433                 sh-setup)         : internal;;
434                 shell)            : daemon;;
435                 show-ref)         : plumbing;;
436                 send-pack)        : plumbing;;
437                 show-index)       : plumbing;;
438                 ssh-*)            : transport;;
439                 stripspace)       : plumbing;;
440                 symbolic-ref)     : plumbing;;
441                 tar-tree)         : deprecated;;
442                 unpack-file)      : plumbing;;
443                 unpack-objects)   : plumbing;;
444                 update-index)     : plumbing;;
445                 update-ref)       : plumbing;;
446                 update-server-info) : daemon;;
447                 upload-archive)   : plumbing;;
448                 upload-pack)      : plumbing;;
449                 write-tree)       : plumbing;;
450                 var)              : infrequent;;
451                 verify-pack)      : infrequent;;
452                 verify-tag)       : plumbing;;
453                 *) echo $i;;
454                 esac
455         done
456 }
457 __git_porcelain_commandlist=
458 __git_porcelain_commandlist="$(__git_porcelain_commands 2>/dev/null)"
459
460 __git_aliases ()
461 {
462         local i IFS=$'\n'
463         for i in $(git --git-dir="$(__gitdir)" config --list); do
464                 case "$i" in
465                 alias.*)
466                         i="${i#alias.}"
467                         echo "${i/=*/}"
468                         ;;
469                 esac
470         done
471 }
472
473 __git_aliased_command ()
474 {
475         local word cmdline=$(git --git-dir="$(__gitdir)" \
476                 config --get "alias.$1")
477         for word in $cmdline; do
478                 if [ "${word##-*}" ]; then
479                         echo $word
480                         return
481                 fi
482         done
483 }
484
485 __git_find_subcommand ()
486 {
487         local word subcommand c=1
488
489         while [ $c -lt $COMP_CWORD ]; do
490                 word="${COMP_WORDS[c]}"
491                 for subcommand in $1; do
492                         if [ "$subcommand" = "$word" ]; then
493                                 echo "$subcommand"
494                                 return
495                         fi
496                 done
497                 c=$((++c))
498         done
499 }
500
501 __git_has_doubledash ()
502 {
503         local c=1
504         while [ $c -lt $COMP_CWORD ]; do
505                 if [ "--" = "${COMP_WORDS[c]}" ]; then
506                         return 0
507                 fi
508                 c=$((++c))
509         done
510         return 1
511 }
512
513 __git_whitespacelist="nowarn warn error error-all fix"
514
515 _git_am ()
516 {
517         local cur="${COMP_WORDS[COMP_CWORD]}" dir="$(__gitdir)"
518         if [ -d "$dir"/rebase-apply ]; then
519                 __gitcomp "--skip --resolved --abort"
520                 return
521         fi
522         case "$cur" in
523         --whitespace=*)
524                 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
525                 return
526                 ;;
527         --*)
528                 __gitcomp "
529                         --signoff --utf8 --binary --3way --interactive
530                         --whitespace=
531                         "
532                 return
533         esac
534         COMPREPLY=()
535 }
536
537 _git_apply ()
538 {
539         local cur="${COMP_WORDS[COMP_CWORD]}"
540         case "$cur" in
541         --whitespace=*)
542                 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
543                 return
544                 ;;
545         --*)
546                 __gitcomp "
547                         --stat --numstat --summary --check --index
548                         --cached --index-info --reverse --reject --unidiff-zero
549                         --apply --no-add --exclude=
550                         --whitespace= --inaccurate-eof --verbose
551                         "
552                 return
553         esac
554         COMPREPLY=()
555 }
556
557 _git_add ()
558 {
559         __git_has_doubledash && return
560
561         local cur="${COMP_WORDS[COMP_CWORD]}"
562         case "$cur" in
563         --*)
564                 __gitcomp "
565                         --interactive --refresh --patch --update --dry-run
566                         --ignore-errors
567                         "
568                 return
569         esac
570         COMPREPLY=()
571 }
572
573 _git_archive ()
574 {
575         local cur="${COMP_WORDS[COMP_CWORD]}"
576         case "$cur" in
577         --format=*)
578                 __gitcomp "$(git archive --list)" "" "${cur##--format=}"
579                 return
580                 ;;
581         --remote=*)
582                 __gitcomp "$(__git_remotes)" "" "${cur##--remote=}"
583                 return
584                 ;;
585         --*)
586                 __gitcomp "
587                         --format= --list --verbose
588                         --prefix= --remote= --exec=
589                         "
590                 return
591                 ;;
592         esac
593         __git_complete_file
594 }
595
596 _git_bisect ()
597 {
598         __git_has_doubledash && return
599
600         local subcommands="start bad good skip reset visualize replay log run"
601         local subcommand="$(__git_find_subcommand "$subcommands")"
602         if [ -z "$subcommand" ]; then
603                 __gitcomp "$subcommands"
604                 return
605         fi
606
607         case "$subcommand" in
608         bad|good|reset|skip)
609                 __gitcomp "$(__git_refs)"
610                 ;;
611         *)
612                 COMPREPLY=()
613                 ;;
614         esac
615 }
616
617 _git_branch ()
618 {
619         local i c=1 only_local_ref="n" has_r="n"
620
621         while [ $c -lt $COMP_CWORD ]; do
622                 i="${COMP_WORDS[c]}"
623                 case "$i" in
624                 -d|-m)  only_local_ref="y" ;;
625                 -r)     has_r="y" ;;
626                 esac
627                 c=$((++c))
628         done
629
630         case "${COMP_WORDS[COMP_CWORD]}" in
631         --*=*)  COMPREPLY=() ;;
632         --*)
633                 __gitcomp "
634                         --color --no-color --verbose --abbrev= --no-abbrev
635                         --track --no-track --contains --merged --no-merged
636                         "
637                 ;;
638         *)
639                 if [ $only_local_ref = "y" -a $has_r = "n" ]; then
640                         __gitcomp "$(__git_heads)"
641                 else
642                         __gitcomp "$(__git_refs)"
643                 fi
644                 ;;
645         esac
646 }
647
648 _git_bundle ()
649 {
650         local mycword="$COMP_CWORD"
651         case "${COMP_WORDS[0]}" in
652         git)
653                 local cmd="${COMP_WORDS[2]}"
654                 mycword="$((mycword-1))"
655                 ;;
656         git-bundle*)
657                 local cmd="${COMP_WORDS[1]}"
658                 ;;
659         esac
660         case "$mycword" in
661         1)
662                 __gitcomp "create list-heads verify unbundle"
663                 ;;
664         2)
665                 # looking for a file
666                 ;;
667         *)
668                 case "$cmd" in
669                         create)
670                                 __git_complete_revlist
671                         ;;
672                 esac
673                 ;;
674         esac
675 }
676
677 _git_checkout ()
678 {
679         __git_has_doubledash && return
680
681         __gitcomp "$(__git_refs)"
682 }
683
684 _git_cherry ()
685 {
686         __gitcomp "$(__git_refs)"
687 }
688
689 _git_cherry_pick ()
690 {
691         local cur="${COMP_WORDS[COMP_CWORD]}"
692         case "$cur" in
693         --*)
694                 __gitcomp "--edit --no-commit"
695                 ;;
696         *)
697                 __gitcomp "$(__git_refs)"
698                 ;;
699         esac
700 }
701
702 _git_clean ()
703 {
704         __git_has_doubledash && return
705
706         local cur="${COMP_WORDS[COMP_CWORD]}"
707         case "$cur" in
708         --*)
709                 __gitcomp "--dry-run --quiet"
710                 return
711                 ;;
712         esac
713         COMPREPLY=()
714 }
715
716 _git_clone ()
717 {
718         local cur="${COMP_WORDS[COMP_CWORD]}"
719         case "$cur" in
720         --*)
721                 __gitcomp "
722                         --local
723                         --no-hardlinks
724                         --shared
725                         --reference
726                         --quiet
727                         --no-checkout
728                         --bare
729                         --mirror
730                         --origin
731                         --upload-pack
732                         --template=
733                         --depth
734                         "
735                 return
736                 ;;
737         esac
738         COMPREPLY=()
739 }
740
741 _git_commit ()
742 {
743         __git_has_doubledash && return
744
745         local cur="${COMP_WORDS[COMP_CWORD]}"
746         case "$cur" in
747         --*)
748                 __gitcomp "
749                         --all --author= --signoff --verify --no-verify
750                         --edit --amend --include --only --interactive
751                         "
752                 return
753         esac
754         COMPREPLY=()
755 }
756
757 _git_describe ()
758 {
759         local cur="${COMP_WORDS[COMP_CWORD]}"
760         case "$cur" in
761         --*)
762                 __gitcomp "
763                         --all --tags --contains --abbrev= --candidates=
764                         --exact-match --debug --long --match --always
765                         "
766                 return
767         esac
768         __gitcomp "$(__git_refs)"
769 }
770
771 _git_diff ()
772 {
773         __git_has_doubledash && return
774
775         local cur="${COMP_WORDS[COMP_CWORD]}"
776         case "$cur" in
777         --*)
778                 __gitcomp "--cached --stat --numstat --shortstat --summary
779                         --patch-with-stat --name-only --name-status --color
780                         --no-color --color-words --no-renames --check
781                         --full-index --binary --abbrev --diff-filter=
782                         --find-copies-harder --pickaxe-all --pickaxe-regex
783                         --text --ignore-space-at-eol --ignore-space-change
784                         --ignore-all-space --exit-code --quiet --ext-diff
785                         --no-ext-diff
786                         --no-prefix --src-prefix= --dst-prefix=
787                         --base --ours --theirs
788                         "
789                 return
790                 ;;
791         esac
792         __git_complete_file
793 }
794
795 _git_fetch ()
796 {
797         local cur="${COMP_WORDS[COMP_CWORD]}"
798
799         if [ "$COMP_CWORD" = 2 ]; then
800                 __gitcomp "$(__git_remotes)"
801         else
802                 case "$cur" in
803                 *:*)
804                         local pfx=""
805                         case "$COMP_WORDBREAKS" in
806                         *:*) : great ;;
807                         *)   pfx="${cur%%:*}:" ;;
808                         esac
809                         __gitcomp "$(__git_refs)" "$pfx" "${cur#*:}"
810                         ;;
811                 *)
812                         local remote
813                         case "${COMP_WORDS[0]}" in
814                         git-fetch) remote="${COMP_WORDS[1]}" ;;
815                         git)       remote="${COMP_WORDS[2]}" ;;
816                         esac
817                         __gitcomp "$(__git_refs2 "$remote")"
818                         ;;
819                 esac
820         fi
821 }
822
823 _git_format_patch ()
824 {
825         local cur="${COMP_WORDS[COMP_CWORD]}"
826         case "$cur" in
827         --*)
828                 __gitcomp "
829                         --stdout --attach --thread
830                         --output-directory
831                         --numbered --start-number
832                         --numbered-files
833                         --keep-subject
834                         --signoff
835                         --in-reply-to=
836                         --full-index --binary
837                         --not --all
838                         --cover-letter
839                         --no-prefix --src-prefix= --dst-prefix=
840                         "
841                 return
842                 ;;
843         esac
844         __git_complete_revlist
845 }
846
847 _git_gc ()
848 {
849         local cur="${COMP_WORDS[COMP_CWORD]}"
850         case "$cur" in
851         --*)
852                 __gitcomp "--prune --aggressive"
853                 return
854                 ;;
855         esac
856         COMPREPLY=()
857 }
858
859 _git_grep ()
860 {
861         __git_has_doubledash && return
862
863         local cur="${COMP_WORDS[COMP_CWORD]}"
864         case "$cur" in
865         --*)
866                 __gitcomp "
867                         --cached
868                         --text --ignore-case --word-regexp --invert-match
869                         --full-name
870                         --extended-regexp --basic-regexp --fixed-strings
871                         --files-with-matches --name-only
872                         --files-without-match
873                         --count
874                         --and --or --not --all-match
875                         "
876                 return
877                 ;;
878         esac
879         COMPREPLY=()
880 }
881
882 _git_help ()
883 {
884         local cur="${COMP_WORDS[COMP_CWORD]}"
885         case "$cur" in
886         --*)
887                 __gitcomp "--all --info --man --web"
888                 return
889                 ;;
890         esac
891         __gitcomp "$(__git_all_commands)
892                 attributes cli core-tutorial cvs-migration
893                 diffcore gitk glossary hooks ignore modules
894                 repository-layout tutorial tutorial-2
895                 workflows
896                 "
897 }
898
899 _git_init ()
900 {
901         local cur="${COMP_WORDS[COMP_CWORD]}"
902         case "$cur" in
903         --shared=*)
904                 __gitcomp "
905                         false true umask group all world everybody
906                         " "" "${cur##--shared=}"
907                 return
908                 ;;
909         --*)
910                 __gitcomp "--quiet --bare --template= --shared --shared="
911                 return
912                 ;;
913         esac
914         COMPREPLY=()
915 }
916
917 _git_ls_files ()
918 {
919         __git_has_doubledash && return
920
921         local cur="${COMP_WORDS[COMP_CWORD]}"
922         case "$cur" in
923         --*)
924                 __gitcomp "--cached --deleted --modified --others --ignored
925                         --stage --directory --no-empty-directory --unmerged
926                         --killed --exclude= --exclude-from=
927                         --exclude-per-directory= --exclude-standard
928                         --error-unmatch --with-tree= --full-name
929                         --abbrev --ignored --exclude-per-directory
930                         "
931                 return
932                 ;;
933         esac
934         COMPREPLY=()
935 }
936
937 _git_ls_remote ()
938 {
939         __gitcomp "$(__git_remotes)"
940 }
941
942 _git_ls_tree ()
943 {
944         __git_complete_file
945 }
946
947 _git_log ()
948 {
949         __git_has_doubledash && return
950
951         local cur="${COMP_WORDS[COMP_CWORD]}"
952         case "$cur" in
953         --pretty=*)
954                 __gitcomp "
955                         oneline short medium full fuller email raw
956                         " "" "${cur##--pretty=}"
957                 return
958                 ;;
959         --date=*)
960                 __gitcomp "
961                         relative iso8601 rfc2822 short local default
962                 " "" "${cur##--date=}"
963                 return
964                 ;;
965         --*)
966                 __gitcomp "
967                         --max-count= --max-age= --since= --after=
968                         --min-age= --before= --until=
969                         --root --topo-order --date-order --reverse
970                         --no-merges --follow
971                         --abbrev-commit --abbrev=
972                         --relative-date --date=
973                         --author= --committer= --grep=
974                         --all-match
975                         --pretty= --name-status --name-only --raw
976                         --not --all
977                         --left-right --cherry-pick
978                         --graph
979                         --stat --numstat --shortstat
980                         --decorate --diff-filter=
981                         --color-words --walk-reflogs
982                         --parents --children --full-history
983                         --merge
984                         "
985                 return
986                 ;;
987         esac
988         __git_complete_revlist
989 }
990
991 _git_merge ()
992 {
993         local cur="${COMP_WORDS[COMP_CWORD]}"
994         case "${COMP_WORDS[COMP_CWORD-1]}" in
995         -s|--strategy)
996                 __gitcomp "$(__git_merge_strategies)"
997                 return
998         esac
999         case "$cur" in
1000         --strategy=*)
1001                 __gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
1002                 return
1003                 ;;
1004         --*)
1005                 __gitcomp "
1006                         --no-commit --no-stat --log --no-log --squash --strategy
1007                         "
1008                 return
1009         esac
1010         __gitcomp "$(__git_refs)"
1011 }
1012
1013 _git_mergetool ()
1014 {
1015         local cur="${COMP_WORDS[COMP_CWORD]}"
1016         case "$cur" in
1017         --tool=*)
1018                 __gitcomp "
1019                         kdiff3 tkdiff meld xxdiff emerge
1020                         vimdiff gvimdiff ecmerge opendiff
1021                         " "" "${cur##--tool=}"
1022                 return
1023                 ;;
1024         --*)
1025                 __gitcomp "--tool="
1026                 return
1027                 ;;
1028         esac
1029         COMPREPLY=()
1030 }
1031
1032 _git_merge_base ()
1033 {
1034         __gitcomp "$(__git_refs)"
1035 }
1036
1037 _git_mv ()
1038 {
1039         local cur="${COMP_WORDS[COMP_CWORD]}"
1040         case "$cur" in
1041         --*)
1042                 __gitcomp "--dry-run"
1043                 return
1044                 ;;
1045         esac
1046         COMPREPLY=()
1047 }
1048
1049 _git_name_rev ()
1050 {
1051         __gitcomp "--tags --all --stdin"
1052 }
1053
1054 _git_pull ()
1055 {
1056         local cur="${COMP_WORDS[COMP_CWORD]}"
1057
1058         if [ "$COMP_CWORD" = 2 ]; then
1059                 __gitcomp "$(__git_remotes)"
1060         else
1061                 local remote
1062                 case "${COMP_WORDS[0]}" in
1063                 git-pull)  remote="${COMP_WORDS[1]}" ;;
1064                 git)       remote="${COMP_WORDS[2]}" ;;
1065                 esac
1066                 __gitcomp "$(__git_refs "$remote")"
1067         fi
1068 }
1069
1070 _git_push ()
1071 {
1072         local cur="${COMP_WORDS[COMP_CWORD]}"
1073
1074         if [ "$COMP_CWORD" = 2 ]; then
1075                 __gitcomp "$(__git_remotes)"
1076         else
1077                 case "$cur" in
1078                 *:*)
1079                         local remote
1080                         case "${COMP_WORDS[0]}" in
1081                         git-push)  remote="${COMP_WORDS[1]}" ;;
1082                         git)       remote="${COMP_WORDS[2]}" ;;
1083                         esac
1084
1085                         local pfx=""
1086                         case "$COMP_WORDBREAKS" in
1087                         *:*) : great ;;
1088                         *)   pfx="${cur%%:*}:" ;;
1089                         esac
1090
1091                         __gitcomp "$(__git_refs "$remote")" "$pfx" "${cur#*:}"
1092                         ;;
1093                 +*)
1094                         __gitcomp "$(__git_refs)" + "${cur#+}"
1095                         ;;
1096                 *)
1097                         __gitcomp "$(__git_refs)"
1098                         ;;
1099                 esac
1100         fi
1101 }
1102
1103 _git_rebase ()
1104 {
1105         local cur="${COMP_WORDS[COMP_CWORD]}" dir="$(__gitdir)"
1106         if [ -d "$dir"/rebase-apply ] || [ -d "$dir"/rebase-merge ]; then
1107                 __gitcomp "--continue --skip --abort"
1108                 return
1109         fi
1110         case "${COMP_WORDS[COMP_CWORD-1]}" in
1111         -s|--strategy)
1112                 __gitcomp "$(__git_merge_strategies)"
1113                 return
1114         esac
1115         case "$cur" in
1116         --strategy=*)
1117                 __gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
1118                 return
1119                 ;;
1120         --*)
1121                 __gitcomp "--onto --merge --strategy --interactive"
1122                 return
1123         esac
1124         __gitcomp "$(__git_refs)"
1125 }
1126
1127 _git_send_email ()
1128 {
1129         local cur="${COMP_WORDS[COMP_CWORD]}"
1130         case "$cur" in
1131         --*)
1132                 __gitcomp "--bcc --cc --cc-cmd --chain-reply-to --compose
1133                         --dry-run --envelope-sender --from --identity
1134                         --in-reply-to --no-chain-reply-to --no-signed-off-by-cc
1135                         --no-suppress-from --no-thread --quiet
1136                         --signed-off-by-cc --smtp-pass --smtp-server
1137                         --smtp-server-port --smtp-ssl --smtp-user --subject
1138                         --suppress-cc --suppress-from --thread --to
1139                         --validate --no-validate"
1140                 return
1141                 ;;
1142         esac
1143         COMPREPLY=()
1144 }
1145
1146 _git_config ()
1147 {
1148         local cur="${COMP_WORDS[COMP_CWORD]}"
1149         local prv="${COMP_WORDS[COMP_CWORD-1]}"
1150         case "$prv" in
1151         branch.*.remote)
1152                 __gitcomp "$(__git_remotes)"
1153                 return
1154                 ;;
1155         branch.*.merge)
1156                 __gitcomp "$(__git_refs)"
1157                 return
1158                 ;;
1159         remote.*.fetch)
1160                 local remote="${prv#remote.}"
1161                 remote="${remote%.fetch}"
1162                 __gitcomp "$(__git_refs_remotes "$remote")"
1163                 return
1164                 ;;
1165         remote.*.push)
1166                 local remote="${prv#remote.}"
1167                 remote="${remote%.push}"
1168                 __gitcomp "$(git --git-dir="$(__gitdir)" \
1169                         for-each-ref --format='%(refname):%(refname)' \
1170                         refs/heads)"
1171                 return
1172                 ;;
1173         pull.twohead|pull.octopus)
1174                 __gitcomp "$(__git_merge_strategies)"
1175                 return
1176                 ;;
1177         color.branch|color.diff|color.status)
1178                 __gitcomp "always never auto"
1179                 return
1180                 ;;
1181         color.*.*)
1182                 __gitcomp "
1183                         black red green yellow blue magenta cyan white
1184                         bold dim ul blink reverse
1185                         "
1186                 return
1187                 ;;
1188         *.*)
1189                 COMPREPLY=()
1190                 return
1191                 ;;
1192         esac
1193         case "$cur" in
1194         --*)
1195                 __gitcomp "
1196                         --global --system --file=
1197                         --list --replace-all
1198                         --get --get-all --get-regexp
1199                         --add --unset --unset-all
1200                         --remove-section --rename-section
1201                         "
1202                 return
1203                 ;;
1204         branch.*.*)
1205                 local pfx="${cur%.*}."
1206                 cur="${cur##*.}"
1207                 __gitcomp "remote merge" "$pfx" "$cur"
1208                 return
1209                 ;;
1210         branch.*)
1211                 local pfx="${cur%.*}."
1212                 cur="${cur#*.}"
1213                 __gitcomp "$(__git_heads)" "$pfx" "$cur" "."
1214                 return
1215                 ;;
1216         remote.*.*)
1217                 local pfx="${cur%.*}."
1218                 cur="${cur##*.}"
1219                 __gitcomp "
1220                         url fetch push skipDefaultUpdate
1221                         receivepack uploadpack tagopt
1222                         " "$pfx" "$cur"
1223                 return
1224                 ;;
1225         remote.*)
1226                 local pfx="${cur%.*}."
1227                 cur="${cur#*.}"
1228                 __gitcomp "$(__git_remotes)" "$pfx" "$cur" "."
1229                 return
1230                 ;;
1231         esac
1232         __gitcomp "
1233                 apply.whitespace
1234                 core.fileMode
1235                 core.gitProxy
1236                 core.ignoreStat
1237                 core.preferSymlinkRefs
1238                 core.logAllRefUpdates
1239                 core.loosecompression
1240                 core.repositoryFormatVersion
1241                 core.sharedRepository
1242                 core.warnAmbiguousRefs
1243                 core.compression
1244                 core.packedGitWindowSize
1245                 core.packedGitLimit
1246                 clean.requireForce
1247                 color.branch
1248                 color.branch.current
1249                 color.branch.local
1250                 color.branch.remote
1251                 color.branch.plain
1252                 color.diff
1253                 color.diff.plain
1254                 color.diff.meta
1255                 color.diff.frag
1256                 color.diff.old
1257                 color.diff.new
1258                 color.diff.commit
1259                 color.diff.whitespace
1260                 color.pager
1261                 color.status
1262                 color.status.header
1263                 color.status.added
1264                 color.status.changed
1265                 color.status.untracked
1266                 diff.renameLimit
1267                 diff.renames
1268                 fetch.unpackLimit
1269                 format.headers
1270                 format.subjectprefix
1271                 gitcvs.enabled
1272                 gitcvs.logfile
1273                 gitcvs.allbinary
1274                 gitcvs.dbname gitcvs.dbdriver gitcvs.dbuser gitcvs.dbpass
1275                 gitcvs.dbtablenameprefix
1276                 gc.packrefs
1277                 gc.reflogexpire
1278                 gc.reflogexpireunreachable
1279                 gc.rerereresolved
1280                 gc.rerereunresolved
1281                 http.sslVerify
1282                 http.sslCert
1283                 http.sslKey
1284                 http.sslCAInfo
1285                 http.sslCAPath
1286                 http.maxRequests
1287                 http.lowSpeedLimit
1288                 http.lowSpeedTime
1289                 http.noEPSV
1290                 i18n.commitEncoding
1291                 i18n.logOutputEncoding
1292                 log.showroot
1293                 merge.tool
1294                 merge.summary
1295                 merge.verbosity
1296                 pack.window
1297                 pack.depth
1298                 pack.windowMemory
1299                 pack.compression
1300                 pack.deltaCacheSize
1301                 pack.deltaCacheLimit
1302                 pull.octopus
1303                 pull.twohead
1304                 repack.useDeltaBaseOffset
1305                 showbranch.default
1306                 tar.umask
1307                 transfer.unpackLimit
1308                 receive.unpackLimit
1309                 receive.denyNonFastForwards
1310                 user.name
1311                 user.email
1312                 user.signingkey
1313                 branch. remote.
1314         "
1315 }
1316
1317 _git_remote ()
1318 {
1319         local subcommands="add rm show prune update"
1320         local subcommand="$(__git_find_subcommand "$subcommands")"
1321         if [ -z "$subcommand" ]; then
1322                 __gitcomp "$subcommands"
1323                 return
1324         fi
1325
1326         case "$subcommand" in
1327         rm|show|prune)
1328                 __gitcomp "$(__git_remotes)"
1329                 ;;
1330         update)
1331                 local i c='' IFS=$'\n'
1332                 for i in $(git --git-dir="$(__gitdir)" config --list); do
1333                         case "$i" in
1334                         remotes.*)
1335                                 i="${i#remotes.}"
1336                                 c="$c ${i/=*/}"
1337                                 ;;
1338                         esac
1339                 done
1340                 __gitcomp "$c"
1341                 ;;
1342         *)
1343                 COMPREPLY=()
1344                 ;;
1345         esac
1346 }
1347
1348 _git_reset ()
1349 {
1350         __git_has_doubledash && return
1351
1352         local cur="${COMP_WORDS[COMP_CWORD]}"
1353         case "$cur" in
1354         --*)
1355                 __gitcomp "--mixed --hard --soft"
1356                 return
1357                 ;;
1358         esac
1359         __gitcomp "$(__git_refs)"
1360 }
1361
1362 _git_revert ()
1363 {
1364         local cur="${COMP_WORDS[COMP_CWORD]}"
1365         case "$cur" in
1366         --*)
1367                 __gitcomp "--edit --mainline --no-edit --no-commit --signoff"
1368                 return
1369                 ;;
1370         esac
1371         COMPREPLY=()
1372 }
1373
1374 _git_rm ()
1375 {
1376         __git_has_doubledash && return
1377
1378         local cur="${COMP_WORDS[COMP_CWORD]}"
1379         case "$cur" in
1380         --*)
1381                 __gitcomp "--cached --dry-run --ignore-unmatch --quiet"
1382                 return
1383                 ;;
1384         esac
1385         COMPREPLY=()
1386 }
1387
1388 _git_shortlog ()
1389 {
1390         __git_has_doubledash && return
1391
1392         local cur="${COMP_WORDS[COMP_CWORD]}"
1393         case "$cur" in
1394         --*)
1395                 __gitcomp "
1396                         --max-count= --max-age= --since= --after=
1397                         --min-age= --before= --until=
1398                         --no-merges
1399                         --author= --committer= --grep=
1400                         --all-match
1401                         --not --all
1402                         --numbered --summary
1403                         "
1404                 return
1405                 ;;
1406         esac
1407         __git_complete_revlist
1408 }
1409
1410 _git_show ()
1411 {
1412         __git_has_doubledash && return
1413
1414         local cur="${COMP_WORDS[COMP_CWORD]}"
1415         case "$cur" in
1416         --pretty=*)
1417                 __gitcomp "
1418                         oneline short medium full fuller email raw
1419                         " "" "${cur##--pretty=}"
1420                 return
1421                 ;;
1422         --*)
1423                 __gitcomp "--pretty="
1424                 return
1425                 ;;
1426         esac
1427         __git_complete_file
1428 }
1429
1430 _git_show_branch ()
1431 {
1432         local cur="${COMP_WORDS[COMP_CWORD]}"
1433         case "$cur" in
1434         --*)
1435                 __gitcomp "
1436                         --all --remotes --topo-order --current --more=
1437                         --list --independent --merge-base --no-name
1438                         --sha1-name --topics --reflog
1439                         "
1440                 return
1441                 ;;
1442         esac
1443         __git_complete_revlist
1444 }
1445
1446 _git_stash ()
1447 {
1448         local subcommands='save list show apply clear drop pop create branch'
1449         local subcommand="$(__git_find_subcommand "$subcommands")"
1450         if [ -z "$subcommand" ]; then
1451                 __gitcomp "$subcommands"
1452         else
1453                 local cur="${COMP_WORDS[COMP_CWORD]}"
1454                 case "$subcommand,$cur" in
1455                 save,--*)
1456                         __gitcomp "--keep-index"
1457                         ;;
1458                 apply,--*)
1459                         __gitcomp "--index"
1460                         ;;
1461                 show,--*|drop,--*|pop,--*|branch,--*)
1462                         COMPREPLY=()
1463                         ;;
1464                 show,*|apply,*|drop,*|pop,*|branch,*)
1465                         __gitcomp "$(git --git-dir="$(__gitdir)" stash list \
1466                                         | sed -n -e 's/:.*//p')"
1467                         ;;
1468                 *)
1469                         COMPREPLY=()
1470                         ;;
1471                 esac
1472         fi
1473 }
1474
1475 _git_submodule ()
1476 {
1477         __git_has_doubledash && return
1478
1479         local subcommands="add status init update summary foreach sync"
1480         if [ -z "$(__git_find_subcommand "$subcommands")" ]; then
1481                 local cur="${COMP_WORDS[COMP_CWORD]}"
1482                 case "$cur" in
1483                 --*)
1484                         __gitcomp "--quiet --cached"
1485                         ;;
1486                 *)
1487                         __gitcomp "$subcommands"
1488                         ;;
1489                 esac
1490                 return
1491         fi
1492 }
1493
1494 _git_svn ()
1495 {
1496         local subcommands="
1497                 init fetch clone rebase dcommit log find-rev
1498                 set-tree commit-diff info create-ignore propget
1499                 proplist show-ignore show-externals
1500                 "
1501         local subcommand="$(__git_find_subcommand "$subcommands")"
1502         if [ -z "$subcommand" ]; then
1503                 __gitcomp "$subcommands"
1504         else
1505                 local remote_opts="--username= --config-dir= --no-auth-cache"
1506                 local fc_opts="
1507                         --follow-parent --authors-file= --repack=
1508                         --no-metadata --use-svm-props --use-svnsync-props
1509                         --log-window-size= --no-checkout --quiet
1510                         --repack-flags --user-log-author $remote_opts
1511                         "
1512                 local init_opts="
1513                         --template= --shared= --trunk= --tags=
1514                         --branches= --stdlayout --minimize-url
1515                         --no-metadata --use-svm-props --use-svnsync-props
1516                         --rewrite-root= $remote_opts
1517                         "
1518                 local cmt_opts="
1519                         --edit --rmdir --find-copies-harder --copy-similarity=
1520                         "
1521
1522                 local cur="${COMP_WORDS[COMP_CWORD]}"
1523                 case "$subcommand,$cur" in
1524                 fetch,--*)
1525                         __gitcomp "--revision= --fetch-all $fc_opts"
1526                         ;;
1527                 clone,--*)
1528                         __gitcomp "--revision= $fc_opts $init_opts"
1529                         ;;
1530                 init,--*)
1531                         __gitcomp "$init_opts"
1532                         ;;
1533                 dcommit,--*)
1534                         __gitcomp "
1535                                 --merge --strategy= --verbose --dry-run
1536                                 --fetch-all --no-rebase $cmt_opts $fc_opts
1537                                 "
1538                         ;;
1539                 set-tree,--*)
1540                         __gitcomp "--stdin $cmt_opts $fc_opts"
1541                         ;;
1542                 create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
1543                 show-externals,--*)
1544                         __gitcomp "--revision="
1545                         ;;
1546                 log,--*)
1547                         __gitcomp "
1548                                 --limit= --revision= --verbose --incremental
1549                                 --oneline --show-commit --non-recursive
1550                                 --authors-file=
1551                                 "
1552                         ;;
1553                 rebase,--*)
1554                         __gitcomp "
1555                                 --merge --verbose --strategy= --local
1556                                 --fetch-all $fc_opts
1557                                 "
1558                         ;;
1559                 commit-diff,--*)
1560                         __gitcomp "--message= --file= --revision= $cmt_opts"
1561                         ;;
1562                 info,--*)
1563                         __gitcomp "--url"
1564                         ;;
1565                 *)
1566                         COMPREPLY=()
1567                         ;;
1568                 esac
1569         fi
1570 }
1571
1572 _git_tag ()
1573 {
1574         local i c=1 f=0
1575         while [ $c -lt $COMP_CWORD ]; do
1576                 i="${COMP_WORDS[c]}"
1577                 case "$i" in
1578                 -d|-v)
1579                         __gitcomp "$(__git_tags)"
1580                         return
1581                         ;;
1582                 -f)
1583                         f=1
1584                         ;;
1585                 esac
1586                 c=$((++c))
1587         done
1588
1589         case "${COMP_WORDS[COMP_CWORD-1]}" in
1590         -m|-F)
1591                 COMPREPLY=()
1592                 ;;
1593         -*|tag|git-tag)
1594                 if [ $f = 1 ]; then
1595                         __gitcomp "$(__git_tags)"
1596                 else
1597                         COMPREPLY=()
1598                 fi
1599                 ;;
1600         *)
1601                 __gitcomp "$(__git_refs)"
1602                 ;;
1603         esac
1604 }
1605
1606 _git ()
1607 {
1608         local i c=1 command __git_dir
1609
1610         while [ $c -lt $COMP_CWORD ]; do
1611                 i="${COMP_WORDS[c]}"
1612                 case "$i" in
1613                 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
1614                 --bare)      __git_dir="." ;;
1615                 --version|-p|--paginate) ;;
1616                 --help) command="help"; break ;;
1617                 *) command="$i"; break ;;
1618                 esac
1619                 c=$((++c))
1620         done
1621
1622         if [ -z "$command" ]; then
1623                 case "${COMP_WORDS[COMP_CWORD]}" in
1624                 --*=*) COMPREPLY=() ;;
1625                 --*)   __gitcomp "
1626                         --paginate
1627                         --no-pager
1628                         --git-dir=
1629                         --bare
1630                         --version
1631                         --exec-path
1632                         --work-tree=
1633                         --help
1634                         "
1635                         ;;
1636                 *)     __gitcomp "$(__git_porcelain_commands) $(__git_aliases)" ;;
1637                 esac
1638                 return
1639         fi
1640
1641         local expansion=$(__git_aliased_command "$command")
1642         [ "$expansion" ] && command="$expansion"
1643
1644         case "$command" in
1645         am)          _git_am ;;
1646         add)         _git_add ;;
1647         apply)       _git_apply ;;
1648         archive)     _git_archive ;;
1649         bisect)      _git_bisect ;;
1650         bundle)      _git_bundle ;;
1651         branch)      _git_branch ;;
1652         checkout)    _git_checkout ;;
1653         cherry)      _git_cherry ;;
1654         cherry-pick) _git_cherry_pick ;;
1655         clean)       _git_clean ;;
1656         clone)       _git_clone ;;
1657         commit)      _git_commit ;;
1658         config)      _git_config ;;
1659         describe)    _git_describe ;;
1660         diff)        _git_diff ;;
1661         fetch)       _git_fetch ;;
1662         format-patch) _git_format_patch ;;
1663         gc)          _git_gc ;;
1664         grep)        _git_grep ;;
1665         help)        _git_help ;;
1666         init)        _git_init ;;
1667         log)         _git_log ;;
1668         ls-files)    _git_ls_files ;;
1669         ls-remote)   _git_ls_remote ;;
1670         ls-tree)     _git_ls_tree ;;
1671         merge)       _git_merge;;
1672         mergetool)   _git_mergetool;;
1673         merge-base)  _git_merge_base ;;
1674         mv)          _git_mv ;;
1675         name-rev)    _git_name_rev ;;
1676         pull)        _git_pull ;;
1677         push)        _git_push ;;
1678         rebase)      _git_rebase ;;
1679         remote)      _git_remote ;;
1680         reset)       _git_reset ;;
1681         revert)      _git_revert ;;
1682         rm)          _git_rm ;;
1683         send-email)  _git_send_email ;;
1684         shortlog)    _git_shortlog ;;
1685         show)        _git_show ;;
1686         show-branch) _git_show_branch ;;
1687         stash)       _git_stash ;;
1688         submodule)   _git_submodule ;;
1689         svn)         _git_svn ;;
1690         tag)         _git_tag ;;
1691         whatchanged) _git_log ;;
1692         *)           COMPREPLY=() ;;
1693         esac
1694 }
1695
1696 _gitk ()
1697 {
1698         __git_has_doubledash && return
1699
1700         local cur="${COMP_WORDS[COMP_CWORD]}"
1701         local g="$(git rev-parse --git-dir 2>/dev/null)"
1702         local merge=""
1703         if [ -f $g/MERGE_HEAD ]; then
1704                 merge="--merge"
1705         fi
1706         case "$cur" in
1707         --*)
1708                 __gitcomp "--not --all $merge"
1709                 return
1710                 ;;
1711         esac
1712         __git_complete_revlist
1713 }
1714
1715 complete -o default -o nospace -F _git git
1716 complete -o default -o nospace -F _gitk gitk
1717
1718 # The following are necessary only for Cygwin, and only are needed
1719 # when the user has tab-completed the executable name and consequently
1720 # included the '.exe' suffix.
1721 #
1722 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
1723 complete -o default -o nospace -F _git git.exe
1724 fi