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