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