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