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