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