Imported Upstream version 2.18.0
[platform/upstream/git.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_all_commands=
993 __git_compute_all_commands ()
994 {
995         test -n "$__git_all_commands" ||
996         __git_all_commands=$(git --list-cmds=main,others,alias,nohelpers)
997 }
998
999 # Lists all set config variables starting with the given section prefix,
1000 # with the prefix removed.
1001 __git_get_config_variables ()
1002 {
1003         local section="$1" i IFS=$'\n'
1004         for i in $(__git config --name-only --get-regexp "^$section\..*"); do
1005                 echo "${i#$section.}"
1006         done
1007 }
1008
1009 __git_pretty_aliases ()
1010 {
1011         __git_get_config_variables "pretty"
1012 }
1013
1014 # __git_aliased_command requires 1 argument
1015 __git_aliased_command ()
1016 {
1017         local word cmdline=$(__git config --get "alias.$1")
1018         for word in $cmdline; do
1019                 case "$word" in
1020                 \!gitk|gitk)
1021                         echo "gitk"
1022                         return
1023                         ;;
1024                 \!*)    : shell command alias ;;
1025                 -*)     : option ;;
1026                 *=*)    : setting env ;;
1027                 git)    : git itself ;;
1028                 \(\))   : skip parens of shell function definition ;;
1029                 {)      : skip start of shell helper function ;;
1030                 :)      : skip null command ;;
1031                 \'*)    : skip opening quote after sh -c ;;
1032                 *)
1033                         echo "$word"
1034                         return
1035                 esac
1036         done
1037 }
1038
1039 # __git_find_on_cmdline requires 1 argument
1040 __git_find_on_cmdline ()
1041 {
1042         local word subcommand c=1
1043         while [ $c -lt $cword ]; do
1044                 word="${words[c]}"
1045                 for subcommand in $1; do
1046                         if [ "$subcommand" = "$word" ]; then
1047                                 echo "$subcommand"
1048                                 return
1049                         fi
1050                 done
1051                 ((c++))
1052         done
1053 }
1054
1055 # Echo the value of an option set on the command line or config
1056 #
1057 # $1: short option name
1058 # $2: long option name including =
1059 # $3: list of possible values
1060 # $4: config string (optional)
1061 #
1062 # example:
1063 # result="$(__git_get_option_value "-d" "--do-something=" \
1064 #     "yes no" "core.doSomething")"
1065 #
1066 # result is then either empty (no option set) or "yes" or "no"
1067 #
1068 # __git_get_option_value requires 3 arguments
1069 __git_get_option_value ()
1070 {
1071         local c short_opt long_opt val
1072         local result= values config_key word
1073
1074         short_opt="$1"
1075         long_opt="$2"
1076         values="$3"
1077         config_key="$4"
1078
1079         ((c = $cword - 1))
1080         while [ $c -ge 0 ]; do
1081                 word="${words[c]}"
1082                 for val in $values; do
1083                         if [ "$short_opt$val" = "$word" ] ||
1084                            [ "$long_opt$val"  = "$word" ]; then
1085                                 result="$val"
1086                                 break 2
1087                         fi
1088                 done
1089                 ((c--))
1090         done
1091
1092         if [ -n "$config_key" ] && [ -z "$result" ]; then
1093                 result="$(__git config "$config_key")"
1094         fi
1095
1096         echo "$result"
1097 }
1098
1099 __git_has_doubledash ()
1100 {
1101         local c=1
1102         while [ $c -lt $cword ]; do
1103                 if [ "--" = "${words[c]}" ]; then
1104                         return 0
1105                 fi
1106                 ((c++))
1107         done
1108         return 1
1109 }
1110
1111 # Try to count non option arguments passed on the command line for the
1112 # specified git command.
1113 # When options are used, it is necessary to use the special -- option to
1114 # tell the implementation were non option arguments begin.
1115 # XXX this can not be improved, since options can appear everywhere, as
1116 # an example:
1117 #       git mv x -n y
1118 #
1119 # __git_count_arguments requires 1 argument: the git command executed.
1120 __git_count_arguments ()
1121 {
1122         local word i c=0
1123
1124         # Skip "git" (first argument)
1125         for ((i=1; i < ${#words[@]}; i++)); do
1126                 word="${words[i]}"
1127
1128                 case "$word" in
1129                         --)
1130                                 # Good; we can assume that the following are only non
1131                                 # option arguments.
1132                                 ((c = 0))
1133                                 ;;
1134                         "$1")
1135                                 # Skip the specified git command and discard git
1136                                 # main options
1137                                 ((c = 0))
1138                                 ;;
1139                         ?*)
1140                                 ((c++))
1141                                 ;;
1142                 esac
1143         done
1144
1145         printf "%d" $c
1146 }
1147
1148 __git_whitespacelist="nowarn warn error error-all fix"
1149 __git_am_inprogress_options="--skip --continue --resolved --abort --quit --show-current-patch"
1150
1151 _git_am ()
1152 {
1153         __git_find_repo_path
1154         if [ -d "$__git_repo_path"/rebase-apply ]; then
1155                 __gitcomp "$__git_am_inprogress_options"
1156                 return
1157         fi
1158         case "$cur" in
1159         --whitespace=*)
1160                 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1161                 return
1162                 ;;
1163         --*)
1164                 __gitcomp_builtin am "--no-utf8" \
1165                         "$__git_am_inprogress_options"
1166                 return
1167         esac
1168 }
1169
1170 _git_apply ()
1171 {
1172         case "$cur" in
1173         --whitespace=*)
1174                 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1175                 return
1176                 ;;
1177         --*)
1178                 __gitcomp_builtin apply
1179                 return
1180         esac
1181 }
1182
1183 _git_add ()
1184 {
1185         case "$cur" in
1186         --*)
1187                 __gitcomp_builtin add
1188                 return
1189         esac
1190
1191         local complete_opt="--others --modified --directory --no-empty-directory"
1192         if test -n "$(__git_find_on_cmdline "-u --update")"
1193         then
1194                 complete_opt="--modified"
1195         fi
1196         __git_complete_index_file "$complete_opt"
1197 }
1198
1199 _git_archive ()
1200 {
1201         case "$cur" in
1202         --format=*)
1203                 __gitcomp "$(git archive --list)" "" "${cur##--format=}"
1204                 return
1205                 ;;
1206         --remote=*)
1207                 __gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
1208                 return
1209                 ;;
1210         --*)
1211                 __gitcomp "
1212                         --format= --list --verbose
1213                         --prefix= --remote= --exec= --output
1214                         "
1215                 return
1216                 ;;
1217         esac
1218         __git_complete_file
1219 }
1220
1221 _git_bisect ()
1222 {
1223         __git_has_doubledash && return
1224
1225         local subcommands="start bad good skip reset visualize replay log run"
1226         local subcommand="$(__git_find_on_cmdline "$subcommands")"
1227         if [ -z "$subcommand" ]; then
1228                 __git_find_repo_path
1229                 if [ -f "$__git_repo_path"/BISECT_START ]; then
1230                         __gitcomp "$subcommands"
1231                 else
1232                         __gitcomp "replay start"
1233                 fi
1234                 return
1235         fi
1236
1237         case "$subcommand" in
1238         bad|good|reset|skip|start)
1239                 __git_complete_refs
1240                 ;;
1241         *)
1242                 ;;
1243         esac
1244 }
1245
1246 _git_branch ()
1247 {
1248         local i c=1 only_local_ref="n" has_r="n"
1249
1250         while [ $c -lt $cword ]; do
1251                 i="${words[c]}"
1252                 case "$i" in
1253                 -d|--delete|-m|--move)  only_local_ref="y" ;;
1254                 -r|--remotes)           has_r="y" ;;
1255                 esac
1256                 ((c++))
1257         done
1258
1259         case "$cur" in
1260         --set-upstream-to=*)
1261                 __git_complete_refs --cur="${cur##--set-upstream-to=}"
1262                 ;;
1263         --*)
1264                 __gitcomp_builtin branch "--no-color --no-abbrev
1265                         --no-track --no-column
1266                         "
1267                 ;;
1268         *)
1269                 if [ $only_local_ref = "y" -a $has_r = "n" ]; then
1270                         __gitcomp_direct "$(__git_heads "" "$cur" " ")"
1271                 else
1272                         __git_complete_refs
1273                 fi
1274                 ;;
1275         esac
1276 }
1277
1278 _git_bundle ()
1279 {
1280         local cmd="${words[2]}"
1281         case "$cword" in
1282         2)
1283                 __gitcomp "create list-heads verify unbundle"
1284                 ;;
1285         3)
1286                 # looking for a file
1287                 ;;
1288         *)
1289                 case "$cmd" in
1290                         create)
1291                                 __git_complete_revlist
1292                         ;;
1293                 esac
1294                 ;;
1295         esac
1296 }
1297
1298 _git_checkout ()
1299 {
1300         __git_has_doubledash && return
1301
1302         case "$cur" in
1303         --conflict=*)
1304                 __gitcomp "diff3 merge" "" "${cur##--conflict=}"
1305                 ;;
1306         --*)
1307                 __gitcomp_builtin checkout "--no-track --no-recurse-submodules"
1308                 ;;
1309         *)
1310                 # check if --track, --no-track, or --no-guess was specified
1311                 # if so, disable DWIM mode
1312                 local flags="--track --no-track --no-guess" track_opt="--track"
1313                 if [ "$GIT_COMPLETION_CHECKOUT_NO_GUESS" = "1" ] ||
1314                    [ -n "$(__git_find_on_cmdline "$flags")" ]; then
1315                         track_opt=''
1316                 fi
1317                 __git_complete_refs $track_opt
1318                 ;;
1319         esac
1320 }
1321
1322 _git_cherry ()
1323 {
1324         case "$cur" in
1325         --*)
1326                 __gitcomp_builtin cherry
1327                 return
1328         esac
1329
1330         __git_complete_refs
1331 }
1332
1333 __git_cherry_pick_inprogress_options="--continue --quit --abort"
1334
1335 _git_cherry_pick ()
1336 {
1337         __git_find_repo_path
1338         if [ -f "$__git_repo_path"/CHERRY_PICK_HEAD ]; then
1339                 __gitcomp "$__git_cherry_pick_inprogress_options"
1340                 return
1341         fi
1342         case "$cur" in
1343         --*)
1344                 __gitcomp_builtin cherry-pick "" \
1345                         "$__git_cherry_pick_inprogress_options"
1346                 ;;
1347         *)
1348                 __git_complete_refs
1349                 ;;
1350         esac
1351 }
1352
1353 _git_clean ()
1354 {
1355         case "$cur" in
1356         --*)
1357                 __gitcomp_builtin clean
1358                 return
1359                 ;;
1360         esac
1361
1362         # XXX should we check for -x option ?
1363         __git_complete_index_file "--others --directory"
1364 }
1365
1366 _git_clone ()
1367 {
1368         case "$cur" in
1369         --*)
1370                 __gitcomp_builtin clone "--no-single-branch"
1371                 return
1372                 ;;
1373         esac
1374 }
1375
1376 __git_untracked_file_modes="all no normal"
1377
1378 _git_commit ()
1379 {
1380         case "$prev" in
1381         -c|-C)
1382                 __git_complete_refs
1383                 return
1384                 ;;
1385         esac
1386
1387         case "$cur" in
1388         --cleanup=*)
1389                 __gitcomp "default scissors strip verbatim whitespace
1390                         " "" "${cur##--cleanup=}"
1391                 return
1392                 ;;
1393         --reuse-message=*|--reedit-message=*|\
1394         --fixup=*|--squash=*)
1395                 __git_complete_refs --cur="${cur#*=}"
1396                 return
1397                 ;;
1398         --untracked-files=*)
1399                 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
1400                 return
1401                 ;;
1402         --*)
1403                 __gitcomp_builtin commit "--no-edit --verify"
1404                 return
1405         esac
1406
1407         if __git rev-parse --verify --quiet HEAD >/dev/null; then
1408                 __git_complete_index_file "--committable"
1409         else
1410                 # This is the first commit
1411                 __git_complete_index_file "--cached"
1412         fi
1413 }
1414
1415 _git_describe ()
1416 {
1417         case "$cur" in
1418         --*)
1419                 __gitcomp_builtin describe
1420                 return
1421         esac
1422         __git_complete_refs
1423 }
1424
1425 __git_diff_algorithms="myers minimal patience histogram"
1426
1427 __git_diff_submodule_formats="diff log short"
1428
1429 __git_diff_common_options="--stat --numstat --shortstat --summary
1430                         --patch-with-stat --name-only --name-status --color
1431                         --no-color --color-words --no-renames --check
1432                         --full-index --binary --abbrev --diff-filter=
1433                         --find-copies-harder --ignore-cr-at-eol
1434                         --text --ignore-space-at-eol --ignore-space-change
1435                         --ignore-all-space --ignore-blank-lines --exit-code
1436                         --quiet --ext-diff --no-ext-diff
1437                         --no-prefix --src-prefix= --dst-prefix=
1438                         --inter-hunk-context=
1439                         --patience --histogram --minimal
1440                         --raw --word-diff --word-diff-regex=
1441                         --dirstat --dirstat= --dirstat-by-file
1442                         --dirstat-by-file= --cumulative
1443                         --diff-algorithm=
1444                         --submodule --submodule= --ignore-submodules
1445 "
1446
1447 _git_diff ()
1448 {
1449         __git_has_doubledash && return
1450
1451         case "$cur" in
1452         --diff-algorithm=*)
1453                 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1454                 return
1455                 ;;
1456         --submodule=*)
1457                 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1458                 return
1459                 ;;
1460         --*)
1461                 __gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
1462                         --base --ours --theirs --no-index
1463                         $__git_diff_common_options
1464                         "
1465                 return
1466                 ;;
1467         esac
1468         __git_complete_revlist_file
1469 }
1470
1471 __git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff
1472                         tkdiff vimdiff gvimdiff xxdiff araxis p4merge bc codecompare
1473 "
1474
1475 _git_difftool ()
1476 {
1477         __git_has_doubledash && return
1478
1479         case "$cur" in
1480         --tool=*)
1481                 __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1482                 return
1483                 ;;
1484         --*)
1485                 __gitcomp_builtin difftool "$__git_diff_common_options
1486                                         --base --cached --ours --theirs
1487                                         --pickaxe-all --pickaxe-regex
1488                                         --relative --staged
1489                                         "
1490                 return
1491                 ;;
1492         esac
1493         __git_complete_revlist_file
1494 }
1495
1496 __git_fetch_recurse_submodules="yes on-demand no"
1497
1498 _git_fetch ()
1499 {
1500         case "$cur" in
1501         --recurse-submodules=*)
1502                 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1503                 return
1504                 ;;
1505         --*)
1506                 __gitcomp_builtin fetch "--no-tags"
1507                 return
1508                 ;;
1509         esac
1510         __git_complete_remote_or_refspec
1511 }
1512
1513 __git_format_patch_options="
1514         --stdout --attach --no-attach --thread --thread= --no-thread
1515         --numbered --start-number --numbered-files --keep-subject --signoff
1516         --signature --no-signature --in-reply-to= --cc= --full-index --binary
1517         --not --all --cover-letter --no-prefix --src-prefix= --dst-prefix=
1518         --inline --suffix= --ignore-if-in-upstream --subject-prefix=
1519         --output-directory --reroll-count --to= --quiet --notes
1520 "
1521
1522 _git_format_patch ()
1523 {
1524         case "$cur" in
1525         --thread=*)
1526                 __gitcomp "
1527                         deep shallow
1528                         " "" "${cur##--thread=}"
1529                 return
1530                 ;;
1531         --*)
1532                 __gitcomp "$__git_format_patch_options"
1533                 return
1534                 ;;
1535         esac
1536         __git_complete_revlist
1537 }
1538
1539 _git_fsck ()
1540 {
1541         case "$cur" in
1542         --*)
1543                 __gitcomp_builtin fsck "--no-reflogs"
1544                 return
1545                 ;;
1546         esac
1547 }
1548
1549 _git_gitk ()
1550 {
1551         _gitk
1552 }
1553
1554 # Lists matching symbol names from a tag (as in ctags) file.
1555 # 1: List symbol names matching this word.
1556 # 2: The tag file to list symbol names from.
1557 # 3: A prefix to be added to each listed symbol name (optional).
1558 # 4: A suffix to be appended to each listed symbol name (optional).
1559 __git_match_ctag () {
1560         awk -v pfx="${3-}" -v sfx="${4-}" "
1561                 /^${1//\//\\/}/ { print pfx \$1 sfx }
1562                 " "$2"
1563 }
1564
1565 # Complete symbol names from a tag file.
1566 # Usage: __git_complete_symbol [<option>]...
1567 # --tags=<file>: The tag file to list symbol names from instead of the
1568 #                default "tags".
1569 # --pfx=<prefix>: A prefix to be added to each symbol name.
1570 # --cur=<word>: The current symbol name to be completed.  Defaults to
1571 #               the current word to be completed.
1572 # --sfx=<suffix>: A suffix to be appended to each symbol name instead
1573 #                 of the default space.
1574 __git_complete_symbol () {
1575         local tags=tags pfx="" cur_="${cur-}" sfx=" "
1576
1577         while test $# != 0; do
1578                 case "$1" in
1579                 --tags=*)       tags="${1##--tags=}" ;;
1580                 --pfx=*)        pfx="${1##--pfx=}" ;;
1581                 --cur=*)        cur_="${1##--cur=}" ;;
1582                 --sfx=*)        sfx="${1##--sfx=}" ;;
1583                 *)              return 1 ;;
1584                 esac
1585                 shift
1586         done
1587
1588         if test -r "$tags"; then
1589                 __gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
1590         fi
1591 }
1592
1593 _git_grep ()
1594 {
1595         __git_has_doubledash && return
1596
1597         case "$cur" in
1598         --*)
1599                 __gitcomp_builtin grep
1600                 return
1601                 ;;
1602         esac
1603
1604         case "$cword,$prev" in
1605         2,*|*,-*)
1606                 __git_complete_symbol && return
1607                 ;;
1608         esac
1609
1610         __git_complete_refs
1611 }
1612
1613 _git_help ()
1614 {
1615         case "$cur" in
1616         --*)
1617                 __gitcomp_builtin help
1618                 return
1619                 ;;
1620         esac
1621         if test -n "$GIT_TESTING_ALL_COMMAND_LIST"
1622         then
1623                 __gitcomp "$GIT_TESTING_ALL_COMMAND_LIST $(git --list-cmds=alias,list-guide) gitk"
1624         else
1625                 __gitcomp "$(git --list-cmds=main,nohelpers,alias,list-guide) gitk"
1626         fi
1627 }
1628
1629 _git_init ()
1630 {
1631         case "$cur" in
1632         --shared=*)
1633                 __gitcomp "
1634                         false true umask group all world everybody
1635                         " "" "${cur##--shared=}"
1636                 return
1637                 ;;
1638         --*)
1639                 __gitcomp_builtin init
1640                 return
1641                 ;;
1642         esac
1643 }
1644
1645 _git_ls_files ()
1646 {
1647         case "$cur" in
1648         --*)
1649                 __gitcomp_builtin ls-files "--no-empty-directory"
1650                 return
1651                 ;;
1652         esac
1653
1654         # XXX ignore options like --modified and always suggest all cached
1655         # files.
1656         __git_complete_index_file "--cached"
1657 }
1658
1659 _git_ls_remote ()
1660 {
1661         case "$cur" in
1662         --*)
1663                 __gitcomp_builtin ls-remote
1664                 return
1665                 ;;
1666         esac
1667         __gitcomp_nl "$(__git_remotes)"
1668 }
1669
1670 _git_ls_tree ()
1671 {
1672         case "$cur" in
1673         --*)
1674                 __gitcomp_builtin ls-tree
1675                 return
1676                 ;;
1677         esac
1678
1679         __git_complete_file
1680 }
1681
1682 # Options that go well for log, shortlog and gitk
1683 __git_log_common_options="
1684         --not --all
1685         --branches --tags --remotes
1686         --first-parent --merges --no-merges
1687         --max-count=
1688         --max-age= --since= --after=
1689         --min-age= --until= --before=
1690         --min-parents= --max-parents=
1691         --no-min-parents --no-max-parents
1692 "
1693 # Options that go well for log and gitk (not shortlog)
1694 __git_log_gitk_options="
1695         --dense --sparse --full-history
1696         --simplify-merges --simplify-by-decoration
1697         --left-right --notes --no-notes
1698 "
1699 # Options that go well for log and shortlog (not gitk)
1700 __git_log_shortlog_options="
1701         --author= --committer= --grep=
1702         --all-match --invert-grep
1703 "
1704
1705 __git_log_pretty_formats="oneline short medium full fuller email raw format:"
1706 __git_log_date_formats="relative iso8601 rfc2822 short local default raw"
1707
1708 _git_log ()
1709 {
1710         __git_has_doubledash && return
1711         __git_find_repo_path
1712
1713         local merge=""
1714         if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
1715                 merge="--merge"
1716         fi
1717         case "$prev,$cur" in
1718         -L,:*:*)
1719                 return  # fall back to Bash filename completion
1720                 ;;
1721         -L,:*)
1722                 __git_complete_symbol --cur="${cur#:}" --sfx=":"
1723                 return
1724                 ;;
1725         -G,*|-S,*)
1726                 __git_complete_symbol
1727                 return
1728                 ;;
1729         esac
1730         case "$cur" in
1731         --pretty=*|--format=*)
1732                 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
1733                         " "" "${cur#*=}"
1734                 return
1735                 ;;
1736         --date=*)
1737                 __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
1738                 return
1739                 ;;
1740         --decorate=*)
1741                 __gitcomp "full short no" "" "${cur##--decorate=}"
1742                 return
1743                 ;;
1744         --diff-algorithm=*)
1745                 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1746                 return
1747                 ;;
1748         --submodule=*)
1749                 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1750                 return
1751                 ;;
1752         --*)
1753                 __gitcomp "
1754                         $__git_log_common_options
1755                         $__git_log_shortlog_options
1756                         $__git_log_gitk_options
1757                         --root --topo-order --date-order --reverse
1758                         --follow --full-diff
1759                         --abbrev-commit --abbrev=
1760                         --relative-date --date=
1761                         --pretty= --format= --oneline
1762                         --show-signature
1763                         --cherry-mark
1764                         --cherry-pick
1765                         --graph
1766                         --decorate --decorate=
1767                         --walk-reflogs
1768                         --parents --children
1769                         $merge
1770                         $__git_diff_common_options
1771                         --pickaxe-all --pickaxe-regex
1772                         "
1773                 return
1774                 ;;
1775         -L:*:*)
1776                 return  # fall back to Bash filename completion
1777                 ;;
1778         -L:*)
1779                 __git_complete_symbol --cur="${cur#-L:}" --sfx=":"
1780                 return
1781                 ;;
1782         -G*)
1783                 __git_complete_symbol --pfx="-G" --cur="${cur#-G}"
1784                 return
1785                 ;;
1786         -S*)
1787                 __git_complete_symbol --pfx="-S" --cur="${cur#-S}"
1788                 return
1789                 ;;
1790         esac
1791         __git_complete_revlist
1792 }
1793
1794 _git_merge ()
1795 {
1796         __git_complete_strategy && return
1797
1798         case "$cur" in
1799         --*)
1800                 __gitcomp_builtin merge "--no-rerere-autoupdate
1801                                 --no-commit --no-edit --no-ff
1802                                 --no-log --no-progress
1803                                 --no-squash --no-stat
1804                                 --no-verify-signatures
1805                                 "
1806                 return
1807         esac
1808         __git_complete_refs
1809 }
1810
1811 _git_mergetool ()
1812 {
1813         case "$cur" in
1814         --tool=*)
1815                 __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
1816                 return
1817                 ;;
1818         --*)
1819                 __gitcomp "--tool= --prompt --no-prompt"
1820                 return
1821                 ;;
1822         esac
1823 }
1824
1825 _git_merge_base ()
1826 {
1827         case "$cur" in
1828         --*)
1829                 __gitcomp_builtin merge-base
1830                 return
1831                 ;;
1832         esac
1833         __git_complete_refs
1834 }
1835
1836 _git_mv ()
1837 {
1838         case "$cur" in
1839         --*)
1840                 __gitcomp_builtin mv
1841                 return
1842                 ;;
1843         esac
1844
1845         if [ $(__git_count_arguments "mv") -gt 0 ]; then
1846                 # We need to show both cached and untracked files (including
1847                 # empty directories) since this may not be the last argument.
1848                 __git_complete_index_file "--cached --others --directory"
1849         else
1850                 __git_complete_index_file "--cached"
1851         fi
1852 }
1853
1854 _git_notes ()
1855 {
1856         local subcommands='add append copy edit get-ref list merge prune remove show'
1857         local subcommand="$(__git_find_on_cmdline "$subcommands")"
1858
1859         case "$subcommand,$cur" in
1860         ,--*)
1861                 __gitcomp_builtin notes
1862                 ;;
1863         ,*)
1864                 case "$prev" in
1865                 --ref)
1866                         __git_complete_refs
1867                         ;;
1868                 *)
1869                         __gitcomp "$subcommands --ref"
1870                         ;;
1871                 esac
1872                 ;;
1873         *,--reuse-message=*|*,--reedit-message=*)
1874                 __git_complete_refs --cur="${cur#*=}"
1875                 ;;
1876         *,--*)
1877                 __gitcomp_builtin notes_$subcommand
1878                 ;;
1879         prune,*|get-ref,*)
1880                 # this command does not take a ref, do not complete it
1881                 ;;
1882         *)
1883                 case "$prev" in
1884                 -m|-F)
1885                         ;;
1886                 *)
1887                         __git_complete_refs
1888                         ;;
1889                 esac
1890                 ;;
1891         esac
1892 }
1893
1894 _git_pull ()
1895 {
1896         __git_complete_strategy && return
1897
1898         case "$cur" in
1899         --recurse-submodules=*)
1900                 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1901                 return
1902                 ;;
1903         --*)
1904                 __gitcomp_builtin pull "--no-autostash --no-commit --no-edit
1905                                         --no-ff --no-log --no-progress --no-rebase
1906                                         --no-squash --no-stat --no-tags
1907                                         --no-verify-signatures"
1908
1909                 return
1910                 ;;
1911         esac
1912         __git_complete_remote_or_refspec
1913 }
1914
1915 __git_push_recurse_submodules="check on-demand only"
1916
1917 __git_complete_force_with_lease ()
1918 {
1919         local cur_=$1
1920
1921         case "$cur_" in
1922         --*=)
1923                 ;;
1924         *:*)
1925                 __git_complete_refs --cur="${cur_#*:}"
1926                 ;;
1927         *)
1928                 __git_complete_refs --cur="$cur_"
1929                 ;;
1930         esac
1931 }
1932
1933 _git_push ()
1934 {
1935         case "$prev" in
1936         --repo)
1937                 __gitcomp_nl "$(__git_remotes)"
1938                 return
1939                 ;;
1940         --recurse-submodules)
1941                 __gitcomp "$__git_push_recurse_submodules"
1942                 return
1943                 ;;
1944         esac
1945         case "$cur" in
1946         --repo=*)
1947                 __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
1948                 return
1949                 ;;
1950         --recurse-submodules=*)
1951                 __gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
1952                 return
1953                 ;;
1954         --force-with-lease=*)
1955                 __git_complete_force_with_lease "${cur##--force-with-lease=}"
1956                 return
1957                 ;;
1958         --*)
1959                 __gitcomp_builtin push
1960                 return
1961                 ;;
1962         esac
1963         __git_complete_remote_or_refspec
1964 }
1965
1966 _git_rebase ()
1967 {
1968         __git_find_repo_path
1969         if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
1970                 __gitcomp "--continue --skip --abort --quit --edit-todo --show-current-patch"
1971                 return
1972         elif [ -d "$__git_repo_path"/rebase-apply ] || \
1973              [ -d "$__git_repo_path"/rebase-merge ]; then
1974                 __gitcomp "--continue --skip --abort --quit --show-current-patch"
1975                 return
1976         fi
1977         __git_complete_strategy && return
1978         case "$cur" in
1979         --whitespace=*)
1980                 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1981                 return
1982                 ;;
1983         --*)
1984                 __gitcomp "
1985                         --onto --merge --strategy --interactive
1986                         --rebase-merges --preserve-merges --stat --no-stat
1987                         --committer-date-is-author-date --ignore-date
1988                         --ignore-whitespace --whitespace=
1989                         --autosquash --no-autosquash
1990                         --fork-point --no-fork-point
1991                         --autostash --no-autostash
1992                         --verify --no-verify
1993                         --keep-empty --root --force-rebase --no-ff
1994                         --rerere-autoupdate
1995                         --exec
1996                         "
1997
1998                 return
1999         esac
2000         __git_complete_refs
2001 }
2002
2003 _git_reflog ()
2004 {
2005         local subcommands="show delete expire"
2006         local subcommand="$(__git_find_on_cmdline "$subcommands")"
2007
2008         if [ -z "$subcommand" ]; then
2009                 __gitcomp "$subcommands"
2010         else
2011                 __git_complete_refs
2012         fi
2013 }
2014
2015 __git_send_email_confirm_options="always never auto cc compose"
2016 __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
2017
2018 _git_send_email ()
2019 {
2020         case "$prev" in
2021         --to|--cc|--bcc|--from)
2022                 __gitcomp "$(__git send-email --dump-aliases)"
2023                 return
2024                 ;;
2025         esac
2026
2027         case "$cur" in
2028         --confirm=*)
2029                 __gitcomp "
2030                         $__git_send_email_confirm_options
2031                         " "" "${cur##--confirm=}"
2032                 return
2033                 ;;
2034         --suppress-cc=*)
2035                 __gitcomp "
2036                         $__git_send_email_suppresscc_options
2037                         " "" "${cur##--suppress-cc=}"
2038
2039                 return
2040                 ;;
2041         --smtp-encryption=*)
2042                 __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
2043                 return
2044                 ;;
2045         --thread=*)
2046                 __gitcomp "
2047                         deep shallow
2048                         " "" "${cur##--thread=}"
2049                 return
2050                 ;;
2051         --to=*|--cc=*|--bcc=*|--from=*)
2052                 __gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
2053                 return
2054                 ;;
2055         --*)
2056                 __gitcomp "--annotate --bcc --cc --cc-cmd --chain-reply-to
2057                         --compose --confirm= --dry-run --envelope-sender
2058                         --from --identity
2059                         --in-reply-to --no-chain-reply-to --no-signed-off-by-cc
2060                         --no-suppress-from --no-thread --quiet --reply-to
2061                         --signed-off-by-cc --smtp-pass --smtp-server
2062                         --smtp-server-port --smtp-encryption= --smtp-user
2063                         --subject --suppress-cc= --suppress-from --thread --to
2064                         --validate --no-validate
2065                         $__git_format_patch_options"
2066                 return
2067                 ;;
2068         esac
2069         __git_complete_revlist
2070 }
2071
2072 _git_stage ()
2073 {
2074         _git_add
2075 }
2076
2077 _git_status ()
2078 {
2079         local complete_opt
2080         local untracked_state
2081
2082         case "$cur" in
2083         --ignore-submodules=*)
2084                 __gitcomp "none untracked dirty all" "" "${cur##--ignore-submodules=}"
2085                 return
2086                 ;;
2087         --untracked-files=*)
2088                 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
2089                 return
2090                 ;;
2091         --column=*)
2092                 __gitcomp "
2093                         always never auto column row plain dense nodense
2094                         " "" "${cur##--column=}"
2095                 return
2096                 ;;
2097         --*)
2098                 __gitcomp_builtin status "--no-column"
2099                 return
2100                 ;;
2101         esac
2102
2103         untracked_state="$(__git_get_option_value "-u" "--untracked-files=" \
2104                 "$__git_untracked_file_modes" "status.showUntrackedFiles")"
2105
2106         case "$untracked_state" in
2107         no)
2108                 # --ignored option does not matter
2109                 complete_opt=
2110                 ;;
2111         all|normal|*)
2112                 complete_opt="--cached --directory --no-empty-directory --others"
2113
2114                 if [ -n "$(__git_find_on_cmdline "--ignored")" ]; then
2115                         complete_opt="$complete_opt --ignored --exclude=*"
2116                 fi
2117                 ;;
2118         esac
2119
2120         __git_complete_index_file "$complete_opt"
2121 }
2122
2123 __git_config_get_set_variables ()
2124 {
2125         local prevword word config_file= c=$cword
2126         while [ $c -gt 1 ]; do
2127                 word="${words[c]}"
2128                 case "$word" in
2129                 --system|--global|--local|--file=*)
2130                         config_file="$word"
2131                         break
2132                         ;;
2133                 -f|--file)
2134                         config_file="$word $prevword"
2135                         break
2136                         ;;
2137                 esac
2138                 prevword=$word
2139                 c=$((--c))
2140         done
2141
2142         __git config $config_file --name-only --list
2143 }
2144
2145 _git_config ()
2146 {
2147         case "$prev" in
2148         branch.*.remote|branch.*.pushremote)
2149                 __gitcomp_nl "$(__git_remotes)"
2150                 return
2151                 ;;
2152         branch.*.merge)
2153                 __git_complete_refs
2154                 return
2155                 ;;
2156         branch.*.rebase)
2157                 __gitcomp "false true merges preserve interactive"
2158                 return
2159                 ;;
2160         remote.pushdefault)
2161                 __gitcomp_nl "$(__git_remotes)"
2162                 return
2163                 ;;
2164         remote.*.fetch)
2165                 local remote="${prev#remote.}"
2166                 remote="${remote%.fetch}"
2167                 if [ -z "$cur" ]; then
2168                         __gitcomp_nl "refs/heads/" "" "" ""
2169                         return
2170                 fi
2171                 __gitcomp_nl "$(__git_refs_remotes "$remote")"
2172                 return
2173                 ;;
2174         remote.*.push)
2175                 local remote="${prev#remote.}"
2176                 remote="${remote%.push}"
2177                 __gitcomp_nl "$(__git for-each-ref \
2178                         --format='%(refname):%(refname)' refs/heads)"
2179                 return
2180                 ;;
2181         pull.twohead|pull.octopus)
2182                 __git_compute_merge_strategies
2183                 __gitcomp "$__git_merge_strategies"
2184                 return
2185                 ;;
2186         color.branch|color.diff|color.interactive|\
2187         color.showbranch|color.status|color.ui)
2188                 __gitcomp "always never auto"
2189                 return
2190                 ;;
2191         color.pager)
2192                 __gitcomp "false true"
2193                 return
2194                 ;;
2195         color.*.*)
2196                 __gitcomp "
2197                         normal black red green yellow blue magenta cyan white
2198                         bold dim ul blink reverse
2199                         "
2200                 return
2201                 ;;
2202         diff.submodule)
2203                 __gitcomp "log short"
2204                 return
2205                 ;;
2206         help.format)
2207                 __gitcomp "man info web html"
2208                 return
2209                 ;;
2210         log.date)
2211                 __gitcomp "$__git_log_date_formats"
2212                 return
2213                 ;;
2214         sendemail.aliasfiletype)
2215                 __gitcomp "mutt mailrc pine elm gnus"
2216                 return
2217                 ;;
2218         sendemail.confirm)
2219                 __gitcomp "$__git_send_email_confirm_options"
2220                 return
2221                 ;;
2222         sendemail.suppresscc)
2223                 __gitcomp "$__git_send_email_suppresscc_options"
2224                 return
2225                 ;;
2226         sendemail.transferencoding)
2227                 __gitcomp "7bit 8bit quoted-printable base64"
2228                 return
2229                 ;;
2230         --get|--get-all|--unset|--unset-all)
2231                 __gitcomp_nl "$(__git_config_get_set_variables)"
2232                 return
2233                 ;;
2234         *.*)
2235                 return
2236                 ;;
2237         esac
2238         case "$cur" in
2239         --*)
2240                 __gitcomp_builtin config
2241                 return
2242                 ;;
2243         branch.*.*)
2244                 local pfx="${cur%.*}." cur_="${cur##*.}"
2245                 __gitcomp "remote pushremote merge mergeoptions rebase" "$pfx" "$cur_"
2246                 return
2247                 ;;
2248         branch.*)
2249                 local pfx="${cur%.*}." cur_="${cur#*.}"
2250                 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
2251                 __gitcomp_nl_append $'autosetupmerge\nautosetuprebase\n' "$pfx" "$cur_"
2252                 return
2253                 ;;
2254         guitool.*.*)
2255                 local pfx="${cur%.*}." cur_="${cur##*.}"
2256                 __gitcomp "
2257                         argprompt cmd confirm needsfile noconsole norescan
2258                         prompt revprompt revunmerged title
2259                         " "$pfx" "$cur_"
2260                 return
2261                 ;;
2262         difftool.*.*)
2263                 local pfx="${cur%.*}." cur_="${cur##*.}"
2264                 __gitcomp "cmd path" "$pfx" "$cur_"
2265                 return
2266                 ;;
2267         man.*.*)
2268                 local pfx="${cur%.*}." cur_="${cur##*.}"
2269                 __gitcomp "cmd path" "$pfx" "$cur_"
2270                 return
2271                 ;;
2272         mergetool.*.*)
2273                 local pfx="${cur%.*}." cur_="${cur##*.}"
2274                 __gitcomp "cmd path trustExitCode" "$pfx" "$cur_"
2275                 return
2276                 ;;
2277         pager.*)
2278                 local pfx="${cur%.*}." cur_="${cur#*.}"
2279                 __git_compute_all_commands
2280                 __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_"
2281                 return
2282                 ;;
2283         remote.*.*)
2284                 local pfx="${cur%.*}." cur_="${cur##*.}"
2285                 __gitcomp "
2286                         url proxy fetch push mirror skipDefaultUpdate
2287                         receivepack uploadpack tagopt pushurl
2288                         " "$pfx" "$cur_"
2289                 return
2290                 ;;
2291         remote.*)
2292                 local pfx="${cur%.*}." cur_="${cur#*.}"
2293                 __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
2294                 __gitcomp_nl_append "pushdefault" "$pfx" "$cur_"
2295                 return
2296                 ;;
2297         url.*.*)
2298                 local pfx="${cur%.*}." cur_="${cur##*.}"
2299                 __gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_"
2300                 return
2301                 ;;
2302         esac
2303         __gitcomp "
2304                 add.ignoreErrors
2305                 advice.amWorkDir
2306                 advice.commitBeforeMerge
2307                 advice.detachedHead
2308                 advice.implicitIdentity
2309                 advice.pushAlreadyExists
2310                 advice.pushFetchFirst
2311                 advice.pushNeedsForce
2312                 advice.pushNonFFCurrent
2313                 advice.pushNonFFMatching
2314                 advice.pushUpdateRejected
2315                 advice.resolveConflict
2316                 advice.rmHints
2317                 advice.statusHints
2318                 advice.statusUoption
2319                 advice.ignoredHook
2320                 alias.
2321                 am.keepcr
2322                 am.threeWay
2323                 apply.ignorewhitespace
2324                 apply.whitespace
2325                 branch.autosetupmerge
2326                 branch.autosetuprebase
2327                 browser.
2328                 clean.requireForce
2329                 color.branch
2330                 color.branch.current
2331                 color.branch.local
2332                 color.branch.plain
2333                 color.branch.remote
2334                 color.decorate.HEAD
2335                 color.decorate.branch
2336                 color.decorate.remoteBranch
2337                 color.decorate.stash
2338                 color.decorate.tag
2339                 color.diff
2340                 color.diff.commit
2341                 color.diff.frag
2342                 color.diff.func
2343                 color.diff.meta
2344                 color.diff.new
2345                 color.diff.old
2346                 color.diff.plain
2347                 color.diff.whitespace
2348                 color.grep
2349                 color.grep.context
2350                 color.grep.filename
2351                 color.grep.function
2352                 color.grep.linenumber
2353                 color.grep.match
2354                 color.grep.selected
2355                 color.grep.separator
2356                 color.interactive
2357                 color.interactive.error
2358                 color.interactive.header
2359                 color.interactive.help
2360                 color.interactive.prompt
2361                 color.pager
2362                 color.showbranch
2363                 color.status
2364                 color.status.added
2365                 color.status.changed
2366                 color.status.header
2367                 color.status.localBranch
2368                 color.status.nobranch
2369                 color.status.remoteBranch
2370                 color.status.unmerged
2371                 color.status.untracked
2372                 color.status.updated
2373                 color.ui
2374                 commit.cleanup
2375                 commit.gpgSign
2376                 commit.status
2377                 commit.template
2378                 commit.verbose
2379                 core.abbrev
2380                 core.askpass
2381                 core.attributesfile
2382                 core.autocrlf
2383                 core.bare
2384                 core.bigFileThreshold
2385                 core.checkStat
2386                 core.commentChar
2387                 core.commitGraph
2388                 core.compression
2389                 core.createObject
2390                 core.deltaBaseCacheLimit
2391                 core.editor
2392                 core.eol
2393                 core.excludesfile
2394                 core.fileMode
2395                 core.fsyncobjectfiles
2396                 core.gitProxy
2397                 core.hideDotFiles
2398                 core.hooksPath
2399                 core.ignoreStat
2400                 core.ignorecase
2401                 core.logAllRefUpdates
2402                 core.loosecompression
2403                 core.notesRef
2404                 core.packedGitLimit
2405                 core.packedGitWindowSize
2406                 core.packedRefsTimeout
2407                 core.pager
2408                 core.precomposeUnicode
2409                 core.preferSymlinkRefs
2410                 core.preloadindex
2411                 core.protectHFS
2412                 core.protectNTFS
2413                 core.quotepath
2414                 core.repositoryFormatVersion
2415                 core.safecrlf
2416                 core.sharedRepository
2417                 core.sparseCheckout
2418                 core.splitIndex
2419                 core.sshCommand
2420                 core.symlinks
2421                 core.trustctime
2422                 core.untrackedCache
2423                 core.warnAmbiguousRefs
2424                 core.whitespace
2425                 core.worktree
2426                 credential.helper
2427                 credential.useHttpPath
2428                 credential.username
2429                 credentialCache.ignoreSIGHUP
2430                 diff.autorefreshindex
2431                 diff.external
2432                 diff.ignoreSubmodules
2433                 diff.mnemonicprefix
2434                 diff.noprefix
2435                 diff.renameLimit
2436                 diff.renames
2437                 diff.statGraphWidth
2438                 diff.submodule
2439                 diff.suppressBlankEmpty
2440                 diff.tool
2441                 diff.wordRegex
2442                 diff.algorithm
2443                 difftool.
2444                 difftool.prompt
2445                 fetch.recurseSubmodules
2446                 fetch.unpackLimit
2447                 format.attach
2448                 format.cc
2449                 format.coverLetter
2450                 format.from
2451                 format.headers
2452                 format.numbered
2453                 format.pretty
2454                 format.signature
2455                 format.signoff
2456                 format.subjectprefix
2457                 format.suffix
2458                 format.thread
2459                 format.to
2460                 gc.
2461                 gc.aggressiveDepth
2462                 gc.aggressiveWindow
2463                 gc.auto
2464                 gc.autoDetach
2465                 gc.autopacklimit
2466                 gc.logExpiry
2467                 gc.packrefs
2468                 gc.pruneexpire
2469                 gc.reflogexpire
2470                 gc.reflogexpireunreachable
2471                 gc.rerereresolved
2472                 gc.rerereunresolved
2473                 gc.worktreePruneExpire
2474                 gitcvs.allbinary
2475                 gitcvs.commitmsgannotation
2476                 gitcvs.dbTableNamePrefix
2477                 gitcvs.dbdriver
2478                 gitcvs.dbname
2479                 gitcvs.dbpass
2480                 gitcvs.dbuser
2481                 gitcvs.enabled
2482                 gitcvs.logfile
2483                 gitcvs.usecrlfattr
2484                 guitool.
2485                 gui.blamehistoryctx
2486                 gui.commitmsgwidth
2487                 gui.copyblamethreshold
2488                 gui.diffcontext
2489                 gui.encoding
2490                 gui.fastcopyblame
2491                 gui.matchtrackingbranch
2492                 gui.newbranchtemplate
2493                 gui.pruneduringfetch
2494                 gui.spellingdictionary
2495                 gui.trustmtime
2496                 help.autocorrect
2497                 help.browser
2498                 help.format
2499                 http.lowSpeedLimit
2500                 http.lowSpeedTime
2501                 http.maxRequests
2502                 http.minSessions
2503                 http.noEPSV
2504                 http.postBuffer
2505                 http.proxy
2506                 http.sslCipherList
2507                 http.sslVersion
2508                 http.sslCAInfo
2509                 http.sslCAPath
2510                 http.sslCert
2511                 http.sslCertPasswordProtected
2512                 http.sslKey
2513                 http.sslVerify
2514                 http.useragent
2515                 i18n.commitEncoding
2516                 i18n.logOutputEncoding
2517                 imap.authMethod
2518                 imap.folder
2519                 imap.host
2520                 imap.pass
2521                 imap.port
2522                 imap.preformattedHTML
2523                 imap.sslverify
2524                 imap.tunnel
2525                 imap.user
2526                 init.templatedir
2527                 instaweb.browser
2528                 instaweb.httpd
2529                 instaweb.local
2530                 instaweb.modulepath
2531                 instaweb.port
2532                 interactive.singlekey
2533                 log.date
2534                 log.decorate
2535                 log.showroot
2536                 mailmap.file
2537                 man.
2538                 man.viewer
2539                 merge.
2540                 merge.conflictstyle
2541                 merge.log
2542                 merge.renameLimit
2543                 merge.renormalize
2544                 merge.stat
2545                 merge.tool
2546                 merge.verbosity
2547                 mergetool.
2548                 mergetool.keepBackup
2549                 mergetool.keepTemporaries
2550                 mergetool.prompt
2551                 notes.displayRef
2552                 notes.rewrite.
2553                 notes.rewrite.amend
2554                 notes.rewrite.rebase
2555                 notes.rewriteMode
2556                 notes.rewriteRef
2557                 pack.compression
2558                 pack.deltaCacheLimit
2559                 pack.deltaCacheSize
2560                 pack.depth
2561                 pack.indexVersion
2562                 pack.packSizeLimit
2563                 pack.threads
2564                 pack.window
2565                 pack.windowMemory
2566                 pager.
2567                 pretty.
2568                 pull.octopus
2569                 pull.twohead
2570                 push.default
2571                 push.followTags
2572                 rebase.autosquash
2573                 rebase.stat
2574                 receive.autogc
2575                 receive.denyCurrentBranch
2576                 receive.denyDeleteCurrent
2577                 receive.denyDeletes
2578                 receive.denyNonFastForwards
2579                 receive.fsckObjects
2580                 receive.unpackLimit
2581                 receive.updateserverinfo
2582                 remote.pushdefault
2583                 remotes.
2584                 repack.usedeltabaseoffset
2585                 rerere.autoupdate
2586                 rerere.enabled
2587                 sendemail.
2588                 sendemail.aliasesfile
2589                 sendemail.aliasfiletype
2590                 sendemail.bcc
2591                 sendemail.cc
2592                 sendemail.cccmd
2593                 sendemail.chainreplyto
2594                 sendemail.confirm
2595                 sendemail.envelopesender
2596                 sendemail.from
2597                 sendemail.identity
2598                 sendemail.multiedit
2599                 sendemail.signedoffbycc
2600                 sendemail.smtpdomain
2601                 sendemail.smtpencryption
2602                 sendemail.smtppass
2603                 sendemail.smtpserver
2604                 sendemail.smtpserveroption
2605                 sendemail.smtpserverport
2606                 sendemail.smtpuser
2607                 sendemail.suppresscc
2608                 sendemail.suppressfrom
2609                 sendemail.thread
2610                 sendemail.to
2611                 sendemail.tocmd
2612                 sendemail.validate
2613                 sendemail.smtpbatchsize
2614                 sendemail.smtprelogindelay
2615                 showbranch.default
2616                 status.relativePaths
2617                 status.showUntrackedFiles
2618                 status.submodulesummary
2619                 submodule.
2620                 tar.umask
2621                 transfer.unpackLimit
2622                 url.
2623                 user.email
2624                 user.name
2625                 user.signingkey
2626                 web.browser
2627                 branch. remote.
2628         "
2629 }
2630
2631 _git_remote ()
2632 {
2633         local subcommands="
2634                 add rename remove set-head set-branches
2635                 get-url set-url show prune update
2636                 "
2637         local subcommand="$(__git_find_on_cmdline "$subcommands")"
2638         if [ -z "$subcommand" ]; then
2639                 case "$cur" in
2640                 --*)
2641                         __gitcomp_builtin remote
2642                         ;;
2643                 *)
2644                         __gitcomp "$subcommands"
2645                         ;;
2646                 esac
2647                 return
2648         fi
2649
2650         case "$subcommand,$cur" in
2651         add,--*)
2652                 __gitcomp_builtin remote_add "--no-tags"
2653                 ;;
2654         add,*)
2655                 ;;
2656         set-head,--*)
2657                 __gitcomp_builtin remote_set-head
2658                 ;;
2659         set-branches,--*)
2660                 __gitcomp_builtin remote_set-branches
2661                 ;;
2662         set-head,*|set-branches,*)
2663                 __git_complete_remote_or_refspec
2664                 ;;
2665         update,--*)
2666                 __gitcomp_builtin remote_update
2667                 ;;
2668         update,*)
2669                 __gitcomp "$(__git_get_config_variables "remotes")"
2670                 ;;
2671         set-url,--*)
2672                 __gitcomp_builtin remote_set-url
2673                 ;;
2674         get-url,--*)
2675                 __gitcomp_builtin remote_get-url
2676                 ;;
2677         prune,--*)
2678                 __gitcomp_builtin remote_prune
2679                 ;;
2680         *)
2681                 __gitcomp_nl "$(__git_remotes)"
2682                 ;;
2683         esac
2684 }
2685
2686 _git_replace ()
2687 {
2688         case "$cur" in
2689         --*)
2690                 __gitcomp_builtin replace
2691                 return
2692                 ;;
2693         esac
2694         __git_complete_refs
2695 }
2696
2697 _git_rerere ()
2698 {
2699         local subcommands="clear forget diff remaining status gc"
2700         local subcommand="$(__git_find_on_cmdline "$subcommands")"
2701         if test -z "$subcommand"
2702         then
2703                 __gitcomp "$subcommands"
2704                 return
2705         fi
2706 }
2707
2708 _git_reset ()
2709 {
2710         __git_has_doubledash && return
2711
2712         case "$cur" in
2713         --*)
2714                 __gitcomp_builtin reset
2715                 return
2716                 ;;
2717         esac
2718         __git_complete_refs
2719 }
2720
2721 __git_revert_inprogress_options="--continue --quit --abort"
2722
2723 _git_revert ()
2724 {
2725         __git_find_repo_path
2726         if [ -f "$__git_repo_path"/REVERT_HEAD ]; then
2727                 __gitcomp "$__git_revert_inprogress_options"
2728                 return
2729         fi
2730         case "$cur" in
2731         --*)
2732                 __gitcomp_builtin revert "--no-edit" \
2733                         "$__git_revert_inprogress_options"
2734                 return
2735                 ;;
2736         esac
2737         __git_complete_refs
2738 }
2739
2740 _git_rm ()
2741 {
2742         case "$cur" in
2743         --*)
2744                 __gitcomp_builtin rm
2745                 return
2746                 ;;
2747         esac
2748
2749         __git_complete_index_file "--cached"
2750 }
2751
2752 _git_shortlog ()
2753 {
2754         __git_has_doubledash && return
2755
2756         case "$cur" in
2757         --*)
2758                 __gitcomp "
2759                         $__git_log_common_options
2760                         $__git_log_shortlog_options
2761                         --numbered --summary --email
2762                         "
2763                 return
2764                 ;;
2765         esac
2766         __git_complete_revlist
2767 }
2768
2769 _git_show ()
2770 {
2771         __git_has_doubledash && return
2772
2773         case "$cur" in
2774         --pretty=*|--format=*)
2775                 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2776                         " "" "${cur#*=}"
2777                 return
2778                 ;;
2779         --diff-algorithm=*)
2780                 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
2781                 return
2782                 ;;
2783         --submodule=*)
2784                 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
2785                 return
2786                 ;;
2787         --*)
2788                 __gitcomp "--pretty= --format= --abbrev-commit --oneline
2789                         --show-signature
2790                         $__git_diff_common_options
2791                         "
2792                 return
2793                 ;;
2794         esac
2795         __git_complete_revlist_file
2796 }
2797
2798 _git_show_branch ()
2799 {
2800         case "$cur" in
2801         --*)
2802                 __gitcomp_builtin show-branch "--no-color"
2803                 return
2804                 ;;
2805         esac
2806         __git_complete_revlist
2807 }
2808
2809 _git_stash ()
2810 {
2811         local save_opts='--all --keep-index --no-keep-index --quiet --patch --include-untracked'
2812         local subcommands='push list show apply clear drop pop create branch'
2813         local subcommand="$(__git_find_on_cmdline "$subcommands save")"
2814         if [ -n "$(__git_find_on_cmdline "-p")" ]; then
2815                 subcommand="push"
2816         fi
2817         if [ -z "$subcommand" ]; then
2818                 case "$cur" in
2819                 --*)
2820                         __gitcomp "$save_opts"
2821                         ;;
2822                 sa*)
2823                         if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
2824                                 __gitcomp "save"
2825                         fi
2826                         ;;
2827                 *)
2828                         if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
2829                                 __gitcomp "$subcommands"
2830                         fi
2831                         ;;
2832                 esac
2833         else
2834                 case "$subcommand,$cur" in
2835                 push,--*)
2836                         __gitcomp "$save_opts --message"
2837                         ;;
2838                 save,--*)
2839                         __gitcomp "$save_opts"
2840                         ;;
2841                 apply,--*|pop,--*)
2842                         __gitcomp "--index --quiet"
2843                         ;;
2844                 drop,--*)
2845                         __gitcomp "--quiet"
2846                         ;;
2847                 show,--*|branch,--*)
2848                         ;;
2849                 branch,*)
2850                         if [ $cword -eq 3 ]; then
2851                                 __git_complete_refs
2852                         else
2853                                 __gitcomp_nl "$(__git stash list \
2854                                                 | sed -n -e 's/:.*//p')"
2855                         fi
2856                         ;;
2857                 show,*|apply,*|drop,*|pop,*)
2858                         __gitcomp_nl "$(__git stash list \
2859                                         | sed -n -e 's/:.*//p')"
2860                         ;;
2861                 *)
2862                         ;;
2863                 esac
2864         fi
2865 }
2866
2867 _git_submodule ()
2868 {
2869         __git_has_doubledash && return
2870
2871         local subcommands="add status init deinit update summary foreach sync"
2872         local subcommand="$(__git_find_on_cmdline "$subcommands")"
2873         if [ -z "$subcommand" ]; then
2874                 case "$cur" in
2875                 --*)
2876                         __gitcomp "--quiet"
2877                         ;;
2878                 *)
2879                         __gitcomp "$subcommands"
2880                         ;;
2881                 esac
2882                 return
2883         fi
2884
2885         case "$subcommand,$cur" in
2886         add,--*)
2887                 __gitcomp "--branch --force --name --reference --depth"
2888                 ;;
2889         status,--*)
2890                 __gitcomp "--cached --recursive"
2891                 ;;
2892         deinit,--*)
2893                 __gitcomp "--force --all"
2894                 ;;
2895         update,--*)
2896                 __gitcomp "
2897                         --init --remote --no-fetch
2898                         --recommend-shallow --no-recommend-shallow
2899                         --force --rebase --merge --reference --depth --recursive --jobs
2900                 "
2901                 ;;
2902         summary,--*)
2903                 __gitcomp "--cached --files --summary-limit"
2904                 ;;
2905         foreach,--*|sync,--*)
2906                 __gitcomp "--recursive"
2907                 ;;
2908         *)
2909                 ;;
2910         esac
2911 }
2912
2913 _git_svn ()
2914 {
2915         local subcommands="
2916                 init fetch clone rebase dcommit log find-rev
2917                 set-tree commit-diff info create-ignore propget
2918                 proplist show-ignore show-externals branch tag blame
2919                 migrate mkdirs reset gc
2920                 "
2921         local subcommand="$(__git_find_on_cmdline "$subcommands")"
2922         if [ -z "$subcommand" ]; then
2923                 __gitcomp "$subcommands"
2924         else
2925                 local remote_opts="--username= --config-dir= --no-auth-cache"
2926                 local fc_opts="
2927                         --follow-parent --authors-file= --repack=
2928                         --no-metadata --use-svm-props --use-svnsync-props
2929                         --log-window-size= --no-checkout --quiet
2930                         --repack-flags --use-log-author --localtime
2931                         --add-author-from
2932                         --ignore-paths= --include-paths= $remote_opts
2933                         "
2934                 local init_opts="
2935                         --template= --shared= --trunk= --tags=
2936                         --branches= --stdlayout --minimize-url
2937                         --no-metadata --use-svm-props --use-svnsync-props
2938                         --rewrite-root= --prefix= $remote_opts
2939                         "
2940                 local cmt_opts="
2941                         --edit --rmdir --find-copies-harder --copy-similarity=
2942                         "
2943
2944                 case "$subcommand,$cur" in
2945                 fetch,--*)
2946                         __gitcomp "--revision= --fetch-all $fc_opts"
2947                         ;;
2948                 clone,--*)
2949                         __gitcomp "--revision= $fc_opts $init_opts"
2950                         ;;
2951                 init,--*)
2952                         __gitcomp "$init_opts"
2953                         ;;
2954                 dcommit,--*)
2955                         __gitcomp "
2956                                 --merge --strategy= --verbose --dry-run
2957                                 --fetch-all --no-rebase --commit-url
2958                                 --revision --interactive $cmt_opts $fc_opts
2959                                 "
2960                         ;;
2961                 set-tree,--*)
2962                         __gitcomp "--stdin $cmt_opts $fc_opts"
2963                         ;;
2964                 create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
2965                 show-externals,--*|mkdirs,--*)
2966                         __gitcomp "--revision="
2967                         ;;
2968                 log,--*)
2969                         __gitcomp "
2970                                 --limit= --revision= --verbose --incremental
2971                                 --oneline --show-commit --non-recursive
2972                                 --authors-file= --color
2973                                 "
2974                         ;;
2975                 rebase,--*)
2976                         __gitcomp "
2977                                 --merge --verbose --strategy= --local
2978                                 --fetch-all --dry-run $fc_opts
2979                                 "
2980                         ;;
2981                 commit-diff,--*)
2982                         __gitcomp "--message= --file= --revision= $cmt_opts"
2983                         ;;
2984                 info,--*)
2985                         __gitcomp "--url"
2986                         ;;
2987                 branch,--*)
2988                         __gitcomp "--dry-run --message --tag"
2989                         ;;
2990                 tag,--*)
2991                         __gitcomp "--dry-run --message"
2992                         ;;
2993                 blame,--*)
2994                         __gitcomp "--git-format"
2995                         ;;
2996                 migrate,--*)
2997                         __gitcomp "
2998                                 --config-dir= --ignore-paths= --minimize
2999                                 --no-auth-cache --username=
3000                                 "
3001                         ;;
3002                 reset,--*)
3003                         __gitcomp "--revision= --parent"
3004                         ;;
3005                 *)
3006                         ;;
3007                 esac
3008         fi
3009 }
3010
3011 _git_tag ()
3012 {
3013         local i c=1 f=0
3014         while [ $c -lt $cword ]; do
3015                 i="${words[c]}"
3016                 case "$i" in
3017                 -d|--delete|-v|--verify)
3018                         __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3019                         return
3020                         ;;
3021                 -f)
3022                         f=1
3023                         ;;
3024                 esac
3025                 ((c++))
3026         done
3027
3028         case "$prev" in
3029         -m|-F)
3030                 ;;
3031         -*|tag)
3032                 if [ $f = 1 ]; then
3033                         __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3034                 fi
3035                 ;;
3036         *)
3037                 __git_complete_refs
3038                 ;;
3039         esac
3040
3041         case "$cur" in
3042         --*)
3043                 __gitcomp_builtin tag
3044                 ;;
3045         esac
3046 }
3047
3048 _git_whatchanged ()
3049 {
3050         _git_log
3051 }
3052
3053 _git_worktree ()
3054 {
3055         local subcommands="add list lock move prune remove unlock"
3056         local subcommand="$(__git_find_on_cmdline "$subcommands")"
3057         if [ -z "$subcommand" ]; then
3058                 __gitcomp "$subcommands"
3059         else
3060                 case "$subcommand,$cur" in
3061                 add,--*)
3062                         __gitcomp_builtin worktree_add
3063                         ;;
3064                 list,--*)
3065                         __gitcomp_builtin worktree_list
3066                         ;;
3067                 lock,--*)
3068                         __gitcomp_builtin worktree_lock
3069                         ;;
3070                 prune,--*)
3071                         __gitcomp_builtin worktree_prune
3072                         ;;
3073                 remove,--*)
3074                         __gitcomp "--force"
3075                         ;;
3076                 *)
3077                         ;;
3078                 esac
3079         fi
3080 }
3081
3082 __git_complete_common () {
3083         local command="$1"
3084
3085         case "$cur" in
3086         --*)
3087                 __gitcomp_builtin "$command"
3088                 ;;
3089         esac
3090 }
3091
3092 __git_cmds_with_parseopt_helper=
3093 __git_support_parseopt_helper () {
3094         test -n "$__git_cmds_with_parseopt_helper" ||
3095                 __git_cmds_with_parseopt_helper="$(__git --list-cmds=parseopt)"
3096
3097         case " $__git_cmds_with_parseopt_helper " in
3098         *" $1 "*)
3099                 return 0
3100                 ;;
3101         *)
3102                 return 1
3103                 ;;
3104         esac
3105 }
3106
3107 __git_complete_command () {
3108         local command="$1"
3109         local completion_func="_git_${command//-/_}"
3110         if ! declare -f $completion_func >/dev/null 2>/dev/null &&
3111                 declare -f _completion_loader >/dev/null 2>/dev/null
3112         then
3113                 _completion_loader "git-$command"
3114         fi
3115         if declare -f $completion_func >/dev/null 2>/dev/null
3116         then
3117                 $completion_func
3118                 return 0
3119         elif __git_support_parseopt_helper "$command"
3120         then
3121                 __git_complete_common "$command"
3122                 return 0
3123         else
3124                 return 1
3125         fi
3126 }
3127
3128 __git_main ()
3129 {
3130         local i c=1 command __git_dir __git_repo_path
3131         local __git_C_args C_args_count=0
3132
3133         while [ $c -lt $cword ]; do
3134                 i="${words[c]}"
3135                 case "$i" in
3136                 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
3137                 --git-dir)   ((c++)) ; __git_dir="${words[c]}" ;;
3138                 --bare)      __git_dir="." ;;
3139                 --help) command="help"; break ;;
3140                 -c|--work-tree|--namespace) ((c++)) ;;
3141                 -C)     __git_C_args[C_args_count++]=-C
3142                         ((c++))
3143                         __git_C_args[C_args_count++]="${words[c]}"
3144                         ;;
3145                 -*) ;;
3146                 *) command="$i"; break ;;
3147                 esac
3148                 ((c++))
3149         done
3150
3151         if [ -z "$command" ]; then
3152                 case "$prev" in
3153                 --git-dir|-C|--work-tree)
3154                         # these need a path argument, let's fall back to
3155                         # Bash filename completion
3156                         return
3157                         ;;
3158                 -c|--namespace)
3159                         # we don't support completing these options' arguments
3160                         return
3161                         ;;
3162                 esac
3163                 case "$cur" in
3164                 --*)   __gitcomp "
3165                         --paginate
3166                         --no-pager
3167                         --git-dir=
3168                         --bare
3169                         --version
3170                         --exec-path
3171                         --exec-path=
3172                         --html-path
3173                         --man-path
3174                         --info-path
3175                         --work-tree=
3176                         --namespace=
3177                         --no-replace-objects
3178                         --help
3179                         "
3180                         ;;
3181                 *)
3182                         if test -n "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
3183                         then
3184                                 __gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
3185                         else
3186                                 __gitcomp "$(git --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config)"
3187                         fi
3188                         ;;
3189                 esac
3190                 return
3191         fi
3192
3193         __git_complete_command "$command" && return
3194
3195         local expansion=$(__git_aliased_command "$command")
3196         if [ -n "$expansion" ]; then
3197                 words[1]=$expansion
3198                 __git_complete_command "$expansion"
3199         fi
3200 }
3201
3202 __gitk_main ()
3203 {
3204         __git_has_doubledash && return
3205
3206         local __git_repo_path
3207         __git_find_repo_path
3208
3209         local merge=""
3210         if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
3211                 merge="--merge"
3212         fi
3213         case "$cur" in
3214         --*)
3215                 __gitcomp "
3216                         $__git_log_common_options
3217                         $__git_log_gitk_options
3218                         $merge
3219                         "
3220                 return
3221                 ;;
3222         esac
3223         __git_complete_revlist
3224 }
3225
3226 if [[ -n ${ZSH_VERSION-} ]] &&
3227    # Don't define these functions when sourced from 'git-completion.zsh',
3228    # it has its own implementations.
3229    [[ -z ${GIT_SOURCING_ZSH_COMPLETION-} ]]; then
3230         echo "WARNING: this script is deprecated, please see git-completion.zsh" 1>&2
3231
3232         autoload -U +X compinit && compinit
3233
3234         __gitcomp ()
3235         {
3236                 emulate -L zsh
3237
3238                 local cur_="${3-$cur}"
3239
3240                 case "$cur_" in
3241                 --*=)
3242                         ;;
3243                 *)
3244                         local c IFS=$' \t\n'
3245                         local -a array
3246                         for c in ${=1}; do
3247                                 c="$c${4-}"
3248                                 case $c in
3249                                 --*=*|*.) ;;
3250                                 *) c="$c " ;;
3251                                 esac
3252                                 array[${#array[@]}+1]="$c"
3253                         done
3254                         compset -P '*[=:]'
3255                         compadd -Q -S '' -p "${2-}" -a -- array && _ret=0
3256                         ;;
3257                 esac
3258         }
3259
3260         __gitcomp_direct ()
3261         {
3262                 emulate -L zsh
3263
3264                 local IFS=$'\n'
3265                 compset -P '*[=:]'
3266                 compadd -Q -- ${=1} && _ret=0
3267         }
3268
3269         __gitcomp_nl ()
3270         {
3271                 emulate -L zsh
3272
3273                 local IFS=$'\n'
3274                 compset -P '*[=:]'
3275                 compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
3276         }
3277
3278         __gitcomp_file_direct ()
3279         {
3280                 emulate -L zsh
3281
3282                 local IFS=$'\n'
3283                 compset -P '*[=:]'
3284                 compadd -Q -f -- ${=1} && _ret=0
3285         }
3286
3287         __gitcomp_file ()
3288         {
3289                 emulate -L zsh
3290
3291                 local IFS=$'\n'
3292                 compset -P '*[=:]'
3293                 compadd -Q -p "${2-}" -f -- ${=1} && _ret=0
3294         }
3295
3296         _git ()
3297         {
3298                 local _ret=1 cur cword prev
3299                 cur=${words[CURRENT]}
3300                 prev=${words[CURRENT-1]}
3301                 let cword=CURRENT-1
3302                 emulate ksh -c __${service}_main
3303                 let _ret && _default && _ret=0
3304                 return _ret
3305         }
3306
3307         compdef _git git gitk
3308         return
3309 fi
3310
3311 __git_func_wrap ()
3312 {
3313         local cur words cword prev
3314         _get_comp_words_by_ref -n =: cur words cword prev
3315         $1
3316 }
3317
3318 # Setup completion for certain functions defined above by setting common
3319 # variables and workarounds.
3320 # This is NOT a public function; use at your own risk.
3321 __git_complete ()
3322 {
3323         local wrapper="__git_wrap${2}"
3324         eval "$wrapper () { __git_func_wrap $2 ; }"
3325         complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
3326                 || complete -o default -o nospace -F $wrapper $1
3327 }
3328
3329 # wrapper for backwards compatibility
3330 _git ()
3331 {
3332         __git_wrap__git_main
3333 }
3334
3335 # wrapper for backwards compatibility
3336 _gitk ()
3337 {
3338         __git_wrap__gitk_main
3339 }
3340
3341 __git_complete git __git_main
3342 __git_complete gitk __gitk_main
3343
3344 # The following are necessary only for Cygwin, and only are needed
3345 # when the user has tab-completed the executable name and consequently
3346 # included the '.exe' suffix.
3347 #
3348 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
3349 __git_complete git.exe __git_main
3350 fi