git-completion.bash: zsh does not implement function redirection correctly
[git.git] / contrib / completion / git-completion.bash
blob430566d33623b3c95359b2ae9fd958ab29dbe743
1 #!bash
3 # bash/zsh completion support for core Git.
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.
9 # The contained completion routines provide support for completing:
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 # *) file paths within current working directory and index
17 # *) common --long-options
19 # To use these routines:
21 # 1) Copy this file to somewhere (e.g. ~/.git-completion.sh).
22 # 2) Add the following line to your .bashrc/.zshrc:
23 # source ~/.git-completion.sh
24 # 3) Consider changing your PS1 to also show the current branch,
25 # see git-prompt.sh for details.
27 case "$COMP_WORDBREAKS" in
28 *:*) : great ;;
29 *) COMP_WORDBREAKS="$COMP_WORDBREAKS:"
30 esac
32 # __gitdir accepts 0 or 1 arguments (i.e., location)
33 # returns location of .git repo
34 __gitdir ()
36 # Note: this function is duplicated in git-prompt.sh
37 # When updating it, make sure you update the other one to match.
38 if [ -z "${1-}" ]; then
39 if [ -n "${__git_dir-}" ]; then
40 echo "$__git_dir"
41 elif [ -n "${GIT_DIR-}" ]; then
42 test -d "${GIT_DIR-}" || return 1
43 echo "$GIT_DIR"
44 elif [ -d .git ]; then
45 echo .git
46 else
47 git rev-parse --git-dir 2>/dev/null
49 elif [ -d "$1/.git" ]; then
50 echo "$1/.git"
51 else
52 echo "$1"
56 __gitcomp_1 ()
58 local c IFS=$' \t\n'
59 for c in $1; do
60 c="$c$2"
61 case $c in
62 --*=*|*.) ;;
63 *) c="$c " ;;
64 esac
65 printf '%s\n' "$c"
66 done
69 # The following function is based on code from:
71 # bash_completion - programmable completion functions for bash 3.2+
73 # Copyright © 2006-2008, Ian Macdonald <ian@caliban.org>
74 # © 2009-2010, Bash Completion Maintainers
75 # <bash-completion-devel@lists.alioth.debian.org>
77 # This program is free software; you can redistribute it and/or modify
78 # it under the terms of the GNU General Public License as published by
79 # the Free Software Foundation; either version 2, or (at your option)
80 # any later version.
82 # This program is distributed in the hope that it will be useful,
83 # but WITHOUT ANY WARRANTY; without even the implied warranty of
84 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
85 # GNU General Public License for more details.
87 # You should have received a copy of the GNU General Public License
88 # along with this program; if not, write to the Free Software Foundation,
89 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
91 # The latest version of this software can be obtained here:
93 # http://bash-completion.alioth.debian.org/
95 # RELEASE: 2.x
97 # This function can be used to access a tokenized list of words
98 # on the command line:
100 # __git_reassemble_comp_words_by_ref '=:'
101 # if test "${words_[cword_-1]}" = -w
102 # then
103 # ...
104 # fi
106 # The argument should be a collection of characters from the list of
107 # word completion separators (COMP_WORDBREAKS) to treat as ordinary
108 # characters.
110 # This is roughly equivalent to going back in time and setting
111 # COMP_WORDBREAKS to exclude those characters. The intent is to
112 # make option types like --date=<type> and <rev>:<path> easy to
113 # recognize by treating each shell word as a single token.
115 # It is best not to set COMP_WORDBREAKS directly because the value is
116 # shared with other completion scripts. By the time the completion
117 # function gets called, COMP_WORDS has already been populated so local
118 # changes to COMP_WORDBREAKS have no effect.
120 # Output: words_, cword_, cur_.
122 __git_reassemble_comp_words_by_ref()
124 local exclude i j first
125 # Which word separators to exclude?
126 exclude="${1//[^$COMP_WORDBREAKS]}"
127 cword_=$COMP_CWORD
128 if [ -z "$exclude" ]; then
129 words_=("${COMP_WORDS[@]}")
130 return
132 # List of word completion separators has shrunk;
133 # re-assemble words to complete.
134 for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
135 # Append each nonempty word consisting of just
136 # word separator characters to the current word.
137 first=t
138 while
139 [ $i -gt 0 ] &&
140 [ -n "${COMP_WORDS[$i]}" ] &&
141 # word consists of excluded word separators
142 [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
144 # Attach to the previous token,
145 # unless the previous token is the command name.
146 if [ $j -ge 2 ] && [ -n "$first" ]; then
147 ((j--))
149 first=
150 words_[$j]=${words_[j]}${COMP_WORDS[i]}
151 if [ $i = $COMP_CWORD ]; then
152 cword_=$j
154 if (($i < ${#COMP_WORDS[@]} - 1)); then
155 ((i++))
156 else
157 # Done.
158 return
160 done
161 words_[$j]=${words_[j]}${COMP_WORDS[i]}
162 if [ $i = $COMP_CWORD ]; then
163 cword_=$j
165 done
168 if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
169 _get_comp_words_by_ref ()
171 local exclude cur_ words_ cword_
172 if [ "$1" = "-n" ]; then
173 exclude=$2
174 shift 2
176 __git_reassemble_comp_words_by_ref "$exclude"
177 cur_=${words_[cword_]}
178 while [ $# -gt 0 ]; do
179 case "$1" in
180 cur)
181 cur=$cur_
183 prev)
184 prev=${words_[$cword_-1]}
186 words)
187 words=("${words_[@]}")
189 cword)
190 cword=$cword_
192 esac
193 shift
194 done
198 # Generates completion reply with compgen, appending a space to possible
199 # completion words, if necessary.
200 # It accepts 1 to 4 arguments:
201 # 1: List of possible completion words.
202 # 2: A prefix to be added to each possible completion word (optional).
203 # 3: Generate possible completion matches for this word (optional).
204 # 4: A suffix to be appended to each possible completion word (optional).
205 __gitcomp ()
207 local cur_="${3-$cur}"
209 case "$cur_" in
210 --*=)
211 COMPREPLY=()
214 local IFS=$'\n'
215 COMPREPLY=($(compgen -P "${2-}" \
216 -W "$(__gitcomp_1 "${1-}" "${4-}")" \
217 -- "$cur_"))
219 esac
222 # Generates completion reply with compgen from newline-separated possible
223 # completion words by appending a space to all of them.
224 # It accepts 1 to 4 arguments:
225 # 1: List of possible completion words, separated by a single newline.
226 # 2: A prefix to be added to each possible completion word (optional).
227 # 3: Generate possible completion matches for this word (optional).
228 # 4: A suffix to be appended to each possible completion word instead of
229 # the default space (optional). If specified but empty, nothing is
230 # appended.
231 __gitcomp_nl ()
233 local IFS=$'\n'
234 COMPREPLY=($(compgen -P "${2-}" -S "${4- }" -W "$1" -- "${3-$cur}"))
237 # Generates completion reply with compgen from newline-separated possible
238 # completion filenames.
239 # It accepts 1 to 3 arguments:
240 # 1: List of possible completion filenames, separated by a single newline.
241 # 2: A directory prefix to be added to each possible completion filename
242 # (optional).
243 # 3: Generate possible completion matches for this word (optional).
244 __gitcomp_file ()
246 local IFS=$'\n'
248 # XXX does not work when the directory prefix contains a tilde,
249 # since tilde expansion is not applied.
250 # This means that COMPREPLY will be empty and Bash default
251 # completion will be used.
252 COMPREPLY=($(compgen -P "${2-}" -W "$1" -- "${3-$cur}"))
254 # Tell Bash that compspec generates filenames.
255 compopt -o filenames 2>/dev/null
258 __git_index_file_list_filter_compat ()
260 local path
262 while read -r path; do
263 case "$path" in
264 ?*/*) echo "${path%%/*}/" ;;
265 *) echo "$path" ;;
266 esac
267 done
270 __git_index_file_list_filter_bash ()
272 local path
274 while read -r path; do
275 case "$path" in
276 ?*/*)
277 # XXX if we append a slash to directory names when using
278 # `compopt -o filenames`, Bash will append another slash.
279 # This is pretty stupid, and this the reason why we have to
280 # define a compatible version for this function.
281 echo "${path%%/*}" ;;
283 echo "$path" ;;
284 esac
285 done
288 # Process path list returned by "ls-files" and "diff-index --name-only"
289 # commands, in order to list only file names relative to a specified
290 # directory, and append a slash to directory names.
291 __git_index_file_list_filter ()
293 # Default to Bash >= 4.x
294 __git_index_file_list_filter_bash
297 # Execute git ls-files, returning paths relative to the directory
298 # specified in the first argument, and using the options specified in
299 # the second argument.
300 __git_ls_files_helper ()
303 test -n "${CDPATH+set}" && unset CDPATH
304 # NOTE: $2 is not quoted in order to support multiple options
305 cd "$1" && git ls-files --exclude-standard $2
306 ) 2>/dev/null
310 # Execute git diff-index, returning paths relative to the directory
311 # specified in the first argument, and using the tree object id
312 # specified in the second argument.
313 __git_diff_index_helper ()
316 test -n "${CDPATH+set}" && unset CDPATH
317 cd "$1" && git diff-index --name-only --relative "$2"
318 ) 2>/dev/null
321 # __git_index_files accepts 1 or 2 arguments:
322 # 1: Options to pass to ls-files (required).
323 # Supported options are --cached, --modified, --deleted, --others,
324 # and --directory.
325 # 2: A directory path (optional).
326 # If provided, only files within the specified directory are listed.
327 # Sub directories are never recursed. Path must have a trailing
328 # slash.
329 __git_index_files ()
331 local dir="$(__gitdir)" root="${2-.}"
333 if [ -d "$dir" ]; then
334 __git_ls_files_helper "$root" "$1" | __git_index_file_list_filter |
335 sort | uniq
339 # __git_diff_index_files accepts 1 or 2 arguments:
340 # 1) The id of a tree object.
341 # 2) A directory path (optional).
342 # If provided, only files within the specified directory are listed.
343 # Sub directories are never recursed. Path must have a trailing
344 # slash.
345 __git_diff_index_files ()
347 local dir="$(__gitdir)" root="${2-.}"
349 if [ -d "$dir" ]; then
350 __git_diff_index_helper "$root" "$1" | __git_index_file_list_filter |
351 sort | uniq
355 __git_heads ()
357 local dir="$(__gitdir)"
358 if [ -d "$dir" ]; then
359 git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
360 refs/heads
361 return
365 __git_tags ()
367 local dir="$(__gitdir)"
368 if [ -d "$dir" ]; then
369 git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
370 refs/tags
371 return
375 # __git_refs accepts 0, 1 (to pass to __gitdir), or 2 arguments
376 # presence of 2nd argument means use the guess heuristic employed
377 # by checkout for tracking branches
378 __git_refs ()
380 local i hash dir="$(__gitdir "${1-}")" track="${2-}"
381 local format refs
382 if [ -d "$dir" ]; then
383 case "$cur" in
384 refs|refs/*)
385 format="refname"
386 refs="${cur%/*}"
387 track=""
390 for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD; do
391 if [ -e "$dir/$i" ]; then echo $i; fi
392 done
393 format="refname:short"
394 refs="refs/tags refs/heads refs/remotes"
396 esac
397 git --git-dir="$dir" for-each-ref --format="%($format)" \
398 $refs
399 if [ -n "$track" ]; then
400 # employ the heuristic used by git checkout
401 # Try to find a remote branch that matches the completion word
402 # but only output if the branch name is unique
403 local ref entry
404 git --git-dir="$dir" for-each-ref --shell --format="ref=%(refname:short)" \
405 "refs/remotes/" | \
406 while read -r entry; do
407 eval "$entry"
408 ref="${ref#*/}"
409 if [[ "$ref" == "$cur"* ]]; then
410 echo "$ref"
412 done | sort | uniq -u
414 return
416 case "$cur" in
417 refs|refs/*)
418 git ls-remote "$dir" "$cur*" 2>/dev/null | \
419 while read -r hash i; do
420 case "$i" in
421 *^{}) ;;
422 *) echo "$i" ;;
423 esac
424 done
427 git ls-remote "$dir" HEAD ORIG_HEAD 'refs/tags/*' 'refs/heads/*' 'refs/remotes/*' 2>/dev/null | \
428 while read -r hash i; do
429 case "$i" in
430 *^{}) ;;
431 refs/*) echo "${i#refs/*/}" ;;
432 *) echo "$i" ;;
433 esac
434 done
436 esac
439 # __git_refs2 requires 1 argument (to pass to __git_refs)
440 __git_refs2 ()
442 local i
443 for i in $(__git_refs "$1"); do
444 echo "$i:$i"
445 done
448 # __git_refs_remotes requires 1 argument (to pass to ls-remote)
449 __git_refs_remotes ()
451 local i hash
452 git ls-remote "$1" 'refs/heads/*' 2>/dev/null | \
453 while read -r hash i; do
454 echo "$i:refs/remotes/$1/${i#refs/heads/}"
455 done
458 __git_remotes ()
460 local i IFS=$'\n' d="$(__gitdir)"
461 test -d "$d/remotes" && ls -1 "$d/remotes"
462 for i in $(git --git-dir="$d" config --get-regexp 'remote\..*\.url' 2>/dev/null); do
463 i="${i#remote.}"
464 echo "${i/.url*/}"
465 done
468 __git_list_merge_strategies ()
470 git merge -s help 2>&1 |
471 sed -n -e '/[Aa]vailable strategies are: /,/^$/{
472 s/\.$//
473 s/.*://
474 s/^[ ]*//
475 s/[ ]*$//
480 __git_merge_strategies=
481 # 'git merge -s help' (and thus detection of the merge strategy
482 # list) fails, unfortunately, if run outside of any git working
483 # tree. __git_merge_strategies is set to the empty string in
484 # that case, and the detection will be repeated the next time it
485 # is needed.
486 __git_compute_merge_strategies ()
488 test -n "$__git_merge_strategies" ||
489 __git_merge_strategies=$(__git_list_merge_strategies)
492 __git_complete_revlist_file ()
494 local pfx ls ref cur_="$cur"
495 case "$cur_" in
496 *..?*:*)
497 return
499 ?*:*)
500 ref="${cur_%%:*}"
501 cur_="${cur_#*:}"
502 case "$cur_" in
503 ?*/*)
504 pfx="${cur_%/*}"
505 cur_="${cur_##*/}"
506 ls="$ref:$pfx"
507 pfx="$pfx/"
510 ls="$ref"
512 esac
514 case "$COMP_WORDBREAKS" in
515 *:*) : great ;;
516 *) pfx="$ref:$pfx" ;;
517 esac
519 __gitcomp_nl "$(git --git-dir="$(__gitdir)" ls-tree "$ls" \
520 | sed '/^100... blob /{
521 s,^.* ,,
522 s,$, ,
524 /^120000 blob /{
525 s,^.* ,,
526 s,$, ,
528 /^040000 tree /{
529 s,^.* ,,
530 s,$,/,
532 s/^.* //')" \
533 "$pfx" "$cur_" ""
535 *...*)
536 pfx="${cur_%...*}..."
537 cur_="${cur_#*...}"
538 __gitcomp_nl "$(__git_refs)" "$pfx" "$cur_"
540 *..*)
541 pfx="${cur_%..*}.."
542 cur_="${cur_#*..}"
543 __gitcomp_nl "$(__git_refs)" "$pfx" "$cur_"
546 __gitcomp_nl "$(__git_refs)"
548 esac
552 # __git_complete_index_file requires 1 argument: the options to pass to
553 # ls-file
554 __git_complete_index_file ()
556 local pfx cur_="$cur"
558 case "$cur_" in
559 ?*/*)
560 pfx="${cur_%/*}"
561 cur_="${cur_##*/}"
562 pfx="${pfx}/"
564 __gitcomp_file "$(__git_index_files "$1" "$pfx")" "$pfx" "$cur_"
567 __gitcomp_file "$(__git_index_files "$1")" "" "$cur_"
569 esac
572 # __git_complete_diff_index_file requires 1 argument: the id of a tree
573 # object
574 __git_complete_diff_index_file ()
576 local pfx cur_="$cur"
578 case "$cur_" in
579 ?*/*)
580 pfx="${cur_%/*}"
581 cur_="${cur_##*/}"
582 pfx="${pfx}/"
584 __gitcomp_file "$(__git_diff_index_files "$1" "$pfx")" "$pfx" "$cur_"
587 __gitcomp_file "$(__git_diff_index_files "$1")" "" "$cur_"
589 esac
592 __git_complete_file ()
594 __git_complete_revlist_file
597 __git_complete_revlist ()
599 __git_complete_revlist_file
602 __git_complete_remote_or_refspec ()
604 local cur_="$cur" cmd="${words[1]}"
605 local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
606 if [ "$cmd" = "remote" ]; then
607 ((c++))
609 while [ $c -lt $cword ]; do
610 i="${words[c]}"
611 case "$i" in
612 --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
613 --all)
614 case "$cmd" in
615 push) no_complete_refspec=1 ;;
616 fetch)
617 COMPREPLY=()
618 return
620 *) ;;
621 esac
623 -*) ;;
624 *) remote="$i"; break ;;
625 esac
626 ((c++))
627 done
628 if [ -z "$remote" ]; then
629 __gitcomp_nl "$(__git_remotes)"
630 return
632 if [ $no_complete_refspec = 1 ]; then
633 COMPREPLY=()
634 return
636 [ "$remote" = "." ] && remote=
637 case "$cur_" in
638 *:*)
639 case "$COMP_WORDBREAKS" in
640 *:*) : great ;;
641 *) pfx="${cur_%%:*}:" ;;
642 esac
643 cur_="${cur_#*:}"
644 lhs=0
647 pfx="+"
648 cur_="${cur_#+}"
650 esac
651 case "$cmd" in
652 fetch)
653 if [ $lhs = 1 ]; then
654 __gitcomp_nl "$(__git_refs2 "$remote")" "$pfx" "$cur_"
655 else
656 __gitcomp_nl "$(__git_refs)" "$pfx" "$cur_"
659 pull|remote)
660 if [ $lhs = 1 ]; then
661 __gitcomp_nl "$(__git_refs "$remote")" "$pfx" "$cur_"
662 else
663 __gitcomp_nl "$(__git_refs)" "$pfx" "$cur_"
666 push)
667 if [ $lhs = 1 ]; then
668 __gitcomp_nl "$(__git_refs)" "$pfx" "$cur_"
669 else
670 __gitcomp_nl "$(__git_refs "$remote")" "$pfx" "$cur_"
673 esac
676 __git_complete_strategy ()
678 __git_compute_merge_strategies
679 case "$prev" in
680 -s|--strategy)
681 __gitcomp "$__git_merge_strategies"
682 return 0
683 esac
684 case "$cur" in
685 --strategy=*)
686 __gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
687 return 0
689 esac
690 return 1
693 __git_list_all_commands ()
695 local i IFS=" "$'\n'
696 for i in $(git help -a|egrep '^ [a-zA-Z0-9]')
698 case $i in
699 *--*) : helper pattern;;
700 *) echo $i;;
701 esac
702 done
705 __git_all_commands=
706 __git_compute_all_commands ()
708 test -n "$__git_all_commands" ||
709 __git_all_commands=$(__git_list_all_commands)
712 __git_list_porcelain_commands ()
714 local i IFS=" "$'\n'
715 __git_compute_all_commands
716 for i in $__git_all_commands
718 case $i in
719 *--*) : helper pattern;;
720 applymbox) : ask gittus;;
721 applypatch) : ask gittus;;
722 archimport) : import;;
723 cat-file) : plumbing;;
724 check-attr) : plumbing;;
725 check-ref-format) : plumbing;;
726 checkout-index) : plumbing;;
727 commit-tree) : plumbing;;
728 count-objects) : infrequent;;
729 credential-cache) : credentials helper;;
730 credential-store) : credentials helper;;
731 cvsexportcommit) : export;;
732 cvsimport) : import;;
733 cvsserver) : daemon;;
734 daemon) : daemon;;
735 diff-files) : plumbing;;
736 diff-index) : plumbing;;
737 diff-tree) : plumbing;;
738 fast-import) : import;;
739 fast-export) : export;;
740 fsck-objects) : plumbing;;
741 fetch-pack) : plumbing;;
742 fmt-merge-msg) : plumbing;;
743 for-each-ref) : plumbing;;
744 hash-object) : plumbing;;
745 http-*) : transport;;
746 index-pack) : plumbing;;
747 init-db) : deprecated;;
748 local-fetch) : plumbing;;
749 lost-found) : infrequent;;
750 ls-files) : plumbing;;
751 ls-remote) : plumbing;;
752 ls-tree) : plumbing;;
753 mailinfo) : plumbing;;
754 mailsplit) : plumbing;;
755 merge-*) : plumbing;;
756 mktree) : plumbing;;
757 mktag) : plumbing;;
758 pack-objects) : plumbing;;
759 pack-redundant) : plumbing;;
760 pack-refs) : plumbing;;
761 parse-remote) : plumbing;;
762 patch-id) : plumbing;;
763 peek-remote) : plumbing;;
764 prune) : plumbing;;
765 prune-packed) : plumbing;;
766 quiltimport) : import;;
767 read-tree) : plumbing;;
768 receive-pack) : plumbing;;
769 remote-*) : transport;;
770 repo-config) : deprecated;;
771 rerere) : plumbing;;
772 rev-list) : plumbing;;
773 rev-parse) : plumbing;;
774 runstatus) : plumbing;;
775 sh-setup) : internal;;
776 shell) : daemon;;
777 show-ref) : plumbing;;
778 send-pack) : plumbing;;
779 show-index) : plumbing;;
780 ssh-*) : transport;;
781 stripspace) : plumbing;;
782 symbolic-ref) : plumbing;;
783 tar-tree) : deprecated;;
784 unpack-file) : plumbing;;
785 unpack-objects) : plumbing;;
786 update-index) : plumbing;;
787 update-ref) : plumbing;;
788 update-server-info) : daemon;;
789 upload-archive) : plumbing;;
790 upload-pack) : plumbing;;
791 write-tree) : plumbing;;
792 var) : infrequent;;
793 verify-pack) : infrequent;;
794 verify-tag) : plumbing;;
795 *) echo $i;;
796 esac
797 done
800 __git_porcelain_commands=
801 __git_compute_porcelain_commands ()
803 __git_compute_all_commands
804 test -n "$__git_porcelain_commands" ||
805 __git_porcelain_commands=$(__git_list_porcelain_commands)
808 __git_pretty_aliases ()
810 local i IFS=$'\n'
811 for i in $(git --git-dir="$(__gitdir)" config --get-regexp "pretty\..*" 2>/dev/null); do
812 case "$i" in
813 pretty.*)
814 i="${i#pretty.}"
815 echo "${i/ */}"
817 esac
818 done
821 __git_aliases ()
823 local i IFS=$'\n'
824 for i in $(git --git-dir="$(__gitdir)" config --get-regexp "alias\..*" 2>/dev/null); do
825 case "$i" in
826 alias.*)
827 i="${i#alias.}"
828 echo "${i/ */}"
830 esac
831 done
834 # __git_aliased_command requires 1 argument
835 __git_aliased_command ()
837 local word cmdline=$(git --git-dir="$(__gitdir)" \
838 config --get "alias.$1")
839 for word in $cmdline; do
840 case "$word" in
841 \!gitk|gitk)
842 echo "gitk"
843 return
845 \!*) : shell command alias ;;
846 -*) : option ;;
847 *=*) : setting env ;;
848 git) : git itself ;;
850 echo "$word"
851 return
852 esac
853 done
856 # __git_find_on_cmdline requires 1 argument
857 __git_find_on_cmdline ()
859 local word subcommand c=1
860 while [ $c -lt $cword ]; do
861 word="${words[c]}"
862 for subcommand in $1; do
863 if [ "$subcommand" = "$word" ]; then
864 echo "$subcommand"
865 return
867 done
868 ((c++))
869 done
872 __git_has_doubledash ()
874 local c=1
875 while [ $c -lt $cword ]; do
876 if [ "--" = "${words[c]}" ]; then
877 return 0
879 ((c++))
880 done
881 return 1
884 # Try to count non option arguments passed on the command line for the
885 # specified git command.
886 # When options are used, it is necessary to use the special -- option to
887 # tell the implementation were non option arguments begin.
888 # XXX this can not be improved, since options can appear everywhere, as
889 # an example:
890 # git mv x -n y
892 # __git_count_arguments requires 1 argument: the git command executed.
893 __git_count_arguments ()
895 local word i c=0
897 # Skip "git" (first argument)
898 for ((i=1; i < ${#words[@]}; i++)); do
899 word="${words[i]}"
901 case "$word" in
903 # Good; we can assume that the following are only non
904 # option arguments.
905 ((c = 0))
907 "$1")
908 # Skip the specified git command and discard git
909 # main options
910 ((c = 0))
913 ((c++))
915 esac
916 done
918 printf "%d" $c
921 __git_whitespacelist="nowarn warn error error-all fix"
923 _git_am ()
925 local dir="$(__gitdir)"
926 if [ -d "$dir"/rebase-apply ]; then
927 __gitcomp "--skip --continue --resolved --abort"
928 return
930 case "$cur" in
931 --whitespace=*)
932 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
933 return
935 --*)
936 __gitcomp "
937 --3way --committer-date-is-author-date --ignore-date
938 --ignore-whitespace --ignore-space-change
939 --interactive --keep --no-utf8 --signoff --utf8
940 --whitespace= --scissors
942 return
943 esac
944 COMPREPLY=()
947 _git_apply ()
949 case "$cur" in
950 --whitespace=*)
951 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
952 return
954 --*)
955 __gitcomp "
956 --stat --numstat --summary --check --index
957 --cached --index-info --reverse --reject --unidiff-zero
958 --apply --no-add --exclude=
959 --ignore-whitespace --ignore-space-change
960 --whitespace= --inaccurate-eof --verbose
962 return
963 esac
964 COMPREPLY=()
967 _git_add ()
969 case "$cur" in
970 --*)
971 __gitcomp "
972 --interactive --refresh --patch --update --dry-run
973 --ignore-errors --intent-to-add
975 return
976 esac
978 # XXX should we check for --update and --all options ?
979 __git_complete_index_file "--others --modified"
982 _git_archive ()
984 case "$cur" in
985 --format=*)
986 __gitcomp "$(git archive --list)" "" "${cur##--format=}"
987 return
989 --remote=*)
990 __gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
991 return
993 --*)
994 __gitcomp "
995 --format= --list --verbose
996 --prefix= --remote= --exec=
998 return
1000 esac
1001 __git_complete_file
1004 _git_bisect ()
1006 __git_has_doubledash && return
1008 local subcommands="start bad good skip reset visualize replay log run"
1009 local subcommand="$(__git_find_on_cmdline "$subcommands")"
1010 if [ -z "$subcommand" ]; then
1011 if [ -f "$(__gitdir)"/BISECT_START ]; then
1012 __gitcomp "$subcommands"
1013 else
1014 __gitcomp "replay start"
1016 return
1019 case "$subcommand" in
1020 bad|good|reset|skip|start)
1021 __gitcomp_nl "$(__git_refs)"
1024 COMPREPLY=()
1026 esac
1029 _git_branch ()
1031 local i c=1 only_local_ref="n" has_r="n"
1033 while [ $c -lt $cword ]; do
1034 i="${words[c]}"
1035 case "$i" in
1036 -d|-m) only_local_ref="y" ;;
1037 -r) has_r="y" ;;
1038 esac
1039 ((c++))
1040 done
1042 case "$cur" in
1043 --set-upstream-to=*)
1044 __gitcomp "$(__git_refs)" "" "${cur##--set-upstream-to=}"
1046 --*)
1047 __gitcomp "
1048 --color --no-color --verbose --abbrev= --no-abbrev
1049 --track --no-track --contains --merged --no-merged
1050 --set-upstream-to= --edit-description --list
1051 --unset-upstream
1055 if [ $only_local_ref = "y" -a $has_r = "n" ]; then
1056 __gitcomp_nl "$(__git_heads)"
1057 else
1058 __gitcomp_nl "$(__git_refs)"
1061 esac
1064 _git_bundle ()
1066 local cmd="${words[2]}"
1067 case "$cword" in
1069 __gitcomp "create list-heads verify unbundle"
1072 # looking for a file
1075 case "$cmd" in
1076 create)
1077 __git_complete_revlist
1079 esac
1081 esac
1084 _git_checkout ()
1086 __git_has_doubledash && return
1088 case "$cur" in
1089 --conflict=*)
1090 __gitcomp "diff3 merge" "" "${cur##--conflict=}"
1092 --*)
1093 __gitcomp "
1094 --quiet --ours --theirs --track --no-track --merge
1095 --conflict= --orphan --patch
1099 # check if --track, --no-track, or --no-guess was specified
1100 # if so, disable DWIM mode
1101 local flags="--track --no-track --no-guess" track=1
1102 if [ -n "$(__git_find_on_cmdline "$flags")" ]; then
1103 track=''
1105 __gitcomp_nl "$(__git_refs '' $track)"
1107 esac
1110 _git_cherry ()
1112 __gitcomp "$(__git_refs)"
1115 _git_cherry_pick ()
1117 case "$cur" in
1118 --*)
1119 __gitcomp "--edit --no-commit"
1122 __gitcomp_nl "$(__git_refs)"
1124 esac
1127 _git_clean ()
1129 case "$cur" in
1130 --*)
1131 __gitcomp "--dry-run --quiet"
1132 return
1134 esac
1136 # XXX should we check for -x option ?
1137 __git_complete_index_file "--others"
1140 _git_clone ()
1142 case "$cur" in
1143 --*)
1144 __gitcomp "
1145 --local
1146 --no-hardlinks
1147 --shared
1148 --reference
1149 --quiet
1150 --no-checkout
1151 --bare
1152 --mirror
1153 --origin
1154 --upload-pack
1155 --template=
1156 --depth
1157 --single-branch
1158 --branch
1160 return
1162 esac
1163 COMPREPLY=()
1166 _git_commit ()
1168 case "$prev" in
1169 -c|-C)
1170 __gitcomp_nl "$(__git_refs)" "" "${cur}"
1171 return
1173 esac
1175 case "$cur" in
1176 --cleanup=*)
1177 __gitcomp "default strip verbatim whitespace
1178 " "" "${cur##--cleanup=}"
1179 return
1181 --reuse-message=*|--reedit-message=*|\
1182 --fixup=*|--squash=*)
1183 __gitcomp_nl "$(__git_refs)" "" "${cur#*=}"
1184 return
1186 --untracked-files=*)
1187 __gitcomp "all no normal" "" "${cur##--untracked-files=}"
1188 return
1190 --*)
1191 __gitcomp "
1192 --all --author= --signoff --verify --no-verify
1193 --edit --no-edit
1194 --amend --include --only --interactive
1195 --dry-run --reuse-message= --reedit-message=
1196 --reset-author --file= --message= --template=
1197 --cleanup= --untracked-files --untracked-files=
1198 --verbose --quiet --fixup= --squash=
1200 return
1201 esac
1203 if git rev-parse --verify --quiet HEAD >/dev/null; then
1204 __git_complete_diff_index_file "HEAD"
1205 else
1206 # This is the first commit
1207 __git_complete_index_file "--cached"
1211 _git_describe ()
1213 case "$cur" in
1214 --*)
1215 __gitcomp "
1216 --all --tags --contains --abbrev= --candidates=
1217 --exact-match --debug --long --match --always
1219 return
1220 esac
1221 __gitcomp_nl "$(__git_refs)"
1224 __git_diff_common_options="--stat --numstat --shortstat --summary
1225 --patch-with-stat --name-only --name-status --color
1226 --no-color --color-words --no-renames --check
1227 --full-index --binary --abbrev --diff-filter=
1228 --find-copies-harder
1229 --text --ignore-space-at-eol --ignore-space-change
1230 --ignore-all-space --exit-code --quiet --ext-diff
1231 --no-ext-diff
1232 --no-prefix --src-prefix= --dst-prefix=
1233 --inter-hunk-context=
1234 --patience
1235 --raw
1236 --dirstat --dirstat= --dirstat-by-file
1237 --dirstat-by-file= --cumulative
1240 _git_diff ()
1242 __git_has_doubledash && return
1244 case "$cur" in
1245 --*)
1246 __gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
1247 --base --ours --theirs --no-index
1248 $__git_diff_common_options
1250 return
1252 esac
1253 __git_complete_revlist_file
1256 __git_mergetools_common="diffuse ecmerge emerge kdiff3 meld opendiff
1257 tkdiff vimdiff gvimdiff xxdiff araxis p4merge bc3 codecompare
1260 _git_difftool ()
1262 __git_has_doubledash && return
1264 case "$cur" in
1265 --tool=*)
1266 __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1267 return
1269 --*)
1270 __gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
1271 --base --ours --theirs
1272 --no-renames --diff-filter= --find-copies-harder
1273 --relative --ignore-submodules
1274 --tool="
1275 return
1277 esac
1278 __git_complete_file
1281 __git_fetch_options="
1282 --quiet --verbose --append --upload-pack --force --keep --depth=
1283 --tags --no-tags --all --prune --dry-run
1286 _git_fetch ()
1288 case "$cur" in
1289 --*)
1290 __gitcomp "$__git_fetch_options"
1291 return
1293 esac
1294 __git_complete_remote_or_refspec
1297 __git_format_patch_options="
1298 --stdout --attach --no-attach --thread --thread= --output-directory
1299 --numbered --start-number --numbered-files --keep-subject --signoff
1300 --signature --no-signature --in-reply-to= --cc= --full-index --binary
1301 --not --all --cover-letter --no-prefix --src-prefix= --dst-prefix=
1302 --inline --suffix= --ignore-if-in-upstream --subject-prefix=
1305 _git_format_patch ()
1307 case "$cur" in
1308 --thread=*)
1309 __gitcomp "
1310 deep shallow
1311 " "" "${cur##--thread=}"
1312 return
1314 --*)
1315 __gitcomp "$__git_format_patch_options"
1316 return
1318 esac
1319 __git_complete_revlist
1322 _git_fsck ()
1324 case "$cur" in
1325 --*)
1326 __gitcomp "
1327 --tags --root --unreachable --cache --no-reflogs --full
1328 --strict --verbose --lost-found
1330 return
1332 esac
1333 COMPREPLY=()
1336 _git_gc ()
1338 case "$cur" in
1339 --*)
1340 __gitcomp "--prune --aggressive"
1341 return
1343 esac
1344 COMPREPLY=()
1347 _git_gitk ()
1349 _gitk
1352 __git_match_ctag() {
1353 awk "/^${1////\\/}/ { print \$1 }" "$2"
1356 _git_grep ()
1358 __git_has_doubledash && return
1360 case "$cur" in
1361 --*)
1362 __gitcomp "
1363 --cached
1364 --text --ignore-case --word-regexp --invert-match
1365 --full-name --line-number
1366 --extended-regexp --basic-regexp --fixed-strings
1367 --perl-regexp
1368 --files-with-matches --name-only
1369 --files-without-match
1370 --max-depth
1371 --count
1372 --and --or --not --all-match
1374 return
1376 esac
1378 case "$cword,$prev" in
1379 2,*|*,-*)
1380 if test -r tags; then
1381 __gitcomp_nl "$(__git_match_ctag "$cur" tags)"
1382 return
1385 esac
1387 __gitcomp_nl "$(__git_refs)"
1390 _git_help ()
1392 case "$cur" in
1393 --*)
1394 __gitcomp "--all --info --man --web"
1395 return
1397 esac
1398 __git_compute_all_commands
1399 __gitcomp "$__git_all_commands $(__git_aliases)
1400 attributes cli core-tutorial cvs-migration
1401 diffcore gitk glossary hooks ignore modules
1402 namespaces repository-layout tutorial tutorial-2
1403 workflows
1407 _git_init ()
1409 case "$cur" in
1410 --shared=*)
1411 __gitcomp "
1412 false true umask group all world everybody
1413 " "" "${cur##--shared=}"
1414 return
1416 --*)
1417 __gitcomp "--quiet --bare --template= --shared --shared="
1418 return
1420 esac
1421 COMPREPLY=()
1424 _git_ls_files ()
1426 case "$cur" in
1427 --*)
1428 __gitcomp "--cached --deleted --modified --others --ignored
1429 --stage --directory --no-empty-directory --unmerged
1430 --killed --exclude= --exclude-from=
1431 --exclude-per-directory= --exclude-standard
1432 --error-unmatch --with-tree= --full-name
1433 --abbrev --ignored --exclude-per-directory
1435 return
1437 esac
1439 # XXX ignore options like --modified and always suggest all cached
1440 # files.
1441 __git_complete_index_file "--cached"
1444 _git_ls_remote ()
1446 __gitcomp_nl "$(__git_remotes)"
1449 _git_ls_tree ()
1451 __git_complete_file
1454 # Options that go well for log, shortlog and gitk
1455 __git_log_common_options="
1456 --not --all
1457 --branches --tags --remotes
1458 --first-parent --merges --no-merges
1459 --max-count=
1460 --max-age= --since= --after=
1461 --min-age= --until= --before=
1462 --min-parents= --max-parents=
1463 --no-min-parents --no-max-parents
1465 # Options that go well for log and gitk (not shortlog)
1466 __git_log_gitk_options="
1467 --dense --sparse --full-history
1468 --simplify-merges --simplify-by-decoration
1469 --left-right --notes --no-notes
1471 # Options that go well for log and shortlog (not gitk)
1472 __git_log_shortlog_options="
1473 --author= --committer= --grep=
1474 --all-match
1477 __git_log_pretty_formats="oneline short medium full fuller email raw format:"
1478 __git_log_date_formats="relative iso8601 rfc2822 short local default raw"
1480 _git_log ()
1482 __git_has_doubledash && return
1484 local g="$(git rev-parse --git-dir 2>/dev/null)"
1485 local merge=""
1486 if [ -f "$g/MERGE_HEAD" ]; then
1487 merge="--merge"
1489 case "$cur" in
1490 --pretty=*|--format=*)
1491 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
1492 " "" "${cur#*=}"
1493 return
1495 --date=*)
1496 __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
1497 return
1499 --decorate=*)
1500 __gitcomp "long short" "" "${cur##--decorate=}"
1501 return
1503 --*)
1504 __gitcomp "
1505 $__git_log_common_options
1506 $__git_log_shortlog_options
1507 $__git_log_gitk_options
1508 --root --topo-order --date-order --reverse
1509 --follow --full-diff
1510 --abbrev-commit --abbrev=
1511 --relative-date --date=
1512 --pretty= --format= --oneline
1513 --cherry-pick
1514 --graph
1515 --decorate --decorate=
1516 --walk-reflogs
1517 --parents --children
1518 $merge
1519 $__git_diff_common_options
1520 --pickaxe-all --pickaxe-regex
1522 return
1524 esac
1525 __git_complete_revlist
1528 __git_merge_options="
1529 --no-commit --no-stat --log --no-log --squash --strategy
1530 --commit --stat --no-squash --ff --no-ff --ff-only --edit --no-edit
1533 _git_merge ()
1535 __git_complete_strategy && return
1537 case "$cur" in
1538 --*)
1539 __gitcomp "$__git_merge_options"
1540 return
1541 esac
1542 __gitcomp_nl "$(__git_refs)"
1545 _git_mergetool ()
1547 case "$cur" in
1548 --tool=*)
1549 __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
1550 return
1552 --*)
1553 __gitcomp "--tool="
1554 return
1556 esac
1557 COMPREPLY=()
1560 _git_merge_base ()
1562 __gitcomp_nl "$(__git_refs)"
1565 _git_mv ()
1567 case "$cur" in
1568 --*)
1569 __gitcomp "--dry-run"
1570 return
1572 esac
1574 if [ $(__git_count_arguments "mv") -gt 0 ]; then
1575 # We need to show both cached and untracked files (including
1576 # empty directories) since this may not be the last argument.
1577 __git_complete_index_file "--cached --others --directory"
1578 else
1579 __git_complete_index_file "--cached"
1583 _git_name_rev ()
1585 __gitcomp "--tags --all --stdin"
1588 _git_notes ()
1590 local subcommands='add append copy edit list prune remove show'
1591 local subcommand="$(__git_find_on_cmdline "$subcommands")"
1593 case "$subcommand,$cur" in
1594 ,--*)
1595 __gitcomp '--ref'
1598 case "$prev" in
1599 --ref)
1600 __gitcomp_nl "$(__git_refs)"
1603 __gitcomp "$subcommands --ref"
1605 esac
1607 add,--reuse-message=*|append,--reuse-message=*|\
1608 add,--reedit-message=*|append,--reedit-message=*)
1609 __gitcomp_nl "$(__git_refs)" "" "${cur#*=}"
1611 add,--*|append,--*)
1612 __gitcomp '--file= --message= --reedit-message=
1613 --reuse-message='
1615 copy,--*)
1616 __gitcomp '--stdin'
1618 prune,--*)
1619 __gitcomp '--dry-run --verbose'
1621 prune,*)
1624 case "$prev" in
1625 -m|-F)
1628 __gitcomp_nl "$(__git_refs)"
1630 esac
1632 esac
1635 _git_pull ()
1637 __git_complete_strategy && return
1639 case "$cur" in
1640 --*)
1641 __gitcomp "
1642 --rebase --no-rebase
1643 $__git_merge_options
1644 $__git_fetch_options
1646 return
1648 esac
1649 __git_complete_remote_or_refspec
1652 _git_push ()
1654 case "$prev" in
1655 --repo)
1656 __gitcomp_nl "$(__git_remotes)"
1657 return
1658 esac
1659 case "$cur" in
1660 --repo=*)
1661 __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
1662 return
1664 --*)
1665 __gitcomp "
1666 --all --mirror --tags --dry-run --force --verbose
1667 --receive-pack= --repo= --set-upstream
1669 return
1671 esac
1672 __git_complete_remote_or_refspec
1675 _git_rebase ()
1677 local dir="$(__gitdir)"
1678 if [ -d "$dir"/rebase-apply ] || [ -d "$dir"/rebase-merge ]; then
1679 __gitcomp "--continue --skip --abort"
1680 return
1682 __git_complete_strategy && return
1683 case "$cur" in
1684 --whitespace=*)
1685 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1686 return
1688 --*)
1689 __gitcomp "
1690 --onto --merge --strategy --interactive
1691 --preserve-merges --stat --no-stat
1692 --committer-date-is-author-date --ignore-date
1693 --ignore-whitespace --whitespace=
1694 --autosquash
1697 return
1698 esac
1699 __gitcomp_nl "$(__git_refs)"
1702 _git_reflog ()
1704 local subcommands="show delete expire"
1705 local subcommand="$(__git_find_on_cmdline "$subcommands")"
1707 if [ -z "$subcommand" ]; then
1708 __gitcomp "$subcommands"
1709 else
1710 __gitcomp_nl "$(__git_refs)"
1714 __git_send_email_confirm_options="always never auto cc compose"
1715 __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
1717 _git_send_email ()
1719 case "$cur" in
1720 --confirm=*)
1721 __gitcomp "
1722 $__git_send_email_confirm_options
1723 " "" "${cur##--confirm=}"
1724 return
1726 --suppress-cc=*)
1727 __gitcomp "
1728 $__git_send_email_suppresscc_options
1729 " "" "${cur##--suppress-cc=}"
1731 return
1733 --smtp-encryption=*)
1734 __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
1735 return
1737 --thread=*)
1738 __gitcomp "
1739 deep shallow
1740 " "" "${cur##--thread=}"
1741 return
1743 --*)
1744 __gitcomp "--annotate --bcc --cc --cc-cmd --chain-reply-to
1745 --compose --confirm= --dry-run --envelope-sender
1746 --from --identity
1747 --in-reply-to --no-chain-reply-to --no-signed-off-by-cc
1748 --no-suppress-from --no-thread --quiet
1749 --signed-off-by-cc --smtp-pass --smtp-server
1750 --smtp-server-port --smtp-encryption= --smtp-user
1751 --subject --suppress-cc= --suppress-from --thread --to
1752 --validate --no-validate
1753 $__git_format_patch_options"
1754 return
1756 esac
1757 __git_complete_revlist
1760 _git_stage ()
1762 _git_add
1765 __git_config_get_set_variables ()
1767 local prevword word config_file= c=$cword
1768 while [ $c -gt 1 ]; do
1769 word="${words[c]}"
1770 case "$word" in
1771 --global|--system|--file=*)
1772 config_file="$word"
1773 break
1775 -f|--file)
1776 config_file="$word $prevword"
1777 break
1779 esac
1780 prevword=$word
1781 c=$((--c))
1782 done
1784 git --git-dir="$(__gitdir)" config $config_file --list 2>/dev/null |
1785 while read -r line
1787 case "$line" in
1788 *.*=*)
1789 echo "${line/=*/}"
1791 esac
1792 done
1795 _git_config ()
1797 case "$prev" in
1798 branch.*.remote)
1799 __gitcomp_nl "$(__git_remotes)"
1800 return
1802 branch.*.merge)
1803 __gitcomp_nl "$(__git_refs)"
1804 return
1806 remote.*.fetch)
1807 local remote="${prev#remote.}"
1808 remote="${remote%.fetch}"
1809 if [ -z "$cur" ]; then
1810 COMPREPLY=("refs/heads/")
1811 return
1813 __gitcomp_nl "$(__git_refs_remotes "$remote")"
1814 return
1816 remote.*.push)
1817 local remote="${prev#remote.}"
1818 remote="${remote%.push}"
1819 __gitcomp_nl "$(git --git-dir="$(__gitdir)" \
1820 for-each-ref --format='%(refname):%(refname)' \
1821 refs/heads)"
1822 return
1824 pull.twohead|pull.octopus)
1825 __git_compute_merge_strategies
1826 __gitcomp "$__git_merge_strategies"
1827 return
1829 color.branch|color.diff|color.interactive|\
1830 color.showbranch|color.status|color.ui)
1831 __gitcomp "always never auto"
1832 return
1834 color.pager)
1835 __gitcomp "false true"
1836 return
1838 color.*.*)
1839 __gitcomp "
1840 normal black red green yellow blue magenta cyan white
1841 bold dim ul blink reverse
1843 return
1845 help.format)
1846 __gitcomp "man info web html"
1847 return
1849 log.date)
1850 __gitcomp "$__git_log_date_formats"
1851 return
1853 sendemail.aliasesfiletype)
1854 __gitcomp "mutt mailrc pine elm gnus"
1855 return
1857 sendemail.confirm)
1858 __gitcomp "$__git_send_email_confirm_options"
1859 return
1861 sendemail.suppresscc)
1862 __gitcomp "$__git_send_email_suppresscc_options"
1863 return
1865 --get|--get-all|--unset|--unset-all)
1866 __gitcomp_nl "$(__git_config_get_set_variables)"
1867 return
1869 *.*)
1870 COMPREPLY=()
1871 return
1873 esac
1874 case "$cur" in
1875 --*)
1876 __gitcomp "
1877 --global --system --file=
1878 --list --replace-all
1879 --get --get-all --get-regexp
1880 --add --unset --unset-all
1881 --remove-section --rename-section
1883 return
1885 branch.*.*)
1886 local pfx="${cur%.*}." cur_="${cur##*.}"
1887 __gitcomp "remote merge mergeoptions rebase" "$pfx" "$cur_"
1888 return
1890 branch.*)
1891 local pfx="${cur%.*}." cur_="${cur#*.}"
1892 __gitcomp_nl "$(__git_heads)" "$pfx" "$cur_" "."
1893 return
1895 guitool.*.*)
1896 local pfx="${cur%.*}." cur_="${cur##*.}"
1897 __gitcomp "
1898 argprompt cmd confirm needsfile noconsole norescan
1899 prompt revprompt revunmerged title
1900 " "$pfx" "$cur_"
1901 return
1903 difftool.*.*)
1904 local pfx="${cur%.*}." cur_="${cur##*.}"
1905 __gitcomp "cmd path" "$pfx" "$cur_"
1906 return
1908 man.*.*)
1909 local pfx="${cur%.*}." cur_="${cur##*.}"
1910 __gitcomp "cmd path" "$pfx" "$cur_"
1911 return
1913 mergetool.*.*)
1914 local pfx="${cur%.*}." cur_="${cur##*.}"
1915 __gitcomp "cmd path trustExitCode" "$pfx" "$cur_"
1916 return
1918 pager.*)
1919 local pfx="${cur%.*}." cur_="${cur#*.}"
1920 __git_compute_all_commands
1921 __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_"
1922 return
1924 remote.*.*)
1925 local pfx="${cur%.*}." cur_="${cur##*.}"
1926 __gitcomp "
1927 url proxy fetch push mirror skipDefaultUpdate
1928 receivepack uploadpack tagopt pushurl
1929 " "$pfx" "$cur_"
1930 return
1932 remote.*)
1933 local pfx="${cur%.*}." cur_="${cur#*.}"
1934 __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
1935 return
1937 url.*.*)
1938 local pfx="${cur%.*}." cur_="${cur##*.}"
1939 __gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_"
1940 return
1942 esac
1943 __gitcomp "
1944 add.ignoreErrors
1945 advice.commitBeforeMerge
1946 advice.detachedHead
1947 advice.implicitIdentity
1948 advice.pushNonFastForward
1949 advice.resolveConflict
1950 advice.statusHints
1951 alias.
1952 am.keepcr
1953 apply.ignorewhitespace
1954 apply.whitespace
1955 branch.autosetupmerge
1956 branch.autosetuprebase
1957 browser.
1958 clean.requireForce
1959 color.branch
1960 color.branch.current
1961 color.branch.local
1962 color.branch.plain
1963 color.branch.remote
1964 color.decorate.HEAD
1965 color.decorate.branch
1966 color.decorate.remoteBranch
1967 color.decorate.stash
1968 color.decorate.tag
1969 color.diff
1970 color.diff.commit
1971 color.diff.frag
1972 color.diff.func
1973 color.diff.meta
1974 color.diff.new
1975 color.diff.old
1976 color.diff.plain
1977 color.diff.whitespace
1978 color.grep
1979 color.grep.context
1980 color.grep.filename
1981 color.grep.function
1982 color.grep.linenumber
1983 color.grep.match
1984 color.grep.selected
1985 color.grep.separator
1986 color.interactive
1987 color.interactive.error
1988 color.interactive.header
1989 color.interactive.help
1990 color.interactive.prompt
1991 color.pager
1992 color.showbranch
1993 color.status
1994 color.status.added
1995 color.status.changed
1996 color.status.header
1997 color.status.nobranch
1998 color.status.untracked
1999 color.status.updated
2000 color.ui
2001 commit.status
2002 commit.template
2003 core.abbrev
2004 core.askpass
2005 core.attributesfile
2006 core.autocrlf
2007 core.bare
2008 core.bigFileThreshold
2009 core.compression
2010 core.createObject
2011 core.deltaBaseCacheLimit
2012 core.editor
2013 core.eol
2014 core.excludesfile
2015 core.fileMode
2016 core.fsyncobjectfiles
2017 core.gitProxy
2018 core.ignoreCygwinFSTricks
2019 core.ignoreStat
2020 core.ignorecase
2021 core.logAllRefUpdates
2022 core.loosecompression
2023 core.notesRef
2024 core.packedGitLimit
2025 core.packedGitWindowSize
2026 core.pager
2027 core.preferSymlinkRefs
2028 core.preloadindex
2029 core.quotepath
2030 core.repositoryFormatVersion
2031 core.safecrlf
2032 core.sharedRepository
2033 core.sparseCheckout
2034 core.symlinks
2035 core.trustctime
2036 core.warnAmbiguousRefs
2037 core.whitespace
2038 core.worktree
2039 diff.autorefreshindex
2040 diff.statGraphWidth
2041 diff.external
2042 diff.ignoreSubmodules
2043 diff.mnemonicprefix
2044 diff.noprefix
2045 diff.renameLimit
2046 diff.renames
2047 diff.suppressBlankEmpty
2048 diff.tool
2049 diff.wordRegex
2050 difftool.
2051 difftool.prompt
2052 fetch.recurseSubmodules
2053 fetch.unpackLimit
2054 format.attach
2055 format.cc
2056 format.headers
2057 format.numbered
2058 format.pretty
2059 format.signature
2060 format.signoff
2061 format.subjectprefix
2062 format.suffix
2063 format.thread
2064 format.to
2066 gc.aggressiveWindow
2067 gc.auto
2068 gc.autopacklimit
2069 gc.packrefs
2070 gc.pruneexpire
2071 gc.reflogexpire
2072 gc.reflogexpireunreachable
2073 gc.rerereresolved
2074 gc.rerereunresolved
2075 gitcvs.allbinary
2076 gitcvs.commitmsgannotation
2077 gitcvs.dbTableNamePrefix
2078 gitcvs.dbdriver
2079 gitcvs.dbname
2080 gitcvs.dbpass
2081 gitcvs.dbuser
2082 gitcvs.enabled
2083 gitcvs.logfile
2084 gitcvs.usecrlfattr
2085 guitool.
2086 gui.blamehistoryctx
2087 gui.commitmsgwidth
2088 gui.copyblamethreshold
2089 gui.diffcontext
2090 gui.encoding
2091 gui.fastcopyblame
2092 gui.matchtrackingbranch
2093 gui.newbranchtemplate
2094 gui.pruneduringfetch
2095 gui.spellingdictionary
2096 gui.trustmtime
2097 help.autocorrect
2098 help.browser
2099 help.format
2100 http.lowSpeedLimit
2101 http.lowSpeedTime
2102 http.maxRequests
2103 http.minSessions
2104 http.noEPSV
2105 http.postBuffer
2106 http.proxy
2107 http.sslCAInfo
2108 http.sslCAPath
2109 http.sslCert
2110 http.sslCertPasswordProtected
2111 http.sslKey
2112 http.sslVerify
2113 http.useragent
2114 i18n.commitEncoding
2115 i18n.logOutputEncoding
2116 imap.authMethod
2117 imap.folder
2118 imap.host
2119 imap.pass
2120 imap.port
2121 imap.preformattedHTML
2122 imap.sslverify
2123 imap.tunnel
2124 imap.user
2125 init.templatedir
2126 instaweb.browser
2127 instaweb.httpd
2128 instaweb.local
2129 instaweb.modulepath
2130 instaweb.port
2131 interactive.singlekey
2132 log.date
2133 log.decorate
2134 log.showroot
2135 mailmap.file
2136 man.
2137 man.viewer
2138 merge.
2139 merge.conflictstyle
2140 merge.log
2141 merge.renameLimit
2142 merge.renormalize
2143 merge.stat
2144 merge.tool
2145 merge.verbosity
2146 mergetool.
2147 mergetool.keepBackup
2148 mergetool.keepTemporaries
2149 mergetool.prompt
2150 notes.displayRef
2151 notes.rewrite.
2152 notes.rewrite.amend
2153 notes.rewrite.rebase
2154 notes.rewriteMode
2155 notes.rewriteRef
2156 pack.compression
2157 pack.deltaCacheLimit
2158 pack.deltaCacheSize
2159 pack.depth
2160 pack.indexVersion
2161 pack.packSizeLimit
2162 pack.threads
2163 pack.window
2164 pack.windowMemory
2165 pager.
2166 pretty.
2167 pull.octopus
2168 pull.twohead
2169 push.default
2170 rebase.autosquash
2171 rebase.stat
2172 receive.autogc
2173 receive.denyCurrentBranch
2174 receive.denyDeleteCurrent
2175 receive.denyDeletes
2176 receive.denyNonFastForwards
2177 receive.fsckObjects
2178 receive.unpackLimit
2179 receive.updateserverinfo
2180 remotes.
2181 repack.usedeltabaseoffset
2182 rerere.autoupdate
2183 rerere.enabled
2184 sendemail.
2185 sendemail.aliasesfile
2186 sendemail.aliasfiletype
2187 sendemail.bcc
2188 sendemail.cc
2189 sendemail.cccmd
2190 sendemail.chainreplyto
2191 sendemail.confirm
2192 sendemail.envelopesender
2193 sendemail.from
2194 sendemail.identity
2195 sendemail.multiedit
2196 sendemail.signedoffbycc
2197 sendemail.smtpdomain
2198 sendemail.smtpencryption
2199 sendemail.smtppass
2200 sendemail.smtpserver
2201 sendemail.smtpserveroption
2202 sendemail.smtpserverport
2203 sendemail.smtpuser
2204 sendemail.suppresscc
2205 sendemail.suppressfrom
2206 sendemail.thread
2207 sendemail.to
2208 sendemail.validate
2209 showbranch.default
2210 status.relativePaths
2211 status.showUntrackedFiles
2212 status.submodulesummary
2213 submodule.
2214 tar.umask
2215 transfer.unpackLimit
2216 url.
2217 user.email
2218 user.name
2219 user.signingkey
2220 web.browser
2221 branch. remote.
2225 _git_remote ()
2227 local subcommands="add rename remove set-head set-branches set-url show prune update"
2228 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2229 if [ -z "$subcommand" ]; then
2230 __gitcomp "$subcommands"
2231 return
2234 case "$subcommand" in
2235 rename|remove|set-url|show|prune)
2236 __gitcomp_nl "$(__git_remotes)"
2238 set-head|set-branches)
2239 __git_complete_remote_or_refspec
2241 update)
2242 local i c='' IFS=$'\n'
2243 for i in $(git --git-dir="$(__gitdir)" config --get-regexp "remotes\..*" 2>/dev/null); do
2244 i="${i#remotes.}"
2245 c="$c ${i/ */}"
2246 done
2247 __gitcomp "$c"
2250 COMPREPLY=()
2252 esac
2255 _git_replace ()
2257 __gitcomp_nl "$(__git_refs)"
2260 _git_reset ()
2262 __git_has_doubledash && return
2264 case "$cur" in
2265 --*)
2266 __gitcomp "--merge --mixed --hard --soft --patch"
2267 return
2269 esac
2270 __gitcomp_nl "$(__git_refs)"
2273 _git_revert ()
2275 case "$cur" in
2276 --*)
2277 __gitcomp "--edit --mainline --no-edit --no-commit --signoff"
2278 return
2280 esac
2281 __gitcomp_nl "$(__git_refs)"
2284 _git_rm ()
2286 case "$cur" in
2287 --*)
2288 __gitcomp "--cached --dry-run --ignore-unmatch --quiet"
2289 return
2291 esac
2293 __git_complete_index_file "--cached"
2296 _git_shortlog ()
2298 __git_has_doubledash && return
2300 case "$cur" in
2301 --*)
2302 __gitcomp "
2303 $__git_log_common_options
2304 $__git_log_shortlog_options
2305 --numbered --summary
2307 return
2309 esac
2310 __git_complete_revlist
2313 _git_show ()
2315 __git_has_doubledash && return
2317 case "$cur" in
2318 --pretty=*|--format=*)
2319 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2320 " "" "${cur#*=}"
2321 return
2323 --*)
2324 __gitcomp "--pretty= --format= --abbrev-commit --oneline
2325 $__git_diff_common_options
2327 return
2329 esac
2330 __git_complete_file
2333 _git_show_branch ()
2335 case "$cur" in
2336 --*)
2337 __gitcomp "
2338 --all --remotes --topo-order --current --more=
2339 --list --independent --merge-base --no-name
2340 --color --no-color
2341 --sha1-name --sparse --topics --reflog
2343 return
2345 esac
2346 __git_complete_revlist
2349 _git_stash ()
2351 local save_opts='--keep-index --no-keep-index --quiet --patch'
2352 local subcommands='save list show apply clear drop pop create branch'
2353 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2354 if [ -z "$subcommand" ]; then
2355 case "$cur" in
2356 --*)
2357 __gitcomp "$save_opts"
2360 if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
2361 __gitcomp "$subcommands"
2362 else
2363 COMPREPLY=()
2366 esac
2367 else
2368 case "$subcommand,$cur" in
2369 save,--*)
2370 __gitcomp "$save_opts"
2372 apply,--*|pop,--*)
2373 __gitcomp "--index --quiet"
2375 show,--*|drop,--*|branch,--*)
2376 COMPREPLY=()
2378 show,*|apply,*|drop,*|pop,*|branch,*)
2379 __gitcomp_nl "$(git --git-dir="$(__gitdir)" stash list \
2380 | sed -n -e 's/:.*//p')"
2383 COMPREPLY=()
2385 esac
2389 _git_submodule ()
2391 __git_has_doubledash && return
2393 local subcommands="add status init update summary foreach sync"
2394 if [ -z "$(__git_find_on_cmdline "$subcommands")" ]; then
2395 case "$cur" in
2396 --*)
2397 __gitcomp "--quiet --cached"
2400 __gitcomp "$subcommands"
2402 esac
2403 return
2407 _git_svn ()
2409 local subcommands="
2410 init fetch clone rebase dcommit log find-rev
2411 set-tree commit-diff info create-ignore propget
2412 proplist show-ignore show-externals branch tag blame
2413 migrate mkdirs reset gc
2415 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2416 if [ -z "$subcommand" ]; then
2417 __gitcomp "$subcommands"
2418 else
2419 local remote_opts="--username= --config-dir= --no-auth-cache"
2420 local fc_opts="
2421 --follow-parent --authors-file= --repack=
2422 --no-metadata --use-svm-props --use-svnsync-props
2423 --log-window-size= --no-checkout --quiet
2424 --repack-flags --use-log-author --localtime
2425 --ignore-paths= $remote_opts
2427 local init_opts="
2428 --template= --shared= --trunk= --tags=
2429 --branches= --stdlayout --minimize-url
2430 --no-metadata --use-svm-props --use-svnsync-props
2431 --rewrite-root= --prefix= --use-log-author
2432 --add-author-from $remote_opts
2434 local cmt_opts="
2435 --edit --rmdir --find-copies-harder --copy-similarity=
2438 case "$subcommand,$cur" in
2439 fetch,--*)
2440 __gitcomp "--revision= --fetch-all $fc_opts"
2442 clone,--*)
2443 __gitcomp "--revision= $fc_opts $init_opts"
2445 init,--*)
2446 __gitcomp "$init_opts"
2448 dcommit,--*)
2449 __gitcomp "
2450 --merge --strategy= --verbose --dry-run
2451 --fetch-all --no-rebase --commit-url
2452 --revision --interactive $cmt_opts $fc_opts
2455 set-tree,--*)
2456 __gitcomp "--stdin $cmt_opts $fc_opts"
2458 create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
2459 show-externals,--*|mkdirs,--*)
2460 __gitcomp "--revision="
2462 log,--*)
2463 __gitcomp "
2464 --limit= --revision= --verbose --incremental
2465 --oneline --show-commit --non-recursive
2466 --authors-file= --color
2469 rebase,--*)
2470 __gitcomp "
2471 --merge --verbose --strategy= --local
2472 --fetch-all --dry-run $fc_opts
2475 commit-diff,--*)
2476 __gitcomp "--message= --file= --revision= $cmt_opts"
2478 info,--*)
2479 __gitcomp "--url"
2481 branch,--*)
2482 __gitcomp "--dry-run --message --tag"
2484 tag,--*)
2485 __gitcomp "--dry-run --message"
2487 blame,--*)
2488 __gitcomp "--git-format"
2490 migrate,--*)
2491 __gitcomp "
2492 --config-dir= --ignore-paths= --minimize
2493 --no-auth-cache --username=
2496 reset,--*)
2497 __gitcomp "--revision= --parent"
2500 COMPREPLY=()
2502 esac
2506 _git_tag ()
2508 local i c=1 f=0
2509 while [ $c -lt $cword ]; do
2510 i="${words[c]}"
2511 case "$i" in
2512 -d|-v)
2513 __gitcomp_nl "$(__git_tags)"
2514 return
2519 esac
2520 ((c++))
2521 done
2523 case "$prev" in
2524 -m|-F)
2525 COMPREPLY=()
2527 -*|tag)
2528 if [ $f = 1 ]; then
2529 __gitcomp_nl "$(__git_tags)"
2530 else
2531 COMPREPLY=()
2535 __gitcomp_nl "$(__git_refs)"
2537 esac
2540 _git_whatchanged ()
2542 _git_log
2545 __git_main ()
2547 local i c=1 command __git_dir
2549 while [ $c -lt $cword ]; do
2550 i="${words[c]}"
2551 case "$i" in
2552 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
2553 --bare) __git_dir="." ;;
2554 --help) command="help"; break ;;
2555 -c) c=$((++c)) ;;
2556 -*) ;;
2557 *) command="$i"; break ;;
2558 esac
2559 ((c++))
2560 done
2562 if [ -z "$command" ]; then
2563 case "$cur" in
2564 --*) __gitcomp "
2565 --paginate
2566 --no-pager
2567 --git-dir=
2568 --bare
2569 --version
2570 --exec-path
2571 --exec-path=
2572 --html-path
2573 --info-path
2574 --work-tree=
2575 --namespace=
2576 --no-replace-objects
2577 --help
2580 *) __git_compute_porcelain_commands
2581 __gitcomp "$__git_porcelain_commands $(__git_aliases)" ;;
2582 esac
2583 return
2586 local completion_func="_git_${command//-/_}"
2587 declare -f $completion_func >/dev/null && $completion_func && return
2589 local expansion=$(__git_aliased_command "$command")
2590 if [ -n "$expansion" ]; then
2591 completion_func="_git_${expansion//-/_}"
2592 declare -f $completion_func >/dev/null && $completion_func
2596 __gitk_main ()
2598 __git_has_doubledash && return
2600 local g="$(__gitdir)"
2601 local merge=""
2602 if [ -f "$g/MERGE_HEAD" ]; then
2603 merge="--merge"
2605 case "$cur" in
2606 --*)
2607 __gitcomp "
2608 $__git_log_common_options
2609 $__git_log_gitk_options
2610 $merge
2612 return
2614 esac
2615 __git_complete_revlist
2618 if [[ -n ${ZSH_VERSION-} ]]; then
2619 echo "WARNING: this script is deprecated, please see git-completion.zsh" 1>&2
2621 autoload -U +X compinit && compinit
2623 __gitcomp ()
2625 emulate -L zsh
2627 local cur_="${3-$cur}"
2629 case "$cur_" in
2630 --*=)
2633 local c IFS=$' \t\n'
2634 local -a array
2635 for c in ${=1}; do
2636 c="$c${4-}"
2637 case $c in
2638 --*=*|*.) ;;
2639 *) c="$c " ;;
2640 esac
2641 array+=("$c")
2642 done
2643 compset -P '*[=:]'
2644 compadd -Q -S '' -p "${2-}" -a -- array && _ret=0
2646 esac
2649 __gitcomp_nl ()
2651 emulate -L zsh
2653 local IFS=$'\n'
2654 compset -P '*[=:]'
2655 compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
2658 __gitcomp_file ()
2660 emulate -L zsh
2662 local IFS=$'\n'
2663 compset -P '*[=:]'
2664 compadd -Q -p "${2-}" -f -- ${=1} && _ret=0
2667 __git_zsh_helper ()
2669 emulate -L ksh
2670 local cur cword prev
2671 cur=${words[CURRENT-1]}
2672 prev=${words[CURRENT-2]}
2673 let cword=CURRENT-1
2674 __${service}_main
2677 _git ()
2679 emulate -L zsh
2680 local _ret=1
2681 __git_zsh_helper
2682 let _ret && _default -S '' && _ret=0
2683 return _ret
2686 compdef _git git gitk
2687 return
2688 elif [[ -n ${BASH_VERSION-} ]]; then
2689 if ((${BASH_VERSINFO[0]} < 4)); then
2690 # compopt is not supported
2691 __git_index_file_list_filter ()
2693 __git_index_file_list_filter_compat
2698 __git_func_wrap ()
2700 local cur words cword prev
2701 _get_comp_words_by_ref -n =: cur words cword prev
2705 # Setup completion for certain functions defined above by setting common
2706 # variables and workarounds.
2707 # This is NOT a public function; use at your own risk.
2708 __git_complete ()
2710 local wrapper="__git_wrap${2}"
2711 eval "$wrapper () { __git_func_wrap $2 ; }"
2712 complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
2713 || complete -o default -o nospace -F $wrapper $1
2716 # wrapper for backwards compatibility
2717 _git ()
2719 __git_wrap__git_main
2722 # wrapper for backwards compatibility
2723 _gitk ()
2725 __git_wrap__gitk_main
2728 __git_complete git __git_main
2729 __git_complete gitk __gitk_main
2731 # The following are necessary only for Cygwin, and only are needed
2732 # when the user has tab-completed the executable name and consequently
2733 # included the '.exe' suffix.
2735 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
2736 __git_complete git.exe __git_main