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