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