clone: add a --no-tags option to clone without tags
[git] / contrib / completion / git-completion.bash
1 # bash/zsh completion support for core Git.
2 #
3 # Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
4 # Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
5 # Distributed under the GNU General Public License, version 2.0.
6 #
7 # The contained completion routines provide support for completing:
8 #
9 #    *) local and remote branch names
10 #    *) local and remote tag names
11 #    *) .git/remotes file names
12 #    *) git 'subcommands'
13 #    *) git email aliases for git-send-email
14 #    *) tree paths within 'ref:path/to/file' expressions
15 #    *) file paths within current working directory and index
16 #    *) common --long-options
17 #
18 # To use these routines:
19 #
20 #    1) Copy this file to somewhere (e.g. ~/.git-completion.bash).
21 #    2) Add the following line to your .bashrc/.zshrc:
22 #        source ~/.git-completion.bash
23 #    3) Consider changing your PS1 to also show the current branch,
24 #       see git-prompt.sh for details.
25 #
26 # If you use complex aliases of form '!f() { ... }; f', you can use the null
27 # command ':' as the first command in the function body to declare the desired
28 # completion style.  For example '!f() { : git commit ; ... }; f' will
29 # tell the completion to use commit completion.  This also works with aliases
30 # of form "!sh -c '...'".  For example, "!sh -c ': git commit ; ... '".
31
32 case "$COMP_WORDBREAKS" in
33 *:*) : great ;;
34 *)   COMP_WORDBREAKS="$COMP_WORDBREAKS:"
35 esac
36
37 # Discovers the path to the git repository taking any '--git-dir=<path>' and
38 # '-C <path>' options into account and stores it in the $__git_repo_path
39 # variable.
40 __git_find_repo_path ()
41 {
42         if [ -n "$__git_repo_path" ]; then
43                 # we already know where it is
44                 return
45         fi
46
47         if [ -n "${__git_C_args-}" ]; then
48                 __git_repo_path="$(git "${__git_C_args[@]}" \
49                         ${__git_dir:+--git-dir="$__git_dir"} \
50                         rev-parse --absolute-git-dir 2>/dev/null)"
51         elif [ -n "${__git_dir-}" ]; then
52                 test -d "$__git_dir" &&
53                 __git_repo_path="$__git_dir"
54         elif [ -n "${GIT_DIR-}" ]; then
55                 test -d "${GIT_DIR-}" &&
56                 __git_repo_path="$GIT_DIR"
57         elif [ -d .git ]; then
58                 __git_repo_path=.git
59         else
60                 __git_repo_path="$(git rev-parse --git-dir 2>/dev/null)"
61         fi
62 }
63
64 # Deprecated: use __git_find_repo_path() and $__git_repo_path instead
65 # __gitdir accepts 0 or 1 arguments (i.e., location)
66 # returns location of .git repo
67 __gitdir ()
68 {
69         if [ -z "${1-}" ]; then
70                 __git_find_repo_path || return 1
71                 echo "$__git_repo_path"
72         elif [ -d "$1/.git" ]; then
73                 echo "$1/.git"
74         else
75                 echo "$1"
76         fi
77 }
78
79 # Runs git with all the options given as argument, respecting any
80 # '--git-dir=<path>' and '-C <path>' options present on the command line
81 __git ()
82 {
83         git ${__git_C_args:+"${__git_C_args[@]}"} \
84                 ${__git_dir:+--git-dir="$__git_dir"} "$@" 2>/dev/null
85 }
86
87 # The following function is based on code from:
88 #
89 #   bash_completion - programmable completion functions for bash 3.2+
90 #
91 #   Copyright © 2006-2008, Ian Macdonald <ian@caliban.org>
92 #             © 2009-2010, Bash Completion Maintainers
93 #                     <bash-completion-devel@lists.alioth.debian.org>
94 #
95 #   This program is free software; you can redistribute it and/or modify
96 #   it under the terms of the GNU General Public License as published by
97 #   the Free Software Foundation; either version 2, or (at your option)
98 #   any later version.
99 #
100 #   This program is distributed in the hope that it will be useful,
101 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
102 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
103 #   GNU General Public License for more details.
104 #
105 #   You should have received a copy of the GNU General Public License
106 #   along with this program; if not, write to the Free Software Foundation,
107 #   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
108 #
109 #   The latest version of this software can be obtained here:
110 #
111 #   http://bash-completion.alioth.debian.org/
112 #
113 #   RELEASE: 2.x
114
115 # This function can be used to access a tokenized list of words
116 # on the command line:
117 #
118 #       __git_reassemble_comp_words_by_ref '=:'
119 #       if test "${words_[cword_-1]}" = -w
120 #       then
121 #               ...
122 #       fi
123 #
124 # The argument should be a collection of characters from the list of
125 # word completion separators (COMP_WORDBREAKS) to treat as ordinary
126 # characters.
127 #
128 # This is roughly equivalent to going back in time and setting
129 # COMP_WORDBREAKS to exclude those characters.  The intent is to
130 # make option types like --date=<type> and <rev>:<path> easy to
131 # recognize by treating each shell word as a single token.
132 #
133 # It is best not to set COMP_WORDBREAKS directly because the value is
134 # shared with other completion scripts.  By the time the completion
135 # function gets called, COMP_WORDS has already been populated so local
136 # changes to COMP_WORDBREAKS have no effect.
137 #
138 # Output: words_, cword_, cur_.
139
140 __git_reassemble_comp_words_by_ref()
141 {
142         local exclude i j first
143         # Which word separators to exclude?
144         exclude="${1//[^$COMP_WORDBREAKS]}"
145         cword_=$COMP_CWORD
146         if [ -z "$exclude" ]; then
147                 words_=("${COMP_WORDS[@]}")
148                 return
149         fi
150         # List of word completion separators has shrunk;
151         # re-assemble words to complete.
152         for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
153                 # Append each nonempty word consisting of just
154                 # word separator characters to the current word.
155                 first=t
156                 while
157                         [ $i -gt 0 ] &&
158                         [ -n "${COMP_WORDS[$i]}" ] &&
159                         # word consists of excluded word separators
160                         [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
161                 do
162                         # Attach to the previous token,
163                         # unless the previous token is the command name.
164                         if [ $j -ge 2 ] && [ -n "$first" ]; then
165                                 ((j--))
166                         fi
167                         first=
168                         words_[$j]=${words_[j]}${COMP_WORDS[i]}
169                         if [ $i = $COMP_CWORD ]; then
170                                 cword_=$j
171                         fi
172                         if (($i < ${#COMP_WORDS[@]} - 1)); then
173                                 ((i++))
174                         else
175                                 # Done.
176                                 return
177                         fi
178                 done
179                 words_[$j]=${words_[j]}${COMP_WORDS[i]}
180                 if [ $i = $COMP_CWORD ]; then
181                         cword_=$j
182                 fi
183         done
184 }
185
186 if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
187 _get_comp_words_by_ref ()
188 {
189         local exclude cur_ words_ cword_
190         if [ "$1" = "-n" ]; then
191                 exclude=$2
192                 shift 2
193         fi
194         __git_reassemble_comp_words_by_ref "$exclude"
195         cur_=${words_[cword_]}
196         while [ $# -gt 0 ]; do
197                 case "$1" in
198                 cur)
199                         cur=$cur_
200                         ;;
201                 prev)
202                         prev=${words_[$cword_-1]}
203                         ;;
204                 words)
205                         words=("${words_[@]}")
206                         ;;
207                 cword)
208                         cword=$cword_
209                         ;;
210                 esac
211                 shift
212         done
213 }
214 fi
215
216 # Fills the COMPREPLY array with prefiltered words without any additional
217 # processing.
218 # Callers must take care of providing only words that match the current word
219 # to be completed and adding any prefix and/or suffix (trailing space!), if
220 # necessary.
221 # 1: List of newline-separated matching completion words, complete with
222 #    prefix and suffix.
223 __gitcomp_direct ()
224 {
225         local IFS=$'\n'
226
227         COMPREPLY=($1)
228 }
229
230 __gitcompappend ()
231 {
232         local x i=${#COMPREPLY[@]}
233         for x in $1; do
234                 if [[ "$x" == "$3"* ]]; then
235                         COMPREPLY[i++]="$2$x$4"
236                 fi
237         done
238 }
239
240 __gitcompadd ()
241 {
242         COMPREPLY=()
243         __gitcompappend "$@"
244 }
245
246 # Generates completion reply, appending a space to possible completion words,
247 # if necessary.
248 # It accepts 1 to 4 arguments:
249 # 1: List of possible completion words.
250 # 2: A prefix to be added to each possible completion word (optional).
251 # 3: Generate possible completion matches for this word (optional).
252 # 4: A suffix to be appended to each possible completion word (optional).
253 __gitcomp ()
254 {
255         local cur_="${3-$cur}"
256
257         case "$cur_" in
258         --*=)
259                 ;;
260         *)
261                 local c i=0 IFS=$' \t\n'
262                 for c in $1; do
263                         c="$c${4-}"
264                         if [[ $c == "$cur_"* ]]; then
265                                 case $c in
266                                 --*=*|*.) ;;
267                                 *) c="$c " ;;
268                                 esac
269                                 COMPREPLY[i++]="${2-}$c"
270                         fi
271                 done
272                 ;;
273         esac
274 }
275
276 # Variation of __gitcomp_nl () that appends to the existing list of
277 # completion candidates, COMPREPLY.
278 __gitcomp_nl_append ()
279 {
280         local IFS=$'\n'
281         __gitcompappend "$1" "${2-}" "${3-$cur}" "${4- }"
282 }
283
284 # Generates completion reply from newline-separated possible completion words
285 # by appending a space to all of them.
286 # It accepts 1 to 4 arguments:
287 # 1: List of possible completion words, separated by a single newline.
288 # 2: A prefix to be added to each possible completion word (optional).
289 # 3: Generate possible completion matches for this word (optional).
290 # 4: A suffix to be appended to each possible completion word instead of
291 #    the default space (optional).  If specified but empty, nothing is
292 #    appended.
293 __gitcomp_nl ()
294 {
295         COMPREPLY=()
296         __gitcomp_nl_append "$@"
297 }
298
299 # Generates completion reply with compgen from newline-separated possible
300 # completion filenames.
301 # It accepts 1 to 3 arguments:
302 # 1: List of possible completion filenames, separated by a single newline.
303 # 2: A directory prefix to be added to each possible completion filename
304 #    (optional).
305 # 3: Generate possible completion matches for this word (optional).
306 __gitcomp_file ()
307 {
308         local IFS=$'\n'
309
310         # XXX does not work when the directory prefix contains a tilde,
311         # since tilde expansion is not applied.
312         # This means that COMPREPLY will be empty and Bash default
313         # completion will be used.
314         __gitcompadd "$1" "${2-}" "${3-$cur}" ""
315
316         # use a hack to enable file mode in bash < 4
317         compopt -o filenames +o nospace 2>/dev/null ||
318         compgen -f /non-existing-dir/ > /dev/null
319 }
320
321 # Execute 'git ls-files', unless the --committable option is specified, in
322 # which case it runs 'git diff-index' to find out the files that can be
323 # committed.  It return paths relative to the directory specified in the first
324 # argument, and using the options specified in the second argument.
325 __git_ls_files_helper ()
326 {
327         if [ "$2" == "--committable" ]; then
328                 __git -C "$1" diff-index --name-only --relative HEAD
329         else
330                 # NOTE: $2 is not quoted in order to support multiple options
331                 __git -C "$1" ls-files --exclude-standard $2
332         fi
333 }
334
335
336 # __git_index_files accepts 1 or 2 arguments:
337 # 1: Options to pass to ls-files (required).
338 # 2: A directory path (optional).
339 #    If provided, only files within the specified directory are listed.
340 #    Sub directories are never recursed.  Path must have a trailing
341 #    slash.
342 __git_index_files ()
343 {
344         local root="${2-.}" file
345
346         __git_ls_files_helper "$root" "$1" |
347         while read -r file; do
348                 case "$file" in
349                 ?*/*) echo "${file%%/*}" ;;
350                 *) echo "$file" ;;
351                 esac
352         done | sort | uniq
353 }
354
355 # Lists branches from the local repository.
356 # 1: A prefix to be added to each listed branch (optional).
357 # 2: List only branches matching this word (optional; list all branches if
358 #    unset or empty).
359 # 3: A suffix to be appended to each listed branch (optional).
360 __git_heads ()
361 {
362         local pfx="${1-}" cur_="${2-}" sfx="${3-}"
363
364         __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
365                         "refs/heads/$cur_*" "refs/heads/$cur_*/**"
366 }
367
368 # Lists tags from the local repository.
369 # Accepts the same positional parameters as __git_heads() above.
370 __git_tags ()
371 {
372         local pfx="${1-}" cur_="${2-}" sfx="${3-}"
373
374         __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
375                         "refs/tags/$cur_*" "refs/tags/$cur_*/**"
376 }
377
378 # Lists refs from the local (by default) or from a remote repository.
379 # It accepts 0, 1 or 2 arguments:
380 # 1: The remote to list refs from (optional; ignored, if set but empty).
381 #    Can be the name of a configured remote, a path, or a URL.
382 # 2: In addition to local refs, list unique branches from refs/remotes/ for
383 #    'git checkout's tracking DWIMery (optional; ignored, if set but empty).
384 # 3: A prefix to be added to each listed ref (optional).
385 # 4: List only refs matching this word (optional; list all refs if unset or
386 #    empty).
387 # 5: A suffix to be appended to each listed ref (optional; ignored, if set
388 #    but empty).
389 #
390 # Use __git_complete_refs() instead.
391 __git_refs ()
392 {
393         local i hash dir track="${2-}"
394         local list_refs_from=path remote="${1-}"
395         local format refs
396         local pfx="${3-}" cur_="${4-$cur}" sfx="${5-}"
397         local match="${4-}"
398         local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
399
400         __git_find_repo_path
401         dir="$__git_repo_path"
402
403         if [ -z "$remote" ]; then
404                 if [ -z "$dir" ]; then
405                         return
406                 fi
407         else
408                 if __git_is_configured_remote "$remote"; then
409                         # configured remote takes precedence over a
410                         # local directory with the same name
411                         list_refs_from=remote
412                 elif [ -d "$remote/.git" ]; then
413                         dir="$remote/.git"
414                 elif [ -d "$remote" ]; then
415                         dir="$remote"
416                 else
417                         list_refs_from=url
418                 fi
419         fi
420
421         if [ "$list_refs_from" = path ]; then
422                 if [[ "$cur_" == ^* ]]; then
423                         pfx="$pfx^"
424                         fer_pfx="$fer_pfx^"
425                         cur_=${cur_#^}
426                         match=${match#^}
427                 fi
428                 case "$cur_" in
429                 refs|refs/*)
430                         format="refname"
431                         refs=("$match*" "$match*/**")
432                         track=""
433                         ;;
434                 *)
435                         for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD; do
436                                 case "$i" in
437                                 $match*)
438                                         if [ -e "$dir/$i" ]; then
439                                                 echo "$pfx$i$sfx"
440                                         fi
441                                         ;;
442                                 esac
443                         done
444                         format="refname:strip=2"
445                         refs=("refs/tags/$match*" "refs/tags/$match*/**"
446                                 "refs/heads/$match*" "refs/heads/$match*/**"
447                                 "refs/remotes/$match*" "refs/remotes/$match*/**")
448                         ;;
449                 esac
450                 __git_dir="$dir" __git for-each-ref --format="$fer_pfx%($format)$sfx" \
451                         "${refs[@]}"
452                 if [ -n "$track" ]; then
453                         # employ the heuristic used by git checkout
454                         # Try to find a remote branch that matches the completion word
455                         # but only output if the branch name is unique
456                         __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
457                                 --sort="refname:strip=3" \
458                                 "refs/remotes/*/$match*" "refs/remotes/*/$match*/**" | \
459                         uniq -u
460                 fi
461                 return
462         fi
463         case "$cur_" in
464         refs|refs/*)
465                 __git ls-remote "$remote" "$match*" | \
466                 while read -r hash i; do
467                         case "$i" in
468                         *^{}) ;;
469                         *) echo "$pfx$i$sfx" ;;
470                         esac
471                 done
472                 ;;
473         *)
474                 if [ "$list_refs_from" = remote ]; then
475                         case "HEAD" in
476                         $match*)        echo "${pfx}HEAD$sfx" ;;
477                         esac
478                         __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
479                                 "refs/remotes/$remote/$match*" \
480                                 "refs/remotes/$remote/$match*/**"
481                 else
482                         local query_symref
483                         case "HEAD" in
484                         $match*)        query_symref="HEAD" ;;
485                         esac
486                         __git ls-remote "$remote" $query_symref \
487                                 "refs/tags/$match*" "refs/heads/$match*" \
488                                 "refs/remotes/$match*" |
489                         while read -r hash i; do
490                                 case "$i" in
491                                 *^{})   ;;
492                                 refs/*) echo "$pfx${i#refs/*/}$sfx" ;;
493                                 *)      echo "$pfx$i$sfx" ;;  # symbolic refs
494                                 esac
495                         done
496                 fi
497                 ;;
498         esac
499 }
500
501 # Completes refs, short and long, local and remote, symbolic and pseudo.
502 #
503 # Usage: __git_complete_refs [<option>]...
504 # --remote=<remote>: The remote to list refs from, can be the name of a
505 #                    configured remote, a path, or a URL.
506 # --track: List unique remote branches for 'git checkout's tracking DWIMery.
507 # --pfx=<prefix>: A prefix to be added to each ref.
508 # --cur=<word>: The current ref to be completed.  Defaults to the current
509 #               word to be completed.
510 # --sfx=<suffix>: A suffix to be appended to each ref instead of the default
511 #                 space.
512 __git_complete_refs ()
513 {
514         local remote track pfx cur_="$cur" sfx=" "
515
516         while test $# != 0; do
517                 case "$1" in
518                 --remote=*)     remote="${1##--remote=}" ;;
519                 --track)        track="yes" ;;
520                 --pfx=*)        pfx="${1##--pfx=}" ;;
521                 --cur=*)        cur_="${1##--cur=}" ;;
522                 --sfx=*)        sfx="${1##--sfx=}" ;;
523                 *)              return 1 ;;
524                 esac
525                 shift
526         done
527
528         __gitcomp_direct "$(__git_refs "$remote" "$track" "$pfx" "$cur_" "$sfx")"
529 }
530
531 # __git_refs2 requires 1 argument (to pass to __git_refs)
532 # Deprecated: use __git_complete_fetch_refspecs() instead.
533 __git_refs2 ()
534 {
535         local i
536         for i in $(__git_refs "$1"); do
537                 echo "$i:$i"
538         done
539 }
540
541 # Completes refspecs for fetching from a remote repository.
542 # 1: The remote repository.
543 # 2: A prefix to be added to each listed refspec (optional).
544 # 3: The ref to be completed as a refspec instead of the current word to be
545 #    completed (optional)
546 # 4: A suffix to be appended to each listed refspec instead of the default
547 #    space (optional).
548 __git_complete_fetch_refspecs ()
549 {
550         local i remote="$1" pfx="${2-}" cur_="${3-$cur}" sfx="${4- }"
551
552         __gitcomp_direct "$(
553                 for i in $(__git_refs "$remote" "" "" "$cur_") ; do
554                         echo "$pfx$i:$i$sfx"
555                 done
556                 )"
557 }
558
559 # __git_refs_remotes requires 1 argument (to pass to ls-remote)
560 __git_refs_remotes ()
561 {
562         local i hash
563         __git ls-remote "$1" 'refs/heads/*' | \
564         while read -r hash i; do
565                 echo "$i:refs/remotes/$1/${i#refs/heads/}"
566         done
567 }
568
569 __git_remotes ()
570 {
571         __git_find_repo_path
572         test -d "$__git_repo_path/remotes" && ls -1 "$__git_repo_path/remotes"
573         __git remote
574 }
575
576 # Returns true if $1 matches the name of a configured remote, false otherwise.
577 __git_is_configured_remote ()
578 {
579         local remote
580         for remote in $(__git_remotes); do
581                 if [ "$remote" = "$1" ]; then
582                         return 0
583                 fi
584         done
585         return 1
586 }
587
588 __git_list_merge_strategies ()
589 {
590         git merge -s help 2>&1 |
591         sed -n -e '/[Aa]vailable strategies are: /,/^$/{
592                 s/\.$//
593                 s/.*://
594                 s/^[    ]*//
595                 s/[     ]*$//
596                 p
597         }'
598 }
599
600 __git_merge_strategies=
601 # 'git merge -s help' (and thus detection of the merge strategy
602 # list) fails, unfortunately, if run outside of any git working
603 # tree.  __git_merge_strategies is set to the empty string in
604 # that case, and the detection will be repeated the next time it
605 # is needed.
606 __git_compute_merge_strategies ()
607 {
608         test -n "$__git_merge_strategies" ||
609         __git_merge_strategies=$(__git_list_merge_strategies)
610 }
611
612 __git_complete_revlist_file ()
613 {
614         local pfx ls ref cur_="$cur"
615         case "$cur_" in
616         *..?*:*)
617                 return
618                 ;;
619         ?*:*)
620                 ref="${cur_%%:*}"
621                 cur_="${cur_#*:}"
622                 case "$cur_" in
623                 ?*/*)
624                         pfx="${cur_%/*}"
625                         cur_="${cur_##*/}"
626                         ls="$ref:$pfx"
627                         pfx="$pfx/"
628                         ;;
629                 *)
630                         ls="$ref"
631                         ;;
632                 esac
633
634                 case "$COMP_WORDBREAKS" in
635                 *:*) : great ;;
636                 *)   pfx="$ref:$pfx" ;;
637                 esac
638
639                 __gitcomp_nl "$(__git ls-tree "$ls" \
640                                 | sed '/^100... blob /{
641                                            s,^.*        ,,
642                                            s,$, ,
643                                        }
644                                        /^120000 blob /{
645                                            s,^.*        ,,
646                                            s,$, ,
647                                        }
648                                        /^040000 tree /{
649                                            s,^.*        ,,
650                                            s,$,/,
651                                        }
652                                        s/^.*    //')" \
653                         "$pfx" "$cur_" ""
654                 ;;
655         *...*)
656                 pfx="${cur_%...*}..."
657                 cur_="${cur_#*...}"
658                 __git_complete_refs --pfx="$pfx" --cur="$cur_"
659                 ;;
660         *..*)
661                 pfx="${cur_%..*}.."
662                 cur_="${cur_#*..}"
663                 __git_complete_refs --pfx="$pfx" --cur="$cur_"
664                 ;;
665         *)
666                 __git_complete_refs
667                 ;;
668         esac
669 }
670
671
672 # __git_complete_index_file requires 1 argument:
673 # 1: the options to pass to ls-file
674 #
675 # The exception is --committable, which finds the files appropriate commit.
676 __git_complete_index_file ()
677 {
678         local pfx="" cur_="$cur"
679
680         case "$cur_" in
681         ?*/*)
682                 pfx="${cur_%/*}"
683                 cur_="${cur_##*/}"
684                 pfx="${pfx}/"
685                 ;;
686         esac
687
688         __gitcomp_file "$(__git_index_files "$1" ${pfx:+"$pfx"})" "$pfx" "$cur_"
689 }
690
691 __git_complete_file ()
692 {
693         __git_complete_revlist_file
694 }
695
696 __git_complete_revlist ()
697 {
698         __git_complete_revlist_file
699 }
700
701 __git_complete_remote_or_refspec ()
702 {
703         local cur_="$cur" cmd="${words[1]}"
704         local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
705         if [ "$cmd" = "remote" ]; then
706                 ((c++))
707         fi
708         while [ $c -lt $cword ]; do
709                 i="${words[c]}"
710                 case "$i" in
711                 --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
712                 --all)
713                         case "$cmd" in
714                         push) no_complete_refspec=1 ;;
715                         fetch)
716                                 return
717                                 ;;
718                         *) ;;
719                         esac
720                         ;;
721                 -*) ;;
722                 *) remote="$i"; break ;;
723                 esac
724                 ((c++))
725         done
726         if [ -z "$remote" ]; then
727                 __gitcomp_nl "$(__git_remotes)"
728                 return
729         fi
730         if [ $no_complete_refspec = 1 ]; then
731                 return
732         fi
733         [ "$remote" = "." ] && remote=
734         case "$cur_" in
735         *:*)
736                 case "$COMP_WORDBREAKS" in
737                 *:*) : great ;;
738                 *)   pfx="${cur_%%:*}:" ;;
739                 esac
740                 cur_="${cur_#*:}"
741                 lhs=0
742                 ;;
743         +*)
744                 pfx="+"
745                 cur_="${cur_#+}"
746                 ;;
747         esac
748         case "$cmd" in
749         fetch)
750                 if [ $lhs = 1 ]; then
751                         __git_complete_fetch_refspecs "$remote" "$pfx" "$cur_"
752                 else
753                         __git_complete_refs --pfx="$pfx" --cur="$cur_"
754                 fi
755                 ;;
756         pull|remote)
757                 if [ $lhs = 1 ]; then
758                         __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
759                 else
760                         __git_complete_refs --pfx="$pfx" --cur="$cur_"
761                 fi
762                 ;;
763         push)
764                 if [ $lhs = 1 ]; then
765                         __git_complete_refs --pfx="$pfx" --cur="$cur_"
766                 else
767                         __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
768                 fi
769                 ;;
770         esac
771 }
772
773 __git_complete_strategy ()
774 {
775         __git_compute_merge_strategies
776         case "$prev" in
777         -s|--strategy)
778                 __gitcomp "$__git_merge_strategies"
779                 return 0
780         esac
781         case "$cur" in
782         --strategy=*)
783                 __gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
784                 return 0
785                 ;;
786         esac
787         return 1
788 }
789
790 __git_commands () {
791         if test -n "${GIT_TESTING_COMMAND_COMPLETION:-}"
792         then
793                 printf "%s" "${GIT_TESTING_COMMAND_COMPLETION}"
794         else
795                 git help -a|egrep '^  [a-zA-Z0-9]'
796         fi
797 }
798
799 __git_list_all_commands ()
800 {
801         local i IFS=" "$'\n'
802         for i in $(__git_commands)
803         do
804                 case $i in
805                 *--*)             : helper pattern;;
806                 *) echo $i;;
807                 esac
808         done
809 }
810
811 __git_all_commands=
812 __git_compute_all_commands ()
813 {
814         test -n "$__git_all_commands" ||
815         __git_all_commands=$(__git_list_all_commands)
816 }
817
818 __git_list_porcelain_commands ()
819 {
820         local i IFS=" "$'\n'
821         __git_compute_all_commands
822         for i in $__git_all_commands
823         do
824                 case $i in
825                 *--*)             : helper pattern;;
826                 applymbox)        : ask gittus;;
827                 applypatch)       : ask gittus;;
828                 archimport)       : import;;
829                 cat-file)         : plumbing;;
830                 check-attr)       : plumbing;;
831                 check-ignore)     : plumbing;;
832                 check-mailmap)    : plumbing;;
833                 check-ref-format) : plumbing;;
834                 checkout-index)   : plumbing;;
835                 column)           : internal helper;;
836                 commit-tree)      : plumbing;;
837                 count-objects)    : infrequent;;
838                 credential)       : credentials;;
839                 credential-*)     : credentials helper;;
840                 cvsexportcommit)  : export;;
841                 cvsimport)        : import;;
842                 cvsserver)        : daemon;;
843                 daemon)           : daemon;;
844                 diff-files)       : plumbing;;
845                 diff-index)       : plumbing;;
846                 diff-tree)        : plumbing;;
847                 fast-import)      : import;;
848                 fast-export)      : export;;
849                 fsck-objects)     : plumbing;;
850                 fetch-pack)       : plumbing;;
851                 fmt-merge-msg)    : plumbing;;
852                 for-each-ref)     : plumbing;;
853                 hash-object)      : plumbing;;
854                 http-*)           : transport;;
855                 index-pack)       : plumbing;;
856                 init-db)          : deprecated;;
857                 local-fetch)      : plumbing;;
858                 ls-files)         : plumbing;;
859                 ls-remote)        : plumbing;;
860                 ls-tree)          : plumbing;;
861                 mailinfo)         : plumbing;;
862                 mailsplit)        : plumbing;;
863                 merge-*)          : plumbing;;
864                 mktree)           : plumbing;;
865                 mktag)            : plumbing;;
866                 pack-objects)     : plumbing;;
867                 pack-redundant)   : plumbing;;
868                 pack-refs)        : plumbing;;
869                 parse-remote)     : plumbing;;
870                 patch-id)         : plumbing;;
871                 prune)            : plumbing;;
872                 prune-packed)     : plumbing;;
873                 quiltimport)      : import;;
874                 read-tree)        : plumbing;;
875                 receive-pack)     : plumbing;;
876                 remote-*)         : transport;;
877                 rerere)           : plumbing;;
878                 rev-list)         : plumbing;;
879                 rev-parse)        : plumbing;;
880                 runstatus)        : plumbing;;
881                 sh-setup)         : internal;;
882                 shell)            : daemon;;
883                 show-ref)         : plumbing;;
884                 send-pack)        : plumbing;;
885                 show-index)       : plumbing;;
886                 ssh-*)            : transport;;
887                 stripspace)       : plumbing;;
888                 symbolic-ref)     : plumbing;;
889                 unpack-file)      : plumbing;;
890                 unpack-objects)   : plumbing;;
891                 update-index)     : plumbing;;
892                 update-ref)       : plumbing;;
893                 update-server-info) : daemon;;
894                 upload-archive)   : plumbing;;
895                 upload-pack)      : plumbing;;
896                 write-tree)       : plumbing;;
897                 var)              : infrequent;;
898                 verify-pack)      : infrequent;;
899                 verify-tag)       : plumbing;;
900                 *) echo $i;;
901                 esac
902         done
903 }
904
905 __git_porcelain_commands=
906 __git_compute_porcelain_commands ()
907 {
908         test -n "$__git_porcelain_commands" ||
909         __git_porcelain_commands=$(__git_list_porcelain_commands)
910 }
911
912 # Lists all set config variables starting with the given section prefix,
913 # with the prefix removed.
914 __git_get_config_variables ()
915 {
916         local section="$1" i IFS=$'\n'
917         for i in $(__git config --name-only --get-regexp "^$section\..*"); do
918                 echo "${i#$section.}"
919         done
920 }
921
922 __git_pretty_aliases ()
923 {
924         __git_get_config_variables "pretty"
925 }
926
927 __git_aliases ()
928 {
929         __git_get_config_variables "alias"
930 }
931
932 # __git_aliased_command requires 1 argument
933 __git_aliased_command ()
934 {
935         local word cmdline=$(__git config --get "alias.$1")
936         for word in $cmdline; do
937                 case "$word" in
938                 \!gitk|gitk)
939                         echo "gitk"
940                         return
941                         ;;
942                 \!*)    : shell command alias ;;
943                 -*)     : option ;;
944                 *=*)    : setting env ;;
945                 git)    : git itself ;;
946                 \(\))   : skip parens of shell function definition ;;
947                 {)      : skip start of shell helper function ;;
948                 :)      : skip null command ;;
949                 \'*)    : skip opening quote after sh -c ;;
950                 *)
951                         echo "$word"
952                         return
953                 esac
954         done
955 }
956
957 # __git_find_on_cmdline requires 1 argument
958 __git_find_on_cmdline ()
959 {
960         local word subcommand c=1
961         while [ $c -lt $cword ]; do
962                 word="${words[c]}"
963                 for subcommand in $1; do
964                         if [ "$subcommand" = "$word" ]; then
965                                 echo "$subcommand"
966                                 return
967                         fi
968                 done
969                 ((c++))
970         done
971 }
972
973 # Echo the value of an option set on the command line or config
974 #
975 # $1: short option name
976 # $2: long option name including =
977 # $3: list of possible values
978 # $4: config string (optional)
979 #
980 # example:
981 # result="$(__git_get_option_value "-d" "--do-something=" \
982 #     "yes no" "core.doSomething")"
983 #
984 # result is then either empty (no option set) or "yes" or "no"
985 #
986 # __git_get_option_value requires 3 arguments
987 __git_get_option_value ()
988 {
989         local c short_opt long_opt val
990         local result= values config_key word
991
992         short_opt="$1"
993         long_opt="$2"
994         values="$3"
995         config_key="$4"
996
997         ((c = $cword - 1))
998         while [ $c -ge 0 ]; do
999                 word="${words[c]}"
1000                 for val in $values; do
1001                         if [ "$short_opt$val" = "$word" ] ||
1002                            [ "$long_opt$val"  = "$word" ]; then
1003                                 result="$val"
1004                                 break 2
1005                         fi
1006                 done
1007                 ((c--))
1008         done
1009
1010         if [ -n "$config_key" ] && [ -z "$result" ]; then
1011                 result="$(__git config "$config_key")"
1012         fi
1013
1014         echo "$result"
1015 }
1016
1017 __git_has_doubledash ()
1018 {
1019         local c=1
1020         while [ $c -lt $cword ]; do
1021                 if [ "--" = "${words[c]}" ]; then
1022                         return 0
1023                 fi
1024                 ((c++))
1025         done
1026         return 1
1027 }
1028
1029 # Try to count non option arguments passed on the command line for the
1030 # specified git command.
1031 # When options are used, it is necessary to use the special -- option to
1032 # tell the implementation were non option arguments begin.
1033 # XXX this can not be improved, since options can appear everywhere, as
1034 # an example:
1035 #       git mv x -n y
1036 #
1037 # __git_count_arguments requires 1 argument: the git command executed.
1038 __git_count_arguments ()
1039 {
1040         local word i c=0
1041
1042         # Skip "git" (first argument)
1043         for ((i=1; i < ${#words[@]}; i++)); do
1044                 word="${words[i]}"
1045
1046                 case "$word" in
1047                         --)
1048                                 # Good; we can assume that the following are only non
1049                                 # option arguments.
1050                                 ((c = 0))
1051                                 ;;
1052                         "$1")
1053                                 # Skip the specified git command and discard git
1054                                 # main options
1055                                 ((c = 0))
1056                                 ;;
1057                         ?*)
1058                                 ((c++))
1059                                 ;;
1060                 esac
1061         done
1062
1063         printf "%d" $c
1064 }
1065
1066 __git_whitespacelist="nowarn warn error error-all fix"
1067
1068 _git_am ()
1069 {
1070         __git_find_repo_path
1071         if [ -d "$__git_repo_path"/rebase-apply ]; then
1072                 __gitcomp "--skip --continue --resolved --abort"
1073                 return
1074         fi
1075         case "$cur" in
1076         --whitespace=*)
1077                 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1078                 return
1079                 ;;
1080         --*)
1081                 __gitcomp "
1082                         --3way --committer-date-is-author-date --ignore-date
1083                         --ignore-whitespace --ignore-space-change
1084                         --interactive --keep --no-utf8 --signoff --utf8
1085                         --whitespace= --scissors
1086                         "
1087                 return
1088         esac
1089 }
1090
1091 _git_apply ()
1092 {
1093         case "$cur" in
1094         --whitespace=*)
1095                 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1096                 return
1097                 ;;
1098         --*)
1099                 __gitcomp "
1100                         --stat --numstat --summary --check --index
1101                         --cached --index-info --reverse --reject --unidiff-zero
1102                         --apply --no-add --exclude=
1103                         --ignore-whitespace --ignore-space-change
1104                         --whitespace= --inaccurate-eof --verbose
1105                         --recount --directory=
1106                         "
1107                 return
1108         esac
1109 }
1110
1111 _git_add ()
1112 {
1113         case "$cur" in
1114         --*)
1115                 __gitcomp "
1116                         --interactive --refresh --patch --update --dry-run
1117                         --ignore-errors --intent-to-add --force --edit --chmod=
1118                         "
1119                 return
1120         esac
1121
1122         local complete_opt="--others --modified --directory --no-empty-directory"
1123         if test -n "$(__git_find_on_cmdline "-u --update")"
1124         then
1125                 complete_opt="--modified"
1126         fi
1127         __git_complete_index_file "$complete_opt"
1128 }
1129
1130 _git_archive ()
1131 {
1132         case "$cur" in
1133         --format=*)
1134                 __gitcomp "$(git archive --list)" "" "${cur##--format=}"
1135                 return
1136                 ;;
1137         --remote=*)
1138                 __gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
1139                 return
1140                 ;;
1141         --*)
1142                 __gitcomp "
1143                         --format= --list --verbose
1144                         --prefix= --remote= --exec= --output
1145                         "
1146                 return
1147                 ;;
1148         esac
1149         __git_complete_file
1150 }
1151
1152 _git_bisect ()
1153 {
1154         __git_has_doubledash && return
1155
1156         local subcommands="start bad good skip reset visualize replay log run"
1157         local subcommand="$(__git_find_on_cmdline "$subcommands")"
1158         if [ -z "$subcommand" ]; then
1159                 __git_find_repo_path
1160                 if [ -f "$__git_repo_path"/BISECT_START ]; then
1161                         __gitcomp "$subcommands"
1162                 else
1163                         __gitcomp "replay start"
1164                 fi
1165                 return
1166         fi
1167
1168         case "$subcommand" in
1169         bad|good|reset|skip|start)
1170                 __git_complete_refs
1171                 ;;
1172         *)
1173                 ;;
1174         esac
1175 }
1176
1177 _git_branch ()
1178 {
1179         local i c=1 only_local_ref="n" has_r="n"
1180
1181         while [ $c -lt $cword ]; do
1182                 i="${words[c]}"
1183                 case "$i" in
1184                 -d|--delete|-m|--move)  only_local_ref="y" ;;
1185                 -r|--remotes)           has_r="y" ;;
1186                 esac
1187                 ((c++))
1188         done
1189
1190         case "$cur" in
1191         --set-upstream-to=*)
1192                 __git_complete_refs --cur="${cur##--set-upstream-to=}"
1193                 ;;
1194         --*)
1195                 __gitcomp "
1196                         --color --no-color --verbose --abbrev= --no-abbrev
1197                         --track --no-track --contains --no-contains --merged --no-merged
1198                         --set-upstream-to= --edit-description --list
1199                         --unset-upstream --delete --move --remotes
1200                         --column --no-column --sort= --points-at
1201                         "
1202                 ;;
1203         *)
1204                 if [ $only_local_ref = "y" -a $has_r = "n" ]; then
1205                         __gitcomp_direct "$(__git_heads "" "$cur" " ")"
1206                 else
1207                         __git_complete_refs
1208                 fi
1209                 ;;
1210         esac
1211 }
1212
1213 _git_bundle ()
1214 {
1215         local cmd="${words[2]}"
1216         case "$cword" in
1217         2)
1218                 __gitcomp "create list-heads verify unbundle"
1219                 ;;
1220         3)
1221                 # looking for a file
1222                 ;;
1223         *)
1224                 case "$cmd" in
1225                         create)
1226                                 __git_complete_revlist
1227                         ;;
1228                 esac
1229                 ;;
1230         esac
1231 }
1232
1233 _git_checkout ()
1234 {
1235         __git_has_doubledash && return
1236
1237         case "$cur" in
1238         --conflict=*)
1239                 __gitcomp "diff3 merge" "" "${cur##--conflict=}"
1240                 ;;
1241         --*)
1242                 __gitcomp "
1243                         --quiet --ours --theirs --track --no-track --merge
1244                         --conflict= --orphan --patch
1245                         "
1246                 ;;
1247         *)
1248                 # check if --track, --no-track, or --no-guess was specified
1249                 # if so, disable DWIM mode
1250                 local flags="--track --no-track --no-guess" track_opt="--track"
1251                 if [ -n "$(__git_find_on_cmdline "$flags")" ]; then
1252                         track_opt=''
1253                 fi
1254                 __git_complete_refs $track_opt
1255                 ;;
1256         esac
1257 }
1258
1259 _git_cherry ()
1260 {
1261         __git_complete_refs
1262 }
1263
1264 _git_cherry_pick ()
1265 {
1266         __git_find_repo_path
1267         if [ -f "$__git_repo_path"/CHERRY_PICK_HEAD ]; then
1268                 __gitcomp "--continue --quit --abort"
1269                 return
1270         fi
1271         case "$cur" in
1272         --*)
1273                 __gitcomp "--edit --no-commit --signoff --strategy= --mainline"
1274                 ;;
1275         *)
1276                 __git_complete_refs
1277                 ;;
1278         esac
1279 }
1280
1281 _git_clean ()
1282 {
1283         case "$cur" in
1284         --*)
1285                 __gitcomp "--dry-run --quiet"
1286                 return
1287                 ;;
1288         esac
1289
1290         # XXX should we check for -x option ?
1291         __git_complete_index_file "--others --directory"
1292 }
1293
1294 _git_clone ()
1295 {
1296         case "$cur" in
1297         --*)
1298                 __gitcomp "
1299                         --local
1300                         --no-hardlinks
1301                         --shared
1302                         --reference
1303                         --quiet
1304                         --no-checkout
1305                         --bare
1306                         --mirror
1307                         --origin
1308                         --upload-pack
1309                         --template=
1310                         --depth
1311                         --single-branch
1312                         --no-tags
1313                         --branch
1314                         --recurse-submodules
1315                         --no-single-branch
1316                         --shallow-submodules
1317                         "
1318                 return
1319                 ;;
1320         esac
1321 }
1322
1323 __git_untracked_file_modes="all no normal"
1324
1325 _git_commit ()
1326 {
1327         case "$prev" in
1328         -c|-C)
1329                 __git_complete_refs
1330                 return
1331                 ;;
1332         esac
1333
1334         case "$cur" in
1335         --cleanup=*)
1336                 __gitcomp "default scissors strip verbatim whitespace
1337                         " "" "${cur##--cleanup=}"
1338                 return
1339                 ;;
1340         --reuse-message=*|--reedit-message=*|\
1341         --fixup=*|--squash=*)
1342                 __git_complete_refs --cur="${cur#*=}"
1343                 return
1344                 ;;
1345         --untracked-files=*)
1346                 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
1347                 return
1348                 ;;
1349         --*)
1350                 __gitcomp "
1351                         --all --author= --signoff --verify --no-verify
1352                         --edit --no-edit
1353                         --amend --include --only --interactive
1354                         --dry-run --reuse-message= --reedit-message=
1355                         --reset-author --file= --message= --template=
1356                         --cleanup= --untracked-files --untracked-files=
1357                         --verbose --quiet --fixup= --squash=
1358                         --patch --short --date --allow-empty
1359                         "
1360                 return
1361         esac
1362
1363         if __git rev-parse --verify --quiet HEAD >/dev/null; then
1364                 __git_complete_index_file "--committable"
1365         else
1366                 # This is the first commit
1367                 __git_complete_index_file "--cached"
1368         fi
1369 }
1370
1371 _git_describe ()
1372 {
1373         case "$cur" in
1374         --*)
1375                 __gitcomp "
1376                         --all --tags --contains --abbrev= --candidates=
1377                         --exact-match --debug --long --match --always --first-parent
1378                         --exclude
1379                         "
1380                 return
1381         esac
1382         __git_complete_refs
1383 }
1384
1385 __git_diff_algorithms="myers minimal patience histogram"
1386
1387 __git_diff_submodule_formats="diff log short"
1388
1389 __git_diff_common_options="--stat --numstat --shortstat --summary
1390                         --patch-with-stat --name-only --name-status --color
1391                         --no-color --color-words --no-renames --check
1392                         --full-index --binary --abbrev --diff-filter=
1393                         --find-copies-harder
1394                         --text --ignore-space-at-eol --ignore-space-change
1395                         --ignore-all-space --ignore-blank-lines --exit-code
1396                         --quiet --ext-diff --no-ext-diff
1397                         --no-prefix --src-prefix= --dst-prefix=
1398                         --inter-hunk-context=
1399                         --patience --histogram --minimal
1400                         --raw --word-diff --word-diff-regex=
1401                         --dirstat --dirstat= --dirstat-by-file
1402                         --dirstat-by-file= --cumulative
1403                         --diff-algorithm=
1404                         --submodule --submodule=
1405 "
1406
1407 _git_diff ()
1408 {
1409         __git_has_doubledash && return
1410
1411         case "$cur" in
1412         --diff-algorithm=*)
1413                 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1414                 return
1415                 ;;
1416         --submodule=*)
1417                 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1418                 return
1419                 ;;
1420         --*)
1421                 __gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
1422                         --base --ours --theirs --no-index
1423                         $__git_diff_common_options
1424                         "
1425                 return
1426                 ;;
1427         esac
1428         __git_complete_revlist_file
1429 }
1430
1431 __git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff
1432                         tkdiff vimdiff gvimdiff xxdiff araxis p4merge bc codecompare
1433 "
1434
1435 _git_difftool ()
1436 {
1437         __git_has_doubledash && return
1438
1439         case "$cur" in
1440         --tool=*)
1441                 __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1442                 return
1443                 ;;
1444         --*)
1445                 __gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
1446                         --base --ours --theirs
1447                         --no-renames --diff-filter= --find-copies-harder
1448                         --relative --ignore-submodules
1449                         --tool="
1450                 return
1451                 ;;
1452         esac
1453         __git_complete_revlist_file
1454 }
1455
1456 __git_fetch_recurse_submodules="yes on-demand no"
1457
1458 __git_fetch_options="
1459         --quiet --verbose --append --upload-pack --force --keep --depth=
1460         --tags --no-tags --all --prune --dry-run --recurse-submodules=
1461         --unshallow --update-shallow
1462 "
1463
1464 _git_fetch ()
1465 {
1466         case "$cur" in
1467         --recurse-submodules=*)
1468                 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1469                 return
1470                 ;;
1471         --*)
1472                 __gitcomp "$__git_fetch_options"
1473                 return
1474                 ;;
1475         esac
1476         __git_complete_remote_or_refspec
1477 }
1478
1479 __git_format_patch_options="
1480         --stdout --attach --no-attach --thread --thread= --no-thread
1481         --numbered --start-number --numbered-files --keep-subject --signoff
1482         --signature --no-signature --in-reply-to= --cc= --full-index --binary
1483         --not --all --cover-letter --no-prefix --src-prefix= --dst-prefix=
1484         --inline --suffix= --ignore-if-in-upstream --subject-prefix=
1485         --output-directory --reroll-count --to= --quiet --notes
1486 "
1487
1488 _git_format_patch ()
1489 {
1490         case "$cur" in
1491         --thread=*)
1492                 __gitcomp "
1493                         deep shallow
1494                         " "" "${cur##--thread=}"
1495                 return
1496                 ;;
1497         --*)
1498                 __gitcomp "$__git_format_patch_options"
1499                 return
1500                 ;;
1501         esac
1502         __git_complete_revlist
1503 }
1504
1505 _git_fsck ()
1506 {
1507         case "$cur" in
1508         --*)
1509                 __gitcomp "
1510                         --tags --root --unreachable --cache --no-reflogs --full
1511                         --strict --verbose --lost-found --name-objects
1512                         "
1513                 return
1514                 ;;
1515         esac
1516 }
1517
1518 _git_gc ()
1519 {
1520         case "$cur" in
1521         --*)
1522                 __gitcomp "--prune --aggressive"
1523                 return
1524                 ;;
1525         esac
1526 }
1527
1528 _git_gitk ()
1529 {
1530         _gitk
1531 }
1532
1533 # Lists matching symbol names from a tag (as in ctags) file.
1534 # 1: List symbol names matching this word.
1535 # 2: The tag file to list symbol names from.
1536 # 3: A prefix to be added to each listed symbol name (optional).
1537 # 4: A suffix to be appended to each listed symbol name (optional).
1538 __git_match_ctag () {
1539         awk -v pfx="${3-}" -v sfx="${4-}" "
1540                 /^${1//\//\\/}/ { print pfx \$1 sfx }
1541                 " "$2"
1542 }
1543
1544 # Complete symbol names from a tag file.
1545 # Usage: __git_complete_symbol [<option>]...
1546 # --tags=<file>: The tag file to list symbol names from instead of the
1547 #                default "tags".
1548 # --pfx=<prefix>: A prefix to be added to each symbol name.
1549 # --cur=<word>: The current symbol name to be completed.  Defaults to
1550 #               the current word to be completed.
1551 # --sfx=<suffix>: A suffix to be appended to each symbol name instead
1552 #                 of the default space.
1553 __git_complete_symbol () {
1554         local tags=tags pfx="" cur_="${cur-}" sfx=" "
1555
1556         while test $# != 0; do
1557                 case "$1" in
1558                 --tags=*)       tags="${1##--tags=}" ;;
1559                 --pfx=*)        pfx="${1##--pfx=}" ;;
1560                 --cur=*)        cur_="${1##--cur=}" ;;
1561                 --sfx=*)        sfx="${1##--sfx=}" ;;
1562                 *)              return 1 ;;
1563                 esac
1564                 shift
1565         done
1566
1567         if test -r "$tags"; then
1568                 __gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
1569         fi
1570 }
1571
1572 _git_grep ()
1573 {
1574         __git_has_doubledash && return
1575
1576         case "$cur" in
1577         --*)
1578                 __gitcomp "
1579                         --cached
1580                         --text --ignore-case --word-regexp --invert-match
1581                         --full-name --line-number
1582                         --extended-regexp --basic-regexp --fixed-strings
1583                         --perl-regexp
1584                         --threads
1585                         --files-with-matches --name-only
1586                         --files-without-match
1587                         --max-depth
1588                         --count
1589                         --and --or --not --all-match
1590                         --break --heading --show-function --function-context
1591                         --untracked --no-index
1592                         "
1593                 return
1594                 ;;
1595         esac
1596
1597         case "$cword,$prev" in
1598         2,*|*,-*)
1599                 __git_complete_symbol && return
1600                 ;;
1601         esac
1602
1603         __git_complete_refs
1604 }
1605
1606 _git_help ()
1607 {
1608         case "$cur" in
1609         --*)
1610                 __gitcomp "--all --guides --info --man --web"
1611                 return
1612                 ;;
1613         esac
1614         __git_compute_all_commands
1615         __gitcomp "$__git_all_commands $(__git_aliases)
1616                 attributes cli core-tutorial cvs-migration
1617                 diffcore everyday gitk glossary hooks ignore modules
1618                 namespaces repository-layout revisions tutorial tutorial-2
1619                 workflows
1620                 "
1621 }
1622
1623 _git_init ()
1624 {
1625         case "$cur" in
1626         --shared=*)
1627                 __gitcomp "
1628                         false true umask group all world everybody
1629                         " "" "${cur##--shared=}"
1630                 return
1631                 ;;
1632         --*)
1633                 __gitcomp "--quiet --bare --template= --shared --shared="
1634                 return
1635                 ;;
1636         esac
1637 }
1638
1639 _git_ls_files ()
1640 {
1641         case "$cur" in
1642         --*)
1643                 __gitcomp "--cached --deleted --modified --others --ignored
1644                         --stage --directory --no-empty-directory --unmerged
1645                         --killed --exclude= --exclude-from=
1646                         --exclude-per-directory= --exclude-standard
1647                         --error-unmatch --with-tree= --full-name
1648                         --abbrev --ignored --exclude-per-directory
1649                         "
1650                 return
1651                 ;;
1652         esac
1653
1654         # XXX ignore options like --modified and always suggest all cached
1655         # files.
1656         __git_complete_index_file "--cached"
1657 }
1658
1659 _git_ls_remote ()
1660 {
1661         case "$cur" in
1662         --*)
1663                 __gitcomp "--heads --tags --refs --get-url --symref"
1664                 return
1665                 ;;
1666         esac
1667         __gitcomp_nl "$(__git_remotes)"
1668 }
1669
1670 _git_ls_tree ()
1671 {
1672         __git_complete_file
1673 }
1674
1675 # Options that go well for log, shortlog and gitk
1676 __git_log_common_options="
1677         --not --all
1678         --branches --tags --remotes
1679         --first-parent --merges --no-merges
1680         --max-count=
1681         --max-age= --since= --after=
1682         --min-age= --until= --before=
1683         --min-parents= --max-parents=
1684         --no-min-parents --no-max-parents
1685 "
1686 # Options that go well for log and gitk (not shortlog)
1687 __git_log_gitk_options="
1688         --dense --sparse --full-history
1689         --simplify-merges --simplify-by-decoration
1690         --left-right --notes --no-notes
1691 "
1692 # Options that go well for log and shortlog (not gitk)
1693 __git_log_shortlog_options="
1694         --author= --committer= --grep=
1695         --all-match --invert-grep
1696 "
1697
1698 __git_log_pretty_formats="oneline short medium full fuller email raw format:"
1699 __git_log_date_formats="relative iso8601 rfc2822 short local default raw"
1700
1701 _git_log ()
1702 {
1703         __git_has_doubledash && return
1704         __git_find_repo_path
1705
1706         local merge=""
1707         if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
1708                 merge="--merge"
1709         fi
1710         case "$prev,$cur" in
1711         -L,:*:*)
1712                 return  # fall back to Bash filename completion
1713                 ;;
1714         -L,:*)
1715                 __git_complete_symbol --cur="${cur#:}" --sfx=":"
1716                 return
1717                 ;;
1718         -G,*|-S,*)
1719                 __git_complete_symbol
1720                 return
1721                 ;;
1722         esac
1723         case "$cur" in
1724         --pretty=*|--format=*)
1725                 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
1726                         " "" "${cur#*=}"
1727                 return
1728                 ;;
1729         --date=*)
1730                 __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
1731                 return
1732                 ;;
1733         --decorate=*)
1734                 __gitcomp "full short no" "" "${cur##--decorate=}"
1735                 return
1736                 ;;
1737         --diff-algorithm=*)
1738                 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1739                 return
1740                 ;;
1741         --submodule=*)
1742                 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1743                 return
1744                 ;;
1745         --*)
1746                 __gitcomp "
1747                         $__git_log_common_options
1748                         $__git_log_shortlog_options
1749                         $__git_log_gitk_options
1750                         --root --topo-order --date-order --reverse
1751                         --follow --full-diff
1752                         --abbrev-commit --abbrev=
1753                         --relative-date --date=
1754                         --pretty= --format= --oneline
1755                         --show-signature
1756                         --cherry-mark
1757                         --cherry-pick
1758                         --graph
1759                         --decorate --decorate=
1760                         --walk-reflogs
1761                         --parents --children
1762                         $merge
1763                         $__git_diff_common_options
1764                         --pickaxe-all --pickaxe-regex
1765                         "
1766                 return
1767                 ;;
1768         -L:*:*)
1769                 return  # fall back to Bash filename completion
1770                 ;;
1771         -L:*)
1772                 __git_complete_symbol --cur="${cur#-L:}" --sfx=":"
1773                 return
1774                 ;;
1775         -G*)
1776                 __git_complete_symbol --pfx="-G" --cur="${cur#-G}"
1777                 return
1778                 ;;
1779         -S*)
1780                 __git_complete_symbol --pfx="-S" --cur="${cur#-S}"
1781                 return
1782                 ;;
1783         esac
1784         __git_complete_revlist
1785 }
1786
1787 # Common merge options shared by git-merge(1) and git-pull(1).
1788 __git_merge_options="
1789         --no-commit --no-stat --log --no-log --squash --strategy
1790         --commit --stat --no-squash --ff --no-ff --ff-only --edit --no-edit
1791         --verify-signatures --no-verify-signatures --gpg-sign
1792         --quiet --verbose --progress --no-progress
1793 "
1794
1795 _git_merge ()
1796 {
1797         __git_complete_strategy && return
1798
1799         case "$cur" in
1800         --*)
1801                 __gitcomp "$__git_merge_options
1802                         --rerere-autoupdate --no-rerere-autoupdate --abort --continue"
1803                 return
1804         esac
1805         __git_complete_refs
1806 }
1807
1808 _git_mergetool ()
1809 {
1810         case "$cur" in
1811         --tool=*)
1812                 __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
1813                 return
1814                 ;;
1815         --*)
1816                 __gitcomp "--tool= --prompt --no-prompt"
1817                 return
1818                 ;;
1819         esac
1820 }
1821
1822 _git_merge_base ()
1823 {
1824         case "$cur" in
1825         --*)
1826                 __gitcomp "--octopus --independent --is-ancestor --fork-point"
1827                 return
1828                 ;;
1829         esac
1830         __git_complete_refs
1831 }
1832
1833 _git_mv ()
1834 {
1835         case "$cur" in
1836         --*)
1837                 __gitcomp "--dry-run"
1838                 return
1839                 ;;
1840         esac
1841
1842         if [ $(__git_count_arguments "mv") -gt 0 ]; then
1843                 # We need to show both cached and untracked files (including
1844                 # empty directories) since this may not be the last argument.
1845                 __git_complete_index_file "--cached --others --directory"
1846         else
1847                 __git_complete_index_file "--cached"
1848         fi
1849 }
1850
1851 _git_name_rev ()
1852 {
1853         __gitcomp "--tags --all --stdin"
1854 }
1855
1856 _git_notes ()
1857 {
1858         local subcommands='add append copy edit list prune remove show'
1859         local subcommand="$(__git_find_on_cmdline "$subcommands")"
1860
1861         case "$subcommand,$cur" in
1862         ,--*)
1863                 __gitcomp '--ref'
1864                 ;;
1865         ,*)
1866                 case "$prev" in
1867                 --ref)
1868                         __git_complete_refs
1869                         ;;
1870                 *)
1871                         __gitcomp "$subcommands --ref"
1872                         ;;
1873                 esac
1874                 ;;
1875         add,--reuse-message=*|append,--reuse-message=*|\
1876         add,--reedit-message=*|append,--reedit-message=*)
1877                 __git_complete_refs --cur="${cur#*=}"
1878                 ;;
1879         add,--*|append,--*)
1880                 __gitcomp '--file= --message= --reedit-message=
1881                                 --reuse-message='
1882                 ;;
1883         copy,--*)
1884                 __gitcomp '--stdin'
1885                 ;;
1886         prune,--*)
1887                 __gitcomp '--dry-run --verbose'
1888                 ;;
1889         prune,*)
1890                 ;;
1891         *)
1892                 case "$prev" in
1893                 -m|-F)
1894                         ;;
1895                 *)
1896                         __git_complete_refs
1897                         ;;
1898                 esac
1899                 ;;
1900         esac
1901 }
1902
1903 _git_pull ()
1904 {
1905         __git_complete_strategy && return
1906
1907         case "$cur" in
1908         --recurse-submodules=*)
1909                 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1910                 return
1911                 ;;
1912         --*)
1913                 __gitcomp "
1914                         --rebase --no-rebase
1915                         $__git_merge_options
1916                         $__git_fetch_options
1917                 "
1918                 return
1919                 ;;
1920         esac
1921         __git_complete_remote_or_refspec
1922 }
1923
1924 __git_push_recurse_submodules="check on-demand only"
1925
1926 __git_complete_force_with_lease ()
1927 {
1928         local cur_=$1
1929
1930         case "$cur_" in
1931         --*=)
1932                 ;;
1933         *:*)
1934                 __git_complete_refs --cur="${cur_#*:}"
1935                 ;;
1936         *)
1937                 __git_complete_refs --cur="$cur_"
1938                 ;;
1939         esac
1940 }
1941
1942 _git_push ()
1943 {
1944         case "$prev" in
1945         --repo)
1946                 __gitcomp_nl "$(__git_remotes)"
1947                 return
1948                 ;;
1949         --recurse-submodules)
1950                 __gitcomp "$__git_push_recurse_submodules"
1951                 return
1952                 ;;
1953         esac
1954         case "$cur" in
1955         --repo=*)
1956                 __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
1957                 return
1958                 ;;
1959         --recurse-submodules=*)
1960                 __gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
1961                 return
1962                 ;;
1963         --force-with-lease=*)
1964                 __git_complete_force_with_lease "${cur##--force-with-lease=}"
1965                 return
1966                 ;;
1967         --*)
1968                 __gitcomp "
1969                         --all --mirror --tags --dry-run --force --verbose
1970                         --quiet --prune --delete --follow-tags
1971                         --receive-pack= --repo= --set-upstream
1972                         --force-with-lease --force-with-lease= --recurse-submodules=
1973                 "
1974                 return
1975                 ;;
1976         esac
1977         __git_complete_remote_or_refspec
1978 }
1979
1980 _git_rebase ()
1981 {
1982         __git_find_repo_path
1983         if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
1984                 __gitcomp "--continue --skip --abort --quit --edit-todo"
1985                 return
1986         elif [ -d "$__git_repo_path"/rebase-apply ] || \
1987              [ -d "$__git_repo_path"/rebase-merge ]; then
1988                 __gitcomp "--continue --skip --abort --quit"
1989                 return
1990         fi
1991         __git_complete_strategy && return
1992         case "$cur" in
1993         --whitespace=*)
1994                 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1995                 return
1996                 ;;
1997         --*)
1998                 __gitcomp "
1999                         --onto --merge --strategy --interactive
2000                         --preserve-merges --stat --no-stat
2001                         --committer-date-is-author-date --ignore-date
2002                         --ignore-whitespace --whitespace=
2003                         --autosquash --no-autosquash
2004                         --fork-point --no-fork-point
2005                         --autostash --no-autostash
2006                         --verify --no-verify
2007                         --keep-empty --root --force-rebase --no-ff
2008                         --exec
2009                         "
2010
2011                 return
2012         esac
2013         __git_complete_refs
2014 }
2015
2016 _git_reflog ()
2017 {
2018         local subcommands="show delete expire"
2019         local subcommand="$(__git_find_on_cmdline "$subcommands")"
2020
2021         if [ -z "$subcommand" ]; then
2022                 __gitcomp "$subcommands"
2023         else
2024                 __git_complete_refs
2025         fi
2026 }
2027
2028 __git_send_email_confirm_options="always never auto cc compose"
2029 __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
2030
2031 _git_send_email ()
2032 {
2033         case "$prev" in
2034         --to|--cc|--bcc|--from)
2035                 __gitcomp "$(__git send-email --dump-aliases)"
2036                 return
2037                 ;;
2038         esac
2039
2040         case "$cur" in
2041         --confirm=*)
2042                 __gitcomp "
2043                         $__git_send_email_confirm_options
2044                         " "" "${cur##--confirm=}"
2045                 return
2046                 ;;
2047         --suppress-cc=*)
2048                 __gitcomp "
2049                         $__git_send_email_suppresscc_options
2050                         " "" "${cur##--suppress-cc=}"
2051
2052                 return
2053                 ;;
2054         --smtp-encryption=*)
2055                 __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
2056                 return
2057                 ;;
2058         --thread=*)
2059                 __gitcomp "
2060                         deep shallow
2061                         " "" "${cur##--thread=}"
2062                 return
2063                 ;;
2064         --to=*|--cc=*|--bcc=*|--from=*)
2065                 __gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
2066                 return
2067                 ;;
2068         --*)
2069                 __gitcomp "--annotate --bcc --cc --cc-cmd --chain-reply-to
2070                         --compose --confirm= --dry-run --envelope-sender
2071                         --from --identity
2072                         --in-reply-to --no-chain-reply-to --no-signed-off-by-cc
2073                         --no-suppress-from --no-thread --quiet
2074                         --signed-off-by-cc --smtp-pass --smtp-server
2075                         --smtp-server-port --smtp-encryption= --smtp-user
2076                         --subject --suppress-cc= --suppress-from --thread --to
2077                         --validate --no-validate
2078                         $__git_format_patch_options"
2079                 return
2080                 ;;
2081         esac
2082         __git_complete_revlist
2083 }
2084
2085 _git_stage ()
2086 {
2087         _git_add
2088 }
2089
2090 _git_status ()
2091 {
2092         local complete_opt
2093         local untracked_state
2094
2095         case "$cur" in
2096         --ignore-submodules=*)
2097                 __gitcomp "none untracked dirty all" "" "${cur##--ignore-submodules=}"
2098                 return
2099                 ;;
2100         --untracked-files=*)
2101                 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
2102                 return
2103                 ;;
2104         --column=*)
2105                 __gitcomp "
2106                         always never auto column row plain dense nodense
2107                         " "" "${cur##--column=}"
2108                 return
2109                 ;;
2110         --*)
2111                 __gitcomp "
2112                         --short --branch --porcelain --long --verbose
2113                         --untracked-files= --ignore-submodules= --ignored
2114                         --column= --no-column
2115                         "
2116                 return
2117                 ;;
2118         esac
2119
2120         untracked_state="$(__git_get_option_value "-u" "--untracked-files=" \
2121                 "$__git_untracked_file_modes" "status.showUntrackedFiles")"
2122
2123         case "$untracked_state" in
2124         no)
2125                 # --ignored option does not matter
2126                 complete_opt=
2127                 ;;
2128         all|normal|*)
2129                 complete_opt="--cached --directory --no-empty-directory --others"
2130
2131                 if [ -n "$(__git_find_on_cmdline "--ignored")" ]; then
2132                         complete_opt="$complete_opt --ignored --exclude=*"
2133                 fi
2134                 ;;
2135         esac
2136
2137         __git_complete_index_file "$complete_opt"
2138 }
2139
2140 __git_config_get_set_variables ()
2141 {
2142         local prevword word config_file= c=$cword
2143         while [ $c -gt 1 ]; do
2144                 word="${words[c]}"
2145                 case "$word" in
2146                 --system|--global|--local|--file=*)
2147                         config_file="$word"
2148                         break
2149                         ;;
2150                 -f|--file)
2151                         config_file="$word $prevword"
2152                         break
2153                         ;;
2154                 esac
2155                 prevword=$word
2156                 c=$((--c))
2157         done
2158
2159         __git config $config_file --name-only --list
2160 }
2161
2162 _git_config ()
2163 {
2164         case "$prev" in
2165         branch.*.remote|branch.*.pushremote)
2166                 __gitcomp_nl "$(__git_remotes)"
2167                 return
2168                 ;;
2169         branch.*.merge)
2170                 __git_complete_refs
2171                 return
2172                 ;;
2173         branch.*.rebase)
2174                 __gitcomp "false true preserve interactive"
2175                 return
2176                 ;;
2177         remote.pushdefault)
2178                 __gitcomp_nl "$(__git_remotes)"
2179                 return
2180                 ;;
2181         remote.*.fetch)
2182                 local remote="${prev#remote.}"
2183                 remote="${remote%.fetch}"
2184                 if [ -z "$cur" ]; then
2185                         __gitcomp_nl "refs/heads/" "" "" ""
2186                         return
2187                 fi
2188                 __gitcomp_nl "$(__git_refs_remotes "$remote")"
2189                 return
2190                 ;;
2191         remote.*.push)
2192                 local remote="${prev#remote.}"
2193                 remote="${remote%.push}"
2194                 __gitcomp_nl "$(__git for-each-ref \
2195                         --format='%(refname):%(refname)' refs/heads)"
2196                 return
2197                 ;;
2198         pull.twohead|pull.octopus)
2199                 __git_compute_merge_strategies
2200                 __gitcomp "$__git_merge_strategies"
2201                 return
2202                 ;;
2203         color.branch|color.diff|color.interactive|\
2204         color.showbranch|color.status|color.ui)
2205                 __gitcomp "always never auto"
2206                 return
2207                 ;;
2208         color.pager)
2209                 __gitcomp "false true"
2210                 return
2211                 ;;
2212         color.*.*)
2213                 __gitcomp "
2214                         normal black red green yellow blue magenta cyan white
2215                         bold dim ul blink reverse
2216                         "
2217                 return
2218                 ;;
2219         diff.submodule)
2220                 __gitcomp "log short"
2221                 return
2222                 ;;
2223         help.format)
2224                 __gitcomp "man info web html"
2225                 return
2226                 ;;
2227         log.date)
2228                 __gitcomp "$__git_log_date_formats"
2229                 return
2230                 ;;
2231         sendemail.aliasesfiletype)
2232                 __gitcomp "mutt mailrc pine elm gnus"
2233                 return
2234                 ;;
2235         sendemail.confirm)
2236                 __gitcomp "$__git_send_email_confirm_options"
2237                 return
2238                 ;;
2239         sendemail.suppresscc)
2240                 __gitcomp "$__git_send_email_suppresscc_options"
2241                 return
2242                 ;;
2243         sendemail.transferencoding)
2244                 __gitcomp "7bit 8bit quoted-printable base64"
2245                 return
2246                 ;;
2247         --get|--get-all|--unset|--unset-all)
2248                 __gitcomp_nl "$(__git_config_get_set_variables)"
2249                 return
2250                 ;;
2251         *.*)
2252                 return
2253                 ;;
2254         esac
2255         case "$cur" in
2256         --*)
2257                 __gitcomp "
2258                         --system --global --local --file=
2259                         --list --replace-all
2260                         --get --get-all --get-regexp
2261                         --add --unset --unset-all
2262                         --remove-section --rename-section
2263                         --name-only
2264                         "
2265                 return
2266                 ;;
2267         branch.*.*)
2268                 local pfx="${cur%.*}." cur_="${cur##*.}"
2269                 __gitcomp "remote pushremote merge mergeoptions rebase" "$pfx" "$cur_"
2270                 return
2271                 ;;
2272         branch.*)
2273                 local pfx="${cur%.*}." cur_="${cur#*.}"
2274                 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
2275                 __gitcomp_nl_append $'autosetupmerge\nautosetuprebase\n' "$pfx" "$cur_"
2276                 return
2277                 ;;
2278         guitool.*.*)
2279                 local pfx="${cur%.*}." cur_="${cur##*.}"
2280                 __gitcomp "
2281                         argprompt cmd confirm needsfile noconsole norescan
2282                         prompt revprompt revunmerged title
2283                         " "$pfx" "$cur_"
2284                 return
2285                 ;;
2286         difftool.*.*)
2287                 local pfx="${cur%.*}." cur_="${cur##*.}"
2288                 __gitcomp "cmd path" "$pfx" "$cur_"
2289                 return
2290                 ;;
2291         man.*.*)
2292                 local pfx="${cur%.*}." cur_="${cur##*.}"
2293                 __gitcomp "cmd path" "$pfx" "$cur_"
2294                 return
2295                 ;;
2296         mergetool.*.*)
2297                 local pfx="${cur%.*}." cur_="${cur##*.}"
2298                 __gitcomp "cmd path trustExitCode" "$pfx" "$cur_"
2299                 return
2300                 ;;
2301         pager.*)
2302                 local pfx="${cur%.*}." cur_="${cur#*.}"
2303                 __git_compute_all_commands
2304                 __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_"
2305                 return
2306                 ;;
2307         remote.*.*)
2308                 local pfx="${cur%.*}." cur_="${cur##*.}"
2309                 __gitcomp "
2310                         url proxy fetch push mirror skipDefaultUpdate
2311                         receivepack uploadpack tagopt pushurl
2312                         " "$pfx" "$cur_"
2313                 return
2314                 ;;
2315         remote.*)
2316                 local pfx="${cur%.*}." cur_="${cur#*.}"
2317                 __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
2318                 __gitcomp_nl_append "pushdefault" "$pfx" "$cur_"
2319                 return
2320                 ;;
2321         url.*.*)
2322                 local pfx="${cur%.*}." cur_="${cur##*.}"
2323                 __gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_"
2324                 return
2325                 ;;
2326         esac
2327         __gitcomp "
2328                 add.ignoreErrors
2329                 advice.commitBeforeMerge
2330                 advice.detachedHead
2331                 advice.implicitIdentity
2332                 advice.pushNonFastForward
2333                 advice.resolveConflict
2334                 advice.statusHints
2335                 alias.
2336                 am.keepcr
2337                 apply.ignorewhitespace
2338                 apply.whitespace
2339                 branch.autosetupmerge
2340                 branch.autosetuprebase
2341                 browser.
2342                 clean.requireForce
2343                 color.branch
2344                 color.branch.current
2345                 color.branch.local
2346                 color.branch.plain
2347                 color.branch.remote
2348                 color.decorate.HEAD
2349                 color.decorate.branch
2350                 color.decorate.remoteBranch
2351                 color.decorate.stash
2352                 color.decorate.tag
2353                 color.diff
2354                 color.diff.commit
2355                 color.diff.frag
2356                 color.diff.func
2357                 color.diff.meta
2358                 color.diff.new
2359                 color.diff.old
2360                 color.diff.plain
2361                 color.diff.whitespace
2362                 color.grep
2363                 color.grep.context
2364                 color.grep.filename
2365                 color.grep.function
2366                 color.grep.linenumber
2367                 color.grep.match
2368                 color.grep.selected
2369                 color.grep.separator
2370                 color.interactive
2371                 color.interactive.error
2372                 color.interactive.header
2373                 color.interactive.help
2374                 color.interactive.prompt
2375                 color.pager
2376                 color.showbranch
2377                 color.status
2378                 color.status.added
2379                 color.status.changed
2380                 color.status.header
2381                 color.status.nobranch
2382                 color.status.unmerged
2383                 color.status.untracked
2384                 color.status.updated
2385                 color.ui
2386                 commit.status
2387                 commit.template
2388                 core.abbrev
2389                 core.askpass
2390                 core.attributesfile
2391                 core.autocrlf
2392                 core.bare
2393                 core.bigFileThreshold
2394                 core.compression
2395                 core.createObject
2396                 core.deltaBaseCacheLimit
2397                 core.editor
2398                 core.eol
2399                 core.excludesfile
2400                 core.fileMode
2401                 core.fsyncobjectfiles
2402                 core.gitProxy
2403                 core.ignoreStat
2404                 core.ignorecase
2405                 core.logAllRefUpdates
2406                 core.loosecompression
2407                 core.notesRef
2408                 core.packedGitLimit
2409                 core.packedGitWindowSize
2410                 core.pager
2411                 core.preferSymlinkRefs
2412                 core.preloadindex
2413                 core.quotepath
2414                 core.repositoryFormatVersion
2415                 core.safecrlf
2416                 core.sharedRepository
2417                 core.sparseCheckout
2418                 core.symlinks
2419                 core.trustctime
2420                 core.untrackedCache
2421                 core.warnAmbiguousRefs
2422                 core.whitespace
2423                 core.worktree
2424                 diff.autorefreshindex
2425                 diff.external
2426                 diff.ignoreSubmodules
2427                 diff.mnemonicprefix
2428                 diff.noprefix
2429                 diff.renameLimit
2430                 diff.renames
2431                 diff.statGraphWidth
2432                 diff.submodule
2433                 diff.suppressBlankEmpty
2434                 diff.tool
2435                 diff.wordRegex
2436                 diff.algorithm
2437                 difftool.
2438                 difftool.prompt
2439                 fetch.recurseSubmodules
2440                 fetch.unpackLimit
2441                 format.attach
2442                 format.cc
2443                 format.coverLetter
2444                 format.from
2445                 format.headers
2446                 format.numbered
2447                 format.pretty
2448                 format.signature
2449                 format.signoff
2450                 format.subjectprefix
2451                 format.suffix
2452                 format.thread
2453                 format.to
2454                 gc.
2455                 gc.aggressiveWindow
2456                 gc.auto
2457                 gc.autopacklimit
2458                 gc.packrefs
2459                 gc.pruneexpire
2460                 gc.reflogexpire
2461                 gc.reflogexpireunreachable
2462                 gc.rerereresolved
2463                 gc.rerereunresolved
2464                 gitcvs.allbinary
2465                 gitcvs.commitmsgannotation
2466                 gitcvs.dbTableNamePrefix
2467                 gitcvs.dbdriver
2468                 gitcvs.dbname
2469                 gitcvs.dbpass
2470                 gitcvs.dbuser
2471                 gitcvs.enabled
2472                 gitcvs.logfile
2473                 gitcvs.usecrlfattr
2474                 guitool.
2475                 gui.blamehistoryctx
2476                 gui.commitmsgwidth
2477                 gui.copyblamethreshold
2478                 gui.diffcontext
2479                 gui.encoding
2480                 gui.fastcopyblame
2481                 gui.matchtrackingbranch
2482                 gui.newbranchtemplate
2483                 gui.pruneduringfetch
2484                 gui.spellingdictionary
2485                 gui.trustmtime
2486                 help.autocorrect
2487                 help.browser
2488                 help.format
2489                 http.lowSpeedLimit
2490                 http.lowSpeedTime
2491                 http.maxRequests
2492                 http.minSessions
2493                 http.noEPSV
2494                 http.postBuffer
2495                 http.proxy
2496                 http.sslCipherList
2497                 http.sslVersion
2498                 http.sslCAInfo
2499                 http.sslCAPath
2500                 http.sslCert
2501                 http.sslCertPasswordProtected
2502                 http.sslKey
2503                 http.sslVerify
2504                 http.useragent
2505                 i18n.commitEncoding
2506                 i18n.logOutputEncoding
2507                 imap.authMethod
2508                 imap.folder
2509                 imap.host
2510                 imap.pass
2511                 imap.port
2512                 imap.preformattedHTML
2513                 imap.sslverify
2514                 imap.tunnel
2515                 imap.user
2516                 init.templatedir
2517                 instaweb.browser
2518                 instaweb.httpd
2519                 instaweb.local
2520                 instaweb.modulepath
2521                 instaweb.port
2522                 interactive.singlekey
2523                 log.date
2524                 log.decorate
2525                 log.showroot
2526                 mailmap.file
2527                 man.
2528                 man.viewer
2529                 merge.
2530                 merge.conflictstyle
2531                 merge.log
2532                 merge.renameLimit
2533                 merge.renormalize
2534                 merge.stat
2535                 merge.tool
2536                 merge.verbosity
2537                 mergetool.
2538                 mergetool.keepBackup
2539                 mergetool.keepTemporaries
2540                 mergetool.prompt
2541                 notes.displayRef
2542                 notes.rewrite.
2543                 notes.rewrite.amend
2544                 notes.rewrite.rebase
2545                 notes.rewriteMode
2546                 notes.rewriteRef
2547                 pack.compression
2548                 pack.deltaCacheLimit
2549                 pack.deltaCacheSize
2550                 pack.depth
2551                 pack.indexVersion
2552                 pack.packSizeLimit
2553                 pack.threads
2554                 pack.window
2555                 pack.windowMemory
2556                 pager.
2557                 pretty.
2558                 pull.octopus
2559                 pull.twohead
2560                 push.default
2561                 push.followTags
2562                 rebase.autosquash
2563                 rebase.stat
2564                 receive.autogc
2565                 receive.denyCurrentBranch
2566                 receive.denyDeleteCurrent
2567                 receive.denyDeletes
2568                 receive.denyNonFastForwards
2569                 receive.fsckObjects
2570                 receive.unpackLimit
2571                 receive.updateserverinfo
2572                 remote.pushdefault
2573                 remotes.
2574                 repack.usedeltabaseoffset
2575                 rerere.autoupdate
2576                 rerere.enabled
2577                 sendemail.
2578                 sendemail.aliasesfile
2579                 sendemail.aliasfiletype
2580                 sendemail.bcc
2581                 sendemail.cc
2582                 sendemail.cccmd
2583                 sendemail.chainreplyto
2584                 sendemail.confirm
2585                 sendemail.envelopesender
2586                 sendemail.from
2587                 sendemail.identity
2588                 sendemail.multiedit
2589                 sendemail.signedoffbycc
2590                 sendemail.smtpdomain
2591                 sendemail.smtpencryption
2592                 sendemail.smtppass
2593                 sendemail.smtpserver
2594                 sendemail.smtpserveroption
2595                 sendemail.smtpserverport
2596                 sendemail.smtpuser
2597                 sendemail.suppresscc
2598                 sendemail.suppressfrom
2599                 sendemail.thread
2600                 sendemail.to
2601                 sendemail.validate
2602                 showbranch.default
2603                 status.relativePaths
2604                 status.showUntrackedFiles
2605                 status.submodulesummary
2606                 submodule.
2607                 tar.umask
2608                 transfer.unpackLimit
2609                 url.
2610                 user.email
2611                 user.name
2612                 user.signingkey
2613                 web.browser
2614                 branch. remote.
2615         "
2616 }
2617
2618 _git_remote ()
2619 {
2620         local subcommands="
2621                 add rename remove set-head set-branches
2622                 get-url set-url show prune update
2623                 "
2624         local subcommand="$(__git_find_on_cmdline "$subcommands")"
2625         if [ -z "$subcommand" ]; then
2626                 case "$cur" in
2627                 --*)
2628                         __gitcomp "--verbose"
2629                         ;;
2630                 *)
2631                         __gitcomp "$subcommands"
2632                         ;;
2633                 esac
2634                 return
2635         fi
2636
2637         case "$subcommand,$cur" in
2638         add,--*)
2639                 __gitcomp "--track --master --fetch --tags --no-tags --mirror="
2640                 ;;
2641         add,*)
2642                 ;;
2643         set-head,--*)
2644                 __gitcomp "--auto --delete"
2645                 ;;
2646         set-branches,--*)
2647                 __gitcomp "--add"
2648                 ;;
2649         set-head,*|set-branches,*)
2650                 __git_complete_remote_or_refspec
2651                 ;;
2652         update,--*)
2653                 __gitcomp "--prune"
2654                 ;;
2655         update,*)
2656                 __gitcomp "$(__git_get_config_variables "remotes")"
2657                 ;;
2658         set-url,--*)
2659                 __gitcomp "--push --add --delete"
2660                 ;;
2661         get-url,--*)
2662                 __gitcomp "--push --all"
2663                 ;;
2664         prune,--*)
2665                 __gitcomp "--dry-run"
2666                 ;;
2667         *)
2668                 __gitcomp_nl "$(__git_remotes)"
2669                 ;;
2670         esac
2671 }
2672
2673 _git_replace ()
2674 {
2675         case "$cur" in
2676         --*)
2677                 __gitcomp "--edit --graft --format= --list --delete"
2678                 return
2679                 ;;
2680         esac
2681         __git_complete_refs
2682 }
2683
2684 _git_rerere ()
2685 {
2686         local subcommands="clear forget diff remaining status gc"
2687         local subcommand="$(__git_find_on_cmdline "$subcommands")"
2688         if test -z "$subcommand"
2689         then
2690                 __gitcomp "$subcommands"
2691                 return
2692         fi
2693 }
2694
2695 _git_reset ()
2696 {
2697         __git_has_doubledash && return
2698
2699         case "$cur" in
2700         --*)
2701                 __gitcomp "--merge --mixed --hard --soft --patch --keep"
2702                 return
2703                 ;;
2704         esac
2705         __git_complete_refs
2706 }
2707
2708 _git_revert ()
2709 {
2710         __git_find_repo_path
2711         if [ -f "$__git_repo_path"/REVERT_HEAD ]; then
2712                 __gitcomp "--continue --quit --abort"
2713                 return
2714         fi
2715         case "$cur" in
2716         --*)
2717                 __gitcomp "
2718                         --edit --mainline --no-edit --no-commit --signoff
2719                         --strategy= --strategy-option=
2720                         "
2721                 return
2722                 ;;
2723         esac
2724         __git_complete_refs
2725 }
2726
2727 _git_rm ()
2728 {
2729         case "$cur" in
2730         --*)
2731                 __gitcomp "--cached --dry-run --ignore-unmatch --quiet"
2732                 return
2733                 ;;
2734         esac
2735
2736         __git_complete_index_file "--cached"
2737 }
2738
2739 _git_shortlog ()
2740 {
2741         __git_has_doubledash && return
2742
2743         case "$cur" in
2744         --*)
2745                 __gitcomp "
2746                         $__git_log_common_options
2747                         $__git_log_shortlog_options
2748                         --numbered --summary --email
2749                         "
2750                 return
2751                 ;;
2752         esac
2753         __git_complete_revlist
2754 }
2755
2756 _git_show ()
2757 {
2758         __git_has_doubledash && return
2759
2760         case "$cur" in
2761         --pretty=*|--format=*)
2762                 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2763                         " "" "${cur#*=}"
2764                 return
2765                 ;;
2766         --diff-algorithm=*)
2767                 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
2768                 return
2769                 ;;
2770         --submodule=*)
2771                 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
2772                 return
2773                 ;;
2774         --*)
2775                 __gitcomp "--pretty= --format= --abbrev-commit --oneline
2776                         --show-signature
2777                         $__git_diff_common_options
2778                         "
2779                 return
2780                 ;;
2781         esac
2782         __git_complete_revlist_file
2783 }
2784
2785 _git_show_branch ()
2786 {
2787         case "$cur" in
2788         --*)
2789                 __gitcomp "
2790                         --all --remotes --topo-order --date-order --current --more=
2791                         --list --independent --merge-base --no-name
2792                         --color --no-color
2793                         --sha1-name --sparse --topics --reflog
2794                         "
2795                 return
2796                 ;;
2797         esac
2798         __git_complete_revlist
2799 }
2800
2801 _git_stash ()
2802 {
2803         local save_opts='--all --keep-index --no-keep-index --quiet --patch --include-untracked'
2804         local subcommands='save list show apply clear drop pop create branch'
2805         local subcommand="$(__git_find_on_cmdline "$subcommands")"
2806         if [ -z "$subcommand" ]; then
2807                 case "$cur" in
2808                 --*)
2809                         __gitcomp "$save_opts"
2810                         ;;
2811                 *)
2812                         if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
2813                                 __gitcomp "$subcommands"
2814                         fi
2815                         ;;
2816                 esac
2817         else
2818                 case "$subcommand,$cur" in
2819                 save,--*)
2820                         __gitcomp "$save_opts"
2821                         ;;
2822                 apply,--*|pop,--*)
2823                         __gitcomp "--index --quiet"
2824                         ;;
2825                 drop,--*)
2826                         __gitcomp "--quiet"
2827                         ;;
2828                 show,--*|branch,--*)
2829                         ;;
2830                 branch,*)
2831                         if [ $cword -eq 3 ]; then
2832                                 __git_complete_refs
2833                         else
2834                                 __gitcomp_nl "$(__git stash list \
2835                                                 | sed -n -e 's/:.*//p')"
2836                         fi
2837                         ;;
2838                 show,*|apply,*|drop,*|pop,*)
2839                         __gitcomp_nl "$(__git stash list \
2840                                         | sed -n -e 's/:.*//p')"
2841                         ;;
2842                 *)
2843                         ;;
2844                 esac
2845         fi
2846 }
2847
2848 _git_submodule ()
2849 {
2850         __git_has_doubledash && return
2851
2852         local subcommands="add status init deinit update summary foreach sync"
2853         local subcommand="$(__git_find_on_cmdline "$subcommands")"
2854         if [ -z "$subcommand" ]; then
2855                 case "$cur" in
2856                 --*)
2857                         __gitcomp "--quiet"
2858                         ;;
2859                 *)
2860                         __gitcomp "$subcommands"
2861                         ;;
2862                 esac
2863                 return
2864         fi
2865
2866         case "$subcommand,$cur" in
2867         add,--*)
2868                 __gitcomp "--branch --force --name --reference --depth"
2869                 ;;
2870         status,--*)
2871                 __gitcomp "--cached --recursive"
2872                 ;;
2873         deinit,--*)
2874                 __gitcomp "--force --all"
2875                 ;;
2876         update,--*)
2877                 __gitcomp "
2878                         --init --remote --no-fetch
2879                         --recommend-shallow --no-recommend-shallow
2880                         --force --rebase --merge --reference --depth --recursive --jobs
2881                 "
2882                 ;;
2883         summary,--*)
2884                 __gitcomp "--cached --files --summary-limit"
2885                 ;;
2886         foreach,--*|sync,--*)
2887                 __gitcomp "--recursive"
2888                 ;;
2889         *)
2890                 ;;
2891         esac
2892 }
2893
2894 _git_svn ()
2895 {
2896         local subcommands="
2897                 init fetch clone rebase dcommit log find-rev
2898                 set-tree commit-diff info create-ignore propget
2899                 proplist show-ignore show-externals branch tag blame
2900                 migrate mkdirs reset gc
2901                 "
2902         local subcommand="$(__git_find_on_cmdline "$subcommands")"
2903         if [ -z "$subcommand" ]; then
2904                 __gitcomp "$subcommands"
2905         else
2906                 local remote_opts="--username= --config-dir= --no-auth-cache"
2907                 local fc_opts="
2908                         --follow-parent --authors-file= --repack=
2909                         --no-metadata --use-svm-props --use-svnsync-props
2910                         --log-window-size= --no-checkout --quiet
2911                         --repack-flags --use-log-author --localtime
2912                         --add-author-from
2913                         --ignore-paths= --include-paths= $remote_opts
2914                         "
2915                 local init_opts="
2916                         --template= --shared= --trunk= --tags=
2917                         --branches= --stdlayout --minimize-url
2918                         --no-metadata --use-svm-props --use-svnsync-props
2919                         --rewrite-root= --prefix= $remote_opts
2920                         "
2921                 local cmt_opts="
2922                         --edit --rmdir --find-copies-harder --copy-similarity=
2923                         "
2924
2925                 case "$subcommand,$cur" in
2926                 fetch,--*)
2927                         __gitcomp "--revision= --fetch-all $fc_opts"
2928                         ;;
2929                 clone,--*)
2930                         __gitcomp "--revision= $fc_opts $init_opts"
2931                         ;;
2932                 init,--*)
2933                         __gitcomp "$init_opts"
2934                         ;;
2935                 dcommit,--*)
2936                         __gitcomp "
2937                                 --merge --strategy= --verbose --dry-run
2938                                 --fetch-all --no-rebase --commit-url
2939                                 --revision --interactive $cmt_opts $fc_opts
2940                                 "
2941                         ;;
2942                 set-tree,--*)
2943                         __gitcomp "--stdin $cmt_opts $fc_opts"
2944                         ;;
2945                 create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
2946                 show-externals,--*|mkdirs,--*)
2947                         __gitcomp "--revision="
2948                         ;;
2949                 log,--*)
2950                         __gitcomp "
2951                                 --limit= --revision= --verbose --incremental
2952                                 --oneline --show-commit --non-recursive
2953                                 --authors-file= --color
2954                                 "
2955                         ;;
2956                 rebase,--*)
2957                         __gitcomp "
2958                                 --merge --verbose --strategy= --local
2959                                 --fetch-all --dry-run $fc_opts
2960                                 "
2961                         ;;
2962                 commit-diff,--*)
2963                         __gitcomp "--message= --file= --revision= $cmt_opts"
2964                         ;;
2965                 info,--*)
2966                         __gitcomp "--url"
2967                         ;;
2968                 branch,--*)
2969                         __gitcomp "--dry-run --message --tag"
2970                         ;;
2971                 tag,--*)
2972                         __gitcomp "--dry-run --message"
2973                         ;;
2974                 blame,--*)
2975                         __gitcomp "--git-format"
2976                         ;;
2977                 migrate,--*)
2978                         __gitcomp "
2979                                 --config-dir= --ignore-paths= --minimize
2980                                 --no-auth-cache --username=
2981                                 "
2982                         ;;
2983                 reset,--*)
2984                         __gitcomp "--revision= --parent"
2985                         ;;
2986                 *)
2987                         ;;
2988                 esac
2989         fi
2990 }
2991
2992 _git_tag ()
2993 {
2994         local i c=1 f=0
2995         while [ $c -lt $cword ]; do
2996                 i="${words[c]}"
2997                 case "$i" in
2998                 -d|-v)
2999                         __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3000                         return
3001                         ;;
3002                 -f)
3003                         f=1
3004                         ;;
3005                 esac
3006                 ((c++))
3007         done
3008
3009         case "$prev" in
3010         -m|-F)
3011                 ;;
3012         -*|tag)
3013                 if [ $f = 1 ]; then
3014                         __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3015                 fi
3016                 ;;
3017         *)
3018                 __git_complete_refs
3019                 ;;
3020         esac
3021
3022         case "$cur" in
3023         --*)
3024                 __gitcomp "
3025                         --list --delete --verify --annotate --message --file
3026                         --sign --cleanup --local-user --force --column --sort=
3027                         --contains --no-contains --points-at --merged --no-merged --create-reflog
3028                         "
3029                 ;;
3030         esac
3031 }
3032
3033 _git_whatchanged ()
3034 {
3035         _git_log
3036 }
3037
3038 _git_worktree ()
3039 {
3040         local subcommands="add list lock prune unlock"
3041         local subcommand="$(__git_find_on_cmdline "$subcommands")"
3042         if [ -z "$subcommand" ]; then
3043                 __gitcomp "$subcommands"
3044         else
3045                 case "$subcommand,$cur" in
3046                 add,--*)
3047                         __gitcomp "--detach"
3048                         ;;
3049                 list,--*)
3050                         __gitcomp "--porcelain"
3051                         ;;
3052                 lock,--*)
3053                         __gitcomp "--reason"
3054                         ;;
3055                 prune,--*)
3056                         __gitcomp "--dry-run --expire --verbose"
3057                         ;;
3058                 *)
3059                         ;;
3060                 esac
3061         fi
3062 }
3063
3064 __git_main ()
3065 {
3066         local i c=1 command __git_dir __git_repo_path
3067         local __git_C_args C_args_count=0
3068
3069         while [ $c -lt $cword ]; do
3070                 i="${words[c]}"
3071                 case "$i" in
3072                 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
3073                 --git-dir)   ((c++)) ; __git_dir="${words[c]}" ;;
3074                 --bare)      __git_dir="." ;;
3075                 --help) command="help"; break ;;
3076                 -c|--work-tree|--namespace) ((c++)) ;;
3077                 -C)     __git_C_args[C_args_count++]=-C
3078                         ((c++))
3079                         __git_C_args[C_args_count++]="${words[c]}"
3080                         ;;
3081                 -*) ;;
3082                 *) command="$i"; break ;;
3083                 esac
3084                 ((c++))
3085         done
3086
3087         if [ -z "$command" ]; then
3088                 case "$prev" in
3089                 --git-dir|-C|--work-tree)
3090                         # these need a path argument, let's fall back to
3091                         # Bash filename completion
3092                         return
3093                         ;;
3094                 -c|--namespace)
3095                         # we don't support completing these options' arguments
3096                         return
3097                         ;;
3098                 esac
3099                 case "$cur" in
3100                 --*)   __gitcomp "
3101                         --paginate
3102                         --no-pager
3103                         --git-dir=
3104                         --bare
3105                         --version
3106                         --exec-path
3107                         --exec-path=
3108                         --html-path
3109                         --man-path
3110                         --info-path
3111                         --work-tree=
3112                         --namespace=
3113                         --no-replace-objects
3114                         --help
3115                         "
3116                         ;;
3117                 *)     __git_compute_porcelain_commands
3118                        __gitcomp "$__git_porcelain_commands $(__git_aliases)" ;;
3119                 esac
3120                 return
3121         fi
3122
3123         local completion_func="_git_${command//-/_}"
3124         declare -f $completion_func >/dev/null 2>/dev/null && $completion_func && return
3125
3126         local expansion=$(__git_aliased_command "$command")
3127         if [ -n "$expansion" ]; then
3128                 words[1]=$expansion
3129                 completion_func="_git_${expansion//-/_}"
3130                 declare -f $completion_func >/dev/null 2>/dev/null && $completion_func
3131         fi
3132 }
3133
3134 __gitk_main ()
3135 {
3136         __git_has_doubledash && return
3137
3138         local __git_repo_path
3139         __git_find_repo_path
3140
3141         local merge=""
3142         if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
3143                 merge="--merge"
3144         fi
3145         case "$cur" in
3146         --*)
3147                 __gitcomp "
3148                         $__git_log_common_options
3149                         $__git_log_gitk_options
3150                         $merge
3151                         "
3152                 return
3153                 ;;
3154         esac
3155         __git_complete_revlist
3156 }
3157
3158 if [[ -n ${ZSH_VERSION-} ]]; then
3159         echo "WARNING: this script is deprecated, please see git-completion.zsh" 1>&2
3160
3161         autoload -U +X compinit && compinit
3162
3163         __gitcomp ()
3164         {
3165                 emulate -L zsh
3166
3167                 local cur_="${3-$cur}"
3168
3169                 case "$cur_" in
3170                 --*=)
3171                         ;;
3172                 *)
3173                         local c IFS=$' \t\n'
3174                         local -a array
3175                         for c in ${=1}; do
3176                                 c="$c${4-}"
3177                                 case $c in
3178                                 --*=*|*.) ;;
3179                                 *) c="$c " ;;
3180                                 esac
3181                                 array[${#array[@]}+1]="$c"
3182                         done
3183                         compset -P '*[=:]'
3184                         compadd -Q -S '' -p "${2-}" -a -- array && _ret=0
3185                         ;;
3186                 esac
3187         }
3188
3189         __gitcomp_direct ()
3190         {
3191                 emulate -L zsh
3192
3193                 local IFS=$'\n'
3194                 compset -P '*[=:]'
3195                 compadd -Q -- ${=1} && _ret=0
3196         }
3197
3198         __gitcomp_nl ()
3199         {
3200                 emulate -L zsh
3201
3202                 local IFS=$'\n'
3203                 compset -P '*[=:]'
3204                 compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
3205         }
3206
3207         __gitcomp_file ()
3208         {
3209                 emulate -L zsh
3210
3211                 local IFS=$'\n'
3212                 compset -P '*[=:]'
3213                 compadd -Q -p "${2-}" -f -- ${=1} && _ret=0
3214         }
3215
3216         _git ()
3217         {
3218                 local _ret=1 cur cword prev
3219                 cur=${words[CURRENT]}
3220                 prev=${words[CURRENT-1]}
3221                 let cword=CURRENT-1
3222                 emulate ksh -c __${service}_main
3223                 let _ret && _default && _ret=0
3224                 return _ret
3225         }
3226
3227         compdef _git git gitk
3228         return
3229 fi
3230
3231 __git_func_wrap ()
3232 {
3233         local cur words cword prev
3234         _get_comp_words_by_ref -n =: cur words cword prev
3235         $1
3236 }
3237
3238 # Setup completion for certain functions defined above by setting common
3239 # variables and workarounds.
3240 # This is NOT a public function; use at your own risk.
3241 __git_complete ()
3242 {
3243         local wrapper="__git_wrap${2}"
3244         eval "$wrapper () { __git_func_wrap $2 ; }"
3245         complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
3246                 || complete -o default -o nospace -F $wrapper $1
3247 }
3248
3249 # wrapper for backwards compatibility
3250 _git ()
3251 {
3252         __git_wrap__git_main
3253 }
3254
3255 # wrapper for backwards compatibility
3256 _gitk ()
3257 {
3258         __git_wrap__gitk_main
3259 }
3260
3261 __git_complete git __git_main
3262 __git_complete gitk __gitk_main
3263
3264 # The following are necessary only for Cygwin, and only are needed
3265 # when the user has tab-completed the executable name and consequently
3266 # included the '.exe' suffix.
3267 #
3268 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
3269 __git_complete git.exe __git_main
3270 fi