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