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