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