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