Imported Upstream version 2.19.2
[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         --no-*)
334                 local c i=0 IFS=$' \t\n'
335                 for c in $1; do
336                         if [[ $c == "--" ]]; then
337                                 continue
338                         fi
339                         c="$c${4-}"
340                         if [[ $c == "$cur_"* ]]; then
341                                 case $c in
342                                 --*=*|*.) ;;
343                                 *) c="$c " ;;
344                                 esac
345                                 COMPREPLY[i++]="${2-}$c"
346                         fi
347                 done
348                 ;;
349         *)
350                 local c i=0 IFS=$' \t\n'
351                 for c in $1; do
352                         if [[ $c == "--" ]]; then
353                                 c="--no-...${4-}"
354                                 if [[ $c == "$cur_"* ]]; then
355                                         COMPREPLY[i++]="${2-}$c "
356                                 fi
357                                 break
358                         fi
359                         c="$c${4-}"
360                         if [[ $c == "$cur_"* ]]; then
361                                 case $c in
362                                 --*=*|*.) ;;
363                                 *) c="$c " ;;
364                                 esac
365                                 COMPREPLY[i++]="${2-}$c"
366                         fi
367                 done
368                 ;;
369         esac
370 }
371
372 # Clear the variables caching builtins' options when (re-)sourcing
373 # the completion script.
374 if [[ -n ${ZSH_VERSION-} ]]; then
375         unset $(set |sed -ne 's/^\(__gitcomp_builtin_[a-zA-Z0-9_][a-zA-Z0-9_]*\)=.*/\1/p') 2>/dev/null
376 else
377         unset $(compgen -v __gitcomp_builtin_)
378 fi
379
380 # This function is equivalent to
381 #
382 #    __gitcomp "$(git xxx --git-completion-helper) ..."
383 #
384 # except that the output is cached. Accept 1-3 arguments:
385 # 1: the git command to execute, this is also the cache key
386 # 2: extra options to be added on top (e.g. negative forms)
387 # 3: options to be excluded
388 __gitcomp_builtin ()
389 {
390         # spaces must be replaced with underscore for multi-word
391         # commands, e.g. "git remote add" becomes remote_add.
392         local cmd="$1"
393         local incl="$2"
394         local excl="$3"
395
396         local var=__gitcomp_builtin_"${cmd/-/_}"
397         local options
398         eval "options=\$$var"
399
400         if [ -z "$options" ]; then
401                 # leading and trailing spaces are significant to make
402                 # option removal work correctly.
403                 options=" $(__git ${cmd/_/ } --git-completion-helper) $incl "
404                 for i in $excl; do
405                         options="${options/ $i / }"
406                 done
407                 eval "$var=\"$options\""
408         fi
409
410         __gitcomp "$options"
411 }
412
413 # Variation of __gitcomp_nl () that appends to the existing list of
414 # completion candidates, COMPREPLY.
415 __gitcomp_nl_append ()
416 {
417         local IFS=$'\n'
418         __gitcompappend "$1" "${2-}" "${3-$cur}" "${4- }"
419 }
420
421 # Generates completion reply from newline-separated possible completion words
422 # by appending a space to all of them.
423 # It accepts 1 to 4 arguments:
424 # 1: List of possible completion words, separated by a single newline.
425 # 2: A prefix to be added to each possible completion word (optional).
426 # 3: Generate possible completion matches for this word (optional).
427 # 4: A suffix to be appended to each possible completion word instead of
428 #    the default space (optional).  If specified but empty, nothing is
429 #    appended.
430 __gitcomp_nl ()
431 {
432         COMPREPLY=()
433         __gitcomp_nl_append "$@"
434 }
435
436 # Fills the COMPREPLY array with prefiltered paths without any additional
437 # processing.
438 # Callers must take care of providing only paths that match the current path
439 # to be completed and adding any prefix path components, if necessary.
440 # 1: List of newline-separated matching paths, complete with all prefix
441 #    path componens.
442 __gitcomp_file_direct ()
443 {
444         local IFS=$'\n'
445
446         COMPREPLY=($1)
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 # Generates completion reply with compgen from newline-separated possible
455 # completion filenames.
456 # It accepts 1 to 3 arguments:
457 # 1: List of possible completion filenames, separated by a single newline.
458 # 2: A directory prefix to be added to each possible completion filename
459 #    (optional).
460 # 3: Generate possible completion matches for this word (optional).
461 __gitcomp_file ()
462 {
463         local IFS=$'\n'
464
465         # XXX does not work when the directory prefix contains a tilde,
466         # since tilde expansion is not applied.
467         # This means that COMPREPLY will be empty and Bash default
468         # completion will be used.
469         __gitcompadd "$1" "${2-}" "${3-$cur}" ""
470
471         # use a hack to enable file mode in bash < 4
472         compopt -o filenames +o nospace 2>/dev/null ||
473         compgen -f /non-existing-dir/ >/dev/null ||
474         true
475 }
476
477 # Execute 'git ls-files', unless the --committable option is specified, in
478 # which case it runs 'git diff-index' to find out the files that can be
479 # committed.  It return paths relative to the directory specified in the first
480 # argument, and using the options specified in the second argument.
481 __git_ls_files_helper ()
482 {
483         if [ "$2" == "--committable" ]; then
484                 __git -C "$1" -c core.quotePath=false diff-index \
485                         --name-only --relative HEAD -- "${3//\\/\\\\}*"
486         else
487                 # NOTE: $2 is not quoted in order to support multiple options
488                 __git -C "$1" -c core.quotePath=false ls-files \
489                         --exclude-standard $2 -- "${3//\\/\\\\}*"
490         fi
491 }
492
493
494 # __git_index_files accepts 1 or 2 arguments:
495 # 1: Options to pass to ls-files (required).
496 # 2: A directory path (optional).
497 #    If provided, only files within the specified directory are listed.
498 #    Sub directories are never recursed.  Path must have a trailing
499 #    slash.
500 # 3: List only paths matching this path component (optional).
501 __git_index_files ()
502 {
503         local root="$2" match="$3"
504
505         __git_ls_files_helper "$root" "$1" "$match" |
506         awk -F / -v pfx="${2//\\/\\\\}" '{
507                 paths[$1] = 1
508         }
509         END {
510                 for (p in paths) {
511                         if (substr(p, 1, 1) != "\"") {
512                                 # No special characters, easy!
513                                 print pfx p
514                                 continue
515                         }
516
517                         # The path is quoted.
518                         p = dequote(p)
519                         if (p == "")
520                                 continue
521
522                         # Even when a directory name itself does not contain
523                         # any special characters, it will still be quoted if
524                         # any of its (stripped) trailing path components do.
525                         # Because of this we may have seen the same direcory
526                         # both quoted and unquoted.
527                         if (p in paths)
528                                 # We have seen the same directory unquoted,
529                                 # skip it.
530                                 continue
531                         else
532                                 print pfx p
533                 }
534         }
535         function dequote(p,    bs_idx, out, esc, esc_idx, dec) {
536                 # Skip opening double quote.
537                 p = substr(p, 2)
538
539                 # Interpret backslash escape sequences.
540                 while ((bs_idx = index(p, "\\")) != 0) {
541                         out = out substr(p, 1, bs_idx - 1)
542                         esc = substr(p, bs_idx + 1, 1)
543                         p = substr(p, bs_idx + 2)
544
545                         if ((esc_idx = index("abtvfr\"\\", esc)) != 0) {
546                                 # C-style one-character escape sequence.
547                                 out = out substr("\a\b\t\v\f\r\"\\",
548                                                  esc_idx, 1)
549                         } else if (esc == "n") {
550                                 # Uh-oh, a newline character.
551                                 # We cant reliably put a pathname
552                                 # containing a newline into COMPREPLY,
553                                 # and the newline would create a mess.
554                                 # Skip this path.
555                                 return ""
556                         } else {
557                                 # Must be a \nnn octal value, then.
558                                 dec = esc             * 64 + \
559                                       substr(p, 1, 1) * 8  + \
560                                       substr(p, 2, 1)
561                                 out = out sprintf("%c", dec)
562                                 p = substr(p, 3)
563                         }
564                 }
565                 # Drop closing double quote, if there is one.
566                 # (There isnt any if this is a directory, as it was
567                 # already stripped with the trailing path components.)
568                 if (substr(p, length(p), 1) == "\"")
569                         out = out substr(p, 1, length(p) - 1)
570                 else
571                         out = out p
572
573                 return out
574         }'
575 }
576
577 # __git_complete_index_file requires 1 argument:
578 # 1: the options to pass to ls-file
579 #
580 # The exception is --committable, which finds the files appropriate commit.
581 __git_complete_index_file ()
582 {
583         local dequoted_word pfx="" cur_
584
585         __git_dequote "$cur"
586
587         case "$dequoted_word" in
588         ?*/*)
589                 pfx="${dequoted_word%/*}/"
590                 cur_="${dequoted_word##*/}"
591                 ;;
592         *)
593                 cur_="$dequoted_word"
594         esac
595
596         __gitcomp_file_direct "$(__git_index_files "$1" "$pfx" "$cur_")"
597 }
598
599 # Lists branches from the local repository.
600 # 1: A prefix to be added to each listed branch (optional).
601 # 2: List only branches matching this word (optional; list all branches if
602 #    unset or empty).
603 # 3: A suffix to be appended to each listed branch (optional).
604 __git_heads ()
605 {
606         local pfx="${1-}" cur_="${2-}" sfx="${3-}"
607
608         __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
609                         "refs/heads/$cur_*" "refs/heads/$cur_*/**"
610 }
611
612 # Lists tags from the local repository.
613 # Accepts the same positional parameters as __git_heads() above.
614 __git_tags ()
615 {
616         local pfx="${1-}" cur_="${2-}" sfx="${3-}"
617
618         __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
619                         "refs/tags/$cur_*" "refs/tags/$cur_*/**"
620 }
621
622 # Lists refs from the local (by default) or from a remote repository.
623 # It accepts 0, 1 or 2 arguments:
624 # 1: The remote to list refs from (optional; ignored, if set but empty).
625 #    Can be the name of a configured remote, a path, or a URL.
626 # 2: In addition to local refs, list unique branches from refs/remotes/ for
627 #    'git checkout's tracking DWIMery (optional; ignored, if set but empty).
628 # 3: A prefix to be added to each listed ref (optional).
629 # 4: List only refs matching this word (optional; list all refs if unset or
630 #    empty).
631 # 5: A suffix to be appended to each listed ref (optional; ignored, if set
632 #    but empty).
633 #
634 # Use __git_complete_refs() instead.
635 __git_refs ()
636 {
637         local i hash dir track="${2-}"
638         local list_refs_from=path remote="${1-}"
639         local format refs
640         local pfx="${3-}" cur_="${4-$cur}" sfx="${5-}"
641         local match="${4-}"
642         local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
643
644         __git_find_repo_path
645         dir="$__git_repo_path"
646
647         if [ -z "$remote" ]; then
648                 if [ -z "$dir" ]; then
649                         return
650                 fi
651         else
652                 if __git_is_configured_remote "$remote"; then
653                         # configured remote takes precedence over a
654                         # local directory with the same name
655                         list_refs_from=remote
656                 elif [ -d "$remote/.git" ]; then
657                         dir="$remote/.git"
658                 elif [ -d "$remote" ]; then
659                         dir="$remote"
660                 else
661                         list_refs_from=url
662                 fi
663         fi
664
665         if [ "$list_refs_from" = path ]; then
666                 if [[ "$cur_" == ^* ]]; then
667                         pfx="$pfx^"
668                         fer_pfx="$fer_pfx^"
669                         cur_=${cur_#^}
670                         match=${match#^}
671                 fi
672                 case "$cur_" in
673                 refs|refs/*)
674                         format="refname"
675                         refs=("$match*" "$match*/**")
676                         track=""
677                         ;;
678                 *)
679                         for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD REBASE_HEAD; do
680                                 case "$i" in
681                                 $match*)
682                                         if [ -e "$dir/$i" ]; then
683                                                 echo "$pfx$i$sfx"
684                                         fi
685                                         ;;
686                                 esac
687                         done
688                         format="refname:strip=2"
689                         refs=("refs/tags/$match*" "refs/tags/$match*/**"
690                                 "refs/heads/$match*" "refs/heads/$match*/**"
691                                 "refs/remotes/$match*" "refs/remotes/$match*/**")
692                         ;;
693                 esac
694                 __git_dir="$dir" __git for-each-ref --format="$fer_pfx%($format)$sfx" \
695                         "${refs[@]}"
696                 if [ -n "$track" ]; then
697                         # employ the heuristic used by git checkout
698                         # Try to find a remote branch that matches the completion word
699                         # but only output if the branch name is unique
700                         __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
701                                 --sort="refname:strip=3" \
702                                 "refs/remotes/*/$match*" "refs/remotes/*/$match*/**" | \
703                         uniq -u
704                 fi
705                 return
706         fi
707         case "$cur_" in
708         refs|refs/*)
709                 __git ls-remote "$remote" "$match*" | \
710                 while read -r hash i; do
711                         case "$i" in
712                         *^{}) ;;
713                         *) echo "$pfx$i$sfx" ;;
714                         esac
715                 done
716                 ;;
717         *)
718                 if [ "$list_refs_from" = remote ]; then
719                         case "HEAD" in
720                         $match*)        echo "${pfx}HEAD$sfx" ;;
721                         esac
722                         __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
723                                 "refs/remotes/$remote/$match*" \
724                                 "refs/remotes/$remote/$match*/**"
725                 else
726                         local query_symref
727                         case "HEAD" in
728                         $match*)        query_symref="HEAD" ;;
729                         esac
730                         __git ls-remote "$remote" $query_symref \
731                                 "refs/tags/$match*" "refs/heads/$match*" \
732                                 "refs/remotes/$match*" |
733                         while read -r hash i; do
734                                 case "$i" in
735                                 *^{})   ;;
736                                 refs/*) echo "$pfx${i#refs/*/}$sfx" ;;
737                                 *)      echo "$pfx$i$sfx" ;;  # symbolic refs
738                                 esac
739                         done
740                 fi
741                 ;;
742         esac
743 }
744
745 # Completes refs, short and long, local and remote, symbolic and pseudo.
746 #
747 # Usage: __git_complete_refs [<option>]...
748 # --remote=<remote>: The remote to list refs from, can be the name of a
749 #                    configured remote, a path, or a URL.
750 # --track: List unique remote branches for 'git checkout's tracking DWIMery.
751 # --pfx=<prefix>: A prefix to be added to each ref.
752 # --cur=<word>: The current ref to be completed.  Defaults to the current
753 #               word to be completed.
754 # --sfx=<suffix>: A suffix to be appended to each ref instead of the default
755 #                 space.
756 __git_complete_refs ()
757 {
758         local remote track pfx cur_="$cur" sfx=" "
759
760         while test $# != 0; do
761                 case "$1" in
762                 --remote=*)     remote="${1##--remote=}" ;;
763                 --track)        track="yes" ;;
764                 --pfx=*)        pfx="${1##--pfx=}" ;;
765                 --cur=*)        cur_="${1##--cur=}" ;;
766                 --sfx=*)        sfx="${1##--sfx=}" ;;
767                 *)              return 1 ;;
768                 esac
769                 shift
770         done
771
772         __gitcomp_direct "$(__git_refs "$remote" "$track" "$pfx" "$cur_" "$sfx")"
773 }
774
775 # __git_refs2 requires 1 argument (to pass to __git_refs)
776 # Deprecated: use __git_complete_fetch_refspecs() instead.
777 __git_refs2 ()
778 {
779         local i
780         for i in $(__git_refs "$1"); do
781                 echo "$i:$i"
782         done
783 }
784
785 # Completes refspecs for fetching from a remote repository.
786 # 1: The remote repository.
787 # 2: A prefix to be added to each listed refspec (optional).
788 # 3: The ref to be completed as a refspec instead of the current word to be
789 #    completed (optional)
790 # 4: A suffix to be appended to each listed refspec instead of the default
791 #    space (optional).
792 __git_complete_fetch_refspecs ()
793 {
794         local i remote="$1" pfx="${2-}" cur_="${3-$cur}" sfx="${4- }"
795
796         __gitcomp_direct "$(
797                 for i in $(__git_refs "$remote" "" "" "$cur_") ; do
798                         echo "$pfx$i:$i$sfx"
799                 done
800                 )"
801 }
802
803 # __git_refs_remotes requires 1 argument (to pass to ls-remote)
804 __git_refs_remotes ()
805 {
806         local i hash
807         __git ls-remote "$1" 'refs/heads/*' | \
808         while read -r hash i; do
809                 echo "$i:refs/remotes/$1/${i#refs/heads/}"
810         done
811 }
812
813 __git_remotes ()
814 {
815         __git_find_repo_path
816         test -d "$__git_repo_path/remotes" && ls -1 "$__git_repo_path/remotes"
817         __git remote
818 }
819
820 # Returns true if $1 matches the name of a configured remote, false otherwise.
821 __git_is_configured_remote ()
822 {
823         local remote
824         for remote in $(__git_remotes); do
825                 if [ "$remote" = "$1" ]; then
826                         return 0
827                 fi
828         done
829         return 1
830 }
831
832 __git_list_merge_strategies ()
833 {
834         LANG=C LC_ALL=C git merge -s help 2>&1 |
835         sed -n -e '/[Aa]vailable strategies are: /,/^$/{
836                 s/\.$//
837                 s/.*://
838                 s/^[    ]*//
839                 s/[     ]*$//
840                 p
841         }'
842 }
843
844 __git_merge_strategies=
845 # 'git merge -s help' (and thus detection of the merge strategy
846 # list) fails, unfortunately, if run outside of any git working
847 # tree.  __git_merge_strategies is set to the empty string in
848 # that case, and the detection will be repeated the next time it
849 # is needed.
850 __git_compute_merge_strategies ()
851 {
852         test -n "$__git_merge_strategies" ||
853         __git_merge_strategies=$(__git_list_merge_strategies)
854 }
855
856 __git_complete_revlist_file ()
857 {
858         local pfx ls ref cur_="$cur"
859         case "$cur_" in
860         *..?*:*)
861                 return
862                 ;;
863         ?*:*)
864                 ref="${cur_%%:*}"
865                 cur_="${cur_#*:}"
866                 case "$cur_" in
867                 ?*/*)
868                         pfx="${cur_%/*}"
869                         cur_="${cur_##*/}"
870                         ls="$ref:$pfx"
871                         pfx="$pfx/"
872                         ;;
873                 *)
874                         ls="$ref"
875                         ;;
876                 esac
877
878                 case "$COMP_WORDBREAKS" in
879                 *:*) : great ;;
880                 *)   pfx="$ref:$pfx" ;;
881                 esac
882
883                 __gitcomp_nl "$(__git ls-tree "$ls" \
884                                 | sed '/^100... blob /{
885                                            s,^.*        ,,
886                                            s,$, ,
887                                        }
888                                        /^120000 blob /{
889                                            s,^.*        ,,
890                                            s,$, ,
891                                        }
892                                        /^040000 tree /{
893                                            s,^.*        ,,
894                                            s,$,/,
895                                        }
896                                        s/^.*    //')" \
897                         "$pfx" "$cur_" ""
898                 ;;
899         *...*)
900                 pfx="${cur_%...*}..."
901                 cur_="${cur_#*...}"
902                 __git_complete_refs --pfx="$pfx" --cur="$cur_"
903                 ;;
904         *..*)
905                 pfx="${cur_%..*}.."
906                 cur_="${cur_#*..}"
907                 __git_complete_refs --pfx="$pfx" --cur="$cur_"
908                 ;;
909         *)
910                 __git_complete_refs
911                 ;;
912         esac
913 }
914
915 __git_complete_file ()
916 {
917         __git_complete_revlist_file
918 }
919
920 __git_complete_revlist ()
921 {
922         __git_complete_revlist_file
923 }
924
925 __git_complete_remote_or_refspec ()
926 {
927         local cur_="$cur" cmd="${words[1]}"
928         local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
929         if [ "$cmd" = "remote" ]; then
930                 ((c++))
931         fi
932         while [ $c -lt $cword ]; do
933                 i="${words[c]}"
934                 case "$i" in
935                 --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
936                 -d|--delete) [ "$cmd" = "push" ] && lhs=0 ;;
937                 --all)
938                         case "$cmd" in
939                         push) no_complete_refspec=1 ;;
940                         fetch)
941                                 return
942                                 ;;
943                         *) ;;
944                         esac
945                         ;;
946                 -*) ;;
947                 *) remote="$i"; break ;;
948                 esac
949                 ((c++))
950         done
951         if [ -z "$remote" ]; then
952                 __gitcomp_nl "$(__git_remotes)"
953                 return
954         fi
955         if [ $no_complete_refspec = 1 ]; then
956                 return
957         fi
958         [ "$remote" = "." ] && remote=
959         case "$cur_" in
960         *:*)
961                 case "$COMP_WORDBREAKS" in
962                 *:*) : great ;;
963                 *)   pfx="${cur_%%:*}:" ;;
964                 esac
965                 cur_="${cur_#*:}"
966                 lhs=0
967                 ;;
968         +*)
969                 pfx="+"
970                 cur_="${cur_#+}"
971                 ;;
972         esac
973         case "$cmd" in
974         fetch)
975                 if [ $lhs = 1 ]; then
976                         __git_complete_fetch_refspecs "$remote" "$pfx" "$cur_"
977                 else
978                         __git_complete_refs --pfx="$pfx" --cur="$cur_"
979                 fi
980                 ;;
981         pull|remote)
982                 if [ $lhs = 1 ]; then
983                         __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
984                 else
985                         __git_complete_refs --pfx="$pfx" --cur="$cur_"
986                 fi
987                 ;;
988         push)
989                 if [ $lhs = 1 ]; then
990                         __git_complete_refs --pfx="$pfx" --cur="$cur_"
991                 else
992                         __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
993                 fi
994                 ;;
995         esac
996 }
997
998 __git_complete_strategy ()
999 {
1000         __git_compute_merge_strategies
1001         case "$prev" in
1002         -s|--strategy)
1003                 __gitcomp "$__git_merge_strategies"
1004                 return 0
1005         esac
1006         case "$cur" in
1007         --strategy=*)
1008                 __gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
1009                 return 0
1010                 ;;
1011         esac
1012         return 1
1013 }
1014
1015 __git_all_commands=
1016 __git_compute_all_commands ()
1017 {
1018         test -n "$__git_all_commands" ||
1019         __git_all_commands=$(git --list-cmds=main,others,alias,nohelpers)
1020 }
1021
1022 # Lists all set config variables starting with the given section prefix,
1023 # with the prefix removed.
1024 __git_get_config_variables ()
1025 {
1026         local section="$1" i IFS=$'\n'
1027         for i in $(__git config --name-only --get-regexp "^$section\..*"); do
1028                 echo "${i#$section.}"
1029         done
1030 }
1031
1032 __git_pretty_aliases ()
1033 {
1034         __git_get_config_variables "pretty"
1035 }
1036
1037 # __git_aliased_command requires 1 argument
1038 __git_aliased_command ()
1039 {
1040         local word cmdline=$(__git config --get "alias.$1")
1041         for word in $cmdline; do
1042                 case "$word" in
1043                 \!gitk|gitk)
1044                         echo "gitk"
1045                         return
1046                         ;;
1047                 \!*)    : shell command alias ;;
1048                 -*)     : option ;;
1049                 *=*)    : setting env ;;
1050                 git)    : git itself ;;
1051                 \(\))   : skip parens of shell function definition ;;
1052                 {)      : skip start of shell helper function ;;
1053                 :)      : skip null command ;;
1054                 \'*)    : skip opening quote after sh -c ;;
1055                 *)
1056                         echo "$word"
1057                         return
1058                 esac
1059         done
1060 }
1061
1062 # __git_find_on_cmdline requires 1 argument
1063 __git_find_on_cmdline ()
1064 {
1065         local word subcommand c=1
1066         while [ $c -lt $cword ]; do
1067                 word="${words[c]}"
1068                 for subcommand in $1; do
1069                         if [ "$subcommand" = "$word" ]; then
1070                                 echo "$subcommand"
1071                                 return
1072                         fi
1073                 done
1074                 ((c++))
1075         done
1076 }
1077
1078 # Echo the value of an option set on the command line or config
1079 #
1080 # $1: short option name
1081 # $2: long option name including =
1082 # $3: list of possible values
1083 # $4: config string (optional)
1084 #
1085 # example:
1086 # result="$(__git_get_option_value "-d" "--do-something=" \
1087 #     "yes no" "core.doSomething")"
1088 #
1089 # result is then either empty (no option set) or "yes" or "no"
1090 #
1091 # __git_get_option_value requires 3 arguments
1092 __git_get_option_value ()
1093 {
1094         local c short_opt long_opt val
1095         local result= values config_key word
1096
1097         short_opt="$1"
1098         long_opt="$2"
1099         values="$3"
1100         config_key="$4"
1101
1102         ((c = $cword - 1))
1103         while [ $c -ge 0 ]; do
1104                 word="${words[c]}"
1105                 for val in $values; do
1106                         if [ "$short_opt$val" = "$word" ] ||
1107                            [ "$long_opt$val"  = "$word" ]; then
1108                                 result="$val"
1109                                 break 2
1110                         fi
1111                 done
1112                 ((c--))
1113         done
1114
1115         if [ -n "$config_key" ] && [ -z "$result" ]; then
1116                 result="$(__git config "$config_key")"
1117         fi
1118
1119         echo "$result"
1120 }
1121
1122 __git_has_doubledash ()
1123 {
1124         local c=1
1125         while [ $c -lt $cword ]; do
1126                 if [ "--" = "${words[c]}" ]; then
1127                         return 0
1128                 fi
1129                 ((c++))
1130         done
1131         return 1
1132 }
1133
1134 # Try to count non option arguments passed on the command line for the
1135 # specified git command.
1136 # When options are used, it is necessary to use the special -- option to
1137 # tell the implementation were non option arguments begin.
1138 # XXX this can not be improved, since options can appear everywhere, as
1139 # an example:
1140 #       git mv x -n y
1141 #
1142 # __git_count_arguments requires 1 argument: the git command executed.
1143 __git_count_arguments ()
1144 {
1145         local word i c=0
1146
1147         # Skip "git" (first argument)
1148         for ((i=1; i < ${#words[@]}; i++)); do
1149                 word="${words[i]}"
1150
1151                 case "$word" in
1152                         --)
1153                                 # Good; we can assume that the following are only non
1154                                 # option arguments.
1155                                 ((c = 0))
1156                                 ;;
1157                         "$1")
1158                                 # Skip the specified git command and discard git
1159                                 # main options
1160                                 ((c = 0))
1161                                 ;;
1162                         ?*)
1163                                 ((c++))
1164                                 ;;
1165                 esac
1166         done
1167
1168         printf "%d" $c
1169 }
1170
1171 __git_whitespacelist="nowarn warn error error-all fix"
1172 __git_am_inprogress_options="--skip --continue --resolved --abort --quit --show-current-patch"
1173
1174 _git_am ()
1175 {
1176         __git_find_repo_path
1177         if [ -d "$__git_repo_path"/rebase-apply ]; then
1178                 __gitcomp "$__git_am_inprogress_options"
1179                 return
1180         fi
1181         case "$cur" in
1182         --whitespace=*)
1183                 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1184                 return
1185                 ;;
1186         --*)
1187                 __gitcomp_builtin am "" \
1188                         "$__git_am_inprogress_options"
1189                 return
1190         esac
1191 }
1192
1193 _git_apply ()
1194 {
1195         case "$cur" in
1196         --whitespace=*)
1197                 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1198                 return
1199                 ;;
1200         --*)
1201                 __gitcomp_builtin apply
1202                 return
1203         esac
1204 }
1205
1206 _git_add ()
1207 {
1208         case "$cur" in
1209         --*)
1210                 __gitcomp_builtin add
1211                 return
1212         esac
1213
1214         local complete_opt="--others --modified --directory --no-empty-directory"
1215         if test -n "$(__git_find_on_cmdline "-u --update")"
1216         then
1217                 complete_opt="--modified"
1218         fi
1219         __git_complete_index_file "$complete_opt"
1220 }
1221
1222 _git_archive ()
1223 {
1224         case "$cur" in
1225         --format=*)
1226                 __gitcomp "$(git archive --list)" "" "${cur##--format=}"
1227                 return
1228                 ;;
1229         --remote=*)
1230                 __gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
1231                 return
1232                 ;;
1233         --*)
1234                 __gitcomp "
1235                         --format= --list --verbose
1236                         --prefix= --remote= --exec= --output
1237                         "
1238                 return
1239                 ;;
1240         esac
1241         __git_complete_file
1242 }
1243
1244 _git_bisect ()
1245 {
1246         __git_has_doubledash && return
1247
1248         local subcommands="start bad good skip reset visualize replay log run"
1249         local subcommand="$(__git_find_on_cmdline "$subcommands")"
1250         if [ -z "$subcommand" ]; then
1251                 __git_find_repo_path
1252                 if [ -f "$__git_repo_path"/BISECT_START ]; then
1253                         __gitcomp "$subcommands"
1254                 else
1255                         __gitcomp "replay start"
1256                 fi
1257                 return
1258         fi
1259
1260         case "$subcommand" in
1261         bad|good|reset|skip|start)
1262                 __git_complete_refs
1263                 ;;
1264         *)
1265                 ;;
1266         esac
1267 }
1268
1269 _git_branch ()
1270 {
1271         local i c=1 only_local_ref="n" has_r="n"
1272
1273         while [ $c -lt $cword ]; do
1274                 i="${words[c]}"
1275                 case "$i" in
1276                 -d|--delete|-m|--move)  only_local_ref="y" ;;
1277                 -r|--remotes)           has_r="y" ;;
1278                 esac
1279                 ((c++))
1280         done
1281
1282         case "$cur" in
1283         --set-upstream-to=*)
1284                 __git_complete_refs --cur="${cur##--set-upstream-to=}"
1285                 ;;
1286         --*)
1287                 __gitcomp_builtin branch
1288                 ;;
1289         *)
1290                 if [ $only_local_ref = "y" -a $has_r = "n" ]; then
1291                         __gitcomp_direct "$(__git_heads "" "$cur" " ")"
1292                 else
1293                         __git_complete_refs
1294                 fi
1295                 ;;
1296         esac
1297 }
1298
1299 _git_bundle ()
1300 {
1301         local cmd="${words[2]}"
1302         case "$cword" in
1303         2)
1304                 __gitcomp "create list-heads verify unbundle"
1305                 ;;
1306         3)
1307                 # looking for a file
1308                 ;;
1309         *)
1310                 case "$cmd" in
1311                         create)
1312                                 __git_complete_revlist
1313                         ;;
1314                 esac
1315                 ;;
1316         esac
1317 }
1318
1319 _git_checkout ()
1320 {
1321         __git_has_doubledash && return
1322
1323         case "$cur" in
1324         --conflict=*)
1325                 __gitcomp "diff3 merge" "" "${cur##--conflict=}"
1326                 ;;
1327         --*)
1328                 __gitcomp_builtin checkout
1329                 ;;
1330         *)
1331                 # check if --track, --no-track, or --no-guess was specified
1332                 # if so, disable DWIM mode
1333                 local flags="--track --no-track --no-guess" track_opt="--track"
1334                 if [ "$GIT_COMPLETION_CHECKOUT_NO_GUESS" = "1" ] ||
1335                    [ -n "$(__git_find_on_cmdline "$flags")" ]; then
1336                         track_opt=''
1337                 fi
1338                 __git_complete_refs $track_opt
1339                 ;;
1340         esac
1341 }
1342
1343 __git_cherry_pick_inprogress_options="--continue --quit --abort"
1344
1345 _git_cherry_pick ()
1346 {
1347         __git_find_repo_path
1348         if [ -f "$__git_repo_path"/CHERRY_PICK_HEAD ]; then
1349                 __gitcomp "$__git_cherry_pick_inprogress_options"
1350                 return
1351         fi
1352         case "$cur" in
1353         --*)
1354                 __gitcomp_builtin cherry-pick "" \
1355                         "$__git_cherry_pick_inprogress_options"
1356                 ;;
1357         *)
1358                 __git_complete_refs
1359                 ;;
1360         esac
1361 }
1362
1363 _git_clean ()
1364 {
1365         case "$cur" in
1366         --*)
1367                 __gitcomp_builtin clean
1368                 return
1369                 ;;
1370         esac
1371
1372         # XXX should we check for -x option ?
1373         __git_complete_index_file "--others --directory"
1374 }
1375
1376 _git_clone ()
1377 {
1378         case "$cur" in
1379         --*)
1380                 __gitcomp_builtin clone
1381                 return
1382                 ;;
1383         esac
1384 }
1385
1386 __git_untracked_file_modes="all no normal"
1387
1388 _git_commit ()
1389 {
1390         case "$prev" in
1391         -c|-C)
1392                 __git_complete_refs
1393                 return
1394                 ;;
1395         esac
1396
1397         case "$cur" in
1398         --cleanup=*)
1399                 __gitcomp "default scissors strip verbatim whitespace
1400                         " "" "${cur##--cleanup=}"
1401                 return
1402                 ;;
1403         --reuse-message=*|--reedit-message=*|\
1404         --fixup=*|--squash=*)
1405                 __git_complete_refs --cur="${cur#*=}"
1406                 return
1407                 ;;
1408         --untracked-files=*)
1409                 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
1410                 return
1411                 ;;
1412         --*)
1413                 __gitcomp_builtin commit
1414                 return
1415         esac
1416
1417         if __git rev-parse --verify --quiet HEAD >/dev/null; then
1418                 __git_complete_index_file "--committable"
1419         else
1420                 # This is the first commit
1421                 __git_complete_index_file "--cached"
1422         fi
1423 }
1424
1425 _git_describe ()
1426 {
1427         case "$cur" in
1428         --*)
1429                 __gitcomp_builtin describe
1430                 return
1431         esac
1432         __git_complete_refs
1433 }
1434
1435 __git_diff_algorithms="myers minimal patience histogram"
1436
1437 __git_diff_submodule_formats="diff log short"
1438
1439 __git_diff_common_options="--stat --numstat --shortstat --summary
1440                         --patch-with-stat --name-only --name-status --color
1441                         --no-color --color-words --no-renames --check
1442                         --full-index --binary --abbrev --diff-filter=
1443                         --find-copies-harder --ignore-cr-at-eol
1444                         --text --ignore-space-at-eol --ignore-space-change
1445                         --ignore-all-space --ignore-blank-lines --exit-code
1446                         --quiet --ext-diff --no-ext-diff
1447                         --no-prefix --src-prefix= --dst-prefix=
1448                         --inter-hunk-context=
1449                         --patience --histogram --minimal
1450                         --raw --word-diff --word-diff-regex=
1451                         --dirstat --dirstat= --dirstat-by-file
1452                         --dirstat-by-file= --cumulative
1453                         --diff-algorithm=
1454                         --submodule --submodule= --ignore-submodules
1455 "
1456
1457 _git_diff ()
1458 {
1459         __git_has_doubledash && return
1460
1461         case "$cur" in
1462         --diff-algorithm=*)
1463                 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1464                 return
1465                 ;;
1466         --submodule=*)
1467                 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1468                 return
1469                 ;;
1470         --*)
1471                 __gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
1472                         --base --ours --theirs --no-index
1473                         $__git_diff_common_options
1474                         "
1475                 return
1476                 ;;
1477         esac
1478         __git_complete_revlist_file
1479 }
1480
1481 __git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff
1482                         tkdiff vimdiff gvimdiff xxdiff araxis p4merge bc codecompare
1483 "
1484
1485 _git_difftool ()
1486 {
1487         __git_has_doubledash && return
1488
1489         case "$cur" in
1490         --tool=*)
1491                 __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1492                 return
1493                 ;;
1494         --*)
1495                 __gitcomp_builtin difftool "$__git_diff_common_options
1496                                         --base --cached --ours --theirs
1497                                         --pickaxe-all --pickaxe-regex
1498                                         --relative --staged
1499                                         "
1500                 return
1501                 ;;
1502         esac
1503         __git_complete_revlist_file
1504 }
1505
1506 __git_fetch_recurse_submodules="yes on-demand no"
1507
1508 _git_fetch ()
1509 {
1510         case "$cur" in
1511         --recurse-submodules=*)
1512                 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1513                 return
1514                 ;;
1515         --*)
1516                 __gitcomp_builtin fetch
1517                 return
1518                 ;;
1519         esac
1520         __git_complete_remote_or_refspec
1521 }
1522
1523 __git_format_patch_options="
1524         --stdout --attach --no-attach --thread --thread= --no-thread
1525         --numbered --start-number --numbered-files --keep-subject --signoff
1526         --signature --no-signature --in-reply-to= --cc= --full-index --binary
1527         --not --all --cover-letter --no-prefix --src-prefix= --dst-prefix=
1528         --inline --suffix= --ignore-if-in-upstream --subject-prefix=
1529         --output-directory --reroll-count --to= --quiet --notes
1530 "
1531
1532 _git_format_patch ()
1533 {
1534         case "$cur" in
1535         --thread=*)
1536                 __gitcomp "
1537                         deep shallow
1538                         " "" "${cur##--thread=}"
1539                 return
1540                 ;;
1541         --*)
1542                 __gitcomp "$__git_format_patch_options"
1543                 return
1544                 ;;
1545         esac
1546         __git_complete_revlist
1547 }
1548
1549 _git_fsck ()
1550 {
1551         case "$cur" in
1552         --*)
1553                 __gitcomp_builtin fsck
1554                 return
1555                 ;;
1556         esac
1557 }
1558
1559 _git_gitk ()
1560 {
1561         _gitk
1562 }
1563
1564 # Lists matching symbol names from a tag (as in ctags) file.
1565 # 1: List symbol names matching this word.
1566 # 2: The tag file to list symbol names from.
1567 # 3: A prefix to be added to each listed symbol name (optional).
1568 # 4: A suffix to be appended to each listed symbol name (optional).
1569 __git_match_ctag () {
1570         awk -v pfx="${3-}" -v sfx="${4-}" "
1571                 /^${1//\//\\/}/ { print pfx \$1 sfx }
1572                 " "$2"
1573 }
1574
1575 # Complete symbol names from a tag file.
1576 # Usage: __git_complete_symbol [<option>]...
1577 # --tags=<file>: The tag file to list symbol names from instead of the
1578 #                default "tags".
1579 # --pfx=<prefix>: A prefix to be added to each symbol name.
1580 # --cur=<word>: The current symbol name to be completed.  Defaults to
1581 #               the current word to be completed.
1582 # --sfx=<suffix>: A suffix to be appended to each symbol name instead
1583 #                 of the default space.
1584 __git_complete_symbol () {
1585         local tags=tags pfx="" cur_="${cur-}" sfx=" "
1586
1587         while test $# != 0; do
1588                 case "$1" in
1589                 --tags=*)       tags="${1##--tags=}" ;;
1590                 --pfx=*)        pfx="${1##--pfx=}" ;;
1591                 --cur=*)        cur_="${1##--cur=}" ;;
1592                 --sfx=*)        sfx="${1##--sfx=}" ;;
1593                 *)              return 1 ;;
1594                 esac
1595                 shift
1596         done
1597
1598         if test -r "$tags"; then
1599                 __gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
1600         fi
1601 }
1602
1603 _git_grep ()
1604 {
1605         __git_has_doubledash && return
1606
1607         case "$cur" in
1608         --*)
1609                 __gitcomp_builtin grep
1610                 return
1611                 ;;
1612         esac
1613
1614         case "$cword,$prev" in
1615         2,*|*,-*)
1616                 __git_complete_symbol && return
1617                 ;;
1618         esac
1619
1620         __git_complete_refs
1621 }
1622
1623 _git_help ()
1624 {
1625         case "$cur" in
1626         --*)
1627                 __gitcomp_builtin help
1628                 return
1629                 ;;
1630         esac
1631         if test -n "$GIT_TESTING_ALL_COMMAND_LIST"
1632         then
1633                 __gitcomp "$GIT_TESTING_ALL_COMMAND_LIST $(git --list-cmds=alias,list-guide) gitk"
1634         else
1635                 __gitcomp "$(git --list-cmds=main,nohelpers,alias,list-guide) gitk"
1636         fi
1637 }
1638
1639 _git_init ()
1640 {
1641         case "$cur" in
1642         --shared=*)
1643                 __gitcomp "
1644                         false true umask group all world everybody
1645                         " "" "${cur##--shared=}"
1646                 return
1647                 ;;
1648         --*)
1649                 __gitcomp_builtin init
1650                 return
1651                 ;;
1652         esac
1653 }
1654
1655 _git_ls_files ()
1656 {
1657         case "$cur" in
1658         --*)
1659                 __gitcomp_builtin ls-files
1660                 return
1661                 ;;
1662         esac
1663
1664         # XXX ignore options like --modified and always suggest all cached
1665         # files.
1666         __git_complete_index_file "--cached"
1667 }
1668
1669 _git_ls_remote ()
1670 {
1671         case "$cur" in
1672         --*)
1673                 __gitcomp_builtin ls-remote
1674                 return
1675                 ;;
1676         esac
1677         __gitcomp_nl "$(__git_remotes)"
1678 }
1679
1680 _git_ls_tree ()
1681 {
1682         case "$cur" in
1683         --*)
1684                 __gitcomp_builtin ls-tree
1685                 return
1686                 ;;
1687         esac
1688
1689         __git_complete_file
1690 }
1691
1692 # Options that go well for log, shortlog and gitk
1693 __git_log_common_options="
1694         --not --all
1695         --branches --tags --remotes
1696         --first-parent --merges --no-merges
1697         --max-count=
1698         --max-age= --since= --after=
1699         --min-age= --until= --before=
1700         --min-parents= --max-parents=
1701         --no-min-parents --no-max-parents
1702 "
1703 # Options that go well for log and gitk (not shortlog)
1704 __git_log_gitk_options="
1705         --dense --sparse --full-history
1706         --simplify-merges --simplify-by-decoration
1707         --left-right --notes --no-notes
1708 "
1709 # Options that go well for log and shortlog (not gitk)
1710 __git_log_shortlog_options="
1711         --author= --committer= --grep=
1712         --all-match --invert-grep
1713 "
1714
1715 __git_log_pretty_formats="oneline short medium full fuller email raw format:"
1716 __git_log_date_formats="relative iso8601 rfc2822 short local default raw"
1717
1718 _git_log ()
1719 {
1720         __git_has_doubledash && return
1721         __git_find_repo_path
1722
1723         local merge=""
1724         if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
1725                 merge="--merge"
1726         fi
1727         case "$prev,$cur" in
1728         -L,:*:*)
1729                 return  # fall back to Bash filename completion
1730                 ;;
1731         -L,:*)
1732                 __git_complete_symbol --cur="${cur#:}" --sfx=":"
1733                 return
1734                 ;;
1735         -G,*|-S,*)
1736                 __git_complete_symbol
1737                 return
1738                 ;;
1739         esac
1740         case "$cur" in
1741         --pretty=*|--format=*)
1742                 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
1743                         " "" "${cur#*=}"
1744                 return
1745                 ;;
1746         --date=*)
1747                 __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
1748                 return
1749                 ;;
1750         --decorate=*)
1751                 __gitcomp "full short no" "" "${cur##--decorate=}"
1752                 return
1753                 ;;
1754         --diff-algorithm=*)
1755                 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1756                 return
1757                 ;;
1758         --submodule=*)
1759                 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1760                 return
1761                 ;;
1762         --*)
1763                 __gitcomp "
1764                         $__git_log_common_options
1765                         $__git_log_shortlog_options
1766                         $__git_log_gitk_options
1767                         --root --topo-order --date-order --reverse
1768                         --follow --full-diff
1769                         --abbrev-commit --abbrev=
1770                         --relative-date --date=
1771                         --pretty= --format= --oneline
1772                         --show-signature
1773                         --cherry-mark
1774                         --cherry-pick
1775                         --graph
1776                         --decorate --decorate=
1777                         --walk-reflogs
1778                         --parents --children
1779                         $merge
1780                         $__git_diff_common_options
1781                         --pickaxe-all --pickaxe-regex
1782                         "
1783                 return
1784                 ;;
1785         -L:*:*)
1786                 return  # fall back to Bash filename completion
1787                 ;;
1788         -L:*)
1789                 __git_complete_symbol --cur="${cur#-L:}" --sfx=":"
1790                 return
1791                 ;;
1792         -G*)
1793                 __git_complete_symbol --pfx="-G" --cur="${cur#-G}"
1794                 return
1795                 ;;
1796         -S*)
1797                 __git_complete_symbol --pfx="-S" --cur="${cur#-S}"
1798                 return
1799                 ;;
1800         esac
1801         __git_complete_revlist
1802 }
1803
1804 _git_merge ()
1805 {
1806         __git_complete_strategy && return
1807
1808         case "$cur" in
1809         --*)
1810                 __gitcomp_builtin merge
1811                 return
1812         esac
1813         __git_complete_refs
1814 }
1815
1816 _git_mergetool ()
1817 {
1818         case "$cur" in
1819         --tool=*)
1820                 __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
1821                 return
1822                 ;;
1823         --*)
1824                 __gitcomp "--tool= --prompt --no-prompt"
1825                 return
1826                 ;;
1827         esac
1828 }
1829
1830 _git_merge_base ()
1831 {
1832         case "$cur" in
1833         --*)
1834                 __gitcomp_builtin merge-base
1835                 return
1836                 ;;
1837         esac
1838         __git_complete_refs
1839 }
1840
1841 _git_mv ()
1842 {
1843         case "$cur" in
1844         --*)
1845                 __gitcomp_builtin mv
1846                 return
1847                 ;;
1848         esac
1849
1850         if [ $(__git_count_arguments "mv") -gt 0 ]; then
1851                 # We need to show both cached and untracked files (including
1852                 # empty directories) since this may not be the last argument.
1853                 __git_complete_index_file "--cached --others --directory"
1854         else
1855                 __git_complete_index_file "--cached"
1856         fi
1857 }
1858
1859 _git_notes ()
1860 {
1861         local subcommands='add append copy edit get-ref list merge prune remove show'
1862         local subcommand="$(__git_find_on_cmdline "$subcommands")"
1863
1864         case "$subcommand,$cur" in
1865         ,--*)
1866                 __gitcomp_builtin notes
1867                 ;;
1868         ,*)
1869                 case "$prev" in
1870                 --ref)
1871                         __git_complete_refs
1872                         ;;
1873                 *)
1874                         __gitcomp "$subcommands --ref"
1875                         ;;
1876                 esac
1877                 ;;
1878         *,--reuse-message=*|*,--reedit-message=*)
1879                 __git_complete_refs --cur="${cur#*=}"
1880                 ;;
1881         *,--*)
1882                 __gitcomp_builtin notes_$subcommand
1883                 ;;
1884         prune,*|get-ref,*)
1885                 # this command does not take a ref, do not complete it
1886                 ;;
1887         *)
1888                 case "$prev" in
1889                 -m|-F)
1890                         ;;
1891                 *)
1892                         __git_complete_refs
1893                         ;;
1894                 esac
1895                 ;;
1896         esac
1897 }
1898
1899 _git_pull ()
1900 {
1901         __git_complete_strategy && return
1902
1903         case "$cur" in
1904         --recurse-submodules=*)
1905                 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1906                 return
1907                 ;;
1908         --*)
1909                 __gitcomp_builtin pull
1910
1911                 return
1912                 ;;
1913         esac
1914         __git_complete_remote_or_refspec
1915 }
1916
1917 __git_push_recurse_submodules="check on-demand only"
1918
1919 __git_complete_force_with_lease ()
1920 {
1921         local cur_=$1
1922
1923         case "$cur_" in
1924         --*=)
1925                 ;;
1926         *:*)
1927                 __git_complete_refs --cur="${cur_#*:}"
1928                 ;;
1929         *)
1930                 __git_complete_refs --cur="$cur_"
1931                 ;;
1932         esac
1933 }
1934
1935 _git_push ()
1936 {
1937         case "$prev" in
1938         --repo)
1939                 __gitcomp_nl "$(__git_remotes)"
1940                 return
1941                 ;;
1942         --recurse-submodules)
1943                 __gitcomp "$__git_push_recurse_submodules"
1944                 return
1945                 ;;
1946         esac
1947         case "$cur" in
1948         --repo=*)
1949                 __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
1950                 return
1951                 ;;
1952         --recurse-submodules=*)
1953                 __gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
1954                 return
1955                 ;;
1956         --force-with-lease=*)
1957                 __git_complete_force_with_lease "${cur##--force-with-lease=}"
1958                 return
1959                 ;;
1960         --*)
1961                 __gitcomp_builtin push
1962                 return
1963                 ;;
1964         esac
1965         __git_complete_remote_or_refspec
1966 }
1967
1968 _git_range_diff ()
1969 {
1970         case "$cur" in
1971         --*)
1972                 __gitcomp "
1973                         --creation-factor= --no-dual-color
1974                         $__git_diff_common_options
1975                 "
1976                 return
1977                 ;;
1978         esac
1979         __git_complete_revlist
1980 }
1981
1982 _git_rebase ()
1983 {
1984         __git_find_repo_path
1985         if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
1986                 __gitcomp "--continue --skip --abort --quit --edit-todo --show-current-patch"
1987                 return
1988         elif [ -d "$__git_repo_path"/rebase-apply ] || \
1989              [ -d "$__git_repo_path"/rebase-merge ]; then
1990                 __gitcomp "--continue --skip --abort --quit --show-current-patch"
1991                 return
1992         fi
1993         __git_complete_strategy && return
1994         case "$cur" in
1995         --whitespace=*)
1996                 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1997                 return
1998                 ;;
1999         --*)
2000                 __gitcomp "
2001                         --onto --merge --strategy --interactive
2002                         --rebase-merges --preserve-merges --stat --no-stat
2003                         --committer-date-is-author-date --ignore-date
2004                         --ignore-whitespace --whitespace=
2005                         --autosquash --no-autosquash
2006                         --fork-point --no-fork-point
2007                         --autostash --no-autostash
2008                         --verify --no-verify
2009                         --keep-empty --root --force-rebase --no-ff
2010                         --rerere-autoupdate
2011                         --exec
2012                         "
2013
2014                 return
2015         esac
2016         __git_complete_refs
2017 }
2018
2019 _git_reflog ()
2020 {
2021         local subcommands="show delete expire"
2022         local subcommand="$(__git_find_on_cmdline "$subcommands")"
2023
2024         if [ -z "$subcommand" ]; then
2025                 __gitcomp "$subcommands"
2026         else
2027                 __git_complete_refs
2028         fi
2029 }
2030
2031 __git_send_email_confirm_options="always never auto cc compose"
2032 __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
2033
2034 _git_send_email ()
2035 {
2036         case "$prev" in
2037         --to|--cc|--bcc|--from)
2038                 __gitcomp "$(__git send-email --dump-aliases)"
2039                 return
2040                 ;;
2041         esac
2042
2043         case "$cur" in
2044         --confirm=*)
2045                 __gitcomp "
2046                         $__git_send_email_confirm_options
2047                         " "" "${cur##--confirm=}"
2048                 return
2049                 ;;
2050         --suppress-cc=*)
2051                 __gitcomp "
2052                         $__git_send_email_suppresscc_options
2053                         " "" "${cur##--suppress-cc=}"
2054
2055                 return
2056                 ;;
2057         --smtp-encryption=*)
2058                 __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
2059                 return
2060                 ;;
2061         --thread=*)
2062                 __gitcomp "
2063                         deep shallow
2064                         " "" "${cur##--thread=}"
2065                 return
2066                 ;;
2067         --to=*|--cc=*|--bcc=*|--from=*)
2068                 __gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
2069                 return
2070                 ;;
2071         --*)
2072                 __gitcomp "--annotate --bcc --cc --cc-cmd --chain-reply-to
2073                         --compose --confirm= --dry-run --envelope-sender
2074                         --from --identity
2075                         --in-reply-to --no-chain-reply-to --no-signed-off-by-cc
2076                         --no-suppress-from --no-thread --quiet --reply-to
2077                         --signed-off-by-cc --smtp-pass --smtp-server
2078                         --smtp-server-port --smtp-encryption= --smtp-user
2079                         --subject --suppress-cc= --suppress-from --thread --to
2080                         --validate --no-validate
2081                         $__git_format_patch_options"
2082                 return
2083                 ;;
2084         esac
2085         __git_complete_revlist
2086 }
2087
2088 _git_stage ()
2089 {
2090         _git_add
2091 }
2092
2093 _git_status ()
2094 {
2095         local complete_opt
2096         local untracked_state
2097
2098         case "$cur" in
2099         --ignore-submodules=*)
2100                 __gitcomp "none untracked dirty all" "" "${cur##--ignore-submodules=}"
2101                 return
2102                 ;;
2103         --untracked-files=*)
2104                 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
2105                 return
2106                 ;;
2107         --column=*)
2108                 __gitcomp "
2109                         always never auto column row plain dense nodense
2110                         " "" "${cur##--column=}"
2111                 return
2112                 ;;
2113         --*)
2114                 __gitcomp_builtin status
2115                 return
2116                 ;;
2117         esac
2118
2119         untracked_state="$(__git_get_option_value "-u" "--untracked-files=" \
2120                 "$__git_untracked_file_modes" "status.showUntrackedFiles")"
2121
2122         case "$untracked_state" in
2123         no)
2124                 # --ignored option does not matter
2125                 complete_opt=
2126                 ;;
2127         all|normal|*)
2128                 complete_opt="--cached --directory --no-empty-directory --others"
2129
2130                 if [ -n "$(__git_find_on_cmdline "--ignored")" ]; then
2131                         complete_opt="$complete_opt --ignored --exclude=*"
2132                 fi
2133                 ;;
2134         esac
2135
2136         __git_complete_index_file "$complete_opt"
2137 }
2138
2139 __git_config_get_set_variables ()
2140 {
2141         local prevword word config_file= c=$cword
2142         while [ $c -gt 1 ]; do
2143                 word="${words[c]}"
2144                 case "$word" in
2145                 --system|--global|--local|--file=*)
2146                         config_file="$word"
2147                         break
2148                         ;;
2149                 -f|--file)
2150                         config_file="$word $prevword"
2151                         break
2152                         ;;
2153                 esac
2154                 prevword=$word
2155                 c=$((--c))
2156         done
2157
2158         __git config $config_file --name-only --list
2159 }
2160
2161 __git_config_vars=
2162 __git_compute_config_vars ()
2163 {
2164         test -n "$__git_config_vars" ||
2165         __git_config_vars="$(git help --config-for-completion | sort | uniq)"
2166 }
2167
2168 _git_config ()
2169 {
2170         local varname
2171
2172         if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
2173                 varname="${prev,,}"
2174         else
2175                 varname="$(echo "$prev" |tr A-Z a-z)"
2176         fi
2177
2178         case "$varname" in
2179         branch.*.remote|branch.*.pushremote)
2180                 __gitcomp_nl "$(__git_remotes)"
2181                 return
2182                 ;;
2183         branch.*.merge)
2184                 __git_complete_refs
2185                 return
2186                 ;;
2187         branch.*.rebase)
2188                 __gitcomp "false true merges preserve interactive"
2189                 return
2190                 ;;
2191         remote.pushdefault)
2192                 __gitcomp_nl "$(__git_remotes)"
2193                 return
2194                 ;;
2195         remote.*.fetch)
2196                 local remote="${prev#remote.}"
2197                 remote="${remote%.fetch}"
2198                 if [ -z "$cur" ]; then
2199                         __gitcomp_nl "refs/heads/" "" "" ""
2200                         return
2201                 fi
2202                 __gitcomp_nl "$(__git_refs_remotes "$remote")"
2203                 return
2204                 ;;
2205         remote.*.push)
2206                 local remote="${prev#remote.}"
2207                 remote="${remote%.push}"
2208                 __gitcomp_nl "$(__git for-each-ref \
2209                         --format='%(refname):%(refname)' refs/heads)"
2210                 return
2211                 ;;
2212         pull.twohead|pull.octopus)
2213                 __git_compute_merge_strategies
2214                 __gitcomp "$__git_merge_strategies"
2215                 return
2216                 ;;
2217         color.branch|color.diff|color.interactive|\
2218         color.showbranch|color.status|color.ui)
2219                 __gitcomp "always never auto"
2220                 return
2221                 ;;
2222         color.pager)
2223                 __gitcomp "false true"
2224                 return
2225                 ;;
2226         color.*.*)
2227                 __gitcomp "
2228                         normal black red green yellow blue magenta cyan white
2229                         bold dim ul blink reverse
2230                         "
2231                 return
2232                 ;;
2233         diff.submodule)
2234                 __gitcomp "log short"
2235                 return
2236                 ;;
2237         help.format)
2238                 __gitcomp "man info web html"
2239                 return
2240                 ;;
2241         log.date)
2242                 __gitcomp "$__git_log_date_formats"
2243                 return
2244                 ;;
2245         sendemail.aliasfiletype)
2246                 __gitcomp "mutt mailrc pine elm gnus"
2247                 return
2248                 ;;
2249         sendemail.confirm)
2250                 __gitcomp "$__git_send_email_confirm_options"
2251                 return
2252                 ;;
2253         sendemail.suppresscc)
2254                 __gitcomp "$__git_send_email_suppresscc_options"
2255                 return
2256                 ;;
2257         sendemail.transferencoding)
2258                 __gitcomp "7bit 8bit quoted-printable base64"
2259                 return
2260                 ;;
2261         --get|--get-all|--unset|--unset-all)
2262                 __gitcomp_nl "$(__git_config_get_set_variables)"
2263                 return
2264                 ;;
2265         *.*)
2266                 return
2267                 ;;
2268         esac
2269         case "$cur" in
2270         --*)
2271                 __gitcomp_builtin config
2272                 return
2273                 ;;
2274         branch.*.*)
2275                 local pfx="${cur%.*}." cur_="${cur##*.}"
2276                 __gitcomp "remote pushRemote merge mergeOptions rebase" "$pfx" "$cur_"
2277                 return
2278                 ;;
2279         branch.*)
2280                 local pfx="${cur%.*}." cur_="${cur#*.}"
2281                 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
2282                 __gitcomp_nl_append $'autoSetupMerge\nautoSetupRebase\n' "$pfx" "$cur_"
2283                 return
2284                 ;;
2285         guitool.*.*)
2286                 local pfx="${cur%.*}." cur_="${cur##*.}"
2287                 __gitcomp "
2288                         argPrompt cmd confirm needsFile noConsole noRescan
2289                         prompt revPrompt revUnmerged title
2290                         " "$pfx" "$cur_"
2291                 return
2292                 ;;
2293         difftool.*.*)
2294                 local pfx="${cur%.*}." cur_="${cur##*.}"
2295                 __gitcomp "cmd path" "$pfx" "$cur_"
2296                 return
2297                 ;;
2298         man.*.*)
2299                 local pfx="${cur%.*}." cur_="${cur##*.}"
2300                 __gitcomp "cmd path" "$pfx" "$cur_"
2301                 return
2302                 ;;
2303         mergetool.*.*)
2304                 local pfx="${cur%.*}." cur_="${cur##*.}"
2305                 __gitcomp "cmd path trustExitCode" "$pfx" "$cur_"
2306                 return
2307                 ;;
2308         pager.*)
2309                 local pfx="${cur%.*}." cur_="${cur#*.}"
2310                 __git_compute_all_commands
2311                 __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_"
2312                 return
2313                 ;;
2314         remote.*.*)
2315                 local pfx="${cur%.*}." cur_="${cur##*.}"
2316                 __gitcomp "
2317                         url proxy fetch push mirror skipDefaultUpdate
2318                         receivepack uploadpack tagOpt pushurl
2319                         " "$pfx" "$cur_"
2320                 return
2321                 ;;
2322         remote.*)
2323                 local pfx="${cur%.*}." cur_="${cur#*.}"
2324                 __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
2325                 __gitcomp_nl_append "pushDefault" "$pfx" "$cur_"
2326                 return
2327                 ;;
2328         url.*.*)
2329                 local pfx="${cur%.*}." cur_="${cur##*.}"
2330                 __gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_"
2331                 return
2332                 ;;
2333         *.*)
2334                 __git_compute_config_vars
2335                 __gitcomp "$__git_config_vars"
2336                 ;;
2337         *)
2338                 __git_compute_config_vars
2339                 __gitcomp "$(echo "$__git_config_vars" | sed 's/\.[^ ]*/./g')"
2340         esac
2341 }
2342
2343 _git_remote ()
2344 {
2345         local subcommands="
2346                 add rename remove set-head set-branches
2347                 get-url set-url show prune update
2348                 "
2349         local subcommand="$(__git_find_on_cmdline "$subcommands")"
2350         if [ -z "$subcommand" ]; then
2351                 case "$cur" in
2352                 --*)
2353                         __gitcomp_builtin remote
2354                         ;;
2355                 *)
2356                         __gitcomp "$subcommands"
2357                         ;;
2358                 esac
2359                 return
2360         fi
2361
2362         case "$subcommand,$cur" in
2363         add,--*)
2364                 __gitcomp_builtin remote_add
2365                 ;;
2366         add,*)
2367                 ;;
2368         set-head,--*)
2369                 __gitcomp_builtin remote_set-head
2370                 ;;
2371         set-branches,--*)
2372                 __gitcomp_builtin remote_set-branches
2373                 ;;
2374         set-head,*|set-branches,*)
2375                 __git_complete_remote_or_refspec
2376                 ;;
2377         update,--*)
2378                 __gitcomp_builtin remote_update
2379                 ;;
2380         update,*)
2381                 __gitcomp "$(__git_remotes) $(__git_get_config_variables "remotes")"
2382                 ;;
2383         set-url,--*)
2384                 __gitcomp_builtin remote_set-url
2385                 ;;
2386         get-url,--*)
2387                 __gitcomp_builtin remote_get-url
2388                 ;;
2389         prune,--*)
2390                 __gitcomp_builtin remote_prune
2391                 ;;
2392         *)
2393                 __gitcomp_nl "$(__git_remotes)"
2394                 ;;
2395         esac
2396 }
2397
2398 _git_replace ()
2399 {
2400         case "$cur" in
2401         --*)
2402                 __gitcomp_builtin replace
2403                 return
2404                 ;;
2405         esac
2406         __git_complete_refs
2407 }
2408
2409 _git_rerere ()
2410 {
2411         local subcommands="clear forget diff remaining status gc"
2412         local subcommand="$(__git_find_on_cmdline "$subcommands")"
2413         if test -z "$subcommand"
2414         then
2415                 __gitcomp "$subcommands"
2416                 return
2417         fi
2418 }
2419
2420 _git_reset ()
2421 {
2422         __git_has_doubledash && return
2423
2424         case "$cur" in
2425         --*)
2426                 __gitcomp_builtin reset
2427                 return
2428                 ;;
2429         esac
2430         __git_complete_refs
2431 }
2432
2433 __git_revert_inprogress_options="--continue --quit --abort"
2434
2435 _git_revert ()
2436 {
2437         __git_find_repo_path
2438         if [ -f "$__git_repo_path"/REVERT_HEAD ]; then
2439                 __gitcomp "$__git_revert_inprogress_options"
2440                 return
2441         fi
2442         case "$cur" in
2443         --*)
2444                 __gitcomp_builtin revert "" \
2445                         "$__git_revert_inprogress_options"
2446                 return
2447                 ;;
2448         esac
2449         __git_complete_refs
2450 }
2451
2452 _git_rm ()
2453 {
2454         case "$cur" in
2455         --*)
2456                 __gitcomp_builtin rm
2457                 return
2458                 ;;
2459         esac
2460
2461         __git_complete_index_file "--cached"
2462 }
2463
2464 _git_shortlog ()
2465 {
2466         __git_has_doubledash && return
2467
2468         case "$cur" in
2469         --*)
2470                 __gitcomp "
2471                         $__git_log_common_options
2472                         $__git_log_shortlog_options
2473                         --numbered --summary --email
2474                         "
2475                 return
2476                 ;;
2477         esac
2478         __git_complete_revlist
2479 }
2480
2481 _git_show ()
2482 {
2483         __git_has_doubledash && return
2484
2485         case "$cur" in
2486         --pretty=*|--format=*)
2487                 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2488                         " "" "${cur#*=}"
2489                 return
2490                 ;;
2491         --diff-algorithm=*)
2492                 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
2493                 return
2494                 ;;
2495         --submodule=*)
2496                 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
2497                 return
2498                 ;;
2499         --*)
2500                 __gitcomp "--pretty= --format= --abbrev-commit --oneline
2501                         --show-signature
2502                         $__git_diff_common_options
2503                         "
2504                 return
2505                 ;;
2506         esac
2507         __git_complete_revlist_file
2508 }
2509
2510 _git_show_branch ()
2511 {
2512         case "$cur" in
2513         --*)
2514                 __gitcomp_builtin show-branch
2515                 return
2516                 ;;
2517         esac
2518         __git_complete_revlist
2519 }
2520
2521 _git_stash ()
2522 {
2523         local save_opts='--all --keep-index --no-keep-index --quiet --patch --include-untracked'
2524         local subcommands='push list show apply clear drop pop create branch'
2525         local subcommand="$(__git_find_on_cmdline "$subcommands save")"
2526         if [ -n "$(__git_find_on_cmdline "-p")" ]; then
2527                 subcommand="push"
2528         fi
2529         if [ -z "$subcommand" ]; then
2530                 case "$cur" in
2531                 --*)
2532                         __gitcomp "$save_opts"
2533                         ;;
2534                 sa*)
2535                         if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
2536                                 __gitcomp "save"
2537                         fi
2538                         ;;
2539                 *)
2540                         if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
2541                                 __gitcomp "$subcommands"
2542                         fi
2543                         ;;
2544                 esac
2545         else
2546                 case "$subcommand,$cur" in
2547                 push,--*)
2548                         __gitcomp "$save_opts --message"
2549                         ;;
2550                 save,--*)
2551                         __gitcomp "$save_opts"
2552                         ;;
2553                 apply,--*|pop,--*)
2554                         __gitcomp "--index --quiet"
2555                         ;;
2556                 drop,--*)
2557                         __gitcomp "--quiet"
2558                         ;;
2559                 show,--*|branch,--*)
2560                         ;;
2561                 branch,*)
2562                         if [ $cword -eq 3 ]; then
2563                                 __git_complete_refs
2564                         else
2565                                 __gitcomp_nl "$(__git stash list \
2566                                                 | sed -n -e 's/:.*//p')"
2567                         fi
2568                         ;;
2569                 show,*|apply,*|drop,*|pop,*)
2570                         __gitcomp_nl "$(__git stash list \
2571                                         | sed -n -e 's/:.*//p')"
2572                         ;;
2573                 *)
2574                         ;;
2575                 esac
2576         fi
2577 }
2578
2579 _git_submodule ()
2580 {
2581         __git_has_doubledash && return
2582
2583         local subcommands="add status init deinit update summary foreach sync"
2584         local subcommand="$(__git_find_on_cmdline "$subcommands")"
2585         if [ -z "$subcommand" ]; then
2586                 case "$cur" in
2587                 --*)
2588                         __gitcomp "--quiet"
2589                         ;;
2590                 *)
2591                         __gitcomp "$subcommands"
2592                         ;;
2593                 esac
2594                 return
2595         fi
2596
2597         case "$subcommand,$cur" in
2598         add,--*)
2599                 __gitcomp "--branch --force --name --reference --depth"
2600                 ;;
2601         status,--*)
2602                 __gitcomp "--cached --recursive"
2603                 ;;
2604         deinit,--*)
2605                 __gitcomp "--force --all"
2606                 ;;
2607         update,--*)
2608                 __gitcomp "
2609                         --init --remote --no-fetch
2610                         --recommend-shallow --no-recommend-shallow
2611                         --force --rebase --merge --reference --depth --recursive --jobs
2612                 "
2613                 ;;
2614         summary,--*)
2615                 __gitcomp "--cached --files --summary-limit"
2616                 ;;
2617         foreach,--*|sync,--*)
2618                 __gitcomp "--recursive"
2619                 ;;
2620         *)
2621                 ;;
2622         esac
2623 }
2624
2625 _git_svn ()
2626 {
2627         local subcommands="
2628                 init fetch clone rebase dcommit log find-rev
2629                 set-tree commit-diff info create-ignore propget
2630                 proplist show-ignore show-externals branch tag blame
2631                 migrate mkdirs reset gc
2632                 "
2633         local subcommand="$(__git_find_on_cmdline "$subcommands")"
2634         if [ -z "$subcommand" ]; then
2635                 __gitcomp "$subcommands"
2636         else
2637                 local remote_opts="--username= --config-dir= --no-auth-cache"
2638                 local fc_opts="
2639                         --follow-parent --authors-file= --repack=
2640                         --no-metadata --use-svm-props --use-svnsync-props
2641                         --log-window-size= --no-checkout --quiet
2642                         --repack-flags --use-log-author --localtime
2643                         --add-author-from
2644                         --ignore-paths= --include-paths= $remote_opts
2645                         "
2646                 local init_opts="
2647                         --template= --shared= --trunk= --tags=
2648                         --branches= --stdlayout --minimize-url
2649                         --no-metadata --use-svm-props --use-svnsync-props
2650                         --rewrite-root= --prefix= $remote_opts
2651                         "
2652                 local cmt_opts="
2653                         --edit --rmdir --find-copies-harder --copy-similarity=
2654                         "
2655
2656                 case "$subcommand,$cur" in
2657                 fetch,--*)
2658                         __gitcomp "--revision= --fetch-all $fc_opts"
2659                         ;;
2660                 clone,--*)
2661                         __gitcomp "--revision= $fc_opts $init_opts"
2662                         ;;
2663                 init,--*)
2664                         __gitcomp "$init_opts"
2665                         ;;
2666                 dcommit,--*)
2667                         __gitcomp "
2668                                 --merge --strategy= --verbose --dry-run
2669                                 --fetch-all --no-rebase --commit-url
2670                                 --revision --interactive $cmt_opts $fc_opts
2671                                 "
2672                         ;;
2673                 set-tree,--*)
2674                         __gitcomp "--stdin $cmt_opts $fc_opts"
2675                         ;;
2676                 create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
2677                 show-externals,--*|mkdirs,--*)
2678                         __gitcomp "--revision="
2679                         ;;
2680                 log,--*)
2681                         __gitcomp "
2682                                 --limit= --revision= --verbose --incremental
2683                                 --oneline --show-commit --non-recursive
2684                                 --authors-file= --color
2685                                 "
2686                         ;;
2687                 rebase,--*)
2688                         __gitcomp "
2689                                 --merge --verbose --strategy= --local
2690                                 --fetch-all --dry-run $fc_opts
2691                                 "
2692                         ;;
2693                 commit-diff,--*)
2694                         __gitcomp "--message= --file= --revision= $cmt_opts"
2695                         ;;
2696                 info,--*)
2697                         __gitcomp "--url"
2698                         ;;
2699                 branch,--*)
2700                         __gitcomp "--dry-run --message --tag"
2701                         ;;
2702                 tag,--*)
2703                         __gitcomp "--dry-run --message"
2704                         ;;
2705                 blame,--*)
2706                         __gitcomp "--git-format"
2707                         ;;
2708                 migrate,--*)
2709                         __gitcomp "
2710                                 --config-dir= --ignore-paths= --minimize
2711                                 --no-auth-cache --username=
2712                                 "
2713                         ;;
2714                 reset,--*)
2715                         __gitcomp "--revision= --parent"
2716                         ;;
2717                 *)
2718                         ;;
2719                 esac
2720         fi
2721 }
2722
2723 _git_tag ()
2724 {
2725         local i c=1 f=0
2726         while [ $c -lt $cword ]; do
2727                 i="${words[c]}"
2728                 case "$i" in
2729                 -d|--delete|-v|--verify)
2730                         __gitcomp_direct "$(__git_tags "" "$cur" " ")"
2731                         return
2732                         ;;
2733                 -f)
2734                         f=1
2735                         ;;
2736                 esac
2737                 ((c++))
2738         done
2739
2740         case "$prev" in
2741         -m|-F)
2742                 ;;
2743         -*|tag)
2744                 if [ $f = 1 ]; then
2745                         __gitcomp_direct "$(__git_tags "" "$cur" " ")"
2746                 fi
2747                 ;;
2748         *)
2749                 __git_complete_refs
2750                 ;;
2751         esac
2752
2753         case "$cur" in
2754         --*)
2755                 __gitcomp_builtin tag
2756                 ;;
2757         esac
2758 }
2759
2760 _git_whatchanged ()
2761 {
2762         _git_log
2763 }
2764
2765 _git_worktree ()
2766 {
2767         local subcommands="add list lock move prune remove unlock"
2768         local subcommand="$(__git_find_on_cmdline "$subcommands")"
2769         if [ -z "$subcommand" ]; then
2770                 __gitcomp "$subcommands"
2771         else
2772                 case "$subcommand,$cur" in
2773                 add,--*)
2774                         __gitcomp_builtin worktree_add
2775                         ;;
2776                 list,--*)
2777                         __gitcomp_builtin worktree_list
2778                         ;;
2779                 lock,--*)
2780                         __gitcomp_builtin worktree_lock
2781                         ;;
2782                 prune,--*)
2783                         __gitcomp_builtin worktree_prune
2784                         ;;
2785                 remove,--*)
2786                         __gitcomp "--force"
2787                         ;;
2788                 *)
2789                         ;;
2790                 esac
2791         fi
2792 }
2793
2794 __git_complete_common () {
2795         local command="$1"
2796
2797         case "$cur" in
2798         --*)
2799                 __gitcomp_builtin "$command"
2800                 ;;
2801         esac
2802 }
2803
2804 __git_cmds_with_parseopt_helper=
2805 __git_support_parseopt_helper () {
2806         test -n "$__git_cmds_with_parseopt_helper" ||
2807                 __git_cmds_with_parseopt_helper="$(__git --list-cmds=parseopt)"
2808
2809         case " $__git_cmds_with_parseopt_helper " in
2810         *" $1 "*)
2811                 return 0
2812                 ;;
2813         *)
2814                 return 1
2815                 ;;
2816         esac
2817 }
2818
2819 __git_complete_command () {
2820         local command="$1"
2821         local completion_func="_git_${command//-/_}"
2822         if ! declare -f $completion_func >/dev/null 2>/dev/null &&
2823                 declare -f _completion_loader >/dev/null 2>/dev/null
2824         then
2825                 _completion_loader "git-$command"
2826         fi
2827         if declare -f $completion_func >/dev/null 2>/dev/null
2828         then
2829                 $completion_func
2830                 return 0
2831         elif __git_support_parseopt_helper "$command"
2832         then
2833                 __git_complete_common "$command"
2834                 return 0
2835         else
2836                 return 1
2837         fi
2838 }
2839
2840 __git_main ()
2841 {
2842         local i c=1 command __git_dir __git_repo_path
2843         local __git_C_args C_args_count=0
2844
2845         while [ $c -lt $cword ]; do
2846                 i="${words[c]}"
2847                 case "$i" in
2848                 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
2849                 --git-dir)   ((c++)) ; __git_dir="${words[c]}" ;;
2850                 --bare)      __git_dir="." ;;
2851                 --help) command="help"; break ;;
2852                 -c|--work-tree|--namespace) ((c++)) ;;
2853                 -C)     __git_C_args[C_args_count++]=-C
2854                         ((c++))
2855                         __git_C_args[C_args_count++]="${words[c]}"
2856                         ;;
2857                 -*) ;;
2858                 *) command="$i"; break ;;
2859                 esac
2860                 ((c++))
2861         done
2862
2863         if [ -z "$command" ]; then
2864                 case "$prev" in
2865                 --git-dir|-C|--work-tree)
2866                         # these need a path argument, let's fall back to
2867                         # Bash filename completion
2868                         return
2869                         ;;
2870                 -c|--namespace)
2871                         # we don't support completing these options' arguments
2872                         return
2873                         ;;
2874                 esac
2875                 case "$cur" in
2876                 --*)   __gitcomp "
2877                         --paginate
2878                         --no-pager
2879                         --git-dir=
2880                         --bare
2881                         --version
2882                         --exec-path
2883                         --exec-path=
2884                         --html-path
2885                         --man-path
2886                         --info-path
2887                         --work-tree=
2888                         --namespace=
2889                         --no-replace-objects
2890                         --help
2891                         "
2892                         ;;
2893                 *)
2894                         if test -n "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
2895                         then
2896                                 __gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
2897                         else
2898                                 __gitcomp "$(git --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config)"
2899                         fi
2900                         ;;
2901                 esac
2902                 return
2903         fi
2904
2905         __git_complete_command "$command" && return
2906
2907         local expansion=$(__git_aliased_command "$command")
2908         if [ -n "$expansion" ]; then
2909                 words[1]=$expansion
2910                 __git_complete_command "$expansion"
2911         fi
2912 }
2913
2914 __gitk_main ()
2915 {
2916         __git_has_doubledash && return
2917
2918         local __git_repo_path
2919         __git_find_repo_path
2920
2921         local merge=""
2922         if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
2923                 merge="--merge"
2924         fi
2925         case "$cur" in
2926         --*)
2927                 __gitcomp "
2928                         $__git_log_common_options
2929                         $__git_log_gitk_options
2930                         $merge
2931                         "
2932                 return
2933                 ;;
2934         esac
2935         __git_complete_revlist
2936 }
2937
2938 if [[ -n ${ZSH_VERSION-} ]] &&
2939    # Don't define these functions when sourced from 'git-completion.zsh',
2940    # it has its own implementations.
2941    [[ -z ${GIT_SOURCING_ZSH_COMPLETION-} ]]; then
2942         echo "WARNING: this script is deprecated, please see git-completion.zsh" 1>&2
2943
2944         autoload -U +X compinit && compinit
2945
2946         __gitcomp ()
2947         {
2948                 emulate -L zsh
2949
2950                 local cur_="${3-$cur}"
2951
2952                 case "$cur_" in
2953                 --*=)
2954                         ;;
2955                 *)
2956                         local c IFS=$' \t\n'
2957                         local -a array
2958                         for c in ${=1}; do
2959                                 c="$c${4-}"
2960                                 case $c in
2961                                 --*=*|*.) ;;
2962                                 *) c="$c " ;;
2963                                 esac
2964                                 array[${#array[@]}+1]="$c"
2965                         done
2966                         compset -P '*[=:]'
2967                         compadd -Q -S '' -p "${2-}" -a -- array && _ret=0
2968                         ;;
2969                 esac
2970         }
2971
2972         __gitcomp_direct ()
2973         {
2974                 emulate -L zsh
2975
2976                 local IFS=$'\n'
2977                 compset -P '*[=:]'
2978                 compadd -Q -- ${=1} && _ret=0
2979         }
2980
2981         __gitcomp_nl ()
2982         {
2983                 emulate -L zsh
2984
2985                 local IFS=$'\n'
2986                 compset -P '*[=:]'
2987                 compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
2988         }
2989
2990         __gitcomp_file_direct ()
2991         {
2992                 emulate -L zsh
2993
2994                 local IFS=$'\n'
2995                 compset -P '*[=:]'
2996                 compadd -Q -f -- ${=1} && _ret=0
2997         }
2998
2999         __gitcomp_file ()
3000         {
3001                 emulate -L zsh
3002
3003                 local IFS=$'\n'
3004                 compset -P '*[=:]'
3005                 compadd -Q -p "${2-}" -f -- ${=1} && _ret=0
3006         }
3007
3008         _git ()
3009         {
3010                 local _ret=1 cur cword prev
3011                 cur=${words[CURRENT]}
3012                 prev=${words[CURRENT-1]}
3013                 let cword=CURRENT-1
3014                 emulate ksh -c __${service}_main
3015                 let _ret && _default && _ret=0
3016                 return _ret
3017         }
3018
3019         compdef _git git gitk
3020         return
3021 fi
3022
3023 __git_func_wrap ()
3024 {
3025         local cur words cword prev
3026         _get_comp_words_by_ref -n =: cur words cword prev
3027         $1
3028 }
3029
3030 # Setup completion for certain functions defined above by setting common
3031 # variables and workarounds.
3032 # This is NOT a public function; use at your own risk.
3033 __git_complete ()
3034 {
3035         local wrapper="__git_wrap${2}"
3036         eval "$wrapper () { __git_func_wrap $2 ; }"
3037         complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
3038                 || complete -o default -o nospace -F $wrapper $1
3039 }
3040
3041 # wrapper for backwards compatibility
3042 _git ()
3043 {
3044         __git_wrap__git_main
3045 }
3046
3047 # wrapper for backwards compatibility
3048 _gitk ()
3049 {
3050         __git_wrap__gitk_main
3051 }
3052
3053 __git_complete git __git_main
3054 __git_complete gitk __gitk_main
3055
3056 # The following are necessary only for Cygwin, and only are needed
3057 # when the user has tab-completed the executable name and consequently
3058 # included the '.exe' suffix.
3059 #
3060 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
3061 __git_complete git.exe __git_main
3062 fi