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
()
141 # If the reftable is in use, we have to shell out to 'git rev-parse'
142 # to determine whether the ref exists instead of looking directly in
143 # the filesystem to determine whether the ref exists. Otherwise, use
144 # Bash builtins since executing Git commands are expensive on some
146 if __git_eread
"$__git_repo_path/HEAD" head; then
148 if [ "$b" == "refs/heads/.invalid" ]; then
149 __git
-C "$__git_repo_path" rev-parse
--verify --quiet "$ref" 2>/dev
/null
154 [ -f "$__git_repo_path/$ref" ]
157 # Removes backslash escaping, single quotes and double quotes from a word,
158 # stores the result in the variable $dequoted_word.
159 # 1: The word to dequote.
162 local rest
="$1" len ch
166 while test -n "$rest"; do
167 len
=${#dequoted_word}
168 dequoted_word
="$dequoted_word${rest%%[\\\'\"]*}"
169 rest
="${rest:$((${#dequoted_word}-$len))}"
171 case "${rest:0:1}" in
178 dequoted_word
="$dequoted_word$ch"
185 len
=${#dequoted_word}
186 dequoted_word
="$dequoted_word${rest%%\'*}"
187 rest
="${rest:$((${#dequoted_word}-$len+1))}"
191 while test -n "$rest" ; do
192 len
=${#dequoted_word}
193 dequoted_word
="$dequoted_word${rest%%[\\\"]*}"
194 rest
="${rest:$((${#dequoted_word}-$len))}"
195 case "${rest:0:1}" in
200 dequoted_word="$dequoted_word$ch"
205 dequoted_word="$dequoted_word\\$ch"
221 # The following function is based on code from:
223 # bash_completion - programmable completion functions for bash 3.2+
225 # Copyright © 2006-2008, Ian Macdonald <ian@caliban.org>
226 # © 2009-2010, Bash Completion Maintainers
227 # <bash-completion-devel@lists.alioth.debian.org>
229 # This program is free software; you can redistribute it and/or modify
230 # it under the terms of the GNU General Public License as published by
231 # the Free Software Foundation; either version 2, or (at your option)
234 # This program is distributed in the hope that it will be useful,
235 # but WITHOUT ANY WARRANTY; without even the implied warranty of
236 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
237 # GNU General Public License for more details.
239 # You should have received a copy of the GNU General Public License
240 # along with this program; if not, see <http://www.gnu.org/licenses/>.
242 # The latest version of this software can be obtained here:
244 # http://bash-completion.alioth.debian.org/
248 # This function can be used to access a tokenized list of words
249 # on the command line:
251 # __git_reassemble_comp_words_by_ref '=:'
252 # if test "${words_[cword_-1]}" = -w
257 # The argument should be a collection of characters from the list of
258 # word completion separators (COMP_WORDBREAKS) to treat as ordinary
261 # This is roughly equivalent to going back in time and setting
262 # COMP_WORDBREAKS to exclude those characters. The intent is to
263 # make option types like --date=<type> and <rev>:<path> easy to
264 # recognize by treating each shell word as a single token.
266 # It is best not to set COMP_WORDBREAKS directly because the value is
267 # shared with other completion scripts. By the time the completion
268 # function gets called, COMP_WORDS has already been populated so local
269 # changes to COMP_WORDBREAKS have no effect.
271 # Output: words_, cword_, cur_.
273 __git_reassemble_comp_words_by_ref()
275 local exclude i j first
276 # Which word separators to exclude?
277 exclude="${1//[^$COMP_WORDBREAKS]}"
279 if [ -z "$exclude" ]; then
280 words_=("${COMP_WORDS[@]}")
283 # List of word completion separators has shrunk;
284 # re-assemble words to complete.
285 for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
286 # Append each nonempty word consisting of just
287 # word separator characters to the current word.
291 [ -n "${COMP_WORDS[$i]}" ] &&
292 # word consists of excluded word separators
293 [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
295 # Attach to the previous token,
296 # unless the previous token is the command name.
297 if [ $j -ge 2 ] && [ -n "$first" ]; then
301 words_[$j]=${words_[j]}${COMP_WORDS[i]}
302 if [ $i = $COMP_CWORD ]; then
305 if (($i < ${#COMP_WORDS[@]} - 1)); then
312 words_[$j]=${words_[j]}${COMP_WORDS[i]}
313 if [ $i = $COMP_CWORD ]; then
319 if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
320 _get_comp_words_by_ref ()
322 local exclude cur_ words_ cword_
323 if [ "$1" = "-n" ]; then
327 __git_reassemble_comp_words_by_ref "$exclude"
328 cur_=${words_[cword_]}
329 while [ $# -gt 0 ]; do
335 prev=${words_[$cword_-1]}
338 words=("${words_[@]}")
349 # Fills the COMPREPLY array with prefiltered words without any additional
351 # Callers must take care of providing only words that match the current word
352 # to be completed and adding any prefix and/or suffix (trailing space!), if
354 # 1: List of newline-separated matching completion words, complete with
363 # Similar to __gitcomp_direct, but appends to COMPREPLY instead.
364 # Callers must take care of providing only words that match the current word
365 # to be completed and adding any prefix and/or suffix (trailing space!), if
367 # 1: List of newline-separated matching completion words, complete with
369 __gitcomp_direct_append ()
378 local x i=${#COMPREPLY[@]}
380 if [[ "$x" == "$3"* ]]; then
381 COMPREPLY[i++]="$2$x$4"
392 # Generates completion reply, appending a space to possible completion words,
394 # It accepts 1 to 4 arguments:
395 # 1: List of possible completion words.
396 # 2: A prefix to be added to each possible completion word (optional).
397 # 3: Generate possible completion matches for this word (optional).
398 # 4: A suffix to be appended to each possible completion word (optional).
401 local cur_="${3-$cur}"
407 local c i=0 IFS=$' \t\n'
409 if [[ $c == "--" ]]; then
413 if [[ $c == "$cur_"* ]]; then
418 COMPREPLY[i++]="${2-}$c"
423 local c i=0 IFS=$' \t\n'
425 if [[ $c == "--" ]]; then
427 if [[ $c == "$cur_"* ]]; then
428 COMPREPLY[i++]="${2-}$c "
433 if [[ $c == "$cur_"* ]]; then
438 COMPREPLY[i++]="${2-}$c"
445 # Clear the variables caching builtins' options when (re-)sourcing
446 # the completion script.
447 if [[ -n ${ZSH_VERSION-} ]]; then
448 unset ${(M)${(k)parameters[@]}:#__gitcomp_builtin_*} 2>/dev/null
450 unset $(compgen -v __gitcomp_builtin_)
453 # This function is equivalent to
455 # __gitcomp "$(git xxx --git-completion-helper) ..."
457 # except that the output is cached. Accept 1-3 arguments:
458 # 1: the git command to execute, this is also the cache key
459 # 2: extra options to be added on top (e.g. negative forms)
460 # 3: options to be excluded
463 # spaces must be replaced with underscore for multi-word
464 # commands, e.g. "git remote add" becomes remote_add.
469 local var=__gitcomp_builtin_"${cmd//-/_}"
471 eval "options=\${$var-}"
473 if [ -z "$options" ]; then
474 local completion_helper
475 if [ "${GIT_COMPLETION_SHOW_ALL-}" = "1" ]; then
476 completion_helper="--git-completion-helper-all"
478 completion_helper="--git-completion-helper"
480 # leading and trailing spaces are significant to make
481 # option removal work correctly.
482 options=" $incl $(__git ${cmd/_/ } $completion_helper) " || return
485 options="${options/ $i / }"
487 eval "$var=\"$options\""
493 # Variation of __gitcomp_nl () that appends to the existing list of
494 # completion candidates, COMPREPLY.
495 __gitcomp_nl_append ()
498 __gitcompappend "$1" "${2-}" "${3-$cur}" "${4- }"
501 # Generates completion reply from newline-separated possible completion words
502 # by appending a space to all of them.
503 # It accepts 1 to 4 arguments:
504 # 1: List of possible completion words, separated by a single newline.
505 # 2: A prefix to be added to each possible completion word (optional).
506 # 3: Generate possible completion matches for this word (optional).
507 # 4: A suffix to be appended to each possible completion word instead of
508 # the default space (optional). If specified but empty, nothing is
513 __gitcomp_nl_append "$@"
516 # Fills the COMPREPLY array with prefiltered paths without any additional
518 # Callers must take care of providing only paths that match the current path
519 # to be completed and adding any prefix path components, if necessary.
520 # 1: List of newline-separated matching paths, complete with all prefix
522 __gitcomp_file_direct ()
528 # use a hack to enable file mode in bash < 4
529 compopt -o filenames +o nospace 2>/dev/null ||
530 compgen -f /non-existing-dir/ >/dev/null ||
534 # Generates completion reply with compgen from newline-separated possible
535 # completion filenames.
536 # It accepts 1 to 3 arguments:
537 # 1: List of possible completion filenames, separated by a single newline.
538 # 2: A directory prefix to be added to each possible completion filename
540 # 3: Generate possible completion matches for this word (optional).
545 # XXX does not work when the directory prefix contains a tilde,
546 # since tilde expansion is not applied.
547 # This means that COMPREPLY will be empty and Bash default
548 # completion will be used.
549 __gitcompadd "$1" "${2-}" "${3-$cur}" ""
551 # use a hack to enable file mode in bash < 4
552 compopt -o filenames +o nospace 2>/dev/null ||
553 compgen -f /non-existing-dir/ >/dev/null ||
557 # Find the current subcommand for commands that follow the syntax:
559 # git <command> <subcommand>
561 # 1: List of possible subcommands.
562 # 2: Optional subcommand to return when none is found.
563 __git_find_subcommand ()
565 local subcommand subcommands="$1" default_subcommand="$2"
567 for subcommand in $subcommands; do
568 if [ "$subcommand" = "${words[__git_cmd_idx+1]}" ]; then
574 echo $default_subcommand
577 # Execute 'git ls-files', unless the --committable option is specified, in
578 # which case it runs 'git diff-index' to find out the files that can be
579 # committed. It return paths relative to the directory specified in the first
580 # argument, and using the options specified in the second argument.
581 __git_ls_files_helper ()
583 if [ "$2" = "--committable" ]; then
584 __git -C "$1" -c core.quotePath=false diff-index \
585 --name-only --relative HEAD -- "${3//\\/\\\\}*"
587 # NOTE: $2 is not quoted in order to support multiple options
588 __git -C "$1" -c core.quotePath=false ls-files \
589 --exclude-standard $2 -- "${3//\\/\\\\}*"
594 # __git_index_files accepts 1 or 2 arguments:
595 # 1: Options to pass to ls-files (required).
596 # 2: A directory path (optional).
597 # If provided, only files within the specified directory are listed.
598 # Sub directories are never recursed. Path must have a trailing
600 # 3: List only paths matching this path component (optional).
603 local root="$2" match="$3"
605 __git_ls_files_helper "$root" "$1" "${match:-?}" |
606 awk -F / -v pfx="${2//\\/\\\\}" '{
611 if (substr(p, 1, 1) != "\"") {
612 # No special characters, easy!
617 # The path is quoted.
622 # Even when a directory name itself does not contain
623 # any special characters, it will still be quoted if
624 # any of its (stripped) trailing path components do.
625 # Because of this we may have seen the same directory
626 # both quoted and unquoted.
628 # We have seen the same directory unquoted,
635 function dequote(p, bs_idx, out, esc, esc_idx, dec) {
636 # Skip opening double quote.
639 # Interpret backslash escape sequences.
640 while ((bs_idx = index(p, "\\")) != 0) {
641 out = out substr(p, 1, bs_idx - 1)
642 esc = substr(p, bs_idx + 1, 1)
643 p = substr(p, bs_idx + 2)
645 if ((esc_idx = index("abtvfr\"\\", esc)) != 0) {
646 # C-style one-character escape sequence.
647 out = out substr("\a\b\t\v\f\r\"\\",
649 } else if (esc == "n") {
650 # Uh-oh, a newline character.
651 # We cannot reliably put a pathname
652 # containing a newline into COMPREPLY,
653 # and the newline would create a mess.
657 # Must be a \nnn octal value, then.
659 substr(p, 1, 1) * 8 + \
661 out = out sprintf("%c", dec)
665 # Drop closing double quote, if there is one.
666 # (There is not any if this is a directory, as it was
667 # already stripped with the trailing path components.)
668 if (substr(p, length(p), 1) == "\"")
669 out = out substr(p, 1, length(p) - 1)
677 # __git_complete_index_file requires 1 argument:
678 # 1: the options to pass to ls-file
680 # The exception is --committable, which finds the files appropriate commit.
681 __git_complete_index_file ()
683 local dequoted_word pfx="" cur_
687 case "$dequoted_word" in
689 pfx="${dequoted_word%/*}/"
690 cur_="${dequoted_word##*/}"
693 cur_="$dequoted_word"
696 __gitcomp_file_direct "$(__git_index_files "$1" "$pfx" "$cur_")"
699 # Lists branches from the local repository.
700 # 1: A prefix to be added to each listed branch (optional).
701 # 2: List only branches matching this word (optional; list all branches if
703 # 3: A suffix to be appended to each listed branch (optional).
706 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
708 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
709 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
710 "refs/heads/$cur_*" "refs/heads/$cur_*/**"
713 # Lists branches from remote repositories.
714 # 1: A prefix to be added to each listed branch (optional).
715 # 2: List only branches matching this word (optional; list all branches if
717 # 3: A suffix to be appended to each listed branch (optional).
718 __git_remote_heads ()
720 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
722 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
723 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
724 "refs/remotes/$cur_*" "refs/remotes/$cur_*/**"
727 # Lists tags from the local repository.
728 # Accepts the same positional parameters as __git_heads() above.
731 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
733 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
734 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
735 "refs/tags/$cur_*" "refs/tags/$cur_*/**"
738 # List unique branches from refs/remotes used for 'git checkout' and 'git
739 # switch' tracking DWIMery.
740 # 1: A prefix to be added to each listed branch (optional)
741 # 2: List only branches matching this word (optional; list all branches if
743 # 3: A suffix to be appended to each listed branch (optional).
744 __git_dwim_remote_heads ()
746 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
747 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
749 # employ the heuristic used by git checkout and git switch
750 # Try to find a remote branch that cur_es the completion word
751 # but only output if the branch name is unique
752 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
753 --sort="refname:strip=3" \
754 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
755 "refs/remotes/*/$cur_*" "refs/remotes/*/$cur_*/**" | \
759 # Lists refs from the local (by default) or from a remote repository.
760 # It accepts 0, 1 or 2 arguments:
761 # 1: The remote to list refs from (optional; ignored, if set but empty).
762 # Can be the name of a configured remote, a path, or a URL.
763 # 2: In addition to local refs, list unique branches from refs/remotes/ for
764 # 'git checkout's tracking DWIMery (optional; ignored, if set but empty).
765 # 3: A prefix to be added to each listed ref (optional).
766 # 4: List only refs matching this word (optional; list all refs if unset or
768 # 5: A suffix to be appended to each listed ref (optional; ignored, if set
771 # Use __git_complete_refs() instead.
774 local i hash dir track="${2-}"
775 local list_refs_from=path remote="${1-}"
777 local pfx="${3-}" cur_="${4-$cur}" sfx="${5-}"
780 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
783 dir="$__git_repo_path"
785 if [ -z "$remote" ]; then
786 if [ -z "$dir" ]; then
790 if __git_is_configured_remote "$remote"; then
791 # configured remote takes precedence over a
792 # local directory with the same name
793 list_refs_from=remote
794 elif [ -d "$remote/.git" ]; then
796 elif [ -d "$remote" ]; then
803 if test "${GIT_COMPLETION_IGNORE_CASE:+1}" = "1"
805 # uppercase with tr instead of ${match,^^} for bash 3.2 compatibility
806 umatch=$(echo "$match" | tr a-z A-Z 2>/dev/null || echo "$match")
809 if [ "$list_refs_from" = path ]; then
810 if [[ "$cur_" == ^* ]]; then
820 refs=("$match*" "$match*/**")
824 for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD REBASE_HEAD CHERRY_PICK_HEAD REVERT_HEAD BISECT_HEAD AUTO_MERGE; do
827 if [ -e "$dir/$i" ]; then
833 format="refname:strip=2"
834 refs=("refs/tags/$match*" "refs/tags/$match*/**"
835 "refs/heads/$match*" "refs/heads/$match*/**"
836 "refs/remotes/$match*" "refs/remotes/$match*/**")
839 __git_dir="$dir" __git for-each-ref --format="$fer_pfx%($format)$sfx" \
840 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
842 if [ -n "$track" ]; then
843 __git_dwim_remote_heads "$pfx" "$match" "$sfx"
849 __git ls-remote "$remote" "$match*" | \
850 while read -r hash i; do
853 *) echo "$pfx$i$sfx" ;;
858 if [ "$list_refs_from" = remote ]; then
860 $match*|$umatch*) echo "${pfx}HEAD$sfx" ;;
862 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
863 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
864 "refs/remotes/$remote/$match*" \
865 "refs/remotes/$remote/$match*/**"
869 $match*|$umatch*) query_symref="HEAD" ;;
871 __git ls-remote "$remote" $query_symref \
872 "refs/tags/$match*" "refs/heads/$match*" \
873 "refs/remotes/$match*" |
874 while read -r hash i; do
877 refs/*) echo "$pfx${i#refs/*/}$sfx" ;;
878 *) echo "$pfx$i$sfx" ;; # symbolic refs
886 # Completes refs, short and long, local and remote, symbolic and pseudo.
888 # Usage: __git_complete_refs [<option>]...
889 # --remote=<remote>: The remote to list refs from, can be the name of a
890 # configured remote, a path, or a URL.
891 # --dwim: List unique remote branches for 'git switch's tracking DWIMery.
892 # --pfx=<prefix>: A prefix to be added to each ref.
893 # --cur=<word>: The current ref to be completed. Defaults to the current
894 # word to be completed.
895 # --sfx=<suffix>: A suffix to be appended to each ref instead of the default
897 # --mode=<mode>: What set of refs to complete, one of 'refs' (the default) to
898 # complete all refs, 'heads' to complete only branches, or
899 # 'remote-heads' to complete only remote branches. Note that
900 # --remote is only compatible with --mode=refs.
901 __git_complete_refs ()
903 local remote= dwim= pfx= cur_="$cur" sfx=" " mode="refs"
905 while test $# != 0; do
907 --remote=*) remote="${1##--remote=}" ;;
908 --dwim) dwim="yes" ;;
909 # --track is an old spelling of --dwim
910 --track) dwim="yes" ;;
911 --pfx=*) pfx="${1##--pfx=}" ;;
912 --cur=*) cur_="${1##--cur=}" ;;
913 --sfx=*) sfx="${1##--sfx=}" ;;
914 --mode=*) mode="${1##--mode=}" ;;
920 # complete references based on the specified mode
923 __gitcomp_direct "$(__git_refs "$remote" "" "$pfx" "$cur_" "$sfx")" ;;
925 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" "$sfx")" ;;
927 __gitcomp_direct "$(__git_remote_heads "$pfx" "$cur_" "$sfx")" ;;
932 # Append DWIM remote branch names if requested
933 if [ "$dwim" = "yes" ]; then
934 __gitcomp_direct_append "$(__git_dwim_remote_heads "$pfx" "$cur_" "$sfx")"
938 # __git_refs2 requires 1 argument (to pass to __git_refs)
939 # Deprecated: use __git_complete_fetch_refspecs() instead.
943 for i in $(__git_refs "$1"); do
948 # Completes refspecs for fetching from a remote repository.
949 # 1: The remote repository.
950 # 2: A prefix to be added to each listed refspec (optional).
951 # 3: The ref to be completed as a refspec instead of the current word to be
952 # completed (optional)
953 # 4: A suffix to be appended to each listed refspec instead of the default
955 __git_complete_fetch_refspecs ()
957 local i remote="$1" pfx="${2-}" cur_="${3-$cur}" sfx="${4- }"
960 for i in $(__git_refs "$remote" "" "" "$cur_") ; do
966 # __git_refs_remotes requires 1 argument (to pass to ls-remote)
967 __git_refs_remotes ()
970 __git ls-remote "$1" 'refs/heads/*' | \
971 while read -r hash i; do
972 echo "$i:refs/remotes/$1/${i#refs/heads/}"
979 test -d "$__git_repo_path/remotes" && ls -1 "$__git_repo_path/remotes"
983 # Returns true if $1 matches the name of a configured remote, false otherwise.
984 __git_is_configured_remote ()
987 for remote in $(__git_remotes); do
988 if [ "$remote" = "$1" ]; then
995 __git_list_merge_strategies ()
997 LANG=C LC_ALL=C git merge -s help 2>&1 |
998 sed -n -e '/[Aa]vailable strategies are: /,/^$/{
1007 __git_merge_strategies=
1008 # 'git merge -s help' (and thus detection of the merge strategy
1009 # list) fails, unfortunately, if run outside of any git working
1010 # tree. __git_merge_strategies is set to the empty string in
1011 # that case, and the detection will be repeated the next time it
1013 __git_compute_merge_strategies ()
1015 test -n "$__git_merge_strategies" ||
1016 __git_merge_strategies=$(__git_list_merge_strategies)
1019 __git_merge_strategy_options="ours theirs subtree subtree= patience
1020 histogram diff-algorithm= ignore-space-change ignore-all-space
1021 ignore-space-at-eol renormalize no-renormalize no-renames
1022 find-renames find-renames= rename-threshold="
1024 __git_complete_revlist_file ()
1026 local dequoted_word pfx ls ref cur_="$cur"
1035 __git_dequote "$cur_"
1037 case "$dequoted_word" in
1039 pfx="${dequoted_word%/*}"
1040 cur_="${dequoted_word##*/}"
1045 cur_="$dequoted_word"
1050 case "$COMP_WORDBREAKS" in
1052 *) pfx="$ref:$pfx" ;;
1055 __gitcomp_file "$(__git ls-tree "$ls" \
1061 pfx="${cur_%...*}..."
1063 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1068 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1076 __git_complete_file ()
1078 __git_complete_revlist_file
1081 __git_complete_revlist ()
1083 __git_complete_revlist_file
1086 __git_complete_remote_or_refspec ()
1088 local cur_="$cur" cmd="${words[__git_cmd_idx]}"
1089 local i c=$((__git_cmd_idx+1)) remote="" pfx="" lhs=1 no_complete_refspec=0
1090 if [ "$cmd" = "remote" ]; then
1093 while [ $c -lt $cword ]; do
1096 --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
1097 -d|--delete) [ "$cmd" = "push" ] && lhs=0 ;;
1100 push) no_complete_refspec=1 ;;
1107 --multiple) no_complete_refspec=1; break ;;
1109 *) remote="$i"; break ;;
1113 if [ -z "$remote" ]; then
1114 __gitcomp_nl "$(__git_remotes)"
1117 if [ $no_complete_refspec = 1 ]; then
1120 [ "$remote" = "." ] && remote=
1123 case "$COMP_WORDBREAKS" in
1125 *) pfx="${cur_%%:*}:" ;;
1137 if [ $lhs = 1 ]; then
1138 __git_complete_fetch_refspecs "$remote" "$pfx" "$cur_"
1140 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1144 if [ $lhs = 1 ]; then
1145 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
1147 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1151 if [ $lhs = 1 ]; then
1152 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1154 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
1160 __git_complete_strategy ()
1162 __git_compute_merge_strategies
1165 __gitcomp "$__git_merge_strategies"
1169 __gitcomp "$__git_merge_strategy_options"
1175 __gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
1178 --strategy-option=*)
1179 __gitcomp "$__git_merge_strategy_options" "" "${cur##--strategy-option=}"
1187 __git_compute_all_commands ()
1189 test -n "$__git_all_commands" ||
1190 __git_all_commands=$(__git --list-cmds=main,others,alias,nohelpers)
1193 # Lists all set config variables starting with the given section prefix,
1194 # with the prefix removed.
1195 __git_get_config_variables ()
1197 local section="$1" i IFS=$'\n'
1198 for i in $(__git config --name-only --get-regexp "^$section\..*"); do
1199 echo "${i#$section.}"
1203 __git_pretty_aliases ()
1205 __git_get_config_variables "pretty"
1208 # __git_aliased_command requires 1 argument
1209 __git_aliased_command ()
1211 local cur=$1 last list= word cmdline
1213 while [[ -n "$cur" ]]; do
1214 if [[ "$list" == *" $cur "* ]]; then
1219 cmdline=$(__git config --get "alias.$cur")
1224 for word in $cmdline; do
1230 \!*) : shell command alias ;;
1232 *=*) : setting env ;;
1233 git) : git itself ;;
1234 \(\)) : skip parens of shell function definition ;;
1235 {) : skip start of shell helper function ;;
1236 :) : skip null command ;;
1237 \'*) : skip opening quote after sh -c ;;
1246 if [[ "$cur" != "$1" ]]; then
1251 # Check whether one of the given words is present on the command line,
1252 # and print the first word found.
1254 # Usage: __git_find_on_cmdline [<option>]... "<wordlist>"
1255 # --show-idx: Optionally show the index of the found word in the $words array.
1256 __git_find_on_cmdline ()
1258 local word c="$__git_cmd_idx" show_idx
1260 while test $# -gt 1; do
1262 --show-idx) show_idx=y ;;
1269 while [ $c -lt $cword ]; do
1270 for word in $wordlist; do
1271 if [ "$word" = "${words[c]}" ]; then
1272 if [ -n "${show_idx-}" ]; then
1284 # Similar to __git_find_on_cmdline, except that it loops backwards and thus
1285 # prints the *last* word found. Useful for finding which of two options that
1286 # supersede each other came last, such as "--guess" and "--no-guess".
1288 # Usage: __git_find_last_on_cmdline [<option>]... "<wordlist>"
1289 # --show-idx: Optionally show the index of the found word in the $words array.
1290 __git_find_last_on_cmdline ()
1292 local word c=$cword show_idx
1294 while test $# -gt 1; do
1296 --show-idx) show_idx=y ;;
1303 while [ $c -gt "$__git_cmd_idx" ]; do
1305 for word in $wordlist; do
1306 if [ "$word" = "${words[c]}" ]; then
1307 if [ -n "$show_idx" ]; then
1318 # Echo the value of an option set on the command line or config
1320 # $1: short option name
1321 # $2: long option name including =
1322 # $3: list of possible values
1323 # $4: config string (optional)
1326 # result="$(__git_get_option_value "-d" "--do-something=" \
1327 # "yes no" "core.doSomething")"
1329 # result is then either empty (no option set) or "yes" or "no"
1331 # __git_get_option_value requires 3 arguments
1332 __git_get_option_value ()
1334 local c short_opt long_opt val
1335 local result= values config_key word
1343 while [ $c -ge 0 ]; do
1345 for val in $values; do
1346 if [ "$short_opt$val" = "$word" ] ||
1347 [ "$long_opt$val" = "$word" ]; then
1355 if [ -n "$config_key" ] && [ -z "$result" ]; then
1356 result="$(__git config "$config_key")"
1362 __git_has_doubledash ()
1365 while [ $c -lt $cword ]; do
1366 if [ "--" = "${words[c]}" ]; then
1374 # Try to count non option arguments passed on the command line for the
1375 # specified git command.
1376 # When options are used, it is necessary to use the special -- option to
1377 # tell the implementation were non option arguments begin.
1378 # XXX this can not be improved, since options can appear everywhere, as
1382 # __git_count_arguments requires 1 argument: the git command executed.
1383 __git_count_arguments ()
1387 # Skip "git" (first argument)
1388 for ((i=$__git_cmd_idx; i < ${#words[@]}; i++)); do
1393 # Good; we can assume that the following are only non
1398 # Skip the specified git command and discard git
1411 __git_whitespacelist="nowarn warn error error-all fix"
1412 __git_patchformat="mbox stgit stgit-series hg mboxrd"
1413 __git_showcurrentpatch="diff raw"
1414 __git_am_inprogress_options="--skip --continue --resolved --abort --quit --show-current-patch"
1415 __git_quoted_cr="nowarn warn strip"
1419 __git_find_repo_path
1420 if [ -d "$__git_repo_path"/rebase-apply ]; then
1421 __gitcomp "$__git_am_inprogress_options"
1426 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1430 __gitcomp "$__git_patchformat" "" "${cur##--patch-format=}"
1433 --show-current-patch=*)
1434 __gitcomp "$__git_showcurrentpatch" "" "${cur##--show-current-patch=}"
1438 __gitcomp "$__git_quoted_cr" "" "${cur##--quoted-cr=}"
1442 __gitcomp_builtin am "" \
1443 "$__git_am_inprogress_options"
1452 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1456 __gitcomp_builtin apply
1465 __gitcomp "+x -x" "" "${cur##--chmod=}"
1469 __gitcomp_builtin add
1473 local complete_opt="--others --modified --directory --no-empty-directory"
1474 if test -n "$(__git_find_on_cmdline "-u --update")"
1476 complete_opt="--modified"
1478 __git_complete_index_file "$complete_opt"
1485 __gitcomp "$(git archive --list)" "" "${cur##--format=}"
1489 __gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
1493 __gitcomp_builtin archive "--format= --list --verbose --prefix= --worktree-attributes"
1502 __git_has_doubledash && return
1504 local subcommands="start bad good skip reset visualize replay log run"
1505 local subcommand="$(__git_find_on_cmdline "$subcommands")"
1506 if [ -z "$subcommand" ]; then
1507 __git_find_repo_path
1508 if [ -f "$__git_repo_path"/BISECT_START ]; then
1509 __gitcomp "$subcommands"
1511 __gitcomp "replay start"
1516 case "$subcommand" in
1517 bad|good|reset|skip|start)
1525 __git_ref_fieldlist="refname objecttype objectsize objectname upstream push HEAD symref"
1529 local i c="$__git_cmd_idx" only_local_ref="n" has_r="n"
1531 while [ $c -lt $cword ]; do
1534 -d|-D|--delete|-m|-M|--move|-c|-C|--copy)
1535 only_local_ref="y" ;;
1543 --set-upstream-to=*)
1544 __git_complete_refs --cur="${cur##--set-upstream-to=}"
1547 __gitcomp_builtin branch
1550 if [ $only_local_ref = "y" -a $has_r = "n" ]; then
1551 __gitcomp_direct "$(__git_heads "" "$cur" " ")"
1561 local cmd="${words[__git_cmd_idx+1]}"
1563 $((__git_cmd_idx+1)))
1564 __gitcomp "create list-heads verify unbundle"
1566 $((__git_cmd_idx+2)))
1567 # looking for a file
1572 __git_complete_revlist
1579 # Helper function to decide whether or not we should enable DWIM logic for
1580 # git-switch and git-checkout.
1582 # To decide between the following rules in decreasing priority order:
1583 # - the last provided of "--guess" or "--no-guess" explicitly enable or
1584 # disable completion of DWIM logic respectively.
1585 # - If checkout.guess is false, disable completion of DWIM logic.
1586 # - If the --no-track option is provided, take this as a hint to disable the
1587 # DWIM completion logic
1588 # - If GIT_COMPLETION_CHECKOUT_NO_GUESS is set, disable the DWIM completion
1589 # logic, as requested by the user.
1590 # - Enable DWIM logic otherwise.
1592 __git_checkout_default_dwim_mode ()
1594 local last_option dwim_opt="--dwim"
1596 if [ "${GIT_COMPLETION_CHECKOUT_NO_GUESS-}" = "1" ]; then
1600 # --no-track disables DWIM, but with lower priority than
1601 # --guess/--no-guess/checkout.guess
1602 if [ -n "$(__git_find_on_cmdline "--no-track")" ]; then
1606 # checkout.guess = false disables DWIM, but with lower priority than
1607 # --guess/--no-guess
1608 if [ "$(__git config --type=bool checkout.guess)" = "false" ]; then
1612 # Find the last provided --guess or --no-guess
1613 last_option="$(__git_find_last_on_cmdline "--guess --no-guess")"
1614 case "$last_option" in
1628 __git_has_doubledash && return
1630 local dwim_opt="$(__git_checkout_default_dwim_mode)"
1634 # Complete local branches (and DWIM branch
1635 # remote branch names) for an option argument
1636 # specifying a new branch name. This is for
1637 # convenience, assuming new branches are
1638 # possibly based on pre-existing branch names.
1639 __git_complete_refs $dwim_opt --mode="heads"
1648 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
1651 __gitcomp_builtin checkout
1654 # At this point, we've already handled special completion for
1655 # the arguments to -b/-B, and --orphan. There are 3 main
1656 # things left we can possibly complete:
1657 # 1) a start-point for -b/-B, -d/--detach, or --orphan
1658 # 2) a remote head, for --track
1659 # 3) an arbitrary reference, possibly including DWIM names
1662 if [ -n "$(__git_find_on_cmdline "-b -B -d --detach --orphan")" ]; then
1663 __git_complete_refs --mode="refs"
1664 elif [ -n "$(__git_find_on_cmdline "-t --track")" ]; then
1665 __git_complete_refs --mode="remote-heads"
1667 __git_complete_refs $dwim_opt --mode="refs"
1673 __git_sequencer_inprogress_options="--continue --quit --abort --skip"
1675 __git_cherry_pick_inprogress_options=$__git_sequencer_inprogress_options
1679 __git_find_repo_path
1680 if __git_pseudoref_exists CHERRY_PICK_HEAD; then
1681 __gitcomp "$__git_cherry_pick_inprogress_options"
1685 __git_complete_strategy && return
1689 __gitcomp_builtin cherry-pick "" \
1690 "$__git_cherry_pick_inprogress_options"
1702 __gitcomp_builtin clean
1707 # XXX should we check for -x option ?
1708 __git_complete_index_file "--others --directory"
1715 __git_complete_config_variable_name_and_value
1721 __git_complete_config_variable_name_and_value \
1722 --cur="${cur##--config=}"
1726 __gitcomp_builtin clone
1732 __git_untracked_file_modes="all no normal"
1734 __git_trailer_tokens ()
1736 __git config --name-only --get-regexp '^trailer\..*\.key$' | cut -d. -f 2- | rev | cut -d. -f2- | rev
1750 __gitcomp "default scissors strip verbatim whitespace
1751 " "" "${cur##--cleanup=}"
1754 --reuse-message=*|--reedit-message=*|\
1755 --fixup=*|--squash=*)
1756 __git_complete_refs --cur="${cur#*=}"
1759 --untracked-files=*)
1760 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
1764 __gitcomp_nl "$(__git_trailer_tokens)" "" "${cur##--trailer=}" ":"
1768 __gitcomp_builtin commit
1772 if __git rev-parse --verify --quiet HEAD >/dev/null; then
1773 __git_complete_index_file "--committable"
1775 # This is the first commit
1776 __git_complete_index_file "--cached"
1784 __gitcomp_builtin describe
1790 __git_diff_algorithms="myers minimal patience histogram"
1792 __git_diff_submodule_formats="diff log short"
1794 __git_color_moved_opts="no default plain blocks zebra dimmed-zebra"
1796 __git_color_moved_ws_opts="no ignore-space-at-eol ignore-space-change
1797 ignore-all-space allow-indentation-change"
1799 __git_ws_error_highlight_opts="context old new all default"
1801 # Options for the diff machinery (diff, log, show, stash, range-diff, ...)
1802 __git_diff_common_options="--stat --numstat --shortstat --summary
1803 --patch-with-stat --name-only --name-status --color
1804 --no-color --color-words --no-renames --check
1805 --color-moved --color-moved= --no-color-moved
1806 --color-moved-ws= --no-color-moved-ws
1807 --full-index --binary --abbrev --diff-filter=
1808 --find-copies --find-object --find-renames
1809 --no-relative --relative
1810 --find-copies-harder --ignore-cr-at-eol
1811 --text --ignore-space-at-eol --ignore-space-change
1812 --ignore-all-space --ignore-blank-lines --exit-code
1813 --quiet --ext-diff --no-ext-diff --unified=
1814 --no-prefix --src-prefix= --dst-prefix=
1815 --inter-hunk-context= --function-context
1816 --patience --histogram --minimal
1817 --raw --word-diff --word-diff-regex=
1818 --dirstat --dirstat= --dirstat-by-file
1819 --dirstat-by-file= --cumulative
1820 --diff-algorithm= --default-prefix
1821 --submodule --submodule= --ignore-submodules
1822 --indent-heuristic --no-indent-heuristic
1823 --textconv --no-textconv --break-rewrites
1824 --patch --no-patch --cc --combined-all-paths
1825 --anchored= --compact-summary --ignore-matching-lines=
1826 --irreversible-delete --line-prefix --no-stat
1827 --output= --output-indicator-context=
1828 --output-indicator-new= --output-indicator-old=
1829 --ws-error-highlight=
1830 --pickaxe-all --pickaxe-regex
1833 # Options for diff/difftool
1834 __git_diff_difftool_options="--cached --staged
1835 --base --ours --theirs --no-index --merge-base
1836 --ita-invisible-in-index --ita-visible-in-index
1837 $__git_diff_common_options"
1841 __git_has_doubledash && return
1845 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1849 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1853 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
1857 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
1860 --ws-error-highlight=*)
1861 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
1865 __gitcomp "$__git_diff_difftool_options"
1869 __git_complete_revlist_file
1872 __git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff
1873 tkdiff vimdiff nvimdiff gvimdiff xxdiff araxis p4merge
1874 bc codecompare smerge
1879 __git_has_doubledash && return
1883 __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1887 __gitcomp_builtin difftool "$__git_diff_difftool_options"
1891 __git_complete_revlist_file
1894 __git_fetch_recurse_submodules="yes on-demand no"
1899 --recurse-submodules=*)
1900 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1904 __gitcomp "blob:none blob:limit= sparse:oid=" "" "${cur##--filter=}"
1908 __gitcomp_builtin fetch
1912 __git_complete_remote_or_refspec
1915 __git_format_patch_extra_options="
1916 --full-index --not --all --no-prefix --src-prefix=
1917 --dst-prefix= --notes
1920 _git_format_patch ()
1926 " "" "${cur##--thread=}"
1929 --base=*|--interdiff=*|--range-diff=*)
1930 __git_complete_refs --cur="${cur#--*=}"
1934 __gitcomp_builtin format-patch "$__git_format_patch_extra_options"
1938 __git_complete_revlist
1945 __gitcomp_builtin fsck
1956 # Lists matching symbol names from a tag (as in ctags) file.
1957 # 1: List symbol names matching this word.
1958 # 2: The tag file to list symbol names from.
1959 # 3: A prefix to be added to each listed symbol name (optional).
1960 # 4: A suffix to be appended to each listed symbol name (optional).
1961 __git_match_ctag () {
1962 awk -v pfx="${3-}" -v sfx="${4-}" "
1963 /^${1//\//\\/}/ { print pfx \$1 sfx }
1967 # Complete symbol names from a tag file.
1968 # Usage: __git_complete_symbol [<option>]...
1969 # --tags=<file>: The tag file to list symbol names from instead of the
1971 # --pfx=<prefix>: A prefix to be added to each symbol name.
1972 # --cur=<word>: The current symbol name to be completed. Defaults to
1973 # the current word to be completed.
1974 # --sfx=<suffix>: A suffix to be appended to each symbol name instead
1975 # of the default space.
1976 __git_complete_symbol () {
1977 local tags=tags pfx="" cur_="${cur-}" sfx=" "
1979 while test $# != 0; do
1981 --tags=*) tags="${1##--tags=}" ;;
1982 --pfx=*) pfx="${1##--pfx=}" ;;
1983 --cur=*) cur_="${1##--cur=}" ;;
1984 --sfx=*) sfx="${1##--sfx=}" ;;
1990 if test -r "$tags"; then
1991 __gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
1997 __git_has_doubledash && return
2001 __gitcomp_builtin grep
2006 case "$cword,$prev" in
2007 $((__git_cmd_idx+1)),*|*,-*)
2008 __git_complete_symbol && return
2019 __gitcomp_builtin help
2023 if test -n "${GIT_TESTING_ALL_COMMAND_LIST-}"
2025 __gitcomp "$GIT_TESTING_ALL_COMMAND_LIST $(__git --list-cmds=alias,list-guide) gitk"
2027 __gitcomp "$(__git --list-cmds=main,nohelpers,alias,list-guide) gitk"
2036 false true umask group all world everybody
2037 " "" "${cur##--shared=}"
2041 __gitcomp_builtin init
2051 __gitcomp_builtin ls-files
2056 # XXX ignore options like --modified and always suggest all cached
2058 __git_complete_index_file "--cached"
2065 __gitcomp_builtin ls-remote
2069 __gitcomp_nl "$(__git_remotes)"
2076 __gitcomp_builtin ls-tree
2084 # Options that go well for log, shortlog and gitk
2085 __git_log_common_options="
2087 --branches --tags --remotes
2088 --first-parent --merges --no-merges
2090 --max-age= --since= --after=
2091 --min-age= --until= --before=
2092 --min-parents= --max-parents=
2093 --no-min-parents --no-max-parents
2095 # Options that go well for log and gitk (not shortlog)
2096 __git_log_gitk_options="
2097 --dense --sparse --full-history
2098 --simplify-merges --simplify-by-decoration
2099 --left-right --notes --no-notes
2101 # Options that go well for log and shortlog (not gitk)
2102 __git_log_shortlog_options="
2103 --author= --committer= --grep=
2104 --all-match --invert-grep
2106 # Options accepted by log and show
2107 __git_log_show_options="
2108 --diff-merges --diff-merges= --no-diff-merges --dd --remerge-diff
2111 __git_diff_merges_opts="off none on first-parent 1 separate m combined c dense-combined cc remerge r"
2113 __git_log_pretty_formats="oneline short medium full fuller reference email raw format: tformat: mboxrd"
2114 __git_log_date_formats="relative iso8601 iso8601-strict rfc2822 short local default human raw unix auto: format:"
2118 __git_has_doubledash && return
2119 __git_find_repo_path
2122 if __git_pseudoref_exists MERGE_HEAD; then
2125 case "$prev,$cur" in
2127 return # fall back to Bash filename completion
2130 __git_complete_symbol --cur="${cur#:}" --sfx=":"
2134 __git_complete_symbol
2139 --pretty=*|--format=*)
2140 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2145 __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
2149 __gitcomp "full short no" "" "${cur##--decorate=}"
2153 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
2157 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
2160 --ws-error-highlight=*)
2161 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
2165 __gitcomp "sorted unsorted" "" "${cur##--no-walk=}"
2169 __gitcomp "$__git_diff_merges_opts" "" "${cur##--diff-merges=}"
2174 $__git_log_common_options
2175 $__git_log_shortlog_options
2176 $__git_log_gitk_options
2177 $__git_log_show_options
2178 --root --topo-order --date-order --reverse
2179 --follow --full-diff
2180 --abbrev-commit --no-abbrev-commit --abbrev=
2181 --relative-date --date=
2182 --pretty= --format= --oneline
2187 --decorate --decorate= --no-decorate
2189 --no-walk --no-walk= --do-walk
2190 --parents --children
2191 --expand-tabs --expand-tabs= --no-expand-tabs
2193 $__git_diff_common_options
2198 return # fall back to Bash filename completion
2201 __git_complete_symbol --cur="${cur#-L:}" --sfx=":"
2205 __git_complete_symbol --pfx="-G" --cur="${cur#-G}"
2209 __git_complete_symbol --pfx="-S" --cur="${cur#-S}"
2213 __git_complete_revlist
2218 __git_complete_strategy && return
2222 __gitcomp_builtin merge
2232 __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
2236 __gitcomp "--tool= --prompt --no-prompt --gui --no-gui"
2246 __gitcomp_builtin merge-base
2257 __gitcomp_builtin mv
2262 if [ $(__git_count_arguments "mv") -gt 0 ]; then
2263 # We need to show both cached and untracked files (including
2264 # empty directories) since this may not be the last argument.
2265 __git_complete_index_file "--cached --others --directory"
2267 __git_complete_index_file "--cached"
2273 local subcommands='add append copy edit get-ref list merge prune remove show'
2274 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2276 case "$subcommand,$cur" in
2278 __gitcomp_builtin notes
2286 __gitcomp "$subcommands --ref"
2290 *,--reuse-message=*|*,--reedit-message=*)
2291 __git_complete_refs --cur="${cur#*=}"
2294 __gitcomp_builtin notes_$subcommand
2297 # this command does not take a ref, do not complete it
2313 __git_complete_strategy && return
2316 --recurse-submodules=*)
2317 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
2321 __gitcomp_builtin pull
2326 __git_complete_remote_or_refspec
2329 __git_push_recurse_submodules="check on-demand only"
2331 __git_complete_force_with_lease ()
2339 __git_complete_refs --cur="${cur_#*:}"
2342 __git_complete_refs --cur="$cur_"
2351 __gitcomp_nl "$(__git_remotes)"
2354 --recurse-submodules)
2355 __gitcomp "$__git_push_recurse_submodules"
2361 __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
2364 --recurse-submodules=*)
2365 __gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
2368 --force-with-lease=*)
2369 __git_complete_force_with_lease "${cur##--force-with-lease=}"
2373 __gitcomp_builtin push
2377 __git_complete_remote_or_refspec
2385 --creation-factor= --no-dual-color
2386 $__git_diff_common_options
2391 __git_complete_revlist
2394 __git_rebase_inprogress_options="--continue --skip --abort --quit --show-current-patch"
2395 __git_rebase_interactive_inprogress_options="$__git_rebase_inprogress_options --edit-todo"
2399 __git_find_repo_path
2400 if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
2401 __gitcomp "$__git_rebase_interactive_inprogress_options"
2403 elif [ -d "$__git_repo_path"/rebase-apply ] || \
2404 [ -d "$__git_repo_path"/rebase-merge ]; then
2405 __gitcomp "$__git_rebase_inprogress_options"
2408 __git_complete_strategy && return
2411 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
2415 __git_complete_refs --cur="${cur##--onto=}"
2419 __gitcomp_builtin rebase "" \
2420 "$__git_rebase_interactive_inprogress_options"
2429 local subcommands="show delete expire"
2430 local subcommand="$(__git_find_subcommand "$subcommands" "show")"
2432 case "$subcommand,$cur" in
2435 $__git_log_common_options
2443 if [ $((cword - __git_cmd_idx)) -eq 1 ]; then
2444 __gitcompappend "$subcommands" "" "$cur" " "
2448 __git_send_email_confirm_options="always never auto cc compose"
2449 __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
2454 --to|--cc|--bcc|--from)
2455 __gitcomp "$(__git send-email --dump-aliases)"
2463 $__git_send_email_confirm_options
2464 " "" "${cur##--confirm=}"
2469 $__git_send_email_suppresscc_options
2470 " "" "${cur##--suppress-cc=}"
2474 --smtp-encryption=*)
2475 __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
2481 " "" "${cur##--thread=}"
2484 --to=*|--cc=*|--bcc=*|--from=*)
2485 __gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
2489 __gitcomp_builtin send-email "$__git_format_patch_extra_options"
2493 __git_complete_revlist
2504 local untracked_state
2507 --ignore-submodules=*)
2508 __gitcomp "none untracked dirty all" "" "${cur##--ignore-submodules=}"
2511 --untracked-files=*)
2512 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
2517 always never auto column row plain dense nodense
2518 " "" "${cur##--column=}"
2522 __gitcomp_builtin status
2527 untracked_state="$(__git_get_option_value "-u" "--untracked-files=" \
2528 "$__git_untracked_file_modes" "status.showUntrackedFiles")"
2530 case "$untracked_state" in
2532 # --ignored option does not matter
2536 complete_opt="--cached --directory --no-empty-directory --others"
2538 if [ -n "$(__git_find_on_cmdline "--ignored")" ]; then
2539 complete_opt="$complete_opt --ignored --exclude=*"
2544 __git_complete_index_file "$complete_opt"
2549 local dwim_opt="$(__git_checkout_default_dwim_mode)"
2553 # Complete local branches (and DWIM branch
2554 # remote branch names) for an option argument
2555 # specifying a new branch name. This is for
2556 # convenience, assuming new branches are
2557 # possibly based on pre-existing branch names.
2558 __git_complete_refs $dwim_opt --mode="heads"
2567 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
2570 __gitcomp_builtin switch
2573 # Unlike in git checkout, git switch --orphan does not take
2574 # a start point. Thus we really have nothing to complete after
2576 if [ -n "$(__git_find_on_cmdline "--orphan")" ]; then
2580 # At this point, we've already handled special completion for
2581 # -c/-C, and --orphan. There are 3 main things left to
2583 # 1) a start-point for -c/-C or -d/--detach
2584 # 2) a remote head, for --track
2585 # 3) a branch name, possibly including DWIM remote branches
2587 if [ -n "$(__git_find_on_cmdline "-c -C -d --detach")" ]; then
2588 __git_complete_refs --mode="refs"
2589 elif [ -n "$(__git_find_on_cmdline "-t --track")" ]; then
2590 __git_complete_refs --mode="remote-heads"
2592 __git_complete_refs $dwim_opt --mode="heads"
2598 __git_config_get_set_variables ()
2600 local prevword word config_file= c=$cword
2601 while [ $c -gt "$__git_cmd_idx" ]; do
2604 --system|--global|--local|--file=*)
2609 config_file="$word $prevword"
2617 __git config $config_file --name-only --list
2621 __git_compute_config_vars ()
2623 test -n "$__git_config_vars" ||
2624 __git_config_vars="$(git help --config-for-completion)"
2627 __git_config_sections=
2628 __git_compute_config_sections ()
2630 test -n "$__git_config_sections" ||
2631 __git_config_sections="$(git help --config-sections-for-completion)"
2634 # Completes possible values of various configuration variables.
2636 # Usage: __git_complete_config_variable_value [<option>]...
2637 # --varname=<word>: The name of the configuration variable whose value is
2638 # to be completed. Defaults to the previous word on the
2640 # --cur=<word>: The current value to be completed. Defaults to the current
2641 # word to be completed.
2642 __git_complete_config_variable_value ()
2644 local varname="$prev" cur_="$cur"
2646 while test $# != 0; do
2648 --varname=*) varname="${1##--varname=}" ;;
2649 --cur=*) cur_="${1##--cur=}" ;;
2655 if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
2656 varname="${varname,,}"
2658 varname="$(echo "$varname" |tr A-Z a-z)"
2662 branch.*.remote|branch.*.pushremote)
2663 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2667 __git_complete_refs --cur="$cur_"
2671 __gitcomp "false true merges interactive" "" "$cur_"
2675 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2679 local remote="${varname#remote.}"
2680 remote="${remote%.fetch}"
2681 if [ -z "$cur_" ]; then
2682 __gitcomp_nl "refs/heads/" "" "" ""
2685 __gitcomp_nl "$(__git_refs_remotes "$remote")" "" "$cur_"
2689 local remote="${varname#remote.}"
2690 remote="${remote%.push}"
2691 __gitcomp_nl "$(__git for-each-ref \
2692 --format='%(refname):%(refname)' refs/heads)" "" "$cur_"
2695 pull.twohead|pull.octopus)
2696 __git_compute_merge_strategies
2697 __gitcomp "$__git_merge_strategies" "" "$cur_"
2701 __gitcomp "false true" "" "$cur_"
2706 normal black red green yellow blue magenta cyan white
2707 bold dim ul blink reverse
2712 __gitcomp "false true always never auto" "" "$cur_"
2716 __gitcomp "$__git_diff_submodule_formats" "" "$cur_"
2720 __gitcomp "man info web html" "" "$cur_"
2724 __gitcomp "$__git_log_date_formats" "" "$cur_"
2727 sendemail.aliasfiletype)
2728 __gitcomp "mutt mailrc pine elm gnus" "" "$cur_"
2732 __gitcomp "$__git_send_email_confirm_options" "" "$cur_"
2735 sendemail.suppresscc)
2736 __gitcomp "$__git_send_email_suppresscc_options" "" "$cur_"
2739 sendemail.transferencoding)
2740 __gitcomp "7bit 8bit quoted-printable base64" "" "$cur_"
2749 # Completes configuration sections, subsections, variable names.
2751 # Usage: __git_complete_config_variable_name [<option>]...
2752 # --cur=<word>: The current configuration section/variable name to be
2753 # completed. Defaults to the current word to be completed.
2754 # --sfx=<suffix>: A suffix to be appended to each fully completed
2755 # configuration variable name (but not to sections or
2756 # subsections) instead of the default space.
2757 __git_complete_config_variable_name ()
2759 local cur_="$cur" sfx
2761 while test $# != 0; do
2763 --cur=*) cur_="${1##--cur=}" ;;
2764 --sfx=*) sfx="${1##--sfx=}" ;;
2772 local pfx="${cur_%.*}."
2774 __gitcomp "remote pushRemote merge mergeOptions rebase" "$pfx" "$cur_" "$sfx"
2778 local pfx="${cur_%.*}."
2780 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
2781 __gitcomp_nl_append $'autoSetupMerge\nautoSetupRebase\n' "$pfx" "$cur_" "${sfx- }"
2785 local pfx="${cur_%.*}."
2788 argPrompt cmd confirm needsFile noConsole noRescan
2789 prompt revPrompt revUnmerged title
2790 " "$pfx" "$cur_" "$sfx"
2794 local pfx="${cur_%.*}."
2796 __gitcomp "cmd path" "$pfx" "$cur_" "$sfx"
2800 local pfx="${cur_%.*}."
2802 __gitcomp "cmd path" "$pfx" "$cur_" "$sfx"
2806 local pfx="${cur_%.*}."
2808 __gitcomp "cmd path trustExitCode" "$pfx" "$cur_" "$sfx"
2812 local pfx="${cur_%.*}."
2814 __git_compute_all_commands
2815 __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_" "${sfx- }"
2819 local pfx="${cur_%.*}."
2822 url proxy fetch push mirror skipDefaultUpdate
2823 receivepack uploadpack tagOpt pushurl
2824 " "$pfx" "$cur_" "$sfx"
2828 local pfx="${cur_%.*}."
2830 __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
2831 __gitcomp_nl_append "pushDefault" "$pfx" "$cur_" "${sfx- }"
2835 local pfx="${cur_%.*}."
2837 __gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_" "$sfx"
2841 __git_compute_config_vars
2842 __gitcomp "$__git_config_vars" "" "$cur_" "$sfx"
2845 __git_compute_config_sections
2846 __gitcomp "$__git_config_sections" "" "$cur_" "."
2851 # Completes '='-separated configuration sections/variable names and values
2852 # for 'git -c section.name=value'.
2854 # Usage: __git_complete_config_variable_name_and_value [<option>]...
2855 # --cur=<word>: The current configuration section/variable name/value to be
2856 # completed. Defaults to the current word to be completed.
2857 __git_complete_config_variable_name_and_value ()
2861 while test $# != 0; do
2863 --cur=*) cur_="${1##--cur=}" ;;
2871 __git_complete_config_variable_value \
2872 --varname="${cur_%%=*}" --cur="${cur_#*=}"
2875 __git_complete_config_variable_name --cur="$cur_" --sfx='='
2883 --get|--get-all|--unset|--unset-all)
2884 __gitcomp_nl "$(__git_config_get_set_variables)"
2888 __git_complete_config_variable_value
2894 __gitcomp_builtin config
2897 __git_complete_config_variable_name
2905 add rename remove set-head set-branches
2906 get-url set-url show prune update
2908 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2909 if [ -z "$subcommand" ]; then
2912 __gitcomp_builtin remote
2915 __gitcomp "$subcommands"
2921 case "$subcommand,$cur" in
2923 __gitcomp_builtin remote_add
2928 __gitcomp_builtin remote_set-head
2931 __gitcomp_builtin remote_set-branches
2933 set-head,*|set-branches,*)
2934 __git_complete_remote_or_refspec
2937 __gitcomp_builtin remote_update
2940 __gitcomp "$(__git_remotes) $(__git_get_config_variables "remotes")"
2943 __gitcomp_builtin remote_set-url
2946 __gitcomp_builtin remote_get-url
2949 __gitcomp_builtin remote_prune
2952 __gitcomp_nl "$(__git_remotes)"
2961 __gitcomp "short medium long" "" "${cur##--format=}"
2965 __gitcomp_builtin replace
2974 local subcommands="clear forget diff remaining status gc"
2975 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2976 if test -z "$subcommand"
2978 __gitcomp "$subcommands"
2985 __git_has_doubledash && return
2989 __gitcomp_builtin reset
2998 __git_find_repo_path
3008 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
3011 __git_complete_refs --cur="${cur##--source=}"
3014 __gitcomp_builtin restore
3017 if __git_pseudoref_exists HEAD; then
3018 __git_complete_index_file "--modified"
3023 __git_revert_inprogress_options=$__git_sequencer_inprogress_options
3027 __git_find_repo_path
3028 if __git_pseudoref_exists REVERT_HEAD; then
3029 __gitcomp "$__git_revert_inprogress_options"
3032 __git_complete_strategy && return
3035 __gitcomp_builtin revert "" \
3036 "$__git_revert_inprogress_options"
3047 __gitcomp_builtin rm
3052 __git_complete_index_file "--cached"
3057 __git_has_doubledash && return
3062 $__git_log_common_options
3063 $__git_log_shortlog_options
3064 --numbered --summary --email
3069 __git_complete_revlist
3074 __git_has_doubledash && return
3077 --pretty=*|--format=*)
3078 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
3083 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
3087 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
3091 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
3095 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
3098 --ws-error-highlight=*)
3099 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
3103 __gitcomp "$__git_diff_merges_opts" "" "${cur##--diff-merges=}"
3107 __gitcomp "--pretty= --format= --abbrev-commit --no-abbrev-commit
3108 --oneline --show-signature
3109 --expand-tabs --expand-tabs= --no-expand-tabs
3110 $__git_log_show_options
3111 $__git_diff_common_options
3116 __git_complete_revlist_file
3123 __gitcomp_builtin show-branch
3127 __git_complete_revlist
3130 __gitcomp_directories ()
3132 local _tmp_dir _tmp_completions _found=0
3134 # Get the directory of the current token; this differs from dirname
3135 # in that it keeps up to the final trailing slash. If no slash found
3138 _tmp_dir=$BASH_REMATCH
3140 # Find possible directory completions, adding trailing '/' characters,
3141 # de-quoting, and handling unusual characters.
3142 while IFS= read -r -d $'\0' c ; do
3143 # If there are directory completions, find ones that start
3144 # with "$cur", the current token, and put those in COMPREPLY
3145 if [[ $c == "$cur"* ]]; then
3149 done < <(__git ls-tree -z -d --name-only HEAD $_tmp_dir)
3151 if [[ $_found == 0 ]] && [[ "$cur" =~ /$ ]]; then
3152 # No possible further completions any deeper, so assume we're at
3153 # a leaf directory and just consider it complete
3154 __gitcomp_direct_append "$cur "
3155 elif [[ $_found == 0 ]]; then
3156 # No possible completions found. Avoid falling back to
3157 # bash's default file and directory completion, because all
3158 # valid completions have already been searched and the
3159 # fallbacks can do nothing but mislead. In fact, they can
3160 # mislead in three different ways:
3161 # 1) Fallback file completion makes no sense when asking
3162 # for directory completions, as this function does.
3163 # 2) Fallback directory completion is bad because
3164 # e.g. "/pro" is invalid and should NOT complete to
3166 # 3) Fallback file/directory completion only completes
3167 # on paths that exist in the current working tree,
3168 # i.e. which are *already* part of their
3169 # sparse-checkout. Thus, normal file and directory
3170 # completion is always useless for "git
3171 # sparse-checkout add" and is also probelmatic for
3172 # "git sparse-checkout set" unless using it to
3173 # strictly narrow the checkout.
3178 # In non-cone mode, the arguments to {set,add} are supposed to be
3179 # patterns, relative to the toplevel directory. These can be any kind
3180 # of general pattern, like 'subdir/*.c' and we can't complete on all
3181 # of those. However, if the user presses Tab to get tab completion, we
3182 # presume that they are trying to provide a pattern that names a specific
3184 __gitcomp_slash_leading_paths ()
3186 local dequoted_word pfx="" cur_ toplevel
3188 # Since we are dealing with a sparse-checkout, subdirectories may not
3189 # exist in the local working copy. Therefore, we want to run all
3190 # ls-files commands relative to the repository toplevel.
3191 toplevel="$(git rev-parse --show-toplevel)/"
3193 __git_dequote "$cur"
3195 # If the paths provided by the user already start with '/', then
3196 # they are considered relative to the toplevel of the repository
3197 # already. If they do not start with /, then we need to adjust
3198 # them to start with the appropriate prefix.
3204 pfx="$(__git rev-parse --show-prefix)"
3207 # Since sparse-index is limited to cone-mode, in non-cone-mode the
3208 # list of valid paths is precisely the cached files in the index.
3211 # 1) We probably need to take care of cases where ls-files
3212 # responds with special quoting.
3213 # 2) We probably need to take care of cases where ${cur} has
3214 # some kind of special quoting.
3215 # 3) On top of any quoting from 1 & 2, we have to provide an extra
3216 # level of quoting for any paths that contain a '*', '?', '\',
3217 # '[', ']', or leading '#' or '!' since those will be
3218 # interpreted by sparse-checkout as something other than a
3219 # literal path character.
3220 # Since there are two types of quoting here, this might get really
3221 # complex. For now, just punt on all of this...
3222 completions="$(__git -C "${toplevel}" -c core.quotePath=false \
3223 ls-files --cached -- "${pfx}${cur}*" \
3224 | sed -e s%^%/% -e 's%$% %')"
3225 # Note, above, though that we needed all of the completions to be
3226 # prefixed with a '/', and we want to add a space so that bash
3227 # completion will actually complete an entry and let us move on to
3230 # Return what we've found.
3231 if test -n "$completions"; then
3232 # We found some completions; return them
3234 COMPREPLY=($completions)
3236 # Do NOT fall back to bash-style all-local-files-and-dirs
3237 # when we find no match. Such options are worse than
3239 # 1. "git sparse-checkout add" needs paths that are NOT
3240 # currently in the working copy. "git
3241 # sparse-checkout set" does as well, except in the
3242 # special cases when users are only trying to narrow
3243 # their sparse checkout to a subset of what they
3246 # 2. A path like '.config' is ambiguous as to whether
3247 # the user wants all '.config' files throughout the
3248 # tree, or just the one under the current directory.
3249 # It would result in a warning from the
3250 # sparse-checkout command due to this. As such, all
3251 # completions of paths should be prefixed with a
3254 # 3. We don't want paths prefixed with a '/' to
3255 # complete files in the system root directory, we
3256 # want it to complete on files relative to the
3259 # As such, make sure that NO completions are offered rather
3260 # than falling back to bash's default completions.
3265 _git_sparse_checkout ()
3267 local subcommands="list init set disable add reapply"
3268 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3269 local using_cone=true
3270 if [ -z "$subcommand" ]; then
3271 __gitcomp "$subcommands"
3275 case "$subcommand,$cur" in
3277 __gitcomp_builtin sparse-checkout_$subcommand "" "--"
3280 if [[ "$(__git config core.sparseCheckout)" == "true" &&
3281 "$(__git config core.sparseCheckoutCone)" == "false" &&
3282 -z "$(__git_find_on_cmdline --cone)" ]]; then
3285 if [[ -n "$(__git_find_on_cmdline --no-cone)" ]]; then
3288 if [[ "$using_cone" == "true" ]]; then
3289 __gitcomp_directories
3291 __gitcomp_slash_leading_paths
3298 local subcommands='push list show apply clear drop pop create branch'
3299 local subcommand="$(__git_find_on_cmdline "$subcommands save")"
3301 if [ -z "$subcommand" ]; then
3302 case "$((cword - __git_cmd_idx)),$cur" in
3304 __gitcomp_builtin stash_push
3310 __gitcomp "$subcommands"
3316 case "$subcommand,$cur" in
3318 # NEEDSWORK: can we somehow unify this with the options in _git_log() and _git_show()
3319 __gitcomp_builtin stash_list "$__git_log_common_options $__git_diff_common_options"
3322 __gitcomp_builtin stash_show "$__git_diff_common_options"
3325 __gitcomp_builtin "stash_$subcommand"
3328 if [ $cword -eq $((__git_cmd_idx+2)) ]; then
3331 __gitcomp_nl "$(__git stash list \
3332 | sed -n -e 's/:.*//p')"
3335 show,*|apply,*|drop,*|pop,*)
3336 __gitcomp_nl "$(__git stash list \
3337 | sed -n -e 's/:.*//p')"
3344 __git_has_doubledash && return
3346 local subcommands="add status init deinit update set-branch set-url summary foreach sync absorbgitdirs"
3347 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3348 if [ -z "$subcommand" ]; then
3354 __gitcomp "$subcommands"
3360 case "$subcommand,$cur" in
3362 __gitcomp "--branch --force --name --reference --depth"
3365 __gitcomp "--cached --recursive"
3368 __gitcomp "--force --all"
3372 --init --remote --no-fetch
3373 --recommend-shallow --no-recommend-shallow
3374 --force --rebase --merge --reference --depth --recursive --jobs
3378 __gitcomp "--default --branch"
3381 __gitcomp "--cached --files --summary-limit"
3383 foreach,--*|sync,--*)
3384 __gitcomp "--recursive"
3394 init fetch clone rebase dcommit log find-rev
3395 set-tree commit-diff info create-ignore propget
3396 proplist show-ignore show-externals branch tag blame
3397 migrate mkdirs reset gc
3399 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3400 if [ -z "$subcommand" ]; then
3401 __gitcomp "$subcommands"
3403 local remote_opts="--username= --config-dir= --no-auth-cache"
3405 --follow-parent --authors-file= --repack=
3406 --no-metadata --use-svm-props --use-svnsync-props
3407 --log-window-size= --no-checkout --quiet
3408 --repack-flags --use-log-author --localtime
3411 --ignore-paths= --include-paths= $remote_opts
3414 --template= --shared= --trunk= --tags=
3415 --branches= --stdlayout --minimize-url
3416 --no-metadata --use-svm-props --use-svnsync-props
3417 --rewrite-root= --prefix= $remote_opts
3420 --edit --rmdir --find-copies-harder --copy-similarity=
3423 case "$subcommand,$cur" in
3425 __gitcomp "--revision= --fetch-all $fc_opts"
3428 __gitcomp "--revision= $fc_opts $init_opts"
3431 __gitcomp "$init_opts"
3435 --merge --strategy= --verbose --dry-run
3436 --fetch-all --no-rebase --commit-url
3437 --revision --interactive $cmt_opts $fc_opts
3441 __gitcomp "--stdin $cmt_opts $fc_opts"
3443 create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
3444 show-externals,--*|mkdirs,--*)
3445 __gitcomp "--revision="
3449 --limit= --revision= --verbose --incremental
3450 --oneline --show-commit --non-recursive
3451 --authors-file= --color
3456 --merge --verbose --strategy= --local
3457 --fetch-all --dry-run $fc_opts
3461 __gitcomp "--message= --file= --revision= $cmt_opts"
3467 __gitcomp "--dry-run --message --tag"
3470 __gitcomp "--dry-run --message"
3473 __gitcomp "--git-format"
3477 --config-dir= --ignore-paths= --minimize
3478 --no-auth-cache --username=
3482 __gitcomp "--revision= --parent"
3492 local i c="$__git_cmd_idx" f=0
3493 while [ $c -lt $cword ]; do
3496 -d|--delete|-v|--verify)
3497 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3512 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3522 __gitcomp_builtin tag
3532 __git_complete_worktree_paths ()
3535 # Generate completion reply from worktree list skipping the first
3536 # entry: it's the path of the main worktree, which can't be moved,
3537 # removed, locked, etc.
3538 __gitcomp_nl "$(git worktree list --porcelain |
3539 sed -n -e '2,$ s/^worktree //p')"
3544 local subcommands="add list lock move prune remove unlock"
3545 local subcommand subcommand_idx
3547 subcommand="$(__git_find_on_cmdline --show-idx "$subcommands")"
3548 subcommand_idx="${subcommand% *}"
3549 subcommand="${subcommand#* }"
3551 case "$subcommand,$cur" in
3553 __gitcomp "$subcommands"
3556 __gitcomp_builtin worktree_$subcommand
3558 add,*) # usage: git worktree add [<options>] <path> [<commit-ish>]
3559 # Here we are not completing an --option, it's either the
3562 -b|-B) # Complete refs for branch to be created/reseted.
3565 -*) # The previous word is an -o|--option without an
3566 # unstuck argument: have to complete the path for
3567 # the new worktree, so don't list anything, but let
3568 # Bash fall back to filename completion.
3570 *) # The previous word is not an --option, so it must
3571 # be either the 'add' subcommand, the unstuck
3572 # argument of an option (e.g. branch for -b|-B), or
3573 # the path for the new worktree.
3574 if [ $cword -eq $((subcommand_idx+1)) ]; then
3575 # Right after the 'add' subcommand: have to
3576 # complete the path, so fall back to Bash
3577 # filename completion.
3580 case "${words[cword-2]}" in
3581 -b|-B) # After '-b <branch>': have to
3582 # complete the path, so fall back
3583 # to Bash filename completion.
3585 *) # After the path: have to complete
3586 # the ref to be checked out.
3594 lock,*|remove,*|unlock,*)
3595 __git_complete_worktree_paths
3598 if [ $cword -eq $((subcommand_idx+1)) ]; then
3599 # The first parameter must be an existing working
3601 __git_complete_worktree_paths
3603 # The second parameter is the destination: it could
3604 # be any path, so don't list anything, but let Bash
3605 # fall back to filename completion.
3612 __git_complete_common () {
3617 __gitcomp_builtin "$command"
3622 __git_cmds_with_parseopt_helper=
3623 __git_support_parseopt_helper () {
3624 test -n "$__git_cmds_with_parseopt_helper" ||
3625 __git_cmds_with_parseopt_helper="$(__git --list-cmds=parseopt)"
3627 case " $__git_cmds_with_parseopt_helper " in
3637 __git_have_func () {
3638 declare -f -- "$1" >/dev/null 2>&1
3641 __git_complete_command () {
3643 local completion_func="_git_${command//-/_}"
3644 if ! __git_have_func $completion_func &&
3645 __git_have_func _completion_loader
3647 _completion_loader "git-$command"
3649 if __git_have_func $completion_func
3653 elif __git_support_parseopt_helper "$command"
3655 __git_complete_common "$command"
3664 local i c=1 command __git_dir __git_repo_path
3665 local __git_C_args C_args_count=0
3668 while [ $c -lt $cword ]; do
3672 __git_dir="${i#--git-dir=}"
3676 __git_dir="${words[c]}"
3685 -c|--work-tree|--namespace)
3689 __git_C_args[C_args_count++]=-C
3691 __git_C_args[C_args_count++]="${words[c]}"
3704 if [ -z "${command-}" ]; then
3706 --git-dir|-C|--work-tree)
3707 # these need a path argument, let's fall back to
3708 # Bash filename completion
3712 __git_complete_config_variable_name_and_value
3716 # we don't support completing these options' arguments
3735 --no-replace-objects
3740 if test -n "${GIT_TESTING_PORCELAIN_COMMAND_LIST-}"
3742 __gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
3744 local list_cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config
3746 if test "${GIT_COMPLETION_SHOW_ALL_COMMANDS-}" = "1"
3748 list_cmds=builtins,$list_cmds
3750 __gitcomp "$(__git --list-cmds=$list_cmds)"
3757 __git_complete_command "$command" && return
3759 local expansion=$(__git_aliased_command "$command")
3760 if [ -n "$expansion" ]; then
3762 __git_complete_command "$expansion"
3768 __git_has_doubledash && return
3770 local __git_repo_path
3771 __git_find_repo_path
3774 if __git_pseudoref_exists MERGE_HEAD; then
3780 $__git_log_common_options
3781 $__git_log_gitk_options
3787 __git_complete_revlist
3790 if [[ -n ${ZSH_VERSION-} && -z ${GIT_SOURCING_ZSH_COMPLETION-} ]]; then
3791 echo "ERROR: this script is obsolete, please see git-completion.zsh" 1>&2
3797 local cur words cword prev
3798 local __git_cmd_idx=0
3799 _get_comp_words_by_ref -n =: cur words cword prev
3805 local wrapper="__git_wrap${2}"
3806 eval "$wrapper () { __git_func_wrap $2 ; }"
3807 complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
3808 || complete -o default -o nospace -F $wrapper $1
3811 # Setup the completion for git commands
3812 # 1: command or alias
3813 # 2: function to call (e.g. `git
`, `gitk
`, `git_fetch
`)
3818 if __git_have_func $2; then
3820 elif __git_have_func __$2_main; then
3822 elif __git_have_func _$2; then
3825 echo "ERROR: could not find function '$2'" 1>&2
3828 ___git_complete $1 $func
3831 ___git_complete git __git_main
3832 ___git_complete gitk __gitk_main
3834 # The following are necessary only for Cygwin, and only are needed
3835 # when the user has tab-completed the executable name and consequently
3836 # included the '.exe' suffix.
3838 if [ "$OSTYPE" = cygwin ]; then
3839 ___git_complete git.exe __git_main