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