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