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