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