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