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