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 command that is not part of git, but you would still
35 # like completion, you can use __git_complete:
37 # __git_complete gl git_log
39 # Or if it's a main command (i.e. git or gitk):
41 # __git_complete gk gitk
43 # Compatible with bash 3.2.57.
45 # You can set the following environment variables to influence the behavior of
46 # the completion routines:
48 # GIT_COMPLETION_CHECKOUT_NO_GUESS
50 # When set to "1", do not include "DWIM" suggestions in git-checkout
51 # and git-switch completion (e.g., completing "foo" when "origin/foo"
54 # GIT_COMPLETION_SHOW_ALL_COMMANDS
56 # When set to "1" suggest all commands, including plumbing commands
57 # which are hidden by default (e.g. "cat-file" on "git ca<TAB>").
59 # GIT_COMPLETION_SHOW_ALL
61 # When set to "1" suggest all options, including options which are
62 # typically hidden (e.g. '--allow-empty' for 'git commit').
64 # GIT_COMPLETION_IGNORE_CASE
66 # When set, uses for-each-ref '--ignore-case' to find refs that match
67 # case insensitively, even on systems with case sensitive file systems
68 # (e.g., completing tag name "FOO" on "git checkout f<TAB>").
70 case "$COMP_WORDBREAKS" in
72 *) COMP_WORDBREAKS
="$COMP_WORDBREAKS:"
75 # Discovers the path to the git repository taking any '--git-dir=<path>' and
76 # '-C <path>' options into account and stores it in the $__git_repo_path
78 __git_find_repo_path
()
80 if [ -n "${__git_repo_path-}" ]; then
81 # we already know where it is
85 if [ -n "${__git_C_args-}" ]; then
86 __git_repo_path
="$(git "${__git_C_args[@]}" \
87 ${__git_dir:+--git-dir="$__git_dir"} \
88 rev-parse --absolute-git-dir 2>/dev/null)"
89 elif [ -n "${__git_dir-}" ]; then
90 test -d "$__git_dir" &&
91 __git_repo_path
="$__git_dir"
92 elif [ -n "${GIT_DIR-}" ]; then
94 __git_repo_path
="$GIT_DIR"
95 elif [ -d .git
]; then
98 __git_repo_path
="$(git rev-parse --git-dir 2>/dev/null)"
102 # Deprecated: use __git_find_repo_path() and $__git_repo_path instead
103 # __gitdir accepts 0 or 1 arguments (i.e., location)
104 # returns location of .git repo
107 if [ -z "${1-}" ]; then
108 __git_find_repo_path ||
return 1
109 echo "$__git_repo_path"
110 elif [ -d "$1/.git" ]; then
117 # Runs git with all the options given as argument, respecting any
118 # '--git-dir=<path>' and '-C <path>' options present on the command line
121 git
${__git_C_args:+"${__git_C_args[@]}"} \
122 ${__git_dir:+--git-dir="$__git_dir"} "$@" 2>/dev
/null
125 # Helper function to read the first line of a file into a variable.
126 # __git_eread requires 2 arguments, the file path and the name of the
127 # variable, in that order.
129 # This is taken from git-prompt.sh.
132 test -r "$1" && IFS
=$
'\r\n' read -r "$2" <"$1"
135 # Runs git in $__git_repo_path to determine whether a pseudoref exists.
136 # 1: The pseudo-ref to search
137 __git_pseudoref_exists
()
144 # If the reftable is in use, we have to shell out to 'git rev-parse'
145 # to determine whether the ref exists instead of looking directly in
146 # the filesystem to determine whether the ref exists. Otherwise, use
147 # Bash builtins since executing Git commands are expensive on some
149 if __git_eread
"$__git_repo_path/HEAD" head; then
150 if [ "$head" == "ref: refs/heads/.invalid" ]; then
151 __git show-ref
--exists "$ref"
156 [ -f "$__git_repo_path/$ref" ]
159 # Removes backslash escaping, single quotes and double quotes from a word,
160 # stores the result in the variable $dequoted_word.
161 # 1: The word to dequote.
164 local rest
="$1" len ch
168 while test -n "$rest"; do
169 len
=${#dequoted_word}
170 dequoted_word
="$dequoted_word${rest%%[\\\'\"]*}"
171 rest
="${rest:$((${#dequoted_word}-$len))}"
173 case "${rest:0:1}" in
180 dequoted_word
="$dequoted_word$ch"
187 len
=${#dequoted_word}
188 dequoted_word
="$dequoted_word${rest%%\'*}"
189 rest
="${rest:$((${#dequoted_word}-$len+1))}"
193 while test -n "$rest" ; do
194 len
=${#dequoted_word}
195 dequoted_word
="$dequoted_word${rest%%[\\\"]*}"
196 rest
="${rest:$((${#dequoted_word}-$len))}"
197 case "${rest:0:1}" in
202 dequoted_word="$dequoted_word$ch"
207 dequoted_word="$dequoted_word\\$ch"
223 # The following function is based on code from:
225 # bash_completion - programmable completion functions for bash 3.2+
227 # Copyright © 2006-2008, Ian Macdonald <ian@caliban.org>
228 # © 2009-2010, Bash Completion Maintainers
229 # <bash-completion-devel@lists.alioth.debian.org>
231 # This program is free software; you can redistribute it and/or modify
232 # it under the terms of the GNU General Public License as published by
233 # the Free Software Foundation; either version 2, or (at your option)
236 # This program is distributed in the hope that it will be useful,
237 # but WITHOUT ANY WARRANTY; without even the implied warranty of
238 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
239 # GNU General Public License for more details.
241 # You should have received a copy of the GNU General Public License
242 # along with this program; if not, see <http://www.gnu.org/licenses/>.
244 # The latest version of this software can be obtained here:
246 # http://bash-completion.alioth.debian.org/
250 # This function can be used to access a tokenized list of words
251 # on the command line:
253 # __git_reassemble_comp_words_by_ref '=:'
254 # if test "${words_[cword_-1]}" = -w
259 # The argument should be a collection of characters from the list of
260 # word completion separators (COMP_WORDBREAKS) to treat as ordinary
263 # This is roughly equivalent to going back in time and setting
264 # COMP_WORDBREAKS to exclude those characters. The intent is to
265 # make option types like --date=<type> and <rev>:<path> easy to
266 # recognize by treating each shell word as a single token.
268 # It is best not to set COMP_WORDBREAKS directly because the value is
269 # shared with other completion scripts. By the time the completion
270 # function gets called, COMP_WORDS has already been populated so local
271 # changes to COMP_WORDBREAKS have no effect.
273 # Output: words_, cword_, cur_.
275 __git_reassemble_comp_words_by_ref()
277 local exclude i j first
278 # Which word separators to exclude?
279 exclude="${1//[^$COMP_WORDBREAKS]}"
281 if [ -z "$exclude" ]; then
282 words_=("${COMP_WORDS[@]}")
285 # List of word completion separators has shrunk;
286 # re-assemble words to complete.
287 for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
288 # Append each nonempty word consisting of just
289 # word separator characters to the current word.
293 [ -n "${COMP_WORDS[$i]}" ] &&
294 # word consists of excluded word separators
295 [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
297 # Attach to the previous token,
298 # unless the previous token is the command name.
299 if [ $j -ge 2 ] && [ -n "$first" ]; then
303 words_[$j]=${words_[j]}${COMP_WORDS[i]}
304 if [ $i = $COMP_CWORD ]; then
307 if (($i < ${#COMP_WORDS[@]} - 1)); then
314 words_[$j]=${words_[j]}${COMP_WORDS[i]}
315 if [ $i = $COMP_CWORD ]; then
321 if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
322 _get_comp_words_by_ref ()
324 local exclude cur_ words_ cword_
325 if [ "$1" = "-n" ]; then
329 __git_reassemble_comp_words_by_ref "$exclude"
330 cur_=${words_[cword_]}
331 while [ $# -gt 0 ]; do
337 prev=${words_[$cword_-1]}
340 words=("${words_[@]}")
351 # Fills the COMPREPLY array with prefiltered words without any additional
353 # Callers must take care of providing only words that match the current word
354 # to be completed and adding any prefix and/or suffix (trailing space!), if
356 # 1: List of newline-separated matching completion words, complete with
365 # Similar to __gitcomp_direct, but appends to COMPREPLY instead.
366 # Callers must take care of providing only words that match the current word
367 # to be completed and adding any prefix and/or suffix (trailing space!), if
369 # 1: List of newline-separated matching completion words, complete with
371 __gitcomp_direct_append ()
380 local x i=${#COMPREPLY[@]}
382 if [[ "$x" == "$3"* ]]; then
383 COMPREPLY[i++]="$2$x$4"
394 # Generates completion reply, appending a space to possible completion words,
396 # It accepts 1 to 4 arguments:
397 # 1: List of possible completion words.
398 # 2: A prefix to be added to each possible completion word (optional).
399 # 3: Generate possible completion matches for this word (optional).
400 # 4: A suffix to be appended to each possible completion word (optional).
403 local cur_="${3-$cur}"
409 local c i=0 IFS=$' \t\n'
411 if [[ $c == "--" ]]; then
415 if [[ $c == "$cur_"* ]]; then
420 COMPREPLY[i++]="${2-}$c"
425 local c i=0 IFS=$' \t\n'
427 if [[ $c == "--" ]]; then
429 if [[ $c == "$cur_"* ]]; then
430 COMPREPLY[i++]="${2-}$c "
435 if [[ $c == "$cur_"* ]]; then
440 COMPREPLY[i++]="${2-}$c"
447 # Clear the variables caching builtins' options when (re-)sourcing
448 # the completion script.
449 if [[ -n ${ZSH_VERSION-} ]]; then
450 unset ${(M)${(k)parameters[@]}:#__gitcomp_builtin_*} 2>/dev/null
452 unset $(compgen -v __gitcomp_builtin_)
455 # This function is equivalent to
457 # ___git_resolved_builtins=$(git xxx --git-completion-helper)
459 # except that the result of the execution is cached.
461 # Accept 1-3 arguments:
462 # 1: the git command to execute, this is also the cache key
463 # (use "_" when the command contains spaces, e.g. "remote add"
464 # becomes "remote_add")
465 # 2: extra options to be added on top (e.g. negative forms)
466 # 3: options to be excluded
467 __git_resolve_builtins ()
473 local var=__gitcomp_builtin_"${cmd//-/_}"
475 eval "options=\${$var-}"
477 if [ -z "$options" ]; then
478 local completion_helper
479 if [ "${GIT_COMPLETION_SHOW_ALL-}" = "1" ]; then
480 completion_helper="--git-completion-helper-all"
482 completion_helper="--git-completion-helper"
484 # leading and trailing spaces are significant to make
485 # option removal work correctly.
486 options=" $incl $(__git ${cmd/_/ } $completion_helper) " || return
489 options="${options/ $i / }"
491 eval "$var=\"$options\""
494 ___git_resolved_builtins="$options"
497 # This function is equivalent to
499 # __gitcomp "$(git xxx --git-completion-helper) ..."
501 # except that the output is cached. Accept 1-3 arguments:
502 # 1: the git command to execute, this is also the cache key
503 # (use "_" when the command contains spaces, e.g. "remote add"
504 # becomes "remote_add")
505 # 2: extra options to be added on top (e.g. negative forms)
506 # 3: options to be excluded
509 __git_resolve_builtins "$1" "$2" "$3"
511 __gitcomp "$___git_resolved_builtins"
514 # Variation of __gitcomp_nl () that appends to the existing list of
515 # completion candidates, COMPREPLY.
516 __gitcomp_nl_append ()
519 __gitcompappend "$1" "${2-}" "${3-$cur}" "${4- }"
522 # Generates completion reply from newline-separated possible completion words
523 # by appending a space to all of them.
524 # It accepts 1 to 4 arguments:
525 # 1: List of possible completion words, separated by a single newline.
526 # 2: A prefix to be added to each possible completion word (optional).
527 # 3: Generate possible completion matches for this word (optional).
528 # 4: A suffix to be appended to each possible completion word instead of
529 # the default space (optional). If specified but empty, nothing is
534 __gitcomp_nl_append "$@"
537 # Fills the COMPREPLY array with prefiltered paths without any additional
539 # Callers must take care of providing only paths that match the current path
540 # to be completed and adding any prefix path components, if necessary.
541 # 1: List of newline-separated matching paths, complete with all prefix
543 __gitcomp_file_direct ()
549 # use a hack to enable file mode in bash < 4
550 compopt -o filenames +o nospace 2>/dev/null ||
551 compgen -f /non-existing-dir/ >/dev/null ||
555 # Generates completion reply with compgen from newline-separated possible
556 # completion filenames.
557 # It accepts 1 to 3 arguments:
558 # 1: List of possible completion filenames, separated by a single newline.
559 # 2: A directory prefix to be added to each possible completion filename
561 # 3: Generate possible completion matches for this word (optional).
566 # XXX does not work when the directory prefix contains a tilde,
567 # since tilde expansion is not applied.
568 # This means that COMPREPLY will be empty and Bash default
569 # completion will be used.
570 __gitcompadd "$1" "${2-}" "${3-$cur}" ""
572 # use a hack to enable file mode in bash < 4
573 compopt -o filenames +o nospace 2>/dev/null ||
574 compgen -f /non-existing-dir/ >/dev/null ||
578 # Find the current subcommand for commands that follow the syntax:
580 # git <command> <subcommand>
582 # 1: List of possible subcommands.
583 # 2: Optional subcommand to return when none is found.
584 __git_find_subcommand ()
586 local subcommand subcommands="$1" default_subcommand="$2"
588 for subcommand in $subcommands; do
589 if [ "$subcommand" = "${words[__git_cmd_idx+1]}" ]; then
595 echo $default_subcommand
598 # Execute 'git ls-files', unless the --committable option is specified, in
599 # which case it runs 'git diff-index' to find out the files that can be
600 # committed. It return paths relative to the directory specified in the first
601 # argument, and using the options specified in the second argument.
602 __git_ls_files_helper ()
604 if [ "$2" = "--committable" ]; then
605 __git -C "$1" -c core.quotePath=false diff-index \
606 --name-only --relative HEAD -- "${3//\\/\\\\}*"
608 # NOTE: $2 is not quoted in order to support multiple options
609 __git -C "$1" -c core.quotePath=false ls-files \
610 --exclude-standard $2 -- "${3//\\/\\\\}*"
615 # __git_index_files accepts 1 or 2 arguments:
616 # 1: Options to pass to ls-files (required).
617 # 2: A directory path (optional).
618 # If provided, only files within the specified directory are listed.
619 # Sub directories are never recursed. Path must have a trailing
621 # 3: List only paths matching this path component (optional).
624 local root="$2" match="$3"
626 __git_ls_files_helper "$root" "$1" "${match:-?}" |
627 awk -F / -v pfx="${2//\\/\\\\}" '{
632 if (substr(p, 1, 1) != "\"") {
633 # No special characters, easy!
638 # The path is quoted.
643 # Even when a directory name itself does not contain
644 # any special characters, it will still be quoted if
645 # any of its (stripped) trailing path components do.
646 # Because of this we may have seen the same directory
647 # both quoted and unquoted.
649 # We have seen the same directory unquoted,
656 function dequote(p, bs_idx, out, esc, esc_idx, dec) {
657 # Skip opening double quote.
660 # Interpret backslash escape sequences.
661 while ((bs_idx = index(p, "\\")) != 0) {
662 out = out substr(p, 1, bs_idx - 1)
663 esc = substr(p, bs_idx + 1, 1)
664 p = substr(p, bs_idx + 2)
666 if ((esc_idx = index("abtvfr\"\\", esc)) != 0) {
667 # C-style one-character escape sequence.
668 out = out substr("\a\b\t\v\f\r\"\\",
670 } else if (esc == "n") {
671 # Uh-oh, a newline character.
672 # We cannot reliably put a pathname
673 # containing a newline into COMPREPLY,
674 # and the newline would create a mess.
678 # Must be a \nnn octal value, then.
680 substr(p, 1, 1) * 8 + \
682 out = out sprintf("%c", dec)
686 # Drop closing double quote, if there is one.
687 # (There is not any if this is a directory, as it was
688 # already stripped with the trailing path components.)
689 if (substr(p, length(p), 1) == "\"")
690 out = out substr(p, 1, length(p) - 1)
698 # __git_complete_index_file requires 1 argument:
699 # 1: the options to pass to ls-file
701 # The exception is --committable, which finds the files appropriate commit.
702 __git_complete_index_file ()
704 local dequoted_word pfx="" cur_
708 case "$dequoted_word" in
710 pfx="${dequoted_word%/*}/"
711 cur_="${dequoted_word##*/}"
714 cur_="$dequoted_word"
717 __gitcomp_file_direct "$(__git_index_files "$1" "$pfx" "$cur_")"
720 # Lists branches from the local repository.
721 # 1: A prefix to be added to each listed branch (optional).
722 # 2: List only branches matching this word (optional; list all branches if
724 # 3: A suffix to be appended to each listed branch (optional).
727 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
729 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
730 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
731 "refs/heads/$cur_*" "refs/heads/$cur_*/**"
734 # Lists branches from remote repositories.
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).
739 __git_remote_heads ()
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/remotes/$cur_*" "refs/remotes/$cur_*/**"
748 # Lists tags from the local repository.
749 # Accepts the same positional parameters as __git_heads() above.
752 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
754 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
755 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
756 "refs/tags/$cur_*" "refs/tags/$cur_*/**"
759 # List unique branches from refs/remotes used for 'git checkout' and 'git
760 # switch' tracking DWIMery.
761 # 1: A prefix to be added to each listed branch (optional)
762 # 2: List only branches matching this word (optional; list all branches if
764 # 3: A suffix to be appended to each listed branch (optional).
765 __git_dwim_remote_heads ()
767 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
768 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
770 # employ the heuristic used by git checkout and git switch
771 # Try to find a remote branch that cur_es the completion word
772 # but only output if the branch name is unique
773 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
774 --sort="refname:strip=3" \
775 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
776 "refs/remotes/*/$cur_*" "refs/remotes/*/$cur_*/**" | \
780 # Lists refs from the local (by default) or from a remote repository.
781 # It accepts 0, 1 or 2 arguments:
782 # 1: The remote to list refs from (optional; ignored, if set but empty).
783 # Can be the name of a configured remote, a path, or a URL.
784 # 2: In addition to local refs, list unique branches from refs/remotes/ for
785 # 'git checkout's tracking DWIMery (optional; ignored, if set but empty).
786 # 3: A prefix to be added to each listed ref (optional).
787 # 4: List only refs matching this word (optional; list all refs if unset or
789 # 5: A suffix to be appended to each listed ref (optional; ignored, if set
792 # Use __git_complete_refs() instead.
795 local i hash dir track="${2-}"
796 local list_refs_from=path remote="${1-}"
798 local pfx="${3-}" cur_="${4-$cur}" sfx="${5-}"
801 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
804 dir="$__git_repo_path"
806 if [ -z "$remote" ]; then
807 if [ -z "$dir" ]; then
811 if __git_is_configured_remote "$remote"; then
812 # configured remote takes precedence over a
813 # local directory with the same name
814 list_refs_from=remote
815 elif [ -d "$remote/.git" ]; then
817 elif [ -d "$remote" ]; then
824 if test "${GIT_COMPLETION_IGNORE_CASE:+1}" = "1"
826 # uppercase with tr instead of ${match,^^} for bash 3.2 compatibility
827 umatch=$(echo "$match" | tr a-z A-Z 2>/dev/null || echo "$match")
830 if [ "$list_refs_from" = path ]; then
831 if [[ "$cur_" == ^* ]]; then
841 refs=("$match*" "$match*/**")
845 for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD REBASE_HEAD CHERRY_PICK_HEAD REVERT_HEAD BISECT_HEAD AUTO_MERGE; do
848 if [ -e "$dir/$i" ]; then
854 format="refname:strip=2"
855 refs=("refs/tags/$match*" "refs/tags/$match*/**"
856 "refs/heads/$match*" "refs/heads/$match*/**"
857 "refs/remotes/$match*" "refs/remotes/$match*/**")
860 __git_dir="$dir" __git for-each-ref --format="$fer_pfx%($format)$sfx" \
861 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
863 if [ -n "$track" ]; then
864 __git_dwim_remote_heads "$pfx" "$match" "$sfx"
870 __git ls-remote "$remote" "$match*" | \
871 while read -r hash i; do
874 *) echo "$pfx$i$sfx" ;;
879 if [ "$list_refs_from" = remote ]; then
881 $match*|$umatch*) echo "${pfx}HEAD$sfx" ;;
883 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
884 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
885 "refs/remotes/$remote/$match*" \
886 "refs/remotes/$remote/$match*/**"
890 $match*|$umatch*) query_symref="HEAD" ;;
892 __git ls-remote "$remote" $query_symref \
893 "refs/tags/$match*" "refs/heads/$match*" \
894 "refs/remotes/$match*" |
895 while read -r hash i; do
898 refs/*) echo "$pfx${i#refs/*/}$sfx" ;;
899 *) echo "$pfx$i$sfx" ;; # symbolic refs
907 # Completes refs, short and long, local and remote, symbolic and pseudo.
909 # Usage: __git_complete_refs [<option>]...
910 # --remote=<remote>: The remote to list refs from, can be the name of a
911 # configured remote, a path, or a URL.
912 # --dwim: List unique remote branches for 'git switch's tracking DWIMery.
913 # --pfx=<prefix>: A prefix to be added to each ref.
914 # --cur=<word>: The current ref to be completed. Defaults to the current
915 # word to be completed.
916 # --sfx=<suffix>: A suffix to be appended to each ref instead of the default
918 # --mode=<mode>: What set of refs to complete, one of 'refs' (the default) to
919 # complete all refs, 'heads' to complete only branches, or
920 # 'remote-heads' to complete only remote branches. Note that
921 # --remote is only compatible with --mode=refs.
922 __git_complete_refs ()
924 local remote= dwim= pfx= cur_="$cur" sfx=" " mode="refs"
926 while test $# != 0; do
928 --remote=*) remote="${1##--remote=}" ;;
929 --dwim) dwim="yes" ;;
930 # --track is an old spelling of --dwim
931 --track) dwim="yes" ;;
932 --pfx=*) pfx="${1##--pfx=}" ;;
933 --cur=*) cur_="${1##--cur=}" ;;
934 --sfx=*) sfx="${1##--sfx=}" ;;
935 --mode=*) mode="${1##--mode=}" ;;
941 # complete references based on the specified mode
944 __gitcomp_direct "$(__git_refs "$remote" "" "$pfx" "$cur_" "$sfx")" ;;
946 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" "$sfx")" ;;
948 __gitcomp_direct "$(__git_remote_heads "$pfx" "$cur_" "$sfx")" ;;
953 # Append DWIM remote branch names if requested
954 if [ "$dwim" = "yes" ]; then
955 __gitcomp_direct_append "$(__git_dwim_remote_heads "$pfx" "$cur_" "$sfx")"
959 # __git_refs2 requires 1 argument (to pass to __git_refs)
960 # Deprecated: use __git_complete_fetch_refspecs() instead.
964 for i in $(__git_refs "$1"); do
969 # Completes refspecs for fetching from a remote repository.
970 # 1: The remote repository.
971 # 2: A prefix to be added to each listed refspec (optional).
972 # 3: The ref to be completed as a refspec instead of the current word to be
973 # completed (optional)
974 # 4: A suffix to be appended to each listed refspec instead of the default
976 __git_complete_fetch_refspecs ()
978 local i remote="$1" pfx="${2-}" cur_="${3-$cur}" sfx="${4- }"
981 for i in $(__git_refs "$remote" "" "" "$cur_") ; do
987 # __git_refs_remotes requires 1 argument (to pass to ls-remote)
988 __git_refs_remotes ()
991 __git ls-remote "$1" 'refs/heads/*' | \
992 while read -r hash i; do
993 echo "$i:refs/remotes/$1/${i#refs/heads/}"
1000 test -d "$__git_repo_path/remotes" && ls -1 "$__git_repo_path/remotes"
1004 # Returns true if $1 matches the name of a configured remote, false otherwise.
1005 __git_is_configured_remote ()
1008 for remote in $(__git_remotes); do
1009 if [ "$remote" = "$1" ]; then
1016 __git_list_merge_strategies ()
1018 LANG=C LC_ALL=C git merge -s help 2>&1 |
1019 sed -n -e '/[Aa]vailable strategies are: /,/^$/{
1028 __git_merge_strategies=
1029 # 'git merge -s help' (and thus detection of the merge strategy
1030 # list) fails, unfortunately, if run outside of any git working
1031 # tree. __git_merge_strategies is set to the empty string in
1032 # that case, and the detection will be repeated the next time it
1034 __git_compute_merge_strategies ()
1036 test -n "$__git_merge_strategies" ||
1037 __git_merge_strategies=$(__git_list_merge_strategies)
1040 __git_merge_strategy_options="ours theirs subtree subtree= patience
1041 histogram diff-algorithm= ignore-space-change ignore-all-space
1042 ignore-space-at-eol renormalize no-renormalize no-renames
1043 find-renames find-renames= rename-threshold="
1045 __git_complete_revlist_file ()
1047 local dequoted_word pfx ls ref cur_="$cur"
1056 __git_dequote "$cur_"
1058 case "$dequoted_word" in
1060 pfx="${dequoted_word%/*}"
1061 cur_="${dequoted_word##*/}"
1066 cur_="$dequoted_word"
1071 case "$COMP_WORDBREAKS" in
1073 *) pfx="$ref:$pfx" ;;
1076 __gitcomp_file "$(__git ls-tree "$ls" \
1082 pfx="${cur_%...*}..."
1084 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1089 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1097 __git_complete_file ()
1099 __git_complete_revlist_file
1102 __git_complete_revlist ()
1104 __git_complete_revlist_file
1107 __git_complete_remote_or_refspec ()
1109 local cur_="$cur" cmd="${words[__git_cmd_idx]}"
1110 local i c=$((__git_cmd_idx+1)) remote="" pfx="" lhs=1 no_complete_refspec=0
1111 if [ "$cmd" = "remote" ]; then
1114 while [ $c -lt $cword ]; do
1117 --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
1118 -d|--delete) [ "$cmd" = "push" ] && lhs=0 ;;
1121 push) no_complete_refspec=1 ;;
1128 --multiple) no_complete_refspec=1; break ;;
1130 *) remote="$i"; break ;;
1134 if [ -z "$remote" ]; then
1135 __gitcomp_nl "$(__git_remotes)"
1138 if [ $no_complete_refspec = 1 ]; then
1141 [ "$remote" = "." ] && remote=
1144 case "$COMP_WORDBREAKS" in
1146 *) pfx="${cur_%%:*}:" ;;
1158 if [ $lhs = 1 ]; then
1159 __git_complete_fetch_refspecs "$remote" "$pfx" "$cur_"
1161 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1165 if [ $lhs = 1 ]; then
1166 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
1168 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1172 if [ $lhs = 1 ]; then
1173 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1175 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
1181 __git_complete_strategy ()
1183 __git_compute_merge_strategies
1186 __gitcomp "$__git_merge_strategies"
1190 __gitcomp "$__git_merge_strategy_options"
1196 __gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
1199 --strategy-option=*)
1200 __gitcomp "$__git_merge_strategy_options" "" "${cur##--strategy-option=}"
1208 __git_compute_all_commands ()
1210 test -n "$__git_all_commands" ||
1211 __git_all_commands=$(__git --list-cmds=main,others,alias,nohelpers)
1214 # Lists all set config variables starting with the given section prefix,
1215 # with the prefix removed.
1216 __git_get_config_variables ()
1218 local section="$1" i IFS=$'\n'
1219 for i in $(__git config --name-only --get-regexp "^$section\..*"); do
1220 echo "${i#$section.}"
1224 __git_pretty_aliases ()
1226 __git_get_config_variables "pretty"
1229 # __git_aliased_command requires 1 argument
1230 __git_aliased_command ()
1232 local cur=$1 last list= word cmdline
1234 while [[ -n "$cur" ]]; do
1235 if [[ "$list" == *" $cur "* ]]; then
1240 cmdline=$(__git config --get "alias.$cur")
1245 for word in $cmdline; do
1251 \!*) : shell command alias ;;
1253 *=*) : setting env ;;
1254 git) : git itself ;;
1255 \(\)) : skip parens of shell function definition ;;
1256 {) : skip start of shell helper function ;;
1257 :) : skip null command ;;
1258 \'*) : skip opening quote after sh -c ;;
1267 if [[ "$cur" != "$1" ]]; then
1272 # Check whether one of the given words is present on the command line,
1273 # and print the first word found.
1275 # Usage: __git_find_on_cmdline [<option>]... "<wordlist>"
1276 # --show-idx: Optionally show the index of the found word in the $words array.
1277 __git_find_on_cmdline ()
1279 local word c="$__git_cmd_idx" show_idx
1281 while test $# -gt 1; do
1283 --show-idx) show_idx=y ;;
1290 while [ $c -lt $cword ]; do
1291 for word in $wordlist; do
1292 if [ "$word" = "${words[c]}" ]; then
1293 if [ -n "${show_idx-}" ]; then
1305 # Similar to __git_find_on_cmdline, except that it loops backwards and thus
1306 # prints the *last* word found. Useful for finding which of two options that
1307 # supersede each other came last, such as "--guess" and "--no-guess".
1309 # Usage: __git_find_last_on_cmdline [<option>]... "<wordlist>"
1310 # --show-idx: Optionally show the index of the found word in the $words array.
1311 __git_find_last_on_cmdline ()
1313 local word c=$cword show_idx
1315 while test $# -gt 1; do
1317 --show-idx) show_idx=y ;;
1324 while [ $c -gt "$__git_cmd_idx" ]; do
1326 for word in $wordlist; do
1327 if [ "$word" = "${words[c]}" ]; then
1328 if [ -n "$show_idx" ]; then
1339 # Echo the value of an option set on the command line or config
1341 # $1: short option name
1342 # $2: long option name including =
1343 # $3: list of possible values
1344 # $4: config string (optional)
1347 # result="$(__git_get_option_value "-d" "--do-something=" \
1348 # "yes no" "core.doSomething")"
1350 # result is then either empty (no option set) or "yes" or "no"
1352 # __git_get_option_value requires 3 arguments
1353 __git_get_option_value ()
1355 local c short_opt long_opt val
1356 local result= values config_key word
1364 while [ $c -ge 0 ]; do
1366 for val in $values; do
1367 if [ "$short_opt$val" = "$word" ] ||
1368 [ "$long_opt$val" = "$word" ]; then
1376 if [ -n "$config_key" ] && [ -z "$result" ]; then
1377 result="$(__git config "$config_key")"
1383 __git_has_doubledash ()
1386 while [ $c -lt $cword ]; do
1387 if [ "--" = "${words[c]}" ]; then
1395 # Try to count non option arguments passed on the command line for the
1396 # specified git command.
1397 # When options are used, it is necessary to use the special -- option to
1398 # tell the implementation were non option arguments begin.
1399 # XXX this can not be improved, since options can appear everywhere, as
1403 # __git_count_arguments requires 1 argument: the git command executed.
1404 __git_count_arguments ()
1408 # Skip "git" (first argument)
1409 for ((i=$__git_cmd_idx; i < ${#words[@]}; i++)); do
1414 # Good; we can assume that the following are only non
1419 # Skip the specified git command and discard git
1432 __git_whitespacelist="nowarn warn error error-all fix"
1433 __git_patchformat="mbox stgit stgit-series hg mboxrd"
1434 __git_showcurrentpatch="diff raw"
1435 __git_am_inprogress_options="--skip --continue --resolved --abort --quit --show-current-patch"
1436 __git_quoted_cr="nowarn warn strip"
1440 __git_find_repo_path
1441 if [ -d "$__git_repo_path"/rebase-apply ]; then
1442 __gitcomp "$__git_am_inprogress_options"
1447 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1451 __gitcomp "$__git_patchformat" "" "${cur##--patch-format=}"
1454 --show-current-patch=*)
1455 __gitcomp "$__git_showcurrentpatch" "" "${cur##--show-current-patch=}"
1459 __gitcomp "$__git_quoted_cr" "" "${cur##--quoted-cr=}"
1463 __gitcomp_builtin am "" \
1464 "$__git_am_inprogress_options"
1473 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1477 __gitcomp_builtin apply
1486 __gitcomp "+x -x" "" "${cur##--chmod=}"
1490 __gitcomp_builtin add
1494 local complete_opt="--others --modified --directory --no-empty-directory"
1495 if test -n "$(__git_find_on_cmdline "-u --update")"
1497 complete_opt="--modified"
1499 __git_complete_index_file "$complete_opt"
1506 __gitcomp "$(git archive --list)" "" "${cur##--format=}"
1510 __gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
1514 __gitcomp_builtin archive "--format= --list --verbose --prefix= --worktree-attributes"
1523 __git_has_doubledash && return
1525 __git_find_repo_path
1527 # If a bisection is in progress get the terms being used.
1528 local term_bad term_good
1529 if [ -f "$__git_repo_path"/BISECT_TERMS ]; then
1530 term_bad=$(__git bisect terms --term-bad)
1531 term_good=$(__git bisect terms --term-good)
1534 # We will complete any custom terms, but still always complete the
1535 # more usual bad/new/good/old because git bisect gives a good error
1536 # message if these are given when not in use, and that's better than
1537 # silent refusal to complete if the user is confused.
1539 # We want to recognize 'view' but not complete it, because it overlaps
1540 # with 'visualize' too much and is just an alias for it.
1542 local completable_subcommands="start bad new $term_bad good old $term_good terms skip reset visualize replay log run help"
1543 local all_subcommands="$completable_subcommands view"
1545 local subcommand="$(__git_find_on_cmdline "$all_subcommands")"
1547 if [ -z "$subcommand" ]; then
1548 __git_find_repo_path
1549 if [ -f "$__git_repo_path"/BISECT_START ]; then
1550 __gitcomp "$completable_subcommands"
1552 __gitcomp "replay start"
1557 case "$subcommand" in
1561 __gitcomp "--first-parent --no-checkout --term-new --term-bad --term-old --term-good"
1570 __gitcomp "--term-good --term-old --term-bad --term-new"
1574 __git_complete_log_opts
1577 bad|new|"$term_bad"|good|old|"$term_good"|reset|skip)
1585 __git_ref_fieldlist="refname objecttype objectsize objectname upstream push HEAD symref"
1589 local i c="$__git_cmd_idx" only_local_ref="n" has_r="n"
1591 while [ $c -lt $cword ]; do
1594 -d|-D|--delete|-m|-M|--move|-c|-C|--copy)
1595 only_local_ref="y" ;;
1603 --set-upstream-to=*)
1604 __git_complete_refs --cur="${cur##--set-upstream-to=}"
1607 __gitcomp_builtin branch
1610 if [ $only_local_ref = "y" -a $has_r = "n" ]; then
1611 __gitcomp_direct "$(__git_heads "" "$cur" " ")"
1621 local cmd="${words[__git_cmd_idx+1]}"
1623 $((__git_cmd_idx+1)))
1624 __gitcomp "create list-heads verify unbundle"
1626 $((__git_cmd_idx+2)))
1627 # looking for a file
1632 __git_complete_revlist
1639 # Helper function to decide whether or not we should enable DWIM logic for
1640 # git-switch and git-checkout.
1642 # To decide between the following rules in decreasing priority order:
1643 # - the last provided of "--guess" or "--no-guess" explicitly enable or
1644 # disable completion of DWIM logic respectively.
1645 # - If checkout.guess is false, disable completion of DWIM logic.
1646 # - If the --no-track option is provided, take this as a hint to disable the
1647 # DWIM completion logic
1648 # - If GIT_COMPLETION_CHECKOUT_NO_GUESS is set, disable the DWIM completion
1649 # logic, as requested by the user.
1650 # - Enable DWIM logic otherwise.
1652 __git_checkout_default_dwim_mode ()
1654 local last_option dwim_opt="--dwim"
1656 if [ "${GIT_COMPLETION_CHECKOUT_NO_GUESS-}" = "1" ]; then
1660 # --no-track disables DWIM, but with lower priority than
1661 # --guess/--no-guess/checkout.guess
1662 if [ -n "$(__git_find_on_cmdline "--no-track")" ]; then
1666 # checkout.guess = false disables DWIM, but with lower priority than
1667 # --guess/--no-guess
1668 if [ "$(__git config --type=bool checkout.guess)" = "false" ]; then
1672 # Find the last provided --guess or --no-guess
1673 last_option="$(__git_find_last_on_cmdline "--guess --no-guess")"
1674 case "$last_option" in
1688 __git_has_doubledash && return
1690 local dwim_opt="$(__git_checkout_default_dwim_mode)"
1694 # Complete local branches (and DWIM branch
1695 # remote branch names) for an option argument
1696 # specifying a new branch name. This is for
1697 # convenience, assuming new branches are
1698 # possibly based on pre-existing branch names.
1699 __git_complete_refs $dwim_opt --mode="heads"
1708 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
1711 __gitcomp_builtin checkout
1714 # At this point, we've already handled special completion for
1715 # the arguments to -b/-B, and --orphan. There are 3 main
1716 # things left we can possibly complete:
1717 # 1) a start-point for -b/-B, -d/--detach, or --orphan
1718 # 2) a remote head, for --track
1719 # 3) an arbitrary reference, possibly including DWIM names
1722 if [ -n "$(__git_find_on_cmdline "-b -B -d --detach --orphan")" ]; then
1723 __git_complete_refs --mode="refs"
1724 elif [ -n "$(__git_find_on_cmdline "-t --track")" ]; then
1725 __git_complete_refs --mode="remote-heads"
1727 __git_complete_refs $dwim_opt --mode="refs"
1733 __git_sequencer_inprogress_options="--continue --quit --abort --skip"
1735 __git_cherry_pick_inprogress_options=$__git_sequencer_inprogress_options
1739 if __git_pseudoref_exists CHERRY_PICK_HEAD; then
1740 __gitcomp "$__git_cherry_pick_inprogress_options"
1744 __git_complete_strategy && return
1748 __gitcomp_builtin cherry-pick "" \
1749 "$__git_cherry_pick_inprogress_options"
1761 __gitcomp_builtin clean
1766 # XXX should we check for -x option ?
1767 __git_complete_index_file "--others --directory"
1774 __git_complete_config_variable_name_and_value
1780 __git_complete_config_variable_name_and_value \
1781 --cur="${cur##--config=}"
1785 __gitcomp_builtin clone
1791 __git_untracked_file_modes="all no normal"
1793 __git_trailer_tokens ()
1795 __git config --name-only --get-regexp '^trailer\..*\.key$' | cut -d. -f 2- | rev | cut -d. -f2- | rev
1809 __gitcomp "default scissors strip verbatim whitespace
1810 " "" "${cur##--cleanup=}"
1813 --reuse-message=*|--reedit-message=*|\
1814 --fixup=*|--squash=*)
1815 __git_complete_refs --cur="${cur#*=}"
1818 --untracked-files=*)
1819 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
1823 __gitcomp_nl "$(__git_trailer_tokens)" "" "${cur##--trailer=}" ":"
1827 __gitcomp_builtin commit
1831 if __git rev-parse --verify --quiet HEAD >/dev/null; then
1832 __git_complete_index_file "--committable"
1834 # This is the first commit
1835 __git_complete_index_file "--cached"
1843 __gitcomp_builtin describe
1849 __git_diff_algorithms="myers minimal patience histogram"
1851 __git_diff_submodule_formats="diff log short"
1853 __git_color_moved_opts="no default plain blocks zebra dimmed-zebra"
1855 __git_color_moved_ws_opts="no ignore-space-at-eol ignore-space-change
1856 ignore-all-space allow-indentation-change"
1858 __git_ws_error_highlight_opts="context old new all default"
1860 # Options for the diff machinery (diff, log, show, stash, range-diff, ...)
1861 __git_diff_common_options="--stat --numstat --shortstat --summary
1862 --patch-with-stat --name-only --name-status --color
1863 --no-color --color-words --no-renames --check
1864 --color-moved --color-moved= --no-color-moved
1865 --color-moved-ws= --no-color-moved-ws
1866 --full-index --binary --abbrev --diff-filter=
1867 --find-copies --find-object --find-renames
1868 --no-relative --relative
1869 --find-copies-harder --ignore-cr-at-eol
1870 --text --ignore-space-at-eol --ignore-space-change
1871 --ignore-all-space --ignore-blank-lines --exit-code
1872 --quiet --ext-diff --no-ext-diff --unified=
1873 --no-prefix --src-prefix= --dst-prefix=
1874 --inter-hunk-context= --function-context
1875 --patience --histogram --minimal
1876 --raw --word-diff --word-diff-regex=
1877 --dirstat --dirstat= --dirstat-by-file
1878 --dirstat-by-file= --cumulative
1879 --diff-algorithm= --default-prefix
1880 --submodule --submodule= --ignore-submodules
1881 --indent-heuristic --no-indent-heuristic
1882 --textconv --no-textconv --break-rewrites
1883 --patch --no-patch --cc --combined-all-paths
1884 --anchored= --compact-summary --ignore-matching-lines=
1885 --irreversible-delete --line-prefix --no-stat
1886 --output= --output-indicator-context=
1887 --output-indicator-new= --output-indicator-old=
1888 --ws-error-highlight=
1889 --pickaxe-all --pickaxe-regex --patch-with-raw
1892 # Options for diff/difftool
1893 __git_diff_difftool_options="--cached --staged
1894 --base --ours --theirs --no-index --merge-base
1895 --ita-invisible-in-index --ita-visible-in-index
1896 $__git_diff_common_options"
1900 __git_has_doubledash && return
1904 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1908 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1912 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
1916 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
1919 --ws-error-highlight=*)
1920 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
1924 __gitcomp "$__git_diff_difftool_options"
1928 __git_complete_revlist_file
1931 __git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff
1932 tkdiff vimdiff nvimdiff gvimdiff xxdiff araxis p4merge
1933 bc codecompare smerge
1938 __git_has_doubledash && return
1942 __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1946 __gitcomp_builtin difftool "$__git_diff_difftool_options"
1950 __git_complete_revlist_file
1953 __git_fetch_recurse_submodules="yes on-demand no"
1958 --recurse-submodules=*)
1959 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1963 __gitcomp "blob:none blob:limit= sparse:oid=" "" "${cur##--filter=}"
1967 __gitcomp_builtin fetch
1971 __git_complete_remote_or_refspec
1974 __git_format_patch_extra_options="
1975 --full-index --not --all --no-prefix --src-prefix=
1976 --dst-prefix= --notes
1979 _git_format_patch ()
1985 " "" "${cur##--thread=}"
1988 --base=*|--interdiff=*|--range-diff=*)
1989 __git_complete_refs --cur="${cur#--*=}"
1993 __gitcomp_builtin format-patch "$__git_format_patch_extra_options"
1997 __git_complete_revlist
2004 __gitcomp_builtin fsck
2015 # Lists matching symbol names from a tag (as in ctags) file.
2016 # 1: List symbol names matching this word.
2017 # 2: The tag file to list symbol names from.
2018 # 3: A prefix to be added to each listed symbol name (optional).
2019 # 4: A suffix to be appended to each listed symbol name (optional).
2020 __git_match_ctag () {
2021 awk -v pfx="${3-}" -v sfx="${4-}" "
2022 /^${1//\//\\/}/ { print pfx \$1 sfx }
2026 # Complete symbol names from a tag file.
2027 # Usage: __git_complete_symbol [<option>]...
2028 # --tags=<file>: The tag file to list symbol names from instead of the
2030 # --pfx=<prefix>: A prefix to be added to each symbol name.
2031 # --cur=<word>: The current symbol name to be completed. Defaults to
2032 # the current word to be completed.
2033 # --sfx=<suffix>: A suffix to be appended to each symbol name instead
2034 # of the default space.
2035 __git_complete_symbol () {
2036 local tags=tags pfx="" cur_="${cur-}" sfx=" "
2038 while test $# != 0; do
2040 --tags=*) tags="${1##--tags=}" ;;
2041 --pfx=*) pfx="${1##--pfx=}" ;;
2042 --cur=*) cur_="${1##--cur=}" ;;
2043 --sfx=*) sfx="${1##--sfx=}" ;;
2049 if test -r "$tags"; then
2050 __gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
2056 __git_has_doubledash && return
2060 __gitcomp_builtin grep
2065 case "$cword,$prev" in
2066 $((__git_cmd_idx+1)),*|*,-*)
2067 __git_complete_symbol && return
2078 __gitcomp_builtin help
2082 if test -n "${GIT_TESTING_ALL_COMMAND_LIST-}"
2084 __gitcomp "$GIT_TESTING_ALL_COMMAND_LIST $(__git --list-cmds=alias,list-guide) gitk"
2086 __gitcomp "$(__git --list-cmds=main,nohelpers,alias,list-guide) gitk"
2095 false true umask group all world everybody
2096 " "" "${cur##--shared=}"
2100 __gitcomp_builtin init
2110 __gitcomp_builtin ls-files
2115 # XXX ignore options like --modified and always suggest all cached
2117 __git_complete_index_file "--cached"
2124 __gitcomp_builtin ls-remote
2128 __gitcomp_nl "$(__git_remotes)"
2135 __gitcomp_builtin ls-tree
2143 # Options that go well for log, shortlog and gitk
2144 __git_log_common_options="
2146 --branches --tags --remotes
2147 --first-parent --merges --no-merges
2149 --max-age= --since= --after=
2150 --min-age= --until= --before=
2151 --min-parents= --max-parents=
2152 --no-min-parents --no-max-parents
2153 --alternate-refs --ancestry-path
2154 --author-date-order --basic-regexp
2155 --bisect --boundary --exclude-first-parent-only
2156 --exclude-hidden --extended-regexp
2157 --fixed-strings --grep-reflog
2158 --ignore-missing --left-only --perl-regexp
2159 --reflog --regexp-ignore-case --remove-empty
2160 --right-only --show-linear-break
2161 --show-notes-by-default --show-pulls
2162 --since-as-filter --single-worktree
2164 # Options that go well for log and gitk (not shortlog)
2165 __git_log_gitk_options="
2166 --dense --sparse --full-history
2167 --simplify-merges --simplify-by-decoration
2168 --left-right --notes --no-notes
2170 # Options that go well for log and shortlog (not gitk)
2171 __git_log_shortlog_options="
2172 --author= --committer= --grep=
2173 --all-match --invert-grep
2175 # Options accepted by log and show
2176 __git_log_show_options="
2177 --diff-merges --diff-merges= --no-diff-merges --dd --remerge-diff
2181 __git_diff_merges_opts="off none on first-parent 1 separate m combined c dense-combined cc remerge r"
2183 __git_log_pretty_formats="oneline short medium full fuller reference email raw format: tformat: mboxrd"
2184 __git_log_date_formats="relative iso8601 iso8601-strict rfc2822 short local default human raw unix auto: format:"
2186 # Complete porcelain (i.e. not git-rev-list) options and at least some
2187 # option arguments accepted by git-log. Note that this same set of options
2188 # are also accepted by some other git commands besides git-log.
2189 __git_complete_log_opts ()
2194 if __git_pseudoref_exists MERGE_HEAD; then
2197 case "$prev,$cur" in
2199 return # fall back to Bash filename completion
2202 __git_complete_symbol --cur="${cur#:}" --sfx=":"
2206 __git_complete_symbol
2211 --pretty=*|--format=*)
2212 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2217 __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
2221 __gitcomp "full short no" "" "${cur##--decorate=}"
2225 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
2229 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
2232 --ws-error-highlight=*)
2233 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
2237 __gitcomp "sorted unsorted" "" "${cur##--no-walk=}"
2241 __gitcomp "$__git_diff_merges_opts" "" "${cur##--diff-merges=}"
2246 $__git_log_common_options
2247 $__git_log_shortlog_options
2248 $__git_log_gitk_options
2249 $__git_log_show_options
2250 --root --topo-order --date-order --reverse
2251 --follow --full-diff
2252 --abbrev-commit --no-abbrev-commit --abbrev=
2253 --relative-date --date=
2254 --pretty= --format= --oneline
2259 --decorate --decorate= --no-decorate
2261 --no-walk --no-walk= --do-walk
2262 --parents --children
2263 --expand-tabs --expand-tabs= --no-expand-tabs
2264 --clear-decorations --decorate-refs=
2265 --decorate-refs-exclude=
2267 $__git_diff_common_options
2272 return # fall back to Bash filename completion
2275 __git_complete_symbol --cur="${cur#-L:}" --sfx=":"
2279 __git_complete_symbol --pfx="-G" --cur="${cur#-G}"
2283 __git_complete_symbol --pfx="-S" --cur="${cur#-S}"
2291 __git_has_doubledash && return
2292 __git_find_repo_path
2294 __git_complete_log_opts
2295 [ ${#COMPREPLY[@]} -eq 0 ] || return
2297 __git_complete_revlist
2302 __git_complete_strategy && return
2306 __gitcomp_builtin merge
2316 __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
2320 __gitcomp "--tool= --prompt --no-prompt --gui --no-gui"
2330 __gitcomp_builtin merge-base
2341 __gitcomp_builtin mv
2346 if [ $(__git_count_arguments "mv") -gt 0 ]; then
2347 # We need to show both cached and untracked files (including
2348 # empty directories) since this may not be the last argument.
2349 __git_complete_index_file "--cached --others --directory"
2351 __git_complete_index_file "--cached"
2357 local subcommands='add append copy edit get-ref list merge prune remove show'
2358 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2360 case "$subcommand,$cur" in
2362 __gitcomp_builtin notes
2370 __gitcomp "$subcommands --ref"
2374 *,--reuse-message=*|*,--reedit-message=*)
2375 __git_complete_refs --cur="${cur#*=}"
2378 __gitcomp_builtin notes_$subcommand
2381 # this command does not take a ref, do not complete it
2397 __git_complete_strategy && return
2400 --recurse-submodules=*)
2401 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
2405 __gitcomp_builtin pull
2410 __git_complete_remote_or_refspec
2413 __git_push_recurse_submodules="check on-demand only"
2415 __git_complete_force_with_lease ()
2423 __git_complete_refs --cur="${cur_#*:}"
2426 __git_complete_refs --cur="$cur_"
2435 __gitcomp_nl "$(__git_remotes)"
2438 --recurse-submodules)
2439 __gitcomp "$__git_push_recurse_submodules"
2445 __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
2448 --recurse-submodules=*)
2449 __gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
2452 --force-with-lease=*)
2453 __git_complete_force_with_lease "${cur##--force-with-lease=}"
2457 __gitcomp_builtin push
2461 __git_complete_remote_or_refspec
2469 --creation-factor= --no-dual-color
2470 $__git_diff_common_options
2475 __git_complete_revlist
2478 __git_rebase_inprogress_options="--continue --skip --abort --quit --show-current-patch"
2479 __git_rebase_interactive_inprogress_options="$__git_rebase_inprogress_options --edit-todo"
2483 __git_find_repo_path
2484 if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
2485 __gitcomp "$__git_rebase_interactive_inprogress_options"
2487 elif [ -d "$__git_repo_path"/rebase-apply ] || \
2488 [ -d "$__git_repo_path"/rebase-merge ]; then
2489 __gitcomp "$__git_rebase_inprogress_options"
2492 __git_complete_strategy && return
2495 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
2499 __git_complete_refs --cur="${cur##--onto=}"
2503 __gitcomp_builtin rebase "" \
2504 "$__git_rebase_interactive_inprogress_options"
2513 local subcommands subcommand
2515 __git_resolve_builtins "reflog"
2517 subcommands="$___git_resolved_builtins"
2518 subcommand="$(__git_find_subcommand "$subcommands" "show")"
2520 case "$subcommand,$cur" in
2523 $__git_log_common_options
2528 __gitcomp_builtin "reflog_$subcommand"
2535 if [ $((cword - __git_cmd_idx)) -eq 1 ]; then
2536 __gitcompappend "$subcommands" "" "$cur" " "
2540 __git_send_email_confirm_options="always never auto cc compose"
2541 __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
2546 --to|--cc|--bcc|--from)
2547 __gitcomp "$(__git send-email --dump-aliases)"
2555 $__git_send_email_confirm_options
2556 " "" "${cur##--confirm=}"
2561 $__git_send_email_suppresscc_options
2562 " "" "${cur##--suppress-cc=}"
2566 --smtp-encryption=*)
2567 __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
2573 " "" "${cur##--thread=}"
2576 --to=*|--cc=*|--bcc=*|--from=*)
2577 __gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
2581 __gitcomp_builtin send-email "$__git_format_patch_extra_options"
2585 __git_complete_revlist
2596 local untracked_state
2599 --ignore-submodules=*)
2600 __gitcomp "none untracked dirty all" "" "${cur##--ignore-submodules=}"
2603 --untracked-files=*)
2604 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
2609 always never auto column row plain dense nodense
2610 " "" "${cur##--column=}"
2614 __gitcomp_builtin status
2619 untracked_state="$(__git_get_option_value "-u" "--untracked-files=" \
2620 "$__git_untracked_file_modes" "status.showUntrackedFiles")"
2622 case "$untracked_state" in
2624 # --ignored option does not matter
2628 complete_opt="--cached --directory --no-empty-directory --others"
2630 if [ -n "$(__git_find_on_cmdline "--ignored")" ]; then
2631 complete_opt="$complete_opt --ignored --exclude=*"
2636 __git_complete_index_file "$complete_opt"
2641 local dwim_opt="$(__git_checkout_default_dwim_mode)"
2645 # Complete local branches (and DWIM branch
2646 # remote branch names) for an option argument
2647 # specifying a new branch name. This is for
2648 # convenience, assuming new branches are
2649 # possibly based on pre-existing branch names.
2650 __git_complete_refs $dwim_opt --mode="heads"
2659 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
2662 __gitcomp_builtin switch
2665 # Unlike in git checkout, git switch --orphan does not take
2666 # a start point. Thus we really have nothing to complete after
2668 if [ -n "$(__git_find_on_cmdline "--orphan")" ]; then
2672 # At this point, we've already handled special completion for
2673 # -c/-C, and --orphan. There are 3 main things left to
2675 # 1) a start-point for -c/-C or -d/--detach
2676 # 2) a remote head, for --track
2677 # 3) a branch name, possibly including DWIM remote branches
2679 if [ -n "$(__git_find_on_cmdline "-c -C -d --detach")" ]; then
2680 __git_complete_refs --mode="refs"
2681 elif [ -n "$(__git_find_on_cmdline "-t --track")" ]; then
2682 __git_complete_refs --mode="remote-heads"
2684 __git_complete_refs $dwim_opt --mode="heads"
2690 __git_config_get_set_variables ()
2692 local prevword word config_file= c=$cword
2693 while [ $c -gt "$__git_cmd_idx" ]; do
2696 --system|--global|--local|--file=*)
2701 config_file="$word $prevword"
2709 __git config $config_file --name-only --list
2713 __git_compute_config_vars ()
2715 test -n "$__git_config_vars" ||
2716 __git_config_vars="$(git help --config-for-completion)"
2719 __git_config_vars_all=
2720 __git_compute_config_vars_all ()
2722 test -n "$__git_config_vars_all" ||
2723 __git_config_vars_all="$(git --no-pager help --config)"
2726 __git_compute_first_level_config_vars_for_section ()
2729 __git_compute_config_vars
2730 local this_section="__git_first_level_config_vars_for_section_${section}"
2731 test -n "${!this_section}" ||
2732 printf -v "__git_first_level_config_vars_for_section_${section}" %s \
2733 "$(echo "$__git_config_vars" | awk -F. "/^${section}\.[a-z]/ { print \$2 }")"
2736 __git_compute_second_level_config_vars_for_section ()
2739 __git_compute_config_vars_all
2740 local this_section="__git_second_level_config_vars_for_section_${section}"
2741 test -n "${!this_section}" ||
2742 printf -v "__git_second_level_config_vars_for_section_${section}" %s \
2743 "$(echo "$__git_config_vars_all" | awk -F. "/^${section}\.</ { print \$3 }")"
2746 __git_config_sections=
2747 __git_compute_config_sections ()
2749 test -n "$__git_config_sections" ||
2750 __git_config_sections="$(git help --config-sections-for-completion)"
2753 # Completes possible values of various configuration variables.
2755 # Usage: __git_complete_config_variable_value [<option>]...
2756 # --varname=<word>: The name of the configuration variable whose value is
2757 # to be completed. Defaults to the previous word on the
2759 # --cur=<word>: The current value to be completed. Defaults to the current
2760 # word to be completed.
2761 __git_complete_config_variable_value ()
2763 local varname="$prev" cur_="$cur"
2765 while test $# != 0; do
2767 --varname=*) varname="${1##--varname=}" ;;
2768 --cur=*) cur_="${1##--cur=}" ;;
2774 if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
2775 varname="${varname,,}"
2777 varname="$(echo "$varname" |tr A-Z a-z)"
2781 branch.*.remote|branch.*.pushremote)
2782 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2786 __git_complete_refs --cur="$cur_"
2790 __gitcomp "false true merges interactive" "" "$cur_"
2794 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2798 local remote="${varname#remote.}"
2799 remote="${remote%.fetch}"
2800 if [ -z "$cur_" ]; then
2801 __gitcomp_nl "refs/heads/" "" "" ""
2804 __gitcomp_nl "$(__git_refs_remotes "$remote")" "" "$cur_"
2808 local remote="${varname#remote.}"
2809 remote="${remote%.push}"
2810 __gitcomp_nl "$(__git for-each-ref \
2811 --format='%(refname):%(refname)' refs/heads)" "" "$cur_"
2814 pull.twohead|pull.octopus)
2815 __git_compute_merge_strategies
2816 __gitcomp "$__git_merge_strategies" "" "$cur_"
2820 __gitcomp "false true" "" "$cur_"
2825 normal black red green yellow blue magenta cyan white
2826 bold dim ul blink reverse
2831 __gitcomp "false true always never auto" "" "$cur_"
2835 __gitcomp "$__git_diff_submodule_formats" "" "$cur_"
2839 __gitcomp "man info web html" "" "$cur_"
2843 __gitcomp "$__git_log_date_formats" "" "$cur_"
2846 sendemail.aliasfiletype)
2847 __gitcomp "mutt mailrc pine elm gnus" "" "$cur_"
2851 __gitcomp "$__git_send_email_confirm_options" "" "$cur_"
2854 sendemail.suppresscc)
2855 __gitcomp "$__git_send_email_suppresscc_options" "" "$cur_"
2858 sendemail.transferencoding)
2859 __gitcomp "7bit 8bit quoted-printable base64" "" "$cur_"
2868 # Completes configuration sections, subsections, variable names.
2870 # Usage: __git_complete_config_variable_name [<option>]...
2871 # --cur=<word>: The current configuration section/variable name to be
2872 # completed. Defaults to the current word to be completed.
2873 # --sfx=<suffix>: A suffix to be appended to each fully completed
2874 # configuration variable name (but not to sections or
2875 # subsections) instead of the default space.
2876 __git_complete_config_variable_name ()
2878 local cur_="$cur" sfx
2880 while test $# != 0; do
2882 --cur=*) cur_="${1##--cur=}" ;;
2883 --sfx=*) sfx="${1##--sfx=}" ;;
2890 branch.*.*|guitool.*.*|difftool.*.*|man.*.*|mergetool.*.*|remote.*.*|submodule.*.*|url.*.*)
2891 local pfx="${cur_%.*}."
2893 local section="${pfx%.*.}"
2894 __git_compute_second_level_config_vars_for_section "${section}"
2895 local this_section="__git_second_level_config_vars_for_section_${section}"
2896 __gitcomp "${!this_section}" "$pfx" "$cur_" "$sfx"
2900 local pfx="${cur_%.*}."
2902 local section="${pfx%.}"
2903 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
2904 __git_compute_first_level_config_vars_for_section "${section}"
2905 local this_section="__git_first_level_config_vars_for_section_${section}"
2906 __gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
2910 local pfx="${cur_%.*}."
2912 __git_compute_all_commands
2913 __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_" "${sfx:- }"
2917 local pfx="${cur_%.*}."
2919 local section="${pfx%.}"
2920 __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
2921 __git_compute_first_level_config_vars_for_section "${section}"
2922 local this_section="__git_first_level_config_vars_for_section_${section}"
2923 __gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
2927 local pfx="${cur_%.*}."
2929 local section="${pfx%.}"
2930 __gitcomp_nl "$(__git config -f "$(__git rev-parse --show-toplevel)/.gitmodules" --get-regexp 'submodule.*.path' | awk -F. '{print $2}')" "$pfx" "$cur_" "."
2931 __git_compute_first_level_config_vars_for_section "${section}"
2932 local this_section="__git_first_level_config_vars_for_section_${section}"
2933 __gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
2937 __git_compute_config_vars
2938 __gitcomp "$__git_config_vars" "" "$cur_" "$sfx"
2941 __git_compute_config_sections
2942 __gitcomp "$__git_config_sections" "" "$cur_" "."
2947 # Completes '='-separated configuration sections/variable names and values
2948 # for 'git -c section.name=value'.
2950 # Usage: __git_complete_config_variable_name_and_value [<option>]...
2951 # --cur=<word>: The current configuration section/variable name/value to be
2952 # completed. Defaults to the current word to be completed.
2953 __git_complete_config_variable_name_and_value ()
2957 while test $# != 0; do
2959 --cur=*) cur_="${1##--cur=}" ;;
2967 __git_complete_config_variable_value \
2968 --varname="${cur_%%=*}" --cur="${cur_#*=}"
2971 __git_complete_config_variable_name --cur="$cur_" --sfx='='
2979 --get|--get-all|--unset|--unset-all)
2980 __gitcomp_nl "$(__git_config_get_set_variables)"
2984 __git_complete_config_variable_value
2990 __gitcomp_builtin config
2993 __git_complete_config_variable_name
3001 add rename remove set-head set-branches
3002 get-url set-url show prune update
3004 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3005 if [ -z "$subcommand" ]; then
3008 __gitcomp_builtin remote
3011 __gitcomp "$subcommands"
3017 case "$subcommand,$cur" in
3019 __gitcomp_builtin remote_add
3024 __gitcomp_builtin remote_set-head
3027 __gitcomp_builtin remote_set-branches
3029 set-head,*|set-branches,*)
3030 __git_complete_remote_or_refspec
3033 __gitcomp_builtin remote_update
3036 __gitcomp "$(__git_remotes) $(__git_get_config_variables "remotes")"
3039 __gitcomp_builtin remote_set-url
3042 __gitcomp_builtin remote_get-url
3045 __gitcomp_builtin remote_prune
3048 __gitcomp_nl "$(__git_remotes)"
3057 __gitcomp "short medium long" "" "${cur##--format=}"
3061 __gitcomp_builtin replace
3070 local subcommands="clear forget diff remaining status gc"
3071 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3072 if test -z "$subcommand"
3074 __gitcomp "$subcommands"
3081 __git_has_doubledash && return
3085 __gitcomp_builtin reset
3103 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
3106 __git_complete_refs --cur="${cur##--source=}"
3109 __gitcomp_builtin restore
3112 if __git_pseudoref_exists HEAD; then
3113 __git_complete_index_file "--modified"
3118 __git_revert_inprogress_options=$__git_sequencer_inprogress_options
3122 if __git_pseudoref_exists REVERT_HEAD; then
3123 __gitcomp "$__git_revert_inprogress_options"
3126 __git_complete_strategy && return
3129 __gitcomp_builtin revert "" \
3130 "$__git_revert_inprogress_options"
3141 __gitcomp_builtin rm
3146 __git_complete_index_file "--cached"
3151 __git_has_doubledash && return
3156 $__git_log_common_options
3157 $__git_log_shortlog_options
3158 --numbered --summary --email
3163 __git_complete_revlist
3168 __git_has_doubledash && return
3171 --pretty=*|--format=*)
3172 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
3177 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
3181 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
3185 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
3189 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
3192 --ws-error-highlight=*)
3193 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
3197 __gitcomp "$__git_diff_merges_opts" "" "${cur##--diff-merges=}"
3201 __gitcomp "--pretty= --format= --abbrev-commit --no-abbrev-commit
3202 --oneline --show-signature
3203 --expand-tabs --expand-tabs= --no-expand-tabs
3204 $__git_log_show_options
3205 $__git_diff_common_options
3210 __git_complete_revlist_file
3217 __gitcomp_builtin show-branch
3221 __git_complete_revlist
3224 __gitcomp_directories ()
3226 local _tmp_dir _tmp_completions _found=0
3228 # Get the directory of the current token; this differs from dirname
3229 # in that it keeps up to the final trailing slash. If no slash found
3232 _tmp_dir=$BASH_REMATCH
3234 # Find possible directory completions, adding trailing '/' characters,
3235 # de-quoting, and handling unusual characters.
3236 while IFS= read -r -d $'\0' c ; do
3237 # If there are directory completions, find ones that start
3238 # with "$cur", the current token, and put those in COMPREPLY
3239 if [[ $c == "$cur"* ]]; then
3243 done < <(__git ls-tree -z -d --name-only HEAD $_tmp_dir)
3245 if [[ $_found == 0 ]] && [[ "$cur" =~ /$ ]]; then
3246 # No possible further completions any deeper, so assume we're at
3247 # a leaf directory and just consider it complete
3248 __gitcomp_direct_append "$cur "
3249 elif [[ $_found == 0 ]]; then
3250 # No possible completions found. Avoid falling back to
3251 # bash's default file and directory completion, because all
3252 # valid completions have already been searched and the
3253 # fallbacks can do nothing but mislead. In fact, they can
3254 # mislead in three different ways:
3255 # 1) Fallback file completion makes no sense when asking
3256 # for directory completions, as this function does.
3257 # 2) Fallback directory completion is bad because
3258 # e.g. "/pro" is invalid and should NOT complete to
3260 # 3) Fallback file/directory completion only completes
3261 # on paths that exist in the current working tree,
3262 # i.e. which are *already* part of their
3263 # sparse-checkout. Thus, normal file and directory
3264 # completion is always useless for "git
3265 # sparse-checkout add" and is also probelmatic for
3266 # "git sparse-checkout set" unless using it to
3267 # strictly narrow the checkout.
3272 # In non-cone mode, the arguments to {set,add} are supposed to be
3273 # patterns, relative to the toplevel directory. These can be any kind
3274 # of general pattern, like 'subdir/*.c' and we can't complete on all
3275 # of those. However, if the user presses Tab to get tab completion, we
3276 # presume that they are trying to provide a pattern that names a specific
3278 __gitcomp_slash_leading_paths ()
3280 local dequoted_word pfx="" cur_ toplevel
3282 # Since we are dealing with a sparse-checkout, subdirectories may not
3283 # exist in the local working copy. Therefore, we want to run all
3284 # ls-files commands relative to the repository toplevel.
3285 toplevel="$(git rev-parse --show-toplevel)/"
3287 __git_dequote "$cur"
3289 # If the paths provided by the user already start with '/', then
3290 # they are considered relative to the toplevel of the repository
3291 # already. If they do not start with /, then we need to adjust
3292 # them to start with the appropriate prefix.
3298 pfx="$(__git rev-parse --show-prefix)"
3301 # Since sparse-index is limited to cone-mode, in non-cone-mode the
3302 # list of valid paths is precisely the cached files in the index.
3305 # 1) We probably need to take care of cases where ls-files
3306 # responds with special quoting.
3307 # 2) We probably need to take care of cases where ${cur} has
3308 # some kind of special quoting.
3309 # 3) On top of any quoting from 1 & 2, we have to provide an extra
3310 # level of quoting for any paths that contain a '*', '?', '\',
3311 # '[', ']', or leading '#' or '!' since those will be
3312 # interpreted by sparse-checkout as something other than a
3313 # literal path character.
3314 # Since there are two types of quoting here, this might get really
3315 # complex. For now, just punt on all of this...
3316 completions="$(__git -C "${toplevel}" -c core.quotePath=false \
3317 ls-files --cached -- "${pfx}${cur}*" \
3318 | sed -e s%^%/% -e 's%$% %')"
3319 # Note, above, though that we needed all of the completions to be
3320 # prefixed with a '/', and we want to add a space so that bash
3321 # completion will actually complete an entry and let us move on to
3324 # Return what we've found.
3325 if test -n "$completions"; then
3326 # We found some completions; return them
3328 COMPREPLY=($completions)
3330 # Do NOT fall back to bash-style all-local-files-and-dirs
3331 # when we find no match. Such options are worse than
3333 # 1. "git sparse-checkout add" needs paths that are NOT
3334 # currently in the working copy. "git
3335 # sparse-checkout set" does as well, except in the
3336 # special cases when users are only trying to narrow
3337 # their sparse checkout to a subset of what they
3340 # 2. A path like '.config' is ambiguous as to whether
3341 # the user wants all '.config' files throughout the
3342 # tree, or just the one under the current directory.
3343 # It would result in a warning from the
3344 # sparse-checkout command due to this. As such, all
3345 # completions of paths should be prefixed with a
3348 # 3. We don't want paths prefixed with a '/' to
3349 # complete files in the system root directory, we
3350 # want it to complete on files relative to the
3353 # As such, make sure that NO completions are offered rather
3354 # than falling back to bash's default completions.
3359 _git_sparse_checkout ()
3361 local subcommands="list init set disable add reapply"
3362 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3363 local using_cone=true
3364 if [ -z "$subcommand" ]; then
3365 __gitcomp "$subcommands"
3369 case "$subcommand,$cur" in
3371 __gitcomp_builtin sparse-checkout_$subcommand "" "--"
3374 if [[ "$(__git config core.sparseCheckout)" == "true" &&
3375 "$(__git config core.sparseCheckoutCone)" == "false" &&
3376 -z "$(__git_find_on_cmdline --cone)" ]]; then
3379 if [[ -n "$(__git_find_on_cmdline --no-cone)" ]]; then
3382 if [[ "$using_cone" == "true" ]]; then
3383 __gitcomp_directories
3385 __gitcomp_slash_leading_paths
3392 local subcommands='push list show apply clear drop pop create branch'
3393 local subcommand="$(__git_find_on_cmdline "$subcommands save")"
3395 if [ -z "$subcommand" ]; then
3396 case "$((cword - __git_cmd_idx)),$cur" in
3398 __gitcomp_builtin stash_push
3404 __gitcomp "$subcommands"
3410 case "$subcommand,$cur" in
3412 # NEEDSWORK: can we somehow unify this with the options in _git_log() and _git_show()
3413 __gitcomp_builtin stash_list "$__git_log_common_options $__git_diff_common_options"
3416 __gitcomp_builtin stash_show "$__git_diff_common_options"
3419 __gitcomp_builtin "stash_$subcommand"
3422 if [ $cword -eq $((__git_cmd_idx+2)) ]; then
3425 __gitcomp_nl "$(__git stash list \
3426 | sed -n -e 's/:.*//p')"
3429 show,*|apply,*|drop,*|pop,*)
3430 __gitcomp_nl "$(__git stash list \
3431 | sed -n -e 's/:.*//p')"
3438 __git_has_doubledash && return
3440 local subcommands="add status init deinit update set-branch set-url summary foreach sync absorbgitdirs"
3441 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3442 if [ -z "$subcommand" ]; then
3448 __gitcomp "$subcommands"
3454 case "$subcommand,$cur" in
3456 __gitcomp "--branch --force --name --reference --depth"
3459 __gitcomp "--cached --recursive"
3462 __gitcomp "--force --all"
3466 --init --remote --no-fetch
3467 --recommend-shallow --no-recommend-shallow
3468 --force --rebase --merge --reference --depth --recursive --jobs
3472 __gitcomp "--default --branch"
3475 __gitcomp "--cached --files --summary-limit"
3477 foreach,--*|sync,--*)
3478 __gitcomp "--recursive"
3488 init fetch clone rebase dcommit log find-rev
3489 set-tree commit-diff info create-ignore propget
3490 proplist show-ignore show-externals branch tag blame
3491 migrate mkdirs reset gc
3493 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3494 if [ -z "$subcommand" ]; then
3495 __gitcomp "$subcommands"
3497 local remote_opts="--username= --config-dir= --no-auth-cache"
3499 --follow-parent --authors-file= --repack=
3500 --no-metadata --use-svm-props --use-svnsync-props
3501 --log-window-size= --no-checkout --quiet
3502 --repack-flags --use-log-author --localtime
3505 --ignore-paths= --include-paths= $remote_opts
3508 --template= --shared= --trunk= --tags=
3509 --branches= --stdlayout --minimize-url
3510 --no-metadata --use-svm-props --use-svnsync-props
3511 --rewrite-root= --prefix= $remote_opts
3514 --edit --rmdir --find-copies-harder --copy-similarity=
3517 case "$subcommand,$cur" in
3519 __gitcomp "--revision= --fetch-all $fc_opts"
3522 __gitcomp "--revision= $fc_opts $init_opts"
3525 __gitcomp "$init_opts"
3529 --merge --strategy= --verbose --dry-run
3530 --fetch-all --no-rebase --commit-url
3531 --revision --interactive $cmt_opts $fc_opts
3535 __gitcomp "--stdin $cmt_opts $fc_opts"
3537 create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
3538 show-externals,--*|mkdirs,--*)
3539 __gitcomp "--revision="
3543 --limit= --revision= --verbose --incremental
3544 --oneline --show-commit --non-recursive
3545 --authors-file= --color
3550 --merge --verbose --strategy= --local
3551 --fetch-all --dry-run $fc_opts
3555 __gitcomp "--message= --file= --revision= $cmt_opts"
3561 __gitcomp "--dry-run --message --tag"
3564 __gitcomp "--dry-run --message"
3567 __gitcomp "--git-format"
3571 --config-dir= --ignore-paths= --minimize
3572 --no-auth-cache --username=
3576 __gitcomp "--revision= --parent"
3586 local i c="$__git_cmd_idx" f=0
3587 while [ $c -lt $cword ]; do
3590 -d|--delete|-v|--verify)
3591 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3606 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3616 __gitcomp_builtin tag
3626 __git_complete_worktree_paths ()
3629 # Generate completion reply from worktree list skipping the first
3630 # entry: it's the path of the main worktree, which can't be moved,
3631 # removed, locked, etc.
3632 __gitcomp_nl "$(__git worktree list --porcelain |
3633 sed -n -e '2,$ s/^worktree //p')"
3638 local subcommands="add list lock move prune remove unlock"
3639 local subcommand subcommand_idx
3641 subcommand="$(__git_find_on_cmdline --show-idx "$subcommands")"
3642 subcommand_idx="${subcommand% *}"
3643 subcommand="${subcommand#* }"
3645 case "$subcommand,$cur" in
3647 __gitcomp "$subcommands"
3650 __gitcomp_builtin worktree_$subcommand
3652 add,*) # usage: git worktree add [<options>] <path> [<commit-ish>]
3653 # Here we are not completing an --option, it's either the
3656 -b|-B) # Complete refs for branch to be created/reseted.
3659 -*) # The previous word is an -o|--option without an
3660 # unstuck argument: have to complete the path for
3661 # the new worktree, so don't list anything, but let
3662 # Bash fall back to filename completion.
3664 *) # The previous word is not an --option, so it must
3665 # be either the 'add' subcommand, the unstuck
3666 # argument of an option (e.g. branch for -b|-B), or
3667 # the path for the new worktree.
3668 if [ $cword -eq $((subcommand_idx+1)) ]; then
3669 # Right after the 'add' subcommand: have to
3670 # complete the path, so fall back to Bash
3671 # filename completion.
3674 case "${words[cword-2]}" in
3675 -b|-B) # After '-b <branch>': have to
3676 # complete the path, so fall back
3677 # to Bash filename completion.
3679 *) # After the path: have to complete
3680 # the ref to be checked out.
3688 lock,*|remove,*|unlock,*)
3689 __git_complete_worktree_paths
3692 if [ $cword -eq $((subcommand_idx+1)) ]; then
3693 # The first parameter must be an existing working
3695 __git_complete_worktree_paths
3697 # The second parameter is the destination: it could
3698 # be any path, so don't list anything, but let Bash
3699 # fall back to filename completion.
3706 __git_complete_common () {
3711 __gitcomp_builtin "$command"
3716 __git_cmds_with_parseopt_helper=
3717 __git_support_parseopt_helper () {
3718 test -n "$__git_cmds_with_parseopt_helper" ||
3719 __git_cmds_with_parseopt_helper="$(__git --list-cmds=parseopt)"
3721 case " $__git_cmds_with_parseopt_helper " in
3731 __git_have_func () {
3732 declare -f -- "$1" >/dev/null 2>&1
3735 __git_complete_command () {
3737 local completion_func="_git_${command//-/_}"
3738 if ! __git_have_func $completion_func &&
3739 __git_have_func _completion_loader
3741 _completion_loader "git-$command"
3743 if __git_have_func $completion_func
3747 elif __git_support_parseopt_helper "$command"
3749 __git_complete_common "$command"
3758 local i c=1 command __git_dir __git_repo_path
3759 local __git_C_args C_args_count=0
3762 while [ $c -lt $cword ]; do
3766 __git_dir="${i#--git-dir=}"
3770 __git_dir="${words[c]}"
3779 -c|--work-tree|--namespace)
3783 __git_C_args[C_args_count++]=-C
3785 __git_C_args[C_args_count++]="${words[c]}"
3798 if [ -z "${command-}" ]; then
3800 --git-dir|-C|--work-tree)
3801 # these need a path argument, let's fall back to
3802 # Bash filename completion
3806 __git_complete_config_variable_name_and_value
3810 # we don't support completing these options' arguments
3829 --no-replace-objects
3834 if test -n "${GIT_TESTING_PORCELAIN_COMMAND_LIST-}"
3836 __gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
3838 local list_cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config
3840 if test "${GIT_COMPLETION_SHOW_ALL_COMMANDS-}" = "1"
3842 list_cmds=builtins,$list_cmds
3844 __gitcomp "$(__git --list-cmds=$list_cmds)"
3851 __git_complete_command "$command" && return
3853 local expansion=$(__git_aliased_command "$command")
3854 if [ -n "$expansion" ]; then
3856 __git_complete_command "$expansion"
3862 __git_has_doubledash && return
3864 local __git_repo_path
3865 __git_find_repo_path
3868 if __git_pseudoref_exists MERGE_HEAD; then
3874 $__git_log_common_options
3875 $__git_log_gitk_options
3881 __git_complete_revlist
3884 if [[ -n ${ZSH_VERSION-} && -z ${GIT_SOURCING_ZSH_COMPLETION-} ]]; then
3885 echo "ERROR: this script is obsolete, please see git-completion.zsh" 1>&2
3891 local cur words cword prev
3892 local __git_cmd_idx=0
3893 _get_comp_words_by_ref -n =: cur words cword prev
3899 local wrapper="__git_wrap${2}"
3900 eval "$wrapper () { __git_func_wrap $2 ; }"
3901 complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
3902 || complete -o default -o nospace -F $wrapper $1
3905 # Setup the completion for git commands
3906 # 1: command or alias
3907 # 2: function to call (e.g. `git
`, `gitk
`, `git_fetch
`)
3912 if __git_have_func $2; then
3914 elif __git_have_func __$2_main; then
3916 elif __git_have_func _$2; then
3919 echo "ERROR: could not find function '$2'" 1>&2
3922 ___git_complete $1 $func
3925 ___git_complete git __git_main
3926 ___git_complete gitk __gitk_main
3928 # The following are necessary only for Cygwin, and only are needed
3929 # when the user has tab-completed the executable name and consequently
3930 # included the '.exe' suffix.
3932 if [ "$OSTYPE" = cygwin ]; then
3933 ___git_complete git.exe __git_main