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