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