Merge branch 'js/rebase-i-redo-exec-fix'
[git] / contrib / completion / git-completion.bash
1 # bash/zsh completion support for core Git.
2 #
3 # Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
4 # Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
5 # Distributed under the GNU General Public License, version 2.0.
6 #
7 # The contained completion routines provide support for completing:
8 #
9 #    *) local and remote branch names
10 #    *) local and remote tag names
11 #    *) .git/remotes file names
12 #    *) git 'subcommands'
13 #    *) git email aliases for git-send-email
14 #    *) tree paths within 'ref:path/to/file' expressions
15 #    *) file paths within current working directory and index
16 #    *) common --long-options
17 #
18 # To use these routines:
19 #
20 #    1) Copy this file to somewhere (e.g. ~/.git-completion.bash).
21 #    2) Add the following line to your .bashrc/.zshrc:
22 #        source ~/.git-completion.bash
23 #    3) Consider changing your PS1 to also show the current branch,
24 #       see git-prompt.sh for details.
25 #
26 # If you use complex aliases of form '!f() { ... }; f', you can use the null
27 # command ':' as the first command in the function body to declare the desired
28 # completion style.  For example '!f() { : git commit ; ... }; f' will
29 # tell the completion to use commit completion.  This also works with aliases
30 # of form "!sh -c '...'".  For example, "!sh -c ': git commit ; ... '".
31 #
32 # Compatible with bash 3.2.57.
33 #
34 # You can set the following environment variables to influence the behavior of
35 # the completion routines:
36 #
37 #   GIT_COMPLETION_CHECKOUT_NO_GUESS
38 #
39 #     When set to "1", do not include "DWIM" suggestions in git-checkout
40 #     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=" $incl $(__git ${cmd/_/ } --git-completion-helper) "
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 components.
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 dequoted_word pfx ls ref cur_="$cur"
859         case "$cur_" in
860         *..?*:*)
861                 return
862                 ;;
863         ?*:*)
864                 ref="${cur_%%:*}"
865                 cur_="${cur_#*:}"
866
867                 __git_dequote "$cur_"
868
869                 case "$dequoted_word" in
870                 ?*/*)
871                         pfx="${dequoted_word%/*}"
872                         cur_="${dequoted_word##*/}"
873                         ls="$ref:$pfx"
874                         pfx="$pfx/"
875                         ;;
876                 *)
877                         cur_="$dequoted_word"
878                         ls="$ref"
879                         ;;
880                 esac
881
882                 case "$COMP_WORDBREAKS" in
883                 *:*) : great ;;
884                 *)   pfx="$ref:$pfx" ;;
885                 esac
886
887                 __gitcomp_file "$(__git ls-tree "$ls" \
888                                 | sed 's/^.*    //
889                                        s/$//')" \
890                         "$pfx" "$cur_"
891                 ;;
892         *...*)
893                 pfx="${cur_%...*}..."
894                 cur_="${cur_#*...}"
895                 __git_complete_refs --pfx="$pfx" --cur="$cur_"
896                 ;;
897         *..*)
898                 pfx="${cur_%..*}.."
899                 cur_="${cur_#*..}"
900                 __git_complete_refs --pfx="$pfx" --cur="$cur_"
901                 ;;
902         *)
903                 __git_complete_refs
904                 ;;
905         esac
906 }
907
908 __git_complete_file ()
909 {
910         __git_complete_revlist_file
911 }
912
913 __git_complete_revlist ()
914 {
915         __git_complete_revlist_file
916 }
917
918 __git_complete_remote_or_refspec ()
919 {
920         local cur_="$cur" cmd="${words[1]}"
921         local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
922         if [ "$cmd" = "remote" ]; then
923                 ((c++))
924         fi
925         while [ $c -lt $cword ]; do
926                 i="${words[c]}"
927                 case "$i" in
928                 --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
929                 -d|--delete) [ "$cmd" = "push" ] && lhs=0 ;;
930                 --all)
931                         case "$cmd" in
932                         push) no_complete_refspec=1 ;;
933                         fetch)
934                                 return
935                                 ;;
936                         *) ;;
937                         esac
938                         ;;
939                 --multiple) no_complete_refspec=1; break ;;
940                 -*) ;;
941                 *) remote="$i"; break ;;
942                 esac
943                 ((c++))
944         done
945         if [ -z "$remote" ]; then
946                 __gitcomp_nl "$(__git_remotes)"
947                 return
948         fi
949         if [ $no_complete_refspec = 1 ]; then
950                 return
951         fi
952         [ "$remote" = "." ] && remote=
953         case "$cur_" in
954         *:*)
955                 case "$COMP_WORDBREAKS" in
956                 *:*) : great ;;
957                 *)   pfx="${cur_%%:*}:" ;;
958                 esac
959                 cur_="${cur_#*:}"
960                 lhs=0
961                 ;;
962         +*)
963                 pfx="+"
964                 cur_="${cur_#+}"
965                 ;;
966         esac
967         case "$cmd" in
968         fetch)
969                 if [ $lhs = 1 ]; then
970                         __git_complete_fetch_refspecs "$remote" "$pfx" "$cur_"
971                 else
972                         __git_complete_refs --pfx="$pfx" --cur="$cur_"
973                 fi
974                 ;;
975         pull|remote)
976                 if [ $lhs = 1 ]; then
977                         __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
978                 else
979                         __git_complete_refs --pfx="$pfx" --cur="$cur_"
980                 fi
981                 ;;
982         push)
983                 if [ $lhs = 1 ]; then
984                         __git_complete_refs --pfx="$pfx" --cur="$cur_"
985                 else
986                         __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
987                 fi
988                 ;;
989         esac
990 }
991
992 __git_complete_strategy ()
993 {
994         __git_compute_merge_strategies
995         case "$prev" in
996         -s|--strategy)
997                 __gitcomp "$__git_merge_strategies"
998                 return 0
999         esac
1000         case "$cur" in
1001         --strategy=*)
1002                 __gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
1003                 return 0
1004                 ;;
1005         esac
1006         return 1
1007 }
1008
1009 __git_all_commands=
1010 __git_compute_all_commands ()
1011 {
1012         test -n "$__git_all_commands" ||
1013         __git_all_commands=$(git --list-cmds=main,others,alias,nohelpers)
1014 }
1015
1016 # Lists all set config variables starting with the given section prefix,
1017 # with the prefix removed.
1018 __git_get_config_variables ()
1019 {
1020         local section="$1" i IFS=$'\n'
1021         for i in $(__git config --name-only --get-regexp "^$section\..*"); do
1022                 echo "${i#$section.}"
1023         done
1024 }
1025
1026 __git_pretty_aliases ()
1027 {
1028         __git_get_config_variables "pretty"
1029 }
1030
1031 # __git_aliased_command requires 1 argument
1032 __git_aliased_command ()
1033 {
1034         local word cmdline=$(__git config --get "alias.$1")
1035         for word in $cmdline; do
1036                 case "$word" in
1037                 \!gitk|gitk)
1038                         echo "gitk"
1039                         return
1040                         ;;
1041                 \!*)    : shell command alias ;;
1042                 -*)     : option ;;
1043                 *=*)    : setting env ;;
1044                 git)    : git itself ;;
1045                 \(\))   : skip parens of shell function definition ;;
1046                 {)      : skip start of shell helper function ;;
1047                 :)      : skip null command ;;
1048                 \'*)    : skip opening quote after sh -c ;;
1049                 *)
1050                         echo "$word"
1051                         return
1052                 esac
1053         done
1054 }
1055
1056 # __git_find_on_cmdline requires 1 argument
1057 __git_find_on_cmdline ()
1058 {
1059         local word subcommand c=1
1060         while [ $c -lt $cword ]; do
1061                 word="${words[c]}"
1062                 for subcommand in $1; do
1063                         if [ "$subcommand" = "$word" ]; then
1064                                 echo "$subcommand"
1065                                 return
1066                         fi
1067                 done
1068                 ((c++))
1069         done
1070 }
1071
1072 # Echo the value of an option set on the command line or config
1073 #
1074 # $1: short option name
1075 # $2: long option name including =
1076 # $3: list of possible values
1077 # $4: config string (optional)
1078 #
1079 # example:
1080 # result="$(__git_get_option_value "-d" "--do-something=" \
1081 #     "yes no" "core.doSomething")"
1082 #
1083 # result is then either empty (no option set) or "yes" or "no"
1084 #
1085 # __git_get_option_value requires 3 arguments
1086 __git_get_option_value ()
1087 {
1088         local c short_opt long_opt val
1089         local result= values config_key word
1090
1091         short_opt="$1"
1092         long_opt="$2"
1093         values="$3"
1094         config_key="$4"
1095
1096         ((c = $cword - 1))
1097         while [ $c -ge 0 ]; do
1098                 word="${words[c]}"
1099                 for val in $values; do
1100                         if [ "$short_opt$val" = "$word" ] ||
1101                            [ "$long_opt$val"  = "$word" ]; then
1102                                 result="$val"
1103                                 break 2
1104                         fi
1105                 done
1106                 ((c--))
1107         done
1108
1109         if [ -n "$config_key" ] && [ -z "$result" ]; then
1110                 result="$(__git config "$config_key")"
1111         fi
1112
1113         echo "$result"
1114 }
1115
1116 __git_has_doubledash ()
1117 {
1118         local c=1
1119         while [ $c -lt $cword ]; do
1120                 if [ "--" = "${words[c]}" ]; then
1121                         return 0
1122                 fi
1123                 ((c++))
1124         done
1125         return 1
1126 }
1127
1128 # Try to count non option arguments passed on the command line for the
1129 # specified git command.
1130 # When options are used, it is necessary to use the special -- option to
1131 # tell the implementation were non option arguments begin.
1132 # XXX this can not be improved, since options can appear everywhere, as
1133 # an example:
1134 #       git mv x -n y
1135 #
1136 # __git_count_arguments requires 1 argument: the git command executed.
1137 __git_count_arguments ()
1138 {
1139         local word i c=0
1140
1141         # Skip "git" (first argument)
1142         for ((i=1; i < ${#words[@]}; i++)); do
1143                 word="${words[i]}"
1144
1145                 case "$word" in
1146                         --)
1147                                 # Good; we can assume that the following are only non
1148                                 # option arguments.
1149                                 ((c = 0))
1150                                 ;;
1151                         "$1")
1152                                 # Skip the specified git command and discard git
1153                                 # main options
1154                                 ((c = 0))
1155                                 ;;
1156                         ?*)
1157                                 ((c++))
1158                                 ;;
1159                 esac
1160         done
1161
1162         printf "%d" $c
1163 }
1164
1165 __git_whitespacelist="nowarn warn error error-all fix"
1166 __git_am_inprogress_options="--skip --continue --resolved --abort --quit --show-current-patch"
1167
1168 _git_am ()
1169 {
1170         __git_find_repo_path
1171         if [ -d "$__git_repo_path"/rebase-apply ]; then
1172                 __gitcomp "$__git_am_inprogress_options"
1173                 return
1174         fi
1175         case "$cur" in
1176         --whitespace=*)
1177                 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1178                 return
1179                 ;;
1180         --*)
1181                 __gitcomp_builtin am "" \
1182                         "$__git_am_inprogress_options"
1183                 return
1184         esac
1185 }
1186
1187 _git_apply ()
1188 {
1189         case "$cur" in
1190         --whitespace=*)
1191                 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1192                 return
1193                 ;;
1194         --*)
1195                 __gitcomp_builtin apply
1196                 return
1197         esac
1198 }
1199
1200 _git_add ()
1201 {
1202         case "$cur" in
1203         --*)
1204                 __gitcomp_builtin add
1205                 return
1206         esac
1207
1208         local complete_opt="--others --modified --directory --no-empty-directory"
1209         if test -n "$(__git_find_on_cmdline "-u --update")"
1210         then
1211                 complete_opt="--modified"
1212         fi
1213         __git_complete_index_file "$complete_opt"
1214 }
1215
1216 _git_archive ()
1217 {
1218         case "$cur" in
1219         --format=*)
1220                 __gitcomp "$(git archive --list)" "" "${cur##--format=}"
1221                 return
1222                 ;;
1223         --remote=*)
1224                 __gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
1225                 return
1226                 ;;
1227         --*)
1228                 __gitcomp "
1229                         --format= --list --verbose
1230                         --prefix= --remote= --exec= --output
1231                         "
1232                 return
1233                 ;;
1234         esac
1235         __git_complete_file
1236 }
1237
1238 _git_bisect ()
1239 {
1240         __git_has_doubledash && return
1241
1242         local subcommands="start bad good skip reset visualize replay log run"
1243         local subcommand="$(__git_find_on_cmdline "$subcommands")"
1244         if [ -z "$subcommand" ]; then
1245                 __git_find_repo_path
1246                 if [ -f "$__git_repo_path"/BISECT_START ]; then
1247                         __gitcomp "$subcommands"
1248                 else
1249                         __gitcomp "replay start"
1250                 fi
1251                 return
1252         fi
1253
1254         case "$subcommand" in
1255         bad|good|reset|skip|start)
1256                 __git_complete_refs
1257                 ;;
1258         *)
1259                 ;;
1260         esac
1261 }
1262
1263 _git_branch ()
1264 {
1265         local i c=1 only_local_ref="n" has_r="n"
1266
1267         while [ $c -lt $cword ]; do
1268                 i="${words[c]}"
1269                 case "$i" in
1270                 -d|--delete|-m|--move)  only_local_ref="y" ;;
1271                 -r|--remotes)           has_r="y" ;;
1272                 esac
1273                 ((c++))
1274         done
1275
1276         case "$cur" in
1277         --set-upstream-to=*)
1278                 __git_complete_refs --cur="${cur##--set-upstream-to=}"
1279                 ;;
1280         --*)
1281                 __gitcomp_builtin branch
1282                 ;;
1283         *)
1284                 if [ $only_local_ref = "y" -a $has_r = "n" ]; then
1285                         __gitcomp_direct "$(__git_heads "" "$cur" " ")"
1286                 else
1287                         __git_complete_refs
1288                 fi
1289                 ;;
1290         esac
1291 }
1292
1293 _git_bundle ()
1294 {
1295         local cmd="${words[2]}"
1296         case "$cword" in
1297         2)
1298                 __gitcomp "create list-heads verify unbundle"
1299                 ;;
1300         3)
1301                 # looking for a file
1302                 ;;
1303         *)
1304                 case "$cmd" in
1305                         create)
1306                                 __git_complete_revlist
1307                         ;;
1308                 esac
1309                 ;;
1310         esac
1311 }
1312
1313 _git_checkout ()
1314 {
1315         __git_has_doubledash && return
1316
1317         case "$cur" in
1318         --conflict=*)
1319                 __gitcomp "diff3 merge" "" "${cur##--conflict=}"
1320                 ;;
1321         --*)
1322                 __gitcomp_builtin checkout
1323                 ;;
1324         *)
1325                 # check if --track, --no-track, or --no-guess was specified
1326                 # if so, disable DWIM mode
1327                 local flags="--track --no-track --no-guess" track_opt="--track"
1328                 if [ "$GIT_COMPLETION_CHECKOUT_NO_GUESS" = "1" ] ||
1329                    [ -n "$(__git_find_on_cmdline "$flags")" ]; then
1330                         track_opt=''
1331                 fi
1332                 __git_complete_refs $track_opt
1333                 ;;
1334         esac
1335 }
1336
1337 __git_cherry_pick_inprogress_options="--continue --quit --abort"
1338
1339 _git_cherry_pick ()
1340 {
1341         __git_find_repo_path
1342         if [ -f "$__git_repo_path"/CHERRY_PICK_HEAD ]; then
1343                 __gitcomp "$__git_cherry_pick_inprogress_options"
1344                 return
1345         fi
1346         case "$cur" in
1347         --*)
1348                 __gitcomp_builtin cherry-pick "" \
1349                         "$__git_cherry_pick_inprogress_options"
1350                 ;;
1351         *)
1352                 __git_complete_refs
1353                 ;;
1354         esac
1355 }
1356
1357 _git_clean ()
1358 {
1359         case "$cur" in
1360         --*)
1361                 __gitcomp_builtin clean
1362                 return
1363                 ;;
1364         esac
1365
1366         # XXX should we check for -x option ?
1367         __git_complete_index_file "--others --directory"
1368 }
1369
1370 _git_clone ()
1371 {
1372         case "$cur" in
1373         --*)
1374                 __gitcomp_builtin clone
1375                 return
1376                 ;;
1377         esac
1378 }
1379
1380 __git_untracked_file_modes="all no normal"
1381
1382 _git_commit ()
1383 {
1384         case "$prev" in
1385         -c|-C)
1386                 __git_complete_refs
1387                 return
1388                 ;;
1389         esac
1390
1391         case "$cur" in
1392         --cleanup=*)
1393                 __gitcomp "default scissors strip verbatim whitespace
1394                         " "" "${cur##--cleanup=}"
1395                 return
1396                 ;;
1397         --reuse-message=*|--reedit-message=*|\
1398         --fixup=*|--squash=*)
1399                 __git_complete_refs --cur="${cur#*=}"
1400                 return
1401                 ;;
1402         --untracked-files=*)
1403                 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
1404                 return
1405                 ;;
1406         --*)
1407                 __gitcomp_builtin commit
1408                 return
1409         esac
1410
1411         if __git rev-parse --verify --quiet HEAD >/dev/null; then
1412                 __git_complete_index_file "--committable"
1413         else
1414                 # This is the first commit
1415                 __git_complete_index_file "--cached"
1416         fi
1417 }
1418
1419 _git_describe ()
1420 {
1421         case "$cur" in
1422         --*)
1423                 __gitcomp_builtin describe
1424                 return
1425         esac
1426         __git_complete_refs
1427 }
1428
1429 __git_diff_algorithms="myers minimal patience histogram"
1430
1431 __git_diff_submodule_formats="diff log short"
1432
1433 __git_diff_common_options="--stat --numstat --shortstat --summary
1434                         --patch-with-stat --name-only --name-status --color
1435                         --no-color --color-words --no-renames --check
1436                         --full-index --binary --abbrev --diff-filter=
1437                         --find-copies-harder --ignore-cr-at-eol
1438                         --text --ignore-space-at-eol --ignore-space-change
1439                         --ignore-all-space --ignore-blank-lines --exit-code
1440                         --quiet --ext-diff --no-ext-diff
1441                         --no-prefix --src-prefix= --dst-prefix=
1442                         --inter-hunk-context=
1443                         --patience --histogram --minimal
1444                         --raw --word-diff --word-diff-regex=
1445                         --dirstat --dirstat= --dirstat-by-file
1446                         --dirstat-by-file= --cumulative
1447                         --diff-algorithm=
1448                         --submodule --submodule= --ignore-submodules
1449 "
1450
1451 _git_diff ()
1452 {
1453         __git_has_doubledash && return
1454
1455         case "$cur" in
1456         --diff-algorithm=*)
1457                 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1458                 return
1459                 ;;
1460         --submodule=*)
1461                 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1462                 return
1463                 ;;
1464         --*)
1465                 __gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
1466                         --base --ours --theirs --no-index
1467                         $__git_diff_common_options
1468                         "
1469                 return
1470                 ;;
1471         esac
1472         __git_complete_revlist_file
1473 }
1474
1475 __git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff
1476                         tkdiff vimdiff gvimdiff xxdiff araxis p4merge bc codecompare
1477 "
1478
1479 _git_difftool ()
1480 {
1481         __git_has_doubledash && return
1482
1483         case "$cur" in
1484         --tool=*)
1485                 __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1486                 return
1487                 ;;
1488         --*)
1489                 __gitcomp_builtin difftool "$__git_diff_common_options
1490                                         --base --cached --ours --theirs
1491                                         --pickaxe-all --pickaxe-regex
1492                                         --relative --staged
1493                                         "
1494                 return
1495                 ;;
1496         esac
1497         __git_complete_revlist_file
1498 }
1499
1500 __git_fetch_recurse_submodules="yes on-demand no"
1501
1502 _git_fetch ()
1503 {
1504         case "$cur" in
1505         --recurse-submodules=*)
1506                 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1507                 return
1508                 ;;
1509         --*)
1510                 __gitcomp_builtin fetch
1511                 return
1512                 ;;
1513         esac
1514         __git_complete_remote_or_refspec
1515 }
1516
1517 __git_format_patch_extra_options="
1518         --full-index --not --all --no-prefix --src-prefix=
1519         --dst-prefix= --notes
1520 "
1521
1522 _git_format_patch ()
1523 {
1524         case "$cur" in
1525         --thread=*)
1526                 __gitcomp "
1527                         deep shallow
1528                         " "" "${cur##--thread=}"
1529                 return
1530                 ;;
1531         --*)
1532                 __gitcomp_builtin format-patch "$__git_format_patch_extra_options"
1533                 return
1534                 ;;
1535         esac
1536         __git_complete_revlist
1537 }
1538
1539 _git_fsck ()
1540 {
1541         case "$cur" in
1542         --*)
1543                 __gitcomp_builtin fsck
1544                 return
1545                 ;;
1546         esac
1547 }
1548
1549 _git_gitk ()
1550 {
1551         _gitk
1552 }
1553
1554 # Lists matching symbol names from a tag (as in ctags) file.
1555 # 1: List symbol names matching this word.
1556 # 2: The tag file to list symbol names from.
1557 # 3: A prefix to be added to each listed symbol name (optional).
1558 # 4: A suffix to be appended to each listed symbol name (optional).
1559 __git_match_ctag () {
1560         awk -v pfx="${3-}" -v sfx="${4-}" "
1561                 /^${1//\//\\/}/ { print pfx \$1 sfx }
1562                 " "$2"
1563 }
1564
1565 # Complete symbol names from a tag file.
1566 # Usage: __git_complete_symbol [<option>]...
1567 # --tags=<file>: The tag file to list symbol names from instead of the
1568 #                default "tags".
1569 # --pfx=<prefix>: A prefix to be added to each symbol name.
1570 # --cur=<word>: The current symbol name to be completed.  Defaults to
1571 #               the current word to be completed.
1572 # --sfx=<suffix>: A suffix to be appended to each symbol name instead
1573 #                 of the default space.
1574 __git_complete_symbol () {
1575         local tags=tags pfx="" cur_="${cur-}" sfx=" "
1576
1577         while test $# != 0; do
1578                 case "$1" in
1579                 --tags=*)       tags="${1##--tags=}" ;;
1580                 --pfx=*)        pfx="${1##--pfx=}" ;;
1581                 --cur=*)        cur_="${1##--cur=}" ;;
1582                 --sfx=*)        sfx="${1##--sfx=}" ;;
1583                 *)              return 1 ;;
1584                 esac
1585                 shift
1586         done
1587
1588         if test -r "$tags"; then
1589                 __gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
1590         fi
1591 }
1592
1593 _git_grep ()
1594 {
1595         __git_has_doubledash && return
1596
1597         case "$cur" in
1598         --*)
1599                 __gitcomp_builtin grep
1600                 return
1601                 ;;
1602         esac
1603
1604         case "$cword,$prev" in
1605         2,*|*,-*)
1606                 __git_complete_symbol && return
1607                 ;;
1608         esac
1609
1610         __git_complete_refs
1611 }
1612
1613 _git_help ()
1614 {
1615         case "$cur" in
1616         --*)
1617                 __gitcomp_builtin help
1618                 return
1619                 ;;
1620         esac
1621         if test -n "$GIT_TESTING_ALL_COMMAND_LIST"
1622         then
1623                 __gitcomp "$GIT_TESTING_ALL_COMMAND_LIST $(git --list-cmds=alias,list-guide) gitk"
1624         else
1625                 __gitcomp "$(git --list-cmds=main,nohelpers,alias,list-guide) gitk"
1626         fi
1627 }
1628
1629 _git_init ()
1630 {
1631         case "$cur" in
1632         --shared=*)
1633                 __gitcomp "
1634                         false true umask group all world everybody
1635                         " "" "${cur##--shared=}"
1636                 return
1637                 ;;
1638         --*)
1639                 __gitcomp_builtin init
1640                 return
1641                 ;;
1642         esac
1643 }
1644
1645 _git_ls_files ()
1646 {
1647         case "$cur" in
1648         --*)
1649                 __gitcomp_builtin ls-files
1650                 return
1651                 ;;
1652         esac
1653
1654         # XXX ignore options like --modified and always suggest all cached
1655         # files.
1656         __git_complete_index_file "--cached"
1657 }
1658
1659 _git_ls_remote ()
1660 {
1661         case "$cur" in
1662         --*)
1663                 __gitcomp_builtin ls-remote
1664                 return
1665                 ;;
1666         esac
1667         __gitcomp_nl "$(__git_remotes)"
1668 }
1669
1670 _git_ls_tree ()
1671 {
1672         case "$cur" in
1673         --*)
1674                 __gitcomp_builtin ls-tree
1675                 return
1676                 ;;
1677         esac
1678
1679         __git_complete_file
1680 }
1681
1682 # Options that go well for log, shortlog and gitk
1683 __git_log_common_options="
1684         --not --all
1685         --branches --tags --remotes
1686         --first-parent --merges --no-merges
1687         --max-count=
1688         --max-age= --since= --after=
1689         --min-age= --until= --before=
1690         --min-parents= --max-parents=
1691         --no-min-parents --no-max-parents
1692 "
1693 # Options that go well for log and gitk (not shortlog)
1694 __git_log_gitk_options="
1695         --dense --sparse --full-history
1696         --simplify-merges --simplify-by-decoration
1697         --left-right --notes --no-notes
1698 "
1699 # Options that go well for log and shortlog (not gitk)
1700 __git_log_shortlog_options="
1701         --author= --committer= --grep=
1702         --all-match --invert-grep
1703 "
1704
1705 __git_log_pretty_formats="oneline short medium full fuller email raw format:"
1706 __git_log_date_formats="relative iso8601 rfc2822 short local default raw"
1707
1708 _git_log ()
1709 {
1710         __git_has_doubledash && return
1711         __git_find_repo_path
1712
1713         local merge=""
1714         if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
1715                 merge="--merge"
1716         fi
1717         case "$prev,$cur" in
1718         -L,:*:*)
1719                 return  # fall back to Bash filename completion
1720                 ;;
1721         -L,:*)
1722                 __git_complete_symbol --cur="${cur#:}" --sfx=":"
1723                 return
1724                 ;;
1725         -G,*|-S,*)
1726                 __git_complete_symbol
1727                 return
1728                 ;;
1729         esac
1730         case "$cur" in
1731         --pretty=*|--format=*)
1732                 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
1733                         " "" "${cur#*=}"
1734                 return
1735                 ;;
1736         --date=*)
1737                 __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
1738                 return
1739                 ;;
1740         --decorate=*)
1741                 __gitcomp "full short no" "" "${cur##--decorate=}"
1742                 return
1743                 ;;
1744         --diff-algorithm=*)
1745                 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1746                 return
1747                 ;;
1748         --submodule=*)
1749                 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1750                 return
1751                 ;;
1752         --*)
1753                 __gitcomp "
1754                         $__git_log_common_options
1755                         $__git_log_shortlog_options
1756                         $__git_log_gitk_options
1757                         --root --topo-order --date-order --reverse
1758                         --follow --full-diff
1759                         --abbrev-commit --abbrev=
1760                         --relative-date --date=
1761                         --pretty= --format= --oneline
1762                         --show-signature
1763                         --cherry-mark
1764                         --cherry-pick
1765                         --graph
1766                         --decorate --decorate=
1767                         --walk-reflogs
1768                         --parents --children
1769                         $merge
1770                         $__git_diff_common_options
1771                         --pickaxe-all --pickaxe-regex
1772                         "
1773                 return
1774                 ;;
1775         -L:*:*)
1776                 return  # fall back to Bash filename completion
1777                 ;;
1778         -L:*)
1779                 __git_complete_symbol --cur="${cur#-L:}" --sfx=":"
1780                 return
1781                 ;;
1782         -G*)
1783                 __git_complete_symbol --pfx="-G" --cur="${cur#-G}"
1784                 return
1785                 ;;
1786         -S*)
1787                 __git_complete_symbol --pfx="-S" --cur="${cur#-S}"
1788                 return
1789                 ;;
1790         esac
1791         __git_complete_revlist
1792 }
1793
1794 _git_merge ()
1795 {
1796         __git_complete_strategy && return
1797
1798         case "$cur" in
1799         --*)
1800                 __gitcomp_builtin merge
1801                 return
1802         esac
1803         __git_complete_refs
1804 }
1805
1806 _git_mergetool ()
1807 {
1808         case "$cur" in
1809         --tool=*)
1810                 __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
1811                 return
1812                 ;;
1813         --*)
1814                 __gitcomp "--tool= --prompt --no-prompt --gui --no-gui"
1815                 return
1816                 ;;
1817         esac
1818 }
1819
1820 _git_merge_base ()
1821 {
1822         case "$cur" in
1823         --*)
1824                 __gitcomp_builtin merge-base
1825                 return
1826                 ;;
1827         esac
1828         __git_complete_refs
1829 }
1830
1831 _git_mv ()
1832 {
1833         case "$cur" in
1834         --*)
1835                 __gitcomp_builtin mv
1836                 return
1837                 ;;
1838         esac
1839
1840         if [ $(__git_count_arguments "mv") -gt 0 ]; then
1841                 # We need to show both cached and untracked files (including
1842                 # empty directories) since this may not be the last argument.
1843                 __git_complete_index_file "--cached --others --directory"
1844         else
1845                 __git_complete_index_file "--cached"
1846         fi
1847 }
1848
1849 _git_notes ()
1850 {
1851         local subcommands='add append copy edit get-ref list merge prune remove show'
1852         local subcommand="$(__git_find_on_cmdline "$subcommands")"
1853
1854         case "$subcommand,$cur" in
1855         ,--*)
1856                 __gitcomp_builtin notes
1857                 ;;
1858         ,*)
1859                 case "$prev" in
1860                 --ref)
1861                         __git_complete_refs
1862                         ;;
1863                 *)
1864                         __gitcomp "$subcommands --ref"
1865                         ;;
1866                 esac
1867                 ;;
1868         *,--reuse-message=*|*,--reedit-message=*)
1869                 __git_complete_refs --cur="${cur#*=}"
1870                 ;;
1871         *,--*)
1872                 __gitcomp_builtin notes_$subcommand
1873                 ;;
1874         prune,*|get-ref,*)
1875                 # this command does not take a ref, do not complete it
1876                 ;;
1877         *)
1878                 case "$prev" in
1879                 -m|-F)
1880                         ;;
1881                 *)
1882                         __git_complete_refs
1883                         ;;
1884                 esac
1885                 ;;
1886         esac
1887 }
1888
1889 _git_pull ()
1890 {
1891         __git_complete_strategy && return
1892
1893         case "$cur" in
1894         --recurse-submodules=*)
1895                 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1896                 return
1897                 ;;
1898         --*)
1899                 __gitcomp_builtin pull
1900
1901                 return
1902                 ;;
1903         esac
1904         __git_complete_remote_or_refspec
1905 }
1906
1907 __git_push_recurse_submodules="check on-demand only"
1908
1909 __git_complete_force_with_lease ()
1910 {
1911         local cur_=$1
1912
1913         case "$cur_" in
1914         --*=)
1915                 ;;
1916         *:*)
1917                 __git_complete_refs --cur="${cur_#*:}"
1918                 ;;
1919         *)
1920                 __git_complete_refs --cur="$cur_"
1921                 ;;
1922         esac
1923 }
1924
1925 _git_push ()
1926 {
1927         case "$prev" in
1928         --repo)
1929                 __gitcomp_nl "$(__git_remotes)"
1930                 return
1931                 ;;
1932         --recurse-submodules)
1933                 __gitcomp "$__git_push_recurse_submodules"
1934                 return
1935                 ;;
1936         esac
1937         case "$cur" in
1938         --repo=*)
1939                 __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
1940                 return
1941                 ;;
1942         --recurse-submodules=*)
1943                 __gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
1944                 return
1945                 ;;
1946         --force-with-lease=*)
1947                 __git_complete_force_with_lease "${cur##--force-with-lease=}"
1948                 return
1949                 ;;
1950         --*)
1951                 __gitcomp_builtin push
1952                 return
1953                 ;;
1954         esac
1955         __git_complete_remote_or_refspec
1956 }
1957
1958 _git_range_diff ()
1959 {
1960         case "$cur" in
1961         --*)
1962                 __gitcomp "
1963                         --creation-factor= --no-dual-color
1964                         $__git_diff_common_options
1965                 "
1966                 return
1967                 ;;
1968         esac
1969         __git_complete_revlist
1970 }
1971
1972 _git_rebase ()
1973 {
1974         __git_find_repo_path
1975         if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
1976                 __gitcomp "--continue --skip --abort --quit --edit-todo --show-current-patch"
1977                 return
1978         elif [ -d "$__git_repo_path"/rebase-apply ] || \
1979              [ -d "$__git_repo_path"/rebase-merge ]; then
1980                 __gitcomp "--continue --skip --abort --quit --show-current-patch"
1981                 return
1982         fi
1983         __git_complete_strategy && return
1984         case "$cur" in
1985         --whitespace=*)
1986                 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1987                 return
1988                 ;;
1989         --*)
1990                 __gitcomp "
1991                         --onto --merge --strategy --interactive
1992                         --rebase-merges --preserve-merges --stat --no-stat
1993                         --committer-date-is-author-date --ignore-date
1994                         --ignore-whitespace --whitespace=
1995                         --autosquash --no-autosquash
1996                         --fork-point --no-fork-point
1997                         --autostash --no-autostash
1998                         --verify --no-verify
1999                         --keep-empty --root --force-rebase --no-ff
2000                         --rerere-autoupdate
2001                         --exec
2002                         "
2003
2004                 return
2005         esac
2006         __git_complete_refs
2007 }
2008
2009 _git_reflog ()
2010 {
2011         local subcommands="show delete expire"
2012         local subcommand="$(__git_find_on_cmdline "$subcommands")"
2013
2014         if [ -z "$subcommand" ]; then
2015                 __gitcomp "$subcommands"
2016         else
2017                 __git_complete_refs
2018         fi
2019 }
2020
2021 __git_send_email_confirm_options="always never auto cc compose"
2022 __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
2023
2024 _git_send_email ()
2025 {
2026         case "$prev" in
2027         --to|--cc|--bcc|--from)
2028                 __gitcomp "$(__git send-email --dump-aliases)"
2029                 return
2030                 ;;
2031         esac
2032
2033         case "$cur" in
2034         --confirm=*)
2035                 __gitcomp "
2036                         $__git_send_email_confirm_options
2037                         " "" "${cur##--confirm=}"
2038                 return
2039                 ;;
2040         --suppress-cc=*)
2041                 __gitcomp "
2042                         $__git_send_email_suppresscc_options
2043                         " "" "${cur##--suppress-cc=}"
2044
2045                 return
2046                 ;;
2047         --smtp-encryption=*)
2048                 __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
2049                 return
2050                 ;;
2051         --thread=*)
2052                 __gitcomp "
2053                         deep shallow
2054                         " "" "${cur##--thread=}"
2055                 return
2056                 ;;
2057         --to=*|--cc=*|--bcc=*|--from=*)
2058                 __gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
2059                 return
2060                 ;;
2061         --*)
2062                 __gitcomp_builtin send-email "--annotate --bcc --cc --cc-cmd --chain-reply-to
2063                         --compose --confirm= --dry-run --envelope-sender
2064                         --from --identity
2065                         --in-reply-to --no-chain-reply-to --no-signed-off-by-cc
2066                         --no-suppress-from --no-thread --quiet --reply-to
2067                         --signed-off-by-cc --smtp-pass --smtp-server
2068                         --smtp-server-port --smtp-encryption= --smtp-user
2069                         --subject --suppress-cc= --suppress-from --thread --to
2070                         --validate --no-validate
2071                         $__git_format_patch_extra_options"
2072                 return
2073                 ;;
2074         esac
2075         __git_complete_revlist
2076 }
2077
2078 _git_stage ()
2079 {
2080         _git_add
2081 }
2082
2083 _git_status ()
2084 {
2085         local complete_opt
2086         local untracked_state
2087
2088         case "$cur" in
2089         --ignore-submodules=*)
2090                 __gitcomp "none untracked dirty all" "" "${cur##--ignore-submodules=}"
2091                 return
2092                 ;;
2093         --untracked-files=*)
2094                 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
2095                 return
2096                 ;;
2097         --column=*)
2098                 __gitcomp "
2099                         always never auto column row plain dense nodense
2100                         " "" "${cur##--column=}"
2101                 return
2102                 ;;
2103         --*)
2104                 __gitcomp_builtin status
2105                 return
2106                 ;;
2107         esac
2108
2109         untracked_state="$(__git_get_option_value "-u" "--untracked-files=" \
2110                 "$__git_untracked_file_modes" "status.showUntrackedFiles")"
2111
2112         case "$untracked_state" in
2113         no)
2114                 # --ignored option does not matter
2115                 complete_opt=
2116                 ;;
2117         all|normal|*)
2118                 complete_opt="--cached --directory --no-empty-directory --others"
2119
2120                 if [ -n "$(__git_find_on_cmdline "--ignored")" ]; then
2121                         complete_opt="$complete_opt --ignored --exclude=*"
2122                 fi
2123                 ;;
2124         esac
2125
2126         __git_complete_index_file "$complete_opt"
2127 }
2128
2129 __git_config_get_set_variables ()
2130 {
2131         local prevword word config_file= c=$cword
2132         while [ $c -gt 1 ]; do
2133                 word="${words[c]}"
2134                 case "$word" in
2135                 --system|--global|--local|--file=*)
2136                         config_file="$word"
2137                         break
2138                         ;;
2139                 -f|--file)
2140                         config_file="$word $prevword"
2141                         break
2142                         ;;
2143                 esac
2144                 prevword=$word
2145                 c=$((--c))
2146         done
2147
2148         __git config $config_file --name-only --list
2149 }
2150
2151 __git_config_vars=
2152 __git_compute_config_vars ()
2153 {
2154         test -n "$__git_config_vars" ||
2155         __git_config_vars="$(git help --config-for-completion | sort | uniq)"
2156 }
2157
2158 _git_config ()
2159 {
2160         local varname
2161
2162         if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
2163                 varname="${prev,,}"
2164         else
2165                 varname="$(echo "$prev" |tr A-Z a-z)"
2166         fi
2167
2168         case "$varname" in
2169         branch.*.remote|branch.*.pushremote)
2170                 __gitcomp_nl "$(__git_remotes)"
2171                 return
2172                 ;;
2173         branch.*.merge)
2174                 __git_complete_refs
2175                 return
2176                 ;;
2177         branch.*.rebase)
2178                 __gitcomp "false true merges preserve interactive"
2179                 return
2180                 ;;
2181         remote.pushdefault)
2182                 __gitcomp_nl "$(__git_remotes)"
2183                 return
2184                 ;;
2185         remote.*.fetch)
2186                 local remote="${prev#remote.}"
2187                 remote="${remote%.fetch}"
2188                 if [ -z "$cur" ]; then
2189                         __gitcomp_nl "refs/heads/" "" "" ""
2190                         return
2191                 fi
2192                 __gitcomp_nl "$(__git_refs_remotes "$remote")"
2193                 return
2194                 ;;
2195         remote.*.push)
2196                 local remote="${prev#remote.}"
2197                 remote="${remote%.push}"
2198                 __gitcomp_nl "$(__git for-each-ref \
2199                         --format='%(refname):%(refname)' refs/heads)"
2200                 return
2201                 ;;
2202         pull.twohead|pull.octopus)
2203                 __git_compute_merge_strategies
2204                 __gitcomp "$__git_merge_strategies"
2205                 return
2206                 ;;
2207         color.branch|color.diff|color.interactive|\
2208         color.showbranch|color.status|color.ui)
2209                 __gitcomp "always never auto"
2210                 return
2211                 ;;
2212         color.pager)
2213                 __gitcomp "false true"
2214                 return
2215                 ;;
2216         color.*.*)
2217                 __gitcomp "
2218                         normal black red green yellow blue magenta cyan white
2219                         bold dim ul blink reverse
2220                         "
2221                 return
2222                 ;;
2223         diff.submodule)
2224                 __gitcomp "log short"
2225                 return
2226                 ;;
2227         help.format)
2228                 __gitcomp "man info web html"
2229                 return
2230                 ;;
2231         log.date)
2232                 __gitcomp "$__git_log_date_formats"
2233                 return
2234                 ;;
2235         sendemail.aliasfiletype)
2236                 __gitcomp "mutt mailrc pine elm gnus"
2237                 return
2238                 ;;
2239         sendemail.confirm)
2240                 __gitcomp "$__git_send_email_confirm_options"
2241                 return
2242                 ;;
2243         sendemail.suppresscc)
2244                 __gitcomp "$__git_send_email_suppresscc_options"
2245                 return
2246                 ;;
2247         sendemail.transferencoding)
2248                 __gitcomp "7bit 8bit quoted-printable base64"
2249                 return
2250                 ;;
2251         --get|--get-all|--unset|--unset-all)
2252                 __gitcomp_nl "$(__git_config_get_set_variables)"
2253                 return
2254                 ;;
2255         *.*)
2256                 return
2257                 ;;
2258         esac
2259         case "$cur" in
2260         --*)
2261                 __gitcomp_builtin config
2262                 return
2263                 ;;
2264         branch.*.*)
2265                 local pfx="${cur%.*}." cur_="${cur##*.}"
2266                 __gitcomp "remote pushRemote merge mergeOptions rebase" "$pfx" "$cur_"
2267                 return
2268                 ;;
2269         branch.*)
2270                 local pfx="${cur%.*}." cur_="${cur#*.}"
2271                 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
2272                 __gitcomp_nl_append $'autoSetupMerge\nautoSetupRebase\n' "$pfx" "$cur_"
2273                 return
2274                 ;;
2275         guitool.*.*)
2276                 local pfx="${cur%.*}." cur_="${cur##*.}"
2277                 __gitcomp "
2278                         argPrompt cmd confirm needsFile noConsole noRescan
2279                         prompt revPrompt revUnmerged title
2280                         " "$pfx" "$cur_"
2281                 return
2282                 ;;
2283         difftool.*.*)
2284                 local pfx="${cur%.*}." cur_="${cur##*.}"
2285                 __gitcomp "cmd path" "$pfx" "$cur_"
2286                 return
2287                 ;;
2288         man.*.*)
2289                 local pfx="${cur%.*}." cur_="${cur##*.}"
2290                 __gitcomp "cmd path" "$pfx" "$cur_"
2291                 return
2292                 ;;
2293         mergetool.*.*)
2294                 local pfx="${cur%.*}." cur_="${cur##*.}"
2295                 __gitcomp "cmd path trustExitCode" "$pfx" "$cur_"
2296                 return
2297                 ;;
2298         pager.*)
2299                 local pfx="${cur%.*}." cur_="${cur#*.}"
2300                 __git_compute_all_commands
2301                 __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_"
2302                 return
2303                 ;;
2304         remote.*.*)
2305                 local pfx="${cur%.*}." cur_="${cur##*.}"
2306                 __gitcomp "
2307                         url proxy fetch push mirror skipDefaultUpdate
2308                         receivepack uploadpack tagOpt pushurl
2309                         " "$pfx" "$cur_"
2310                 return
2311                 ;;
2312         remote.*)
2313                 local pfx="${cur%.*}." cur_="${cur#*.}"
2314                 __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
2315                 __gitcomp_nl_append "pushDefault" "$pfx" "$cur_"
2316                 return
2317                 ;;
2318         url.*.*)
2319                 local pfx="${cur%.*}." cur_="${cur##*.}"
2320                 __gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_"
2321                 return
2322                 ;;
2323         *.*)
2324                 __git_compute_config_vars
2325                 __gitcomp "$__git_config_vars"
2326                 ;;
2327         *)
2328                 __git_compute_config_vars
2329                 __gitcomp "$(echo "$__git_config_vars" | sed 's/\.[^ ]*/./g')"
2330         esac
2331 }
2332
2333 _git_remote ()
2334 {
2335         local subcommands="
2336                 add rename remove set-head set-branches
2337                 get-url set-url show prune update
2338                 "
2339         local subcommand="$(__git_find_on_cmdline "$subcommands")"
2340         if [ -z "$subcommand" ]; then
2341                 case "$cur" in
2342                 --*)
2343                         __gitcomp_builtin remote
2344                         ;;
2345                 *)
2346                         __gitcomp "$subcommands"
2347                         ;;
2348                 esac
2349                 return
2350         fi
2351
2352         case "$subcommand,$cur" in
2353         add,--*)
2354                 __gitcomp_builtin remote_add
2355                 ;;
2356         add,*)
2357                 ;;
2358         set-head,--*)
2359                 __gitcomp_builtin remote_set-head
2360                 ;;
2361         set-branches,--*)
2362                 __gitcomp_builtin remote_set-branches
2363                 ;;
2364         set-head,*|set-branches,*)
2365                 __git_complete_remote_or_refspec
2366                 ;;
2367         update,--*)
2368                 __gitcomp_builtin remote_update
2369                 ;;
2370         update,*)
2371                 __gitcomp "$(__git_remotes) $(__git_get_config_variables "remotes")"
2372                 ;;
2373         set-url,--*)
2374                 __gitcomp_builtin remote_set-url
2375                 ;;
2376         get-url,--*)
2377                 __gitcomp_builtin remote_get-url
2378                 ;;
2379         prune,--*)
2380                 __gitcomp_builtin remote_prune
2381                 ;;
2382         *)
2383                 __gitcomp_nl "$(__git_remotes)"
2384                 ;;
2385         esac
2386 }
2387
2388 _git_replace ()
2389 {
2390         case "$cur" in
2391         --*)
2392                 __gitcomp_builtin replace
2393                 return
2394                 ;;
2395         esac
2396         __git_complete_refs
2397 }
2398
2399 _git_rerere ()
2400 {
2401         local subcommands="clear forget diff remaining status gc"
2402         local subcommand="$(__git_find_on_cmdline "$subcommands")"
2403         if test -z "$subcommand"
2404         then
2405                 __gitcomp "$subcommands"
2406                 return
2407         fi
2408 }
2409
2410 _git_reset ()
2411 {
2412         __git_has_doubledash && return
2413
2414         case "$cur" in
2415         --*)
2416                 __gitcomp_builtin reset
2417                 return
2418                 ;;
2419         esac
2420         __git_complete_refs
2421 }
2422
2423 __git_revert_inprogress_options="--continue --quit --abort"
2424
2425 _git_revert ()
2426 {
2427         __git_find_repo_path
2428         if [ -f "$__git_repo_path"/REVERT_HEAD ]; then
2429                 __gitcomp "$__git_revert_inprogress_options"
2430                 return
2431         fi
2432         case "$cur" in
2433         --*)
2434                 __gitcomp_builtin revert "" \
2435                         "$__git_revert_inprogress_options"
2436                 return
2437                 ;;
2438         esac
2439         __git_complete_refs
2440 }
2441
2442 _git_rm ()
2443 {
2444         case "$cur" in
2445         --*)
2446                 __gitcomp_builtin rm
2447                 return
2448                 ;;
2449         esac
2450
2451         __git_complete_index_file "--cached"
2452 }
2453
2454 _git_shortlog ()
2455 {
2456         __git_has_doubledash && return
2457
2458         case "$cur" in
2459         --*)
2460                 __gitcomp "
2461                         $__git_log_common_options
2462                         $__git_log_shortlog_options
2463                         --numbered --summary --email
2464                         "
2465                 return
2466                 ;;
2467         esac
2468         __git_complete_revlist
2469 }
2470
2471 _git_show ()
2472 {
2473         __git_has_doubledash && return
2474
2475         case "$cur" in
2476         --pretty=*|--format=*)
2477                 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2478                         " "" "${cur#*=}"
2479                 return
2480                 ;;
2481         --diff-algorithm=*)
2482                 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
2483                 return
2484                 ;;
2485         --submodule=*)
2486                 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
2487                 return
2488                 ;;
2489         --*)
2490                 __gitcomp "--pretty= --format= --abbrev-commit --oneline
2491                         --show-signature
2492                         $__git_diff_common_options
2493                         "
2494                 return
2495                 ;;
2496         esac
2497         __git_complete_revlist_file
2498 }
2499
2500 _git_show_branch ()
2501 {
2502         case "$cur" in
2503         --*)
2504                 __gitcomp_builtin show-branch
2505                 return
2506                 ;;
2507         esac
2508         __git_complete_revlist
2509 }
2510
2511 _git_stash ()
2512 {
2513         local save_opts='--all --keep-index --no-keep-index --quiet --patch --include-untracked'
2514         local subcommands='push list show apply clear drop pop create branch'
2515         local subcommand="$(__git_find_on_cmdline "$subcommands save")"
2516         if [ -n "$(__git_find_on_cmdline "-p")" ]; then
2517                 subcommand="push"
2518         fi
2519         if [ -z "$subcommand" ]; then
2520                 case "$cur" in
2521                 --*)
2522                         __gitcomp "$save_opts"
2523                         ;;
2524                 sa*)
2525                         if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
2526                                 __gitcomp "save"
2527                         fi
2528                         ;;
2529                 *)
2530                         if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
2531                                 __gitcomp "$subcommands"
2532                         fi
2533                         ;;
2534                 esac
2535         else
2536                 case "$subcommand,$cur" in
2537                 push,--*)
2538                         __gitcomp "$save_opts --message"
2539                         ;;
2540                 save,--*)
2541                         __gitcomp "$save_opts"
2542                         ;;
2543                 apply,--*|pop,--*)
2544                         __gitcomp "--index --quiet"
2545                         ;;
2546                 drop,--*)
2547                         __gitcomp "--quiet"
2548                         ;;
2549                 list,--*)
2550                         __gitcomp "--name-status --oneline --patch-with-stat"
2551                         ;;
2552                 show,--*|branch,--*)
2553                         ;;
2554                 branch,*)
2555                         if [ $cword -eq 3 ]; then
2556                                 __git_complete_refs
2557                         else
2558                                 __gitcomp_nl "$(__git stash list \
2559                                                 | sed -n -e 's/:.*//p')"
2560                         fi
2561                         ;;
2562                 show,*|apply,*|drop,*|pop,*)
2563                         __gitcomp_nl "$(__git stash list \
2564                                         | sed -n -e 's/:.*//p')"
2565                         ;;
2566                 *)
2567                         ;;
2568                 esac
2569         fi
2570 }
2571
2572 _git_submodule ()
2573 {
2574         __git_has_doubledash && return
2575
2576         local subcommands="add status init deinit update summary foreach sync"
2577         local subcommand="$(__git_find_on_cmdline "$subcommands")"
2578         if [ -z "$subcommand" ]; then
2579                 case "$cur" in
2580                 --*)
2581                         __gitcomp "--quiet"
2582                         ;;
2583                 *)
2584                         __gitcomp "$subcommands"
2585                         ;;
2586                 esac
2587                 return
2588         fi
2589
2590         case "$subcommand,$cur" in
2591         add,--*)
2592                 __gitcomp "--branch --force --name --reference --depth"
2593                 ;;
2594         status,--*)
2595                 __gitcomp "--cached --recursive"
2596                 ;;
2597         deinit,--*)
2598                 __gitcomp "--force --all"
2599                 ;;
2600         update,--*)
2601                 __gitcomp "
2602                         --init --remote --no-fetch
2603                         --recommend-shallow --no-recommend-shallow
2604                         --force --rebase --merge --reference --depth --recursive --jobs
2605                 "
2606                 ;;
2607         summary,--*)
2608                 __gitcomp "--cached --files --summary-limit"
2609                 ;;
2610         foreach,--*|sync,--*)
2611                 __gitcomp "--recursive"
2612                 ;;
2613         *)
2614                 ;;
2615         esac
2616 }
2617
2618 _git_svn ()
2619 {
2620         local subcommands="
2621                 init fetch clone rebase dcommit log find-rev
2622                 set-tree commit-diff info create-ignore propget
2623                 proplist show-ignore show-externals branch tag blame
2624                 migrate mkdirs reset gc
2625                 "
2626         local subcommand="$(__git_find_on_cmdline "$subcommands")"
2627         if [ -z "$subcommand" ]; then
2628                 __gitcomp "$subcommands"
2629         else
2630                 local remote_opts="--username= --config-dir= --no-auth-cache"
2631                 local fc_opts="
2632                         --follow-parent --authors-file= --repack=
2633                         --no-metadata --use-svm-props --use-svnsync-props
2634                         --log-window-size= --no-checkout --quiet
2635                         --repack-flags --use-log-author --localtime
2636                         --add-author-from
2637                         --ignore-paths= --include-paths= $remote_opts
2638                         "
2639                 local init_opts="
2640                         --template= --shared= --trunk= --tags=
2641                         --branches= --stdlayout --minimize-url
2642                         --no-metadata --use-svm-props --use-svnsync-props
2643                         --rewrite-root= --prefix= $remote_opts
2644                         "
2645                 local cmt_opts="
2646                         --edit --rmdir --find-copies-harder --copy-similarity=
2647                         "
2648
2649                 case "$subcommand,$cur" in
2650                 fetch,--*)
2651                         __gitcomp "--revision= --fetch-all $fc_opts"
2652                         ;;
2653                 clone,--*)
2654                         __gitcomp "--revision= $fc_opts $init_opts"
2655                         ;;
2656                 init,--*)
2657                         __gitcomp "$init_opts"
2658                         ;;
2659                 dcommit,--*)
2660                         __gitcomp "
2661                                 --merge --strategy= --verbose --dry-run
2662                                 --fetch-all --no-rebase --commit-url
2663                                 --revision --interactive $cmt_opts $fc_opts
2664                                 "
2665                         ;;
2666                 set-tree,--*)
2667                         __gitcomp "--stdin $cmt_opts $fc_opts"
2668                         ;;
2669                 create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
2670                 show-externals,--*|mkdirs,--*)
2671                         __gitcomp "--revision="
2672                         ;;
2673                 log,--*)
2674                         __gitcomp "
2675                                 --limit= --revision= --verbose --incremental
2676                                 --oneline --show-commit --non-recursive
2677                                 --authors-file= --color
2678                                 "
2679                         ;;
2680                 rebase,--*)
2681                         __gitcomp "
2682                                 --merge --verbose --strategy= --local
2683                                 --fetch-all --dry-run $fc_opts
2684                                 "
2685                         ;;
2686                 commit-diff,--*)
2687                         __gitcomp "--message= --file= --revision= $cmt_opts"
2688                         ;;
2689                 info,--*)
2690                         __gitcomp "--url"
2691                         ;;
2692                 branch,--*)
2693                         __gitcomp "--dry-run --message --tag"
2694                         ;;
2695                 tag,--*)
2696                         __gitcomp "--dry-run --message"
2697                         ;;
2698                 blame,--*)
2699                         __gitcomp "--git-format"
2700                         ;;
2701                 migrate,--*)
2702                         __gitcomp "
2703                                 --config-dir= --ignore-paths= --minimize
2704                                 --no-auth-cache --username=
2705                                 "
2706                         ;;
2707                 reset,--*)
2708                         __gitcomp "--revision= --parent"
2709                         ;;
2710                 *)
2711                         ;;
2712                 esac
2713         fi
2714 }
2715
2716 _git_tag ()
2717 {
2718         local i c=1 f=0
2719         while [ $c -lt $cword ]; do
2720                 i="${words[c]}"
2721                 case "$i" in
2722                 -d|--delete|-v|--verify)
2723                         __gitcomp_direct "$(__git_tags "" "$cur" " ")"
2724                         return
2725                         ;;
2726                 -f)
2727                         f=1
2728                         ;;
2729                 esac
2730                 ((c++))
2731         done
2732
2733         case "$prev" in
2734         -m|-F)
2735                 ;;
2736         -*|tag)
2737                 if [ $f = 1 ]; then
2738                         __gitcomp_direct "$(__git_tags "" "$cur" " ")"
2739                 fi
2740                 ;;
2741         *)
2742                 __git_complete_refs
2743                 ;;
2744         esac
2745
2746         case "$cur" in
2747         --*)
2748                 __gitcomp_builtin tag
2749                 ;;
2750         esac
2751 }
2752
2753 _git_whatchanged ()
2754 {
2755         _git_log
2756 }
2757
2758 _git_worktree ()
2759 {
2760         local subcommands="add list lock move prune remove unlock"
2761         local subcommand="$(__git_find_on_cmdline "$subcommands")"
2762         if [ -z "$subcommand" ]; then
2763                 __gitcomp "$subcommands"
2764         else
2765                 case "$subcommand,$cur" in
2766                 add,--*)
2767                         __gitcomp_builtin worktree_add
2768                         ;;
2769                 list,--*)
2770                         __gitcomp_builtin worktree_list
2771                         ;;
2772                 lock,--*)
2773                         __gitcomp_builtin worktree_lock
2774                         ;;
2775                 prune,--*)
2776                         __gitcomp_builtin worktree_prune
2777                         ;;
2778                 remove,--*)
2779                         __gitcomp "--force"
2780                         ;;
2781                 *)
2782                         ;;
2783                 esac
2784         fi
2785 }
2786
2787 __git_complete_common () {
2788         local command="$1"
2789
2790         case "$cur" in
2791         --*)
2792                 __gitcomp_builtin "$command"
2793                 ;;
2794         esac
2795 }
2796
2797 __git_cmds_with_parseopt_helper=
2798 __git_support_parseopt_helper () {
2799         test -n "$__git_cmds_with_parseopt_helper" ||
2800                 __git_cmds_with_parseopt_helper="$(__git --list-cmds=parseopt)"
2801
2802         case " $__git_cmds_with_parseopt_helper " in
2803         *" $1 "*)
2804                 return 0
2805                 ;;
2806         *)
2807                 return 1
2808                 ;;
2809         esac
2810 }
2811
2812 __git_complete_command () {
2813         local command="$1"
2814         local completion_func="_git_${command//-/_}"
2815         if ! declare -f $completion_func >/dev/null 2>/dev/null &&
2816                 declare -f _completion_loader >/dev/null 2>/dev/null
2817         then
2818                 _completion_loader "git-$command"
2819         fi
2820         if declare -f $completion_func >/dev/null 2>/dev/null
2821         then
2822                 $completion_func
2823                 return 0
2824         elif __git_support_parseopt_helper "$command"
2825         then
2826                 __git_complete_common "$command"
2827                 return 0
2828         else
2829                 return 1
2830         fi
2831 }
2832
2833 __git_main ()
2834 {
2835         local i c=1 command __git_dir __git_repo_path
2836         local __git_C_args C_args_count=0
2837
2838         while [ $c -lt $cword ]; do
2839                 i="${words[c]}"
2840                 case "$i" in
2841                 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
2842                 --git-dir)   ((c++)) ; __git_dir="${words[c]}" ;;
2843                 --bare)      __git_dir="." ;;
2844                 --help) command="help"; break ;;
2845                 -c|--work-tree|--namespace) ((c++)) ;;
2846                 -C)     __git_C_args[C_args_count++]=-C
2847                         ((c++))
2848                         __git_C_args[C_args_count++]="${words[c]}"
2849                         ;;
2850                 -*) ;;
2851                 *) command="$i"; break ;;
2852                 esac
2853                 ((c++))
2854         done
2855
2856         if [ -z "$command" ]; then
2857                 case "$prev" in
2858                 --git-dir|-C|--work-tree)
2859                         # these need a path argument, let's fall back to
2860                         # Bash filename completion
2861                         return
2862                         ;;
2863                 -c|--namespace)
2864                         # we don't support completing these options' arguments
2865                         return
2866                         ;;
2867                 esac
2868                 case "$cur" in
2869                 --*)   __gitcomp "
2870                         --paginate
2871                         --no-pager
2872                         --git-dir=
2873                         --bare
2874                         --version
2875                         --exec-path
2876                         --exec-path=
2877                         --html-path
2878                         --man-path
2879                         --info-path
2880                         --work-tree=
2881                         --namespace=
2882                         --no-replace-objects
2883                         --help
2884                         "
2885                         ;;
2886                 *)
2887                         if test -n "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
2888                         then
2889                                 __gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
2890                         else
2891                                 __gitcomp "$(git --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config)"
2892                         fi
2893                         ;;
2894                 esac
2895                 return
2896         fi
2897
2898         __git_complete_command "$command" && return
2899
2900         local expansion=$(__git_aliased_command "$command")
2901         if [ -n "$expansion" ]; then
2902                 words[1]=$expansion
2903                 __git_complete_command "$expansion"
2904         fi
2905 }
2906
2907 __gitk_main ()
2908 {
2909         __git_has_doubledash && return
2910
2911         local __git_repo_path
2912         __git_find_repo_path
2913
2914         local merge=""
2915         if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
2916                 merge="--merge"
2917         fi
2918         case "$cur" in
2919         --*)
2920                 __gitcomp "
2921                         $__git_log_common_options
2922                         $__git_log_gitk_options
2923                         $merge
2924                         "
2925                 return
2926                 ;;
2927         esac
2928         __git_complete_revlist
2929 }
2930
2931 if [[ -n ${ZSH_VERSION-} ]] &&
2932    # Don't define these functions when sourced from 'git-completion.zsh',
2933    # it has its own implementations.
2934    [[ -z ${GIT_SOURCING_ZSH_COMPLETION-} ]]; then
2935         echo "WARNING: this script is deprecated, please see git-completion.zsh" 1>&2
2936
2937         autoload -U +X compinit && compinit
2938
2939         __gitcomp ()
2940         {
2941                 emulate -L zsh
2942
2943                 local cur_="${3-$cur}"
2944
2945                 case "$cur_" in
2946                 --*=)
2947                         ;;
2948                 *)
2949                         local c IFS=$' \t\n'
2950                         local -a array
2951                         for c in ${=1}; do
2952                                 c="$c${4-}"
2953                                 case $c in
2954                                 --*=*|*.) ;;
2955                                 *) c="$c " ;;
2956                                 esac
2957                                 array[${#array[@]}+1]="$c"
2958                         done
2959                         compset -P '*[=:]'
2960                         compadd -Q -S '' -p "${2-}" -a -- array && _ret=0
2961                         ;;
2962                 esac
2963         }
2964
2965         __gitcomp_direct ()
2966         {
2967                 emulate -L zsh
2968
2969                 local IFS=$'\n'
2970                 compset -P '*[=:]'
2971                 compadd -Q -- ${=1} && _ret=0
2972         }
2973
2974         __gitcomp_nl ()
2975         {
2976                 emulate -L zsh
2977
2978                 local IFS=$'\n'
2979                 compset -P '*[=:]'
2980                 compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
2981         }
2982
2983         __gitcomp_file_direct ()
2984         {
2985                 emulate -L zsh
2986
2987                 local IFS=$'\n'
2988                 compset -P '*[=:]'
2989                 compadd -f -- ${=1} && _ret=0
2990         }
2991
2992         __gitcomp_file ()
2993         {
2994                 emulate -L zsh
2995
2996                 local IFS=$'\n'
2997                 compset -P '*[=:]'
2998                 compadd -p "${2-}" -f -- ${=1} && _ret=0
2999         }
3000
3001         _git ()
3002         {
3003                 local _ret=1 cur cword prev
3004                 cur=${words[CURRENT]}
3005                 prev=${words[CURRENT-1]}
3006                 let cword=CURRENT-1
3007                 emulate ksh -c __${service}_main
3008                 let _ret && _default && _ret=0
3009                 return _ret
3010         }
3011
3012         compdef _git git gitk
3013         return
3014 fi
3015
3016 __git_func_wrap ()
3017 {
3018         local cur words cword prev
3019         _get_comp_words_by_ref -n =: cur words cword prev
3020         $1
3021 }
3022
3023 # Setup completion for certain functions defined above by setting common
3024 # variables and workarounds.
3025 # This is NOT a public function; use at your own risk.
3026 __git_complete ()
3027 {
3028         local wrapper="__git_wrap${2}"
3029         eval "$wrapper () { __git_func_wrap $2 ; }"
3030         complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
3031                 || complete -o default -o nospace -F $wrapper $1
3032 }
3033
3034 # wrapper for backwards compatibility
3035 _git ()
3036 {
3037         __git_wrap__git_main
3038 }
3039
3040 # wrapper for backwards compatibility
3041 _gitk ()
3042 {
3043         __git_wrap__gitk_main
3044 }
3045
3046 __git_complete git __git_main
3047 __git_complete gitk __gitk_main
3048
3049 # The following are necessary only for Cygwin, and only are needed
3050 # when the user has tab-completed the executable name and consequently
3051 # included the '.exe' suffix.
3052 #
3053 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
3054 __git_complete git.exe __git_main
3055 fi