60a22d619a85cc17334ef2e73185048120d3e51f
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 # To add completion for git subcommands that are implemented in external
35 # scripts, define a function of the form '_git_${subcommand}' while replacing
36 # all dashes with underscores, and the main git completion will make use of it.
37 # For example, to add completion for 'git do-stuff' (which could e.g. live
38 # in /usr/bin/git-do-stuff), name the completion function '_git_do_stuff'.
39 # See _git_show, _git_bisect etc. below for more examples.
41 # If you have a shell command that is not part of git (and is not called as a
42 # git subcommand), but you would still like git-style completion for it, use
43 # __git_complete. For example, to use the same completion as for 'git log' also
44 # for the 'gl' command:
46 # __git_complete gl git_log
48 # Or if the 'gk' command should be completed the same as 'gitk':
50 # __git_complete gk gitk
52 # The second parameter of __git_complete gives the completion function; it is
53 # resolved as a function named "$2", or "__$2_main", or "_$2" in that order.
54 # In the examples above, the actual functions used for completion will be
55 # _git_log and __gitk_main.
57 # Compatible with bash 3.2.57.
59 # You can set the following environment variables to influence the behavior of
60 # the completion routines:
62 # GIT_COMPLETION_CHECKOUT_NO_GUESS
64 # When set to "1", do not include "DWIM" suggestions in git-checkout
65 # and git-switch completion (e.g., completing "foo" when "origin/foo"
68 # GIT_COMPLETION_SHOW_ALL_COMMANDS
70 # When set to "1" suggest all commands, including plumbing commands
71 # which are hidden by default (e.g. "cat-file" on "git ca<TAB>").
73 # GIT_COMPLETION_SHOW_ALL
75 # When set to "1" suggest all options, including options which are
76 # typically hidden (e.g. '--allow-empty' for 'git commit').
78 # GIT_COMPLETION_IGNORE_CASE
80 # When set, uses for-each-ref '--ignore-case' to find refs that match
81 # case insensitively, even on systems with case sensitive file systems
82 # (e.g., completing tag name "FOO" on "git checkout f<TAB>").
84 case "$COMP_WORDBREAKS" in
86 *) COMP_WORDBREAKS
="$COMP_WORDBREAKS:"
89 # Discovers the path to the git repository taking any '--git-dir=<path>' and
90 # '-C <path>' options into account and stores it in the $__git_repo_path
92 __git_find_repo_path
()
94 if [ -n "${__git_repo_path-}" ]; then
95 # we already know where it is
99 if [ -n "${__git_C_args-}" ]; then
100 __git_repo_path
="$(git "${__git_C_args[@]}" \
101 ${__git_dir:+--git-dir="$__git_dir"} \
102 rev-parse --absolute-git-dir 2>/dev/null)"
103 elif [ -n "${__git_dir-}" ]; then
104 test -d "$__git_dir" &&
105 __git_repo_path
="$__git_dir"
106 elif [ -n "${GIT_DIR-}" ]; then
107 test -d "$GIT_DIR" &&
108 __git_repo_path
="$GIT_DIR"
109 elif [ -d .git
]; then
112 __git_repo_path
="$(git rev-parse --git-dir 2>/dev/null)"
116 # Deprecated: use __git_find_repo_path() and $__git_repo_path instead
117 # __gitdir accepts 0 or 1 arguments (i.e., location)
118 # returns location of .git repo
121 if [ -z "${1-}" ]; then
122 __git_find_repo_path ||
return 1
123 echo "$__git_repo_path"
124 elif [ -d "$1/.git" ]; then
131 # Runs git with all the options given as argument, respecting any
132 # '--git-dir=<path>' and '-C <path>' options present on the command line
135 git
${__git_C_args:+"${__git_C_args[@]}"} \
136 ${__git_dir:+--git-dir="$__git_dir"} "$@" 2>/dev
/null
139 # Helper function to read the first line of a file into a variable.
140 # __git_eread requires 2 arguments, the file path and the name of the
141 # variable, in that order.
143 # This is taken from git-prompt.sh.
146 test -r "$1" && IFS
=$
'\r\n' read -r "$2" <"$1"
149 # Runs git in $__git_repo_path to determine whether a pseudoref exists.
150 # 1: The pseudo-ref to search
151 __git_pseudoref_exists
()
158 # If the reftable is in use, we have to shell out to 'git rev-parse'
159 # to determine whether the ref exists instead of looking directly in
160 # the filesystem to determine whether the ref exists. Otherwise, use
161 # Bash builtins since executing Git commands are expensive on some
163 if __git_eread
"$__git_repo_path/HEAD" head; then
164 if [ "$head" == "ref: refs/heads/.invalid" ]; then
165 __git show-ref
--exists "$ref"
170 [ -f "$__git_repo_path/$ref" ]
173 # Removes backslash escaping, single quotes and double quotes from a word,
174 # stores the result in the variable $dequoted_word.
175 # 1: The word to dequote.
178 local rest
="$1" len ch
182 while test -n "$rest"; do
183 len
=${#dequoted_word}
184 dequoted_word
="$dequoted_word${rest%%[\\\'\"]*}"
185 rest
="${rest:$((${#dequoted_word}-$len))}"
187 case "${rest:0:1}" in
194 dequoted_word
="$dequoted_word$ch"
201 len
=${#dequoted_word}
202 dequoted_word
="$dequoted_word${rest%%\'*}"
203 rest
="${rest:$((${#dequoted_word}-$len+1))}"
207 while test -n "$rest" ; do
208 len
=${#dequoted_word}
209 dequoted_word
="$dequoted_word${rest%%[\\\"]*}"
210 rest
="${rest:$((${#dequoted_word}-$len))}"
211 case "${rest:0:1}" in
216 dequoted_word="$dequoted_word$ch"
221 dequoted_word="$dequoted_word\\$ch"
237 # The following function is based on code from:
239 # bash_completion - programmable completion functions for bash 3.2+
241 # Copyright © 2006-2008, Ian Macdonald <ian@caliban.org>
242 # © 2009-2010, Bash Completion Maintainers
243 # <bash-completion-devel@lists.alioth.debian.org>
245 # This program is free software; you can redistribute it and/or modify
246 # it under the terms of the GNU General Public License as published by
247 # the Free Software Foundation; either version 2, or (at your option)
250 # This program is distributed in the hope that it will be useful,
251 # but WITHOUT ANY WARRANTY; without even the implied warranty of
252 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
253 # GNU General Public License for more details.
255 # You should have received a copy of the GNU General Public License
256 # along with this program; if not, see <http://www.gnu.org/licenses/>.
258 # The latest version of this software can be obtained here:
260 # http://bash-completion.alioth.debian.org/
264 # This function can be used to access a tokenized list of words
265 # on the command line:
267 # __git_reassemble_comp_words_by_ref '=:'
268 # if test "${words_[cword_-1]}" = -w
273 # The argument should be a collection of characters from the list of
274 # word completion separators (COMP_WORDBREAKS) to treat as ordinary
277 # This is roughly equivalent to going back in time and setting
278 # COMP_WORDBREAKS to exclude those characters. The intent is to
279 # make option types like --date=<type> and <rev>:<path> easy to
280 # recognize by treating each shell word as a single token.
282 # It is best not to set COMP_WORDBREAKS directly because the value is
283 # shared with other completion scripts. By the time the completion
284 # function gets called, COMP_WORDS has already been populated so local
285 # changes to COMP_WORDBREAKS have no effect.
287 # Output: words_, cword_, cur_.
289 __git_reassemble_comp_words_by_ref()
291 local exclude i j first
292 # Which word separators to exclude?
293 exclude="${1//[^$COMP_WORDBREAKS]}"
295 if [ -z "$exclude" ]; then
296 words_=("${COMP_WORDS[@]}")
299 # List of word completion separators has shrunk;
300 # re-assemble words to complete.
301 for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
302 # Append each nonempty word consisting of just
303 # word separator characters to the current word.
307 [ -n "${COMP_WORDS[$i]}" ] &&
308 # word consists of excluded word separators
309 [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
311 # Attach to the previous token,
312 # unless the previous token is the command name.
313 if [ $j -ge 2 ] && [ -n "$first" ]; then
317 words_[$j]=${words_[j]}${COMP_WORDS[i]}
318 if [ $i = $COMP_CWORD ]; then
321 if (($i < ${#COMP_WORDS[@]} - 1)); then
328 words_[$j]=${words_[j]}${COMP_WORDS[i]}
329 if [ $i = $COMP_CWORD ]; then
335 if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
336 _get_comp_words_by_ref ()
338 local exclude cur_ words_ cword_
339 if [ "$1" = "-n" ]; then
343 __git_reassemble_comp_words_by_ref "$exclude"
344 cur_=${words_[cword_]}
345 while [ $# -gt 0 ]; do
351 prev=${words_[$cword_-1]}
354 words=("${words_[@]}")
365 # Fills the COMPREPLY array with prefiltered words without any additional
367 # Callers must take care of providing only words that match the current word
368 # to be completed and adding any prefix and/or suffix (trailing space!), if
370 # 1: List of newline-separated matching completion words, complete with
379 # Similar to __gitcomp_direct, but appends to COMPREPLY instead.
380 # Callers must take care of providing only words that match the current word
381 # to be completed and adding any prefix and/or suffix (trailing space!), if
383 # 1: List of newline-separated matching completion words, complete with
385 __gitcomp_direct_append ()
394 local x i=${#COMPREPLY[@]}
396 if [[ "$x" == "$3"* ]]; then
397 COMPREPLY[i++]="$2$x$4"
408 # Generates completion reply, appending a space to possible completion words,
410 # It accepts 1 to 4 arguments:
411 # 1: List of possible completion words.
412 # 2: A prefix to be added to each possible completion word (optional).
413 # 3: Generate possible completion matches for this word (optional).
414 # 4: A suffix to be appended to each possible completion word (optional).
417 local cur_="${3-$cur}"
423 local c i=0 IFS=$' \t\n'
425 if [[ $c == "--" ]]; then
429 if [[ $c == "$cur_"* ]]; then
434 COMPREPLY[i++]="${2-}$c"
439 local c i=0 IFS=$' \t\n'
441 if [[ $c == "--" ]]; then
443 if [[ $c == "$cur_"* ]]; then
444 COMPREPLY[i++]="${2-}$c "
449 if [[ $c == "$cur_"* ]]; then
454 COMPREPLY[i++]="${2-}$c"
461 # Clear the variables caching builtins' options when (re-)sourcing
462 # the completion script.
463 if [[ -n ${ZSH_VERSION-} ]]; then
464 unset ${(M)${(k)parameters[@]}:#__gitcomp_builtin_*} 2>/dev/null
466 unset $(compgen -v __gitcomp_builtin_)
469 # This function is equivalent to
471 # ___git_resolved_builtins=$(git xxx --git-completion-helper)
473 # except that the result of the execution is cached.
475 # Accept 1-3 arguments:
476 # 1: the git command to execute, this is also the cache key
477 # (use "_" when the command contains spaces, e.g. "remote add"
478 # becomes "remote_add")
479 # 2: extra options to be added on top (e.g. negative forms)
480 # 3: options to be excluded
481 __git_resolve_builtins ()
487 local var=__gitcomp_builtin_"${cmd//-/_}"
489 eval "options=\${$var-}"
491 if [ -z "$options" ]; then
492 local completion_helper
493 if [ "${GIT_COMPLETION_SHOW_ALL-}" = "1" ]; then
494 completion_helper="--git-completion-helper-all"
496 completion_helper="--git-completion-helper"
498 # leading and trailing spaces are significant to make
499 # option removal work correctly.
500 options=" $incl $(__git ${cmd/_/ } $completion_helper) " || return
503 options="${options/ $i / }"
505 eval "$var=\"$options\""
508 ___git_resolved_builtins="$options"
511 # This function is equivalent to
513 # __gitcomp "$(git xxx --git-completion-helper) ..."
515 # except that the output is cached. Accept 1-3 arguments:
516 # 1: the git command to execute, this is also the cache key
517 # (use "_" when the command contains spaces, e.g. "remote add"
518 # becomes "remote_add")
519 # 2: extra options to be added on top (e.g. negative forms)
520 # 3: options to be excluded
523 __git_resolve_builtins "$1" "$2" "$3"
525 __gitcomp "$___git_resolved_builtins"
528 # Variation of __gitcomp_nl () that appends to the existing list of
529 # completion candidates, COMPREPLY.
530 __gitcomp_nl_append ()
533 __gitcompappend "$1" "${2-}" "${3-$cur}" "${4- }"
536 # Generates completion reply from newline-separated possible completion words
537 # by appending a space to all of them.
538 # It accepts 1 to 4 arguments:
539 # 1: List of possible completion words, separated by a single newline.
540 # 2: A prefix to be added to each possible completion word (optional).
541 # 3: Generate possible completion matches for this word (optional).
542 # 4: A suffix to be appended to each possible completion word instead of
543 # the default space (optional). If specified but empty, nothing is
548 __gitcomp_nl_append "$@"
551 # Fills the COMPREPLY array with prefiltered paths without any additional
553 # Callers must take care of providing only paths that match the current path
554 # to be completed and adding any prefix path components, if necessary.
555 # 1: List of newline-separated matching paths, complete with all prefix
557 __gitcomp_file_direct ()
563 # use a hack to enable file mode in bash < 4
564 compopt -o filenames +o nospace 2>/dev/null ||
565 compgen -f /non-existing-dir/ >/dev/null ||
569 # Generates completion reply with compgen from newline-separated possible
570 # completion filenames.
571 # It accepts 1 to 3 arguments:
572 # 1: List of possible completion filenames, separated by a single newline.
573 # 2: A directory prefix to be added to each possible completion filename
575 # 3: Generate possible completion matches for this word (optional).
580 # XXX does not work when the directory prefix contains a tilde,
581 # since tilde expansion is not applied.
582 # This means that COMPREPLY will be empty and Bash default
583 # completion will be used.
584 __gitcompadd "$1" "${2-}" "${3-$cur}" ""
586 # use a hack to enable file mode in bash < 4
587 compopt -o filenames +o nospace 2>/dev/null ||
588 compgen -f /non-existing-dir/ >/dev/null ||
592 # Find the current subcommand for commands that follow the syntax:
594 # git <command> <subcommand>
596 # 1: List of possible subcommands.
597 # 2: Optional subcommand to return when none is found.
598 __git_find_subcommand ()
600 local subcommand subcommands="$1" default_subcommand="$2"
602 for subcommand in $subcommands; do
603 if [ "$subcommand" = "${words[__git_cmd_idx+1]}" ]; then
609 echo $default_subcommand
612 # Execute 'git ls-files', unless the --committable option is specified, in
613 # which case it runs 'git diff-index' to find out the files that can be
614 # committed. It return paths relative to the directory specified in the first
615 # argument, and using the options specified in the second argument.
616 __git_ls_files_helper ()
618 if [ "$2" = "--committable" ]; then
619 __git -C "$1" -c core.quotePath=false diff-index \
620 --name-only --relative HEAD -- "${3//\\/\\\\}*"
622 # NOTE: $2 is not quoted in order to support multiple options
623 __git -C "$1" -c core.quotePath=false ls-files \
624 --exclude-standard $2 -- "${3//\\/\\\\}*"
629 # __git_index_files accepts 1 or 2 arguments:
630 # 1: Options to pass to ls-files (required).
631 # 2: A directory path (optional).
632 # If provided, only files within the specified directory are listed.
633 # Sub directories are never recursed. Path must have a trailing
635 # 3: List only paths matching this path component (optional).
638 local root="$2" match="$3"
640 __git_ls_files_helper "$root" "$1" "${match:-?}" |
641 awk -F / -v pfx="${2//\\/\\\\}" '{
646 if (substr(p, 1, 1) != "\"") {
647 # No special characters, easy!
652 # The path is quoted.
657 # Even when a directory name itself does not contain
658 # any special characters, it will still be quoted if
659 # any of its (stripped) trailing path components do.
660 # Because of this we may have seen the same directory
661 # both quoted and unquoted.
663 # We have seen the same directory unquoted,
670 function dequote(p, bs_idx, out, esc, esc_idx, dec) {
671 # Skip opening double quote.
674 # Interpret backslash escape sequences.
675 while ((bs_idx = index(p, "\\")) != 0) {
676 out = out substr(p, 1, bs_idx - 1)
677 esc = substr(p, bs_idx + 1, 1)
678 p = substr(p, bs_idx + 2)
680 if ((esc_idx = index("abtvfr\"\\", esc)) != 0) {
681 # C-style one-character escape sequence.
682 out = out substr("\a\b\t\v\f\r\"\\",
684 } else if (esc == "n") {
685 # Uh-oh, a newline character.
686 # We cannot reliably put a pathname
687 # containing a newline into COMPREPLY,
688 # and the newline would create a mess.
692 # Must be a \nnn octal value, then.
694 substr(p, 1, 1) * 8 + \
696 out = out sprintf("%c", dec)
700 # Drop closing double quote, if there is one.
701 # (There is not any if this is a directory, as it was
702 # already stripped with the trailing path components.)
703 if (substr(p, length(p), 1) == "\"")
704 out = out substr(p, 1, length(p) - 1)
712 # __git_complete_index_file requires 1 argument:
713 # 1: the options to pass to ls-file
715 # The exception is --committable, which finds the files appropriate commit.
716 __git_complete_index_file ()
718 local dequoted_word pfx="" cur_
722 case "$dequoted_word" in
724 pfx="${dequoted_word%/*}/"
725 cur_="${dequoted_word##*/}"
728 cur_="$dequoted_word"
731 __gitcomp_file_direct "$(__git_index_files "$1" "$pfx" "$cur_")"
734 # Lists branches from the local repository.
735 # 1: A prefix to be added to each listed branch (optional).
736 # 2: List only branches matching this word (optional; list all branches if
738 # 3: A suffix to be appended to each listed branch (optional).
741 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
743 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
744 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
745 "refs/heads/$cur_*" "refs/heads/$cur_*/**"
748 # Lists branches from remote repositories.
749 # 1: A prefix to be added to each listed branch (optional).
750 # 2: List only branches matching this word (optional; list all branches if
752 # 3: A suffix to be appended to each listed branch (optional).
753 __git_remote_heads ()
755 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
757 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
758 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
759 "refs/remotes/$cur_*" "refs/remotes/$cur_*/**"
762 # Lists tags from the local repository.
763 # Accepts the same positional parameters as __git_heads() above.
766 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
768 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
769 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
770 "refs/tags/$cur_*" "refs/tags/$cur_*/**"
773 # List unique branches from refs/remotes used for 'git checkout' and 'git
774 # switch' tracking DWIMery.
775 # 1: A prefix to be added to each listed branch (optional)
776 # 2: List only branches matching this word (optional; list all branches if
778 # 3: A suffix to be appended to each listed branch (optional).
779 __git_dwim_remote_heads ()
781 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
782 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
784 # employ the heuristic used by git checkout and git switch
785 # Try to find a remote branch that cur_es the completion word
786 # but only output if the branch name is unique
787 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
788 --sort="refname:strip=3" \
789 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
790 "refs/remotes/*/$cur_*" "refs/remotes/*/$cur_*/**" | \
794 # Lists refs from the local (by default) or from a remote repository.
795 # It accepts 0, 1 or 2 arguments:
796 # 1: The remote to list refs from (optional; ignored, if set but empty).
797 # Can be the name of a configured remote, a path, or a URL.
798 # 2: In addition to local refs, list unique branches from refs/remotes/ for
799 # 'git checkout's tracking DWIMery (optional; ignored, if set but empty).
800 # 3: A prefix to be added to each listed ref (optional).
801 # 4: List only refs matching this word (optional; list all refs if unset or
803 # 5: A suffix to be appended to each listed ref (optional; ignored, if set
806 # Use __git_complete_refs() instead.
809 local i hash dir track="${2-}"
810 local list_refs_from=path remote="${1-}"
812 local pfx="${3-}" cur_="${4-$cur}" sfx="${5-}"
815 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
818 dir="$__git_repo_path"
820 if [ -z "$remote" ]; then
821 if [ -z "$dir" ]; then
825 if __git_is_configured_remote "$remote"; then
826 # configured remote takes precedence over a
827 # local directory with the same name
828 list_refs_from=remote
829 elif [ -d "$remote/.git" ]; then
831 elif [ -d "$remote" ]; then
838 if test "${GIT_COMPLETION_IGNORE_CASE:+1}" = "1"
840 # uppercase with tr instead of ${match,^^} for bash 3.2 compatibility
841 umatch=$(echo "$match" | tr a-z A-Z 2>/dev/null || echo "$match")
844 if [ "$list_refs_from" = path ]; then
845 if [[ "$cur_" == ^* ]]; then
855 refs=("$match*" "$match*/**")
859 for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD REBASE_HEAD CHERRY_PICK_HEAD REVERT_HEAD BISECT_HEAD AUTO_MERGE; do
862 if [ -e "$dir/$i" ]; then
868 format="refname:strip=2"
869 refs=("refs/tags/$match*" "refs/tags/$match*/**"
870 "refs/heads/$match*" "refs/heads/$match*/**"
871 "refs/remotes/$match*" "refs/remotes/$match*/**")
874 __git_dir="$dir" __git for-each-ref --format="$fer_pfx%($format)$sfx" \
875 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
877 if [ -n "$track" ]; then
878 __git_dwim_remote_heads "$pfx" "$match" "$sfx"
884 __git ls-remote "$remote" "$match*" | \
885 while read -r hash i; do
888 *) echo "$pfx$i$sfx" ;;
893 if [ "$list_refs_from" = remote ]; then
895 $match*|$umatch*) echo "${pfx}HEAD$sfx" ;;
897 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
898 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
899 "refs/remotes/$remote/$match*" \
900 "refs/remotes/$remote/$match*/**"
904 $match*|$umatch*) query_symref="HEAD" ;;
906 __git ls-remote "$remote" $query_symref \
907 "refs/tags/$match*" "refs/heads/$match*" \
908 "refs/remotes/$match*" |
909 while read -r hash i; do
912 refs/*) echo "$pfx${i#refs/*/}$sfx" ;;
913 *) echo "$pfx$i$sfx" ;; # symbolic refs
921 # Completes refs, short and long, local and remote, symbolic and pseudo.
923 # Usage: __git_complete_refs [<option>]...
924 # --remote=<remote>: The remote to list refs from, can be the name of a
925 # configured remote, a path, or a URL.
926 # --dwim: List unique remote branches for 'git switch's tracking DWIMery.
927 # --pfx=<prefix>: A prefix to be added to each ref.
928 # --cur=<word>: The current ref to be completed. Defaults to the current
929 # word to be completed.
930 # --sfx=<suffix>: A suffix to be appended to each ref instead of the default
932 # --mode=<mode>: What set of refs to complete, one of 'refs' (the default) to
933 # complete all refs, 'heads' to complete only branches, or
934 # 'remote-heads' to complete only remote branches. Note that
935 # --remote is only compatible with --mode=refs.
936 __git_complete_refs ()
938 local remote= dwim= pfx= cur_="$cur" sfx=" " mode="refs"
940 while test $# != 0; do
942 --remote=*) remote="${1##--remote=}" ;;
943 --dwim) dwim="yes" ;;
944 # --track is an old spelling of --dwim
945 --track) dwim="yes" ;;
946 --pfx=*) pfx="${1##--pfx=}" ;;
947 --cur=*) cur_="${1##--cur=}" ;;
948 --sfx=*) sfx="${1##--sfx=}" ;;
949 --mode=*) mode="${1##--mode=}" ;;
955 # complete references based on the specified mode
958 __gitcomp_direct "$(__git_refs "$remote" "" "$pfx" "$cur_" "$sfx")" ;;
960 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" "$sfx")" ;;
962 __gitcomp_direct "$(__git_remote_heads "$pfx" "$cur_" "$sfx")" ;;
967 # Append DWIM remote branch names if requested
968 if [ "$dwim" = "yes" ]; then
969 __gitcomp_direct_append "$(__git_dwim_remote_heads "$pfx" "$cur_" "$sfx")"
973 # __git_refs2 requires 1 argument (to pass to __git_refs)
974 # Deprecated: use __git_complete_fetch_refspecs() instead.
978 for i in $(__git_refs "$1"); do
983 # Completes refspecs for fetching from a remote repository.
984 # 1: The remote repository.
985 # 2: A prefix to be added to each listed refspec (optional).
986 # 3: The ref to be completed as a refspec instead of the current word to be
987 # completed (optional)
988 # 4: A suffix to be appended to each listed refspec instead of the default
990 __git_complete_fetch_refspecs ()
992 local i remote="$1" pfx="${2-}" cur_="${3-$cur}" sfx="${4- }"
995 for i in $(__git_refs "$remote" "" "" "$cur_") ; do
1001 # __git_refs_remotes requires 1 argument (to pass to ls-remote)
1002 __git_refs_remotes ()
1005 __git ls-remote "$1" 'refs/heads/*' | \
1006 while read -r hash i; do
1007 echo "$i:refs/remotes/$1/${i#refs/heads/}"
1013 __git_find_repo_path
1014 test -d "$__git_repo_path/remotes" && ls -1 "$__git_repo_path/remotes"
1018 # Returns true if $1 matches the name of a configured remote, false otherwise.
1019 __git_is_configured_remote ()
1022 for remote in $(__git_remotes); do
1023 if [ "$remote" = "$1" ]; then
1030 __git_list_merge_strategies ()
1032 LANG=C LC_ALL=C git merge -s help 2>&1 |
1033 sed -n -e '/[Aa]vailable strategies are: /,/^$/{
1042 __git_merge_strategies=
1043 # 'git merge -s help' (and thus detection of the merge strategy
1044 # list) fails, unfortunately, if run outside of any git working
1045 # tree. __git_merge_strategies is set to the empty string in
1046 # that case, and the detection will be repeated the next time it
1048 __git_compute_merge_strategies ()
1050 test -n "$__git_merge_strategies" ||
1051 __git_merge_strategies=$(__git_list_merge_strategies)
1054 __git_merge_strategy_options="ours theirs subtree subtree= patience
1055 histogram diff-algorithm= ignore-space-change ignore-all-space
1056 ignore-space-at-eol renormalize no-renormalize no-renames
1057 find-renames find-renames= rename-threshold="
1059 __git_complete_revlist_file ()
1061 local dequoted_word pfx ls ref cur_="$cur"
1070 __git_dequote "$cur_"
1072 case "$dequoted_word" in
1074 pfx="${dequoted_word%/*}"
1075 cur_="${dequoted_word##*/}"
1080 cur_="$dequoted_word"
1085 case "$COMP_WORDBREAKS" in
1087 *) pfx="$ref:$pfx" ;;
1090 __gitcomp_file "$(__git ls-tree "$ls" \
1096 pfx="${cur_%...*}..."
1098 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1103 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1111 __git_complete_file ()
1113 __git_complete_revlist_file
1116 __git_complete_revlist ()
1118 __git_complete_revlist_file
1121 __git_complete_remote_or_refspec ()
1123 local cur_="$cur" cmd="${words[__git_cmd_idx]}"
1124 local i c=$((__git_cmd_idx+1)) remote="" pfx="" lhs=1 no_complete_refspec=0
1125 if [ "$cmd" = "remote" ]; then
1128 while [ $c -lt $cword ]; do
1131 --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
1132 -d|--delete) [ "$cmd" = "push" ] && lhs=0 ;;
1135 push) no_complete_refspec=1 ;;
1142 --multiple) no_complete_refspec=1; break ;;
1144 *) remote="$i"; break ;;
1148 if [ -z "$remote" ]; then
1149 __gitcomp_nl "$(__git_remotes)"
1152 if [ $no_complete_refspec = 1 ]; then
1155 [ "$remote" = "." ] && remote=
1158 case "$COMP_WORDBREAKS" in
1160 *) pfx="${cur_%%:*}:" ;;
1172 if [ $lhs = 1 ]; then
1173 __git_complete_fetch_refspecs "$remote" "$pfx" "$cur_"
1175 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1179 if [ $lhs = 1 ]; then
1180 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
1182 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1186 if [ $lhs = 1 ]; then
1187 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1189 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
1195 __git_complete_strategy ()
1197 __git_compute_merge_strategies
1200 __gitcomp "$__git_merge_strategies"
1204 __gitcomp "$__git_merge_strategy_options"
1210 __gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
1213 --strategy-option=*)
1214 __gitcomp "$__git_merge_strategy_options" "" "${cur##--strategy-option=}"
1222 __git_compute_all_commands ()
1224 test -n "$__git_all_commands" ||
1225 __git_all_commands=$(__git --list-cmds=main,others,alias,nohelpers)
1228 # Lists all set config variables starting with the given section prefix,
1229 # with the prefix removed.
1230 __git_get_config_variables ()
1232 local section="$1" i IFS=$'\n'
1233 for i in $(__git config --name-only --get-regexp "^$section\..*"); do
1234 echo "${i#$section.}"
1238 __git_pretty_aliases ()
1240 __git_get_config_variables "pretty"
1243 # __git_aliased_command requires 1 argument
1244 __git_aliased_command ()
1246 local cur=$1 last list= word cmdline
1248 while [[ -n "$cur" ]]; do
1249 if [[ "$list" == *" $cur "* ]]; then
1254 cmdline=$(__git config --get "alias.$cur")
1259 for word in $cmdline; do
1265 \!*) : shell command alias ;;
1267 *=*) : setting env ;;
1268 git) : git itself ;;
1269 \(\)) : skip parens of shell function definition ;;
1270 {) : skip start of shell helper function ;;
1271 :) : skip null command ;;
1272 \'*) : skip opening quote after sh -c ;;
1281 if [[ "$cur" != "$1" ]]; then
1286 # Check whether one of the given words is present on the command line,
1287 # and print the first word found.
1289 # Usage: __git_find_on_cmdline [<option>]... "<wordlist>"
1290 # --show-idx: Optionally show the index of the found word in the $words array.
1291 __git_find_on_cmdline ()
1293 local word c="$__git_cmd_idx" show_idx
1295 while test $# -gt 1; do
1297 --show-idx) show_idx=y ;;
1304 while [ $c -lt $cword ]; do
1305 for word in $wordlist; do
1306 if [ "$word" = "${words[c]}" ]; then
1307 if [ -n "${show_idx-}" ]; then
1319 # Similar to __git_find_on_cmdline, except that it loops backwards and thus
1320 # prints the *last* word found. Useful for finding which of two options that
1321 # supersede each other came last, such as "--guess" and "--no-guess".
1323 # Usage: __git_find_last_on_cmdline [<option>]... "<wordlist>"
1324 # --show-idx: Optionally show the index of the found word in the $words array.
1325 __git_find_last_on_cmdline ()
1327 local word c=$cword show_idx
1329 while test $# -gt 1; do
1331 --show-idx) show_idx=y ;;
1338 while [ $c -gt "$__git_cmd_idx" ]; do
1340 for word in $wordlist; do
1341 if [ "$word" = "${words[c]}" ]; then
1342 if [ -n "$show_idx" ]; then
1353 # Echo the value of an option set on the command line or config
1355 # $1: short option name
1356 # $2: long option name including =
1357 # $3: list of possible values
1358 # $4: config string (optional)
1361 # result="$(__git_get_option_value "-d" "--do-something=" \
1362 # "yes no" "core.doSomething")"
1364 # result is then either empty (no option set) or "yes" or "no"
1366 # __git_get_option_value requires 3 arguments
1367 __git_get_option_value ()
1369 local c short_opt long_opt val
1370 local result= values config_key word
1378 while [ $c -ge 0 ]; do
1380 for val in $values; do
1381 if [ "$short_opt$val" = "$word" ] ||
1382 [ "$long_opt$val" = "$word" ]; then
1390 if [ -n "$config_key" ] && [ -z "$result" ]; then
1391 result="$(__git config "$config_key")"
1397 __git_has_doubledash ()
1400 while [ $c -lt $cword ]; do
1401 if [ "--" = "${words[c]}" ]; then
1409 # Try to count non option arguments passed on the command line for the
1410 # specified git command.
1411 # When options are used, it is necessary to use the special -- option to
1412 # tell the implementation were non option arguments begin.
1413 # XXX this can not be improved, since options can appear everywhere, as
1417 # __git_count_arguments requires 1 argument: the git command executed.
1418 __git_count_arguments ()
1422 # Skip "git" (first argument)
1423 for ((i=$__git_cmd_idx; i < ${#words[@]}; i++)); do
1428 # Good; we can assume that the following are only non
1433 # Skip the specified git command and discard git
1446 __git_whitespacelist="nowarn warn error error-all fix"
1447 __git_patchformat="mbox stgit stgit-series hg mboxrd"
1448 __git_showcurrentpatch="diff raw"
1449 __git_am_inprogress_options="--skip --continue --resolved --abort --quit --show-current-patch"
1450 __git_quoted_cr="nowarn warn strip"
1454 __git_find_repo_path
1455 if [ -d "$__git_repo_path"/rebase-apply ]; then
1456 __gitcomp "$__git_am_inprogress_options"
1461 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1465 __gitcomp "$__git_patchformat" "" "${cur##--patch-format=}"
1468 --show-current-patch=*)
1469 __gitcomp "$__git_showcurrentpatch" "" "${cur##--show-current-patch=}"
1473 __gitcomp "$__git_quoted_cr" "" "${cur##--quoted-cr=}"
1477 __gitcomp_builtin am "" \
1478 "$__git_am_inprogress_options"
1487 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1491 __gitcomp_builtin apply
1500 __gitcomp "+x -x" "" "${cur##--chmod=}"
1504 __gitcomp_builtin add
1508 local complete_opt="--others --modified --directory --no-empty-directory"
1509 if test -n "$(__git_find_on_cmdline "-u --update")"
1511 complete_opt="--modified"
1513 __git_complete_index_file "$complete_opt"
1520 __gitcomp "$(git archive --list)" "" "${cur##--format=}"
1524 __gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
1528 __gitcomp_builtin archive "--format= --list --verbose --prefix= --worktree-attributes"
1537 __git_has_doubledash && return
1539 __git_find_repo_path
1541 # If a bisection is in progress get the terms being used.
1542 local term_bad term_good
1543 if [ -f "$__git_repo_path"/BISECT_TERMS ]; then
1544 term_bad=$(__git bisect terms --term-bad)
1545 term_good=$(__git bisect terms --term-good)
1548 # We will complete any custom terms, but still always complete the
1549 # more usual bad/new/good/old because git bisect gives a good error
1550 # message if these are given when not in use, and that's better than
1551 # silent refusal to complete if the user is confused.
1553 # We want to recognize 'view' but not complete it, because it overlaps
1554 # with 'visualize' too much and is just an alias for it.
1556 local completable_subcommands="start bad new $term_bad good old $term_good terms skip reset visualize replay log run help"
1557 local all_subcommands="$completable_subcommands view"
1559 local subcommand="$(__git_find_on_cmdline "$all_subcommands")"
1561 if [ -z "$subcommand" ]; then
1562 __git_find_repo_path
1563 if [ -f "$__git_repo_path"/BISECT_START ]; then
1564 __gitcomp "$completable_subcommands"
1566 __gitcomp "replay start"
1571 case "$subcommand" in
1575 __gitcomp "--first-parent --no-checkout --term-new --term-bad --term-old --term-good"
1584 __gitcomp "--term-good --term-old --term-bad --term-new"
1588 __git_complete_log_opts
1591 bad|new|"$term_bad"|good|old|"$term_good"|reset|skip)
1599 __git_ref_fieldlist="refname objecttype objectsize objectname upstream push HEAD symref"
1603 local i c="$__git_cmd_idx" only_local_ref="n" has_r="n"
1605 while [ $c -lt $cword ]; do
1608 -d|-D|--delete|-m|-M|--move|-c|-C|--copy)
1609 only_local_ref="y" ;;
1617 --set-upstream-to=*)
1618 __git_complete_refs --cur="${cur##--set-upstream-to=}"
1621 __gitcomp_builtin branch
1624 if [ $only_local_ref = "y" -a $has_r = "n" ]; then
1625 __gitcomp_direct "$(__git_heads "" "$cur" " ")"
1635 local cmd="${words[__git_cmd_idx+1]}"
1637 $((__git_cmd_idx+1)))
1638 __gitcomp "create list-heads verify unbundle"
1640 $((__git_cmd_idx+2)))
1641 # looking for a file
1646 __git_complete_revlist
1653 # Helper function to decide whether or not we should enable DWIM logic for
1654 # git-switch and git-checkout.
1656 # To decide between the following rules in decreasing priority order:
1657 # - the last provided of "--guess" or "--no-guess" explicitly enable or
1658 # disable completion of DWIM logic respectively.
1659 # - If checkout.guess is false, disable completion of DWIM logic.
1660 # - If the --no-track option is provided, take this as a hint to disable the
1661 # DWIM completion logic
1662 # - If GIT_COMPLETION_CHECKOUT_NO_GUESS is set, disable the DWIM completion
1663 # logic, as requested by the user.
1664 # - Enable DWIM logic otherwise.
1666 __git_checkout_default_dwim_mode ()
1668 local last_option dwim_opt="--dwim"
1670 if [ "${GIT_COMPLETION_CHECKOUT_NO_GUESS-}" = "1" ]; then
1674 # --no-track disables DWIM, but with lower priority than
1675 # --guess/--no-guess/checkout.guess
1676 if [ -n "$(__git_find_on_cmdline "--no-track")" ]; then
1680 # checkout.guess = false disables DWIM, but with lower priority than
1681 # --guess/--no-guess
1682 if [ "$(__git config --type=bool checkout.guess)" = "false" ]; then
1686 # Find the last provided --guess or --no-guess
1687 last_option="$(__git_find_last_on_cmdline "--guess --no-guess")"
1688 case "$last_option" in
1702 __git_has_doubledash && return
1704 local dwim_opt="$(__git_checkout_default_dwim_mode)"
1708 # Complete local branches (and DWIM branch
1709 # remote branch names) for an option argument
1710 # specifying a new branch name. This is for
1711 # convenience, assuming new branches are
1712 # possibly based on pre-existing branch names.
1713 __git_complete_refs $dwim_opt --mode="heads"
1722 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
1725 __gitcomp_builtin checkout
1728 # At this point, we've already handled special completion for
1729 # the arguments to -b/-B, and --orphan. There are 3 main
1730 # things left we can possibly complete:
1731 # 1) a start-point for -b/-B, -d/--detach, or --orphan
1732 # 2) a remote head, for --track
1733 # 3) an arbitrary reference, possibly including DWIM names
1736 if [ -n "$(__git_find_on_cmdline "-b -B -d --detach --orphan")" ]; then
1737 __git_complete_refs --mode="refs"
1738 elif [ -n "$(__git_find_on_cmdline "-t --track")" ]; then
1739 __git_complete_refs --mode="remote-heads"
1741 __git_complete_refs $dwim_opt --mode="refs"
1747 __git_sequencer_inprogress_options="--continue --quit --abort --skip"
1749 __git_cherry_pick_inprogress_options=$__git_sequencer_inprogress_options
1753 if __git_pseudoref_exists CHERRY_PICK_HEAD; then
1754 __gitcomp "$__git_cherry_pick_inprogress_options"
1758 __git_complete_strategy && return
1762 __gitcomp_builtin cherry-pick "" \
1763 "$__git_cherry_pick_inprogress_options"
1775 __gitcomp_builtin clean
1780 # XXX should we check for -x option ?
1781 __git_complete_index_file "--others --directory"
1788 __git_complete_config_variable_name_and_value
1794 __git_complete_config_variable_name_and_value \
1795 --cur="${cur##--config=}"
1799 __gitcomp_builtin clone
1805 __git_untracked_file_modes="all no normal"
1807 __git_trailer_tokens ()
1809 __git config --name-only --get-regexp '^trailer\..*\.key$' | cut -d. -f 2- | rev | cut -d. -f2- | rev
1823 __gitcomp "default scissors strip verbatim whitespace
1824 " "" "${cur##--cleanup=}"
1827 --reuse-message=*|--reedit-message=*|\
1828 --fixup=*|--squash=*)
1829 __git_complete_refs --cur="${cur#*=}"
1832 --untracked-files=*)
1833 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
1837 __gitcomp_nl "$(__git_trailer_tokens)" "" "${cur##--trailer=}" ":"
1841 __gitcomp_builtin commit
1845 if __git rev-parse --verify --quiet HEAD >/dev/null; then
1846 __git_complete_index_file "--committable"
1848 # This is the first commit
1849 __git_complete_index_file "--cached"
1857 __gitcomp_builtin describe
1863 __git_diff_algorithms="myers minimal patience histogram"
1865 __git_diff_submodule_formats="diff log short"
1867 __git_color_moved_opts="no default plain blocks zebra dimmed-zebra"
1869 __git_color_moved_ws_opts="no ignore-space-at-eol ignore-space-change
1870 ignore-all-space allow-indentation-change"
1872 __git_ws_error_highlight_opts="context old new all default"
1874 # Options for the diff machinery (diff, log, show, stash, range-diff, ...)
1875 __git_diff_common_options="--stat --numstat --shortstat --summary
1876 --patch-with-stat --name-only --name-status --color
1877 --no-color --color-words --no-renames --check
1878 --color-moved --color-moved= --no-color-moved
1879 --color-moved-ws= --no-color-moved-ws
1880 --full-index --binary --abbrev --diff-filter=
1881 --find-copies --find-object --find-renames
1882 --no-relative --relative
1883 --find-copies-harder --ignore-cr-at-eol
1884 --text --ignore-space-at-eol --ignore-space-change
1885 --ignore-all-space --ignore-blank-lines --exit-code
1886 --quiet --ext-diff --no-ext-diff --unified=
1887 --no-prefix --src-prefix= --dst-prefix=
1888 --inter-hunk-context= --function-context
1889 --patience --histogram --minimal
1890 --raw --word-diff --word-diff-regex=
1891 --dirstat --dirstat= --dirstat-by-file
1892 --dirstat-by-file= --cumulative
1893 --diff-algorithm= --default-prefix
1894 --submodule --submodule= --ignore-submodules
1895 --indent-heuristic --no-indent-heuristic
1896 --textconv --no-textconv --break-rewrites
1897 --patch --no-patch --cc --combined-all-paths
1898 --anchored= --compact-summary --ignore-matching-lines=
1899 --irreversible-delete --line-prefix --no-stat
1900 --output= --output-indicator-context=
1901 --output-indicator-new= --output-indicator-old=
1902 --ws-error-highlight=
1903 --pickaxe-all --pickaxe-regex --patch-with-raw
1906 # Options for diff/difftool
1907 __git_diff_difftool_options="--cached --staged
1908 --base --ours --theirs --no-index --merge-base
1909 --ita-invisible-in-index --ita-visible-in-index
1910 $__git_diff_common_options"
1914 __git_has_doubledash && return
1918 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1922 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1926 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
1930 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
1933 --ws-error-highlight=*)
1934 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
1938 __gitcomp "$__git_diff_difftool_options"
1942 __git_complete_revlist_file
1945 __git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff
1946 tkdiff vimdiff nvimdiff gvimdiff xxdiff araxis p4merge
1947 bc codecompare smerge
1952 __git_has_doubledash && return
1956 __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1960 __gitcomp_builtin difftool "$__git_diff_difftool_options"
1964 __git_complete_revlist_file
1967 __git_fetch_recurse_submodules="yes on-demand no"
1972 --recurse-submodules=*)
1973 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1977 __gitcomp "blob:none blob:limit= sparse:oid=" "" "${cur##--filter=}"
1981 __gitcomp_builtin fetch
1985 __git_complete_remote_or_refspec
1988 __git_format_patch_extra_options="
1989 --full-index --not --all --no-prefix --src-prefix=
1990 --dst-prefix= --notes
1993 _git_format_patch ()
1999 " "" "${cur##--thread=}"
2002 --base=*|--interdiff=*|--range-diff=*)
2003 __git_complete_refs --cur="${cur#--*=}"
2007 __gitcomp_builtin format-patch "$__git_format_patch_extra_options"
2011 __git_complete_revlist
2018 __gitcomp_builtin fsck
2029 # Lists matching symbol names from a tag (as in ctags) file.
2030 # 1: List symbol names matching this word.
2031 # 2: The tag file to list symbol names from.
2032 # 3: A prefix to be added to each listed symbol name (optional).
2033 # 4: A suffix to be appended to each listed symbol name (optional).
2034 __git_match_ctag () {
2035 awk -v pfx="${3-}" -v sfx="${4-}" "
2036 /^${1//\//\\/}/ { print pfx \$1 sfx }
2040 # Complete symbol names from a tag file.
2041 # Usage: __git_complete_symbol [<option>]...
2042 # --tags=<file>: The tag file to list symbol names from instead of the
2044 # --pfx=<prefix>: A prefix to be added to each symbol name.
2045 # --cur=<word>: The current symbol name to be completed. Defaults to
2046 # the current word to be completed.
2047 # --sfx=<suffix>: A suffix to be appended to each symbol name instead
2048 # of the default space.
2049 __git_complete_symbol () {
2050 local tags=tags pfx="" cur_="${cur-}" sfx=" "
2052 while test $# != 0; do
2054 --tags=*) tags="${1##--tags=}" ;;
2055 --pfx=*) pfx="${1##--pfx=}" ;;
2056 --cur=*) cur_="${1##--cur=}" ;;
2057 --sfx=*) sfx="${1##--sfx=}" ;;
2063 if test -r "$tags"; then
2064 __gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
2070 __git_has_doubledash && return
2074 __gitcomp_builtin grep
2079 case "$cword,$prev" in
2080 $((__git_cmd_idx+1)),*|*,-*)
2081 __git_complete_symbol && return
2092 __gitcomp_builtin help
2096 if test -n "${GIT_TESTING_ALL_COMMAND_LIST-}"
2098 __gitcomp "$GIT_TESTING_ALL_COMMAND_LIST $(__git --list-cmds=alias,list-guide) gitk"
2100 __gitcomp "$(__git --list-cmds=main,nohelpers,alias,list-guide) gitk"
2109 false true umask group all world everybody
2110 " "" "${cur##--shared=}"
2114 __gitcomp_builtin init
2124 __gitcomp_builtin ls-files
2129 # XXX ignore options like --modified and always suggest all cached
2131 __git_complete_index_file "--cached"
2138 __gitcomp_builtin ls-remote
2142 __gitcomp_nl "$(__git_remotes)"
2149 __gitcomp_builtin ls-tree
2157 # Options that go well for log, shortlog and gitk
2158 __git_log_common_options="
2160 --branches --tags --remotes
2161 --first-parent --merges --no-merges
2163 --max-age= --since= --after=
2164 --min-age= --until= --before=
2165 --min-parents= --max-parents=
2166 --no-min-parents --no-max-parents
2167 --alternate-refs --ancestry-path
2168 --author-date-order --basic-regexp
2169 --bisect --boundary --exclude-first-parent-only
2170 --exclude-hidden --extended-regexp
2171 --fixed-strings --grep-reflog
2172 --ignore-missing --left-only --perl-regexp
2173 --reflog --regexp-ignore-case --remove-empty
2174 --right-only --show-linear-break
2175 --show-notes-by-default --show-pulls
2176 --since-as-filter --single-worktree
2178 # Options that go well for log and gitk (not shortlog)
2179 __git_log_gitk_options="
2180 --dense --sparse --full-history
2181 --simplify-merges --simplify-by-decoration
2182 --left-right --notes --no-notes
2184 # Options that go well for log and shortlog (not gitk)
2185 __git_log_shortlog_options="
2186 --author= --committer= --grep=
2187 --all-match --invert-grep
2189 # Options accepted by log and show
2190 __git_log_show_options="
2191 --diff-merges --diff-merges= --no-diff-merges --dd --remerge-diff
2195 __git_diff_merges_opts="off none on first-parent 1 separate m combined c dense-combined cc remerge r"
2197 __git_log_pretty_formats="oneline short medium full fuller reference email raw format: tformat: mboxrd"
2198 __git_log_date_formats="relative iso8601 iso8601-strict rfc2822 short local default human raw unix auto: format:"
2200 # Complete porcelain (i.e. not git-rev-list) options and at least some
2201 # option arguments accepted by git-log. Note that this same set of options
2202 # are also accepted by some other git commands besides git-log.
2203 __git_complete_log_opts ()
2208 if __git_pseudoref_exists MERGE_HEAD; then
2211 case "$prev,$cur" in
2213 return # fall back to Bash filename completion
2216 __git_complete_symbol --cur="${cur#:}" --sfx=":"
2220 __git_complete_symbol
2225 --pretty=*|--format=*)
2226 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2231 __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
2235 __gitcomp "full short no" "" "${cur##--decorate=}"
2239 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
2243 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
2246 --ws-error-highlight=*)
2247 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
2251 __gitcomp "sorted unsorted" "" "${cur##--no-walk=}"
2255 __gitcomp "$__git_diff_merges_opts" "" "${cur##--diff-merges=}"
2260 $__git_log_common_options
2261 $__git_log_shortlog_options
2262 $__git_log_gitk_options
2263 $__git_log_show_options
2264 --root --topo-order --date-order --reverse
2265 --follow --full-diff
2266 --abbrev-commit --no-abbrev-commit --abbrev=
2267 --relative-date --date=
2268 --pretty= --format= --oneline
2273 --decorate --decorate= --no-decorate
2275 --no-walk --no-walk= --do-walk
2276 --parents --children
2277 --expand-tabs --expand-tabs= --no-expand-tabs
2278 --clear-decorations --decorate-refs=
2279 --decorate-refs-exclude=
2281 $__git_diff_common_options
2286 return # fall back to Bash filename completion
2289 __git_complete_symbol --cur="${cur#-L:}" --sfx=":"
2293 __git_complete_symbol --pfx="-G" --cur="${cur#-G}"
2297 __git_complete_symbol --pfx="-S" --cur="${cur#-S}"
2305 __git_has_doubledash && return
2306 __git_find_repo_path
2308 __git_complete_log_opts
2309 [ ${#COMPREPLY[@]} -eq 0 ] || return
2311 __git_complete_revlist
2316 __git_complete_strategy && return
2320 __gitcomp_builtin merge
2330 __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
2334 __gitcomp "--tool= --prompt --no-prompt --gui --no-gui"
2344 __gitcomp_builtin merge-base
2355 __gitcomp_builtin mv
2360 if [ $(__git_count_arguments "mv") -gt 0 ]; then
2361 # We need to show both cached and untracked files (including
2362 # empty directories) since this may not be the last argument.
2363 __git_complete_index_file "--cached --others --directory"
2365 __git_complete_index_file "--cached"
2371 local subcommands='add append copy edit get-ref list merge prune remove show'
2372 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2374 case "$subcommand,$cur" in
2376 __gitcomp_builtin notes
2384 __gitcomp "$subcommands --ref"
2388 *,--reuse-message=*|*,--reedit-message=*)
2389 __git_complete_refs --cur="${cur#*=}"
2392 __gitcomp_builtin notes_$subcommand
2395 # this command does not take a ref, do not complete it
2411 __git_complete_strategy && return
2414 --recurse-submodules=*)
2415 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
2419 __gitcomp_builtin pull
2424 __git_complete_remote_or_refspec
2427 __git_push_recurse_submodules="check on-demand only"
2429 __git_complete_force_with_lease ()
2437 __git_complete_refs --cur="${cur_#*:}"
2440 __git_complete_refs --cur="$cur_"
2449 __gitcomp_nl "$(__git_remotes)"
2452 --recurse-submodules)
2453 __gitcomp "$__git_push_recurse_submodules"
2459 __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
2462 --recurse-submodules=*)
2463 __gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
2466 --force-with-lease=*)
2467 __git_complete_force_with_lease "${cur##--force-with-lease=}"
2471 __gitcomp_builtin push
2475 __git_complete_remote_or_refspec
2483 --creation-factor= --no-dual-color
2484 $__git_diff_common_options
2489 __git_complete_revlist
2492 __git_rebase_inprogress_options="--continue --skip --abort --quit --show-current-patch"
2493 __git_rebase_interactive_inprogress_options="$__git_rebase_inprogress_options --edit-todo"
2497 __git_find_repo_path
2498 if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
2499 __gitcomp "$__git_rebase_interactive_inprogress_options"
2501 elif [ -d "$__git_repo_path"/rebase-apply ] || \
2502 [ -d "$__git_repo_path"/rebase-merge ]; then
2503 __gitcomp "$__git_rebase_inprogress_options"
2506 __git_complete_strategy && return
2509 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
2513 __git_complete_refs --cur="${cur##--onto=}"
2517 __gitcomp_builtin rebase "" \
2518 "$__git_rebase_interactive_inprogress_options"
2527 local subcommands subcommand
2529 __git_resolve_builtins "reflog"
2531 subcommands="$___git_resolved_builtins"
2532 subcommand="$(__git_find_subcommand "$subcommands" "show")"
2534 case "$subcommand,$cur" in
2537 $__git_log_common_options
2542 __gitcomp_builtin "reflog_$subcommand"
2549 if [ $((cword - __git_cmd_idx)) -eq 1 ]; then
2550 __gitcompappend "$subcommands" "" "$cur" " "
2554 __git_send_email_confirm_options="always never auto cc compose"
2555 __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
2560 --to|--cc|--bcc|--from)
2561 __gitcomp "$(__git send-email --dump-aliases)"
2569 $__git_send_email_confirm_options
2570 " "" "${cur##--confirm=}"
2575 $__git_send_email_suppresscc_options
2576 " "" "${cur##--suppress-cc=}"
2580 --smtp-encryption=*)
2581 __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
2587 " "" "${cur##--thread=}"
2590 --to=*|--cc=*|--bcc=*|--from=*)
2591 __gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
2595 __gitcomp_builtin send-email "$__git_format_patch_extra_options"
2599 __git_complete_revlist
2610 local untracked_state
2613 --ignore-submodules=*)
2614 __gitcomp "none untracked dirty all" "" "${cur##--ignore-submodules=}"
2617 --untracked-files=*)
2618 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
2623 always never auto column row plain dense nodense
2624 " "" "${cur##--column=}"
2628 __gitcomp_builtin status
2633 untracked_state="$(__git_get_option_value "-u" "--untracked-files=" \
2634 "$__git_untracked_file_modes" "status.showUntrackedFiles")"
2636 case "$untracked_state" in
2638 # --ignored option does not matter
2642 complete_opt="--cached --directory --no-empty-directory --others"
2644 if [ -n "$(__git_find_on_cmdline "--ignored")" ]; then
2645 complete_opt="$complete_opt --ignored --exclude=*"
2650 __git_complete_index_file "$complete_opt"
2655 local dwim_opt="$(__git_checkout_default_dwim_mode)"
2659 # Complete local branches (and DWIM branch
2660 # remote branch names) for an option argument
2661 # specifying a new branch name. This is for
2662 # convenience, assuming new branches are
2663 # possibly based on pre-existing branch names.
2664 __git_complete_refs $dwim_opt --mode="heads"
2673 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
2676 __gitcomp_builtin switch
2679 # Unlike in git checkout, git switch --orphan does not take
2680 # a start point. Thus we really have nothing to complete after
2682 if [ -n "$(__git_find_on_cmdline "--orphan")" ]; then
2686 # At this point, we've already handled special completion for
2687 # -c/-C, and --orphan. There are 3 main things left to
2689 # 1) a start-point for -c/-C or -d/--detach
2690 # 2) a remote head, for --track
2691 # 3) a branch name, possibly including DWIM remote branches
2693 if [ -n "$(__git_find_on_cmdline "-c -C -d --detach")" ]; then
2694 __git_complete_refs --mode="refs"
2695 elif [ -n "$(__git_find_on_cmdline "-t --track")" ]; then
2696 __git_complete_refs --mode="remote-heads"
2698 __git_complete_refs $dwim_opt --mode="heads"
2704 __git_config_get_set_variables ()
2706 local prevword word config_file= c=$cword
2707 while [ $c -gt "$__git_cmd_idx" ]; do
2710 --system|--global|--local|--file=*)
2715 config_file="$word $prevword"
2723 __git config $config_file --name-only --list
2727 __git_compute_config_vars ()
2729 test -n "$__git_config_vars" ||
2730 __git_config_vars="$(git help --config-for-completion)"
2733 __git_config_vars_all=
2734 __git_compute_config_vars_all ()
2736 test -n "$__git_config_vars_all" ||
2737 __git_config_vars_all="$(git --no-pager help --config)"
2740 __git_compute_first_level_config_vars_for_section ()
2743 __git_compute_config_vars
2744 local this_section="__git_first_level_config_vars_for_section_${section}"
2745 test -n "${!this_section}" ||
2746 printf -v "__git_first_level_config_vars_for_section_${section}" %s \
2747 "$(echo "$__git_config_vars" | awk -F. "/^${section}\.[a-z]/ { print \$2 }")"
2750 __git_compute_second_level_config_vars_for_section ()
2753 __git_compute_config_vars_all
2754 local this_section="__git_second_level_config_vars_for_section_${section}"
2755 test -n "${!this_section}" ||
2756 printf -v "__git_second_level_config_vars_for_section_${section}" %s \
2757 "$(echo "$__git_config_vars_all" | awk -F. "/^${section}\.</ { print \$3 }")"
2760 __git_config_sections=
2761 __git_compute_config_sections ()
2763 test -n "$__git_config_sections" ||
2764 __git_config_sections="$(git help --config-sections-for-completion)"
2767 # Completes possible values of various configuration variables.
2769 # Usage: __git_complete_config_variable_value [<option>]...
2770 # --varname=<word>: The name of the configuration variable whose value is
2771 # to be completed. Defaults to the previous word on the
2773 # --cur=<word>: The current value to be completed. Defaults to the current
2774 # word to be completed.
2775 __git_complete_config_variable_value ()
2777 local varname="$prev" cur_="$cur"
2779 while test $# != 0; do
2781 --varname=*) varname="${1##--varname=}" ;;
2782 --cur=*) cur_="${1##--cur=}" ;;
2788 if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
2789 varname="${varname,,}"
2791 varname="$(echo "$varname" |tr A-Z a-z)"
2795 branch.*.remote|branch.*.pushremote)
2796 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2800 __git_complete_refs --cur="$cur_"
2804 __gitcomp "false true merges interactive" "" "$cur_"
2808 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2812 local remote="${varname#remote.}"
2813 remote="${remote%.fetch}"
2814 if [ -z "$cur_" ]; then
2815 __gitcomp_nl "refs/heads/" "" "" ""
2818 __gitcomp_nl "$(__git_refs_remotes "$remote")" "" "$cur_"
2822 local remote="${varname#remote.}"
2823 remote="${remote%.push}"
2824 __gitcomp_nl "$(__git for-each-ref \
2825 --format='%(refname):%(refname)' refs/heads)" "" "$cur_"
2828 pull.twohead|pull.octopus)
2829 __git_compute_merge_strategies
2830 __gitcomp "$__git_merge_strategies" "" "$cur_"
2834 __gitcomp "false true" "" "$cur_"
2839 normal black red green yellow blue magenta cyan white
2840 bold dim ul blink reverse
2845 __gitcomp "false true always never auto" "" "$cur_"
2849 __gitcomp "$__git_diff_submodule_formats" "" "$cur_"
2853 __gitcomp "man info web html" "" "$cur_"
2857 __gitcomp "$__git_log_date_formats" "" "$cur_"
2860 sendemail.aliasfiletype)
2861 __gitcomp "mutt mailrc pine elm gnus" "" "$cur_"
2865 __gitcomp "$__git_send_email_confirm_options" "" "$cur_"
2868 sendemail.suppresscc)
2869 __gitcomp "$__git_send_email_suppresscc_options" "" "$cur_"
2872 sendemail.transferencoding)
2873 __gitcomp "7bit 8bit quoted-printable base64" "" "$cur_"
2882 # Completes configuration sections, subsections, variable names.
2884 # Usage: __git_complete_config_variable_name [<option>]...
2885 # --cur=<word>: The current configuration section/variable name to be
2886 # completed. Defaults to the current word to be completed.
2887 # --sfx=<suffix>: A suffix to be appended to each fully completed
2888 # configuration variable name (but not to sections or
2889 # subsections) instead of the default space.
2890 __git_complete_config_variable_name ()
2892 local cur_="$cur" sfx
2894 while test $# != 0; do
2896 --cur=*) cur_="${1##--cur=}" ;;
2897 --sfx=*) sfx="${1##--sfx=}" ;;
2904 branch.*.*|guitool.*.*|difftool.*.*|man.*.*|mergetool.*.*|remote.*.*|submodule.*.*|url.*.*)
2905 local pfx="${cur_%.*}."
2907 local section="${pfx%.*.}"
2908 __git_compute_second_level_config_vars_for_section "${section}"
2909 local this_section="__git_second_level_config_vars_for_section_${section}"
2910 __gitcomp "${!this_section}" "$pfx" "$cur_" "$sfx"
2914 local pfx="${cur_%.*}."
2916 local section="${pfx%.}"
2917 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
2918 __git_compute_first_level_config_vars_for_section "${section}"
2919 local this_section="__git_first_level_config_vars_for_section_${section}"
2920 __gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
2924 local pfx="${cur_%.*}."
2926 __git_compute_all_commands
2927 __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_" "${sfx:- }"
2931 local pfx="${cur_%.*}."
2933 local section="${pfx%.}"
2934 __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
2935 __git_compute_first_level_config_vars_for_section "${section}"
2936 local this_section="__git_first_level_config_vars_for_section_${section}"
2937 __gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
2941 local pfx="${cur_%.*}."
2943 local section="${pfx%.}"
2944 __gitcomp_nl "$(__git config -f "$(__git rev-parse --show-toplevel)/.gitmodules" --get-regexp 'submodule.*.path' | awk -F. '{print $2}')" "$pfx" "$cur_" "."
2945 __git_compute_first_level_config_vars_for_section "${section}"
2946 local this_section="__git_first_level_config_vars_for_section_${section}"
2947 __gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
2951 __git_compute_config_vars
2952 __gitcomp "$__git_config_vars" "" "$cur_" "$sfx"
2955 __git_compute_config_sections
2956 __gitcomp "$__git_config_sections" "" "$cur_" "."
2961 # Completes '='-separated configuration sections/variable names and values
2962 # for 'git -c section.name=value'.
2964 # Usage: __git_complete_config_variable_name_and_value [<option>]...
2965 # --cur=<word>: The current configuration section/variable name/value to be
2966 # completed. Defaults to the current word to be completed.
2967 __git_complete_config_variable_name_and_value ()
2971 while test $# != 0; do
2973 --cur=*) cur_="${1##--cur=}" ;;
2981 __git_complete_config_variable_value \
2982 --varname="${cur_%%=*}" --cur="${cur_#*=}"
2985 __git_complete_config_variable_name --cur="$cur_" --sfx='='
2992 local subcommands subcommand
2994 __git_resolve_builtins "config"
2996 subcommands="$___git_resolved_builtins"
2997 subcommand="$(__git_find_subcommand "$subcommands")"
2999 if [ -z "$subcommand" ]
3001 __gitcomp "$subcommands"
3007 __gitcomp_builtin "config_$subcommand"
3012 case "$subcommand" in
3014 __gitcomp_nl "$(__git_config_get_set_variables)"
3019 __git_complete_config_variable_value
3022 __git_complete_config_variable_name
3027 __gitcomp_nl "$(__git_config_get_set_variables)"
3035 add rename remove set-head set-branches
3036 get-url set-url show prune update
3038 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3039 if [ -z "$subcommand" ]; then
3042 __gitcomp_builtin remote
3045 __gitcomp "$subcommands"
3051 case "$subcommand,$cur" in
3053 __gitcomp_builtin remote_add
3058 __gitcomp_builtin remote_set-head
3061 __gitcomp_builtin remote_set-branches
3063 set-head,*|set-branches,*)
3064 __git_complete_remote_or_refspec
3067 __gitcomp_builtin remote_update
3070 __gitcomp "$(__git_remotes) $(__git_get_config_variables "remotes")"
3073 __gitcomp_builtin remote_set-url
3076 __gitcomp_builtin remote_get-url
3079 __gitcomp_builtin remote_prune
3082 __gitcomp_nl "$(__git_remotes)"
3091 __gitcomp "short medium long" "" "${cur##--format=}"
3095 __gitcomp_builtin replace
3104 local subcommands="clear forget diff remaining status gc"
3105 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3106 if test -z "$subcommand"
3108 __gitcomp "$subcommands"
3115 __git_has_doubledash && return
3119 __gitcomp_builtin reset
3137 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
3140 __git_complete_refs --cur="${cur##--source=}"
3143 __gitcomp_builtin restore
3146 if __git_pseudoref_exists HEAD; then
3147 __git_complete_index_file "--modified"
3152 __git_revert_inprogress_options=$__git_sequencer_inprogress_options
3156 if __git_pseudoref_exists REVERT_HEAD; then
3157 __gitcomp "$__git_revert_inprogress_options"
3160 __git_complete_strategy && return
3163 __gitcomp_builtin revert "" \
3164 "$__git_revert_inprogress_options"
3175 __gitcomp_builtin rm
3180 __git_complete_index_file "--cached"
3185 __git_has_doubledash && return
3190 $__git_log_common_options
3191 $__git_log_shortlog_options
3192 --numbered --summary --email
3197 __git_complete_revlist
3202 __git_has_doubledash && return
3205 --pretty=*|--format=*)
3206 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
3211 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
3215 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
3219 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
3223 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
3226 --ws-error-highlight=*)
3227 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
3231 __gitcomp "$__git_diff_merges_opts" "" "${cur##--diff-merges=}"
3235 __gitcomp "--pretty= --format= --abbrev-commit --no-abbrev-commit
3236 --oneline --show-signature
3237 --expand-tabs --expand-tabs= --no-expand-tabs
3238 $__git_log_show_options
3239 $__git_diff_common_options
3244 __git_complete_revlist_file
3251 __gitcomp_builtin show-branch
3255 __git_complete_revlist
3258 __gitcomp_directories ()
3260 local _tmp_dir _tmp_completions _found=0
3262 # Get the directory of the current token; this differs from dirname
3263 # in that it keeps up to the final trailing slash. If no slash found
3266 _tmp_dir=$BASH_REMATCH
3268 # Find possible directory completions, adding trailing '/' characters,
3269 # de-quoting, and handling unusual characters.
3270 while IFS= read -r -d $'\0' c ; do
3271 # If there are directory completions, find ones that start
3272 # with "$cur", the current token, and put those in COMPREPLY
3273 if [[ $c == "$cur"* ]]; then
3277 done < <(__git ls-tree -z -d --name-only HEAD $_tmp_dir)
3279 if [[ $_found == 0 ]] && [[ "$cur" =~ /$ ]]; then
3280 # No possible further completions any deeper, so assume we're at
3281 # a leaf directory and just consider it complete
3282 __gitcomp_direct_append "$cur "
3283 elif [[ $_found == 0 ]]; then
3284 # No possible completions found. Avoid falling back to
3285 # bash's default file and directory completion, because all
3286 # valid completions have already been searched and the
3287 # fallbacks can do nothing but mislead. In fact, they can
3288 # mislead in three different ways:
3289 # 1) Fallback file completion makes no sense when asking
3290 # for directory completions, as this function does.
3291 # 2) Fallback directory completion is bad because
3292 # e.g. "/pro" is invalid and should NOT complete to
3294 # 3) Fallback file/directory completion only completes
3295 # on paths that exist in the current working tree,
3296 # i.e. which are *already* part of their
3297 # sparse-checkout. Thus, normal file and directory
3298 # completion is always useless for "git
3299 # sparse-checkout add" and is also probelmatic for
3300 # "git sparse-checkout set" unless using it to
3301 # strictly narrow the checkout.
3306 # In non-cone mode, the arguments to {set,add} are supposed to be
3307 # patterns, relative to the toplevel directory. These can be any kind
3308 # of general pattern, like 'subdir/*.c' and we can't complete on all
3309 # of those. However, if the user presses Tab to get tab completion, we
3310 # presume that they are trying to provide a pattern that names a specific
3312 __gitcomp_slash_leading_paths ()
3314 local dequoted_word pfx="" cur_ toplevel
3316 # Since we are dealing with a sparse-checkout, subdirectories may not
3317 # exist in the local working copy. Therefore, we want to run all
3318 # ls-files commands relative to the repository toplevel.
3319 toplevel="$(git rev-parse --show-toplevel)/"
3321 __git_dequote "$cur"
3323 # If the paths provided by the user already start with '/', then
3324 # they are considered relative to the toplevel of the repository
3325 # already. If they do not start with /, then we need to adjust
3326 # them to start with the appropriate prefix.
3332 pfx="$(__git rev-parse --show-prefix)"
3335 # Since sparse-index is limited to cone-mode, in non-cone-mode the
3336 # list of valid paths is precisely the cached files in the index.
3339 # 1) We probably need to take care of cases where ls-files
3340 # responds with special quoting.
3341 # 2) We probably need to take care of cases where ${cur} has
3342 # some kind of special quoting.
3343 # 3) On top of any quoting from 1 & 2, we have to provide an extra
3344 # level of quoting for any paths that contain a '*', '?', '\',
3345 # '[', ']', or leading '#' or '!' since those will be
3346 # interpreted by sparse-checkout as something other than a
3347 # literal path character.
3348 # Since there are two types of quoting here, this might get really
3349 # complex. For now, just punt on all of this...
3350 completions="$(__git -C "${toplevel}" -c core.quotePath=false \
3351 ls-files --cached -- "${pfx}${cur}*" \
3352 | sed -e s%^%/% -e 's%$% %')"
3353 # Note, above, though that we needed all of the completions to be
3354 # prefixed with a '/', and we want to add a space so that bash
3355 # completion will actually complete an entry and let us move on to
3358 # Return what we've found.
3359 if test -n "$completions"; then
3360 # We found some completions; return them
3362 COMPREPLY=($completions)
3364 # Do NOT fall back to bash-style all-local-files-and-dirs
3365 # when we find no match. Such options are worse than
3367 # 1. "git sparse-checkout add" needs paths that are NOT
3368 # currently in the working copy. "git
3369 # sparse-checkout set" does as well, except in the
3370 # special cases when users are only trying to narrow
3371 # their sparse checkout to a subset of what they
3374 # 2. A path like '.config' is ambiguous as to whether
3375 # the user wants all '.config' files throughout the
3376 # tree, or just the one under the current directory.
3377 # It would result in a warning from the
3378 # sparse-checkout command due to this. As such, all
3379 # completions of paths should be prefixed with a
3382 # 3. We don't want paths prefixed with a '/' to
3383 # complete files in the system root directory, we
3384 # want it to complete on files relative to the
3387 # As such, make sure that NO completions are offered rather
3388 # than falling back to bash's default completions.
3393 _git_sparse_checkout ()
3395 local subcommands="list init set disable add reapply"
3396 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3397 local using_cone=true
3398 if [ -z "$subcommand" ]; then
3399 __gitcomp "$subcommands"
3403 case "$subcommand,$cur" in
3405 __gitcomp_builtin sparse-checkout_$subcommand "" "--"
3408 if [[ "$(__git config core.sparseCheckout)" == "true" &&
3409 "$(__git config core.sparseCheckoutCone)" == "false" &&
3410 -z "$(__git_find_on_cmdline --cone)" ]]; then
3413 if [[ -n "$(__git_find_on_cmdline --no-cone)" ]]; then
3416 if [[ "$using_cone" == "true" ]]; then
3417 __gitcomp_directories
3419 __gitcomp_slash_leading_paths
3426 local subcommands='push list show apply clear drop pop create branch'
3427 local subcommand="$(__git_find_on_cmdline "$subcommands save")"
3429 if [ -z "$subcommand" ]; then
3430 case "$((cword - __git_cmd_idx)),$cur" in
3432 __gitcomp_builtin stash_push
3438 __gitcomp "$subcommands"
3444 case "$subcommand,$cur" in
3446 # NEEDSWORK: can we somehow unify this with the options in _git_log() and _git_show()
3447 __gitcomp_builtin stash_list "$__git_log_common_options $__git_diff_common_options"
3450 __gitcomp_builtin stash_show "$__git_diff_common_options"
3453 __gitcomp_builtin "stash_$subcommand"
3456 if [ $cword -eq $((__git_cmd_idx+2)) ]; then
3459 __gitcomp_nl "$(__git stash list \
3460 | sed -n -e 's/:.*//p')"
3463 show,*|apply,*|drop,*|pop,*)
3464 __gitcomp_nl "$(__git stash list \
3465 | sed -n -e 's/:.*//p')"
3472 __git_has_doubledash && return
3474 local subcommands="add status init deinit update set-branch set-url summary foreach sync absorbgitdirs"
3475 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3476 if [ -z "$subcommand" ]; then
3482 __gitcomp "$subcommands"
3488 case "$subcommand,$cur" in
3490 __gitcomp "--branch --force --name --reference --depth"
3493 __gitcomp "--cached --recursive"
3496 __gitcomp "--force --all"
3500 --init --remote --no-fetch
3501 --recommend-shallow --no-recommend-shallow
3502 --force --rebase --merge --reference --depth --recursive --jobs
3506 __gitcomp "--default --branch"
3509 __gitcomp "--cached --files --summary-limit"
3511 foreach,--*|sync,--*)
3512 __gitcomp "--recursive"
3522 init fetch clone rebase dcommit log find-rev
3523 set-tree commit-diff info create-ignore propget
3524 proplist show-ignore show-externals branch tag blame
3525 migrate mkdirs reset gc
3527 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3528 if [ -z "$subcommand" ]; then
3529 __gitcomp "$subcommands"
3531 local remote_opts="--username= --config-dir= --no-auth-cache"
3533 --follow-parent --authors-file= --repack=
3534 --no-metadata --use-svm-props --use-svnsync-props
3535 --log-window-size= --no-checkout --quiet
3536 --repack-flags --use-log-author --localtime
3539 --ignore-paths= --include-paths= $remote_opts
3542 --template= --shared= --trunk= --tags=
3543 --branches= --stdlayout --minimize-url
3544 --no-metadata --use-svm-props --use-svnsync-props
3545 --rewrite-root= --prefix= $remote_opts
3548 --edit --rmdir --find-copies-harder --copy-similarity=
3551 case "$subcommand,$cur" in
3553 __gitcomp "--revision= --fetch-all $fc_opts"
3556 __gitcomp "--revision= $fc_opts $init_opts"
3559 __gitcomp "$init_opts"
3563 --merge --strategy= --verbose --dry-run
3564 --fetch-all --no-rebase --commit-url
3565 --revision --interactive $cmt_opts $fc_opts
3569 __gitcomp "--stdin $cmt_opts $fc_opts"
3571 create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
3572 show-externals,--*|mkdirs,--*)
3573 __gitcomp "--revision="
3577 --limit= --revision= --verbose --incremental
3578 --oneline --show-commit --non-recursive
3579 --authors-file= --color
3584 --merge --verbose --strategy= --local
3585 --fetch-all --dry-run $fc_opts
3589 __gitcomp "--message= --file= --revision= $cmt_opts"
3595 __gitcomp "--dry-run --message --tag"
3598 __gitcomp "--dry-run --message"
3601 __gitcomp "--git-format"
3605 --config-dir= --ignore-paths= --minimize
3606 --no-auth-cache --username=
3610 __gitcomp "--revision= --parent"
3618 _git_symbolic_ref () {
3621 __gitcomp_builtin symbolic-ref
3631 local i c="$__git_cmd_idx" f=0
3632 while [ $c -lt $cword ]; do
3635 -d|--delete|-v|--verify)
3636 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3651 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3661 __gitcomp_builtin tag
3671 __git_complete_worktree_paths ()
3674 # Generate completion reply from worktree list skipping the first
3675 # entry: it's the path of the main worktree, which can't be moved,
3676 # removed, locked, etc.
3677 __gitcomp_nl "$(__git worktree list --porcelain |
3678 sed -n -e '2,$ s/^worktree //p')"
3683 local subcommands="add list lock move prune remove unlock"
3684 local subcommand subcommand_idx
3686 subcommand="$(__git_find_on_cmdline --show-idx "$subcommands")"
3687 subcommand_idx="${subcommand% *}"
3688 subcommand="${subcommand#* }"
3690 case "$subcommand,$cur" in
3692 __gitcomp "$subcommands"
3695 __gitcomp_builtin worktree_$subcommand
3697 add,*) # usage: git worktree add [<options>] <path> [<commit-ish>]
3698 # Here we are not completing an --option, it's either the
3701 -b|-B) # Complete refs for branch to be created/reseted.
3704 -*) # The previous word is an -o|--option without an
3705 # unstuck argument: have to complete the path for
3706 # the new worktree, so don't list anything, but let
3707 # Bash fall back to filename completion.
3709 *) # The previous word is not an --option, so it must
3710 # be either the 'add' subcommand, the unstuck
3711 # argument of an option (e.g. branch for -b|-B), or
3712 # the path for the new worktree.
3713 if [ $cword -eq $((subcommand_idx+1)) ]; then
3714 # Right after the 'add' subcommand: have to
3715 # complete the path, so fall back to Bash
3716 # filename completion.
3719 case "${words[cword-2]}" in
3720 -b|-B) # After '-b <branch>': have to
3721 # complete the path, so fall back
3722 # to Bash filename completion.
3724 *) # After the path: have to complete
3725 # the ref to be checked out.
3733 lock,*|remove,*|unlock,*)
3734 __git_complete_worktree_paths
3737 if [ $cword -eq $((subcommand_idx+1)) ]; then
3738 # The first parameter must be an existing working
3740 __git_complete_worktree_paths
3742 # The second parameter is the destination: it could
3743 # be any path, so don't list anything, but let Bash
3744 # fall back to filename completion.
3751 __git_complete_common () {
3756 __gitcomp_builtin "$command"
3761 __git_cmds_with_parseopt_helper=
3762 __git_support_parseopt_helper () {
3763 test -n "$__git_cmds_with_parseopt_helper" ||
3764 __git_cmds_with_parseopt_helper="$(__git --list-cmds=parseopt)"
3766 case " $__git_cmds_with_parseopt_helper " in
3776 __git_have_func () {
3777 declare -f -- "$1" >/dev/null 2>&1
3780 __git_complete_command () {
3782 local completion_func="_git_${command//-/_}"
3783 if ! __git_have_func $completion_func &&
3784 __git_have_func _completion_loader
3786 _completion_loader "git-$command"
3788 if __git_have_func $completion_func
3792 elif __git_support_parseopt_helper "$command"
3794 __git_complete_common "$command"
3803 local i c=1 command __git_dir __git_repo_path
3804 local __git_C_args C_args_count=0
3807 while [ $c -lt $cword ]; do
3811 __git_dir="${i#--git-dir=}"
3815 __git_dir="${words[c]}"
3824 -c|--work-tree|--namespace)
3828 __git_C_args[C_args_count++]=-C
3830 __git_C_args[C_args_count++]="${words[c]}"
3843 if [ -z "${command-}" ]; then
3845 --git-dir|-C|--work-tree)
3846 # these need a path argument, let's fall back to
3847 # Bash filename completion
3851 __git_complete_config_variable_name_and_value
3855 # we don't support completing these options' arguments
3874 --no-replace-objects
3879 if test -n "${GIT_TESTING_PORCELAIN_COMMAND_LIST-}"
3881 __gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
3883 local list_cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config
3885 if test "${GIT_COMPLETION_SHOW_ALL_COMMANDS-}" = "1"
3887 list_cmds=builtins,$list_cmds
3889 __gitcomp "$(__git --list-cmds=$list_cmds)"
3896 __git_complete_command "$command" && return
3898 local expansion=$(__git_aliased_command "$command")
3899 if [ -n "$expansion" ]; then
3901 __git_complete_command "$expansion"
3907 __git_has_doubledash && return
3909 local __git_repo_path
3910 __git_find_repo_path
3913 if __git_pseudoref_exists MERGE_HEAD; then
3919 $__git_log_common_options
3920 $__git_log_gitk_options
3926 __git_complete_revlist
3929 if [[ -n ${ZSH_VERSION-} && -z ${GIT_SOURCING_ZSH_COMPLETION-} ]]; then
3930 echo "ERROR: this script is obsolete, please see git-completion.zsh" 1>&2
3936 local cur words cword prev
3937 local __git_cmd_idx=0
3938 _get_comp_words_by_ref -n =: cur words cword prev
3944 local wrapper="__git_wrap${2}"
3945 eval "$wrapper () { __git_func_wrap $2 ; }"
3946 complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
3947 || complete -o default -o nospace -F $wrapper $1
3950 # Setup the completion for git commands
3951 # 1: command or alias
3952 # 2: function to call (e.g. `git
`, `gitk
`, `git_fetch
`)
3957 if __git_have_func $2; then
3959 elif __git_have_func __$2_main; then
3961 elif __git_have_func _$2; then
3964 echo "ERROR: could not find function '$2'" 1>&2
3967 ___git_complete $1 $func
3970 ___git_complete git __git_main
3971 ___git_complete gitk __gitk_main
3973 # The following are necessary only for Cygwin, and only are needed
3974 # when the user has tab-completed the executable name and consequently
3975 # included the '.exe' suffix.
3977 if [ "$OSTYPE" = cygwin ]; then
3978 ___git_complete git.exe __git_main