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