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