bash completion: Don't offer "a.." as a completion for "a."
[git] / contrib / completion / git-completion.bash
1 #
2 # bash completion support for core Git.
3 #
4 # Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
5 # Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
6 # Distributed under the GNU General Public License, version 2.0.
7 #
8 # The contained completion routines provide support for completing:
9 #
10 #    *) local and remote branch names
11 #    *) local and remote tag names
12 #    *) .git/remotes file names
13 #    *) git 'subcommands'
14 #    *) tree paths within 'ref:path/to/file' expressions
15 #    *) common --long-options
16 #
17 # To use these routines:
18 #
19 #    1) Copy this file to somewhere (e.g. ~/.git-completion.sh).
20 #    2) Added the following line to your .bashrc:
21 #        source ~/.git-completion.sh
22 #
23 #    3) You may want to make sure the git executable is available
24 #       in your PATH before this script is sourced, as some caching
25 #       is performed while the script loads.  If git isn't found
26 #       at source time then all lookups will be done on demand,
27 #       which may be slightly slower.
28 #
29 #    4) Consider changing your PS1 to also show the current branch:
30 #        PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
31 #
32 #       The argument to __git_ps1 will be displayed only if you
33 #       are currently in a git repository.  The %s token will be
34 #       the name of the current branch.
35 #
36 # To submit patches:
37 #
38 #    *) Read Documentation/SubmittingPatches
39 #    *) Send all patches to the current maintainer:
40 #
41 #       "Shawn O. Pearce" <spearce@spearce.org>
42 #
43 #    *) Always CC the Git mailing list:
44 #
45 #       git@vger.kernel.org
46 #
47
48 __gitdir ()
49 {
50         if [ -z "$1" ]; then
51                 if [ -n "$__git_dir" ]; then
52                         echo "$__git_dir"
53                 elif [ -d .git ]; then
54                         echo .git
55                 else
56                         git rev-parse --git-dir 2>/dev/null
57                 fi
58         elif [ -d "$1/.git" ]; then
59                 echo "$1/.git"
60         else
61                 echo "$1"
62         fi
63 }
64
65 __git_ps1 ()
66 {
67         local g="$(git rev-parse --git-dir 2>/dev/null)"
68         if [ -n "$g" ]; then
69                 local r
70                 local b
71                 if [ -d "$g/../.dotest" ]
72                 then
73                         if test -f "$g/../.dotest/rebasing"
74                         then
75                                 r="|REBASE"
76                         elif test -f "$g/../.dotest/applying"
77                         then
78                                 r="|AM"
79                         else
80                                 r="|AM/REBASE"
81                         fi
82                         b="$(git symbolic-ref HEAD 2>/dev/null)"
83                 elif [ -f "$g/.dotest-merge/interactive" ]
84                 then
85                         r="|REBASE-i"
86                         b="$(cat "$g/.dotest-merge/head-name")"
87                 elif [ -d "$g/.dotest-merge" ]
88                 then
89                         r="|REBASE-m"
90                         b="$(cat "$g/.dotest-merge/head-name")"
91                 elif [ -f "$g/MERGE_HEAD" ]
92                 then
93                         r="|MERGING"
94                         b="$(git symbolic-ref HEAD 2>/dev/null)"
95                 else
96                         if [ -f "$g/BISECT_LOG" ]
97                         then
98                                 r="|BISECTING"
99                         fi
100                         if ! b="$(git symbolic-ref HEAD 2>/dev/null)"
101                         then
102                                 if ! b="$(git describe --exact-match HEAD 2>/dev/null)"
103                                 then
104                                         b="$(cut -c1-7 "$g/HEAD")..."
105                                 fi
106                         fi
107                 fi
108
109                 if [ -n "$1" ]; then
110                         printf "$1" "${b##refs/heads/}$r"
111                 else
112                         printf " (%s)" "${b##refs/heads/}$r"
113                 fi
114         fi
115 }
116
117 __gitcomp_1 ()
118 {
119         local c IFS=' '$'\t'$'\n'
120         for c in $1; do
121                 case "$c$2" in
122                 --*=*) printf %s$'\n' "$c$2" ;;
123                 *.)    printf %s$'\n' "$c$2" ;;
124                 *)     printf %s$'\n' "$c$2 " ;;
125                 esac
126         done
127 }
128
129 __gitcomp ()
130 {
131         local cur="${COMP_WORDS[COMP_CWORD]}"
132         if [ $# -gt 2 ]; then
133                 cur="$3"
134         fi
135         case "$cur" in
136         --*=)
137                 COMPREPLY=()
138                 ;;
139         *)
140                 local IFS=$'\n'
141                 COMPREPLY=($(compgen -P "$2" \
142                         -W "$(__gitcomp_1 "$1" "$4")" \
143                         -- "$cur"))
144                 ;;
145         esac
146 }
147
148 __git_heads ()
149 {
150         local cmd i is_hash=y dir="$(__gitdir "$1")"
151         if [ -d "$dir" ]; then
152                 for i in $(git --git-dir="$dir" \
153                         for-each-ref --format='%(refname)' \
154                         refs/heads ); do
155                         echo "${i#refs/heads/}"
156                 done
157                 return
158         fi
159         for i in $(git ls-remote "$1" 2>/dev/null); do
160                 case "$is_hash,$i" in
161                 y,*) is_hash=n ;;
162                 n,*^{}) is_hash=y ;;
163                 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
164                 n,*) is_hash=y; echo "$i" ;;
165                 esac
166         done
167 }
168
169 __git_tags ()
170 {
171         local cmd i is_hash=y dir="$(__gitdir "$1")"
172         if [ -d "$dir" ]; then
173                 for i in $(git --git-dir="$dir" \
174                         for-each-ref --format='%(refname)' \
175                         refs/tags ); do
176                         echo "${i#refs/tags/}"
177                 done
178                 return
179         fi
180         for i in $(git ls-remote "$1" 2>/dev/null); do
181                 case "$is_hash,$i" in
182                 y,*) is_hash=n ;;
183                 n,*^{}) is_hash=y ;;
184                 n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
185                 n,*) is_hash=y; echo "$i" ;;
186                 esac
187         done
188 }
189
190 __git_refs ()
191 {
192         local cmd i is_hash=y dir="$(__gitdir "$1")"
193         if [ -d "$dir" ]; then
194                 if [ -e "$dir/HEAD" ]; then echo HEAD; fi
195                 for i in $(git --git-dir="$dir" \
196                         for-each-ref --format='%(refname)' \
197                         refs/tags refs/heads refs/remotes); do
198                         case "$i" in
199                                 refs/tags/*)    echo "${i#refs/tags/}" ;;
200                                 refs/heads/*)   echo "${i#refs/heads/}" ;;
201                                 refs/remotes/*) echo "${i#refs/remotes/}" ;;
202                                 *)              echo "$i" ;;
203                         esac
204                 done
205                 return
206         fi
207         for i in $(git ls-remote "$dir" 2>/dev/null); do
208                 case "$is_hash,$i" in
209                 y,*) is_hash=n ;;
210                 n,*^{}) is_hash=y ;;
211                 n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
212                 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
213                 n,refs/remotes/*) is_hash=y; echo "${i#refs/remotes/}" ;;
214                 n,*) is_hash=y; echo "$i" ;;
215                 esac
216         done
217 }
218
219 __git_refs2 ()
220 {
221         local i
222         for i in $(__git_refs "$1"); do
223                 echo "$i:$i"
224         done
225 }
226
227 __git_refs_remotes ()
228 {
229         local cmd i is_hash=y
230         for i in $(git ls-remote "$1" 2>/dev/null); do
231                 case "$is_hash,$i" in
232                 n,refs/heads/*)
233                         is_hash=y
234                         echo "$i:refs/remotes/$1/${i#refs/heads/}"
235                         ;;
236                 y,*) is_hash=n ;;
237                 n,*^{}) is_hash=y ;;
238                 n,refs/tags/*) is_hash=y;;
239                 n,*) is_hash=y; ;;
240                 esac
241         done
242 }
243
244 __git_remotes ()
245 {
246         local i ngoff IFS=$'\n' d="$(__gitdir)"
247         shopt -q nullglob || ngoff=1
248         shopt -s nullglob
249         for i in "$d/remotes"/*; do
250                 echo ${i#$d/remotes/}
251         done
252         [ "$ngoff" ] && shopt -u nullglob
253         for i in $(git --git-dir="$d" config --list); do
254                 case "$i" in
255                 remote.*.url=*)
256                         i="${i#remote.}"
257                         echo "${i/.url=*/}"
258                         ;;
259                 esac
260         done
261 }
262
263 __git_merge_strategies ()
264 {
265         if [ -n "$__git_merge_strategylist" ]; then
266                 echo "$__git_merge_strategylist"
267                 return
268         fi
269         sed -n "/^all_strategies='/{
270                 s/^all_strategies='//
271                 s/'//
272                 p
273                 q
274                 }" "$(git --exec-path)/git-merge"
275 }
276 __git_merge_strategylist=
277 __git_merge_strategylist="$(__git_merge_strategies 2>/dev/null)"
278
279 __git_complete_file ()
280 {
281         local pfx ls ref cur="${COMP_WORDS[COMP_CWORD]}"
282         case "$cur" in
283         ?*:*)
284                 ref="${cur%%:*}"
285                 cur="${cur#*:}"
286                 case "$cur" in
287                 ?*/*)
288                         pfx="${cur%/*}"
289                         cur="${cur##*/}"
290                         ls="$ref:$pfx"
291                         pfx="$pfx/"
292                         ;;
293                 *)
294                         ls="$ref"
295                         ;;
296             esac
297                 COMPREPLY=($(compgen -P "$pfx" \
298                         -W "$(git --git-dir="$(__gitdir)" ls-tree "$ls" \
299                                 | sed '/^100... blob /s,^.*     ,,
300                                        /^040000 tree /{
301                                            s,^.*        ,,
302                                            s,$,/,
303                                        }
304                                        s/^.*    //')" \
305                         -- "$cur"))
306                 ;;
307         *)
308                 __gitcomp "$(__git_refs)"
309                 ;;
310         esac
311 }
312
313 __git_complete_revlist ()
314 {
315         local pfx cur="${COMP_WORDS[COMP_CWORD]}"
316         case "$cur" in
317         *...*)
318                 pfx="${cur%...*}..."
319                 cur="${cur#*...}"
320                 __gitcomp "$(__git_refs)" "$pfx" "$cur"
321                 ;;
322         *..*)
323                 pfx="${cur%..*}.."
324                 cur="${cur#*..}"
325                 __gitcomp "$(__git_refs)" "$pfx" "$cur"
326                 ;;
327         *)
328                 __gitcomp "$(__git_refs)"
329                 ;;
330         esac
331 }
332
333 __git_commands ()
334 {
335         if [ -n "$__git_commandlist" ]; then
336                 echo "$__git_commandlist"
337                 return
338         fi
339         local i IFS=" "$'\n'
340         for i in $(git help -a|egrep '^ ')
341         do
342                 case $i in
343                 *--*)             : helper pattern;;
344                 applymbox)        : ask gittus;;
345                 applypatch)       : ask gittus;;
346                 archimport)       : import;;
347                 cat-file)         : plumbing;;
348                 check-attr)       : plumbing;;
349                 check-ref-format) : plumbing;;
350                 commit-tree)      : plumbing;;
351                 cvsexportcommit)  : export;;
352                 cvsimport)        : import;;
353                 cvsserver)        : daemon;;
354                 daemon)           : daemon;;
355                 diff-files)       : plumbing;;
356                 diff-index)       : plumbing;;
357                 diff-tree)        : plumbing;;
358                 fast-import)      : import;;
359                 fsck-objects)     : plumbing;;
360                 fetch-pack)       : plumbing;;
361                 fmt-merge-msg)    : plumbing;;
362                 for-each-ref)     : plumbing;;
363                 hash-object)      : plumbing;;
364                 http-*)           : transport;;
365                 index-pack)       : plumbing;;
366                 init-db)          : deprecated;;
367                 local-fetch)      : plumbing;;
368                 mailinfo)         : plumbing;;
369                 mailsplit)        : plumbing;;
370                 merge-*)          : plumbing;;
371                 mktree)           : plumbing;;
372                 mktag)            : plumbing;;
373                 pack-objects)     : plumbing;;
374                 pack-redundant)   : plumbing;;
375                 pack-refs)        : plumbing;;
376                 parse-remote)     : plumbing;;
377                 patch-id)         : plumbing;;
378                 peek-remote)      : plumbing;;
379                 prune)            : plumbing;;
380                 prune-packed)     : plumbing;;
381                 quiltimport)      : import;;
382                 read-tree)        : plumbing;;
383                 receive-pack)     : plumbing;;
384                 reflog)           : plumbing;;
385                 repo-config)      : deprecated;;
386                 rerere)           : plumbing;;
387                 rev-list)         : plumbing;;
388                 rev-parse)        : plumbing;;
389                 runstatus)        : plumbing;;
390                 sh-setup)         : internal;;
391                 shell)            : daemon;;
392                 send-pack)        : plumbing;;
393                 show-index)       : plumbing;;
394                 ssh-*)            : transport;;
395                 stripspace)       : plumbing;;
396                 symbolic-ref)     : plumbing;;
397                 tar-tree)         : deprecated;;
398                 unpack-file)      : plumbing;;
399                 unpack-objects)   : plumbing;;
400                 update-index)     : plumbing;;
401                 update-ref)       : plumbing;;
402                 update-server-info) : daemon;;
403                 upload-archive)   : plumbing;;
404                 upload-pack)      : plumbing;;
405                 write-tree)       : plumbing;;
406                 verify-tag)       : plumbing;;
407                 *) echo $i;;
408                 esac
409         done
410 }
411 __git_commandlist=
412 __git_commandlist="$(__git_commands 2>/dev/null)"
413
414 __git_aliases ()
415 {
416         local i IFS=$'\n'
417         for i in $(git --git-dir="$(__gitdir)" config --list); do
418                 case "$i" in
419                 alias.*)
420                         i="${i#alias.}"
421                         echo "${i/=*/}"
422                         ;;
423                 esac
424         done
425 }
426
427 __git_aliased_command ()
428 {
429         local word cmdline=$(git --git-dir="$(__gitdir)" \
430                 config --get "alias.$1")
431         for word in $cmdline; do
432                 if [ "${word##-*}" ]; then
433                         echo $word
434                         return
435                 fi
436         done
437 }
438
439 __git_find_subcommand ()
440 {
441         local word subcommand c=1
442
443         while [ $c -lt $COMP_CWORD ]; do
444                 word="${COMP_WORDS[c]}"
445                 for subcommand in $1; do
446                         if [ "$subcommand" = "$word" ]; then
447                                 echo "$subcommand"
448                                 return
449                         fi
450                 done
451                 c=$((++c))
452         done
453 }
454
455 __git_has_doubledash ()
456 {
457         local c=1
458         while [ $c -lt $COMP_CWORD ]; do
459                 if [ "--" = "${COMP_WORDS[c]}" ]; then
460                         return 0
461                 fi
462                 c=$((++c))
463         done
464         return 1
465 }
466
467 __git_whitespacelist="nowarn warn error error-all strip"
468
469 _git_am ()
470 {
471         local cur="${COMP_WORDS[COMP_CWORD]}"
472         if [ -d .dotest ]; then
473                 __gitcomp "--skip --resolved"
474                 return
475         fi
476         case "$cur" in
477         --whitespace=*)
478                 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
479                 return
480                 ;;
481         --*)
482                 __gitcomp "
483                         --signoff --utf8 --binary --3way --interactive
484                         --whitespace=
485                         "
486                 return
487         esac
488         COMPREPLY=()
489 }
490
491 _git_apply ()
492 {
493         local cur="${COMP_WORDS[COMP_CWORD]}"
494         case "$cur" in
495         --whitespace=*)
496                 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
497                 return
498                 ;;
499         --*)
500                 __gitcomp "
501                         --stat --numstat --summary --check --index
502                         --cached --index-info --reverse --reject --unidiff-zero
503                         --apply --no-add --exclude=
504                         --whitespace= --inaccurate-eof --verbose
505                         "
506                 return
507         esac
508         COMPREPLY=()
509 }
510
511 _git_add ()
512 {
513         __git_has_doubledash && return
514
515         local cur="${COMP_WORDS[COMP_CWORD]}"
516         case "$cur" in
517         --*)
518                 __gitcomp "
519                         --interactive --refresh --patch --update --dry-run
520                         --ignore-errors
521                         "
522                 return
523         esac
524         COMPREPLY=()
525 }
526
527 _git_bisect ()
528 {
529         __git_has_doubledash && return
530
531         local subcommands="start bad good reset visualize replay log"
532         local subcommand="$(__git_find_subcommand "$subcommands")"
533         if [ -z "$subcommand" ]; then
534                 __gitcomp "$subcommands"
535                 return
536         fi
537
538         case "$subcommand" in
539         bad|good|reset)
540                 __gitcomp "$(__git_refs)"
541                 ;;
542         *)
543                 COMPREPLY=()
544                 ;;
545         esac
546 }
547
548 _git_branch ()
549 {
550         local i c=1 only_local_ref="n" has_r="n"
551
552         while [ $c -lt $COMP_CWORD ]; do
553                 i="${COMP_WORDS[c]}"
554                 case "$i" in
555                 -d|-m)  only_local_ref="y" ;;
556                 -r)     has_r="y" ;;
557                 esac
558                 c=$((++c))
559         done
560
561         case "${COMP_WORDS[COMP_CWORD]}" in
562         --*=*)  COMPREPLY=() ;;
563         --*)
564                 __gitcomp "
565                         --color --no-color --verbose --abbrev= --no-abbrev
566                         --track --no-track
567                         "
568                 ;;
569         *)
570                 if [ $only_local_ref = "y" -a $has_r = "n" ]; then
571                         __gitcomp "$(__git_heads)"
572                 else
573                         __gitcomp "$(__git_refs)"
574                 fi
575                 ;;
576         esac
577 }
578
579 _git_bundle ()
580 {
581         local mycword="$COMP_CWORD"
582         case "${COMP_WORDS[0]}" in
583         git)
584                 local cmd="${COMP_WORDS[2]}"
585                 mycword="$((mycword-1))"
586                 ;;
587         git-bundle*)
588                 local cmd="${COMP_WORDS[1]}"
589                 ;;
590         esac
591         case "$mycword" in
592         1)
593                 __gitcomp "create list-heads verify unbundle"
594                 ;;
595         2)
596                 # looking for a file
597                 ;;
598         *)
599                 case "$cmd" in
600                         create)
601                                 __git_complete_revlist
602                         ;;
603                 esac
604                 ;;
605         esac
606 }
607
608 _git_checkout ()
609 {
610         __gitcomp "$(__git_refs)"
611 }
612
613 _git_cherry ()
614 {
615         __gitcomp "$(__git_refs)"
616 }
617
618 _git_cherry_pick ()
619 {
620         local cur="${COMP_WORDS[COMP_CWORD]}"
621         case "$cur" in
622         --*)
623                 __gitcomp "--edit --no-commit"
624                 ;;
625         *)
626                 __gitcomp "$(__git_refs)"
627                 ;;
628         esac
629 }
630
631 _git_commit ()
632 {
633         __git_has_doubledash && return
634
635         local cur="${COMP_WORDS[COMP_CWORD]}"
636         case "$cur" in
637         --*)
638                 __gitcomp "
639                         --all --author= --signoff --verify --no-verify
640                         --edit --amend --include --only
641                         "
642                 return
643         esac
644         COMPREPLY=()
645 }
646
647 _git_describe ()
648 {
649         __gitcomp "$(__git_refs)"
650 }
651
652 _git_diff ()
653 {
654         __git_has_doubledash && return
655
656         local cur="${COMP_WORDS[COMP_CWORD]}"
657         case "$cur" in
658         --*)
659                 __gitcomp "--cached --stat --numstat --shortstat --summary
660                         --patch-with-stat --name-only --name-status --color
661                         --no-color --color-words --no-renames --check
662                         --full-index --binary --abbrev --diff-filter
663                         --find-copies-harder --pickaxe-all --pickaxe-regex
664                         --text --ignore-space-at-eol --ignore-space-change
665                         --ignore-all-space --exit-code --quiet --ext-diff
666                         --no-ext-diff
667                         --no-prefix --src-prefix= --dst-prefix=
668                         --base --ours --theirs
669                         "
670                 return
671                 ;;
672         esac
673         __git_complete_file
674 }
675
676 _git_diff_tree ()
677 {
678         __gitcomp "$(__git_refs)"
679 }
680
681 _git_fetch ()
682 {
683         local cur="${COMP_WORDS[COMP_CWORD]}"
684
685         case "${COMP_WORDS[0]},$COMP_CWORD" in
686         git-fetch*,1)
687                 __gitcomp "$(__git_remotes)"
688                 ;;
689         git,2)
690                 __gitcomp "$(__git_remotes)"
691                 ;;
692         *)
693                 case "$cur" in
694                 *:*)
695                         __gitcomp "$(__git_refs)" "" "${cur#*:}"
696                         ;;
697                 *)
698                         local remote
699                         case "${COMP_WORDS[0]}" in
700                         git-fetch) remote="${COMP_WORDS[1]}" ;;
701                         git)       remote="${COMP_WORDS[2]}" ;;
702                         esac
703                         __gitcomp "$(__git_refs2 "$remote")"
704                         ;;
705                 esac
706                 ;;
707         esac
708 }
709
710 _git_format_patch ()
711 {
712         local cur="${COMP_WORDS[COMP_CWORD]}"
713         case "$cur" in
714         --*)
715                 __gitcomp "
716                         --stdout --attach --thread
717                         --output-directory
718                         --numbered --start-number
719                         --numbered-files
720                         --keep-subject
721                         --signoff
722                         --in-reply-to=
723                         --full-index --binary
724                         --not --all
725                         --cover-letter
726                         --no-prefix --src-prefix= --dst-prefix=
727                         "
728                 return
729                 ;;
730         esac
731         __git_complete_revlist
732 }
733
734 _git_gc ()
735 {
736         local cur="${COMP_WORDS[COMP_CWORD]}"
737         case "$cur" in
738         --*)
739                 __gitcomp "--prune --aggressive"
740                 return
741                 ;;
742         esac
743         COMPREPLY=()
744 }
745
746 _git_ls_remote ()
747 {
748         __gitcomp "$(__git_remotes)"
749 }
750
751 _git_ls_tree ()
752 {
753         __git_complete_file
754 }
755
756 _git_log ()
757 {
758         __git_has_doubledash && return
759
760         local cur="${COMP_WORDS[COMP_CWORD]}"
761         case "$cur" in
762         --pretty=*)
763                 __gitcomp "
764                         oneline short medium full fuller email raw
765                         " "" "${cur##--pretty=}"
766                 return
767                 ;;
768         --date=*)
769                 __gitcomp "
770                         relative iso8601 rfc2822 short local default
771                 " "" "${cur##--date=}"
772                 return
773                 ;;
774         --*)
775                 __gitcomp "
776                         --max-count= --max-age= --since= --after=
777                         --min-age= --before= --until=
778                         --root --topo-order --date-order --reverse
779                         --no-merges --follow
780                         --abbrev-commit --abbrev=
781                         --relative-date --date=
782                         --author= --committer= --grep=
783                         --all-match
784                         --pretty= --name-status --name-only --raw
785                         --not --all
786                         --left-right --cherry-pick
787                         --graph
788                         "
789                 return
790                 ;;
791         esac
792         __git_complete_revlist
793 }
794
795 _git_merge ()
796 {
797         local cur="${COMP_WORDS[COMP_CWORD]}"
798         case "${COMP_WORDS[COMP_CWORD-1]}" in
799         -s|--strategy)
800                 __gitcomp "$(__git_merge_strategies)"
801                 return
802         esac
803         case "$cur" in
804         --strategy=*)
805                 __gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
806                 return
807                 ;;
808         --*)
809                 __gitcomp "
810                         --no-commit --no-stat --log --no-log --squash --strategy
811                         "
812                 return
813         esac
814         __gitcomp "$(__git_refs)"
815 }
816
817 _git_merge_base ()
818 {
819         __gitcomp "$(__git_refs)"
820 }
821
822 _git_name_rev ()
823 {
824         __gitcomp "--tags --all --stdin"
825 }
826
827 _git_pull ()
828 {
829         local cur="${COMP_WORDS[COMP_CWORD]}"
830
831         case "${COMP_WORDS[0]},$COMP_CWORD" in
832         git-pull*,1)
833                 __gitcomp "$(__git_remotes)"
834                 ;;
835         git,2)
836                 __gitcomp "$(__git_remotes)"
837                 ;;
838         *)
839                 local remote
840                 case "${COMP_WORDS[0]}" in
841                 git-pull)  remote="${COMP_WORDS[1]}" ;;
842                 git)       remote="${COMP_WORDS[2]}" ;;
843                 esac
844                 __gitcomp "$(__git_refs "$remote")"
845                 ;;
846         esac
847 }
848
849 _git_push ()
850 {
851         local cur="${COMP_WORDS[COMP_CWORD]}"
852
853         case "${COMP_WORDS[0]},$COMP_CWORD" in
854         git-push*,1)
855                 __gitcomp "$(__git_remotes)"
856                 ;;
857         git,2)
858                 __gitcomp "$(__git_remotes)"
859                 ;;
860         *)
861                 case "$cur" in
862                 *:*)
863                         local remote
864                         case "${COMP_WORDS[0]}" in
865                         git-push)  remote="${COMP_WORDS[1]}" ;;
866                         git)       remote="${COMP_WORDS[2]}" ;;
867                         esac
868                         __gitcomp "$(__git_refs "$remote")" "" "${cur#*:}"
869                         ;;
870                 +*)
871                         __gitcomp "$(__git_refs)" + "${cur#+}"
872                         ;;
873                 *)
874                         __gitcomp "$(__git_refs)"
875                         ;;
876                 esac
877                 ;;
878         esac
879 }
880
881 _git_rebase ()
882 {
883         local cur="${COMP_WORDS[COMP_CWORD]}" dir="$(__gitdir)"
884         if [ -d .dotest ] || [ -d "$dir"/.dotest-merge ]; then
885                 __gitcomp "--continue --skip --abort"
886                 return
887         fi
888         case "${COMP_WORDS[COMP_CWORD-1]}" in
889         -s|--strategy)
890                 __gitcomp "$(__git_merge_strategies)"
891                 return
892         esac
893         case "$cur" in
894         --strategy=*)
895                 __gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
896                 return
897                 ;;
898         --*)
899                 __gitcomp "--onto --merge --strategy --interactive"
900                 return
901         esac
902         __gitcomp "$(__git_refs)"
903 }
904
905 _git_config ()
906 {
907         local cur="${COMP_WORDS[COMP_CWORD]}"
908         local prv="${COMP_WORDS[COMP_CWORD-1]}"
909         case "$prv" in
910         branch.*.remote)
911                 __gitcomp "$(__git_remotes)"
912                 return
913                 ;;
914         branch.*.merge)
915                 __gitcomp "$(__git_refs)"
916                 return
917                 ;;
918         remote.*.fetch)
919                 local remote="${prv#remote.}"
920                 remote="${remote%.fetch}"
921                 __gitcomp "$(__git_refs_remotes "$remote")"
922                 return
923                 ;;
924         remote.*.push)
925                 local remote="${prv#remote.}"
926                 remote="${remote%.push}"
927                 __gitcomp "$(git --git-dir="$(__gitdir)" \
928                         for-each-ref --format='%(refname):%(refname)' \
929                         refs/heads)"
930                 return
931                 ;;
932         pull.twohead|pull.octopus)
933                 __gitcomp "$(__git_merge_strategies)"
934                 return
935                 ;;
936         color.branch|color.diff|color.status)
937                 __gitcomp "always never auto"
938                 return
939                 ;;
940         color.*.*)
941                 __gitcomp "
942                         black red green yellow blue magenta cyan white
943                         bold dim ul blink reverse
944                         "
945                 return
946                 ;;
947         *.*)
948                 COMPREPLY=()
949                 return
950                 ;;
951         esac
952         case "$cur" in
953         --*)
954                 __gitcomp "
955                         --global --system --file=
956                         --list --replace-all
957                         --get --get-all --get-regexp
958                         --add --unset --unset-all
959                         --remove-section --rename-section
960                         "
961                 return
962                 ;;
963         branch.*.*)
964                 local pfx="${cur%.*}."
965                 cur="${cur##*.}"
966                 __gitcomp "remote merge" "$pfx" "$cur"
967                 return
968                 ;;
969         branch.*)
970                 local pfx="${cur%.*}."
971                 cur="${cur#*.}"
972                 __gitcomp "$(__git_heads)" "$pfx" "$cur" "."
973                 return
974                 ;;
975         remote.*.*)
976                 local pfx="${cur%.*}."
977                 cur="${cur##*.}"
978                 __gitcomp "
979                         url fetch push skipDefaultUpdate
980                         receivepack uploadpack tagopt
981                         " "$pfx" "$cur"
982                 return
983                 ;;
984         remote.*)
985                 local pfx="${cur%.*}."
986                 cur="${cur#*.}"
987                 __gitcomp "$(__git_remotes)" "$pfx" "$cur" "."
988                 return
989                 ;;
990         esac
991         __gitcomp "
992                 apply.whitespace
993                 core.fileMode
994                 core.gitProxy
995                 core.ignoreStat
996                 core.preferSymlinkRefs
997                 core.logAllRefUpdates
998                 core.loosecompression
999                 core.repositoryFormatVersion
1000                 core.sharedRepository
1001                 core.warnAmbiguousRefs
1002                 core.compression
1003                 core.packedGitWindowSize
1004                 core.packedGitLimit
1005                 clean.requireForce
1006                 color.branch
1007                 color.branch.current
1008                 color.branch.local
1009                 color.branch.remote
1010                 color.branch.plain
1011                 color.diff
1012                 color.diff.plain
1013                 color.diff.meta
1014                 color.diff.frag
1015                 color.diff.old
1016                 color.diff.new
1017                 color.diff.commit
1018                 color.diff.whitespace
1019                 color.pager
1020                 color.status
1021                 color.status.header
1022                 color.status.added
1023                 color.status.changed
1024                 color.status.untracked
1025                 diff.renameLimit
1026                 diff.renames
1027                 fetch.unpackLimit
1028                 format.headers
1029                 format.subjectprefix
1030                 gitcvs.enabled
1031                 gitcvs.logfile
1032                 gitcvs.allbinary
1033                 gitcvs.dbname gitcvs.dbdriver gitcvs.dbuser gitcvs.dbpass
1034                 gitcvs.dbtablenameprefix
1035                 gc.packrefs
1036                 gc.reflogexpire
1037                 gc.reflogexpireunreachable
1038                 gc.rerereresolved
1039                 gc.rerereunresolved
1040                 http.sslVerify
1041                 http.sslCert
1042                 http.sslKey
1043                 http.sslCAInfo
1044                 http.sslCAPath
1045                 http.maxRequests
1046                 http.lowSpeedLimit
1047                 http.lowSpeedTime
1048                 http.noEPSV
1049                 i18n.commitEncoding
1050                 i18n.logOutputEncoding
1051                 log.showroot
1052                 merge.tool
1053                 merge.summary
1054                 merge.verbosity
1055                 pack.window
1056                 pack.depth
1057                 pack.windowMemory
1058                 pack.compression
1059                 pack.deltaCacheSize
1060                 pack.deltaCacheLimit
1061                 pull.octopus
1062                 pull.twohead
1063                 repack.useDeltaBaseOffset
1064                 show.difftree
1065                 showbranch.default
1066                 tar.umask
1067                 transfer.unpackLimit
1068                 receive.unpackLimit
1069                 receive.denyNonFastForwards
1070                 user.name
1071                 user.email
1072                 user.signingkey
1073                 whatchanged.difftree
1074                 branch. remote.
1075         "
1076 }
1077
1078 _git_remote ()
1079 {
1080         local subcommands="add rm show prune update"
1081         local subcommand="$(__git_find_subcommand "$subcommands")"
1082         if [ -z "$subcommand" ]; then
1083                 __gitcomp "$subcommands"
1084                 return
1085         fi
1086
1087         case "$subcommand" in
1088         rm|show|prune)
1089                 __gitcomp "$(__git_remotes)"
1090                 ;;
1091         update)
1092                 local i c='' IFS=$'\n'
1093                 for i in $(git --git-dir="$(__gitdir)" config --list); do
1094                         case "$i" in
1095                         remotes.*)
1096                                 i="${i#remotes.}"
1097                                 c="$c ${i/=*/}"
1098                                 ;;
1099                         esac
1100                 done
1101                 __gitcomp "$c"
1102                 ;;
1103         *)
1104                 COMPREPLY=()
1105                 ;;
1106         esac
1107 }
1108
1109 _git_reset ()
1110 {
1111         __git_has_doubledash && return
1112
1113         local cur="${COMP_WORDS[COMP_CWORD]}"
1114         case "$cur" in
1115         --*)
1116                 __gitcomp "--mixed --hard --soft"
1117                 return
1118                 ;;
1119         esac
1120         __gitcomp "$(__git_refs)"
1121 }
1122
1123 _git_shortlog ()
1124 {
1125         __git_has_doubledash && return
1126
1127         local cur="${COMP_WORDS[COMP_CWORD]}"
1128         case "$cur" in
1129         --*)
1130                 __gitcomp "
1131                         --max-count= --max-age= --since= --after=
1132                         --min-age= --before= --until=
1133                         --no-merges
1134                         --author= --committer= --grep=
1135                         --all-match
1136                         --not --all
1137                         --numbered --summary
1138                         "
1139                 return
1140                 ;;
1141         esac
1142         __git_complete_revlist
1143 }
1144
1145 _git_show ()
1146 {
1147         local cur="${COMP_WORDS[COMP_CWORD]}"
1148         case "$cur" in
1149         --pretty=*)
1150                 __gitcomp "
1151                         oneline short medium full fuller email raw
1152                         " "" "${cur##--pretty=}"
1153                 return
1154                 ;;
1155         --*)
1156                 __gitcomp "--pretty="
1157                 return
1158                 ;;
1159         esac
1160         __git_complete_file
1161 }
1162
1163 _git_stash ()
1164 {
1165         local subcommands='save list show apply clear drop pop create'
1166         if [ -z "$(__git_find_subcommand "$subcommands")" ]; then
1167                 __gitcomp "$subcommands"
1168         fi
1169 }
1170
1171 _git_submodule ()
1172 {
1173         __git_has_doubledash && return
1174
1175         local subcommands="add status init update"
1176         if [ -z "$(__git_find_subcommand "$subcommands")" ]; then
1177                 local cur="${COMP_WORDS[COMP_CWORD]}"
1178                 case "$cur" in
1179                 --*)
1180                         __gitcomp "--quiet --cached"
1181                         ;;
1182                 *)
1183                         __gitcomp "$subcommands"
1184                         ;;
1185                 esac
1186                 return
1187         fi
1188 }
1189
1190 _git_svn ()
1191 {
1192         local subcommands="
1193                 init fetch clone rebase dcommit log find-rev
1194                 set-tree commit-diff info create-ignore propget
1195                 proplist show-ignore show-externals
1196                 "
1197         local subcommand="$(__git_find_subcommand "$subcommands")"
1198         if [ -z "$subcommand" ]; then
1199                 __gitcomp "$subcommands"
1200         else
1201                 local remote_opts="--username= --config-dir= --no-auth-cache"
1202                 local fc_opts="
1203                         --follow-parent --authors-file= --repack=
1204                         --no-metadata --use-svm-props --use-svnsync-props
1205                         --log-window-size= --no-checkout --quiet
1206                         --repack-flags --user-log-author $remote_opts
1207                         "
1208                 local init_opts="
1209                         --template= --shared= --trunk= --tags=
1210                         --branches= --stdlayout --minimize-url
1211                         --no-metadata --use-svm-props --use-svnsync-props
1212                         --rewrite-root= $remote_opts
1213                         "
1214                 local cmt_opts="
1215                         --edit --rmdir --find-copies-harder --copy-similarity=
1216                         "
1217
1218                 local cur="${COMP_WORDS[COMP_CWORD]}"
1219                 case "$subcommand,$cur" in
1220                 fetch,--*)
1221                         __gitcomp "--revision= --fetch-all $fc_opts"
1222                         ;;
1223                 clone,--*)
1224                         __gitcomp "--revision= $fc_opts $init_opts"
1225                         ;;
1226                 init,--*)
1227                         __gitcomp "$init_opts"
1228                         ;;
1229                 dcommit,--*)
1230                         __gitcomp "
1231                                 --merge --strategy= --verbose --dry-run
1232                                 --fetch-all --no-rebase $cmt_opts $fc_opts
1233                                 "
1234                         ;;
1235                 set-tree,--*)
1236                         __gitcomp "--stdin $cmt_opts $fc_opts"
1237                         ;;
1238                 create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
1239                 show-externals,--*)
1240                         __gitcomp "--revision="
1241                         ;;
1242                 log,--*)
1243                         __gitcomp "
1244                                 --limit= --revision= --verbose --incremental
1245                                 --oneline --show-commit --non-recursive
1246                                 --authors-file=
1247                                 "
1248                         ;;
1249                 rebase,--*)
1250                         __gitcomp "
1251                                 --merge --verbose --strategy= --local
1252                                 --fetch-all $fc_opts
1253                                 "
1254                         ;;
1255                 commit-diff,--*)
1256                         __gitcomp "--message= --file= --revision= $cmt_opts"
1257                         ;;
1258                 info,--*)
1259                         __gitcomp "--url"
1260                         ;;
1261                 *)
1262                         COMPREPLY=()
1263                         ;;
1264                 esac
1265         fi
1266 }
1267
1268 _git_tag ()
1269 {
1270         local i c=1 f=0
1271         while [ $c -lt $COMP_CWORD ]; do
1272                 i="${COMP_WORDS[c]}"
1273                 case "$i" in
1274                 -d|-v)
1275                         __gitcomp "$(__git_tags)"
1276                         return
1277                         ;;
1278                 -f)
1279                         f=1
1280                         ;;
1281                 esac
1282                 c=$((++c))
1283         done
1284
1285         case "${COMP_WORDS[COMP_CWORD-1]}" in
1286         -m|-F)
1287                 COMPREPLY=()
1288                 ;;
1289         -*|tag|git-tag)
1290                 if [ $f = 1 ]; then
1291                         __gitcomp "$(__git_tags)"
1292                 else
1293                         COMPREPLY=()
1294                 fi
1295                 ;;
1296         *)
1297                 __gitcomp "$(__git_refs)"
1298                 ;;
1299         esac
1300 }
1301
1302 _git ()
1303 {
1304         local i c=1 command __git_dir
1305
1306         while [ $c -lt $COMP_CWORD ]; do
1307                 i="${COMP_WORDS[c]}"
1308                 case "$i" in
1309                 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
1310                 --bare)      __git_dir="." ;;
1311                 --version|--help|-p|--paginate) ;;
1312                 *) command="$i"; break ;;
1313                 esac
1314                 c=$((++c))
1315         done
1316
1317         if [ -z "$command" ]; then
1318                 case "${COMP_WORDS[COMP_CWORD]}" in
1319                 --*=*) COMPREPLY=() ;;
1320                 --*)   __gitcomp "
1321                         --paginate
1322                         --no-pager
1323                         --git-dir=
1324                         --bare
1325                         --version
1326                         --exec-path
1327                         --work-tree=
1328                         --help
1329                         "
1330                         ;;
1331                 *)     __gitcomp "$(__git_commands) $(__git_aliases)" ;;
1332                 esac
1333                 return
1334         fi
1335
1336         local expansion=$(__git_aliased_command "$command")
1337         [ "$expansion" ] && command="$expansion"
1338
1339         case "$command" in
1340         am)          _git_am ;;
1341         add)         _git_add ;;
1342         apply)       _git_apply ;;
1343         bisect)      _git_bisect ;;
1344         bundle)      _git_bundle ;;
1345         branch)      _git_branch ;;
1346         checkout)    _git_checkout ;;
1347         cherry)      _git_cherry ;;
1348         cherry-pick) _git_cherry_pick ;;
1349         commit)      _git_commit ;;
1350         config)      _git_config ;;
1351         describe)    _git_describe ;;
1352         diff)        _git_diff ;;
1353         fetch)       _git_fetch ;;
1354         format-patch) _git_format_patch ;;
1355         gc)          _git_gc ;;
1356         log)         _git_log ;;
1357         ls-remote)   _git_ls_remote ;;
1358         ls-tree)     _git_ls_tree ;;
1359         merge)       _git_merge;;
1360         merge-base)  _git_merge_base ;;
1361         name-rev)    _git_name_rev ;;
1362         pull)        _git_pull ;;
1363         push)        _git_push ;;
1364         rebase)      _git_rebase ;;
1365         remote)      _git_remote ;;
1366         reset)       _git_reset ;;
1367         shortlog)    _git_shortlog ;;
1368         show)        _git_show ;;
1369         show-branch) _git_log ;;
1370         stash)       _git_stash ;;
1371         submodule)   _git_submodule ;;
1372         svn)         _git_svn ;;
1373         tag)         _git_tag ;;
1374         whatchanged) _git_log ;;
1375         *)           COMPREPLY=() ;;
1376         esac
1377 }
1378
1379 _gitk ()
1380 {
1381         __git_has_doubledash && return
1382
1383         local cur="${COMP_WORDS[COMP_CWORD]}"
1384         local g="$(git rev-parse --git-dir 2>/dev/null)"
1385         local merge=""
1386         if [ -f $g/MERGE_HEAD ]; then
1387                 merge="--merge"
1388         fi
1389         case "$cur" in
1390         --*)
1391                 __gitcomp "--not --all $merge"
1392                 return
1393                 ;;
1394         esac
1395         __git_complete_revlist
1396 }
1397
1398 complete -o default -o nospace -F _git git
1399 complete -o default -o nospace -F _gitk gitk
1400 complete -o default -o nospace -F _git_am git-am
1401 complete -o default -o nospace -F _git_apply git-apply
1402 complete -o default -o nospace -F _git_bisect git-bisect
1403 complete -o default -o nospace -F _git_branch git-branch
1404 complete -o default -o nospace -F _git_bundle git-bundle
1405 complete -o default -o nospace -F _git_checkout git-checkout
1406 complete -o default -o nospace -F _git_cherry git-cherry
1407 complete -o default -o nospace -F _git_cherry_pick git-cherry-pick
1408 complete -o default -o nospace -F _git_commit git-commit
1409 complete -o default -o nospace -F _git_describe git-describe
1410 complete -o default -o nospace -F _git_diff git-diff
1411 complete -o default -o nospace -F _git_fetch git-fetch
1412 complete -o default -o nospace -F _git_format_patch git-format-patch
1413 complete -o default -o nospace -F _git_gc git-gc
1414 complete -o default -o nospace -F _git_log git-log
1415 complete -o default -o nospace -F _git_ls_remote git-ls-remote
1416 complete -o default -o nospace -F _git_ls_tree git-ls-tree
1417 complete -o default -o nospace -F _git_merge git-merge
1418 complete -o default -o nospace -F _git_merge_base git-merge-base
1419 complete -o default -o nospace -F _git_name_rev git-name-rev
1420 complete -o default -o nospace -F _git_pull git-pull
1421 complete -o default -o nospace -F _git_push git-push
1422 complete -o default -o nospace -F _git_rebase git-rebase
1423 complete -o default -o nospace -F _git_config git-config
1424 complete -o default -o nospace -F _git_remote git-remote
1425 complete -o default -o nospace -F _git_reset git-reset
1426 complete -o default -o nospace -F _git_shortlog git-shortlog
1427 complete -o default -o nospace -F _git_show git-show
1428 complete -o default -o nospace -F _git_stash git-stash
1429 complete -o default -o nospace -F _git_submodule git-submodule
1430 complete -o default -o nospace -F _git_svn git-svn
1431 complete -o default -o nospace -F _git_log git-show-branch
1432 complete -o default -o nospace -F _git_tag git-tag
1433 complete -o default -o nospace -F _git_log git-whatchanged
1434
1435 # The following are necessary only for Cygwin, and only are needed
1436 # when the user has tab-completed the executable name and consequently
1437 # included the '.exe' suffix.
1438 #
1439 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
1440 complete -o default -o nospace -F _git_add git-add.exe
1441 complete -o default -o nospace -F _git_apply git-apply.exe
1442 complete -o default -o nospace -F _git git.exe
1443 complete -o default -o nospace -F _git_branch git-branch.exe
1444 complete -o default -o nospace -F _git_bundle git-bundle.exe
1445 complete -o default -o nospace -F _git_cherry git-cherry.exe
1446 complete -o default -o nospace -F _git_describe git-describe.exe
1447 complete -o default -o nospace -F _git_diff git-diff.exe
1448 complete -o default -o nospace -F _git_format_patch git-format-patch.exe
1449 complete -o default -o nospace -F _git_log git-log.exe
1450 complete -o default -o nospace -F _git_ls_tree git-ls-tree.exe
1451 complete -o default -o nospace -F _git_merge_base git-merge-base.exe
1452 complete -o default -o nospace -F _git_name_rev git-name-rev.exe
1453 complete -o default -o nospace -F _git_push git-push.exe
1454 complete -o default -o nospace -F _git_config git-config
1455 complete -o default -o nospace -F _git_shortlog git-shortlog.exe
1456 complete -o default -o nospace -F _git_show git-show.exe
1457 complete -o default -o nospace -F _git_log git-show-branch.exe
1458 complete -o default -o nospace -F _git_tag git-tag.exe
1459 complete -o default -o nospace -F _git_log git-whatchanged.exe
1460 fi