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