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 ; ... '".
31 # Note that "git" is optional --- '!f() { : commit; ...}; f' would complete
32 # just like the 'git commit' command.
34 # If you have a shell command that is not part of git (and is not called as a
35 # git subcommand), but you would still like git-style completion for it, use
36 # __git_complete. For example, to use the same completion as for 'git log' also
37 # for the 'gl' command:
39 # __git_complete gl git_log
41 # Or if the 'gk' command should be completed the same as 'gitk':
43 # __git_complete gk gitk
45 # The second parameter of __git_complete gives the completion function; it is
46 # resolved as a function named "$2", or "__$2_main", or "_$2" in that order.
47 # In the examples above, the actual functions used for completion will be
48 # _git_log and __gitk_main.
50 # Compatible with bash 3.2.57.
52 # You can set the following environment variables to influence the behavior of
53 # the completion routines:
55 # GIT_COMPLETION_CHECKOUT_NO_GUESS
57 # When set to "1", do not include "DWIM" suggestions in git-checkout
58 # and git-switch completion (e.g., completing "foo" when "origin/foo"
61 # GIT_COMPLETION_SHOW_ALL_COMMANDS
63 # When set to "1" suggest all commands, including plumbing commands
64 # which are hidden by default (e.g. "cat-file" on "git ca<TAB>").
66 # GIT_COMPLETION_SHOW_ALL
68 # When set to "1" suggest all options, including options which are
69 # typically hidden (e.g. '--allow-empty' for 'git commit').
71 # GIT_COMPLETION_IGNORE_CASE
73 # When set, uses for-each-ref '--ignore-case' to find refs that match
74 # case insensitively, even on systems with case sensitive file systems
75 # (e.g., completing tag name "FOO" on "git checkout f<TAB>").
77 case "$COMP_WORDBREAKS" in
79 *) COMP_WORDBREAKS
="$COMP_WORDBREAKS:"
82 # Discovers the path to the git repository taking any '--git-dir=<path>' and
83 # '-C <path>' options into account and stores it in the $__git_repo_path
85 __git_find_repo_path
()
87 if [ -n "${__git_repo_path-}" ]; then
88 # we already know where it is
92 if [ -n "${__git_C_args-}" ]; then
93 __git_repo_path
="$(git "${__git_C_args[@]}" \
94 ${__git_dir:+--git-dir="$__git_dir"} \
95 rev-parse --absolute-git-dir 2>/dev/null)"
96 elif [ -n "${__git_dir-}" ]; then
97 test -d "$__git_dir" &&
98 __git_repo_path
="$__git_dir"
99 elif [ -n "${GIT_DIR-}" ]; then
100 test -d "$GIT_DIR" &&
101 __git_repo_path
="$GIT_DIR"
102 elif [ -d .git
]; then
105 __git_repo_path
="$(git rev-parse --git-dir 2>/dev/null)"
109 # Deprecated: use __git_find_repo_path() and $__git_repo_path instead
110 # __gitdir accepts 0 or 1 arguments (i.e., location)
111 # returns location of .git repo
114 if [ -z "${1-}" ]; then
115 __git_find_repo_path ||
return 1
116 echo "$__git_repo_path"
117 elif [ -d "$1/.git" ]; then
124 # Runs git with all the options given as argument, respecting any
125 # '--git-dir=<path>' and '-C <path>' options present on the command line
128 git
${__git_C_args:+"${__git_C_args[@]}"} \
129 ${__git_dir:+--git-dir="$__git_dir"} "$@" 2>/dev
/null
132 # Helper function to read the first line of a file into a variable.
133 # __git_eread requires 2 arguments, the file path and the name of the
134 # variable, in that order.
136 # This is taken from git-prompt.sh.
139 test -r "$1" && IFS
=$
'\r\n' read -r "$2" <"$1"
142 # Runs git in $__git_repo_path to determine whether a pseudoref exists.
143 # 1: The pseudo-ref to search
144 __git_pseudoref_exists
()
151 # If the reftable is in use, we have to shell out to 'git rev-parse'
152 # to determine whether the ref exists instead of looking directly in
153 # the filesystem to determine whether the ref exists. Otherwise, use
154 # Bash builtins since executing Git commands are expensive on some
156 if __git_eread
"$__git_repo_path/HEAD" head; then
157 if [ "$head" == "ref: refs/heads/.invalid" ]; then
158 __git show-ref
--exists "$ref"
163 [ -f "$__git_repo_path/$ref" ]
166 # Removes backslash escaping, single quotes and double quotes from a word,
167 # stores the result in the variable $dequoted_word.
168 # 1: The word to dequote.
171 local rest
="$1" len ch
175 while test -n "$rest"; do
176 len
=${#dequoted_word}
177 dequoted_word
="$dequoted_word${rest%%[\\\'\"]*}"
178 rest
="${rest:$((${#dequoted_word}-$len))}"
180 case "${rest:0:1}" in
187 dequoted_word
="$dequoted_word$ch"
194 len
=${#dequoted_word}
195 dequoted_word
="$dequoted_word${rest%%\'*}"
196 rest
="${rest:$((${#dequoted_word}-$len+1))}"
200 while test -n "$rest" ; do
201 len
=${#dequoted_word}
202 dequoted_word
="$dequoted_word${rest%%[\\\"]*}"
203 rest
="${rest:$((${#dequoted_word}-$len))}"
204 case "${rest:0:1}" in
209 dequoted_word="$dequoted_word$ch"
214 dequoted_word="$dequoted_word\\$ch"
230 # The following function is based on code from:
232 # bash_completion - programmable completion functions for bash 3.2+
234 # Copyright © 2006-2008, Ian Macdonald <ian@caliban.org>
235 # © 2009-2010, Bash Completion Maintainers
236 # <bash-completion-devel@lists.alioth.debian.org>
238 # This program is free software; you can redistribute it and/or modify
239 # it under the terms of the GNU General Public License as published by
240 # the Free Software Foundation; either version 2, or (at your option)
243 # This program is distributed in the hope that it will be useful,
244 # but WITHOUT ANY WARRANTY; without even the implied warranty of
245 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
246 # GNU General Public License for more details.
248 # You should have received a copy of the GNU General Public License
249 # along with this program; if not, see <http://www.gnu.org/licenses/>.
251 # The latest version of this software can be obtained here:
253 # http://bash-completion.alioth.debian.org/
257 # This function can be used to access a tokenized list of words
258 # on the command line:
260 # __git_reassemble_comp_words_by_ref '=:'
261 # if test "${words_[cword_-1]}" = -w
266 # The argument should be a collection of characters from the list of
267 # word completion separators (COMP_WORDBREAKS) to treat as ordinary
270 # This is roughly equivalent to going back in time and setting
271 # COMP_WORDBREAKS to exclude those characters. The intent is to
272 # make option types like --date=<type> and <rev>:<path> easy to
273 # recognize by treating each shell word as a single token.
275 # It is best not to set COMP_WORDBREAKS directly because the value is
276 # shared with other completion scripts. By the time the completion
277 # function gets called, COMP_WORDS has already been populated so local
278 # changes to COMP_WORDBREAKS have no effect.
280 # Output: words_, cword_, cur_.
282 __git_reassemble_comp_words_by_ref()
284 local exclude i j first
285 # Which word separators to exclude?
286 exclude="${1//[^$COMP_WORDBREAKS]}"
288 if [ -z "$exclude" ]; then
289 words_=("${COMP_WORDS[@]}")
292 # List of word completion separators has shrunk;
293 # re-assemble words to complete.
294 for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
295 # Append each nonempty word consisting of just
296 # word separator characters to the current word.
300 [ -n "${COMP_WORDS[$i]}" ] &&
301 # word consists of excluded word separators
302 [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
304 # Attach to the previous token,
305 # unless the previous token is the command name.
306 if [ $j -ge 2 ] && [ -n "$first" ]; then
310 words_[$j]=${words_[j]}${COMP_WORDS[i]}
311 if [ $i = $COMP_CWORD ]; then
314 if (($i < ${#COMP_WORDS[@]} - 1)); then
321 words_[$j]=${words_[j]}${COMP_WORDS[i]}
322 if [ $i = $COMP_CWORD ]; then
328 if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
329 _get_comp_words_by_ref ()
331 local exclude cur_ words_ cword_
332 if [ "$1" = "-n" ]; then
336 __git_reassemble_comp_words_by_ref "$exclude"
337 cur_=${words_[cword_]}
338 while [ $# -gt 0 ]; do
344 prev=${words_[$cword_-1]}
347 words=("${words_[@]}")
358 # Fills the COMPREPLY array with prefiltered words without any additional
360 # Callers must take care of providing only words that match the current word
361 # to be completed and adding any prefix and/or suffix (trailing space!), if
363 # 1: List of newline-separated matching completion words, complete with
372 # Similar to __gitcomp_direct, but appends to COMPREPLY instead.
373 # Callers must take care of providing only words that match the current word
374 # to be completed and adding any prefix and/or suffix (trailing space!), if
376 # 1: List of newline-separated matching completion words, complete with
378 __gitcomp_direct_append ()
387 local x i=${#COMPREPLY[@]}
389 if [[ "$x" == "$3"* ]]; then
390 COMPREPLY[i++]="$2$x$4"
401 # Generates completion reply, appending a space to possible completion words,
403 # It accepts 1 to 4 arguments:
404 # 1: List of possible completion words.
405 # 2: A prefix to be added to each possible completion word (optional).
406 # 3: Generate possible completion matches for this word (optional).
407 # 4: A suffix to be appended to each possible completion word (optional).
410 local cur_="${3-$cur}"
416 local c i=0 IFS=$' \t\n'
418 if [[ $c == "--" ]]; then
422 if [[ $c == "$cur_"* ]]; then
427 COMPREPLY[i++]="${2-}$c"
432 local c i=0 IFS=$' \t\n'
434 if [[ $c == "--" ]]; then
436 if [[ $c == "$cur_"* ]]; then
437 COMPREPLY[i++]="${2-}$c "
442 if [[ $c == "$cur_"* ]]; then
447 COMPREPLY[i++]="${2-}$c"
454 # Clear the variables caching builtins' options when (re-)sourcing
455 # the completion script.
456 if [[ -n ${ZSH_VERSION-} ]]; then
457 unset ${(M)${(k)parameters[@]}:#__gitcomp_builtin_*} 2>/dev/null
459 unset $(compgen -v __gitcomp_builtin_)
462 # This function is equivalent to
464 # __gitcomp "$(git xxx --git-completion-helper) ..."
466 # except that the output is cached. Accept 1-3 arguments:
467 # 1: the git command to execute, this is also the cache key
468 # 2: extra options to be added on top (e.g. negative forms)
469 # 3: options to be excluded
472 # spaces must be replaced with underscore for multi-word
473 # commands, e.g. "git remote add" becomes remote_add.
478 local var=__gitcomp_builtin_"${cmd//-/_}"
480 eval "options=\${$var-}"
482 if [ -z "$options" ]; then
483 local completion_helper
484 if [ "${GIT_COMPLETION_SHOW_ALL-}" = "1" ]; then
485 completion_helper="--git-completion-helper-all"
487 completion_helper="--git-completion-helper"
489 # leading and trailing spaces are significant to make
490 # option removal work correctly.
491 options=" $incl $(__git ${cmd/_/ } $completion_helper) " || return
494 options="${options/ $i / }"
496 eval "$var=\"$options\""
502 # Variation of __gitcomp_nl () that appends to the existing list of
503 # completion candidates, COMPREPLY.
504 __gitcomp_nl_append ()
507 __gitcompappend "$1" "${2-}" "${3-$cur}" "${4- }"
510 # Generates completion reply from newline-separated possible completion words
511 # by appending a space to all of them.
512 # It accepts 1 to 4 arguments:
513 # 1: List of possible completion words, separated by a single newline.
514 # 2: A prefix to be added to each possible completion word (optional).
515 # 3: Generate possible completion matches for this word (optional).
516 # 4: A suffix to be appended to each possible completion word instead of
517 # the default space (optional). If specified but empty, nothing is
522 __gitcomp_nl_append "$@"
525 # Fills the COMPREPLY array with prefiltered paths without any additional
527 # Callers must take care of providing only paths that match the current path
528 # to be completed and adding any prefix path components, if necessary.
529 # 1: List of newline-separated matching paths, complete with all prefix
531 __gitcomp_file_direct ()
537 # use a hack to enable file mode in bash < 4
538 compopt -o filenames +o nospace 2>/dev/null ||
539 compgen -f /non-existing-dir/ >/dev/null ||
543 # Generates completion reply with compgen from newline-separated possible
544 # completion filenames.
545 # It accepts 1 to 3 arguments:
546 # 1: List of possible completion filenames, separated by a single newline.
547 # 2: A directory prefix to be added to each possible completion filename
549 # 3: Generate possible completion matches for this word (optional).
554 # XXX does not work when the directory prefix contains a tilde,
555 # since tilde expansion is not applied.
556 # This means that COMPREPLY will be empty and Bash default
557 # completion will be used.
558 __gitcompadd "$1" "${2-}" "${3-$cur}" ""
560 # use a hack to enable file mode in bash < 4
561 compopt -o filenames +o nospace 2>/dev/null ||
562 compgen -f /non-existing-dir/ >/dev/null ||
566 # Execute 'git ls-files', unless the --committable option is specified, in
567 # which case it runs 'git diff-index' to find out the files that can be
568 # committed. It return paths relative to the directory specified in the first
569 # argument, and using the options specified in the second argument.
570 __git_ls_files_helper ()
572 if [ "$2" = "--committable" ]; then
573 __git -C "$1" -c core.quotePath=false diff-index \
574 --name-only --relative HEAD -- "${3//\\/\\\\}*"
576 # NOTE: $2 is not quoted in order to support multiple options
577 __git -C "$1" -c core.quotePath=false ls-files \
578 --exclude-standard $2 -- "${3//\\/\\\\}*"
583 # __git_index_files accepts 1 or 2 arguments:
584 # 1: Options to pass to ls-files (required).
585 # 2: A directory path (optional).
586 # If provided, only files within the specified directory are listed.
587 # Sub directories are never recursed. Path must have a trailing
589 # 3: List only paths matching this path component (optional).
592 local root="$2" match="$3"
594 __git_ls_files_helper "$root" "$1" "${match:-?}" |
595 awk -F / -v pfx="${2//\\/\\\\}" '{
600 if (substr(p, 1, 1) != "\"") {
601 # No special characters, easy!
606 # The path is quoted.
611 # Even when a directory name itself does not contain
612 # any special characters, it will still be quoted if
613 # any of its (stripped) trailing path components do.
614 # Because of this we may have seen the same directory
615 # both quoted and unquoted.
617 # We have seen the same directory unquoted,
624 function dequote(p, bs_idx, out, esc, esc_idx, dec) {
625 # Skip opening double quote.
628 # Interpret backslash escape sequences.
629 while ((bs_idx = index(p, "\\")) != 0) {
630 out = out substr(p, 1, bs_idx - 1)
631 esc = substr(p, bs_idx + 1, 1)
632 p = substr(p, bs_idx + 2)
634 if ((esc_idx = index("abtvfr\"\\", esc)) != 0) {
635 # C-style one-character escape sequence.
636 out = out substr("\a\b\t\v\f\r\"\\",
638 } else if (esc == "n") {
639 # Uh-oh, a newline character.
640 # We cannot reliably put a pathname
641 # containing a newline into COMPREPLY,
642 # and the newline would create a mess.
646 # Must be a \nnn octal value, then.
648 substr(p, 1, 1) * 8 + \
650 out = out sprintf("%c", dec)
654 # Drop closing double quote, if there is one.
655 # (There is not any if this is a directory, as it was
656 # already stripped with the trailing path components.)
657 if (substr(p, length(p), 1) == "\"")
658 out = out substr(p, 1, length(p) - 1)
666 # __git_complete_index_file requires 1 argument:
667 # 1: the options to pass to ls-file
669 # The exception is --committable, which finds the files appropriate commit.
670 __git_complete_index_file ()
672 local dequoted_word pfx="" cur_
676 case "$dequoted_word" in
678 pfx="${dequoted_word%/*}/"
679 cur_="${dequoted_word##*/}"
682 cur_="$dequoted_word"
685 __gitcomp_file_direct "$(__git_index_files "$1" "$pfx" "$cur_")"
688 # Lists branches from the local repository.
689 # 1: A prefix to be added to each listed branch (optional).
690 # 2: List only branches matching this word (optional; list all branches if
692 # 3: A suffix to be appended to each listed branch (optional).
695 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
697 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
698 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
699 "refs/heads/$cur_*" "refs/heads/$cur_*/**"
702 # Lists branches from remote repositories.
703 # 1: A prefix to be added to each listed branch (optional).
704 # 2: List only branches matching this word (optional; list all branches if
706 # 3: A suffix to be appended to each listed branch (optional).
707 __git_remote_heads ()
709 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
711 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
712 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
713 "refs/remotes/$cur_*" "refs/remotes/$cur_*/**"
716 # Lists tags from the local repository.
717 # Accepts the same positional parameters as __git_heads() above.
720 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
722 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
723 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
724 "refs/tags/$cur_*" "refs/tags/$cur_*/**"
727 # List unique branches from refs/remotes used for 'git checkout' and 'git
728 # switch' tracking DWIMery.
729 # 1: A prefix to be added to each listed branch (optional)
730 # 2: List only branches matching this word (optional; list all branches if
732 # 3: A suffix to be appended to each listed branch (optional).
733 __git_dwim_remote_heads ()
735 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
736 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
738 # employ the heuristic used by git checkout and git switch
739 # Try to find a remote branch that cur_es the completion word
740 # but only output if the branch name is unique
741 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
742 --sort="refname:strip=3" \
743 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
744 "refs/remotes/*/$cur_*" "refs/remotes/*/$cur_*/**" | \
748 # Lists refs from the local (by default) or from a remote repository.
749 # It accepts 0, 1 or 2 arguments:
750 # 1: The remote to list refs from (optional; ignored, if set but empty).
751 # Can be the name of a configured remote, a path, or a URL.
752 # 2: In addition to local refs, list unique branches from refs/remotes/ for
753 # 'git checkout's tracking DWIMery (optional; ignored, if set but empty).
754 # 3: A prefix to be added to each listed ref (optional).
755 # 4: List only refs matching this word (optional; list all refs if unset or
757 # 5: A suffix to be appended to each listed ref (optional; ignored, if set
760 # Use __git_complete_refs() instead.
763 local i hash dir track="${2-}"
764 local list_refs_from=path remote="${1-}"
766 local pfx="${3-}" cur_="${4-$cur}" sfx="${5-}"
769 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
772 dir="$__git_repo_path"
774 if [ -z "$remote" ]; then
775 if [ -z "$dir" ]; then
779 if __git_is_configured_remote "$remote"; then
780 # configured remote takes precedence over a
781 # local directory with the same name
782 list_refs_from=remote
783 elif [ -d "$remote/.git" ]; then
785 elif [ -d "$remote" ]; then
792 if test "${GIT_COMPLETION_IGNORE_CASE:+1}" = "1"
794 # uppercase with tr instead of ${match,^^} for bash 3.2 compatibility
795 umatch=$(echo "$match" | tr a-z A-Z 2>/dev/null || echo "$match")
798 if [ "$list_refs_from" = path ]; then
799 if [[ "$cur_" == ^* ]]; then
809 refs=("$match*" "$match*/**")
813 for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD REBASE_HEAD CHERRY_PICK_HEAD REVERT_HEAD BISECT_HEAD AUTO_MERGE; do
816 if [ -e "$dir/$i" ]; then
822 format="refname:strip=2"
823 refs=("refs/tags/$match*" "refs/tags/$match*/**"
824 "refs/heads/$match*" "refs/heads/$match*/**"
825 "refs/remotes/$match*" "refs/remotes/$match*/**")
828 __git_dir="$dir" __git for-each-ref --format="$fer_pfx%($format)$sfx" \
829 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
831 if [ -n "$track" ]; then
832 __git_dwim_remote_heads "$pfx" "$match" "$sfx"
838 __git ls-remote "$remote" "$match*" | \
839 while read -r hash i; do
842 *) echo "$pfx$i$sfx" ;;
847 if [ "$list_refs_from" = remote ]; then
849 $match*|$umatch*) echo "${pfx}HEAD$sfx" ;;
851 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
852 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
853 "refs/remotes/$remote/$match*" \
854 "refs/remotes/$remote/$match*/**"
858 $match*|$umatch*) query_symref="HEAD" ;;
860 __git ls-remote "$remote" $query_symref \
861 "refs/tags/$match*" "refs/heads/$match*" \
862 "refs/remotes/$match*" |
863 while read -r hash i; do
866 refs/*) echo "$pfx${i#refs/*/}$sfx" ;;
867 *) echo "$pfx$i$sfx" ;; # symbolic refs
875 # Completes refs, short and long, local and remote, symbolic and pseudo.
877 # Usage: __git_complete_refs [<option>]...
878 # --remote=<remote>: The remote to list refs from, can be the name of a
879 # configured remote, a path, or a URL.
880 # --dwim: List unique remote branches for 'git switch's tracking DWIMery.
881 # --pfx=<prefix>: A prefix to be added to each ref.
882 # --cur=<word>: The current ref to be completed. Defaults to the current
883 # word to be completed.
884 # --sfx=<suffix>: A suffix to be appended to each ref instead of the default
886 # --mode=<mode>: What set of refs to complete, one of 'refs' (the default) to
887 # complete all refs, 'heads' to complete only branches, or
888 # 'remote-heads' to complete only remote branches. Note that
889 # --remote is only compatible with --mode=refs.
890 __git_complete_refs ()
892 local remote= dwim= pfx= cur_="$cur" sfx=" " mode="refs"
894 while test $# != 0; do
896 --remote=*) remote="${1##--remote=}" ;;
897 --dwim) dwim="yes" ;;
898 # --track is an old spelling of --dwim
899 --track) dwim="yes" ;;
900 --pfx=*) pfx="${1##--pfx=}" ;;
901 --cur=*) cur_="${1##--cur=}" ;;
902 --sfx=*) sfx="${1##--sfx=}" ;;
903 --mode=*) mode="${1##--mode=}" ;;
909 # complete references based on the specified mode
912 __gitcomp_direct "$(__git_refs "$remote" "" "$pfx" "$cur_" "$sfx")" ;;
914 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" "$sfx")" ;;
916 __gitcomp_direct "$(__git_remote_heads "$pfx" "$cur_" "$sfx")" ;;
921 # Append DWIM remote branch names if requested
922 if [ "$dwim" = "yes" ]; then
923 __gitcomp_direct_append "$(__git_dwim_remote_heads "$pfx" "$cur_" "$sfx")"
927 # __git_refs2 requires 1 argument (to pass to __git_refs)
928 # Deprecated: use __git_complete_fetch_refspecs() instead.
932 for i in $(__git_refs "$1"); do
937 # Completes refspecs for fetching from a remote repository.
938 # 1: The remote repository.
939 # 2: A prefix to be added to each listed refspec (optional).
940 # 3: The ref to be completed as a refspec instead of the current word to be
941 # completed (optional)
942 # 4: A suffix to be appended to each listed refspec instead of the default
944 __git_complete_fetch_refspecs ()
946 local i remote="$1" pfx="${2-}" cur_="${3-$cur}" sfx="${4- }"
949 for i in $(__git_refs "$remote" "" "" "$cur_") ; do
955 # __git_refs_remotes requires 1 argument (to pass to ls-remote)
956 __git_refs_remotes ()
959 __git ls-remote "$1" 'refs/heads/*' | \
960 while read -r hash i; do
961 echo "$i:refs/remotes/$1/${i#refs/heads/}"
968 test -d "$__git_repo_path/remotes" && ls -1 "$__git_repo_path/remotes"
972 # Returns true if $1 matches the name of a configured remote, false otherwise.
973 __git_is_configured_remote ()
976 for remote in $(__git_remotes); do
977 if [ "$remote" = "$1" ]; then
984 __git_list_merge_strategies ()
986 LANG=C LC_ALL=C git merge -s help 2>&1 |
987 sed -n -e '/[Aa]vailable strategies are: /,/^$/{
996 __git_merge_strategies=
997 # 'git merge -s help' (and thus detection of the merge strategy
998 # list) fails, unfortunately, if run outside of any git working
999 # tree. __git_merge_strategies is set to the empty string in
1000 # that case, and the detection will be repeated the next time it
1002 __git_compute_merge_strategies ()
1004 test -n "$__git_merge_strategies" ||
1005 __git_merge_strategies=$(__git_list_merge_strategies)
1008 __git_merge_strategy_options="ours theirs subtree subtree= patience
1009 histogram diff-algorithm= ignore-space-change ignore-all-space
1010 ignore-space-at-eol renormalize no-renormalize no-renames
1011 find-renames find-renames= rename-threshold="
1013 __git_complete_revlist_file ()
1015 local dequoted_word pfx ls ref cur_="$cur"
1024 __git_dequote "$cur_"
1026 case "$dequoted_word" in
1028 pfx="${dequoted_word%/*}"
1029 cur_="${dequoted_word##*/}"
1034 cur_="$dequoted_word"
1039 case "$COMP_WORDBREAKS" in
1041 *) pfx="$ref:$pfx" ;;
1044 __gitcomp_file "$(__git ls-tree "$ls" \
1050 pfx="${cur_%...*}..."
1052 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1057 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1065 __git_complete_file ()
1067 __git_complete_revlist_file
1070 __git_complete_revlist ()
1072 __git_complete_revlist_file
1075 __git_complete_remote_or_refspec ()
1077 local cur_="$cur" cmd="${words[__git_cmd_idx]}"
1078 local i c=$((__git_cmd_idx+1)) remote="" pfx="" lhs=1 no_complete_refspec=0
1079 if [ "$cmd" = "remote" ]; then
1082 while [ $c -lt $cword ]; do
1085 --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
1086 -d|--delete) [ "$cmd" = "push" ] && lhs=0 ;;
1089 push) no_complete_refspec=1 ;;
1096 --multiple) no_complete_refspec=1; break ;;
1098 *) remote="$i"; break ;;
1102 if [ -z "$remote" ]; then
1103 __gitcomp_nl "$(__git_remotes)"
1106 if [ $no_complete_refspec = 1 ]; then
1109 [ "$remote" = "." ] && remote=
1112 case "$COMP_WORDBREAKS" in
1114 *) pfx="${cur_%%:*}:" ;;
1126 if [ $lhs = 1 ]; then
1127 __git_complete_fetch_refspecs "$remote" "$pfx" "$cur_"
1129 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1133 if [ $lhs = 1 ]; then
1134 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
1136 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1140 if [ $lhs = 1 ]; then
1141 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1143 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
1149 __git_complete_strategy ()
1151 __git_compute_merge_strategies
1154 __gitcomp "$__git_merge_strategies"
1158 __gitcomp "$__git_merge_strategy_options"
1164 __gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
1167 --strategy-option=*)
1168 __gitcomp "$__git_merge_strategy_options" "" "${cur##--strategy-option=}"
1176 __git_compute_all_commands ()
1178 test -n "$__git_all_commands" ||
1179 __git_all_commands=$(__git --list-cmds=main,others,alias,nohelpers)
1182 # Lists all set config variables starting with the given section prefix,
1183 # with the prefix removed.
1184 __git_get_config_variables ()
1186 local section="$1" i IFS=$'\n'
1187 for i in $(__git config --name-only --get-regexp "^$section\..*"); do
1188 echo "${i#$section.}"
1192 __git_pretty_aliases ()
1194 __git_get_config_variables "pretty"
1197 # __git_aliased_command requires 1 argument
1198 __git_aliased_command ()
1200 local cur=$1 last list= word cmdline
1202 while [[ -n "$cur" ]]; do
1203 if [[ "$list" == *" $cur "* ]]; then
1208 cmdline=$(__git config --get "alias.$cur")
1213 for word in $cmdline; do
1219 \!*) : shell command alias ;;
1221 *=*) : setting env ;;
1222 git) : git itself ;;
1223 \(\)) : skip parens of shell function definition ;;
1224 {) : skip start of shell helper function ;;
1225 :) : skip null command ;;
1226 \'*) : skip opening quote after sh -c ;;
1235 if [[ "$cur" != "$1" ]]; then
1240 # Check whether one of the given words is present on the command line,
1241 # and print the first word found.
1243 # Usage: __git_find_on_cmdline [<option>]... "<wordlist>"
1244 # --show-idx: Optionally show the index of the found word in the $words array.
1245 __git_find_on_cmdline ()
1247 local word c="$__git_cmd_idx" show_idx
1249 while test $# -gt 1; do
1251 --show-idx) show_idx=y ;;
1258 while [ $c -lt $cword ]; do
1259 for word in $wordlist; do
1260 if [ "$word" = "${words[c]}" ]; then
1261 if [ -n "${show_idx-}" ]; then
1273 # Similar to __git_find_on_cmdline, except that it loops backwards and thus
1274 # prints the *last* word found. Useful for finding which of two options that
1275 # supersede each other came last, such as "--guess" and "--no-guess".
1277 # Usage: __git_find_last_on_cmdline [<option>]... "<wordlist>"
1278 # --show-idx: Optionally show the index of the found word in the $words array.
1279 __git_find_last_on_cmdline ()
1281 local word c=$cword show_idx
1283 while test $# -gt 1; do
1285 --show-idx) show_idx=y ;;
1292 while [ $c -gt "$__git_cmd_idx" ]; do
1294 for word in $wordlist; do
1295 if [ "$word" = "${words[c]}" ]; then
1296 if [ -n "$show_idx" ]; then
1307 # Echo the value of an option set on the command line or config
1309 # $1: short option name
1310 # $2: long option name including =
1311 # $3: list of possible values
1312 # $4: config string (optional)
1315 # result="$(__git_get_option_value "-d" "--do-something=" \
1316 # "yes no" "core.doSomething")"
1318 # result is then either empty (no option set) or "yes" or "no"
1320 # __git_get_option_value requires 3 arguments
1321 __git_get_option_value ()
1323 local c short_opt long_opt val
1324 local result= values config_key word
1332 while [ $c -ge 0 ]; do
1334 for val in $values; do
1335 if [ "$short_opt$val" = "$word" ] ||
1336 [ "$long_opt$val" = "$word" ]; then
1344 if [ -n "$config_key" ] && [ -z "$result" ]; then
1345 result="$(__git config "$config_key")"
1351 __git_has_doubledash ()
1354 while [ $c -lt $cword ]; do
1355 if [ "--" = "${words[c]}" ]; then
1363 # Try to count non option arguments passed on the command line for the
1364 # specified git command.
1365 # When options are used, it is necessary to use the special -- option to
1366 # tell the implementation were non option arguments begin.
1367 # XXX this can not be improved, since options can appear everywhere, as
1371 # __git_count_arguments requires 1 argument: the git command executed.
1372 __git_count_arguments ()
1376 # Skip "git" (first argument)
1377 for ((i=$__git_cmd_idx; i < ${#words[@]}; i++)); do
1382 # Good; we can assume that the following are only non
1387 # Skip the specified git command and discard git
1400 __git_whitespacelist="nowarn warn error error-all fix"
1401 __git_patchformat="mbox stgit stgit-series hg mboxrd"
1402 __git_showcurrentpatch="diff raw"
1403 __git_am_inprogress_options="--skip --continue --resolved --abort --quit --show-current-patch"
1404 __git_quoted_cr="nowarn warn strip"
1408 __git_find_repo_path
1409 if [ -d "$__git_repo_path"/rebase-apply ]; then
1410 __gitcomp "$__git_am_inprogress_options"
1415 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1419 __gitcomp "$__git_patchformat" "" "${cur##--patch-format=}"
1422 --show-current-patch=*)
1423 __gitcomp "$__git_showcurrentpatch" "" "${cur##--show-current-patch=}"
1427 __gitcomp "$__git_quoted_cr" "" "${cur##--quoted-cr=}"
1431 __gitcomp_builtin am "" \
1432 "$__git_am_inprogress_options"
1441 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1445 __gitcomp_builtin apply
1454 __gitcomp "+x -x" "" "${cur##--chmod=}"
1458 __gitcomp_builtin add
1462 local complete_opt="--others --modified --directory --no-empty-directory"
1463 if test -n "$(__git_find_on_cmdline "-u --update")"
1465 complete_opt="--modified"
1467 __git_complete_index_file "$complete_opt"
1474 __gitcomp "$(git archive --list)" "" "${cur##--format=}"
1478 __gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
1482 __gitcomp_builtin archive "--format= --list --verbose --prefix= --worktree-attributes"
1491 __git_has_doubledash && return
1493 __git_find_repo_path
1495 # If a bisection is in progress get the terms being used.
1496 local term_bad term_good
1497 if [ -f "$__git_repo_path"/BISECT_TERMS ]; then
1498 term_bad=$(__git bisect terms --term-bad)
1499 term_good=$(__git bisect terms --term-good)
1502 # We will complete any custom terms, but still always complete the
1503 # more usual bad/new/good/old because git bisect gives a good error
1504 # message if these are given when not in use, and that's better than
1505 # silent refusal to complete if the user is confused.
1507 # We want to recognize 'view' but not complete it, because it overlaps
1508 # with 'visualize' too much and is just an alias for it.
1510 local completable_subcommands="start bad new $term_bad good old $term_good terms skip reset visualize replay log run help"
1511 local all_subcommands="$completable_subcommands view"
1513 local subcommand="$(__git_find_on_cmdline "$all_subcommands")"
1515 if [ -z "$subcommand" ]; then
1516 __git_find_repo_path
1517 if [ -f "$__git_repo_path"/BISECT_START ]; then
1518 __gitcomp "$completable_subcommands"
1520 __gitcomp "replay start"
1525 case "$subcommand" in
1529 __gitcomp "--first-parent --no-checkout --term-new --term-bad --term-old --term-good"
1538 __gitcomp "--term-good --term-old --term-bad --term-new"
1542 __git_complete_log_opts
1545 bad|new|"$term_bad"|good|old|"$term_good"|reset|skip)
1553 __git_ref_fieldlist="refname objecttype objectsize objectname upstream push HEAD symref"
1557 local i c="$__git_cmd_idx" only_local_ref="n" has_r="n"
1559 while [ $c -lt $cword ]; do
1562 -d|-D|--delete|-m|-M|--move|-c|-C|--copy)
1563 only_local_ref="y" ;;
1571 --set-upstream-to=*)
1572 __git_complete_refs --cur="${cur##--set-upstream-to=}"
1575 __gitcomp_builtin branch
1578 if [ $only_local_ref = "y" -a $has_r = "n" ]; then
1579 __gitcomp_direct "$(__git_heads "" "$cur" " ")"
1589 local cmd="${words[__git_cmd_idx+1]}"
1591 $((__git_cmd_idx+1)))
1592 __gitcomp "create list-heads verify unbundle"
1594 $((__git_cmd_idx+2)))
1595 # looking for a file
1600 __git_complete_revlist
1607 # Helper function to decide whether or not we should enable DWIM logic for
1608 # git-switch and git-checkout.
1610 # To decide between the following rules in decreasing priority order:
1611 # - the last provided of "--guess" or "--no-guess" explicitly enable or
1612 # disable completion of DWIM logic respectively.
1613 # - If checkout.guess is false, disable completion of DWIM logic.
1614 # - If the --no-track option is provided, take this as a hint to disable the
1615 # DWIM completion logic
1616 # - If GIT_COMPLETION_CHECKOUT_NO_GUESS is set, disable the DWIM completion
1617 # logic, as requested by the user.
1618 # - Enable DWIM logic otherwise.
1620 __git_checkout_default_dwim_mode ()
1622 local last_option dwim_opt="--dwim"
1624 if [ "${GIT_COMPLETION_CHECKOUT_NO_GUESS-}" = "1" ]; then
1628 # --no-track disables DWIM, but with lower priority than
1629 # --guess/--no-guess/checkout.guess
1630 if [ -n "$(__git_find_on_cmdline "--no-track")" ]; then
1634 # checkout.guess = false disables DWIM, but with lower priority than
1635 # --guess/--no-guess
1636 if [ "$(__git config --type=bool checkout.guess)" = "false" ]; then
1640 # Find the last provided --guess or --no-guess
1641 last_option="$(__git_find_last_on_cmdline "--guess --no-guess")"
1642 case "$last_option" in
1656 __git_has_doubledash && return
1658 local dwim_opt="$(__git_checkout_default_dwim_mode)"
1662 # Complete local branches (and DWIM branch
1663 # remote branch names) for an option argument
1664 # specifying a new branch name. This is for
1665 # convenience, assuming new branches are
1666 # possibly based on pre-existing branch names.
1667 __git_complete_refs $dwim_opt --mode="heads"
1676 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
1679 __gitcomp_builtin checkout
1682 # At this point, we've already handled special completion for
1683 # the arguments to -b/-B, and --orphan. There are 3 main
1684 # things left we can possibly complete:
1685 # 1) a start-point for -b/-B, -d/--detach, or --orphan
1686 # 2) a remote head, for --track
1687 # 3) an arbitrary reference, possibly including DWIM names
1690 if [ -n "$(__git_find_on_cmdline "-b -B -d --detach --orphan")" ]; then
1691 __git_complete_refs --mode="refs"
1692 elif [ -n "$(__git_find_on_cmdline "-t --track")" ]; then
1693 __git_complete_refs --mode="remote-heads"
1695 __git_complete_refs $dwim_opt --mode="refs"
1701 __git_sequencer_inprogress_options="--continue --quit --abort --skip"
1703 __git_cherry_pick_inprogress_options=$__git_sequencer_inprogress_options
1707 if __git_pseudoref_exists CHERRY_PICK_HEAD; then
1708 __gitcomp "$__git_cherry_pick_inprogress_options"
1712 __git_complete_strategy && return
1716 __gitcomp_builtin cherry-pick "" \
1717 "$__git_cherry_pick_inprogress_options"
1729 __gitcomp_builtin clean
1734 # XXX should we check for -x option ?
1735 __git_complete_index_file "--others --directory"
1742 __git_complete_config_variable_name_and_value
1748 __git_complete_config_variable_name_and_value \
1749 --cur="${cur##--config=}"
1753 __gitcomp_builtin clone
1759 __git_untracked_file_modes="all no normal"
1761 __git_trailer_tokens ()
1763 __git config --name-only --get-regexp '^trailer\..*\.key$' | cut -d. -f 2- | rev | cut -d. -f2- | rev
1777 __gitcomp "default scissors strip verbatim whitespace
1778 " "" "${cur##--cleanup=}"
1781 --reuse-message=*|--reedit-message=*|\
1782 --fixup=*|--squash=*)
1783 __git_complete_refs --cur="${cur#*=}"
1786 --untracked-files=*)
1787 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
1791 __gitcomp_nl "$(__git_trailer_tokens)" "" "${cur##--trailer=}" ":"
1795 __gitcomp_builtin commit
1799 if __git rev-parse --verify --quiet HEAD >/dev/null; then
1800 __git_complete_index_file "--committable"
1802 # This is the first commit
1803 __git_complete_index_file "--cached"
1811 __gitcomp_builtin describe
1817 __git_diff_algorithms="myers minimal patience histogram"
1819 __git_diff_submodule_formats="diff log short"
1821 __git_color_moved_opts="no default plain blocks zebra dimmed-zebra"
1823 __git_color_moved_ws_opts="no ignore-space-at-eol ignore-space-change
1824 ignore-all-space allow-indentation-change"
1826 __git_ws_error_highlight_opts="context old new all default"
1828 # Options for the diff machinery (diff, log, show, stash, range-diff, ...)
1829 __git_diff_common_options="--stat --numstat --shortstat --summary
1830 --patch-with-stat --name-only --name-status --color
1831 --no-color --color-words --no-renames --check
1832 --color-moved --color-moved= --no-color-moved
1833 --color-moved-ws= --no-color-moved-ws
1834 --full-index --binary --abbrev --diff-filter=
1835 --find-copies --find-object --find-renames
1836 --no-relative --relative
1837 --find-copies-harder --ignore-cr-at-eol
1838 --text --ignore-space-at-eol --ignore-space-change
1839 --ignore-all-space --ignore-blank-lines --exit-code
1840 --quiet --ext-diff --no-ext-diff --unified=
1841 --no-prefix --src-prefix= --dst-prefix=
1842 --inter-hunk-context= --function-context
1843 --patience --histogram --minimal
1844 --raw --word-diff --word-diff-regex=
1845 --dirstat --dirstat= --dirstat-by-file
1846 --dirstat-by-file= --cumulative
1847 --diff-algorithm= --default-prefix
1848 --submodule --submodule= --ignore-submodules
1849 --indent-heuristic --no-indent-heuristic
1850 --textconv --no-textconv --break-rewrites
1851 --patch --no-patch --cc --combined-all-paths
1852 --anchored= --compact-summary --ignore-matching-lines=
1853 --irreversible-delete --line-prefix --no-stat
1854 --output= --output-indicator-context=
1855 --output-indicator-new= --output-indicator-old=
1856 --ws-error-highlight=
1857 --pickaxe-all --pickaxe-regex --patch-with-raw
1860 # Options for diff/difftool
1861 __git_diff_difftool_options="--cached --staged
1862 --base --ours --theirs --no-index --merge-base
1863 --ita-invisible-in-index --ita-visible-in-index
1864 $__git_diff_common_options"
1868 __git_has_doubledash && return
1872 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1876 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1880 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
1884 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
1887 --ws-error-highlight=*)
1888 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
1892 __gitcomp "$__git_diff_difftool_options"
1896 __git_complete_revlist_file
1899 __git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff
1900 tkdiff vimdiff nvimdiff gvimdiff xxdiff araxis p4merge
1901 bc codecompare smerge
1906 __git_has_doubledash && return
1910 __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1914 __gitcomp_builtin difftool "$__git_diff_difftool_options"
1918 __git_complete_revlist_file
1921 __git_fetch_recurse_submodules="yes on-demand no"
1926 --recurse-submodules=*)
1927 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1931 __gitcomp "blob:none blob:limit= sparse:oid=" "" "${cur##--filter=}"
1935 __gitcomp_builtin fetch
1939 __git_complete_remote_or_refspec
1942 __git_format_patch_extra_options="
1943 --full-index --not --all --no-prefix --src-prefix=
1944 --dst-prefix= --notes
1947 _git_format_patch ()
1953 " "" "${cur##--thread=}"
1956 --base=*|--interdiff=*|--range-diff=*)
1957 __git_complete_refs --cur="${cur#--*=}"
1961 __gitcomp_builtin format-patch "$__git_format_patch_extra_options"
1965 __git_complete_revlist
1972 __gitcomp_builtin fsck
1983 # Lists matching symbol names from a tag (as in ctags) file.
1984 # 1: List symbol names matching this word.
1985 # 2: The tag file to list symbol names from.
1986 # 3: A prefix to be added to each listed symbol name (optional).
1987 # 4: A suffix to be appended to each listed symbol name (optional).
1988 __git_match_ctag () {
1989 awk -v pfx="${3-}" -v sfx="${4-}" "
1990 /^${1//\//\\/}/ { print pfx \$1 sfx }
1994 # Complete symbol names from a tag file.
1995 # Usage: __git_complete_symbol [<option>]...
1996 # --tags=<file>: The tag file to list symbol names from instead of the
1998 # --pfx=<prefix>: A prefix to be added to each symbol name.
1999 # --cur=<word>: The current symbol name to be completed. Defaults to
2000 # the current word to be completed.
2001 # --sfx=<suffix>: A suffix to be appended to each symbol name instead
2002 # of the default space.
2003 __git_complete_symbol () {
2004 local tags=tags pfx="" cur_="${cur-}" sfx=" "
2006 while test $# != 0; do
2008 --tags=*) tags="${1##--tags=}" ;;
2009 --pfx=*) pfx="${1##--pfx=}" ;;
2010 --cur=*) cur_="${1##--cur=}" ;;
2011 --sfx=*) sfx="${1##--sfx=}" ;;
2017 if test -r "$tags"; then
2018 __gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
2024 __git_has_doubledash && return
2028 __gitcomp_builtin grep
2033 case "$cword,$prev" in
2034 $((__git_cmd_idx+1)),*|*,-*)
2035 __git_complete_symbol && return
2046 __gitcomp_builtin help
2050 if test -n "${GIT_TESTING_ALL_COMMAND_LIST-}"
2052 __gitcomp "$GIT_TESTING_ALL_COMMAND_LIST $(__git --list-cmds=alias,list-guide) gitk"
2054 __gitcomp "$(__git --list-cmds=main,nohelpers,alias,list-guide) gitk"
2063 false true umask group all world everybody
2064 " "" "${cur##--shared=}"
2068 __gitcomp_builtin init
2078 __gitcomp_builtin ls-files
2083 # XXX ignore options like --modified and always suggest all cached
2085 __git_complete_index_file "--cached"
2092 __gitcomp_builtin ls-remote
2096 __gitcomp_nl "$(__git_remotes)"
2103 __gitcomp_builtin ls-tree
2111 # Options that go well for log, shortlog and gitk
2112 __git_log_common_options="
2114 --branches --tags --remotes
2115 --first-parent --merges --no-merges
2117 --max-age= --since= --after=
2118 --min-age= --until= --before=
2119 --min-parents= --max-parents=
2120 --no-min-parents --no-max-parents
2121 --alternate-refs --ancestry-path
2122 --author-date-order --basic-regexp
2123 --bisect --boundary --exclude-first-parent-only
2124 --exclude-hidden --extended-regexp
2125 --fixed-strings --grep-reflog
2126 --ignore-missing --left-only --perl-regexp
2127 --reflog --regexp-ignore-case --remove-empty
2128 --right-only --show-linear-break
2129 --show-notes-by-default --show-pulls
2130 --since-as-filter --single-worktree
2132 # Options that go well for log and gitk (not shortlog)
2133 __git_log_gitk_options="
2134 --dense --sparse --full-history
2135 --simplify-merges --simplify-by-decoration
2136 --left-right --notes --no-notes
2138 # Options that go well for log and shortlog (not gitk)
2139 __git_log_shortlog_options="
2140 --author= --committer= --grep=
2141 --all-match --invert-grep
2143 # Options accepted by log and show
2144 __git_log_show_options="
2145 --diff-merges --diff-merges= --no-diff-merges --dd --remerge-diff
2149 __git_diff_merges_opts="off none on first-parent 1 separate m combined c dense-combined cc remerge r"
2151 __git_log_pretty_formats="oneline short medium full fuller reference email raw format: tformat: mboxrd"
2152 __git_log_date_formats="relative iso8601 iso8601-strict rfc2822 short local default human raw unix auto: format:"
2154 # Complete porcelain (i.e. not git-rev-list) options and at least some
2155 # option arguments accepted by git-log. Note that this same set of options
2156 # are also accepted by some other git commands besides git-log.
2157 __git_complete_log_opts ()
2162 if __git_pseudoref_exists MERGE_HEAD; then
2165 case "$prev,$cur" in
2167 return # fall back to Bash filename completion
2170 __git_complete_symbol --cur="${cur#:}" --sfx=":"
2174 __git_complete_symbol
2179 --pretty=*|--format=*)
2180 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2185 __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
2189 __gitcomp "full short no" "" "${cur##--decorate=}"
2193 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
2197 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
2200 --ws-error-highlight=*)
2201 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
2205 __gitcomp "sorted unsorted" "" "${cur##--no-walk=}"
2209 __gitcomp "$__git_diff_merges_opts" "" "${cur##--diff-merges=}"
2214 $__git_log_common_options
2215 $__git_log_shortlog_options
2216 $__git_log_gitk_options
2217 $__git_log_show_options
2218 --root --topo-order --date-order --reverse
2219 --follow --full-diff
2220 --abbrev-commit --no-abbrev-commit --abbrev=
2221 --relative-date --date=
2222 --pretty= --format= --oneline
2227 --decorate --decorate= --no-decorate
2229 --no-walk --no-walk= --do-walk
2230 --parents --children
2231 --expand-tabs --expand-tabs= --no-expand-tabs
2232 --clear-decorations --decorate-refs=
2233 --decorate-refs-exclude=
2235 $__git_diff_common_options
2240 return # fall back to Bash filename completion
2243 __git_complete_symbol --cur="${cur#-L:}" --sfx=":"
2247 __git_complete_symbol --pfx="-G" --cur="${cur#-G}"
2251 __git_complete_symbol --pfx="-S" --cur="${cur#-S}"
2259 __git_has_doubledash && return
2260 __git_find_repo_path
2262 __git_complete_log_opts
2263 [ ${#COMPREPLY[@]} -eq 0 ] || return
2265 __git_complete_revlist
2270 __git_complete_strategy && return
2274 __gitcomp_builtin merge
2284 __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
2288 __gitcomp "--tool= --prompt --no-prompt --gui --no-gui"
2298 __gitcomp_builtin merge-base
2309 __gitcomp_builtin mv
2314 if [ $(__git_count_arguments "mv") -gt 0 ]; then
2315 # We need to show both cached and untracked files (including
2316 # empty directories) since this may not be the last argument.
2317 __git_complete_index_file "--cached --others --directory"
2319 __git_complete_index_file "--cached"
2325 local subcommands='add append copy edit get-ref list merge prune remove show'
2326 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2328 case "$subcommand,$cur" in
2330 __gitcomp_builtin notes
2338 __gitcomp "$subcommands --ref"
2342 *,--reuse-message=*|*,--reedit-message=*)
2343 __git_complete_refs --cur="${cur#*=}"
2346 __gitcomp_builtin notes_$subcommand
2349 # this command does not take a ref, do not complete it
2365 __git_complete_strategy && return
2368 --recurse-submodules=*)
2369 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
2373 __gitcomp_builtin pull
2378 __git_complete_remote_or_refspec
2381 __git_push_recurse_submodules="check on-demand only"
2383 __git_complete_force_with_lease ()
2391 __git_complete_refs --cur="${cur_#*:}"
2394 __git_complete_refs --cur="$cur_"
2403 __gitcomp_nl "$(__git_remotes)"
2406 --recurse-submodules)
2407 __gitcomp "$__git_push_recurse_submodules"
2413 __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
2416 --recurse-submodules=*)
2417 __gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
2420 --force-with-lease=*)
2421 __git_complete_force_with_lease "${cur##--force-with-lease=}"
2425 __gitcomp_builtin push
2429 __git_complete_remote_or_refspec
2437 --creation-factor= --no-dual-color
2438 $__git_diff_common_options
2443 __git_complete_revlist
2446 __git_rebase_inprogress_options="--continue --skip --abort --quit --show-current-patch"
2447 __git_rebase_interactive_inprogress_options="$__git_rebase_inprogress_options --edit-todo"
2451 __git_find_repo_path
2452 if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
2453 __gitcomp "$__git_rebase_interactive_inprogress_options"
2455 elif [ -d "$__git_repo_path"/rebase-apply ] || \
2456 [ -d "$__git_repo_path"/rebase-merge ]; then
2457 __gitcomp "$__git_rebase_inprogress_options"
2460 __git_complete_strategy && return
2463 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
2467 __git_complete_refs --cur="${cur##--onto=}"
2471 __gitcomp_builtin rebase "" \
2472 "$__git_rebase_interactive_inprogress_options"
2481 local subcommands="show delete expire"
2482 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2484 if [ -z "$subcommand" ]; then
2485 __gitcomp "$subcommands"
2491 __git_send_email_confirm_options="always never auto cc compose"
2492 __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
2497 --to|--cc|--bcc|--from)
2498 __gitcomp "$(__git send-email --dump-aliases)"
2506 $__git_send_email_confirm_options
2507 " "" "${cur##--confirm=}"
2512 $__git_send_email_suppresscc_options
2513 " "" "${cur##--suppress-cc=}"
2517 --smtp-encryption=*)
2518 __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
2524 " "" "${cur##--thread=}"
2527 --to=*|--cc=*|--bcc=*|--from=*)
2528 __gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
2532 __gitcomp_builtin send-email "$__git_format_patch_extra_options"
2536 __git_complete_revlist
2547 local untracked_state
2550 --ignore-submodules=*)
2551 __gitcomp "none untracked dirty all" "" "${cur##--ignore-submodules=}"
2554 --untracked-files=*)
2555 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
2560 always never auto column row plain dense nodense
2561 " "" "${cur##--column=}"
2565 __gitcomp_builtin status
2570 untracked_state="$(__git_get_option_value "-u" "--untracked-files=" \
2571 "$__git_untracked_file_modes" "status.showUntrackedFiles")"
2573 case "$untracked_state" in
2575 # --ignored option does not matter
2579 complete_opt="--cached --directory --no-empty-directory --others"
2581 if [ -n "$(__git_find_on_cmdline "--ignored")" ]; then
2582 complete_opt="$complete_opt --ignored --exclude=*"
2587 __git_complete_index_file "$complete_opt"
2592 local dwim_opt="$(__git_checkout_default_dwim_mode)"
2596 # Complete local branches (and DWIM branch
2597 # remote branch names) for an option argument
2598 # specifying a new branch name. This is for
2599 # convenience, assuming new branches are
2600 # possibly based on pre-existing branch names.
2601 __git_complete_refs $dwim_opt --mode="heads"
2610 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
2613 __gitcomp_builtin switch
2616 # Unlike in git checkout, git switch --orphan does not take
2617 # a start point. Thus we really have nothing to complete after
2619 if [ -n "$(__git_find_on_cmdline "--orphan")" ]; then
2623 # At this point, we've already handled special completion for
2624 # -c/-C, and --orphan. There are 3 main things left to
2626 # 1) a start-point for -c/-C or -d/--detach
2627 # 2) a remote head, for --track
2628 # 3) a branch name, possibly including DWIM remote branches
2630 if [ -n "$(__git_find_on_cmdline "-c -C -d --detach")" ]; then
2631 __git_complete_refs --mode="refs"
2632 elif [ -n "$(__git_find_on_cmdline "-t --track")" ]; then
2633 __git_complete_refs --mode="remote-heads"
2635 __git_complete_refs $dwim_opt --mode="heads"
2641 __git_config_get_set_variables ()
2643 local prevword word config_file= c=$cword
2644 while [ $c -gt "$__git_cmd_idx" ]; do
2647 --system|--global|--local|--file=*)
2652 config_file="$word $prevword"
2660 __git config $config_file --name-only --list
2664 __git_compute_config_vars ()
2666 test -n "$__git_config_vars" ||
2667 __git_config_vars="$(git help --config-for-completion)"
2670 __git_config_vars_all=
2671 __git_compute_config_vars_all ()
2673 test -n "$__git_config_vars_all" ||
2674 __git_config_vars_all="$(git --no-pager help --config)"
2677 __git_compute_first_level_config_vars_for_section ()
2680 __git_compute_config_vars
2681 local this_section="__git_first_level_config_vars_for_section_${section}"
2682 test -n "${!this_section}" ||
2683 printf -v "__git_first_level_config_vars_for_section_${section}" %s "$(echo "$__git_config_vars" | grep -E "^${section}\.[a-z]" | awk -F. '{print $2}')"
2686 __git_compute_second_level_config_vars_for_section ()
2689 __git_compute_config_vars_all
2690 local this_section="__git_second_level_config_vars_for_section_${section}"
2691 test -n "${!this_section}" ||
2692 printf -v "__git_second_level_config_vars_for_section_${section}" %s "$(echo "$__git_config_vars_all" | grep -E "^${section}\.<" | awk -F. '{print $3}')"
2695 __git_config_sections=
2696 __git_compute_config_sections ()
2698 test -n "$__git_config_sections" ||
2699 __git_config_sections="$(git help --config-sections-for-completion)"
2702 # Completes possible values of various configuration variables.
2704 # Usage: __git_complete_config_variable_value [<option>]...
2705 # --varname=<word>: The name of the configuration variable whose value is
2706 # to be completed. Defaults to the previous word on the
2708 # --cur=<word>: The current value to be completed. Defaults to the current
2709 # word to be completed.
2710 __git_complete_config_variable_value ()
2712 local varname="$prev" cur_="$cur"
2714 while test $# != 0; do
2716 --varname=*) varname="${1##--varname=}" ;;
2717 --cur=*) cur_="${1##--cur=}" ;;
2723 if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
2724 varname="${varname,,}"
2726 varname="$(echo "$varname" |tr A-Z a-z)"
2730 branch.*.remote|branch.*.pushremote)
2731 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2735 __git_complete_refs --cur="$cur_"
2739 __gitcomp "false true merges interactive" "" "$cur_"
2743 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2747 local remote="${varname#remote.}"
2748 remote="${remote%.fetch}"
2749 if [ -z "$cur_" ]; then
2750 __gitcomp_nl "refs/heads/" "" "" ""
2753 __gitcomp_nl "$(__git_refs_remotes "$remote")" "" "$cur_"
2757 local remote="${varname#remote.}"
2758 remote="${remote%.push}"
2759 __gitcomp_nl "$(__git for-each-ref \
2760 --format='%(refname):%(refname)' refs/heads)" "" "$cur_"
2763 pull.twohead|pull.octopus)
2764 __git_compute_merge_strategies
2765 __gitcomp "$__git_merge_strategies" "" "$cur_"
2769 __gitcomp "false true" "" "$cur_"
2774 normal black red green yellow blue magenta cyan white
2775 bold dim ul blink reverse
2780 __gitcomp "false true always never auto" "" "$cur_"
2784 __gitcomp "$__git_diff_submodule_formats" "" "$cur_"
2788 __gitcomp "man info web html" "" "$cur_"
2792 __gitcomp "$__git_log_date_formats" "" "$cur_"
2795 sendemail.aliasfiletype)
2796 __gitcomp "mutt mailrc pine elm gnus" "" "$cur_"
2800 __gitcomp "$__git_send_email_confirm_options" "" "$cur_"
2803 sendemail.suppresscc)
2804 __gitcomp "$__git_send_email_suppresscc_options" "" "$cur_"
2807 sendemail.transferencoding)
2808 __gitcomp "7bit 8bit quoted-printable base64" "" "$cur_"
2817 # Completes configuration sections, subsections, variable names.
2819 # Usage: __git_complete_config_variable_name [<option>]...
2820 # --cur=<word>: The current configuration section/variable name to be
2821 # completed. Defaults to the current word to be completed.
2822 # --sfx=<suffix>: A suffix to be appended to each fully completed
2823 # configuration variable name (but not to sections or
2824 # subsections) instead of the default space.
2825 __git_complete_config_variable_name ()
2827 local cur_="$cur" sfx
2829 while test $# != 0; do
2831 --cur=*) cur_="${1##--cur=}" ;;
2832 --sfx=*) sfx="${1##--sfx=}" ;;
2839 branch.*.*|guitool.*.*|difftool.*.*|man.*.*|mergetool.*.*|remote.*.*|submodule.*.*|url.*.*)
2840 local pfx="${cur_%.*}."
2842 local section="${pfx%.*.}"
2843 __git_compute_second_level_config_vars_for_section "${section}"
2844 local this_section="__git_second_level_config_vars_for_section_${section}"
2845 __gitcomp "${!this_section}" "$pfx" "$cur_" "$sfx"
2849 local pfx="${cur_%.*}."
2851 local section="${pfx%.}"
2852 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
2853 __git_compute_first_level_config_vars_for_section "${section}"
2854 local this_section="__git_first_level_config_vars_for_section_${section}"
2855 __gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
2859 local pfx="${cur_%.*}."
2861 __git_compute_all_commands
2862 __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_" "${sfx:- }"
2866 local pfx="${cur_%.*}."
2868 local section="${pfx%.}"
2869 __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
2870 __git_compute_first_level_config_vars_for_section "${section}"
2871 local this_section="__git_first_level_config_vars_for_section_${section}"
2872 __gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
2876 local pfx="${cur_%.*}."
2878 local section="${pfx%.}"
2879 __gitcomp_nl "$(__git config -f "$(__git rev-parse --show-toplevel)/.gitmodules" --get-regexp 'submodule.*.path' | awk -F. '{print $2}')" "$pfx" "$cur_" "."
2880 __git_compute_first_level_config_vars_for_section "${section}"
2881 local this_section="__git_first_level_config_vars_for_section_${section}"
2882 __gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
2886 __git_compute_config_vars
2887 __gitcomp "$__git_config_vars" "" "$cur_" "$sfx"
2890 __git_compute_config_sections
2891 __gitcomp "$__git_config_sections" "" "$cur_" "."
2896 # Completes '='-separated configuration sections/variable names and values
2897 # for 'git -c section.name=value'.
2899 # Usage: __git_complete_config_variable_name_and_value [<option>]...
2900 # --cur=<word>: The current configuration section/variable name/value to be
2901 # completed. Defaults to the current word to be completed.
2902 __git_complete_config_variable_name_and_value ()
2906 while test $# != 0; do
2908 --cur=*) cur_="${1##--cur=}" ;;
2916 __git_complete_config_variable_value \
2917 --varname="${cur_%%=*}" --cur="${cur_#*=}"
2920 __git_complete_config_variable_name --cur="$cur_" --sfx='='
2928 --get|--get-all|--unset|--unset-all)
2929 __gitcomp_nl "$(__git_config_get_set_variables)"
2933 __git_complete_config_variable_value
2939 __gitcomp_builtin config
2942 __git_complete_config_variable_name
2950 add rename remove set-head set-branches
2951 get-url set-url show prune update
2953 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2954 if [ -z "$subcommand" ]; then
2957 __gitcomp_builtin remote
2960 __gitcomp "$subcommands"
2966 case "$subcommand,$cur" in
2968 __gitcomp_builtin remote_add
2973 __gitcomp_builtin remote_set-head
2976 __gitcomp_builtin remote_set-branches
2978 set-head,*|set-branches,*)
2979 __git_complete_remote_or_refspec
2982 __gitcomp_builtin remote_update
2985 __gitcomp "$(__git_remotes) $(__git_get_config_variables "remotes")"
2988 __gitcomp_builtin remote_set-url
2991 __gitcomp_builtin remote_get-url
2994 __gitcomp_builtin remote_prune
2997 __gitcomp_nl "$(__git_remotes)"
3006 __gitcomp "short medium long" "" "${cur##--format=}"
3010 __gitcomp_builtin replace
3019 local subcommands="clear forget diff remaining status gc"
3020 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3021 if test -z "$subcommand"
3023 __gitcomp "$subcommands"
3030 __git_has_doubledash && return
3034 __gitcomp_builtin reset
3052 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
3055 __git_complete_refs --cur="${cur##--source=}"
3058 __gitcomp_builtin restore
3061 if __git_pseudoref_exists HEAD; then
3062 __git_complete_index_file "--modified"
3067 __git_revert_inprogress_options=$__git_sequencer_inprogress_options
3071 if __git_pseudoref_exists REVERT_HEAD; then
3072 __gitcomp "$__git_revert_inprogress_options"
3075 __git_complete_strategy && return
3078 __gitcomp_builtin revert "" \
3079 "$__git_revert_inprogress_options"
3090 __gitcomp_builtin rm
3095 __git_complete_index_file "--cached"
3100 __git_has_doubledash && return
3105 $__git_log_common_options
3106 $__git_log_shortlog_options
3107 --numbered --summary --email
3112 __git_complete_revlist
3117 __git_has_doubledash && return
3120 --pretty=*|--format=*)
3121 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
3126 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
3130 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
3134 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
3138 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
3141 --ws-error-highlight=*)
3142 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
3146 __gitcomp "$__git_diff_merges_opts" "" "${cur##--diff-merges=}"
3150 __gitcomp "--pretty= --format= --abbrev-commit --no-abbrev-commit
3151 --oneline --show-signature
3152 --expand-tabs --expand-tabs= --no-expand-tabs
3153 $__git_log_show_options
3154 $__git_diff_common_options
3159 __git_complete_revlist_file
3166 __gitcomp_builtin show-branch
3170 __git_complete_revlist
3173 __gitcomp_directories ()
3175 local _tmp_dir _tmp_completions _found=0
3177 # Get the directory of the current token; this differs from dirname
3178 # in that it keeps up to the final trailing slash. If no slash found
3181 _tmp_dir=$BASH_REMATCH
3183 # Find possible directory completions, adding trailing '/' characters,
3184 # de-quoting, and handling unusual characters.
3185 while IFS= read -r -d $'\0' c ; do
3186 # If there are directory completions, find ones that start
3187 # with "$cur", the current token, and put those in COMPREPLY
3188 if [[ $c == "$cur"* ]]; then
3192 done < <(__git ls-tree -z -d --name-only HEAD $_tmp_dir)
3194 if [[ $_found == 0 ]] && [[ "$cur" =~ /$ ]]; then
3195 # No possible further completions any deeper, so assume we're at
3196 # a leaf directory and just consider it complete
3197 __gitcomp_direct_append "$cur "
3198 elif [[ $_found == 0 ]]; then
3199 # No possible completions found. Avoid falling back to
3200 # bash's default file and directory completion, because all
3201 # valid completions have already been searched and the
3202 # fallbacks can do nothing but mislead. In fact, they can
3203 # mislead in three different ways:
3204 # 1) Fallback file completion makes no sense when asking
3205 # for directory completions, as this function does.
3206 # 2) Fallback directory completion is bad because
3207 # e.g. "/pro" is invalid and should NOT complete to
3209 # 3) Fallback file/directory completion only completes
3210 # on paths that exist in the current working tree,
3211 # i.e. which are *already* part of their
3212 # sparse-checkout. Thus, normal file and directory
3213 # completion is always useless for "git
3214 # sparse-checkout add" and is also probelmatic for
3215 # "git sparse-checkout set" unless using it to
3216 # strictly narrow the checkout.
3221 # In non-cone mode, the arguments to {set,add} are supposed to be
3222 # patterns, relative to the toplevel directory. These can be any kind
3223 # of general pattern, like 'subdir/*.c' and we can't complete on all
3224 # of those. However, if the user presses Tab to get tab completion, we
3225 # presume that they are trying to provide a pattern that names a specific
3227 __gitcomp_slash_leading_paths ()
3229 local dequoted_word pfx="" cur_ toplevel
3231 # Since we are dealing with a sparse-checkout, subdirectories may not
3232 # exist in the local working copy. Therefore, we want to run all
3233 # ls-files commands relative to the repository toplevel.
3234 toplevel="$(git rev-parse --show-toplevel)/"
3236 __git_dequote "$cur"
3238 # If the paths provided by the user already start with '/', then
3239 # they are considered relative to the toplevel of the repository
3240 # already. If they do not start with /, then we need to adjust
3241 # them to start with the appropriate prefix.
3247 pfx="$(__git rev-parse --show-prefix)"
3250 # Since sparse-index is limited to cone-mode, in non-cone-mode the
3251 # list of valid paths is precisely the cached files in the index.
3254 # 1) We probably need to take care of cases where ls-files
3255 # responds with special quoting.
3256 # 2) We probably need to take care of cases where ${cur} has
3257 # some kind of special quoting.
3258 # 3) On top of any quoting from 1 & 2, we have to provide an extra
3259 # level of quoting for any paths that contain a '*', '?', '\',
3260 # '[', ']', or leading '#' or '!' since those will be
3261 # interpreted by sparse-checkout as something other than a
3262 # literal path character.
3263 # Since there are two types of quoting here, this might get really
3264 # complex. For now, just punt on all of this...
3265 completions="$(__git -C "${toplevel}" -c core.quotePath=false \
3266 ls-files --cached -- "${pfx}${cur}*" \
3267 | sed -e s%^%/% -e 's%$% %')"
3268 # Note, above, though that we needed all of the completions to be
3269 # prefixed with a '/', and we want to add a space so that bash
3270 # completion will actually complete an entry and let us move on to
3273 # Return what we've found.
3274 if test -n "$completions"; then
3275 # We found some completions; return them
3277 COMPREPLY=($completions)
3279 # Do NOT fall back to bash-style all-local-files-and-dirs
3280 # when we find no match. Such options are worse than
3282 # 1. "git sparse-checkout add" needs paths that are NOT
3283 # currently in the working copy. "git
3284 # sparse-checkout set" does as well, except in the
3285 # special cases when users are only trying to narrow
3286 # their sparse checkout to a subset of what they
3289 # 2. A path like '.config' is ambiguous as to whether
3290 # the user wants all '.config' files throughout the
3291 # tree, or just the one under the current directory.
3292 # It would result in a warning from the
3293 # sparse-checkout command due to this. As such, all
3294 # completions of paths should be prefixed with a
3297 # 3. We don't want paths prefixed with a '/' to
3298 # complete files in the system root directory, we
3299 # want it to complete on files relative to the
3302 # As such, make sure that NO completions are offered rather
3303 # than falling back to bash's default completions.
3308 _git_sparse_checkout ()
3310 local subcommands="list init set disable add reapply"
3311 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3312 local using_cone=true
3313 if [ -z "$subcommand" ]; then
3314 __gitcomp "$subcommands"
3318 case "$subcommand,$cur" in
3320 __gitcomp_builtin sparse-checkout_$subcommand "" "--"
3323 if [[ "$(__git config core.sparseCheckout)" == "true" &&
3324 "$(__git config core.sparseCheckoutCone)" == "false" &&
3325 -z "$(__git_find_on_cmdline --cone)" ]]; then
3328 if [[ -n "$(__git_find_on_cmdline --no-cone)" ]]; then
3331 if [[ "$using_cone" == "true" ]]; then
3332 __gitcomp_directories
3334 __gitcomp_slash_leading_paths
3341 local subcommands='push list show apply clear drop pop create branch'
3342 local subcommand="$(__git_find_on_cmdline "$subcommands save")"
3344 if [ -z "$subcommand" ]; then
3345 case "$((cword - __git_cmd_idx)),$cur" in
3347 __gitcomp_builtin stash_push
3353 __gitcomp "$subcommands"
3359 case "$subcommand,$cur" in
3361 # NEEDSWORK: can we somehow unify this with the options in _git_log() and _git_show()
3362 __gitcomp_builtin stash_list "$__git_log_common_options $__git_diff_common_options"
3365 __gitcomp_builtin stash_show "$__git_diff_common_options"
3368 __gitcomp_builtin "stash_$subcommand"
3371 if [ $cword -eq $((__git_cmd_idx+2)) ]; then
3374 __gitcomp_nl "$(__git stash list \
3375 | sed -n -e 's/:.*//p')"
3378 show,*|apply,*|drop,*|pop,*)
3379 __gitcomp_nl "$(__git stash list \
3380 | sed -n -e 's/:.*//p')"
3387 __git_has_doubledash && return
3389 local subcommands="add status init deinit update set-branch set-url summary foreach sync absorbgitdirs"
3390 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3391 if [ -z "$subcommand" ]; then
3397 __gitcomp "$subcommands"
3403 case "$subcommand,$cur" in
3405 __gitcomp "--branch --force --name --reference --depth"
3408 __gitcomp "--cached --recursive"
3411 __gitcomp "--force --all"
3415 --init --remote --no-fetch
3416 --recommend-shallow --no-recommend-shallow
3417 --force --rebase --merge --reference --depth --recursive --jobs
3421 __gitcomp "--default --branch"
3424 __gitcomp "--cached --files --summary-limit"
3426 foreach,--*|sync,--*)
3427 __gitcomp "--recursive"
3437 init fetch clone rebase dcommit log find-rev
3438 set-tree commit-diff info create-ignore propget
3439 proplist show-ignore show-externals branch tag blame
3440 migrate mkdirs reset gc
3442 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3443 if [ -z "$subcommand" ]; then
3444 __gitcomp "$subcommands"
3446 local remote_opts="--username= --config-dir= --no-auth-cache"
3448 --follow-parent --authors-file= --repack=
3449 --no-metadata --use-svm-props --use-svnsync-props
3450 --log-window-size= --no-checkout --quiet
3451 --repack-flags --use-log-author --localtime
3454 --ignore-paths= --include-paths= $remote_opts
3457 --template= --shared= --trunk= --tags=
3458 --branches= --stdlayout --minimize-url
3459 --no-metadata --use-svm-props --use-svnsync-props
3460 --rewrite-root= --prefix= $remote_opts
3463 --edit --rmdir --find-copies-harder --copy-similarity=
3466 case "$subcommand,$cur" in
3468 __gitcomp "--revision= --fetch-all $fc_opts"
3471 __gitcomp "--revision= $fc_opts $init_opts"
3474 __gitcomp "$init_opts"
3478 --merge --strategy= --verbose --dry-run
3479 --fetch-all --no-rebase --commit-url
3480 --revision --interactive $cmt_opts $fc_opts
3484 __gitcomp "--stdin $cmt_opts $fc_opts"
3486 create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
3487 show-externals,--*|mkdirs,--*)
3488 __gitcomp "--revision="
3492 --limit= --revision= --verbose --incremental
3493 --oneline --show-commit --non-recursive
3494 --authors-file= --color
3499 --merge --verbose --strategy= --local
3500 --fetch-all --dry-run $fc_opts
3504 __gitcomp "--message= --file= --revision= $cmt_opts"
3510 __gitcomp "--dry-run --message --tag"
3513 __gitcomp "--dry-run --message"
3516 __gitcomp "--git-format"
3520 --config-dir= --ignore-paths= --minimize
3521 --no-auth-cache --username=
3525 __gitcomp "--revision= --parent"
3533 _git_symbolic_ref () {
3536 __gitcomp_builtin symbolic-ref
3546 local i c="$__git_cmd_idx" f=0
3547 while [ $c -lt $cword ]; do
3550 -d|--delete|-v|--verify)
3551 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3566 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3576 __gitcomp_builtin tag
3586 __git_complete_worktree_paths ()
3589 # Generate completion reply from worktree list skipping the first
3590 # entry: it's the path of the main worktree, which can't be moved,
3591 # removed, locked, etc.
3592 __gitcomp_nl "$(git worktree list --porcelain |
3593 sed -n -e '2,$ s/^worktree //p')"
3598 local subcommands="add list lock move prune remove unlock"
3599 local subcommand subcommand_idx
3601 subcommand="$(__git_find_on_cmdline --show-idx "$subcommands")"
3602 subcommand_idx="${subcommand% *}"
3603 subcommand="${subcommand#* }"
3605 case "$subcommand,$cur" in
3607 __gitcomp "$subcommands"
3610 __gitcomp_builtin worktree_$subcommand
3612 add,*) # usage: git worktree add [<options>] <path> [<commit-ish>]
3613 # Here we are not completing an --option, it's either the
3616 -b|-B) # Complete refs for branch to be created/reseted.
3619 -*) # The previous word is an -o|--option without an
3620 # unstuck argument: have to complete the path for
3621 # the new worktree, so don't list anything, but let
3622 # Bash fall back to filename completion.
3624 *) # The previous word is not an --option, so it must
3625 # be either the 'add' subcommand, the unstuck
3626 # argument of an option (e.g. branch for -b|-B), or
3627 # the path for the new worktree.
3628 if [ $cword -eq $((subcommand_idx+1)) ]; then
3629 # Right after the 'add' subcommand: have to
3630 # complete the path, so fall back to Bash
3631 # filename completion.
3634 case "${words[cword-2]}" in
3635 -b|-B) # After '-b <branch>': have to
3636 # complete the path, so fall back
3637 # to Bash filename completion.
3639 *) # After the path: have to complete
3640 # the ref to be checked out.
3648 lock,*|remove,*|unlock,*)
3649 __git_complete_worktree_paths
3652 if [ $cword -eq $((subcommand_idx+1)) ]; then
3653 # The first parameter must be an existing working
3655 __git_complete_worktree_paths
3657 # The second parameter is the destination: it could
3658 # be any path, so don't list anything, but let Bash
3659 # fall back to filename completion.
3666 __git_complete_common () {
3671 __gitcomp_builtin "$command"
3676 __git_cmds_with_parseopt_helper=
3677 __git_support_parseopt_helper () {
3678 test -n "$__git_cmds_with_parseopt_helper" ||
3679 __git_cmds_with_parseopt_helper="$(__git --list-cmds=parseopt)"
3681 case " $__git_cmds_with_parseopt_helper " in
3691 __git_have_func () {
3692 declare -f -- "$1" >/dev/null 2>&1
3695 __git_complete_command () {
3697 local completion_func="_git_${command//-/_}"
3698 if ! __git_have_func $completion_func &&
3699 __git_have_func _completion_loader
3701 _completion_loader "git-$command"
3703 if __git_have_func $completion_func
3707 elif __git_support_parseopt_helper "$command"
3709 __git_complete_common "$command"
3718 local i c=1 command __git_dir __git_repo_path
3719 local __git_C_args C_args_count=0
3722 while [ $c -lt $cword ]; do
3726 __git_dir="${i#--git-dir=}"
3730 __git_dir="${words[c]}"
3739 -c|--work-tree|--namespace)
3743 __git_C_args[C_args_count++]=-C
3745 __git_C_args[C_args_count++]="${words[c]}"
3758 if [ -z "${command-}" ]; then
3760 --git-dir|-C|--work-tree)
3761 # these need a path argument, let's fall back to
3762 # Bash filename completion
3766 __git_complete_config_variable_name_and_value
3770 # we don't support completing these options' arguments
3789 --no-replace-objects
3794 if test -n "${GIT_TESTING_PORCELAIN_COMMAND_LIST-}"
3796 __gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
3798 local list_cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config
3800 if test "${GIT_COMPLETION_SHOW_ALL_COMMANDS-}" = "1"
3802 list_cmds=builtins,$list_cmds
3804 __gitcomp "$(__git --list-cmds=$list_cmds)"
3811 __git_complete_command "$command" && return
3813 local expansion=$(__git_aliased_command "$command")
3814 if [ -n "$expansion" ]; then
3816 __git_complete_command "$expansion"
3822 __git_has_doubledash && return
3824 local __git_repo_path
3825 __git_find_repo_path
3828 if __git_pseudoref_exists MERGE_HEAD; then
3834 $__git_log_common_options
3835 $__git_log_gitk_options
3841 __git_complete_revlist
3844 if [[ -n ${ZSH_VERSION-} && -z ${GIT_SOURCING_ZSH_COMPLETION-} ]]; then
3845 echo "ERROR: this script is obsolete, please see git-completion.zsh" 1>&2
3851 local cur words cword prev
3852 local __git_cmd_idx=0
3853 _get_comp_words_by_ref -n =: cur words cword prev
3859 local wrapper="__git_wrap${2}"
3860 eval "$wrapper () { __git_func_wrap $2 ; }"
3861 complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
3862 || complete -o default -o nospace -F $wrapper $1
3865 # Setup the completion for git commands
3866 # 1: command or alias
3867 # 2: function to call (e.g. `git
`, `gitk
`, `git_fetch
`)
3872 if __git_have_func $2; then
3874 elif __git_have_func __$2_main; then
3876 elif __git_have_func _$2; then
3879 echo "ERROR: could not find function '$2'" 1>&2
3882 ___git_complete $1 $func
3885 ___git_complete git __git_main
3886 ___git_complete gitk __gitk_main
3888 # The following are necessary only for Cygwin, and only are needed
3889 # when the user has tab-completed the executable name and consequently
3890 # included the '.exe' suffix.
3892 if [ "$OSTYPE" = cygwin ]; then
3893 ___git_complete git.exe __git_main