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