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