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