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