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