]>
git.ipfire.org Git - thirdparty/git.git/blob - contrib/completion/git-completion.bash
e3d88b06721b3910faf5bd4bfb3f14ebca46b870
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 # Prints the number of slash-separated components in a path.
238 # 1: Path to count components of.
239 __git_count_path_components ()
242 local relative="${path#/}"
243 relative="${relative%/}"
244 local slashes="/${relative//[^\/]}"
248 # The following function is based on code from:
250 # bash_completion - programmable completion functions for bash 3.2+
252 # Copyright © 2006-2008, Ian Macdonald <ian@caliban.org>
253 # © 2009-2010, Bash Completion Maintainers
254 # <bash-completion-devel@lists.alioth.debian.org>
256 # This program is free software; you can redistribute it and/or modify
257 # it under the terms of the GNU General Public License as published by
258 # the Free Software Foundation; either version 2, or (at your option)
261 # This program is distributed in the hope that it will be useful,
262 # but WITHOUT ANY WARRANTY; without even the implied warranty of
263 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
264 # GNU General Public License for more details.
266 # You should have received a copy of the GNU General Public License
267 # along with this program; if not, see <http://www.gnu.org/licenses/>.
269 # The latest version of this software can be obtained here:
271 # http://bash-completion.alioth.debian.org/
275 # This function can be used to access a tokenized list of words
276 # on the command line:
278 # __git_reassemble_comp_words_by_ref '=:'
279 # if test "${words_[cword_-1]}" = -w
284 # The argument should be a collection of characters from the list of
285 # word completion separators (COMP_WORDBREAKS) to treat as ordinary
288 # This is roughly equivalent to going back in time and setting
289 # COMP_WORDBREAKS to exclude those characters. The intent is to
290 # make option types like --date=<type> and <rev>:<path> easy to
291 # recognize by treating each shell word as a single token.
293 # It is best not to set COMP_WORDBREAKS directly because the value is
294 # shared with other completion scripts. By the time the completion
295 # function gets called, COMP_WORDS has already been populated so local
296 # changes to COMP_WORDBREAKS have no effect.
298 # Output: words_, cword_, cur_.
300 __git_reassemble_comp_words_by_ref()
302 local exclude i j first
303 # Which word separators to exclude?
304 exclude="${1//[^$COMP_WORDBREAKS]}"
306 if [ -z "$exclude" ]; then
307 words_=("${COMP_WORDS[@]}")
310 # List of word completion separators has shrunk;
311 # re-assemble words to complete.
312 for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
313 # Append each nonempty word consisting of just
314 # word separator characters to the current word.
318 [ -n "${COMP_WORDS[$i]}" ] &&
319 # word consists of excluded word separators
320 [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
322 # Attach to the previous token,
323 # unless the previous token is the command name.
324 if [ $j -ge 2 ] && [ -n "$first" ]; then
328 words_[$j]=${words_[j]}${COMP_WORDS[i]}
329 if [ $i = $COMP_CWORD ]; then
332 if (($i < ${#COMP_WORDS[@]} - 1)); then
339 words_[$j]=${words_[j]}${COMP_WORDS[i]}
340 if [ $i = $COMP_CWORD ]; then
346 if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
347 _get_comp_words_by_ref ()
349 local exclude cur_ words_ cword_
350 if [ "$1" = "-n" ]; then
354 __git_reassemble_comp_words_by_ref "$exclude"
355 cur_=${words_[cword_]}
356 while [ $# -gt 0 ]; do
362 prev=${words_[$cword_-1]}
365 words=("${words_[@]}")
376 # Fills the COMPREPLY array with prefiltered words without any additional
378 # Callers must take care of providing only words that match the current word
379 # to be completed and adding any prefix and/or suffix (trailing space!), if
381 # 1: List of newline-separated matching completion words, complete with
390 # Similar to __gitcomp_direct, but appends to COMPREPLY instead.
391 # Callers must take care of providing only words that match the current word
392 # to be completed and adding any prefix and/or suffix (trailing space!), if
394 # 1: List of newline-separated matching completion words, complete with
396 __gitcomp_direct_append ()
405 local x i=${#COMPREPLY[@]}
407 if [[ "$x" == "$3"* ]]; then
408 COMPREPLY[i++]="$2$x$4"
419 # Generates completion reply, appending a space to possible completion words,
421 # It accepts 1 to 4 arguments:
422 # 1: List of possible completion words.
423 # 2: A prefix to be added to each possible completion word (optional).
424 # 3: Generate possible completion matches for this word (optional).
425 # 4: A suffix to be appended to each possible completion word (optional).
428 local cur_="${3-$cur}"
434 local c i=0 IFS=$' \t\n'
436 if [[ $c == "--" ]]; then
440 if [[ $c == "$cur_"* ]]; then
445 COMPREPLY[i++]="${2-}$c"
450 local c i=0 IFS=$' \t\n'
452 if [[ $c == "--" ]]; then
454 if [[ $c == "$cur_"* ]]; then
455 COMPREPLY[i++]="${2-}$c "
460 if [[ $c == "$cur_"* ]]; then
465 COMPREPLY[i++]="${2-}$c"
472 # Clear the variables caching builtins' options when (re-)sourcing
473 # the completion script.
474 if [[ -n ${ZSH_VERSION-} ]]; then
475 unset ${(M)${(k)parameters[@]}:#__gitcomp_builtin_*} 2>/dev/null
477 unset $(compgen -v __gitcomp_builtin_)
480 # This function is equivalent to
482 # ___git_resolved_builtins=$(git xxx --git-completion-helper)
484 # except that the result of the execution is cached.
486 # Accept 1-3 arguments:
487 # 1: the git command to execute, this is also the cache key
488 # (use "_" when the command contains spaces, e.g. "remote add"
489 # becomes "remote_add")
490 # 2: extra options to be added on top (e.g. negative forms)
491 # 3: options to be excluded
492 __git_resolve_builtins ()
498 local var=__gitcomp_builtin_"${cmd//-/_}"
500 eval "options=\${$var-}"
502 if [ -z "$options" ]; then
503 local completion_helper
504 if [ "${GIT_COMPLETION_SHOW_ALL-}" = "1" ]; then
505 completion_helper="--git-completion-helper-all"
507 completion_helper="--git-completion-helper"
509 # leading and trailing spaces are significant to make
510 # option removal work correctly.
511 options=" $incl $(__git ${cmd/_/ } $completion_helper) " || return
514 options="${options/ $i / }"
516 eval "$var=\"$options\""
519 ___git_resolved_builtins="$options"
522 # This function is equivalent to
524 # __gitcomp "$(git xxx --git-completion-helper) ..."
526 # except that the output is cached. Accept 1-3 arguments:
527 # 1: the git command to execute, this is also the cache key
528 # (use "_" when the command contains spaces, e.g. "remote add"
529 # becomes "remote_add")
530 # 2: extra options to be added on top (e.g. negative forms)
531 # 3: options to be excluded
534 __git_resolve_builtins "$1" "$2" "$3"
536 __gitcomp "$___git_resolved_builtins"
539 # Variation of __gitcomp_nl () that appends to the existing list of
540 # completion candidates, COMPREPLY.
541 __gitcomp_nl_append ()
544 __gitcompappend "$1" "${2-}" "${3-$cur}" "${4- }"
547 # Generates completion reply from newline-separated possible completion words
548 # by appending a space to all of them.
549 # It accepts 1 to 4 arguments:
550 # 1: List of possible completion words, separated by a single newline.
551 # 2: A prefix to be added to each possible completion word (optional).
552 # 3: Generate possible completion matches for this word (optional).
553 # 4: A suffix to be appended to each possible completion word instead of
554 # the default space (optional). If specified but empty, nothing is
559 __gitcomp_nl_append "$@"
562 # Fills the COMPREPLY array with prefiltered paths without any additional
564 # Callers must take care of providing only paths that match the current path
565 # to be completed and adding any prefix path components, if necessary.
566 # 1: List of newline-separated matching paths, complete with all prefix
568 __gitcomp_file_direct ()
574 # use a hack to enable file mode in bash < 4
575 compopt -o filenames +o nospace 2>/dev/null ||
576 compgen -f /non-existing-dir/ >/dev/null ||
580 # Generates completion reply with compgen from newline-separated possible
581 # completion filenames.
582 # It accepts 1 to 3 arguments:
583 # 1: List of possible completion filenames, separated by a single newline.
584 # 2: A directory prefix to be added to each possible completion filename
586 # 3: Generate possible completion matches for this word (optional).
591 # XXX does not work when the directory prefix contains a tilde,
592 # since tilde expansion is not applied.
593 # This means that COMPREPLY will be empty and Bash default
594 # completion will be used.
595 __gitcompadd "$1" "${2-}" "${3-$cur}" ""
597 # use a hack to enable file mode in bash < 4
598 compopt -o filenames +o nospace 2>/dev/null ||
599 compgen -f /non-existing-dir/ >/dev/null ||
603 # Find the current subcommand for commands that follow the syntax:
605 # git <command> <subcommand>
607 # 1: List of possible subcommands.
608 # 2: Optional subcommand to return when none is found.
609 __git_find_subcommand ()
611 local subcommand subcommands="$1" default_subcommand="$2"
613 for subcommand in $subcommands; do
614 if [ "$subcommand" = "${words[__git_cmd_idx+1]}" ]; then
620 echo $default_subcommand
623 # Execute 'git ls-files', unless the --committable option is specified, in
624 # which case it runs 'git diff-index' to find out the files that can be
625 # committed. It return paths relative to the directory specified in the first
626 # argument, and using the options specified in the second argument.
627 __git_ls_files_helper ()
629 if [ "$2" = "--committable" ]; then
630 __git -C "$1" -c core.quotePath=false diff-index \
631 --name-only --relative HEAD -- "${3//\\/\\\\}*"
633 # NOTE: $2 is not quoted in order to support multiple options
634 __git -C "$1" -c core.quotePath=false ls-files \
635 --exclude-standard $2 -- "${3//\\/\\\\}*"
640 # __git_index_files accepts 1 or 2 arguments:
641 # 1: Options to pass to ls-files (required).
642 # 2: A directory path (optional).
643 # If provided, only files within the specified directory are listed.
644 # Sub directories are never recursed. Path must have a trailing
646 # 3: List only paths matching this path component (optional).
649 local root="$2" match="$3"
651 __git_ls_files_helper "$root" "$1" "${match:-?}" |
652 awk -F / -v pfx="${2//\\/\\\\}" '{
657 if (substr(p, 1, 1) != "\"") {
658 # No special characters, easy!
663 # The path is quoted.
668 # Even when a directory name itself does not contain
669 # any special characters, it will still be quoted if
670 # any of its (stripped) trailing path components do.
671 # Because of this we may have seen the same directory
672 # both quoted and unquoted.
674 # We have seen the same directory unquoted,
681 function dequote(p, bs_idx, out, esc, esc_idx, dec) {
682 # Skip opening double quote.
685 # Interpret backslash escape sequences.
686 while ((bs_idx = index(p, "\\")) != 0) {
687 out = out substr(p, 1, bs_idx - 1)
688 esc = substr(p, bs_idx + 1, 1)
689 p = substr(p, bs_idx + 2)
691 if ((esc_idx = index("abtvfr\"\\", esc)) != 0) {
692 # C-style one-character escape sequence.
693 out = out substr("\a\b\t\v\f\r\"\\",
695 } else if (esc == "n") {
696 # Uh-oh, a newline character.
697 # We cannot reliably put a pathname
698 # containing a newline into COMPREPLY,
699 # and the newline would create a mess.
703 # Must be a \nnn octal value, then.
705 substr(p, 1, 1) * 8 + \
707 out = out sprintf("%c", dec)
711 # Drop closing double quote, if there is one.
712 # (There is not any if this is a directory, as it was
713 # already stripped with the trailing path components.)
714 if (substr(p, length(p), 1) == "\"")
715 out = out substr(p, 1, length(p) - 1)
723 # __git_complete_index_file requires 1 argument:
724 # 1: the options to pass to ls-file
726 # The exception is --committable, which finds the files appropriate commit.
727 __git_complete_index_file ()
729 local dequoted_word pfx="" cur_
733 case "$dequoted_word" in
735 pfx="${dequoted_word%/*}/"
736 cur_="${dequoted_word##*/}"
739 cur_="$dequoted_word"
742 __gitcomp_file_direct "$(__git_index_files "$1" "$pfx" "$cur_")"
745 # Lists branches from the local repository.
746 # 1: A prefix to be added to each listed branch (optional).
747 # 2: List only branches matching this word (optional; list all branches if
749 # 3: A suffix to be appended to each listed branch (optional).
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/heads/$cur_*" "refs/heads/$cur_*/**"
759 # Lists branches from remote repositories.
760 # 1: A prefix to be added to each listed branch (optional).
761 # 2: List only branches matching this word (optional; list all branches if
763 # 3: A suffix to be appended to each listed branch (optional).
764 __git_remote_heads ()
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/remotes/$cur_*" "refs/remotes/$cur_*/**"
773 # Lists tags from the local repository.
774 # Accepts the same positional parameters as __git_heads() above.
777 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
779 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
780 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
781 "refs/tags/$cur_*" "refs/tags/$cur_*/**"
784 # List unique branches from refs/remotes used for 'git checkout' and 'git
785 # switch' tracking DWIMery.
786 # 1: A prefix to be added to each listed branch (optional)
787 # 2: List only branches matching this word (optional; list all branches if
789 # 3: A suffix to be appended to each listed branch (optional).
790 __git_dwim_remote_heads ()
792 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
794 # employ the heuristic used by git checkout and git switch
795 # Try to find a remote branch that cur_es the completion word
796 # but only output if the branch name is unique
798 function casemap(s) {
799 if (ENVIRON["IGNORE_CASE"])
805 split(ENVIRON["REMOTES"], remotes, /\n/)
807 remotes[i] = "refs/remotes/" casemap(remotes[i])
808 cur_ = casemap(ENVIRON["CUR_"])
811 ref_case = casemap($0)
813 if (index(ref_case, remotes[i] "/" cur_) == 1) {
814 branch = substr($0, length(remotes[i] "/") + 1)
815 print ENVIRON["PFX"] branch ENVIRON["SFX"]
821 __git for-each-ref --format='%(refname)' refs/remotes/ |
822 PFX="$pfx" SFX="$sfx" CUR_="$cur_" \
823 IGNORE_CASE=${GIT_COMPLETION_IGNORE_CASE+1} \
824 REMOTES="$(__git_remotes | sort -r)" awk "$awk_script" |
828 # Lists refs from the local (by default) or from a remote repository.
829 # It accepts 0, 1 or 2 arguments:
830 # 1: The remote to list refs from (optional; ignored, if set but empty).
831 # Can be the name of a configured remote, a path, or a URL.
832 # 2: In addition to local refs, list unique branches from refs/remotes/ for
833 # 'git checkout's tracking DWIMery (optional; ignored, if set but empty).
834 # 3: A prefix to be added to each listed ref (optional).
835 # 4: List only refs matching this word (optional; list all refs if unset or
837 # 5: A suffix to be appended to each listed ref (optional; ignored, if set
840 # Use __git_complete_refs() instead.
843 local i hash dir track="${2-}"
844 local list_refs_from=path remote="${1-}"
846 local pfx="${3-}" cur_="${4-$cur}" sfx="${5-}"
849 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
852 dir="$__git_repo_path"
854 if [ -z "$remote" ]; then
855 if [ -z "$dir" ]; then
859 if __git_is_configured_remote "$remote"; then
860 # configured remote takes precedence over a
861 # local directory with the same name
862 list_refs_from=remote
863 elif [ -d "$remote/.git" ]; then
865 elif [ -d "$remote" ]; then
872 if test "${GIT_COMPLETION_IGNORE_CASE:+1}" = "1"
874 # uppercase with tr instead of ${match,^^} for bash 3.2 compatibility
875 umatch=$(echo "$match" | tr a-z A-Z 2>/dev/null || echo "$match")
878 if [ "$list_refs_from" = path ]; then
879 if [[ "$cur_" == ^* ]]; then
889 refs=("$match*" "$match*/**")
893 for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD REBASE_HEAD CHERRY_PICK_HEAD REVERT_HEAD BISECT_HEAD AUTO_MERGE; do
896 if [ -e "$dir/$i" ]; then
902 format="refname:strip=2"
903 refs=("refs/tags/$match*" "refs/tags/$match*/**"
904 "refs/heads/$match*" "refs/heads/$match*/**"
905 "refs/remotes/$match*" "refs/remotes/$match*/**")
908 __git_dir="$dir" __git for-each-ref --format="$fer_pfx%($format)$sfx" \
909 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
911 if [ -n "$track" ]; then
912 __git_dwim_remote_heads "$pfx" "$match" "$sfx"
918 __git ls-remote "$remote" "$match*" | \
919 while read -r hash i; do
922 *) echo "$pfx$i$sfx" ;;
927 if [ "$list_refs_from" = remote ]; then
929 $match*|$umatch*) echo "${pfx}HEAD$sfx" ;;
931 local strip="$(__git_count_path_components "refs/remotes/$remote")"
932 __git for-each-ref --format="$fer_pfx%(refname:strip=$strip)$sfx" \
933 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
934 "refs/remotes/$remote/$match*" \
935 "refs/remotes/$remote/$match*/**"
939 $match*|$umatch*) query_symref="HEAD" ;;
941 __git ls-remote "$remote" $query_symref \
942 "refs/tags/$match*" "refs/heads/$match*" \
943 "refs/remotes/$match*" |
944 while read -r hash i; do
947 refs/*) echo "$pfx${i#refs/*/}$sfx" ;;
948 *) echo "$pfx$i$sfx" ;; # symbolic refs
956 # Completes refs, short and long, local and remote, symbolic and pseudo.
958 # Usage: __git_complete_refs [<option>]...
959 # --remote=<remote>: The remote to list refs from, can be the name of a
960 # configured remote, a path, or a URL.
961 # --dwim: List unique remote branches for 'git switch's tracking DWIMery.
962 # --pfx=<prefix>: A prefix to be added to each ref.
963 # --cur=<word>: The current ref to be completed. Defaults to the current
964 # word to be completed.
965 # --sfx=<suffix>: A suffix to be appended to each ref instead of the default
967 # --mode=<mode>: What set of refs to complete, one of 'refs' (the default) to
968 # complete all refs, 'heads' to complete only branches, or
969 # 'remote-heads' to complete only remote branches. Note that
970 # --remote is only compatible with --mode=refs.
971 __git_complete_refs ()
973 local remote= dwim= pfx= cur_="$cur" sfx=" " mode="refs"
975 while test $# != 0; do
977 --remote=*) remote="${1##--remote=}" ;;
978 --dwim) dwim="yes" ;;
979 # --track is an old spelling of --dwim
980 --track) dwim="yes" ;;
981 --pfx=*) pfx="${1##--pfx=}" ;;
982 --cur=*) cur_="${1##--cur=}" ;;
983 --sfx=*) sfx="${1##--sfx=}" ;;
984 --mode=*) mode="${1##--mode=}" ;;
990 # complete references based on the specified mode
993 __gitcomp_direct "$(__git_refs "$remote" "" "$pfx" "$cur_" "$sfx")" ;;
995 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" "$sfx")" ;;
997 __gitcomp_direct "$(__git_remote_heads "$pfx" "$cur_" "$sfx")" ;;
1002 # Append DWIM remote branch names if requested
1003 if [ "$dwim" = "yes" ]; then
1004 __gitcomp_direct_append "$(__git_dwim_remote_heads "$pfx" "$cur_" "$sfx")"
1008 # __git_refs2 requires 1 argument (to pass to __git_refs)
1009 # Deprecated: use __git_complete_fetch_refspecs() instead.
1013 for i in $(__git_refs "$1"); do
1018 # Completes refspecs for fetching from a remote repository.
1019 # 1: The remote repository.
1020 # 2: A prefix to be added to each listed refspec (optional).
1021 # 3: The ref to be completed as a refspec instead of the current word to be
1022 # completed (optional)
1023 # 4: A suffix to be appended to each listed refspec instead of the default
1025 __git_complete_fetch_refspecs ()
1027 local i remote="$1" pfx="${2-}" cur_="${3-$cur}" sfx="${4- }"
1029 __gitcomp_direct "$(
1030 for i in $(__git_refs "$remote" "" "" "$cur_") ; do
1031 echo "$pfx$i:$i$sfx"
1036 # __git_refs_remotes requires 1 argument (to pass to ls-remote)
1037 __git_refs_remotes ()
1040 __git ls-remote "$1" 'refs/heads/*' | \
1041 while read -r hash i; do
1042 echo "$i:refs/remotes/$1/${i#refs/heads/}"
1048 __git_find_repo_path
1049 test -d "$__git_repo_path/remotes" && ls -1 "$__git_repo_path/remotes"
1053 # Returns true if $1 matches the name of a configured remote, false otherwise.
1054 __git_is_configured_remote ()
1057 for remote in $(__git_remotes); do
1058 if [ "$remote" = "$1" ]; then
1065 __git_list_merge_strategies ()
1067 LANG=C LC_ALL=C git merge -s help 2>&1 |
1068 sed -n -e '/[Aa]vailable strategies are: /,/^$/{
1077 __git_merge_strategies=
1078 # 'git merge -s help' (and thus detection of the merge strategy
1079 # list) fails, unfortunately, if run outside of any git working
1080 # tree. __git_merge_strategies is set to the empty string in
1081 # that case, and the detection will be repeated the next time it
1083 __git_compute_merge_strategies ()
1085 test -n "$__git_merge_strategies" ||
1086 __git_merge_strategies=$(__git_list_merge_strategies)
1089 __git_merge_strategy_options="ours theirs subtree subtree= patience
1090 histogram diff-algorithm= ignore-space-change ignore-all-space
1091 ignore-space-at-eol renormalize no-renormalize no-renames
1092 find-renames find-renames= rename-threshold="
1094 __git_complete_revlist_file ()
1096 local dequoted_word pfx ls ref cur_="$cur"
1105 __git_dequote "$cur_"
1107 case "$dequoted_word" in
1109 pfx="${dequoted_word%/*}"
1110 cur_="${dequoted_word##*/}"
1115 cur_="$dequoted_word"
1120 case "$COMP_WORDBREAKS" in
1122 *) pfx="$ref:$pfx" ;;
1125 __gitcomp_file "$(__git ls-tree "$ls" \
1131 pfx="${cur_%...*}..."
1133 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1138 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1146 __git_complete_file ()
1148 __git_complete_revlist_file
1151 __git_complete_revlist ()
1153 __git_complete_revlist_file
1156 __git_complete_remote_or_refspec ()
1158 local cur_="$cur" cmd="${words[__git_cmd_idx]}"
1159 local i c=$((__git_cmd_idx+1)) remote="" pfx="" lhs=1 no_complete_refspec=0
1160 if [ "$cmd" = "remote" ]; then
1163 while [ $c -lt $cword ]; do
1166 --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
1167 -d|--delete) [ "$cmd" = "push" ] && lhs=0 ;;
1170 push) no_complete_refspec=1 ;;
1177 --multiple) no_complete_refspec=1; break ;;
1179 *) remote="$i"; break ;;
1183 if [ -z "$remote" ]; then
1184 __gitcomp_nl "$(__git_remotes)"
1187 if [ $no_complete_refspec = 1 ]; then
1190 [ "$remote" = "." ] && remote=
1193 case "$COMP_WORDBREAKS" in
1195 *) pfx="${cur_%%:*}:" ;;
1207 if [ $lhs = 1 ]; then
1208 __git_complete_fetch_refspecs "$remote" "$pfx" "$cur_"
1210 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1214 if [ $lhs = 1 ]; then
1215 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
1217 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1221 if [ $lhs = 1 ]; then
1222 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1224 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
1230 __git_complete_strategy ()
1232 __git_compute_merge_strategies
1235 __gitcomp "$__git_merge_strategies"
1239 __gitcomp "$__git_merge_strategy_options"
1245 __gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
1248 --strategy-option=*)
1249 __gitcomp "$__git_merge_strategy_options" "" "${cur##--strategy-option=}"
1257 __git_compute_all_commands ()
1259 test -n "$__git_all_commands" ||
1260 __git_all_commands=$(__git --list-cmds=main,others,alias,nohelpers)
1263 # Lists all set config variables starting with the given section prefix,
1264 # with the prefix removed.
1265 __git_get_config_variables ()
1267 local section="$1" i IFS=$'\n'
1268 for i in $(__git config --name-only --get-regexp "^$section\..*"); do
1269 echo "${i#$section.}"
1273 __git_pretty_aliases ()
1275 __git_get_config_variables "pretty"
1278 # __git_aliased_command requires 1 argument
1279 __git_aliased_command ()
1281 local cur=$1 last list= word cmdline
1283 while [[ -n "$cur" ]]; do
1284 if [[ "$list" == *" $cur "* ]]; then
1289 cmdline=$(__git config --get "alias.$cur")
1294 for word in $cmdline; do
1300 \!*) : shell command alias ;;
1302 *=*) : setting env ;;
1303 git) : git itself ;;
1304 \(\)) : skip parens of shell function definition ;;
1305 {) : skip start of shell helper function ;;
1306 :) : skip null command ;;
1307 \'*) : skip opening quote after sh -c ;;
1316 if [[ "$cur" != "$1" ]]; then
1321 # Check whether one of the given words is present on the command line,
1322 # and print the first word found.
1324 # Usage: __git_find_on_cmdline [<option>]... "<wordlist>"
1325 # --show-idx: Optionally show the index of the found word in the $words array.
1326 __git_find_on_cmdline ()
1328 local word c="$__git_cmd_idx" show_idx
1330 while test $# -gt 1; do
1332 --show-idx) show_idx=y ;;
1339 while [ $c -lt $cword ]; do
1340 for word in $wordlist; do
1341 if [ "$word" = "${words[c]}" ]; then
1342 if [ -n "${show_idx-}" ]; then
1354 # Similar to __git_find_on_cmdline, except that it loops backwards and thus
1355 # prints the *last* word found. Useful for finding which of two options that
1356 # supersede each other came last, such as "--guess" and "--no-guess".
1358 # Usage: __git_find_last_on_cmdline [<option>]... "<wordlist>"
1359 # --show-idx: Optionally show the index of the found word in the $words array.
1360 __git_find_last_on_cmdline ()
1362 local word c=$cword show_idx
1364 while test $# -gt 1; do
1366 --show-idx) show_idx=y ;;
1373 while [ $c -gt "$__git_cmd_idx" ]; do
1375 for word in $wordlist; do
1376 if [ "$word" = "${words[c]}" ]; then
1377 if [ -n "$show_idx" ]; then
1388 # Echo the value of an option set on the command line or config
1390 # $1: short option name
1391 # $2: long option name including =
1392 # $3: list of possible values
1393 # $4: config string (optional)
1396 # result="$(__git_get_option_value "-d" "--do-something=" \
1397 # "yes no" "core.doSomething")"
1399 # result is then either empty (no option set) or "yes" or "no"
1401 # __git_get_option_value requires 3 arguments
1402 __git_get_option_value ()
1404 local c short_opt long_opt val
1405 local result= values config_key word
1413 while [ $c -ge 0 ]; do
1415 for val in $values; do
1416 if [ "$short_opt$val" = "$word" ] ||
1417 [ "$long_opt$val" = "$word" ]; then
1425 if [ -n "$config_key" ] && [ -z "$result" ]; then
1426 result="$(__git config "$config_key")"
1432 __git_has_doubledash ()
1435 while [ $c -lt $cword ]; do
1436 if [ "--" = "${words[c]}" ]; then
1444 # Try to count non option arguments passed on the command line for the
1445 # specified git command.
1446 # When options are used, it is necessary to use the special -- option to
1447 # tell the implementation were non option arguments begin.
1448 # XXX this can not be improved, since options can appear everywhere, as
1452 # __git_count_arguments requires 1 argument: the git command executed.
1453 __git_count_arguments ()
1457 # Skip "git" (first argument)
1458 for ((i=$__git_cmd_idx; i < ${#words[@]}; i++)); do
1463 # Good; we can assume that the following are only non
1468 # Skip the specified git command and discard git
1481 __git_whitespacelist="nowarn warn error error-all fix"
1482 __git_patchformat="mbox stgit stgit-series hg mboxrd"
1483 __git_showcurrentpatch="diff raw"
1484 __git_am_inprogress_options="--skip --continue --resolved --abort --quit --show-current-patch"
1485 __git_quoted_cr="nowarn warn strip"
1489 __git_find_repo_path
1490 if [ -d "$__git_repo_path"/rebase-apply ]; then
1491 __gitcomp "$__git_am_inprogress_options"
1496 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1500 __gitcomp "$__git_patchformat" "" "${cur##--patch-format=}"
1503 --show-current-patch=*)
1504 __gitcomp "$__git_showcurrentpatch" "" "${cur##--show-current-patch=}"
1508 __gitcomp "$__git_quoted_cr" "" "${cur##--quoted-cr=}"
1512 __gitcomp_builtin am "" \
1513 "$__git_am_inprogress_options"
1522 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1526 __gitcomp_builtin apply
1535 __gitcomp "+x -x" "" "${cur##--chmod=}"
1539 __gitcomp_builtin add
1543 local complete_opt="--others --modified --directory --no-empty-directory"
1544 if test -n "$(__git_find_on_cmdline "-u --update")"
1546 complete_opt="--modified"
1548 __git_complete_index_file "$complete_opt"
1555 __gitcomp "$(git archive --list)" "" "${cur##--format=}"
1559 __gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
1563 __gitcomp_builtin archive "--format= --list --verbose --prefix= --worktree-attributes"
1572 __git_has_doubledash && return
1574 __git_find_repo_path
1576 # If a bisection is in progress get the terms being used.
1577 local term_bad term_good
1578 if [ -f "$__git_repo_path"/BISECT_TERMS ]; then
1579 term_bad=$(__git bisect terms --term-bad)
1580 term_good=$(__git bisect terms --term-good)
1583 # We will complete any custom terms, but still always complete the
1584 # more usual bad/new/good/old because git bisect gives a good error
1585 # message if these are given when not in use, and that's better than
1586 # silent refusal to complete if the user is confused.
1588 # We want to recognize 'view' but not complete it, because it overlaps
1589 # with 'visualize' too much and is just an alias for it.
1591 local completable_subcommands="start bad new $term_bad good old $term_good terms skip reset visualize replay log run help"
1592 local all_subcommands="$completable_subcommands view"
1594 local subcommand="$(__git_find_on_cmdline "$all_subcommands")"
1596 if [ -z "$subcommand" ]; then
1597 __git_find_repo_path
1598 if [ -f "$__git_repo_path"/BISECT_START ]; then
1599 __gitcomp "$completable_subcommands"
1601 __gitcomp "replay start"
1606 case "$subcommand" in
1610 __gitcomp "--first-parent --no-checkout --term-new --term-bad --term-old --term-good"
1619 __gitcomp "--term-good --term-old --term-bad --term-new"
1623 __git_complete_log_opts
1626 bad|new|"$term_bad"|good|old|"$term_good"|reset|skip)
1634 __git_ref_fieldlist="refname objecttype objectsize objectname upstream push HEAD symref"
1638 local i c="$__git_cmd_idx" only_local_ref="n" has_r="n"
1640 while [ $c -lt $cword ]; do
1643 -d|-D|--delete|-m|-M|--move|-c|-C|--copy)
1644 only_local_ref="y" ;;
1652 --set-upstream-to=*)
1653 __git_complete_refs --cur="${cur##--set-upstream-to=}"
1656 __gitcomp_builtin branch
1659 if [ $only_local_ref = "y" -a $has_r = "n" ]; then
1660 __gitcomp_direct "$(__git_heads "" "$cur" " ")"
1670 local cmd="${words[__git_cmd_idx+1]}"
1672 $((__git_cmd_idx+1)))
1673 __gitcomp "create list-heads verify unbundle"
1675 $((__git_cmd_idx+2)))
1676 # looking for a file
1681 __git_complete_revlist
1688 # Helper function to decide whether or not we should enable DWIM logic for
1689 # git-switch and git-checkout.
1691 # To decide between the following rules in decreasing priority order:
1692 # - the last provided of "--guess" or "--no-guess" explicitly enable or
1693 # disable completion of DWIM logic respectively.
1694 # - If checkout.guess is false, disable completion of DWIM logic.
1695 # - If the --no-track option is provided, take this as a hint to disable the
1696 # DWIM completion logic
1697 # - If GIT_COMPLETION_CHECKOUT_NO_GUESS is set, disable the DWIM completion
1698 # logic, as requested by the user.
1699 # - Enable DWIM logic otherwise.
1701 __git_checkout_default_dwim_mode ()
1703 local last_option dwim_opt="--dwim"
1705 if [ "${GIT_COMPLETION_CHECKOUT_NO_GUESS-}" = "1" ]; then
1709 # --no-track disables DWIM, but with lower priority than
1710 # --guess/--no-guess/checkout.guess
1711 if [ -n "$(__git_find_on_cmdline "--no-track")" ]; then
1715 # checkout.guess = false disables DWIM, but with lower priority than
1716 # --guess/--no-guess
1717 if [ "$(__git config --type=bool checkout.guess)" = "false" ]; then
1721 # Find the last provided --guess or --no-guess
1722 last_option="$(__git_find_last_on_cmdline "--guess --no-guess")"
1723 case "$last_option" in
1737 __git_has_doubledash && return
1739 local dwim_opt="$(__git_checkout_default_dwim_mode)"
1743 # Complete local branches (and DWIM branch
1744 # remote branch names) for an option argument
1745 # specifying a new branch name. This is for
1746 # convenience, assuming new branches are
1747 # possibly based on pre-existing branch names.
1748 __git_complete_refs $dwim_opt --mode="heads"
1757 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
1760 __gitcomp_builtin checkout
1763 # At this point, we've already handled special completion for
1764 # the arguments to -b/-B, and --orphan. There are 3 main
1765 # things left we can possibly complete:
1766 # 1) a start-point for -b/-B, -d/--detach, or --orphan
1767 # 2) a remote head, for --track
1768 # 3) an arbitrary reference, possibly including DWIM names
1771 if [ -n "$(__git_find_on_cmdline "-b -B -d --detach --orphan")" ]; then
1772 __git_complete_refs --mode="refs"
1773 elif [ -n "$(__git_find_on_cmdline "-t --track")" ]; then
1774 __git_complete_refs --mode="remote-heads"
1776 __git_complete_refs $dwim_opt --mode="refs"
1782 __git_sequencer_inprogress_options="--continue --quit --abort --skip"
1784 __git_cherry_pick_inprogress_options=$__git_sequencer_inprogress_options
1788 if __git_pseudoref_exists CHERRY_PICK_HEAD; then
1789 __gitcomp "$__git_cherry_pick_inprogress_options"
1793 __git_complete_strategy && return
1797 __gitcomp_builtin cherry-pick "" \
1798 "$__git_cherry_pick_inprogress_options"
1810 __gitcomp_builtin clean
1815 # XXX should we check for -x option ?
1816 __git_complete_index_file "--others --directory"
1823 __git_complete_config_variable_name_and_value
1829 __git_complete_config_variable_name_and_value \
1830 --cur="${cur##--config=}"
1834 __gitcomp_builtin clone
1840 __git_untracked_file_modes="all no normal"
1842 __git_trailer_tokens ()
1844 __git config --name-only --get-regexp '^trailer\..*\.key$' | cut -d. -f 2- | rev | cut -d. -f2- | rev
1858 __gitcomp "default scissors strip verbatim whitespace
1859 " "" "${cur##--cleanup=}"
1862 --reuse-message=*|--reedit-message=*|\
1863 --fixup=*|--squash=*)
1864 __git_complete_refs --cur="${cur#*=}"
1867 --untracked-files=*)
1868 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
1872 __gitcomp_nl "$(__git_trailer_tokens)" "" "${cur##--trailer=}" ":"
1876 __gitcomp_builtin commit
1880 if __git rev-parse --verify --quiet HEAD >/dev/null; then
1881 __git_complete_index_file "--committable"
1883 # This is the first commit
1884 __git_complete_index_file "--cached"
1892 __gitcomp_builtin describe
1898 __git_diff_algorithms="myers minimal patience histogram"
1900 __git_diff_submodule_formats="diff log short"
1902 __git_color_moved_opts="no default plain blocks zebra dimmed-zebra"
1904 __git_color_moved_ws_opts="no ignore-space-at-eol ignore-space-change
1905 ignore-all-space allow-indentation-change"
1907 __git_ws_error_highlight_opts="context old new all default"
1909 # Options for the diff machinery (diff, log, show, stash, range-diff, ...)
1910 __git_diff_common_options="--stat --numstat --shortstat --summary
1911 --patch-with-stat --name-only --name-status --color
1912 --no-color --color-words --no-renames --check
1913 --color-moved --color-moved= --no-color-moved
1914 --color-moved-ws= --no-color-moved-ws
1915 --full-index --binary --abbrev --diff-filter=
1916 --find-copies --find-object --find-renames
1917 --no-relative --relative
1918 --find-copies-harder --ignore-cr-at-eol
1919 --text --ignore-space-at-eol --ignore-space-change
1920 --ignore-all-space --ignore-blank-lines --exit-code
1921 --quiet --ext-diff --no-ext-diff --unified=
1922 --no-prefix --src-prefix= --dst-prefix=
1923 --inter-hunk-context= --function-context
1924 --patience --histogram --minimal
1925 --raw --word-diff --word-diff-regex=
1926 --dirstat --dirstat= --dirstat-by-file
1927 --dirstat-by-file= --cumulative
1928 --diff-algorithm= --default-prefix
1929 --submodule --submodule= --ignore-submodules
1930 --indent-heuristic --no-indent-heuristic
1931 --textconv --no-textconv --break-rewrites
1932 --patch --no-patch --cc --combined-all-paths
1933 --anchored= --compact-summary --ignore-matching-lines=
1934 --irreversible-delete --line-prefix --no-stat
1935 --output= --output-indicator-context=
1936 --output-indicator-new= --output-indicator-old=
1937 --ws-error-highlight=
1938 --pickaxe-all --pickaxe-regex --patch-with-raw
1941 # Options for diff/difftool
1942 __git_diff_difftool_options="--cached --staged
1943 --base --ours --theirs --no-index --merge-base
1944 --ita-invisible-in-index --ita-visible-in-index
1945 $__git_diff_common_options"
1949 __git_has_doubledash && return
1953 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1957 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1961 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
1965 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
1968 --ws-error-highlight=*)
1969 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
1973 __gitcomp "$__git_diff_difftool_options"
1977 __git_complete_revlist_file
1980 __git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff
1981 tkdiff vimdiff nvimdiff gvimdiff xxdiff araxis p4merge
1982 bc codecompare smerge
1987 __git_has_doubledash && return
1991 __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1995 __gitcomp_builtin difftool "$__git_diff_difftool_options"
1999 __git_complete_revlist_file
2002 __git_fetch_recurse_submodules="yes on-demand no"
2007 --recurse-submodules=*)
2008 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
2012 __gitcomp "blob:none blob:limit= sparse:oid=" "" "${cur##--filter=}"
2016 __gitcomp_builtin fetch
2020 __git_complete_remote_or_refspec
2023 __git_format_patch_extra_options="
2024 --full-index --not --all --no-prefix --src-prefix=
2025 --dst-prefix= --notes
2028 _git_format_patch ()
2034 " "" "${cur##--thread=}"
2037 --base=*|--interdiff=*|--range-diff=*)
2038 __git_complete_refs --cur="${cur#--*=}"
2042 __gitcomp_builtin format-patch "$__git_format_patch_extra_options"
2046 __git_complete_revlist
2053 __gitcomp_builtin fsck
2064 # Lists matching symbol names from a tag (as in ctags) file.
2065 # 1: List symbol names matching this word.
2066 # 2: The tag file to list symbol names from.
2067 # 3: A prefix to be added to each listed symbol name (optional).
2068 # 4: A suffix to be appended to each listed symbol name (optional).
2069 __git_match_ctag () {
2070 awk -v pfx="${3-}" -v sfx="${4-}" "
2071 /^${1//\//\\/}/ { print pfx \$1 sfx }
2075 # Complete symbol names from a tag file.
2076 # Usage: __git_complete_symbol [<option>]...
2077 # --tags=<file>: The tag file to list symbol names from instead of the
2079 # --pfx=<prefix>: A prefix to be added to each symbol name.
2080 # --cur=<word>: The current symbol name to be completed. Defaults to
2081 # the current word to be completed.
2082 # --sfx=<suffix>: A suffix to be appended to each symbol name instead
2083 # of the default space.
2084 __git_complete_symbol () {
2085 local tags=tags pfx="" cur_="${cur-}" sfx=" "
2087 while test $# != 0; do
2089 --tags=*) tags="${1##--tags=}" ;;
2090 --pfx=*) pfx="${1##--pfx=}" ;;
2091 --cur=*) cur_="${1##--cur=}" ;;
2092 --sfx=*) sfx="${1##--sfx=}" ;;
2098 if test -r "$tags"; then
2099 __gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
2105 __git_has_doubledash && return
2109 __gitcomp_builtin grep
2114 case "$cword,$prev" in
2115 $((__git_cmd_idx+1)),*|*,-*)
2116 __git_complete_symbol && return
2127 __gitcomp_builtin help
2131 if test -n "${GIT_TESTING_ALL_COMMAND_LIST-}"
2133 __gitcomp "$GIT_TESTING_ALL_COMMAND_LIST $(__git --list-cmds=alias,list-guide) gitk"
2135 __gitcomp "$(__git --list-cmds=main,nohelpers,alias,list-guide) gitk"
2144 false true umask group all world everybody
2145 " "" "${cur##--shared=}"
2149 __gitcomp_builtin init
2159 __gitcomp_builtin ls-files
2164 # XXX ignore options like --modified and always suggest all cached
2166 __git_complete_index_file "--cached"
2173 __gitcomp_builtin ls-remote
2177 __gitcomp_nl "$(__git_remotes)"
2184 __gitcomp_builtin ls-tree
2192 # Options that go well for log, shortlog and gitk
2193 __git_log_common_options="
2195 --branches --tags --remotes
2196 --first-parent --merges --no-merges
2198 --max-age= --since= --after=
2199 --min-age= --until= --before=
2200 --min-parents= --max-parents=
2201 --no-min-parents --no-max-parents
2202 --alternate-refs --ancestry-path
2203 --author-date-order --basic-regexp
2204 --bisect --boundary --exclude-first-parent-only
2205 --exclude-hidden --extended-regexp
2206 --fixed-strings --grep-reflog
2207 --ignore-missing --left-only --perl-regexp
2208 --reflog --regexp-ignore-case --remove-empty
2209 --right-only --show-linear-break
2210 --show-notes-by-default --show-pulls
2211 --since-as-filter --single-worktree
2213 # Options that go well for log and gitk (not shortlog)
2214 __git_log_gitk_options="
2215 --dense --sparse --full-history
2216 --simplify-merges --simplify-by-decoration
2217 --left-right --notes --no-notes
2219 # Options that go well for log and shortlog (not gitk)
2220 __git_log_shortlog_options="
2221 --author= --committer= --grep=
2222 --all-match --invert-grep
2224 # Options accepted by log and show
2225 __git_log_show_options="
2226 --diff-merges --diff-merges= --no-diff-merges --dd --remerge-diff
2230 __git_diff_merges_opts="off none on first-parent 1 separate m combined c dense-combined cc remerge r"
2232 __git_log_pretty_formats="oneline short medium full fuller reference email raw format: tformat: mboxrd"
2233 __git_log_date_formats="relative iso8601 iso8601-strict rfc2822 short local default human raw unix auto: format:"
2235 # Complete porcelain (i.e. not git-rev-list) options and at least some
2236 # option arguments accepted by git-log. Note that this same set of options
2237 # are also accepted by some other git commands besides git-log.
2238 __git_complete_log_opts ()
2243 if __git_pseudoref_exists MERGE_HEAD; then
2246 case "$prev,$cur" in
2248 return # fall back to Bash filename completion
2251 __git_complete_symbol --cur="${cur#:}" --sfx=":"
2255 __git_complete_symbol
2260 --pretty=*|--format=*)
2261 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2266 __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
2270 __gitcomp "full short no" "" "${cur##--decorate=}"
2274 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
2278 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
2281 --ws-error-highlight=*)
2282 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
2286 __gitcomp "sorted unsorted" "" "${cur##--no-walk=}"
2290 __gitcomp "$__git_diff_merges_opts" "" "${cur##--diff-merges=}"
2295 $__git_log_common_options
2296 $__git_log_shortlog_options
2297 $__git_log_gitk_options
2298 $__git_log_show_options
2299 --root --topo-order --date-order --reverse
2300 --follow --full-diff
2301 --abbrev-commit --no-abbrev-commit --abbrev=
2302 --relative-date --date=
2303 --pretty= --format= --oneline
2308 --decorate --decorate= --no-decorate
2310 --no-walk --no-walk= --do-walk
2311 --parents --children
2312 --expand-tabs --expand-tabs= --no-expand-tabs
2313 --clear-decorations --decorate-refs=
2314 --decorate-refs-exclude=
2316 $__git_diff_common_options
2321 return # fall back to Bash filename completion
2324 __git_complete_symbol --cur="${cur#-L:}" --sfx=":"
2328 __git_complete_symbol --pfx="-G" --cur="${cur#-G}"
2332 __git_complete_symbol --pfx="-S" --cur="${cur#-S}"
2340 __git_has_doubledash && return
2341 __git_find_repo_path
2343 __git_complete_log_opts
2344 [ ${#COMPREPLY[@]} -eq 0 ] || return
2346 __git_complete_revlist
2351 __git_complete_strategy && return
2355 __gitcomp_builtin merge
2365 __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
2369 __gitcomp "--tool= --tool-help --prompt --no-prompt --gui --no-gui"
2379 __gitcomp_builtin merge-base
2390 __gitcomp_builtin mv
2395 if [ $(__git_count_arguments "mv") -gt 0 ]; then
2396 # We need to show both cached and untracked files (including
2397 # empty directories) since this may not be the last argument.
2398 __git_complete_index_file "--cached --others --directory"
2400 __git_complete_index_file "--cached"
2406 local subcommands='add append copy edit get-ref list merge prune remove show'
2407 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2409 case "$subcommand,$cur" in
2411 __gitcomp_builtin notes
2419 __gitcomp "$subcommands --ref"
2423 *,--reuse-message=*|*,--reedit-message=*)
2424 __git_complete_refs --cur="${cur#*=}"
2427 __gitcomp_builtin notes_$subcommand
2430 # this command does not take a ref, do not complete it
2446 __git_complete_strategy && return
2449 --recurse-submodules=*)
2450 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
2454 __gitcomp_builtin pull
2459 __git_complete_remote_or_refspec
2462 __git_push_recurse_submodules="check on-demand only"
2464 __git_complete_force_with_lease ()
2472 __git_complete_refs --cur="${cur_#*:}"
2475 __git_complete_refs --cur="$cur_"
2484 __gitcomp_nl "$(__git_remotes)"
2487 --recurse-submodules)
2488 __gitcomp "$__git_push_recurse_submodules"
2494 __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
2497 --recurse-submodules=*)
2498 __gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
2501 --force-with-lease=*)
2502 __git_complete_force_with_lease "${cur##--force-with-lease=}"
2506 __gitcomp_builtin push
2510 __git_complete_remote_or_refspec
2518 --creation-factor= --no-dual-color
2519 $__git_diff_common_options
2524 __git_complete_revlist
2527 __git_rebase_inprogress_options="--continue --skip --abort --quit --show-current-patch"
2528 __git_rebase_interactive_inprogress_options="$__git_rebase_inprogress_options --edit-todo"
2532 __git_find_repo_path
2533 if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
2534 __gitcomp "$__git_rebase_interactive_inprogress_options"
2536 elif [ -d "$__git_repo_path"/rebase-apply ] || \
2537 [ -d "$__git_repo_path"/rebase-merge ]; then
2538 __gitcomp "$__git_rebase_inprogress_options"
2541 __git_complete_strategy && return
2544 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
2548 __git_complete_refs --cur="${cur##--onto=}"
2552 __gitcomp_builtin rebase "" \
2553 "$__git_rebase_interactive_inprogress_options"
2562 local subcommands subcommand
2564 __git_resolve_builtins "reflog"
2566 subcommands="$___git_resolved_builtins"
2567 subcommand="$(__git_find_subcommand "$subcommands" "show")"
2569 case "$subcommand,$cur" in
2572 $__git_log_common_options
2577 __gitcomp_builtin "reflog_$subcommand"
2584 if [ $((cword - __git_cmd_idx)) -eq 1 ]; then
2585 __gitcompappend "$subcommands" "" "$cur" " "
2589 __git_send_email_confirm_options="always never auto cc compose"
2590 __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
2595 --to|--cc|--bcc|--from)
2596 __gitcomp "$(__git send-email --dump-aliases)"
2604 $__git_send_email_confirm_options
2605 " "" "${cur##--confirm=}"
2610 $__git_send_email_suppresscc_options
2611 " "" "${cur##--suppress-cc=}"
2615 --smtp-encryption=*)
2616 __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
2622 " "" "${cur##--thread=}"
2625 --to=*|--cc=*|--bcc=*|--from=*)
2626 __gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
2630 __gitcomp_builtin send-email "$__git_format_patch_extra_options"
2634 __git_complete_revlist
2645 local untracked_state
2648 --ignore-submodules=*)
2649 __gitcomp "none untracked dirty all" "" "${cur##--ignore-submodules=}"
2652 --untracked-files=*)
2653 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
2658 always never auto column row plain dense nodense
2659 " "" "${cur##--column=}"
2663 __gitcomp_builtin status
2668 untracked_state="$(__git_get_option_value "-u" "--untracked-files=" \
2669 "$__git_untracked_file_modes" "status.showUntrackedFiles")"
2671 case "$untracked_state" in
2673 # --ignored option does not matter
2677 complete_opt="--cached --directory --no-empty-directory --others"
2679 if [ -n "$(__git_find_on_cmdline "--ignored")" ]; then
2680 complete_opt="$complete_opt --ignored --exclude=*"
2685 __git_complete_index_file "$complete_opt"
2690 local dwim_opt="$(__git_checkout_default_dwim_mode)"
2694 # Complete local branches (and DWIM branch
2695 # remote branch names) for an option argument
2696 # specifying a new branch name. This is for
2697 # convenience, assuming new branches are
2698 # possibly based on pre-existing branch names.
2699 __git_complete_refs $dwim_opt --mode="heads"
2708 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
2711 __gitcomp_builtin switch
2714 # Unlike in git checkout, git switch --orphan does not take
2715 # a start point. Thus we really have nothing to complete after
2717 if [ -n "$(__git_find_on_cmdline "--orphan")" ]; then
2721 # At this point, we've already handled special completion for
2722 # -c/-C, and --orphan. There are 3 main things left to
2724 # 1) a start-point for -c/-C or -d/--detach
2725 # 2) a remote head, for --track
2726 # 3) a branch name, possibly including DWIM remote branches
2728 if [ -n "$(__git_find_on_cmdline "-c -C -d --detach")" ]; then
2729 __git_complete_refs --mode="refs"
2730 elif [ -n "$(__git_find_on_cmdline "-t --track")" ]; then
2731 __git_complete_refs --mode="remote-heads"
2733 __git_complete_refs $dwim_opt --mode="heads"
2739 __git_config_get_set_variables ()
2741 local prevword word config_file= c=$cword
2742 while [ $c -gt "$__git_cmd_idx" ]; do
2745 --system|--global|--local|--file=*)
2750 config_file="$word $prevword"
2758 __git config $config_file --name-only --list
2762 __git_compute_config_vars ()
2764 test -n "$__git_config_vars" ||
2765 __git_config_vars="$(git help --config-for-completion)"
2768 __git_config_vars_all=
2769 __git_compute_config_vars_all ()
2771 test -n "$__git_config_vars_all" ||
2772 __git_config_vars_all="$(git --no-pager help --config)"
2777 eval printf '%s' "\"\$$1\""
2780 __git_compute_first_level_config_vars_for_section ()
2783 __git_compute_config_vars
2784 local this_section="__git_first_level_config_vars_for_section_${section}"
2785 test -n "$(__git_indirect "${this_section}")" ||
2786 printf -v "__git_first_level_config_vars_for_section_${section}" %s \
2787 "$(echo "$__git_config_vars" | awk -F. "/^${section}\.[a-z]/ { print \$2 }")"
2790 __git_compute_second_level_config_vars_for_section ()
2793 __git_compute_config_vars_all
2794 local this_section="__git_second_level_config_vars_for_section_${section}"
2795 test -n "$(__git_indirect "${this_section}")" ||
2796 printf -v "__git_second_level_config_vars_for_section_${section}" %s \
2797 "$(echo "$__git_config_vars_all" | awk -F. "/^${section}\.</ { print \$3 }")"
2800 __git_config_sections=
2801 __git_compute_config_sections ()
2803 test -n "$__git_config_sections" ||
2804 __git_config_sections="$(git help --config-sections-for-completion)"
2807 # Completes possible values of various configuration variables.
2809 # Usage: __git_complete_config_variable_value [<option>]...
2810 # --varname=<word>: The name of the configuration variable whose value is
2811 # to be completed. Defaults to the previous word on the
2813 # --cur=<word>: The current value to be completed. Defaults to the current
2814 # word to be completed.
2815 __git_complete_config_variable_value ()
2817 local varname="$prev" cur_="$cur"
2819 while test $# != 0; do
2821 --varname=*) varname="${1##--varname=}" ;;
2822 --cur=*) cur_="${1##--cur=}" ;;
2828 if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
2829 varname="${varname,,}"
2831 varname="$(echo "$varname" |tr A-Z a-z)"
2835 branch.*.remote|branch.*.pushremote)
2836 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2840 __git_complete_refs --cur="$cur_"
2844 __gitcomp "false true merges interactive" "" "$cur_"
2848 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2852 local remote="${varname#remote.}"
2853 remote="${remote%.fetch}"
2854 if [ -z "$cur_" ]; then
2855 __gitcomp_nl "refs/heads/" "" "" ""
2858 __gitcomp_nl "$(__git_refs_remotes "$remote")" "" "$cur_"
2862 local remote="${varname#remote.}"
2863 remote="${remote%.push}"
2864 __gitcomp_nl "$(__git for-each-ref \
2865 --format='%(refname):%(refname)' refs/heads)" "" "$cur_"
2868 pull.twohead|pull.octopus)
2869 __git_compute_merge_strategies
2870 __gitcomp "$__git_merge_strategies" "" "$cur_"
2874 __gitcomp "false true" "" "$cur_"
2879 normal black red green yellow blue magenta cyan white
2880 bold dim ul blink reverse
2885 __gitcomp "false true always never auto" "" "$cur_"
2889 __gitcomp "$__git_diff_submodule_formats" "" "$cur_"
2893 __gitcomp "man info web html" "" "$cur_"
2897 __gitcomp "$__git_log_date_formats" "" "$cur_"
2900 sendemail.aliasfiletype)
2901 __gitcomp "mutt mailrc pine elm gnus" "" "$cur_"
2905 __gitcomp "$__git_send_email_confirm_options" "" "$cur_"
2908 sendemail.suppresscc)
2909 __gitcomp "$__git_send_email_suppresscc_options" "" "$cur_"
2912 sendemail.transferencoding)
2913 __gitcomp "7bit 8bit quoted-printable base64" "" "$cur_"
2922 # Completes configuration sections, subsections, variable names.
2924 # Usage: __git_complete_config_variable_name [<option>]...
2925 # --cur=<word>: The current configuration section/variable name to be
2926 # completed. Defaults to the current word to be completed.
2927 # --sfx=<suffix>: A suffix to be appended to each fully completed
2928 # configuration variable name (but not to sections or
2929 # subsections) instead of the default space.
2930 __git_complete_config_variable_name ()
2932 local cur_="$cur" sfx
2934 while test $# != 0; do
2936 --cur=*) cur_="${1##--cur=}" ;;
2937 --sfx=*) sfx="${1##--sfx=}" ;;
2944 branch.*.*|guitool.*.*|difftool.*.*|man.*.*|mergetool.*.*|remote.*.*|submodule.*.*|url.*.*)
2945 local pfx="${cur_%.*}."
2947 local section="${pfx%.*.}"
2948 __git_compute_second_level_config_vars_for_section "${section}"
2949 local this_section="__git_second_level_config_vars_for_section_${section}"
2950 __gitcomp "$(__git_indirect "${this_section}")" "$pfx" "$cur_" "$sfx"
2954 local pfx="${cur_%.*}."
2956 local section="${pfx%.}"
2957 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
2958 __git_compute_first_level_config_vars_for_section "${section}"
2959 local this_section="__git_first_level_config_vars_for_section_${section}"
2960 __gitcomp_nl_append "$(__git_indirect "${this_section}")" "$pfx" "$cur_" "${sfx:- }"
2964 local pfx="${cur_%.*}."
2966 __git_compute_all_commands
2967 __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_" "${sfx:- }"
2971 local pfx="${cur_%.*}."
2973 local section="${pfx%.}"
2974 __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
2975 __git_compute_first_level_config_vars_for_section "${section}"
2976 local this_section="__git_first_level_config_vars_for_section_${section}"
2977 __gitcomp_nl_append "$(__git_indirect "${this_section}")" "$pfx" "$cur_" "${sfx:- }"
2981 local pfx="${cur_%.*}."
2983 local section="${pfx%.}"
2984 __gitcomp_nl "$(__git config -f "$(__git rev-parse --show-toplevel)/.gitmodules" --get-regexp 'submodule.*.path' | awk -F. '{print $2}')" "$pfx" "$cur_" "."
2985 __git_compute_first_level_config_vars_for_section "${section}"
2986 local this_section="__git_first_level_config_vars_for_section_${section}"
2987 __gitcomp_nl_append "$(__git_indirect "${this_section}")" "$pfx" "$cur_" "${sfx:- }"
2991 __git_compute_config_vars
2992 __gitcomp "$__git_config_vars" "" "$cur_" "$sfx"
2995 __git_compute_config_sections
2996 __gitcomp "$__git_config_sections" "" "$cur_" "."
3001 # Completes '='-separated configuration sections/variable names and values
3002 # for 'git -c section.name=value'.
3004 # Usage: __git_complete_config_variable_name_and_value [<option>]...
3005 # --cur=<word>: The current configuration section/variable name/value to be
3006 # completed. Defaults to the current word to be completed.
3007 __git_complete_config_variable_name_and_value ()
3011 while test $# != 0; do
3013 --cur=*) cur_="${1##--cur=}" ;;
3021 __git_complete_config_variable_value \
3022 --varname="${cur_%%=*}" --cur="${cur_#*=}"
3025 __git_complete_config_variable_name --cur="$cur_" --sfx='='
3032 local subcommands subcommand
3034 __git_resolve_builtins "config"
3036 subcommands="$___git_resolved_builtins"
3037 subcommand="$(__git_find_subcommand "$subcommands")"
3039 if [ -z "$subcommand" ]
3041 __gitcomp "$subcommands"
3047 __gitcomp_builtin "config_$subcommand"
3052 case "$subcommand" in
3054 __gitcomp_nl "$(__git_config_get_set_variables)"
3059 __git_complete_config_variable_value
3062 __git_complete_config_variable_name
3067 __gitcomp_nl "$(__git_config_get_set_variables)"
3075 add rename remove set-head set-branches
3076 get-url set-url show prune update
3078 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3079 if [ -z "$subcommand" ]; then
3082 __gitcomp_builtin remote
3085 __gitcomp "$subcommands"
3091 case "$subcommand,$cur" in
3093 __gitcomp_builtin remote_add
3098 __gitcomp_builtin remote_set-head
3101 __gitcomp_builtin remote_set-branches
3103 set-head,*|set-branches,*)
3104 __git_complete_remote_or_refspec
3107 __gitcomp_builtin remote_update
3110 __gitcomp "$(__git_remotes) $(__git_get_config_variables "remotes")"
3113 __gitcomp_builtin remote_set-url
3116 __gitcomp_builtin remote_get-url
3119 __gitcomp_builtin remote_prune
3122 __gitcomp_nl "$(__git_remotes)"
3131 __gitcomp "short medium long" "" "${cur##--format=}"
3135 __gitcomp_builtin replace
3144 local subcommands="clear forget diff remaining status gc"
3145 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3146 if test -z "$subcommand"
3148 __gitcomp "$subcommands"
3155 __git_has_doubledash && return
3159 __gitcomp_builtin reset
3177 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
3180 __git_complete_refs --cur="${cur##--source=}"
3183 __gitcomp_builtin restore
3186 if __git_pseudoref_exists HEAD; then
3187 __git_complete_index_file "--modified"
3192 __git_revert_inprogress_options=$__git_sequencer_inprogress_options
3196 if __git_pseudoref_exists REVERT_HEAD; then
3197 __gitcomp "$__git_revert_inprogress_options"
3200 __git_complete_strategy && return
3203 __gitcomp_builtin revert "" \
3204 "$__git_revert_inprogress_options"
3215 __gitcomp_builtin rm
3220 __git_complete_index_file "--cached"
3225 __git_has_doubledash && return
3230 $__git_log_common_options
3231 $__git_log_shortlog_options
3232 --numbered --summary --email
3237 __git_complete_revlist
3242 __git_has_doubledash && return
3245 --pretty=*|--format=*)
3246 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
3251 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
3255 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
3259 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
3263 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
3266 --ws-error-highlight=*)
3267 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
3271 __gitcomp "$__git_diff_merges_opts" "" "${cur##--diff-merges=}"
3275 __gitcomp "--pretty= --format= --abbrev-commit --no-abbrev-commit
3276 --oneline --show-signature
3277 --expand-tabs --expand-tabs= --no-expand-tabs
3278 $__git_log_show_options
3279 $__git_diff_common_options
3284 __git_complete_revlist_file
3291 __gitcomp_builtin show-branch
3295 __git_complete_revlist
3298 __gitcomp_directories ()
3300 local _tmp_dir _tmp_completions _found=0
3302 # Get the directory of the current token; this differs from dirname
3303 # in that it keeps up to the final trailing slash. If no slash found
3306 _tmp_dir=$BASH_REMATCH
3308 # Find possible directory completions, adding trailing '/' characters,
3309 # de-quoting, and handling unusual characters.
3310 while IFS= read -r -d $'\0' c ; do
3311 # If there are directory completions, find ones that start
3312 # with "$cur", the current token, and put those in COMPREPLY
3313 if [[ $c == "$cur"* ]]; then
3317 done < <(__git ls-tree -z -d --name-only HEAD $_tmp_dir)
3319 if [[ $_found == 0 ]] && [[ "$cur" =~ /$ ]]; then
3320 # No possible further completions any deeper, so assume we're at
3321 # a leaf directory and just consider it complete
3322 __gitcomp_direct_append "$cur "
3323 elif [[ $_found == 0 ]]; then
3324 # No possible completions found. Avoid falling back to
3325 # bash's default file and directory completion, because all
3326 # valid completions have already been searched and the
3327 # fallbacks can do nothing but mislead. In fact, they can
3328 # mislead in three different ways:
3329 # 1) Fallback file completion makes no sense when asking
3330 # for directory completions, as this function does.
3331 # 2) Fallback directory completion is bad because
3332 # e.g. "/pro" is invalid and should NOT complete to
3334 # 3) Fallback file/directory completion only completes
3335 # on paths that exist in the current working tree,
3336 # i.e. which are *already* part of their
3337 # sparse-checkout. Thus, normal file and directory
3338 # completion is always useless for "git
3339 # sparse-checkout add" and is also problematic for
3340 # "git sparse-checkout set" unless using it to
3341 # strictly narrow the checkout.
3346 # In non-cone mode, the arguments to {set,add} are supposed to be
3347 # patterns, relative to the toplevel directory. These can be any kind
3348 # of general pattern, like 'subdir/*.c' and we can't complete on all
3349 # of those. However, if the user presses Tab to get tab completion, we
3350 # presume that they are trying to provide a pattern that names a specific
3352 __gitcomp_slash_leading_paths ()
3354 local dequoted_word pfx="" cur_ toplevel
3356 # Since we are dealing with a sparse-checkout, subdirectories may not
3357 # exist in the local working copy. Therefore, we want to run all
3358 # ls-files commands relative to the repository toplevel.
3359 toplevel="$(git rev-parse --show-toplevel)/"
3361 __git_dequote "$cur"
3363 # If the paths provided by the user already start with '/', then
3364 # they are considered relative to the toplevel of the repository
3365 # already. If they do not start with /, then we need to adjust
3366 # them to start with the appropriate prefix.
3372 pfx="$(__git rev-parse --show-prefix)"
3375 # Since sparse-index is limited to cone-mode, in non-cone-mode the
3376 # list of valid paths is precisely the cached files in the index.
3379 # 1) We probably need to take care of cases where ls-files
3380 # responds with special quoting.
3381 # 2) We probably need to take care of cases where ${cur} has
3382 # some kind of special quoting.
3383 # 3) On top of any quoting from 1 & 2, we have to provide an extra
3384 # level of quoting for any paths that contain a '*', '?', '\',
3385 # '[', ']', or leading '#' or '!' since those will be
3386 # interpreted by sparse-checkout as something other than a
3387 # literal path character.
3388 # Since there are two types of quoting here, this might get really
3389 # complex. For now, just punt on all of this...
3390 completions="$(__git -C "${toplevel}" -c core.quotePath=false \
3391 ls-files --cached -- "${pfx}${cur}*" \
3392 | sed -e s%^%/% -e 's%$% %')"
3393 # Note, above, though that we needed all of the completions to be
3394 # prefixed with a '/', and we want to add a space so that bash
3395 # completion will actually complete an entry and let us move on to
3398 # Return what we've found.
3399 if test -n "$completions"; then
3400 # We found some completions; return them
3402 COMPREPLY=($completions)
3404 # Do NOT fall back to bash-style all-local-files-and-dirs
3405 # when we find no match. Such options are worse than
3407 # 1. "git sparse-checkout add" needs paths that are NOT
3408 # currently in the working copy. "git
3409 # sparse-checkout set" does as well, except in the
3410 # special cases when users are only trying to narrow
3411 # their sparse checkout to a subset of what they
3414 # 2. A path like '.config' is ambiguous as to whether
3415 # the user wants all '.config' files throughout the
3416 # tree, or just the one under the current directory.
3417 # It would result in a warning from the
3418 # sparse-checkout command due to this. As such, all
3419 # completions of paths should be prefixed with a
3422 # 3. We don't want paths prefixed with a '/' to
3423 # complete files in the system root directory, we
3424 # want it to complete on files relative to the
3427 # As such, make sure that NO completions are offered rather
3428 # than falling back to bash's default completions.
3433 _git_sparse_checkout ()
3435 local subcommands="list init set disable add reapply"
3436 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3437 local using_cone=true
3438 if [ -z "$subcommand" ]; then
3439 __gitcomp "$subcommands"
3443 case "$subcommand,$cur" in
3445 __gitcomp_builtin sparse-checkout_$subcommand "" "--"
3448 if [[ "$(__git config core.sparseCheckout)" == "true" &&
3449 "$(__git config core.sparseCheckoutCone)" == "false" &&
3450 -z "$(__git_find_on_cmdline --cone)" ]]; then
3453 if [[ -n "$(__git_find_on_cmdline --no-cone)" ]]; then
3456 if [[ "$using_cone" == "true" ]]; then
3457 __gitcomp_directories
3459 __gitcomp_slash_leading_paths
3466 local subcommands='push list show apply clear drop pop create branch'
3467 local subcommand="$(__git_find_on_cmdline "$subcommands save")"
3469 if [ -z "$subcommand" ]; then
3470 case "$((cword - __git_cmd_idx)),$cur" in
3472 __gitcomp_builtin stash_push
3478 __gitcomp "$subcommands"
3484 case "$subcommand,$cur" in
3486 # NEEDSWORK: can we somehow unify this with the options in _git_log() and _git_show()
3487 __gitcomp_builtin stash_list "$__git_log_common_options $__git_diff_common_options"
3490 __gitcomp_builtin stash_show "$__git_diff_common_options"
3493 __gitcomp_builtin "stash_$subcommand"
3496 if [ $cword -eq $((__git_cmd_idx+2)) ]; then
3499 __gitcomp_nl "$(__git stash list \
3500 | sed -n -e 's/:.*//p')"
3503 show,*|apply,*|drop,*|pop,*)
3504 __gitcomp_nl "$(__git stash list \
3505 | sed -n -e 's/:.*//p')"
3512 __git_has_doubledash && return
3514 local subcommands="add status init deinit update set-branch set-url summary foreach sync absorbgitdirs"
3515 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3516 if [ -z "$subcommand" ]; then
3522 __gitcomp "$subcommands"
3528 case "$subcommand,$cur" in
3530 __gitcomp "--branch --force --name --reference --depth"
3533 __gitcomp "--cached --recursive"
3536 __gitcomp "--force --all"
3540 --init --remote --no-fetch
3541 --recommend-shallow --no-recommend-shallow
3542 --force --rebase --merge --reference --depth --recursive --jobs
3546 __gitcomp "--default --branch"
3549 __gitcomp "--cached --files --summary-limit"
3551 foreach,--*|sync,--*)
3552 __gitcomp "--recursive"
3562 init fetch clone rebase dcommit log find-rev
3563 set-tree commit-diff info create-ignore propget
3564 proplist show-ignore show-externals branch tag blame
3565 migrate mkdirs reset gc
3567 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3568 if [ -z "$subcommand" ]; then
3569 __gitcomp "$subcommands"
3571 local remote_opts="--username= --config-dir= --no-auth-cache"
3573 --follow-parent --authors-file= --repack=
3574 --no-metadata --use-svm-props --use-svnsync-props
3575 --log-window-size= --no-checkout --quiet
3576 --repack-flags --use-log-author --localtime
3579 --ignore-paths= --include-paths= $remote_opts
3582 --template= --shared= --trunk= --tags=
3583 --branches= --stdlayout --minimize-url
3584 --no-metadata --use-svm-props --use-svnsync-props
3585 --rewrite-root= --prefix= $remote_opts
3588 --edit --rmdir --find-copies-harder --copy-similarity=
3591 case "$subcommand,$cur" in
3593 __gitcomp "--revision= --fetch-all $fc_opts"
3596 __gitcomp "--revision= $fc_opts $init_opts"
3599 __gitcomp "$init_opts"
3603 --merge --strategy= --verbose --dry-run
3604 --fetch-all --no-rebase --commit-url
3605 --revision --interactive $cmt_opts $fc_opts
3609 __gitcomp "--stdin $cmt_opts $fc_opts"
3611 create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
3612 show-externals,--*|mkdirs,--*)
3613 __gitcomp "--revision="
3617 --limit= --revision= --verbose --incremental
3618 --oneline --show-commit --non-recursive
3619 --authors-file= --color
3624 --merge --verbose --strategy= --local
3625 --fetch-all --dry-run $fc_opts
3629 __gitcomp "--message= --file= --revision= $cmt_opts"
3635 __gitcomp "--dry-run --message --tag"
3638 __gitcomp "--dry-run --message"
3641 __gitcomp "--git-format"
3645 --config-dir= --ignore-paths= --minimize
3646 --no-auth-cache --username=
3650 __gitcomp "--revision= --parent"
3658 _git_symbolic_ref () {
3661 __gitcomp_builtin symbolic-ref
3671 local i c="$__git_cmd_idx" f=0
3672 while [ $c -lt $cword ]; do
3675 -d|--delete|-v|--verify)
3676 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3691 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3701 __gitcomp_builtin tag
3711 __git_complete_worktree_paths ()
3714 # Generate completion reply from worktree list skipping the first
3715 # entry: it's the path of the main worktree, which can't be moved,
3716 # removed, locked, etc.
3717 __gitcomp_nl "$(__git worktree list --porcelain |
3718 sed -n -e '2,$ s/^worktree //p')"
3723 local subcommands="add list lock move prune remove unlock"
3724 local subcommand subcommand_idx
3726 subcommand="$(__git_find_on_cmdline --show-idx "$subcommands")"
3727 subcommand_idx="${subcommand% *}"
3728 subcommand="${subcommand#* }"
3730 case "$subcommand,$cur" in
3732 __gitcomp "$subcommands"
3735 __gitcomp_builtin worktree_$subcommand
3737 add,*) # usage: git worktree add [<options>] <path> [<commit-ish>]
3738 # Here we are not completing an --option, it's either the
3741 -b|-B) # Complete refs for branch to be created/reset.
3744 -*) # The previous word is an -o|--option without an
3745 # unstuck argument: have to complete the path for
3746 # the new worktree, so don't list anything, but let
3747 # Bash fall back to filename completion.
3749 *) # The previous word is not an --option, so it must
3750 # be either the 'add' subcommand, the unstuck
3751 # argument of an option (e.g. branch for -b|-B), or
3752 # the path for the new worktree.
3753 if [ $cword -eq $((subcommand_idx+1)) ]; then
3754 # Right after the 'add' subcommand: have to
3755 # complete the path, so fall back to Bash
3756 # filename completion.
3759 case "${words[cword-2]}" in
3760 -b|-B) # After '-b <branch>': have to
3761 # complete the path, so fall back
3762 # to Bash filename completion.
3764 *) # After the path: have to complete
3765 # the ref to be checked out.
3773 lock,*|remove,*|unlock,*)
3774 __git_complete_worktree_paths
3777 if [ $cword -eq $((subcommand_idx+1)) ]; then
3778 # The first parameter must be an existing working
3780 __git_complete_worktree_paths
3782 # The second parameter is the destination: it could
3783 # be any path, so don't list anything, but let Bash
3784 # fall back to filename completion.
3791 __git_complete_common () {
3796 __gitcomp_builtin "$command"
3801 __git_cmds_with_parseopt_helper=
3802 __git_support_parseopt_helper () {
3803 test -n "$__git_cmds_with_parseopt_helper" ||
3804 __git_cmds_with_parseopt_helper="$(__git --list-cmds=parseopt)"
3806 case " $__git_cmds_with_parseopt_helper " in
3816 __git_have_func () {
3817 declare -f -- "$1" >/dev/null 2>&1
3820 __git_complete_command () {
3822 local completion_func="_git_${command//-/_}"
3823 if ! __git_have_func $completion_func &&
3824 __git_have_func _completion_loader
3826 _completion_loader "git-$command"
3828 if __git_have_func $completion_func
3832 elif __git_support_parseopt_helper "$command"
3834 __git_complete_common "$command"
3843 local i c=1 command __git_dir __git_repo_path
3844 local __git_C_args C_args_count=0
3847 while [ $c -lt $cword ]; do
3851 __git_dir="${i#--git-dir=}"
3855 __git_dir="${words[c]}"
3864 -c|--work-tree|--namespace)
3868 __git_C_args[C_args_count++]=-C
3870 __git_C_args[C_args_count++]="${words[c]}"
3883 if [ -z "${command-}" ]; then
3885 --git-dir|-C|--work-tree)
3886 # these need a path argument, let's fall back to
3887 # Bash filename completion
3891 __git_complete_config_variable_name_and_value
3895 # we don't support completing these options' arguments
3914 --no-replace-objects
3919 if test -n "${GIT_TESTING_PORCELAIN_COMMAND_LIST-}"
3921 __gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
3923 local list_cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config
3925 if test "${GIT_COMPLETION_SHOW_ALL_COMMANDS-}" = "1"
3927 list_cmds=builtins,$list_cmds
3929 __gitcomp "$(__git --list-cmds=$list_cmds)"
3936 __git_complete_command "$command" && return
3938 local expansion=$(__git_aliased_command "$command")
3939 if [ -n "$expansion" ]; then
3941 __git_complete_command "$expansion"
3947 __git_has_doubledash && return
3949 local __git_repo_path
3950 __git_find_repo_path
3953 if __git_pseudoref_exists MERGE_HEAD; then
3959 $__git_log_common_options
3960 $__git_log_gitk_options
3966 __git_complete_revlist
3969 if [[ -n ${ZSH_VERSION-} && -z ${GIT_SOURCING_ZSH_COMPLETION-} ]]; then
3970 echo "ERROR: this script is obsolete, please see git-completion.zsh" 1>&2
3976 local cur words cword prev
3977 local __git_cmd_idx=0
3978 _get_comp_words_by_ref -n =: cur words cword prev
3984 local wrapper="__git_wrap${2}"
3985 eval "$wrapper () { __git_func_wrap $2 ; }"
3986 complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
3987 || complete -o default -o nospace -F $wrapper $1
3990 # Setup the completion for git commands
3991 # 1: command or alias
3992 # 2: function to call (e.g. `git
`, `gitk
`, `git_fetch
`)
3997 if __git_have_func $2; then
3999 elif __git_have_func __$2_main; then
4001 elif __git_have_func _$2; then
4004 echo "ERROR: could not find function '$2'" 1>&2
4007 ___git_complete $1 $func
4010 ___git_complete git __git_main
4011 ___git_complete gitk __gitk_main
4013 # The following are necessary only for Cygwin, and only are needed
4014 # when the user has tab-completed the executable name and consequently
4015 # included the '.exe' suffix.
4017 if [ "$OSTYPE" = cygwin ]; then
4018 ___git_complete git.exe __git_main