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