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