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