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