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