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