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