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