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