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