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