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