completion: add new __gitcompadd helper
[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 __gitcompadd ()
199 {
200         COMPREPLY=($(compgen -W "$1" -P "$2" -S "$4" -- "$3"))
201 }
202
203 # Generates completion reply with compgen, appending a space to possible
204 # completion words, if necessary.
205 # It accepts 1 to 4 arguments:
206 # 1: List of possible completion words.
207 # 2: A prefix to be added to each possible completion word (optional).
208 # 3: Generate possible completion matches for this word (optional).
209 # 4: A suffix to be appended to each possible completion word (optional).
210 __gitcomp ()
211 {
212         local cur_="${3-$cur}"
213
214         case "$cur_" in
215         --*=)
216                 ;;
217         *)
218                 local IFS=$'\n'
219                 __gitcompadd "$(__gitcomp_1 "${1-}" "${4-}")" "${2-}" "$cur_" ""
220                 ;;
221         esac
222 }
223
224 # Generates completion reply with compgen from newline-separated possible
225 # completion words by appending a space to all of them.
226 # It accepts 1 to 4 arguments:
227 # 1: List of possible completion words, separated by a single newline.
228 # 2: A prefix to be added to each possible completion word (optional).
229 # 3: Generate possible completion matches for this word (optional).
230 # 4: A suffix to be appended to each possible completion word instead of
231 #    the default space (optional).  If specified but empty, nothing is
232 #    appended.
233 __gitcomp_nl ()
234 {
235         local IFS=$'\n'
236         __gitcompadd "$1" "${2-}" "${3-$cur}" "${4- }"
237 }
238
239 # Generates completion reply with compgen from newline-separated possible
240 # completion filenames.
241 # It accepts 1 to 3 arguments:
242 # 1: List of possible completion filenames, separated by a single newline.
243 # 2: A directory prefix to be added to each possible completion filename
244 #    (optional).
245 # 3: Generate possible completion matches for this word (optional).
246 __gitcomp_file ()
247 {
248         local IFS=$'\n'
249
250         # XXX does not work when the directory prefix contains a tilde,
251         # since tilde expansion is not applied.
252         # This means that COMPREPLY will be empty and Bash default
253         # completion will be used.
254         COMPREPLY=($(compgen -P "${2-}" -W "$1" -- "${3-$cur}"))
255
256         # Tell Bash that compspec generates filenames.
257         compopt -o filenames 2>/dev/null
258 }
259
260 __git_index_file_list_filter_compat ()
261 {
262         local path
263
264         while read -r path; do
265                 case "$path" in
266                 ?*/*) echo "${path%%/*}/" ;;
267                 *) echo "$path" ;;
268                 esac
269         done
270 }
271
272 __git_index_file_list_filter_bash ()
273 {
274         local path
275
276         while read -r path; do
277                 case "$path" in
278                 ?*/*)
279                         # XXX if we append a slash to directory names when using
280                         # `compopt -o filenames`, Bash will append another slash.
281                         # This is pretty stupid, and this the reason why we have to
282                         # define a compatible version for this function.
283                         echo "${path%%/*}" ;;
284                 *)
285                         echo "$path" ;;
286                 esac
287         done
288 }
289
290 # Process path list returned by "ls-files" and "diff-index --name-only"
291 # commands, in order to list only file names relative to a specified
292 # directory, and append a slash to directory names.
293 __git_index_file_list_filter ()
294 {
295         # Default to Bash >= 4.x
296         __git_index_file_list_filter_bash
297 }
298
299 # Execute git ls-files, returning paths relative to the directory
300 # specified in the first argument, and using the options specified in
301 # the second argument.
302 __git_ls_files_helper ()
303 {
304         (
305                 test -n "${CDPATH+set}" && unset CDPATH
306                 # NOTE: $2 is not quoted in order to support multiple options
307                 cd "$1" && git ls-files --exclude-standard $2
308         ) 2>/dev/null
309 }
310
311
312 # Execute git diff-index, returning paths relative to the directory
313 # specified in the first argument, and using the tree object id
314 # specified in the second argument.
315 __git_diff_index_helper ()
316 {
317         (
318                 test -n "${CDPATH+set}" && unset CDPATH
319                 cd "$1" && git diff-index --name-only --relative "$2"
320         ) 2>/dev/null
321 }
322
323 # __git_index_files accepts 1 or 2 arguments:
324 # 1: Options to pass to ls-files (required).
325 #    Supported options are --cached, --modified, --deleted, --others,
326 #    and --directory.
327 # 2: A directory path (optional).
328 #    If provided, only files within the specified directory are listed.
329 #    Sub directories are never recursed.  Path must have a trailing
330 #    slash.
331 __git_index_files ()
332 {
333         local dir="$(__gitdir)" root="${2-.}"
334
335         if [ -d "$dir" ]; then
336                 __git_ls_files_helper "$root" "$1" | __git_index_file_list_filter |
337                         sort | uniq
338         fi
339 }
340
341 # __git_diff_index_files accepts 1 or 2 arguments:
342 # 1) The id of a tree object.
343 # 2) A directory path (optional).
344 #    If provided, only files within the specified directory are listed.
345 #    Sub directories are never recursed.  Path must have a trailing
346 #    slash.
347 __git_diff_index_files ()
348 {
349         local dir="$(__gitdir)" root="${2-.}"
350
351         if [ -d "$dir" ]; then
352                 __git_diff_index_helper "$root" "$1" | __git_index_file_list_filter |
353                         sort | uniq
354         fi
355 }
356
357 __git_heads ()
358 {
359         local dir="$(__gitdir)"
360         if [ -d "$dir" ]; then
361                 git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
362                         refs/heads
363                 return
364         fi
365 }
366
367 __git_tags ()
368 {
369         local dir="$(__gitdir)"
370         if [ -d "$dir" ]; then
371                 git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
372                         refs/tags
373                 return
374         fi
375 }
376
377 # __git_refs accepts 0, 1 (to pass to __gitdir), or 2 arguments
378 # presence of 2nd argument means use the guess heuristic employed
379 # by checkout for tracking branches
380 __git_refs ()
381 {
382         local i hash dir="$(__gitdir "${1-}")" track="${2-}"
383         local format refs
384         if [ -d "$dir" ]; then
385                 case "$cur" in
386                 refs|refs/*)
387                         format="refname"
388                         refs="${cur%/*}"
389                         track=""
390                         ;;
391                 *)
392                         for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD; do
393                                 if [ -e "$dir/$i" ]; then echo $i; fi
394                         done
395                         format="refname:short"
396                         refs="refs/tags refs/heads refs/remotes"
397                         ;;
398                 esac
399                 git --git-dir="$dir" for-each-ref --format="%($format)" \
400                         $refs
401                 if [ -n "$track" ]; then
402                         # employ the heuristic used by git checkout
403                         # Try to find a remote branch that matches the completion word
404                         # but only output if the branch name is unique
405                         local ref entry
406                         git --git-dir="$dir" for-each-ref --shell --format="ref=%(refname:short)" \
407                                 "refs/remotes/" | \
408                         while read -r entry; do
409                                 eval "$entry"
410                                 ref="${ref#*/}"
411                                 if [[ "$ref" == "$cur"* ]]; then
412                                         echo "$ref"
413                                 fi
414                         done | sort | uniq -u
415                 fi
416                 return
417         fi
418         case "$cur" in
419         refs|refs/*)
420                 git ls-remote "$dir" "$cur*" 2>/dev/null | \
421                 while read -r hash i; do
422                         case "$i" in
423                         *^{}) ;;
424                         *) echo "$i" ;;
425                         esac
426                 done
427                 ;;
428         *)
429                 git ls-remote "$dir" HEAD ORIG_HEAD 'refs/tags/*' 'refs/heads/*' 'refs/remotes/*' 2>/dev/null | \
430                 while read -r hash i; do
431                         case "$i" in
432                         *^{}) ;;
433                         refs/*) echo "${i#refs/*/}" ;;
434                         *) echo "$i" ;;
435                         esac
436                 done
437                 ;;
438         esac
439 }
440
441 # __git_refs2 requires 1 argument (to pass to __git_refs)
442 __git_refs2 ()
443 {
444         local i
445         for i in $(__git_refs "$1"); do
446                 echo "$i:$i"
447         done
448 }
449
450 # __git_refs_remotes requires 1 argument (to pass to ls-remote)
451 __git_refs_remotes ()
452 {
453         local i hash
454         git ls-remote "$1" 'refs/heads/*' 2>/dev/null | \
455         while read -r hash i; do
456                 echo "$i:refs/remotes/$1/${i#refs/heads/}"
457         done
458 }
459
460 __git_remotes ()
461 {
462         local i IFS=$'\n' d="$(__gitdir)"
463         test -d "$d/remotes" && ls -1 "$d/remotes"
464         for i in $(git --git-dir="$d" config --get-regexp 'remote\..*\.url' 2>/dev/null); do
465                 i="${i#remote.}"
466                 echo "${i/.url*/}"
467         done
468 }
469
470 __git_list_merge_strategies ()
471 {
472         git merge -s help 2>&1 |
473         sed -n -e '/[Aa]vailable strategies are: /,/^$/{
474                 s/\.$//
475                 s/.*://
476                 s/^[    ]*//
477                 s/[     ]*$//
478                 p
479         }'
480 }
481
482 __git_merge_strategies=
483 # 'git merge -s help' (and thus detection of the merge strategy
484 # list) fails, unfortunately, if run outside of any git working
485 # tree.  __git_merge_strategies is set to the empty string in
486 # that case, and the detection will be repeated the next time it
487 # is needed.
488 __git_compute_merge_strategies ()
489 {
490         test -n "$__git_merge_strategies" ||
491         __git_merge_strategies=$(__git_list_merge_strategies)
492 }
493
494 __git_complete_revlist_file ()
495 {
496         local pfx ls ref cur_="$cur"
497         case "$cur_" in
498         *..?*:*)
499                 return
500                 ;;
501         ?*:*)
502                 ref="${cur_%%:*}"
503                 cur_="${cur_#*:}"
504                 case "$cur_" in
505                 ?*/*)
506                         pfx="${cur_%/*}"
507                         cur_="${cur_##*/}"
508                         ls="$ref:$pfx"
509                         pfx="$pfx/"
510                         ;;
511                 *)
512                         ls="$ref"
513                         ;;
514                 esac
515
516                 case "$COMP_WORDBREAKS" in
517                 *:*) : great ;;
518                 *)   pfx="$ref:$pfx" ;;
519                 esac
520
521                 __gitcomp_nl "$(git --git-dir="$(__gitdir)" ls-tree "$ls" 2>/dev/null \
522                                 | sed '/^100... blob /{
523                                            s,^.*        ,,
524                                            s,$, ,
525                                        }
526                                        /^120000 blob /{
527                                            s,^.*        ,,
528                                            s,$, ,
529                                        }
530                                        /^040000 tree /{
531                                            s,^.*        ,,
532                                            s,$,/,
533                                        }
534                                        s/^.*    //')" \
535                         "$pfx" "$cur_" ""
536                 ;;
537         *...*)
538                 pfx="${cur_%...*}..."
539                 cur_="${cur_#*...}"
540                 __gitcomp_nl "$(__git_refs)" "$pfx" "$cur_"
541                 ;;
542         *..*)
543                 pfx="${cur_%..*}.."
544                 cur_="${cur_#*..}"
545                 __gitcomp_nl "$(__git_refs)" "$pfx" "$cur_"
546                 ;;
547         *)
548                 __gitcomp_nl "$(__git_refs)"
549                 ;;
550         esac
551 }
552
553
554 # __git_complete_index_file requires 1 argument: the options to pass to
555 # ls-file
556 __git_complete_index_file ()
557 {
558         local pfx cur_="$cur"
559
560         case "$cur_" in
561         ?*/*)
562                 pfx="${cur_%/*}"
563                 cur_="${cur_##*/}"
564                 pfx="${pfx}/"
565
566                 __gitcomp_file "$(__git_index_files "$1" "$pfx")" "$pfx" "$cur_"
567                 ;;
568         *)
569                 __gitcomp_file "$(__git_index_files "$1")" "" "$cur_"
570                 ;;
571         esac
572 }
573
574 # __git_complete_diff_index_file requires 1 argument: the id of a tree
575 # object
576 __git_complete_diff_index_file ()
577 {
578         local pfx cur_="$cur"
579
580         case "$cur_" in
581         ?*/*)
582                 pfx="${cur_%/*}"
583                 cur_="${cur_##*/}"
584                 pfx="${pfx}/"
585
586                 __gitcomp_file "$(__git_diff_index_files "$1" "$pfx")" "$pfx" "$cur_"
587                 ;;
588         *)
589                 __gitcomp_file "$(__git_diff_index_files "$1")" "" "$cur_"
590                 ;;
591         esac
592 }
593
594 __git_complete_file ()
595 {
596         __git_complete_revlist_file
597 }
598
599 __git_complete_revlist ()
600 {
601         __git_complete_revlist_file
602 }
603
604 __git_complete_remote_or_refspec ()
605 {
606         local cur_="$cur" cmd="${words[1]}"
607         local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
608         if [ "$cmd" = "remote" ]; then
609                 ((c++))
610         fi
611         while [ $c -lt $cword ]; do
612                 i="${words[c]}"
613                 case "$i" in
614                 --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
615                 --all)
616                         case "$cmd" in
617                         push) no_complete_refspec=1 ;;
618                         fetch)
619                                 return
620                                 ;;
621                         *) ;;
622                         esac
623                         ;;
624                 -*) ;;
625                 *) remote="$i"; break ;;
626                 esac
627                 ((c++))
628         done
629         if [ -z "$remote" ]; then
630                 __gitcomp_nl "$(__git_remotes)"
631                 return
632         fi
633         if [ $no_complete_refspec = 1 ]; then
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 }
955
956 _git_apply ()
957 {
958         case "$cur" in
959         --whitespace=*)
960                 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
961                 return
962                 ;;
963         --*)
964                 __gitcomp "
965                         --stat --numstat --summary --check --index
966                         --cached --index-info --reverse --reject --unidiff-zero
967                         --apply --no-add --exclude=
968                         --ignore-whitespace --ignore-space-change
969                         --whitespace= --inaccurate-eof --verbose
970                         "
971                 return
972         esac
973 }
974
975 _git_add ()
976 {
977         case "$cur" in
978         --*)
979                 __gitcomp "
980                         --interactive --refresh --patch --update --dry-run
981                         --ignore-errors --intent-to-add
982                         "
983                 return
984         esac
985
986         # XXX should we check for --update and --all options ?
987         __git_complete_index_file "--others --modified"
988 }
989
990 _git_archive ()
991 {
992         case "$cur" in
993         --format=*)
994                 __gitcomp "$(git archive --list)" "" "${cur##--format=}"
995                 return
996                 ;;
997         --remote=*)
998                 __gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
999                 return
1000                 ;;
1001         --*)
1002                 __gitcomp "
1003                         --format= --list --verbose
1004                         --prefix= --remote= --exec=
1005                         "
1006                 return
1007                 ;;
1008         esac
1009         __git_complete_file
1010 }
1011
1012 _git_bisect ()
1013 {
1014         __git_has_doubledash && return
1015
1016         local subcommands="start bad good skip reset visualize replay log run"
1017         local subcommand="$(__git_find_on_cmdline "$subcommands")"
1018         if [ -z "$subcommand" ]; then
1019                 if [ -f "$(__gitdir)"/BISECT_START ]; then
1020                         __gitcomp "$subcommands"
1021                 else
1022                         __gitcomp "replay start"
1023                 fi
1024                 return
1025         fi
1026
1027         case "$subcommand" in
1028         bad|good|reset|skip|start)
1029                 __gitcomp_nl "$(__git_refs)"
1030                 ;;
1031         *)
1032                 ;;
1033         esac
1034 }
1035
1036 _git_branch ()
1037 {
1038         local i c=1 only_local_ref="n" has_r="n"
1039
1040         while [ $c -lt $cword ]; do
1041                 i="${words[c]}"
1042                 case "$i" in
1043                 -d|-m)  only_local_ref="y" ;;
1044                 -r)     has_r="y" ;;
1045                 esac
1046                 ((c++))
1047         done
1048
1049         case "$cur" in
1050         --set-upstream-to=*)
1051                 __gitcomp "$(__git_refs)" "" "${cur##--set-upstream-to=}"
1052                 ;;
1053         --*)
1054                 __gitcomp "
1055                         --color --no-color --verbose --abbrev= --no-abbrev
1056                         --track --no-track --contains --merged --no-merged
1057                         --set-upstream-to= --edit-description --list
1058                         --unset-upstream
1059                         "
1060                 ;;
1061         *)
1062                 if [ $only_local_ref = "y" -a $has_r = "n" ]; then
1063                         __gitcomp_nl "$(__git_heads)"
1064                 else
1065                         __gitcomp_nl "$(__git_refs)"
1066                 fi
1067                 ;;
1068         esac
1069 }
1070
1071 _git_bundle ()
1072 {
1073         local cmd="${words[2]}"
1074         case "$cword" in
1075         2)
1076                 __gitcomp "create list-heads verify unbundle"
1077                 ;;
1078         3)
1079                 # looking for a file
1080                 ;;
1081         *)
1082                 case "$cmd" in
1083                         create)
1084                                 __git_complete_revlist
1085                         ;;
1086                 esac
1087                 ;;
1088         esac
1089 }
1090
1091 _git_checkout ()
1092 {
1093         __git_has_doubledash && return
1094
1095         case "$cur" in
1096         --conflict=*)
1097                 __gitcomp "diff3 merge" "" "${cur##--conflict=}"
1098                 ;;
1099         --*)
1100                 __gitcomp "
1101                         --quiet --ours --theirs --track --no-track --merge
1102                         --conflict= --orphan --patch
1103                         "
1104                 ;;
1105         *)
1106                 # check if --track, --no-track, or --no-guess was specified
1107                 # if so, disable DWIM mode
1108                 local flags="--track --no-track --no-guess" track=1
1109                 if [ -n "$(__git_find_on_cmdline "$flags")" ]; then
1110                         track=''
1111                 fi
1112                 __gitcomp_nl "$(__git_refs '' $track)"
1113                 ;;
1114         esac
1115 }
1116
1117 _git_cherry ()
1118 {
1119         __gitcomp "$(__git_refs)"
1120 }
1121
1122 _git_cherry_pick ()
1123 {
1124         local dir="$(__gitdir)"
1125         if [ -f "$dir"/CHERRY_PICK_HEAD ]; then
1126                 __gitcomp "--continue --quit --abort"
1127                 return
1128         fi
1129         case "$cur" in
1130         --*)
1131                 __gitcomp "--edit --no-commit --signoff --strategy= --mainline"
1132                 ;;
1133         *)
1134                 __gitcomp_nl "$(__git_refs)"
1135                 ;;
1136         esac
1137 }
1138
1139 _git_clean ()
1140 {
1141         case "$cur" in
1142         --*)
1143                 __gitcomp "--dry-run --quiet"
1144                 return
1145                 ;;
1146         esac
1147
1148         # XXX should we check for -x option ?
1149         __git_complete_index_file "--others"
1150 }
1151
1152 _git_clone ()
1153 {
1154         case "$cur" in
1155         --*)
1156                 __gitcomp "
1157                         --local
1158                         --no-hardlinks
1159                         --shared
1160                         --reference
1161                         --quiet
1162                         --no-checkout
1163                         --bare
1164                         --mirror
1165                         --origin
1166                         --upload-pack
1167                         --template=
1168                         --depth
1169                         --single-branch
1170                         --branch
1171                         "
1172                 return
1173                 ;;
1174         esac
1175 }
1176
1177 _git_commit ()
1178 {
1179         case "$prev" in
1180         -c|-C)
1181                 __gitcomp_nl "$(__git_refs)" "" "${cur}"
1182                 return
1183                 ;;
1184         esac
1185
1186         case "$prev" in
1187         -c|-C)
1188                 __gitcomp_nl "$(__git_refs)" "" "${cur}"
1189                 return
1190                 ;;
1191         esac
1192
1193         case "$cur" in
1194         --cleanup=*)
1195                 __gitcomp "default strip verbatim whitespace
1196                         " "" "${cur##--cleanup=}"
1197                 return
1198                 ;;
1199         --reuse-message=*|--reedit-message=*|\
1200         --fixup=*|--squash=*)
1201                 __gitcomp_nl "$(__git_refs)" "" "${cur#*=}"
1202                 return
1203                 ;;
1204         --untracked-files=*)
1205                 __gitcomp "all no normal" "" "${cur##--untracked-files=}"
1206                 return
1207                 ;;
1208         --*)
1209                 __gitcomp "
1210                         --all --author= --signoff --verify --no-verify
1211                         --edit --no-edit
1212                         --amend --include --only --interactive
1213                         --dry-run --reuse-message= --reedit-message=
1214                         --reset-author --file= --message= --template=
1215                         --cleanup= --untracked-files --untracked-files=
1216                         --verbose --quiet --fixup= --squash=
1217                         "
1218                 return
1219         esac
1220
1221         if git rev-parse --verify --quiet HEAD >/dev/null; then
1222                 __git_complete_diff_index_file "HEAD"
1223         else
1224                 # This is the first commit
1225                 __git_complete_index_file "--cached"
1226         fi
1227 }
1228
1229 _git_describe ()
1230 {
1231         case "$cur" in
1232         --*)
1233                 __gitcomp "
1234                         --all --tags --contains --abbrev= --candidates=
1235                         --exact-match --debug --long --match --always
1236                         "
1237                 return
1238         esac
1239         __gitcomp_nl "$(__git_refs)"
1240 }
1241
1242 __git_diff_algorithms="myers minimal patience histogram"
1243
1244 __git_diff_common_options="--stat --numstat --shortstat --summary
1245                         --patch-with-stat --name-only --name-status --color
1246                         --no-color --color-words --no-renames --check
1247                         --full-index --binary --abbrev --diff-filter=
1248                         --find-copies-harder
1249                         --text --ignore-space-at-eol --ignore-space-change
1250                         --ignore-all-space --exit-code --quiet --ext-diff
1251                         --no-ext-diff
1252                         --no-prefix --src-prefix= --dst-prefix=
1253                         --inter-hunk-context=
1254                         --patience --histogram --minimal
1255                         --raw
1256                         --dirstat --dirstat= --dirstat-by-file
1257                         --dirstat-by-file= --cumulative
1258                         --diff-algorithm=
1259 "
1260
1261 _git_diff ()
1262 {
1263         __git_has_doubledash && return
1264
1265         case "$cur" in
1266         --diff-algorithm=*)
1267                 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1268                 return
1269                 ;;
1270         --*)
1271                 __gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
1272                         --base --ours --theirs --no-index
1273                         $__git_diff_common_options
1274                         "
1275                 return
1276                 ;;
1277         esac
1278         __git_complete_revlist_file
1279 }
1280
1281 __git_mergetools_common="diffuse ecmerge emerge kdiff3 meld opendiff
1282                         tkdiff vimdiff gvimdiff xxdiff araxis p4merge bc3 codecompare
1283 "
1284
1285 _git_difftool ()
1286 {
1287         __git_has_doubledash && return
1288
1289         case "$cur" in
1290         --tool=*)
1291                 __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1292                 return
1293                 ;;
1294         --*)
1295                 __gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
1296                         --base --ours --theirs
1297                         --no-renames --diff-filter= --find-copies-harder
1298                         --relative --ignore-submodules
1299                         --tool="
1300                 return
1301                 ;;
1302         esac
1303         __git_complete_file
1304 }
1305
1306 __git_fetch_options="
1307         --quiet --verbose --append --upload-pack --force --keep --depth=
1308         --tags --no-tags --all --prune --dry-run
1309 "
1310
1311 _git_fetch ()
1312 {
1313         case "$cur" in
1314         --*)
1315                 __gitcomp "$__git_fetch_options"
1316                 return
1317                 ;;
1318         esac
1319         __git_complete_remote_or_refspec
1320 }
1321
1322 __git_format_patch_options="
1323         --stdout --attach --no-attach --thread --thread= --output-directory
1324         --numbered --start-number --numbered-files --keep-subject --signoff
1325         --signature --no-signature --in-reply-to= --cc= --full-index --binary
1326         --not --all --cover-letter --no-prefix --src-prefix= --dst-prefix=
1327         --inline --suffix= --ignore-if-in-upstream --subject-prefix=
1328 "
1329
1330 _git_format_patch ()
1331 {
1332         case "$cur" in
1333         --thread=*)
1334                 __gitcomp "
1335                         deep shallow
1336                         " "" "${cur##--thread=}"
1337                 return
1338                 ;;
1339         --*)
1340                 __gitcomp "$__git_format_patch_options"
1341                 return
1342                 ;;
1343         esac
1344         __git_complete_revlist
1345 }
1346
1347 _git_fsck ()
1348 {
1349         case "$cur" in
1350         --*)
1351                 __gitcomp "
1352                         --tags --root --unreachable --cache --no-reflogs --full
1353                         --strict --verbose --lost-found
1354                         "
1355                 return
1356                 ;;
1357         esac
1358 }
1359
1360 _git_gc ()
1361 {
1362         case "$cur" in
1363         --*)
1364                 __gitcomp "--prune --aggressive"
1365                 return
1366                 ;;
1367         esac
1368 }
1369
1370 _git_gitk ()
1371 {
1372         _gitk
1373 }
1374
1375 __git_match_ctag() {
1376         awk "/^${1////\\/}/ { print \$1 }" "$2"
1377 }
1378
1379 _git_grep ()
1380 {
1381         __git_has_doubledash && return
1382
1383         case "$cur" in
1384         --*)
1385                 __gitcomp "
1386                         --cached
1387                         --text --ignore-case --word-regexp --invert-match
1388                         --full-name --line-number
1389                         --extended-regexp --basic-regexp --fixed-strings
1390                         --perl-regexp
1391                         --files-with-matches --name-only
1392                         --files-without-match
1393                         --max-depth
1394                         --count
1395                         --and --or --not --all-match
1396                         "
1397                 return
1398                 ;;
1399         esac
1400
1401         case "$cword,$prev" in
1402         2,*|*,-*)
1403                 if test -r tags; then
1404                         __gitcomp_nl "$(__git_match_ctag "$cur" tags)"
1405                         return
1406                 fi
1407                 ;;
1408         esac
1409
1410         __gitcomp_nl "$(__git_refs)"
1411 }
1412
1413 _git_help ()
1414 {
1415         case "$cur" in
1416         --*)
1417                 __gitcomp "--all --info --man --web"
1418                 return
1419                 ;;
1420         esac
1421         __git_compute_all_commands
1422         __gitcomp "$__git_all_commands $(__git_aliases)
1423                 attributes cli core-tutorial cvs-migration
1424                 diffcore gitk glossary hooks ignore modules
1425                 namespaces repository-layout tutorial tutorial-2
1426                 workflows
1427                 "
1428 }
1429
1430 _git_init ()
1431 {
1432         case "$cur" in
1433         --shared=*)
1434                 __gitcomp "
1435                         false true umask group all world everybody
1436                         " "" "${cur##--shared=}"
1437                 return
1438                 ;;
1439         --*)
1440                 __gitcomp "--quiet --bare --template= --shared --shared="
1441                 return
1442                 ;;
1443         esac
1444 }
1445
1446 _git_ls_files ()
1447 {
1448         case "$cur" in
1449         --*)
1450                 __gitcomp "--cached --deleted --modified --others --ignored
1451                         --stage --directory --no-empty-directory --unmerged
1452                         --killed --exclude= --exclude-from=
1453                         --exclude-per-directory= --exclude-standard
1454                         --error-unmatch --with-tree= --full-name
1455                         --abbrev --ignored --exclude-per-directory
1456                         "
1457                 return
1458                 ;;
1459         esac
1460
1461         # XXX ignore options like --modified and always suggest all cached
1462         # files.
1463         __git_complete_index_file "--cached"
1464 }
1465
1466 _git_ls_remote ()
1467 {
1468         __gitcomp_nl "$(__git_remotes)"
1469 }
1470
1471 _git_ls_tree ()
1472 {
1473         __git_complete_file
1474 }
1475
1476 # Options that go well for log, shortlog and gitk
1477 __git_log_common_options="
1478         --not --all
1479         --branches --tags --remotes
1480         --first-parent --merges --no-merges
1481         --max-count=
1482         --max-age= --since= --after=
1483         --min-age= --until= --before=
1484         --min-parents= --max-parents=
1485         --no-min-parents --no-max-parents
1486 "
1487 # Options that go well for log and gitk (not shortlog)
1488 __git_log_gitk_options="
1489         --dense --sparse --full-history
1490         --simplify-merges --simplify-by-decoration
1491         --left-right --notes --no-notes
1492 "
1493 # Options that go well for log and shortlog (not gitk)
1494 __git_log_shortlog_options="
1495         --author= --committer= --grep=
1496         --all-match
1497 "
1498
1499 __git_log_pretty_formats="oneline short medium full fuller email raw format:"
1500 __git_log_date_formats="relative iso8601 rfc2822 short local default raw"
1501
1502 _git_log ()
1503 {
1504         __git_has_doubledash && return
1505
1506         local g="$(git rev-parse --git-dir 2>/dev/null)"
1507         local merge=""
1508         if [ -f "$g/MERGE_HEAD" ]; then
1509                 merge="--merge"
1510         fi
1511         case "$cur" in
1512         --pretty=*|--format=*)
1513                 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
1514                         " "" "${cur#*=}"
1515                 return
1516                 ;;
1517         --date=*)
1518                 __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
1519                 return
1520                 ;;
1521         --decorate=*)
1522                 __gitcomp "long short" "" "${cur##--decorate=}"
1523                 return
1524                 ;;
1525         --*)
1526                 __gitcomp "
1527                         $__git_log_common_options
1528                         $__git_log_shortlog_options
1529                         $__git_log_gitk_options
1530                         --root --topo-order --date-order --reverse
1531                         --follow --full-diff
1532                         --abbrev-commit --abbrev=
1533                         --relative-date --date=
1534                         --pretty= --format= --oneline
1535                         --cherry-pick
1536                         --graph
1537                         --decorate --decorate=
1538                         --walk-reflogs
1539                         --parents --children
1540                         $merge
1541                         $__git_diff_common_options
1542                         --pickaxe-all --pickaxe-regex
1543                         "
1544                 return
1545                 ;;
1546         esac
1547         __git_complete_revlist
1548 }
1549
1550 __git_merge_options="
1551         --no-commit --no-stat --log --no-log --squash --strategy
1552         --commit --stat --no-squash --ff --no-ff --ff-only --edit --no-edit
1553 "
1554
1555 _git_merge ()
1556 {
1557         __git_complete_strategy && return
1558
1559         case "$cur" in
1560         --*)
1561                 __gitcomp "$__git_merge_options"
1562                 return
1563         esac
1564         __gitcomp_nl "$(__git_refs)"
1565 }
1566
1567 _git_mergetool ()
1568 {
1569         case "$cur" in
1570         --tool=*)
1571                 __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
1572                 return
1573                 ;;
1574         --*)
1575                 __gitcomp "--tool="
1576                 return
1577                 ;;
1578         esac
1579 }
1580
1581 _git_merge_base ()
1582 {
1583         __gitcomp_nl "$(__git_refs)"
1584 }
1585
1586 _git_mv ()
1587 {
1588         case "$cur" in
1589         --*)
1590                 __gitcomp "--dry-run"
1591                 return
1592                 ;;
1593         esac
1594
1595         if [ $(__git_count_arguments "mv") -gt 0 ]; then
1596                 # We need to show both cached and untracked files (including
1597                 # empty directories) since this may not be the last argument.
1598                 __git_complete_index_file "--cached --others --directory"
1599         else
1600                 __git_complete_index_file "--cached"
1601         fi
1602 }
1603
1604 _git_name_rev ()
1605 {
1606         __gitcomp "--tags --all --stdin"
1607 }
1608
1609 _git_notes ()
1610 {
1611         local subcommands='add append copy edit list prune remove show'
1612         local subcommand="$(__git_find_on_cmdline "$subcommands")"
1613
1614         case "$subcommand,$cur" in
1615         ,--*)
1616                 __gitcomp '--ref'
1617                 ;;
1618         ,*)
1619                 case "$prev" in
1620                 --ref)
1621                         __gitcomp_nl "$(__git_refs)"
1622                         ;;
1623                 *)
1624                         __gitcomp "$subcommands --ref"
1625                         ;;
1626                 esac
1627                 ;;
1628         add,--reuse-message=*|append,--reuse-message=*|\
1629         add,--reedit-message=*|append,--reedit-message=*)
1630                 __gitcomp_nl "$(__git_refs)" "" "${cur#*=}"
1631                 ;;
1632         add,--*|append,--*)
1633                 __gitcomp '--file= --message= --reedit-message=
1634                                 --reuse-message='
1635                 ;;
1636         copy,--*)
1637                 __gitcomp '--stdin'
1638                 ;;
1639         prune,--*)
1640                 __gitcomp '--dry-run --verbose'
1641                 ;;
1642         prune,*)
1643                 ;;
1644         *)
1645                 case "$prev" in
1646                 -m|-F)
1647                         ;;
1648                 *)
1649                         __gitcomp_nl "$(__git_refs)"
1650                         ;;
1651                 esac
1652                 ;;
1653         esac
1654 }
1655
1656 _git_pull ()
1657 {
1658         __git_complete_strategy && return
1659
1660         case "$cur" in
1661         --*)
1662                 __gitcomp "
1663                         --rebase --no-rebase
1664                         $__git_merge_options
1665                         $__git_fetch_options
1666                 "
1667                 return
1668                 ;;
1669         esac
1670         __git_complete_remote_or_refspec
1671 }
1672
1673 _git_push ()
1674 {
1675         case "$prev" in
1676         --repo)
1677                 __gitcomp_nl "$(__git_remotes)"
1678                 return
1679         esac
1680         case "$cur" in
1681         --repo=*)
1682                 __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
1683                 return
1684                 ;;
1685         --*)
1686                 __gitcomp "
1687                         --all --mirror --tags --dry-run --force --verbose
1688                         --receive-pack= --repo= --set-upstream
1689                 "
1690                 return
1691                 ;;
1692         esac
1693         __git_complete_remote_or_refspec
1694 }
1695
1696 _git_rebase ()
1697 {
1698         local dir="$(__gitdir)"
1699         if [ -d "$dir"/rebase-apply ] || [ -d "$dir"/rebase-merge ]; then
1700                 __gitcomp "--continue --skip --abort"
1701                 return
1702         fi
1703         __git_complete_strategy && return
1704         case "$cur" in
1705         --whitespace=*)
1706                 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1707                 return
1708                 ;;
1709         --*)
1710                 __gitcomp "
1711                         --onto --merge --strategy --interactive
1712                         --preserve-merges --stat --no-stat
1713                         --committer-date-is-author-date --ignore-date
1714                         --ignore-whitespace --whitespace=
1715                         --autosquash
1716                         "
1717
1718                 return
1719         esac
1720         __gitcomp_nl "$(__git_refs)"
1721 }
1722
1723 _git_reflog ()
1724 {
1725         local subcommands="show delete expire"
1726         local subcommand="$(__git_find_on_cmdline "$subcommands")"
1727
1728         if [ -z "$subcommand" ]; then
1729                 __gitcomp "$subcommands"
1730         else
1731                 __gitcomp_nl "$(__git_refs)"
1732         fi
1733 }
1734
1735 __git_send_email_confirm_options="always never auto cc compose"
1736 __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
1737
1738 _git_send_email ()
1739 {
1740         case "$cur" in
1741         --confirm=*)
1742                 __gitcomp "
1743                         $__git_send_email_confirm_options
1744                         " "" "${cur##--confirm=}"
1745                 return
1746                 ;;
1747         --suppress-cc=*)
1748                 __gitcomp "
1749                         $__git_send_email_suppresscc_options
1750                         " "" "${cur##--suppress-cc=}"
1751
1752                 return
1753                 ;;
1754         --smtp-encryption=*)
1755                 __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
1756                 return
1757                 ;;
1758         --thread=*)
1759                 __gitcomp "
1760                         deep shallow
1761                         " "" "${cur##--thread=}"
1762                 return
1763                 ;;
1764         --*)
1765                 __gitcomp "--annotate --bcc --cc --cc-cmd --chain-reply-to
1766                         --compose --confirm= --dry-run --envelope-sender
1767                         --from --identity
1768                         --in-reply-to --no-chain-reply-to --no-signed-off-by-cc
1769                         --no-suppress-from --no-thread --quiet
1770                         --signed-off-by-cc --smtp-pass --smtp-server
1771                         --smtp-server-port --smtp-encryption= --smtp-user
1772                         --subject --suppress-cc= --suppress-from --thread --to
1773                         --validate --no-validate
1774                         $__git_format_patch_options"
1775                 return
1776                 ;;
1777         esac
1778         __git_complete_revlist
1779 }
1780
1781 _git_stage ()
1782 {
1783         _git_add
1784 }
1785
1786 __git_config_get_set_variables ()
1787 {
1788         local prevword word config_file= c=$cword
1789         while [ $c -gt 1 ]; do
1790                 word="${words[c]}"
1791                 case "$word" in
1792                 --system|--global|--local|--file=*)
1793                         config_file="$word"
1794                         break
1795                         ;;
1796                 -f|--file)
1797                         config_file="$word $prevword"
1798                         break
1799                         ;;
1800                 esac
1801                 prevword=$word
1802                 c=$((--c))
1803         done
1804
1805         git --git-dir="$(__gitdir)" config $config_file --list 2>/dev/null |
1806         while read -r line
1807         do
1808                 case "$line" in
1809                 *.*=*)
1810                         echo "${line/=*/}"
1811                         ;;
1812                 esac
1813         done
1814 }
1815
1816 _git_config ()
1817 {
1818         case "$prev" in
1819         branch.*.remote)
1820                 __gitcomp_nl "$(__git_remotes)"
1821                 return
1822                 ;;
1823         branch.*.merge)
1824                 __gitcomp_nl "$(__git_refs)"
1825                 return
1826                 ;;
1827         remote.*.fetch)
1828                 local remote="${prev#remote.}"
1829                 remote="${remote%.fetch}"
1830                 if [ -z "$cur" ]; then
1831                         __gitcompadd "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         help.format)
1867                 __gitcomp "man info web html"
1868                 return
1869                 ;;
1870         log.date)
1871                 __gitcomp "$__git_log_date_formats"
1872                 return
1873                 ;;
1874         sendemail.aliasesfiletype)
1875                 __gitcomp "mutt mailrc pine elm gnus"
1876                 return
1877                 ;;
1878         sendemail.confirm)
1879                 __gitcomp "$__git_send_email_confirm_options"
1880                 return
1881                 ;;
1882         sendemail.suppresscc)
1883                 __gitcomp "$__git_send_email_suppresscc_options"
1884                 return
1885                 ;;
1886         --get|--get-all|--unset|--unset-all)
1887                 __gitcomp_nl "$(__git_config_get_set_variables)"
1888                 return
1889                 ;;
1890         *.*)
1891                 return
1892                 ;;
1893         esac
1894         case "$cur" in
1895         --*)
1896                 __gitcomp "
1897                         --system --global --local --file=
1898                         --list --replace-all
1899                         --get --get-all --get-regexp
1900                         --add --unset --unset-all
1901                         --remove-section --rename-section
1902                         "
1903                 return
1904                 ;;
1905         branch.*.*)
1906                 local pfx="${cur%.*}." cur_="${cur##*.}"
1907                 __gitcomp "remote merge mergeoptions rebase" "$pfx" "$cur_"
1908                 return
1909                 ;;
1910         branch.*)
1911                 local pfx="${cur%.*}." cur_="${cur#*.}"
1912                 __gitcomp_nl "$(__git_heads)" "$pfx" "$cur_" "."
1913                 return
1914                 ;;
1915         guitool.*.*)
1916                 local pfx="${cur%.*}." cur_="${cur##*.}"
1917                 __gitcomp "
1918                         argprompt cmd confirm needsfile noconsole norescan
1919                         prompt revprompt revunmerged title
1920                         " "$pfx" "$cur_"
1921                 return
1922                 ;;
1923         difftool.*.*)
1924                 local pfx="${cur%.*}." cur_="${cur##*.}"
1925                 __gitcomp "cmd path" "$pfx" "$cur_"
1926                 return
1927                 ;;
1928         man.*.*)
1929                 local pfx="${cur%.*}." cur_="${cur##*.}"
1930                 __gitcomp "cmd path" "$pfx" "$cur_"
1931                 return
1932                 ;;
1933         mergetool.*.*)
1934                 local pfx="${cur%.*}." cur_="${cur##*.}"
1935                 __gitcomp "cmd path trustExitCode" "$pfx" "$cur_"
1936                 return
1937                 ;;
1938         pager.*)
1939                 local pfx="${cur%.*}." cur_="${cur#*.}"
1940                 __git_compute_all_commands
1941                 __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_"
1942                 return
1943                 ;;
1944         remote.*.*)
1945                 local pfx="${cur%.*}." cur_="${cur##*.}"
1946                 __gitcomp "
1947                         url proxy fetch push mirror skipDefaultUpdate
1948                         receivepack uploadpack tagopt pushurl
1949                         " "$pfx" "$cur_"
1950                 return
1951                 ;;
1952         remote.*)
1953                 local pfx="${cur%.*}." cur_="${cur#*.}"
1954                 __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
1955                 return
1956                 ;;
1957         url.*.*)
1958                 local pfx="${cur%.*}." cur_="${cur##*.}"
1959                 __gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_"
1960                 return
1961                 ;;
1962         esac
1963         __gitcomp "
1964                 add.ignoreErrors
1965                 advice.commitBeforeMerge
1966                 advice.detachedHead
1967                 advice.implicitIdentity
1968                 advice.pushNonFastForward
1969                 advice.resolveConflict
1970                 advice.statusHints
1971                 alias.
1972                 am.keepcr
1973                 apply.ignorewhitespace
1974                 apply.whitespace
1975                 branch.autosetupmerge
1976                 branch.autosetuprebase
1977                 browser.
1978                 clean.requireForce
1979                 color.branch
1980                 color.branch.current
1981                 color.branch.local
1982                 color.branch.plain
1983                 color.branch.remote
1984                 color.decorate.HEAD
1985                 color.decorate.branch
1986                 color.decorate.remoteBranch
1987                 color.decorate.stash
1988                 color.decorate.tag
1989                 color.diff
1990                 color.diff.commit
1991                 color.diff.frag
1992                 color.diff.func
1993                 color.diff.meta
1994                 color.diff.new
1995                 color.diff.old
1996                 color.diff.plain
1997                 color.diff.whitespace
1998                 color.grep
1999                 color.grep.context
2000                 color.grep.filename
2001                 color.grep.function
2002                 color.grep.linenumber
2003                 color.grep.match
2004                 color.grep.selected
2005                 color.grep.separator
2006                 color.interactive
2007                 color.interactive.error
2008                 color.interactive.header
2009                 color.interactive.help
2010                 color.interactive.prompt
2011                 color.pager
2012                 color.showbranch
2013                 color.status
2014                 color.status.added
2015                 color.status.changed
2016                 color.status.header
2017                 color.status.nobranch
2018                 color.status.untracked
2019                 color.status.updated
2020                 color.ui
2021                 commit.status
2022                 commit.template
2023                 core.abbrev
2024                 core.askpass
2025                 core.attributesfile
2026                 core.autocrlf
2027                 core.bare
2028                 core.bigFileThreshold
2029                 core.compression
2030                 core.createObject
2031                 core.deltaBaseCacheLimit
2032                 core.editor
2033                 core.eol
2034                 core.excludesfile
2035                 core.fileMode
2036                 core.fsyncobjectfiles
2037                 core.gitProxy
2038                 core.ignoreCygwinFSTricks
2039                 core.ignoreStat
2040                 core.ignorecase
2041                 core.logAllRefUpdates
2042                 core.loosecompression
2043                 core.notesRef
2044                 core.packedGitLimit
2045                 core.packedGitWindowSize
2046                 core.pager
2047                 core.preferSymlinkRefs
2048                 core.preloadindex
2049                 core.quotepath
2050                 core.repositoryFormatVersion
2051                 core.safecrlf
2052                 core.sharedRepository
2053                 core.sparseCheckout
2054                 core.symlinks
2055                 core.trustctime
2056                 core.warnAmbiguousRefs
2057                 core.whitespace
2058                 core.worktree
2059                 diff.autorefreshindex
2060                 diff.statGraphWidth
2061                 diff.external
2062                 diff.ignoreSubmodules
2063                 diff.mnemonicprefix
2064                 diff.noprefix
2065                 diff.renameLimit
2066                 diff.renames
2067                 diff.suppressBlankEmpty
2068                 diff.tool
2069                 diff.wordRegex
2070                 diff.algorithm
2071                 difftool.
2072                 difftool.prompt
2073                 fetch.recurseSubmodules
2074                 fetch.unpackLimit
2075                 format.attach
2076                 format.cc
2077                 format.headers
2078                 format.numbered
2079                 format.pretty
2080                 format.signature
2081                 format.signoff
2082                 format.subjectprefix
2083                 format.suffix
2084                 format.thread
2085                 format.to
2086                 gc.
2087                 gc.aggressiveWindow
2088                 gc.auto
2089                 gc.autopacklimit
2090                 gc.packrefs
2091                 gc.pruneexpire
2092                 gc.reflogexpire
2093                 gc.reflogexpireunreachable
2094                 gc.rerereresolved
2095                 gc.rerereunresolved
2096                 gitcvs.allbinary
2097                 gitcvs.commitmsgannotation
2098                 gitcvs.dbTableNamePrefix
2099                 gitcvs.dbdriver
2100                 gitcvs.dbname
2101                 gitcvs.dbpass
2102                 gitcvs.dbuser
2103                 gitcvs.enabled
2104                 gitcvs.logfile
2105                 gitcvs.usecrlfattr
2106                 guitool.
2107                 gui.blamehistoryctx
2108                 gui.commitmsgwidth
2109                 gui.copyblamethreshold
2110                 gui.diffcontext
2111                 gui.encoding
2112                 gui.fastcopyblame
2113                 gui.matchtrackingbranch
2114                 gui.newbranchtemplate
2115                 gui.pruneduringfetch
2116                 gui.spellingdictionary
2117                 gui.trustmtime
2118                 help.autocorrect
2119                 help.browser
2120                 help.format
2121                 http.lowSpeedLimit
2122                 http.lowSpeedTime
2123                 http.maxRequests
2124                 http.minSessions
2125                 http.noEPSV
2126                 http.postBuffer
2127                 http.proxy
2128                 http.sslCAInfo
2129                 http.sslCAPath
2130                 http.sslCert
2131                 http.sslCertPasswordProtected
2132                 http.sslKey
2133                 http.sslVerify
2134                 http.useragent
2135                 i18n.commitEncoding
2136                 i18n.logOutputEncoding
2137                 imap.authMethod
2138                 imap.folder
2139                 imap.host
2140                 imap.pass
2141                 imap.port
2142                 imap.preformattedHTML
2143                 imap.sslverify
2144                 imap.tunnel
2145                 imap.user
2146                 init.templatedir
2147                 instaweb.browser
2148                 instaweb.httpd
2149                 instaweb.local
2150                 instaweb.modulepath
2151                 instaweb.port
2152                 interactive.singlekey
2153                 log.date
2154                 log.decorate
2155                 log.showroot
2156                 mailmap.file
2157                 man.
2158                 man.viewer
2159                 merge.
2160                 merge.conflictstyle
2161                 merge.log
2162                 merge.renameLimit
2163                 merge.renormalize
2164                 merge.stat
2165                 merge.tool
2166                 merge.verbosity
2167                 mergetool.
2168                 mergetool.keepBackup
2169                 mergetool.keepTemporaries
2170                 mergetool.prompt
2171                 notes.displayRef
2172                 notes.rewrite.
2173                 notes.rewrite.amend
2174                 notes.rewrite.rebase
2175                 notes.rewriteMode
2176                 notes.rewriteRef
2177                 pack.compression
2178                 pack.deltaCacheLimit
2179                 pack.deltaCacheSize
2180                 pack.depth
2181                 pack.indexVersion
2182                 pack.packSizeLimit
2183                 pack.threads
2184                 pack.window
2185                 pack.windowMemory
2186                 pager.
2187                 pretty.
2188                 pull.octopus
2189                 pull.twohead
2190                 push.default
2191                 rebase.autosquash
2192                 rebase.stat
2193                 receive.autogc
2194                 receive.denyCurrentBranch
2195                 receive.denyDeleteCurrent
2196                 receive.denyDeletes
2197                 receive.denyNonFastForwards
2198                 receive.fsckObjects
2199                 receive.unpackLimit
2200                 receive.updateserverinfo
2201                 remotes.
2202                 repack.usedeltabaseoffset
2203                 rerere.autoupdate
2204                 rerere.enabled
2205                 sendemail.
2206                 sendemail.aliasesfile
2207                 sendemail.aliasfiletype
2208                 sendemail.bcc
2209                 sendemail.cc
2210                 sendemail.cccmd
2211                 sendemail.chainreplyto
2212                 sendemail.confirm
2213                 sendemail.envelopesender
2214                 sendemail.from
2215                 sendemail.identity
2216                 sendemail.multiedit
2217                 sendemail.signedoffbycc
2218                 sendemail.smtpdomain
2219                 sendemail.smtpencryption
2220                 sendemail.smtppass
2221                 sendemail.smtpserver
2222                 sendemail.smtpserveroption
2223                 sendemail.smtpserverport
2224                 sendemail.smtpuser
2225                 sendemail.suppresscc
2226                 sendemail.suppressfrom
2227                 sendemail.thread
2228                 sendemail.to
2229                 sendemail.validate
2230                 showbranch.default
2231                 status.relativePaths
2232                 status.showUntrackedFiles
2233                 status.submodulesummary
2234                 submodule.
2235                 tar.umask
2236                 transfer.unpackLimit
2237                 url.
2238                 user.email
2239                 user.name
2240                 user.signingkey
2241                 web.browser
2242                 branch. remote.
2243         "
2244 }
2245
2246 _git_remote ()
2247 {
2248         local subcommands="add rename remove set-head set-branches set-url show prune update"
2249         local subcommand="$(__git_find_on_cmdline "$subcommands")"
2250         if [ -z "$subcommand" ]; then
2251                 __gitcomp "$subcommands"
2252                 return
2253         fi
2254
2255         case "$subcommand" in
2256         rename|remove|set-url|show|prune)
2257                 __gitcomp_nl "$(__git_remotes)"
2258                 ;;
2259         set-head|set-branches)
2260                 __git_complete_remote_or_refspec
2261                 ;;
2262         update)
2263                 local i c='' IFS=$'\n'
2264                 for i in $(git --git-dir="$(__gitdir)" config --get-regexp "remotes\..*" 2>/dev/null); do
2265                         i="${i#remotes.}"
2266                         c="$c ${i/ */}"
2267                 done
2268                 __gitcomp "$c"
2269                 ;;
2270         *)
2271                 ;;
2272         esac
2273 }
2274
2275 _git_replace ()
2276 {
2277         __gitcomp_nl "$(__git_refs)"
2278 }
2279
2280 _git_reset ()
2281 {
2282         __git_has_doubledash && return
2283
2284         case "$cur" in
2285         --*)
2286                 __gitcomp "--merge --mixed --hard --soft --patch"
2287                 return
2288                 ;;
2289         esac
2290         __gitcomp_nl "$(__git_refs)"
2291 }
2292
2293 _git_revert ()
2294 {
2295         case "$cur" in
2296         --*)
2297                 __gitcomp "--edit --mainline --no-edit --no-commit --signoff"
2298                 return
2299                 ;;
2300         esac
2301         __gitcomp_nl "$(__git_refs)"
2302 }
2303
2304 _git_rm ()
2305 {
2306         case "$cur" in
2307         --*)
2308                 __gitcomp "--cached --dry-run --ignore-unmatch --quiet"
2309                 return
2310                 ;;
2311         esac
2312
2313         __git_complete_index_file "--cached"
2314 }
2315
2316 _git_shortlog ()
2317 {
2318         __git_has_doubledash && return
2319
2320         case "$cur" in
2321         --*)
2322                 __gitcomp "
2323                         $__git_log_common_options
2324                         $__git_log_shortlog_options
2325                         --numbered --summary
2326                         "
2327                 return
2328                 ;;
2329         esac
2330         __git_complete_revlist
2331 }
2332
2333 _git_show ()
2334 {
2335         __git_has_doubledash && return
2336
2337         case "$cur" in
2338         --pretty=*|--format=*)
2339                 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2340                         " "" "${cur#*=}"
2341                 return
2342                 ;;
2343         --diff-algorithm=*)
2344                 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
2345                 return
2346                 ;;
2347         --*)
2348                 __gitcomp "--pretty= --format= --abbrev-commit --oneline
2349                         $__git_diff_common_options
2350                         "
2351                 return
2352                 ;;
2353         esac
2354         __git_complete_file
2355 }
2356
2357 _git_show_branch ()
2358 {
2359         case "$cur" in
2360         --*)
2361                 __gitcomp "
2362                         --all --remotes --topo-order --current --more=
2363                         --list --independent --merge-base --no-name
2364                         --color --no-color
2365                         --sha1-name --sparse --topics --reflog
2366                         "
2367                 return
2368                 ;;
2369         esac
2370         __git_complete_revlist
2371 }
2372
2373 _git_stash ()
2374 {
2375         local save_opts='--keep-index --no-keep-index --quiet --patch'
2376         local subcommands='save list show apply clear drop pop create branch'
2377         local subcommand="$(__git_find_on_cmdline "$subcommands")"
2378         if [ -z "$subcommand" ]; then
2379                 case "$cur" in
2380                 --*)
2381                         __gitcomp "$save_opts"
2382                         ;;
2383                 *)
2384                         if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
2385                                 __gitcomp "$subcommands"
2386                         fi
2387                         ;;
2388                 esac
2389         else
2390                 case "$subcommand,$cur" in
2391                 save,--*)
2392                         __gitcomp "$save_opts"
2393                         ;;
2394                 apply,--*|pop,--*)
2395                         __gitcomp "--index --quiet"
2396                         ;;
2397                 show,--*|drop,--*|branch,--*)
2398                         ;;
2399                 show,*|apply,*|drop,*|pop,*|branch,*)
2400                         __gitcomp_nl "$(git --git-dir="$(__gitdir)" stash list \
2401                                         | sed -n -e 's/:.*//p')"
2402                         ;;
2403                 *)
2404                         ;;
2405                 esac
2406         fi
2407 }
2408
2409 _git_submodule ()
2410 {
2411         __git_has_doubledash && return
2412
2413         local subcommands="add status init deinit update summary foreach sync"
2414         if [ -z "$(__git_find_on_cmdline "$subcommands")" ]; then
2415                 case "$cur" in
2416                 --*)
2417                         __gitcomp "--quiet --cached"
2418                         ;;
2419                 *)
2420                         __gitcomp "$subcommands"
2421                         ;;
2422                 esac
2423                 return
2424         fi
2425 }
2426
2427 _git_svn ()
2428 {
2429         local subcommands="
2430                 init fetch clone rebase dcommit log find-rev
2431                 set-tree commit-diff info create-ignore propget
2432                 proplist show-ignore show-externals branch tag blame
2433                 migrate mkdirs reset gc
2434                 "
2435         local subcommand="$(__git_find_on_cmdline "$subcommands")"
2436         if [ -z "$subcommand" ]; then
2437                 __gitcomp "$subcommands"
2438         else
2439                 local remote_opts="--username= --config-dir= --no-auth-cache"
2440                 local fc_opts="
2441                         --follow-parent --authors-file= --repack=
2442                         --no-metadata --use-svm-props --use-svnsync-props
2443                         --log-window-size= --no-checkout --quiet
2444                         --repack-flags --use-log-author --localtime
2445                         --ignore-paths= $remote_opts
2446                         "
2447                 local init_opts="
2448                         --template= --shared= --trunk= --tags=
2449                         --branches= --stdlayout --minimize-url
2450                         --no-metadata --use-svm-props --use-svnsync-props
2451                         --rewrite-root= --prefix= --use-log-author
2452                         --add-author-from $remote_opts
2453                         "
2454                 local cmt_opts="
2455                         --edit --rmdir --find-copies-harder --copy-similarity=
2456                         "
2457
2458                 case "$subcommand,$cur" in
2459                 fetch,--*)
2460                         __gitcomp "--revision= --fetch-all $fc_opts"
2461                         ;;
2462                 clone,--*)
2463                         __gitcomp "--revision= $fc_opts $init_opts"
2464                         ;;
2465                 init,--*)
2466                         __gitcomp "$init_opts"
2467                         ;;
2468                 dcommit,--*)
2469                         __gitcomp "
2470                                 --merge --strategy= --verbose --dry-run
2471                                 --fetch-all --no-rebase --commit-url
2472                                 --revision --interactive $cmt_opts $fc_opts
2473                                 "
2474                         ;;
2475                 set-tree,--*)
2476                         __gitcomp "--stdin $cmt_opts $fc_opts"
2477                         ;;
2478                 create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
2479                 show-externals,--*|mkdirs,--*)
2480                         __gitcomp "--revision="
2481                         ;;
2482                 log,--*)
2483                         __gitcomp "
2484                                 --limit= --revision= --verbose --incremental
2485                                 --oneline --show-commit --non-recursive
2486                                 --authors-file= --color
2487                                 "
2488                         ;;
2489                 rebase,--*)
2490                         __gitcomp "
2491                                 --merge --verbose --strategy= --local
2492                                 --fetch-all --dry-run $fc_opts
2493                                 "
2494                         ;;
2495                 commit-diff,--*)
2496                         __gitcomp "--message= --file= --revision= $cmt_opts"
2497                         ;;
2498                 info,--*)
2499                         __gitcomp "--url"
2500                         ;;
2501                 branch,--*)
2502                         __gitcomp "--dry-run --message --tag"
2503                         ;;
2504                 tag,--*)
2505                         __gitcomp "--dry-run --message"
2506                         ;;
2507                 blame,--*)
2508                         __gitcomp "--git-format"
2509                         ;;
2510                 migrate,--*)
2511                         __gitcomp "
2512                                 --config-dir= --ignore-paths= --minimize
2513                                 --no-auth-cache --username=
2514                                 "
2515                         ;;
2516                 reset,--*)
2517                         __gitcomp "--revision= --parent"
2518                         ;;
2519                 *)
2520                         ;;
2521                 esac
2522         fi
2523 }
2524
2525 _git_tag ()
2526 {
2527         local i c=1 f=0
2528         while [ $c -lt $cword ]; do
2529                 i="${words[c]}"
2530                 case "$i" in
2531                 -d|-v)
2532                         __gitcomp_nl "$(__git_tags)"
2533                         return
2534                         ;;
2535                 -f)
2536                         f=1
2537                         ;;
2538                 esac
2539                 ((c++))
2540         done
2541
2542         case "$prev" in
2543         -m|-F)
2544                 ;;
2545         -*|tag)
2546                 if [ $f = 1 ]; then
2547                         __gitcomp_nl "$(__git_tags)"
2548                 fi
2549                 ;;
2550         *)
2551                 __gitcomp_nl "$(__git_refs)"
2552                 ;;
2553         esac
2554 }
2555
2556 _git_whatchanged ()
2557 {
2558         _git_log
2559 }
2560
2561 __git_main ()
2562 {
2563         local i c=1 command __git_dir
2564
2565         while [ $c -lt $cword ]; do
2566                 i="${words[c]}"
2567                 case "$i" in
2568                 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
2569                 --bare)      __git_dir="." ;;
2570                 --help) command="help"; break ;;
2571                 -c) c=$((++c)) ;;
2572                 -*) ;;
2573                 *) command="$i"; break ;;
2574                 esac
2575                 ((c++))
2576         done
2577
2578         if [ -z "$command" ]; then
2579                 case "$cur" in
2580                 --*)   __gitcomp "
2581                         --paginate
2582                         --no-pager
2583                         --git-dir=
2584                         --bare
2585                         --version
2586                         --exec-path
2587                         --exec-path=
2588                         --html-path
2589                         --info-path
2590                         --work-tree=
2591                         --namespace=
2592                         --no-replace-objects
2593                         --help
2594                         "
2595                         ;;
2596                 *)     __git_compute_porcelain_commands
2597                        __gitcomp "$__git_porcelain_commands $(__git_aliases)" ;;
2598                 esac
2599                 return
2600         fi
2601
2602         local completion_func="_git_${command//-/_}"
2603         declare -f $completion_func >/dev/null && $completion_func && return
2604
2605         local expansion=$(__git_aliased_command "$command")
2606         if [ -n "$expansion" ]; then
2607                 completion_func="_git_${expansion//-/_}"
2608                 declare -f $completion_func >/dev/null && $completion_func
2609         fi
2610 }
2611
2612 __gitk_main ()
2613 {
2614         __git_has_doubledash && return
2615
2616         local g="$(__gitdir)"
2617         local merge=""
2618         if [ -f "$g/MERGE_HEAD" ]; then
2619                 merge="--merge"
2620         fi
2621         case "$cur" in
2622         --*)
2623                 __gitcomp "
2624                         $__git_log_common_options
2625                         $__git_log_gitk_options
2626                         $merge
2627                         "
2628                 return
2629                 ;;
2630         esac
2631         __git_complete_revlist
2632 }
2633
2634 if [[ -n ${ZSH_VERSION-} ]]; then
2635         echo "WARNING: this script is deprecated, please see git-completion.zsh" 1>&2
2636
2637         autoload -U +X compinit && compinit
2638
2639         __gitcomp ()
2640         {
2641                 emulate -L zsh
2642
2643                 local cur_="${3-$cur}"
2644
2645                 case "$cur_" in
2646                 --*=)
2647                         ;;
2648                 *)
2649                         local c IFS=$' \t\n'
2650                         local -a array
2651                         for c in ${=1}; do
2652                                 c="$c${4-}"
2653                                 case $c in
2654                                 --*=*|*.) ;;
2655                                 *) c="$c " ;;
2656                                 esac
2657                                 array[$#array+1]="$c"
2658                         done
2659                         compset -P '*[=:]'
2660                         compadd -Q -S '' -p "${2-}" -a -- array && _ret=0
2661                         ;;
2662                 esac
2663         }
2664
2665         __gitcomp_nl ()
2666         {
2667                 emulate -L zsh
2668
2669                 local IFS=$'\n'
2670                 compset -P '*[=:]'
2671                 compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
2672         }
2673
2674         __gitcomp_file ()
2675         {
2676                 emulate -L zsh
2677
2678                 local IFS=$'\n'
2679                 compset -P '*[=:]'
2680                 compadd -Q -p "${2-}" -f -- ${=1} && _ret=0
2681         }
2682
2683         __git_zsh_helper ()
2684         {
2685                 emulate -L ksh
2686                 local cur cword prev
2687                 cur=${words[CURRENT-1]}
2688                 prev=${words[CURRENT-2]}
2689                 let cword=CURRENT-1
2690                 __${service}_main
2691         }
2692
2693         _git ()
2694         {
2695                 emulate -L zsh
2696                 local _ret=1
2697                 __git_zsh_helper
2698                 let _ret && _default -S '' && _ret=0
2699                 return _ret
2700         }
2701
2702         compdef _git git gitk
2703         return
2704 elif [[ -n ${BASH_VERSION-} ]]; then
2705         if ((${BASH_VERSINFO[0]} < 4)); then
2706                 # compopt is not supported
2707                 __git_index_file_list_filter ()
2708                 {
2709                         __git_index_file_list_filter_compat
2710                 }
2711         fi
2712 fi
2713
2714 __git_func_wrap ()
2715 {
2716         local cur words cword prev
2717         _get_comp_words_by_ref -n =: cur words cword prev
2718         $1
2719 }
2720
2721 # Setup completion for certain functions defined above by setting common
2722 # variables and workarounds.
2723 # This is NOT a public function; use at your own risk.
2724 __git_complete ()
2725 {
2726         local wrapper="__git_wrap${2}"
2727         eval "$wrapper () { __git_func_wrap $2 ; }"
2728         complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
2729                 || complete -o default -o nospace -F $wrapper $1
2730 }
2731
2732 # wrapper for backwards compatibility
2733 _git ()
2734 {
2735         __git_wrap__git_main
2736 }
2737
2738 # wrapper for backwards compatibility
2739 _gitk ()
2740 {
2741         __git_wrap__gitk_main
2742 }
2743
2744 __git_complete git __git_main
2745 __git_complete gitk __gitk_main
2746
2747 # The following are necessary only for Cygwin, and only are needed
2748 # when the user has tab-completed the executable name and consequently
2749 # included the '.exe' suffix.
2750 #
2751 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
2752 __git_complete git.exe __git_main
2753 fi