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