1 # bash/zsh completion support for core Git.
3 # Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
4 # Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
5 # Distributed under the GNU General Public License, version 2.0.
7 # The contained completion routines provide support for completing:
9 # *) local and remote branch names
10 # *) local and remote tag names
11 # *) .git/remotes file names
12 # *) git 'subcommands'
13 # *) git email aliases for git-send-email
14 # *) tree paths within 'ref:path/to/file' expressions
15 # *) file paths within current working directory and index
16 # *) common --long-options
18 # To use these routines:
20 # 1) Copy this file to somewhere (e.g. ~/.git-completion.bash).
21 # 2) Add the following line to your .bashrc/.zshrc:
22 # source ~/.git-completion.bash
23 # 3) Consider changing your PS1 to also show the current branch,
24 # see git-prompt.sh for details.
26 # If you use complex aliases of form '!f() { ... }; f', you can use the null
27 # command ':' as the first command in the function body to declare the desired
28 # completion style. For example '!f() { : git commit ; ... }; f' will
29 # tell the completion to use commit completion. This also works with aliases
30 # of form "!sh -c '...'". For example, "!sh -c ': git commit ; ... '".
32 # Compatible with bash 3.2.57.
34 # You can set the following environment variables to influence the behavior of
35 # the completion routines:
37 # GIT_COMPLETION_CHECKOUT_NO_GUESS
39 # When set to "1", do not include "DWIM" suggestions in git-checkout
40 # and git-switch completion (e.g., completing "foo" when "origin/foo"
43 # GIT_COMPLETION_SHOW_ALL
45 # When set to "1" suggest all options, including options which are
46 # typically hidden (e.g. '--allow-empty' for 'git commit').
48 case "$COMP_WORDBREAKS" in
50 *) COMP_WORDBREAKS="$COMP_WORDBREAKS:"
53 # Discovers the path to the git repository taking any '--git-dir=<path>' and
54 # '-C <path>' options into account and stores it in the $__git_repo_path
56 __git_find_repo_path ()
58 if [ -n "${__git_repo_path-}" ]; then
59 # we already know where it is
63 if [ -n "${__git_C_args-}" ]; then
64 __git_repo_path="$(git "${__git_C_args[@]}" \
65 ${__git_dir:+--git-dir="$__git_dir"} \
66 rev-parse --absolute-git-dir 2>/dev/null)"
67 elif [ -n "${__git_dir-}" ]; then
68 test -d "$__git_dir" &&
69 __git_repo_path="$__git_dir"
70 elif [ -n "${GIT_DIR-}" ]; then
71 test -d "${GIT_DIR-}" &&
72 __git_repo_path="$GIT_DIR"
73 elif [ -d .git ]; then
76 __git_repo_path="$(git rev-parse --git-dir 2>/dev/null)"
80 # Deprecated: use __git_find_repo_path() and $__git_repo_path instead
81 # __gitdir accepts 0 or 1 arguments (i.e., location)
82 # returns location of .git repo
85 if [ -z "${1-}" ]; then
86 __git_find_repo_path || return 1
87 echo "$__git_repo_path"
88 elif [ -d "$1/.git" ]; then
95 # Runs git with all the options given as argument, respecting any
96 # '--git-dir=<path>' and '-C <path>' options present on the command line
99 git ${__git_C_args:+"${__git_C_args[@]}"} \
100 ${__git_dir:+--git-dir="$__git_dir"} "$@" 2>/dev/null
103 # Removes backslash escaping, single quotes and double quotes from a word,
104 # stores the result in the variable $dequoted_word.
105 # 1: The word to dequote.
108 local rest="$1" len ch
112 while test -n "$rest"; do
113 len=${#dequoted_word}
114 dequoted_word="$dequoted_word${rest%%[\\\'\"]*}"
115 rest="${rest:$((${#dequoted_word}-$len))}"
117 case "${rest:0:1}" in
124 dequoted_word="$dequoted_word$ch"
131 len=${#dequoted_word}
132 dequoted_word="$dequoted_word${rest%%\'*}"
133 rest="${rest:$((${#dequoted_word}-$len+1))}"
137 while test -n "$rest" ; do
138 len=${#dequoted_word}
139 dequoted_word="$dequoted_word${rest%%[\\\"]*}"
140 rest="${rest:$((${#dequoted_word}-$len))}"
141 case "${rest:0:1}" in
146 dequoted_word="$dequoted_word$ch"
151 dequoted_word="$dequoted_word\\$ch"
167 # The following function is based on code from:
169 # bash_completion - programmable completion functions for bash 3.2+
171 # Copyright © 2006-2008, Ian Macdonald <ian@caliban.org>
172 # © 2009-2010, Bash Completion Maintainers
173 # <bash-completion-devel@lists.alioth.debian.org>
175 # This program is free software; you can redistribute it and/or modify
176 # it under the terms of the GNU General Public License as published by
177 # the Free Software Foundation; either version 2, or (at your option)
180 # This program is distributed in the hope that it will be useful,
181 # but WITHOUT ANY WARRANTY; without even the implied warranty of
182 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
183 # GNU General Public License for more details.
185 # You should have received a copy of the GNU General Public License
186 # along with this program; if not, see <http://www.gnu.org/licenses/>.
188 # The latest version of this software can be obtained here:
190 # http://bash-completion.alioth.debian.org/
194 # This function can be used to access a tokenized list of words
195 # on the command line:
197 # __git_reassemble_comp_words_by_ref '=:'
198 # if test "${words_[cword_-1]}" = -w
203 # The argument should be a collection of characters from the list of
204 # word completion separators (COMP_WORDBREAKS) to treat as ordinary
207 # This is roughly equivalent to going back in time and setting
208 # COMP_WORDBREAKS to exclude those characters. The intent is to
209 # make option types like --date=<type> and <rev>:<path> easy to
210 # recognize by treating each shell word as a single token.
212 # It is best not to set COMP_WORDBREAKS directly because the value is
213 # shared with other completion scripts. By the time the completion
214 # function gets called, COMP_WORDS has already been populated so local
215 # changes to COMP_WORDBREAKS have no effect.
217 # Output: words_, cword_, cur_.
219 __git_reassemble_comp_words_by_ref()
221 local exclude i j first
222 # Which word separators to exclude?
223 exclude="${1//[^$COMP_WORDBREAKS]}"
225 if [ -z "$exclude" ]; then
226 words_=("${COMP_WORDS[@]}")
229 # List of word completion separators has shrunk;
230 # re-assemble words to complete.
231 for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
232 # Append each nonempty word consisting of just
233 # word separator characters to the current word.
237 [ -n "${COMP_WORDS[$i]}" ] &&
238 # word consists of excluded word separators
239 [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
241 # Attach to the previous token,
242 # unless the previous token is the command name.
243 if [ $j -ge 2 ] && [ -n "$first" ]; then
247 words_[$j]=${words_[j]}${COMP_WORDS[i]}
248 if [ $i = $COMP_CWORD ]; then
251 if (($i < ${#COMP_WORDS[@]} - 1)); then
258 words_[$j]=${words_[j]}${COMP_WORDS[i]}
259 if [ $i = $COMP_CWORD ]; then
265 if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
266 _get_comp_words_by_ref ()
268 local exclude cur_ words_ cword_
269 if [ "$1" = "-n" ]; then
273 __git_reassemble_comp_words_by_ref "$exclude"
274 cur_=${words_[cword_]}
275 while [ $# -gt 0 ]; do
281 prev=${words_[$cword_-1]}
284 words=("${words_[@]}")
295 # Fills the COMPREPLY array with prefiltered words without any additional
297 # Callers must take care of providing only words that match the current word
298 # to be completed and adding any prefix and/or suffix (trailing space!), if
300 # 1: List of newline-separated matching completion words, complete with
309 # Similar to __gitcomp_direct, but appends to COMPREPLY instead.
310 # Callers must take care of providing only words that match the current word
311 # to be completed and adding any prefix and/or suffix (trailing space!), if
313 # 1: List of newline-separated matching completion words, complete with
315 __gitcomp_direct_append ()
324 local x i=${#COMPREPLY[@]}
326 if [[ "$x" == "$3"* ]]; then
327 COMPREPLY[i++]="$2$x$4"
338 # Generates completion reply, appending a space to possible completion words,
340 # It accepts 1 to 4 arguments:
341 # 1: List of possible completion words.
342 # 2: A prefix to be added to each possible completion word (optional).
343 # 3: Generate possible completion matches for this word (optional).
344 # 4: A suffix to be appended to each possible completion word (optional).
347 local cur_="${3-$cur}"
353 local c i=0 IFS=$' \t\n'
355 if [[ $c == "--" ]]; then
359 if [[ $c == "$cur_"* ]]; then
364 COMPREPLY[i++]="${2-}$c"
369 local c i=0 IFS=$' \t\n'
371 if [[ $c == "--" ]]; then
373 if [[ $c == "$cur_"* ]]; then
374 COMPREPLY[i++]="${2-}$c "
379 if [[ $c == "$cur_"* ]]; then
384 COMPREPLY[i++]="${2-}$c"
391 # Clear the variables caching builtins' options when (re-)sourcing
392 # the completion script.
393 if [[ -n ${ZSH_VERSION-} ]]; then
394 unset ${(M)${(k)parameters[@]}:#__gitcomp_builtin_*} 2>/dev/null
396 unset $(compgen -v __gitcomp_builtin_)
399 # This function is equivalent to
401 # __gitcomp "$(git xxx --git-completion-helper) ..."
403 # except that the output is cached. Accept 1-3 arguments:
404 # 1: the git command to execute, this is also the cache key
405 # 2: extra options to be added on top (e.g. negative forms)
406 # 3: options to be excluded
409 # spaces must be replaced with underscore for multi-word
410 # commands, e.g. "git remote add" becomes remote_add.
415 local var=__gitcomp_builtin_"${cmd/-/_}"
417 eval "options=\${$var-}"
419 if [ -z "$options" ]; then
420 local completion_helper
421 if [ "$GIT_COMPLETION_SHOW_ALL" = "1" ]; then
422 completion_helper="--git-completion-helper-all"
424 completion_helper="--git-completion-helper"
426 # leading and trailing spaces are significant to make
427 # option removal work correctly.
428 options=" $incl $(__git ${cmd/_/ } $completion_helper) " || return
431 options="${options/ $i / }"
433 eval "$var=\"$options\""
439 # Variation of __gitcomp_nl () that appends to the existing list of
440 # completion candidates, COMPREPLY.
441 __gitcomp_nl_append ()
444 __gitcompappend "$1" "${2-}" "${3-$cur}" "${4- }"
447 # Generates completion reply from newline-separated possible completion words
448 # by appending a space to all of them.
449 # It accepts 1 to 4 arguments:
450 # 1: List of possible completion words, separated by a single newline.
451 # 2: A prefix to be added to each possible completion word (optional).
452 # 3: Generate possible completion matches for this word (optional).
453 # 4: A suffix to be appended to each possible completion word instead of
454 # the default space (optional). If specified but empty, nothing is
459 __gitcomp_nl_append "$@"
462 # Fills the COMPREPLY array with prefiltered paths without any additional
464 # Callers must take care of providing only paths that match the current path
465 # to be completed and adding any prefix path components, if necessary.
466 # 1: List of newline-separated matching paths, complete with all prefix
468 __gitcomp_file_direct ()
474 # use a hack to enable file mode in bash < 4
475 compopt -o filenames +o nospace 2>/dev/null ||
476 compgen -f /non-existing-dir/ >/dev/null ||
480 # Generates completion reply with compgen from newline-separated possible
481 # completion filenames.
482 # It accepts 1 to 3 arguments:
483 # 1: List of possible completion filenames, separated by a single newline.
484 # 2: A directory prefix to be added to each possible completion filename
486 # 3: Generate possible completion matches for this word (optional).
491 # XXX does not work when the directory prefix contains a tilde,
492 # since tilde expansion is not applied.
493 # This means that COMPREPLY will be empty and Bash default
494 # completion will be used.
495 __gitcompadd "$1" "${2-}" "${3-$cur}" ""
497 # use a hack to enable file mode in bash < 4
498 compopt -o filenames +o nospace 2>/dev/null ||
499 compgen -f /non-existing-dir/ >/dev/null ||
503 # Execute 'git ls-files', unless the --committable option is specified, in
504 # which case it runs 'git diff-index' to find out the files that can be
505 # committed. It return paths relative to the directory specified in the first
506 # argument, and using the options specified in the second argument.
507 __git_ls_files_helper ()
509 if [ "$2" == "--committable" ]; then
510 __git -C "$1" -c core.quotePath=false diff-index \
511 --name-only --relative HEAD -- "${3//\\/\\\\}*"
513 # NOTE: $2 is not quoted in order to support multiple options
514 __git -C "$1" -c core.quotePath=false ls-files \
515 --exclude-standard $2 -- "${3//\\/\\\\}*"
520 # __git_index_files accepts 1 or 2 arguments:
521 # 1: Options to pass to ls-files (required).
522 # 2: A directory path (optional).
523 # If provided, only files within the specified directory are listed.
524 # Sub directories are never recursed. Path must have a trailing
526 # 3: List only paths matching this path component (optional).
529 local root="$2" match="$3"
531 __git_ls_files_helper "$root" "$1" "${match:-?}" |
532 awk -F / -v pfx="${2//\\/\\\\}" '{
537 if (substr(p, 1, 1) != "\"") {
538 # No special characters, easy!
543 # The path is quoted.
548 # Even when a directory name itself does not contain
549 # any special characters, it will still be quoted if
550 # any of its (stripped) trailing path components do.
551 # Because of this we may have seen the same directory
552 # both quoted and unquoted.
554 # We have seen the same directory unquoted,
561 function dequote(p, bs_idx, out, esc, esc_idx, dec) {
562 # Skip opening double quote.
565 # Interpret backslash escape sequences.
566 while ((bs_idx = index(p, "\\")) != 0) {
567 out = out substr(p, 1, bs_idx - 1)
568 esc = substr(p, bs_idx + 1, 1)
569 p = substr(p, bs_idx + 2)
571 if ((esc_idx = index("abtvfr\"\\", esc)) != 0) {
572 # C-style one-character escape sequence.
573 out = out substr("\a\b\t\v\f\r\"\\",
575 } else if (esc == "n") {
576 # Uh-oh, a newline character.
577 # We cannot reliably put a pathname
578 # containing a newline into COMPREPLY,
579 # and the newline would create a mess.
583 # Must be a \nnn octal value, then.
585 substr(p, 1, 1) * 8 + \
587 out = out sprintf("%c", dec)
591 # Drop closing double quote, if there is one.
592 # (There is not any if this is a directory, as it was
593 # already stripped with the trailing path components.)
594 if (substr(p, length(p), 1) == "\"")
595 out = out substr(p, 1, length(p) - 1)
603 # __git_complete_index_file requires 1 argument:
604 # 1: the options to pass to ls-file
606 # The exception is --committable, which finds the files appropriate commit.
607 __git_complete_index_file ()
609 local dequoted_word pfx="" cur_
613 case "$dequoted_word" in
615 pfx="${dequoted_word%/*}/"
616 cur_="${dequoted_word##*/}"
619 cur_="$dequoted_word"
622 __gitcomp_file_direct "$(__git_index_files "$1" "$pfx" "$cur_")"
625 # Lists branches from the local repository.
626 # 1: A prefix to be added to each listed branch (optional).
627 # 2: List only branches matching this word (optional; list all branches if
629 # 3: A suffix to be appended to each listed branch (optional).
632 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
634 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
635 "refs/heads/$cur_*" "refs/heads/$cur_*/**"
638 # Lists branches from remote repositories.
639 # 1: A prefix to be added to each listed branch (optional).
640 # 2: List only branches matching this word (optional; list all branches if
642 # 3: A suffix to be appended to each listed branch (optional).
643 __git_remote_heads ()
645 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
647 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
648 "refs/remotes/$cur_*" "refs/remotes/$cur_*/**"
651 # Lists tags from the local repository.
652 # Accepts the same positional parameters as __git_heads() above.
655 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
657 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
658 "refs/tags/$cur_*" "refs/tags/$cur_*/**"
661 # List unique branches from refs/remotes used for 'git checkout' and 'git
662 # switch' tracking DWIMery.
663 # 1: A prefix to be added to each listed branch (optional)
664 # 2: List only branches matching this word (optional; list all branches if
666 # 3: A suffix to be appended to each listed branch (optional).
667 __git_dwim_remote_heads ()
669 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
670 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
672 # employ the heuristic used by git checkout and git switch
673 # Try to find a remote branch that cur_es the completion word
674 # but only output if the branch name is unique
675 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
676 --sort="refname:strip=3" \
677 "refs/remotes/*/$cur_*" "refs/remotes/*/$cur_*/**" | \
681 # Lists refs from the local (by default) or from a remote repository.
682 # It accepts 0, 1 or 2 arguments:
683 # 1: The remote to list refs from (optional; ignored, if set but empty).
684 # Can be the name of a configured remote, a path, or a URL.
685 # 2: In addition to local refs, list unique branches from refs/remotes/ for
686 # 'git checkout's tracking DWIMery (optional; ignored, if set but empty).
687 # 3: A prefix to be added to each listed ref (optional).
688 # 4: List only refs matching this word (optional; list all refs if unset or
690 # 5: A suffix to be appended to each listed ref (optional; ignored, if set
693 # Use __git_complete_refs() instead.
696 local i hash dir track="${2-}"
697 local list_refs_from=path remote="${1-}"
699 local pfx="${3-}" cur_="${4-$cur}" sfx="${5-}"
701 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
704 dir="$__git_repo_path"
706 if [ -z "$remote" ]; then
707 if [ -z "$dir" ]; then
711 if __git_is_configured_remote "$remote"; then
712 # configured remote takes precedence over a
713 # local directory with the same name
714 list_refs_from=remote
715 elif [ -d "$remote/.git" ]; then
717 elif [ -d "$remote" ]; then
724 if [ "$list_refs_from" = path ]; then
725 if [[ "$cur_" == ^* ]]; then
734 refs=("$match*" "$match*/**")
738 for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD REBASE_HEAD; do
741 if [ -e "$dir/$i" ]; then
747 format="refname:strip=2"
748 refs=("refs/tags/$match*" "refs/tags/$match*/**"
749 "refs/heads/$match*" "refs/heads/$match*/**"
750 "refs/remotes/$match*" "refs/remotes/$match*/**")
753 __git_dir="$dir" __git for-each-ref --format="$fer_pfx%($format)$sfx" \
755 if [ -n "$track" ]; then
756 __git_dwim_remote_heads "$pfx" "$match" "$sfx"
762 __git ls-remote "$remote" "$match*" | \
763 while read -r hash i; do
766 *) echo "$pfx$i$sfx" ;;
771 if [ "$list_refs_from" = remote ]; then
773 $match*) echo "${pfx}HEAD$sfx" ;;
775 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
776 "refs/remotes/$remote/$match*" \
777 "refs/remotes/$remote/$match*/**"
781 $match*) query_symref="HEAD" ;;
783 __git ls-remote "$remote" $query_symref \
784 "refs/tags/$match*" "refs/heads/$match*" \
785 "refs/remotes/$match*" |
786 while read -r hash i; do
789 refs/*) echo "$pfx${i#refs/*/}$sfx" ;;
790 *) echo "$pfx$i$sfx" ;; # symbolic refs
798 # Completes refs, short and long, local and remote, symbolic and pseudo.
800 # Usage: __git_complete_refs [<option>]...
801 # --remote=<remote>: The remote to list refs from, can be the name of a
802 # configured remote, a path, or a URL.
803 # --dwim: List unique remote branches for 'git switch's tracking DWIMery.
804 # --pfx=<prefix>: A prefix to be added to each ref.
805 # --cur=<word>: The current ref to be completed. Defaults to the current
806 # word to be completed.
807 # --sfx=<suffix>: A suffix to be appended to each ref instead of the default
809 # --mode=<mode>: What set of refs to complete, one of 'refs' (the default) to
810 # complete all refs, 'heads' to complete only branches, or
811 # 'remote-heads' to complete only remote branches. Note that
812 # --remote is only compatible with --mode=refs.
813 __git_complete_refs ()
815 local remote= dwim= pfx= cur_="$cur" sfx=" " mode="refs"
817 while test $# != 0; do
819 --remote=*) remote="${1##--remote=}" ;;
820 --dwim) dwim="yes" ;;
821 # --track is an old spelling of --dwim
822 --track) dwim="yes" ;;
823 --pfx=*) pfx="${1##--pfx=}" ;;
824 --cur=*) cur_="${1##--cur=}" ;;
825 --sfx=*) sfx="${1##--sfx=}" ;;
826 --mode=*) mode="${1##--mode=}" ;;
832 # complete references based on the specified mode
835 __gitcomp_direct "$(__git_refs "$remote" "" "$pfx" "$cur_" "$sfx")" ;;
837 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" "$sfx")" ;;
839 __gitcomp_direct "$(__git_remote_heads "$pfx" "$cur_" "$sfx")" ;;
844 # Append DWIM remote branch names if requested
845 if [ "$dwim" = "yes" ]; then
846 __gitcomp_direct_append "$(__git_dwim_remote_heads "$pfx" "$cur_" "$sfx")"
850 # __git_refs2 requires 1 argument (to pass to __git_refs)
851 # Deprecated: use __git_complete_fetch_refspecs() instead.
855 for i in $(__git_refs "$1"); do
860 # Completes refspecs for fetching from a remote repository.
861 # 1: The remote repository.
862 # 2: A prefix to be added to each listed refspec (optional).
863 # 3: The ref to be completed as a refspec instead of the current word to be
864 # completed (optional)
865 # 4: A suffix to be appended to each listed refspec instead of the default
867 __git_complete_fetch_refspecs ()
869 local i remote="$1" pfx="${2-}" cur_="${3-$cur}" sfx="${4- }"
872 for i in $(__git_refs "$remote" "" "" "$cur_") ; do
878 # __git_refs_remotes requires 1 argument (to pass to ls-remote)
879 __git_refs_remotes ()
882 __git ls-remote "$1" 'refs/heads/*' | \
883 while read -r hash i; do
884 echo "$i:refs/remotes/$1/${i#refs/heads/}"
891 test -d "$__git_repo_path/remotes" && ls -1 "$__git_repo_path/remotes"
895 # Returns true if $1 matches the name of a configured remote, false otherwise.
896 __git_is_configured_remote ()
899 for remote in $(__git_remotes); do
900 if [ "$remote" = "$1" ]; then
907 __git_list_merge_strategies ()
909 LANG=C LC_ALL=C git merge -s help 2>&1 |
910 sed -n -e '/[Aa]vailable strategies are: /,/^$/{
919 __git_merge_strategies=
920 # 'git merge -s help' (and thus detection of the merge strategy
921 # list) fails, unfortunately, if run outside of any git working
922 # tree. __git_merge_strategies is set to the empty string in
923 # that case, and the detection will be repeated the next time it
925 __git_compute_merge_strategies ()
927 test -n "$__git_merge_strategies" ||
928 __git_merge_strategies=$(__git_list_merge_strategies)
931 __git_merge_strategy_options="ours theirs subtree subtree= patience
932 histogram diff-algorithm= ignore-space-change ignore-all-space
933 ignore-space-at-eol renormalize no-renormalize no-renames
934 find-renames find-renames= rename-threshold="
936 __git_complete_revlist_file ()
938 local dequoted_word pfx ls ref cur_="$cur"
947 __git_dequote "$cur_"
949 case "$dequoted_word" in
951 pfx="${dequoted_word%/*}"
952 cur_="${dequoted_word##*/}"
957 cur_="$dequoted_word"
962 case "$COMP_WORDBREAKS" in
964 *) pfx="$ref:$pfx" ;;
967 __gitcomp_file "$(__git ls-tree "$ls" \
973 pfx="${cur_%...*}..."
975 __git_complete_refs --pfx="$pfx" --cur="$cur_"
980 __git_complete_refs --pfx="$pfx" --cur="$cur_"
988 __git_complete_file ()
990 __git_complete_revlist_file
993 __git_complete_revlist ()
995 __git_complete_revlist_file
998 __git_complete_remote_or_refspec ()
1000 local cur_="$cur" cmd="${words[1]}"
1001 local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
1002 if [ "$cmd" = "remote" ]; then
1005 while [ $c -lt $cword ]; do
1008 --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
1009 -d|--delete) [ "$cmd" = "push" ] && lhs=0 ;;
1012 push) no_complete_refspec=1 ;;
1019 --multiple) no_complete_refspec=1; break ;;
1021 *) remote="$i"; break ;;
1025 if [ -z "$remote" ]; then
1026 __gitcomp_nl "$(__git_remotes)"
1029 if [ $no_complete_refspec = 1 ]; then
1032 [ "$remote" = "." ] && remote=
1035 case "$COMP_WORDBREAKS" in
1037 *) pfx="${cur_%%:*}:" ;;
1049 if [ $lhs = 1 ]; then
1050 __git_complete_fetch_refspecs "$remote" "$pfx" "$cur_"
1052 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1056 if [ $lhs = 1 ]; then
1057 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
1059 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1063 if [ $lhs = 1 ]; then
1064 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1066 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
1072 __git_complete_strategy ()
1074 __git_compute_merge_strategies
1077 __gitcomp "$__git_merge_strategies"
1081 __gitcomp "$__git_merge_strategy_options"
1087 __gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
1090 --strategy-option=*)
1091 __gitcomp "$__git_merge_strategy_options" "" "${cur##--strategy-option=}"
1099 __git_compute_all_commands ()
1101 test -n "$__git_all_commands" ||
1102 __git_all_commands=$(__git --list-cmds=main,others,alias,nohelpers)
1105 # Lists all set config variables starting with the given section prefix,
1106 # with the prefix removed.
1107 __git_get_config_variables ()
1109 local section="$1" i IFS=$'\n'
1110 for i in $(__git config --name-only --get-regexp "^$section\..*"); do
1111 echo "${i#$section.}"
1115 __git_pretty_aliases ()
1117 __git_get_config_variables "pretty"
1120 # __git_aliased_command requires 1 argument
1121 __git_aliased_command ()
1123 local word cmdline=$(__git config --get "alias.$1")
1124 for word in $cmdline; do
1130 \!*) : shell command alias ;;
1132 *=*) : setting env ;;
1133 git) : git itself ;;
1134 \(\)) : skip parens of shell function definition ;;
1135 {) : skip start of shell helper function ;;
1136 :) : skip null command ;;
1137 \'*) : skip opening quote after sh -c ;;
1145 # Check whether one of the given words is present on the command line,
1146 # and print the first word found.
1148 # Usage: __git_find_on_cmdline [<option>]... "<wordlist>"
1149 # --show-idx: Optionally show the index of the found word in the $words array.
1150 __git_find_on_cmdline ()
1152 local word c=1 show_idx
1154 while test $# -gt 1; do
1156 --show-idx) show_idx=y ;;
1163 while [ $c -lt $cword ]; do
1164 for word in $wordlist; do
1165 if [ "$word" = "${words[c]}" ]; then
1166 if [ -n "${show_idx-}" ]; then
1178 # Similar to __git_find_on_cmdline, except that it loops backwards and thus
1179 # prints the *last* word found. Useful for finding which of two options that
1180 # supersede each other came last, such as "--guess" and "--no-guess".
1182 # Usage: __git_find_last_on_cmdline [<option>]... "<wordlist>"
1183 # --show-idx: Optionally show the index of the found word in the $words array.
1184 __git_find_last_on_cmdline ()
1186 local word c=$cword show_idx
1188 while test $# -gt 1; do
1190 --show-idx) show_idx=y ;;
1197 while [ $c -gt 1 ]; do
1199 for word in $wordlist; do
1200 if [ "$word" = "${words[c]}" ]; then
1201 if [ -n "$show_idx" ]; then
1212 # Echo the value of an option set on the command line or config
1214 # $1: short option name
1215 # $2: long option name including =
1216 # $3: list of possible values
1217 # $4: config string (optional)
1220 # result="$(__git_get_option_value "-d" "--do-something=" \
1221 # "yes no" "core.doSomething")"
1223 # result is then either empty (no option set) or "yes" or "no"
1225 # __git_get_option_value requires 3 arguments
1226 __git_get_option_value ()
1228 local c short_opt long_opt val
1229 local result= values config_key word
1237 while [ $c -ge 0 ]; do
1239 for val in $values; do
1240 if [ "$short_opt$val" = "$word" ] ||
1241 [ "$long_opt$val" = "$word" ]; then
1249 if [ -n "$config_key" ] && [ -z "$result" ]; then
1250 result="$(__git config "$config_key")"
1256 __git_has_doubledash ()
1259 while [ $c -lt $cword ]; do
1260 if [ "--" = "${words[c]}" ]; then
1268 # Try to count non option arguments passed on the command line for the
1269 # specified git command.
1270 # When options are used, it is necessary to use the special -- option to
1271 # tell the implementation were non option arguments begin.
1272 # XXX this can not be improved, since options can appear everywhere, as
1276 # __git_count_arguments requires 1 argument: the git command executed.
1277 __git_count_arguments ()
1281 # Skip "git" (first argument)
1282 for ((i=1; i < ${#words[@]}; i++)); do
1287 # Good; we can assume that the following are only non
1292 # Skip the specified git command and discard git
1305 __git_whitespacelist="nowarn warn error error-all fix"
1306 __git_patchformat="mbox stgit stgit-series hg mboxrd"
1307 __git_showcurrentpatch="diff raw"
1308 __git_am_inprogress_options="--skip --continue --resolved --abort --quit --show-current-patch"
1312 __git_find_repo_path
1313 if [ -d "$__git_repo_path"/rebase-apply ]; then
1314 __gitcomp "$__git_am_inprogress_options"
1319 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1323 __gitcomp "$__git_patchformat" "" "${cur##--patch-format=}"
1326 --show-current-patch=*)
1327 __gitcomp "$__git_showcurrentpatch" "" "${cur##--show-current-patch=}"
1331 __gitcomp_builtin am "" \
1332 "$__git_am_inprogress_options"
1341 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1345 __gitcomp_builtin apply
1354 __gitcomp "+x -x" "" "${cur##--chmod=}"
1358 __gitcomp_builtin add
1362 local complete_opt="--others --modified --directory --no-empty-directory"
1363 if test -n "$(__git_find_on_cmdline "-u --update")"
1365 complete_opt="--modified"
1367 __git_complete_index_file "$complete_opt"
1374 __gitcomp "$(git archive --list)" "" "${cur##--format=}"
1378 __gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
1382 __gitcomp_builtin archive "--format= --list --verbose --prefix= --worktree-attributes"
1391 __git_has_doubledash && return
1393 local subcommands="start bad good skip reset visualize replay log run"
1394 local subcommand="$(__git_find_on_cmdline "$subcommands")"
1395 if [ -z "$subcommand" ]; then
1396 __git_find_repo_path
1397 if [ -f "$__git_repo_path"/BISECT_START ]; then
1398 __gitcomp "$subcommands"
1400 __gitcomp "replay start"
1405 case "$subcommand" in
1406 bad|good|reset|skip|start)
1414 __git_ref_fieldlist="refname objecttype objectsize objectname upstream push HEAD symref"
1418 local i c=1 only_local_ref="n" has_r="n"
1420 while [ $c -lt $cword ]; do
1423 -d|--delete|-m|--move) only_local_ref="y" ;;
1424 -r|--remotes) has_r="y" ;;
1430 --set-upstream-to=*)
1431 __git_complete_refs --cur="${cur##--set-upstream-to=}"
1434 __gitcomp_builtin branch
1437 if [ $only_local_ref = "y" -a $has_r = "n" ]; then
1438 __gitcomp_direct "$(__git_heads "" "$cur" " ")"
1448 local cmd="${words[2]}"
1451 __gitcomp "create list-heads verify unbundle"
1454 # looking for a file
1459 __git_complete_revlist
1466 # Helper function to decide whether or not we should enable DWIM logic for
1467 # git-switch and git-checkout.
1469 # To decide between the following rules in decreasing priority order:
1470 # - the last provided of "--guess" or "--no-guess" explicitly enable or
1471 # disable completion of DWIM logic respectively.
1472 # - If checkout.guess is false, disable completion of DWIM logic.
1473 # - If the --no-track option is provided, take this as a hint to disable the
1474 # DWIM completion logic
1475 # - If GIT_COMPLETION_CHECKOUT_NO_GUESS is set, disable the DWIM completion
1476 # logic, as requested by the user.
1477 # - Enable DWIM logic otherwise.
1479 __git_checkout_default_dwim_mode ()
1481 local last_option dwim_opt="--dwim"
1483 if [ "${GIT_COMPLETION_CHECKOUT_NO_GUESS-}" = "1" ]; then
1487 # --no-track disables DWIM, but with lower priority than
1488 # --guess/--no-guess/checkout.guess
1489 if [ -n "$(__git_find_on_cmdline "--no-track")" ]; then
1493 # checkout.guess = false disables DWIM, but with lower priority than
1494 # --guess/--no-guess
1495 if [ "$(__git config --type=bool checkout.guess)" = "false" ]; then
1499 # Find the last provided --guess or --no-guess
1500 last_option="$(__git_find_last_on_cmdline "--guess --no-guess")"
1501 case "$last_option" in
1515 __git_has_doubledash && return
1517 local dwim_opt="$(__git_checkout_default_dwim_mode)"
1521 # Complete local branches (and DWIM branch
1522 # remote branch names) for an option argument
1523 # specifying a new branch name. This is for
1524 # convenience, assuming new branches are
1525 # possibly based on pre-existing branch names.
1526 __git_complete_refs $dwim_opt --mode="heads"
1535 __gitcomp "diff3 merge" "" "${cur##--conflict=}"
1538 __gitcomp_builtin checkout
1541 # At this point, we've already handled special completion for
1542 # the arguments to -b/-B, and --orphan. There are 3 main
1543 # things left we can possibly complete:
1544 # 1) a start-point for -b/-B, -d/--detach, or --orphan
1545 # 2) a remote head, for --track
1546 # 3) an arbitrary reference, possibly including DWIM names
1549 if [ -n "$(__git_find_on_cmdline "-b -B -d --detach --orphan")" ]; then
1550 __git_complete_refs --mode="refs"
1551 elif [ -n "$(__git_find_on_cmdline "--track")" ]; then
1552 __git_complete_refs --mode="remote-heads"
1554 __git_complete_refs $dwim_opt --mode="refs"
1560 __git_sequencer_inprogress_options="--continue --quit --abort --skip"
1562 __git_cherry_pick_inprogress_options=$__git_sequencer_inprogress_options
1566 __git_find_repo_path
1567 if [ -f "$__git_repo_path"/CHERRY_PICK_HEAD ]; then
1568 __gitcomp "$__git_cherry_pick_inprogress_options"
1572 __git_complete_strategy && return
1576 __gitcomp_builtin cherry-pick "" \
1577 "$__git_cherry_pick_inprogress_options"
1589 __gitcomp_builtin clean
1594 # XXX should we check for -x option ?
1595 __git_complete_index_file "--others --directory"
1602 __git_complete_config_variable_name_and_value
1608 __git_complete_config_variable_name_and_value \
1609 --cur="${cur##--config=}"
1613 __gitcomp_builtin clone
1619 __git_untracked_file_modes="all no normal"
1632 __gitcomp "default scissors strip verbatim whitespace
1633 " "" "${cur##--cleanup=}"
1636 --reuse-message=*|--reedit-message=*|\
1637 --fixup=*|--squash=*)
1638 __git_complete_refs --cur="${cur#*=}"
1641 --untracked-files=*)
1642 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
1646 __gitcomp_builtin commit
1650 if __git rev-parse --verify --quiet HEAD >/dev/null; then
1651 __git_complete_index_file "--committable"
1653 # This is the first commit
1654 __git_complete_index_file "--cached"
1662 __gitcomp_builtin describe
1668 __git_diff_algorithms="myers minimal patience histogram"
1670 __git_diff_submodule_formats="diff log short"
1672 __git_color_moved_opts="no default plain blocks zebra dimmed-zebra"
1674 __git_color_moved_ws_opts="no ignore-space-at-eol ignore-space-change
1675 ignore-all-space allow-indentation-change"
1677 __git_diff_common_options="--stat --numstat --shortstat --summary
1678 --patch-with-stat --name-only --name-status --color
1679 --no-color --color-words --no-renames --check
1680 --color-moved --color-moved= --no-color-moved
1681 --color-moved-ws= --no-color-moved-ws
1682 --full-index --binary --abbrev --diff-filter=
1683 --find-copies-harder --ignore-cr-at-eol
1684 --text --ignore-space-at-eol --ignore-space-change
1685 --ignore-all-space --ignore-blank-lines --exit-code
1686 --quiet --ext-diff --no-ext-diff
1687 --no-prefix --src-prefix= --dst-prefix=
1688 --inter-hunk-context=
1689 --patience --histogram --minimal
1690 --raw --word-diff --word-diff-regex=
1691 --dirstat --dirstat= --dirstat-by-file
1692 --dirstat-by-file= --cumulative
1694 --submodule --submodule= --ignore-submodules
1695 --indent-heuristic --no-indent-heuristic
1696 --textconv --no-textconv
1700 __git_diff_difftool_options="--cached --staged --pickaxe-all --pickaxe-regex
1701 --base --ours --theirs --no-index --relative --merge-base
1702 $__git_diff_common_options"
1706 __git_has_doubledash && return
1710 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1714 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1718 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
1722 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
1726 __gitcomp "$__git_diff_difftool_options"
1730 __git_complete_revlist_file
1733 __git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff
1734 tkdiff vimdiff nvimdiff gvimdiff xxdiff araxis p4merge
1735 bc codecompare smerge
1740 __git_has_doubledash && return
1744 __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1748 __gitcomp_builtin difftool "$__git_diff_difftool_options"
1752 __git_complete_revlist_file
1755 __git_fetch_recurse_submodules="yes on-demand no"
1760 --recurse-submodules=*)
1761 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1765 __gitcomp "blob:none blob:limit= sparse:oid=" "" "${cur##--filter=}"
1769 __gitcomp_builtin fetch
1773 __git_complete_remote_or_refspec
1776 __git_format_patch_extra_options="
1777 --full-index --not --all --no-prefix --src-prefix=
1778 --dst-prefix= --notes
1781 _git_format_patch ()
1787 " "" "${cur##--thread=}"
1790 --base=*|--interdiff=*|--range-diff=*)
1791 __git_complete_refs --cur="${cur#--*=}"
1795 __gitcomp_builtin format-patch "$__git_format_patch_extra_options"
1799 __git_complete_revlist
1806 __gitcomp_builtin fsck
1817 # Lists matching symbol names from a tag (as in ctags) file.
1818 # 1: List symbol names matching this word.
1819 # 2: The tag file to list symbol names from.
1820 # 3: A prefix to be added to each listed symbol name (optional).
1821 # 4: A suffix to be appended to each listed symbol name (optional).
1822 __git_match_ctag () {
1823 awk -v pfx="${3-}" -v sfx="${4-}" "
1824 /^${1//\//\\/}/ { print pfx \$1 sfx }
1828 # Complete symbol names from a tag file.
1829 # Usage: __git_complete_symbol [<option>]...
1830 # --tags=<file>: The tag file to list symbol names from instead of the
1832 # --pfx=<prefix>: A prefix to be added to each symbol name.
1833 # --cur=<word>: The current symbol name to be completed. Defaults to
1834 # the current word to be completed.
1835 # --sfx=<suffix>: A suffix to be appended to each symbol name instead
1836 # of the default space.
1837 __git_complete_symbol () {
1838 local tags=tags pfx="" cur_="${cur-}" sfx=" "
1840 while test $# != 0; do
1842 --tags=*) tags="${1##--tags=}" ;;
1843 --pfx=*) pfx="${1##--pfx=}" ;;
1844 --cur=*) cur_="${1##--cur=}" ;;
1845 --sfx=*) sfx="${1##--sfx=}" ;;
1851 if test -r "$tags"; then
1852 __gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
1858 __git_has_doubledash && return
1862 __gitcomp_builtin grep
1867 case "$cword,$prev" in
1869 __git_complete_symbol && return
1880 __gitcomp_builtin help
1884 if test -n "$GIT_TESTING_ALL_COMMAND_LIST"
1886 __gitcomp "$GIT_TESTING_ALL_COMMAND_LIST $(__git --list-cmds=alias,list-guide) gitk"
1888 __gitcomp "$(__git --list-cmds=main,nohelpers,alias,list-guide) gitk"
1897 false true umask group all world everybody
1898 " "" "${cur##--shared=}"
1902 __gitcomp_builtin init
1912 __gitcomp_builtin ls-files
1917 # XXX ignore options like --modified and always suggest all cached
1919 __git_complete_index_file "--cached"
1926 __gitcomp_builtin ls-remote
1930 __gitcomp_nl "$(__git_remotes)"
1937 __gitcomp_builtin ls-tree
1945 # Options that go well for log, shortlog and gitk
1946 __git_log_common_options="
1948 --branches --tags --remotes
1949 --first-parent --merges --no-merges
1951 --max-age= --since= --after=
1952 --min-age= --until= --before=
1953 --min-parents= --max-parents=
1954 --no-min-parents --no-max-parents
1956 # Options that go well for log and gitk (not shortlog)
1957 __git_log_gitk_options="
1958 --dense --sparse --full-history
1959 --simplify-merges --simplify-by-decoration
1960 --left-right --notes --no-notes
1962 # Options that go well for log and shortlog (not gitk)
1963 __git_log_shortlog_options="
1964 --author= --committer= --grep=
1965 --all-match --invert-grep
1968 __git_log_pretty_formats="oneline short medium full fuller reference email raw format: tformat: mboxrd"
1969 __git_log_date_formats="relative iso8601 iso8601-strict rfc2822 short local default raw unix format:"
1973 __git_has_doubledash && return
1974 __git_find_repo_path
1977 if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
1980 case "$prev,$cur" in
1982 return # fall back to Bash filename completion
1985 __git_complete_symbol --cur="${cur#:}" --sfx=":"
1989 __git_complete_symbol
1994 --pretty=*|--format=*)
1995 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2000 __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
2004 __gitcomp "full short no" "" "${cur##--decorate=}"
2008 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
2012 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
2016 __gitcomp "sorted unsorted" "" "${cur##--no-walk=}"
2021 $__git_log_common_options
2022 $__git_log_shortlog_options
2023 $__git_log_gitk_options
2024 --root --topo-order --date-order --reverse
2025 --follow --full-diff
2026 --abbrev-commit --no-abbrev-commit --abbrev=
2027 --relative-date --date=
2028 --pretty= --format= --oneline
2033 --decorate --decorate= --no-decorate
2035 --no-walk --no-walk= --do-walk
2036 --parents --children
2037 --expand-tabs --expand-tabs= --no-expand-tabs
2039 $__git_diff_common_options
2040 --pickaxe-all --pickaxe-regex
2045 return # fall back to Bash filename completion
2048 __git_complete_symbol --cur="${cur#-L:}" --sfx=":"
2052 __git_complete_symbol --pfx="-G" --cur="${cur#-G}"
2056 __git_complete_symbol --pfx="-S" --cur="${cur#-S}"
2060 __git_complete_revlist
2065 __git_complete_strategy && return
2069 __gitcomp_builtin merge
2079 __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
2083 __gitcomp "--tool= --prompt --no-prompt --gui --no-gui"
2093 __gitcomp_builtin merge-base
2104 __gitcomp_builtin mv
2109 if [ $(__git_count_arguments "mv") -gt 0 ]; then
2110 # We need to show both cached and untracked files (including
2111 # empty directories) since this may not be the last argument.
2112 __git_complete_index_file "--cached --others --directory"
2114 __git_complete_index_file "--cached"
2120 local subcommands='add append copy edit get-ref list merge prune remove show'
2121 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2123 case "$subcommand,$cur" in
2125 __gitcomp_builtin notes
2133 __gitcomp "$subcommands --ref"
2137 *,--reuse-message=*|*,--reedit-message=*)
2138 __git_complete_refs --cur="${cur#*=}"
2141 __gitcomp_builtin notes_$subcommand
2144 # this command does not take a ref, do not complete it
2160 __git_complete_strategy && return
2163 --recurse-submodules=*)
2164 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
2168 __gitcomp_builtin pull
2173 __git_complete_remote_or_refspec
2176 __git_push_recurse_submodules="check on-demand only"
2178 __git_complete_force_with_lease ()
2186 __git_complete_refs --cur="${cur_#*:}"
2189 __git_complete_refs --cur="$cur_"
2198 __gitcomp_nl "$(__git_remotes)"
2201 --recurse-submodules)
2202 __gitcomp "$__git_push_recurse_submodules"
2208 __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
2211 --recurse-submodules=*)
2212 __gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
2215 --force-with-lease=*)
2216 __git_complete_force_with_lease "${cur##--force-with-lease=}"
2220 __gitcomp_builtin push
2224 __git_complete_remote_or_refspec
2232 --creation-factor= --no-dual-color
2233 $__git_diff_common_options
2238 __git_complete_revlist
2241 __git_rebase_inprogress_options="--continue --skip --abort --quit --show-current-patch"
2242 __git_rebase_interactive_inprogress_options="$__git_rebase_inprogress_options --edit-todo"
2246 __git_find_repo_path
2247 if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
2248 __gitcomp "$__git_rebase_interactive_inprogress_options"
2250 elif [ -d "$__git_repo_path"/rebase-apply ] || \
2251 [ -d "$__git_repo_path"/rebase-merge ]; then
2252 __gitcomp "$__git_rebase_inprogress_options"
2255 __git_complete_strategy && return
2258 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
2262 __git_complete_refs --cur="${cur##--onto=}"
2266 __gitcomp_builtin rebase "" \
2267 "$__git_rebase_interactive_inprogress_options"
2276 local subcommands="show delete expire"
2277 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2279 if [ -z "$subcommand" ]; then
2280 __gitcomp "$subcommands"
2286 __git_send_email_confirm_options="always never auto cc compose"
2287 __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
2292 --to|--cc|--bcc|--from)
2293 __gitcomp "$(__git send-email --dump-aliases)"
2301 $__git_send_email_confirm_options
2302 " "" "${cur##--confirm=}"
2307 $__git_send_email_suppresscc_options
2308 " "" "${cur##--suppress-cc=}"
2312 --smtp-encryption=*)
2313 __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
2319 " "" "${cur##--thread=}"
2322 --to=*|--cc=*|--bcc=*|--from=*)
2323 __gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
2327 __gitcomp_builtin send-email "--annotate --bcc --cc --cc-cmd --chain-reply-to
2328 --compose --confirm= --dry-run --envelope-sender
2330 --in-reply-to --no-chain-reply-to --no-signed-off-by-cc
2331 --no-suppress-from --no-thread --quiet --reply-to
2332 --signed-off-by-cc --smtp-pass --smtp-server
2333 --smtp-server-port --smtp-encryption= --smtp-user
2334 --subject --suppress-cc= --suppress-from --thread --to
2335 --validate --no-validate
2336 $__git_format_patch_extra_options"
2340 __git_complete_revlist
2351 local untracked_state
2354 --ignore-submodules=*)
2355 __gitcomp "none untracked dirty all" "" "${cur##--ignore-submodules=}"
2358 --untracked-files=*)
2359 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
2364 always never auto column row plain dense nodense
2365 " "" "${cur##--column=}"
2369 __gitcomp_builtin status
2374 untracked_state="$(__git_get_option_value "-u" "--untracked-files=" \
2375 "$__git_untracked_file_modes" "status.showUntrackedFiles")"
2377 case "$untracked_state" in
2379 # --ignored option does not matter
2383 complete_opt="--cached --directory --no-empty-directory --others"
2385 if [ -n "$(__git_find_on_cmdline "--ignored")" ]; then
2386 complete_opt="$complete_opt --ignored --exclude=*"
2391 __git_complete_index_file "$complete_opt"
2396 local dwim_opt="$(__git_checkout_default_dwim_mode)"
2400 # Complete local branches (and DWIM branch
2401 # remote branch names) for an option argument
2402 # specifying a new branch name. This is for
2403 # convenience, assuming new branches are
2404 # possibly based on pre-existing branch names.
2405 __git_complete_refs $dwim_opt --mode="heads"
2414 __gitcomp "diff3 merge" "" "${cur##--conflict=}"
2417 __gitcomp_builtin switch
2420 # Unlike in git checkout, git switch --orphan does not take
2421 # a start point. Thus we really have nothing to complete after
2423 if [ -n "$(__git_find_on_cmdline "--orphan")" ]; then
2427 # At this point, we've already handled special completion for
2428 # -c/-C, and --orphan. There are 3 main things left to
2430 # 1) a start-point for -c/-C or -d/--detach
2431 # 2) a remote head, for --track
2432 # 3) a branch name, possibly including DWIM remote branches
2434 if [ -n "$(__git_find_on_cmdline "-c -C -d --detach")" ]; then
2435 __git_complete_refs --mode="refs"
2436 elif [ -n "$(__git_find_on_cmdline "--track")" ]; then
2437 __git_complete_refs --mode="remote-heads"
2439 __git_complete_refs $dwim_opt --mode="heads"
2445 __git_config_get_set_variables ()
2447 local prevword word config_file= c=$cword
2448 while [ $c -gt 1 ]; do
2451 --system|--global|--local|--file=*)
2456 config_file="$word $prevword"
2464 __git config $config_file --name-only --list
2468 __git_compute_config_vars ()
2470 test -n "$__git_config_vars" ||
2471 __git_config_vars="$(git help --config-for-completion | sort -u)"
2474 # Completes possible values of various configuration variables.
2476 # Usage: __git_complete_config_variable_value [<option>]...
2477 # --varname=<word>: The name of the configuration variable whose value is
2478 # to be completed. Defaults to the previous word on the
2480 # --cur=<word>: The current value to be completed. Defaults to the current
2481 # word to be completed.
2482 __git_complete_config_variable_value ()
2484 local varname="$prev" cur_="$cur"
2486 while test $# != 0; do
2488 --varname=*) varname="${1##--varname=}" ;;
2489 --cur=*) cur_="${1##--cur=}" ;;
2495 if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
2496 varname="${varname,,}"
2498 varname="$(echo "$varname" |tr A-Z a-z)"
2502 branch.*.remote|branch.*.pushremote)
2503 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2507 __git_complete_refs --cur="$cur_"
2511 __gitcomp "false true merges preserve interactive" "" "$cur_"
2515 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2519 local remote="${varname#remote.}"
2520 remote="${remote%.fetch}"
2521 if [ -z "$cur_" ]; then
2522 __gitcomp_nl "refs/heads/" "" "" ""
2525 __gitcomp_nl "$(__git_refs_remotes "$remote")" "" "$cur_"
2529 local remote="${varname#remote.}"
2530 remote="${remote%.push}"
2531 __gitcomp_nl "$(__git for-each-ref \
2532 --format='%(refname):%(refname)' refs/heads)" "" "$cur_"
2535 pull.twohead|pull.octopus)
2536 __git_compute_merge_strategies
2537 __gitcomp "$__git_merge_strategies" "" "$cur_"
2541 __gitcomp "false true" "" "$cur_"
2546 normal black red green yellow blue magenta cyan white
2547 bold dim ul blink reverse
2552 __gitcomp "false true always never auto" "" "$cur_"
2556 __gitcomp "$__git_diff_submodule_formats" "" "$cur_"
2560 __gitcomp "man info web html" "" "$cur_"
2564 __gitcomp "$__git_log_date_formats" "" "$cur_"
2567 sendemail.aliasfiletype)
2568 __gitcomp "mutt mailrc pine elm gnus" "" "$cur_"
2572 __gitcomp "$__git_send_email_confirm_options" "" "$cur_"
2575 sendemail.suppresscc)
2576 __gitcomp "$__git_send_email_suppresscc_options" "" "$cur_"
2579 sendemail.transferencoding)
2580 __gitcomp "7bit 8bit quoted-printable base64" "" "$cur_"
2589 # Completes configuration sections, subsections, variable names.
2591 # Usage: __git_complete_config_variable_name [<option>]...
2592 # --cur=<word>: The current configuration section/variable name to be
2593 # completed. Defaults to the current word to be completed.
2594 # --sfx=<suffix>: A suffix to be appended to each fully completed
2595 # configuration variable name (but not to sections or
2596 # subsections) instead of the default space.
2597 __git_complete_config_variable_name ()
2599 local cur_="$cur" sfx
2601 while test $# != 0; do
2603 --cur=*) cur_="${1##--cur=}" ;;
2604 --sfx=*) sfx="${1##--sfx=}" ;;
2612 local pfx="${cur_%.*}."
2614 __gitcomp "remote pushRemote merge mergeOptions rebase" "$pfx" "$cur_" "$sfx"
2618 local pfx="${cur%.*}."
2620 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
2621 __gitcomp_nl_append $'autoSetupMerge\nautoSetupRebase\n' "$pfx" "$cur_" "$sfx"
2625 local pfx="${cur_%.*}."
2628 argPrompt cmd confirm needsFile noConsole noRescan
2629 prompt revPrompt revUnmerged title
2630 " "$pfx" "$cur_" "$sfx"
2634 local pfx="${cur_%.*}."
2636 __gitcomp "cmd path" "$pfx" "$cur_" "$sfx"
2640 local pfx="${cur_%.*}."
2642 __gitcomp "cmd path" "$pfx" "$cur_" "$sfx"
2646 local pfx="${cur_%.*}."
2648 __gitcomp "cmd path trustExitCode" "$pfx" "$cur_" "$sfx"
2652 local pfx="${cur_%.*}."
2654 __git_compute_all_commands
2655 __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_" "$sfx"
2659 local pfx="${cur_%.*}."
2662 url proxy fetch push mirror skipDefaultUpdate
2663 receivepack uploadpack tagOpt pushurl
2664 " "$pfx" "$cur_" "$sfx"
2668 local pfx="${cur_%.*}."
2670 __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
2671 __gitcomp_nl_append "pushDefault" "$pfx" "$cur_" "$sfx"
2675 local pfx="${cur_%.*}."
2677 __gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_" "$sfx"
2681 __git_compute_config_vars
2682 __gitcomp "$__git_config_vars" "" "$cur_" "$sfx"
2685 __git_compute_config_vars
2686 __gitcomp "$(echo "$__git_config_vars" |
2699 # Completes '='-separated configuration sections/variable names and values
2700 # for 'git -c section.name=value'.
2702 # Usage: __git_complete_config_variable_name_and_value [<option>]...
2703 # --cur=<word>: The current configuration section/variable name/value to be
2704 # completed. Defaults to the current word to be completed.
2705 __git_complete_config_variable_name_and_value ()
2709 while test $# != 0; do
2711 --cur=*) cur_="${1##--cur=}" ;;
2719 __git_complete_config_variable_value \
2720 --varname="${cur_%%=*}" --cur="${cur_#*=}"
2723 __git_complete_config_variable_name --cur="$cur_" --sfx='='
2731 --get|--get-all|--unset|--unset-all)
2732 __gitcomp_nl "$(__git_config_get_set_variables)"
2736 __git_complete_config_variable_value
2742 __gitcomp_builtin config
2745 __git_complete_config_variable_name
2753 add rename remove set-head set-branches
2754 get-url set-url show prune update
2756 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2757 if [ -z "$subcommand" ]; then
2760 __gitcomp_builtin remote
2763 __gitcomp "$subcommands"
2769 case "$subcommand,$cur" in
2771 __gitcomp_builtin remote_add
2776 __gitcomp_builtin remote_set-head
2779 __gitcomp_builtin remote_set-branches
2781 set-head,*|set-branches,*)
2782 __git_complete_remote_or_refspec
2785 __gitcomp_builtin remote_update
2788 __gitcomp "$(__git_remotes) $(__git_get_config_variables "remotes")"
2791 __gitcomp_builtin remote_set-url
2794 __gitcomp_builtin remote_get-url
2797 __gitcomp_builtin remote_prune
2800 __gitcomp_nl "$(__git_remotes)"
2809 __gitcomp "short medium long" "" "${cur##--format=}"
2813 __gitcomp_builtin replace
2822 local subcommands="clear forget diff remaining status gc"
2823 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2824 if test -z "$subcommand"
2826 __gitcomp "$subcommands"
2833 __git_has_doubledash && return
2837 __gitcomp_builtin reset
2855 __gitcomp "diff3 merge" "" "${cur##--conflict=}"
2858 __git_complete_refs --cur="${cur##--source=}"
2861 __gitcomp_builtin restore
2866 __git_revert_inprogress_options=$__git_sequencer_inprogress_options
2870 __git_find_repo_path
2871 if [ -f "$__git_repo_path"/REVERT_HEAD ]; then
2872 __gitcomp "$__git_revert_inprogress_options"
2875 __git_complete_strategy && return
2878 __gitcomp_builtin revert "" \
2879 "$__git_revert_inprogress_options"
2890 __gitcomp_builtin rm
2895 __git_complete_index_file "--cached"
2900 __git_has_doubledash && return
2905 $__git_log_common_options
2906 $__git_log_shortlog_options
2907 --numbered --summary --email
2912 __git_complete_revlist
2917 __git_has_doubledash && return
2920 --pretty=*|--format=*)
2921 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2926 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
2930 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
2934 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
2938 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
2942 __gitcomp "--pretty= --format= --abbrev-commit --no-abbrev-commit
2943 --oneline --show-signature
2944 --expand-tabs --expand-tabs= --no-expand-tabs
2945 $__git_diff_common_options
2950 __git_complete_revlist_file
2957 __gitcomp_builtin show-branch
2961 __git_complete_revlist
2964 _git_sparse_checkout ()
2966 local subcommands="list init set disable"
2967 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2968 if [ -z "$subcommand" ]; then
2969 __gitcomp "$subcommands"
2973 case "$subcommand,$cur" in
2987 local save_opts='--all --keep-index --no-keep-index --quiet --patch --include-untracked'
2988 local subcommands='push list show apply clear drop pop create branch'
2989 local subcommand="$(__git_find_on_cmdline "$subcommands save")"
2990 if [ -z "$subcommand" -a -n "$(__git_find_on_cmdline "-p")" ]; then
2993 if [ -z "$subcommand" ]; then
2996 __gitcomp "$save_opts"
2999 if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
3004 if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
3005 __gitcomp "$subcommands"
3010 case "$subcommand,$cur" in
3012 __gitcomp "$save_opts --message"
3015 __gitcomp "$save_opts"
3018 __gitcomp "--index --quiet"
3024 __gitcomp "--name-status --oneline --patch-with-stat"
3027 __gitcomp "$__git_diff_common_options"
3032 if [ $cword -eq 3 ]; then
3035 __gitcomp_nl "$(__git stash list \
3036 | sed -n -e 's/:.*//p')"
3039 show,*|apply,*|drop,*|pop,*)
3040 __gitcomp_nl "$(__git stash list \
3041 | sed -n -e 's/:.*//p')"
3051 __git_has_doubledash && return
3053 local subcommands="add status init deinit update set-branch set-url summary foreach sync absorbgitdirs"
3054 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3055 if [ -z "$subcommand" ]; then
3061 __gitcomp "$subcommands"
3067 case "$subcommand,$cur" in
3069 __gitcomp "--branch --force --name --reference --depth"
3072 __gitcomp "--cached --recursive"
3075 __gitcomp "--force --all"
3079 --init --remote --no-fetch
3080 --recommend-shallow --no-recommend-shallow
3081 --force --rebase --merge --reference --depth --recursive --jobs
3085 __gitcomp "--default --branch"
3088 __gitcomp "--cached --files --summary-limit"
3090 foreach,--*|sync,--*)
3091 __gitcomp "--recursive"
3101 init fetch clone rebase dcommit log find-rev
3102 set-tree commit-diff info create-ignore propget
3103 proplist show-ignore show-externals branch tag blame
3104 migrate mkdirs reset gc
3106 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3107 if [ -z "$subcommand" ]; then
3108 __gitcomp "$subcommands"
3110 local remote_opts="--username= --config-dir= --no-auth-cache"
3112 --follow-parent --authors-file= --repack=
3113 --no-metadata --use-svm-props --use-svnsync-props
3114 --log-window-size= --no-checkout --quiet
3115 --repack-flags --use-log-author --localtime
3118 --ignore-paths= --include-paths= $remote_opts
3121 --template= --shared= --trunk= --tags=
3122 --branches= --stdlayout --minimize-url
3123 --no-metadata --use-svm-props --use-svnsync-props
3124 --rewrite-root= --prefix= $remote_opts
3127 --edit --rmdir --find-copies-harder --copy-similarity=
3130 case "$subcommand,$cur" in
3132 __gitcomp "--revision= --fetch-all $fc_opts"
3135 __gitcomp "--revision= $fc_opts $init_opts"
3138 __gitcomp "$init_opts"
3142 --merge --strategy= --verbose --dry-run
3143 --fetch-all --no-rebase --commit-url
3144 --revision --interactive $cmt_opts $fc_opts
3148 __gitcomp "--stdin $cmt_opts $fc_opts"
3150 create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
3151 show-externals,--*|mkdirs,--*)
3152 __gitcomp "--revision="
3156 --limit= --revision= --verbose --incremental
3157 --oneline --show-commit --non-recursive
3158 --authors-file= --color
3163 --merge --verbose --strategy= --local
3164 --fetch-all --dry-run $fc_opts
3168 __gitcomp "--message= --file= --revision= $cmt_opts"
3174 __gitcomp "--dry-run --message --tag"
3177 __gitcomp "--dry-run --message"
3180 __gitcomp "--git-format"
3184 --config-dir= --ignore-paths= --minimize
3185 --no-auth-cache --username=
3189 __gitcomp "--revision= --parent"
3200 while [ $c -lt $cword ]; do
3203 -d|--delete|-v|--verify)
3204 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3219 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3229 __gitcomp_builtin tag
3239 __git_complete_worktree_paths ()
3242 __gitcomp_nl "$(git worktree list --porcelain |
3243 # Skip the first entry: it's the path of the main worktree,
3244 # which can't be moved, removed, locked, etc.
3245 sed -n -e '2,$ s/^worktree //p')"
3250 local subcommands="add list lock move prune remove unlock"
3251 local subcommand subcommand_idx
3253 subcommand="$(__git_find_on_cmdline --show-idx "$subcommands")"
3254 subcommand_idx="${subcommand% *}"
3255 subcommand="${subcommand#* }"
3257 case "$subcommand,$cur" in
3259 __gitcomp "$subcommands"
3262 __gitcomp_builtin worktree_$subcommand
3264 add,*) # usage: git worktree add [<options>] <path> [<commit-ish>]
3265 # Here we are not completing an --option, it's either the
3268 -b|-B) # Complete refs for branch to be created/reseted.
3271 -*) # The previous word is an -o|--option without an
3272 # unstuck argument: have to complete the path for
3273 # the new worktree, so don't list anything, but let
3274 # Bash fall back to filename completion.
3276 *) # The previous word is not an --option, so it must
3277 # be either the 'add' subcommand, the unstuck
3278 # argument of an option (e.g. branch for -b|-B), or
3279 # the path for the new worktree.
3280 if [ $cword -eq $((subcommand_idx+1)) ]; then
3281 # Right after the 'add' subcommand: have to
3282 # complete the path, so fall back to Bash
3283 # filename completion.
3286 case "${words[cword-2]}" in
3287 -b|-B) # After '-b <branch>': have to
3288 # complete the path, so fall back
3289 # to Bash filename completion.
3291 *) # After the path: have to complete
3292 # the ref to be checked out.
3300 lock,*|remove,*|unlock,*)
3301 __git_complete_worktree_paths
3304 if [ $cword -eq $((subcommand_idx+1)) ]; then
3305 # The first parameter must be an existing working
3307 __git_complete_worktree_paths
3309 # The second parameter is the destination: it could
3310 # be any path, so don't list anything, but let Bash
3311 # fall back to filename completion.
3318 __git_complete_common () {
3323 __gitcomp_builtin "$command"
3328 __git_cmds_with_parseopt_helper=
3329 __git_support_parseopt_helper () {
3330 test -n "$__git_cmds_with_parseopt_helper" ||
3331 __git_cmds_with_parseopt_helper="$(__git --list-cmds=parseopt)"
3333 case " $__git_cmds_with_parseopt_helper " in
3343 __git_complete_command () {
3345 local completion_func="_git_${command//-/_}"
3346 if ! declare -f $completion_func >/dev/null 2>/dev/null &&
3347 declare -f _completion_loader >/dev/null 2>/dev/null
3349 _completion_loader "git-$command"
3351 if declare -f $completion_func >/dev/null 2>/dev/null
3355 elif __git_support_parseopt_helper "$command"
3357 __git_complete_common "$command"
3366 local i c=1 command __git_dir __git_repo_path
3367 local __git_C_args C_args_count=0
3369 while [ $c -lt $cword ]; do
3372 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
3373 --git-dir) ((c++)) ; __git_dir="${words[c]}" ;;
3374 --bare) __git_dir="." ;;
3375 --help) command="help"; break ;;
3376 -c|--work-tree|--namespace) ((c++)) ;;
3377 -C) __git_C_args[C_args_count++]=-C
3379 __git_C_args[C_args_count++]="${words[c]}"
3382 *) command="$i"; break ;;
3387 if [ -z "${command-}" ]; then
3389 --git-dir|-C|--work-tree)
3390 # these need a path argument, let's fall back to
3391 # Bash filename completion
3395 __git_complete_config_variable_name_and_value
3399 # we don't support completing these options' arguments
3417 --no-replace-objects
3422 if test -n "${GIT_TESTING_PORCELAIN_COMMAND_LIST-}"
3424 __gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
3426 __gitcomp "$(__git --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config)"
3433 __git_complete_command "$command" && return
3435 local expansion=$(__git_aliased_command "$command")
3436 if [ -n "$expansion" ]; then
3438 __git_complete_command "$expansion"
3444 __git_has_doubledash && return
3446 local __git_repo_path
3447 __git_find_repo_path
3450 if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
3456 $__git_log_common_options
3457 $__git_log_gitk_options
3463 __git_complete_revlist
3466 if [[ -n ${ZSH_VERSION-} && -z ${GIT_SOURCING_ZSH_COMPLETION-} ]]; then
3467 echo "ERROR: this script is obsolete, please see git-completion.zsh" 1>&2
3473 local cur words cword prev
3474 _get_comp_words_by_ref -n =: cur words cword prev
3478 # Setup completion for certain functions defined above by setting common
3479 # variables and workarounds.
3480 # This is NOT a public function; use at your own risk.
3483 local wrapper="__git_wrap${2}"
3484 eval "$wrapper () { __git_func_wrap $2 ; }"
3485 complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
3486 || complete -o default -o nospace -F $wrapper $1
3489 __git_complete git __git_main
3490 __git_complete gitk __gitk_main
3492 # The following are necessary only for Cygwin, and only are needed
3493 # when the user has tab-completed the executable name and consequently
3494 # included the '.exe' suffix.
3496 if [ "$OSTYPE" = cygwin ]; then
3497 __git_complete git.exe __git_main