completion: improve docs for using __git_complete
[git/gitster.git] / contrib / completion / git-completion.bash
blob2cc5997401de94a73fb5035905b3387b620c1c8d
1 # bash/zsh completion support for core Git.
3 # Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
4 # Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
5 # Distributed under the GNU General Public License, version 2.0.
7 # The contained completion routines provide support for completing:
9 # *) local and remote branch names
10 # *) local and remote tag names
11 # *) .git/remotes file names
12 # *) git 'subcommands'
13 # *) git email aliases for git-send-email
14 # *) tree paths within 'ref:path/to/file' expressions
15 # *) file paths within current working directory and index
16 # *) common --long-options
18 # To use these routines:
20 # 1) Copy this file to somewhere (e.g. ~/.git-completion.bash).
21 # 2) Add the following line to your .bashrc/.zshrc:
22 # source ~/.git-completion.bash
23 # 3) Consider changing your PS1 to also show the current branch,
24 # see git-prompt.sh for details.
26 # If you use complex aliases of form '!f() { ... }; f', you can use the null
27 # command ':' as the first command in the function body to declare the desired
28 # completion style. For example '!f() { : git commit ; ... }; f' will
29 # tell the completion to use commit completion. This also works with aliases
30 # of form "!sh -c '...'". For example, "!sh -c ': git commit ; ... '".
31 # Note that "git" is optional --- '!f() { : commit; ...}; f' would complete
32 # just like the 'git commit' command.
34 # If you have a shell command that is not part of git (and is not called as a
35 # git subcommand), but you would still like git-style completion for it, use
36 # __git_complete. For example, to use the same completion as for 'git log' also
37 # for the 'gl' command:
39 # __git_complete gl git_log
41 # Or if the 'gk' command should be completed the same as 'gitk':
43 # __git_complete gk gitk
45 # The second parameter of __git_complete gives the completion function; it is
46 # resolved as a function named "$2", or "__$2_main", or "_$2" in that order.
47 # In the examples above, the actual functions used for completion will be
48 # _git_log and __gitk_main.
50 # Compatible with bash 3.2.57.
52 # You can set the following environment variables to influence the behavior of
53 # the completion routines:
55 # GIT_COMPLETION_CHECKOUT_NO_GUESS
57 # When set to "1", do not include "DWIM" suggestions in git-checkout
58 # and git-switch completion (e.g., completing "foo" when "origin/foo"
59 # exists).
61 # GIT_COMPLETION_SHOW_ALL_COMMANDS
63 # When set to "1" suggest all commands, including plumbing commands
64 # which are hidden by default (e.g. "cat-file" on "git ca<TAB>").
66 # GIT_COMPLETION_SHOW_ALL
68 # When set to "1" suggest all options, including options which are
69 # typically hidden (e.g. '--allow-empty' for 'git commit').
71 # GIT_COMPLETION_IGNORE_CASE
73 # When set, uses for-each-ref '--ignore-case' to find refs that match
74 # case insensitively, even on systems with case sensitive file systems
75 # (e.g., completing tag name "FOO" on "git checkout f<TAB>").
77 case "$COMP_WORDBREAKS" in
78 *:*) : great ;;
79 *) COMP_WORDBREAKS="$COMP_WORDBREAKS:"
80 esac
82 # Discovers the path to the git repository taking any '--git-dir=<path>' and
83 # '-C <path>' options into account and stores it in the $__git_repo_path
84 # variable.
85 __git_find_repo_path ()
87 if [ -n "${__git_repo_path-}" ]; then
88 # we already know where it is
89 return
92 if [ -n "${__git_C_args-}" ]; then
93 __git_repo_path="$(git "${__git_C_args[@]}" \
94 ${__git_dir:+--git-dir="$__git_dir"} \
95 rev-parse --absolute-git-dir 2>/dev/null)"
96 elif [ -n "${__git_dir-}" ]; then
97 test -d "$__git_dir" &&
98 __git_repo_path="$__git_dir"
99 elif [ -n "${GIT_DIR-}" ]; then
100 test -d "$GIT_DIR" &&
101 __git_repo_path="$GIT_DIR"
102 elif [ -d .git ]; then
103 __git_repo_path=.git
104 else
105 __git_repo_path="$(git rev-parse --git-dir 2>/dev/null)"
109 # Deprecated: use __git_find_repo_path() and $__git_repo_path instead
110 # __gitdir accepts 0 or 1 arguments (i.e., location)
111 # returns location of .git repo
112 __gitdir ()
114 if [ -z "${1-}" ]; then
115 __git_find_repo_path || return 1
116 echo "$__git_repo_path"
117 elif [ -d "$1/.git" ]; then
118 echo "$1/.git"
119 else
120 echo "$1"
124 # Runs git with all the options given as argument, respecting any
125 # '--git-dir=<path>' and '-C <path>' options present on the command line
126 __git ()
128 git ${__git_C_args:+"${__git_C_args[@]}"} \
129 ${__git_dir:+--git-dir="$__git_dir"} "$@" 2>/dev/null
132 # Helper function to read the first line of a file into a variable.
133 # __git_eread requires 2 arguments, the file path and the name of the
134 # variable, in that order.
136 # This is taken from git-prompt.sh.
137 __git_eread ()
139 test -r "$1" && IFS=$'\r\n' read -r "$2" <"$1"
142 # Runs git in $__git_repo_path to determine whether a pseudoref exists.
143 # 1: The pseudo-ref to search
144 __git_pseudoref_exists ()
146 local ref=$1
147 local head
149 __git_find_repo_path
151 # If the reftable is in use, we have to shell out to 'git rev-parse'
152 # to determine whether the ref exists instead of looking directly in
153 # the filesystem to determine whether the ref exists. Otherwise, use
154 # Bash builtins since executing Git commands are expensive on some
155 # platforms.
156 if __git_eread "$__git_repo_path/HEAD" head; then
157 if [ "$head" == "ref: refs/heads/.invalid" ]; then
158 __git show-ref --exists "$ref"
159 return $?
163 [ -f "$__git_repo_path/$ref" ]
166 # Removes backslash escaping, single quotes and double quotes from a word,
167 # stores the result in the variable $dequoted_word.
168 # 1: The word to dequote.
169 __git_dequote ()
171 local rest="$1" len ch
173 dequoted_word=""
175 while test -n "$rest"; do
176 len=${#dequoted_word}
177 dequoted_word="$dequoted_word${rest%%[\\\'\"]*}"
178 rest="${rest:$((${#dequoted_word}-$len))}"
180 case "${rest:0:1}" in
182 ch="${rest:1:1}"
183 case "$ch" in
184 $'\n')
187 dequoted_word="$dequoted_word$ch"
189 esac
190 rest="${rest:2}"
193 rest="${rest:1}"
194 len=${#dequoted_word}
195 dequoted_word="$dequoted_word${rest%%\'*}"
196 rest="${rest:$((${#dequoted_word}-$len+1))}"
199 rest="${rest:1}"
200 while test -n "$rest" ; do
201 len=${#dequoted_word}
202 dequoted_word="$dequoted_word${rest%%[\\\"]*}"
203 rest="${rest:$((${#dequoted_word}-$len))}"
204 case "${rest:0:1}" in
206 ch="${rest:1:1}"
207 case "$ch" in
208 \"|\\|\$|\`)
209 dequoted_word="$dequoted_word$ch"
211 $'\n')
214 dequoted_word="$dequoted_word\\$ch"
216 esac
217 rest="${rest:2}"
220 rest="${rest:1}"
221 break
223 esac
224 done
226 esac
227 done
230 # The following function is based on code from:
232 # bash_completion - programmable completion functions for bash 3.2+
234 # Copyright © 2006-2008, Ian Macdonald <ian@caliban.org>
235 # © 2009-2010, Bash Completion Maintainers
236 # <bash-completion-devel@lists.alioth.debian.org>
238 # This program is free software; you can redistribute it and/or modify
239 # it under the terms of the GNU General Public License as published by
240 # the Free Software Foundation; either version 2, or (at your option)
241 # any later version.
243 # This program is distributed in the hope that it will be useful,
244 # but WITHOUT ANY WARRANTY; without even the implied warranty of
245 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
246 # GNU General Public License for more details.
248 # You should have received a copy of the GNU General Public License
249 # along with this program; if not, see <http://www.gnu.org/licenses/>.
251 # The latest version of this software can be obtained here:
253 # http://bash-completion.alioth.debian.org/
255 # RELEASE: 2.x
257 # This function can be used to access a tokenized list of words
258 # on the command line:
260 # __git_reassemble_comp_words_by_ref '=:'
261 # if test "${words_[cword_-1]}" = -w
262 # then
263 # ...
264 # fi
266 # The argument should be a collection of characters from the list of
267 # word completion separators (COMP_WORDBREAKS) to treat as ordinary
268 # characters.
270 # This is roughly equivalent to going back in time and setting
271 # COMP_WORDBREAKS to exclude those characters. The intent is to
272 # make option types like --date=<type> and <rev>:<path> easy to
273 # recognize by treating each shell word as a single token.
275 # It is best not to set COMP_WORDBREAKS directly because the value is
276 # shared with other completion scripts. By the time the completion
277 # function gets called, COMP_WORDS has already been populated so local
278 # changes to COMP_WORDBREAKS have no effect.
280 # Output: words_, cword_, cur_.
282 __git_reassemble_comp_words_by_ref()
284 local exclude i j first
285 # Which word separators to exclude?
286 exclude="${1//[^$COMP_WORDBREAKS]}"
287 cword_=$COMP_CWORD
288 if [ -z "$exclude" ]; then
289 words_=("${COMP_WORDS[@]}")
290 return
292 # List of word completion separators has shrunk;
293 # re-assemble words to complete.
294 for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
295 # Append each nonempty word consisting of just
296 # word separator characters to the current word.
297 first=t
298 while
299 [ $i -gt 0 ] &&
300 [ -n "${COMP_WORDS[$i]}" ] &&
301 # word consists of excluded word separators
302 [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
304 # Attach to the previous token,
305 # unless the previous token is the command name.
306 if [ $j -ge 2 ] && [ -n "$first" ]; then
307 ((j--))
309 first=
310 words_[$j]=${words_[j]}${COMP_WORDS[i]}
311 if [ $i = $COMP_CWORD ]; then
312 cword_=$j
314 if (($i < ${#COMP_WORDS[@]} - 1)); then
315 ((i++))
316 else
317 # Done.
318 return
320 done
321 words_[$j]=${words_[j]}${COMP_WORDS[i]}
322 if [ $i = $COMP_CWORD ]; then
323 cword_=$j
325 done
328 if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
329 _get_comp_words_by_ref ()
331 local exclude cur_ words_ cword_
332 if [ "$1" = "-n" ]; then
333 exclude=$2
334 shift 2
336 __git_reassemble_comp_words_by_ref "$exclude"
337 cur_=${words_[cword_]}
338 while [ $# -gt 0 ]; do
339 case "$1" in
340 cur)
341 cur=$cur_
343 prev)
344 prev=${words_[$cword_-1]}
346 words)
347 words=("${words_[@]}")
349 cword)
350 cword=$cword_
352 esac
353 shift
354 done
358 # Fills the COMPREPLY array with prefiltered words without any additional
359 # processing.
360 # Callers must take care of providing only words that match the current word
361 # to be completed and adding any prefix and/or suffix (trailing space!), if
362 # necessary.
363 # 1: List of newline-separated matching completion words, complete with
364 # prefix and suffix.
365 __gitcomp_direct ()
367 local IFS=$'\n'
369 COMPREPLY=($1)
372 # Similar to __gitcomp_direct, but appends to COMPREPLY instead.
373 # Callers must take care of providing only words that match the current word
374 # to be completed and adding any prefix and/or suffix (trailing space!), if
375 # necessary.
376 # 1: List of newline-separated matching completion words, complete with
377 # prefix and suffix.
378 __gitcomp_direct_append ()
380 local IFS=$'\n'
382 COMPREPLY+=($1)
385 __gitcompappend ()
387 local x i=${#COMPREPLY[@]}
388 for x in $1; do
389 if [[ "$x" == "$3"* ]]; then
390 COMPREPLY[i++]="$2$x$4"
392 done
395 __gitcompadd ()
397 COMPREPLY=()
398 __gitcompappend "$@"
401 # Generates completion reply, appending a space to possible completion words,
402 # if necessary.
403 # It accepts 1 to 4 arguments:
404 # 1: List of possible completion words.
405 # 2: A prefix to be added to each possible completion word (optional).
406 # 3: Generate possible completion matches for this word (optional).
407 # 4: A suffix to be appended to each possible completion word (optional).
408 __gitcomp ()
410 local cur_="${3-$cur}"
412 case "$cur_" in
415 --no-*)
416 local c i=0 IFS=$' \t\n'
417 for c in $1; do
418 if [[ $c == "--" ]]; then
419 continue
421 c="$c${4-}"
422 if [[ $c == "$cur_"* ]]; then
423 case $c in
424 --*=|*.) ;;
425 *) c="$c " ;;
426 esac
427 COMPREPLY[i++]="${2-}$c"
429 done
432 local c i=0 IFS=$' \t\n'
433 for c in $1; do
434 if [[ $c == "--" ]]; then
435 c="--no-...${4-}"
436 if [[ $c == "$cur_"* ]]; then
437 COMPREPLY[i++]="${2-}$c "
439 break
441 c="$c${4-}"
442 if [[ $c == "$cur_"* ]]; then
443 case $c in
444 *=|*.) ;;
445 *) c="$c " ;;
446 esac
447 COMPREPLY[i++]="${2-}$c"
449 done
451 esac
454 # Clear the variables caching builtins' options when (re-)sourcing
455 # the completion script.
456 if [[ -n ${ZSH_VERSION-} ]]; then
457 unset ${(M)${(k)parameters[@]}:#__gitcomp_builtin_*} 2>/dev/null
458 else
459 unset $(compgen -v __gitcomp_builtin_)
462 # This function is equivalent to
464 # __gitcomp "$(git xxx --git-completion-helper) ..."
466 # except that the output is cached. Accept 1-3 arguments:
467 # 1: the git command to execute, this is also the cache key
468 # 2: extra options to be added on top (e.g. negative forms)
469 # 3: options to be excluded
470 __gitcomp_builtin ()
472 # spaces must be replaced with underscore for multi-word
473 # commands, e.g. "git remote add" becomes remote_add.
474 local cmd="$1"
475 local incl="${2-}"
476 local excl="${3-}"
478 local var=__gitcomp_builtin_"${cmd//-/_}"
479 local options
480 eval "options=\${$var-}"
482 if [ -z "$options" ]; then
483 local completion_helper
484 if [ "${GIT_COMPLETION_SHOW_ALL-}" = "1" ]; then
485 completion_helper="--git-completion-helper-all"
486 else
487 completion_helper="--git-completion-helper"
489 # leading and trailing spaces are significant to make
490 # option removal work correctly.
491 options=" $incl $(__git ${cmd/_/ } $completion_helper) " || return
493 for i in $excl; do
494 options="${options/ $i / }"
495 done
496 eval "$var=\"$options\""
499 __gitcomp "$options"
502 # Variation of __gitcomp_nl () that appends to the existing list of
503 # completion candidates, COMPREPLY.
504 __gitcomp_nl_append ()
506 local IFS=$'\n'
507 __gitcompappend "$1" "${2-}" "${3-$cur}" "${4- }"
510 # Generates completion reply from newline-separated possible completion words
511 # by appending a space to all of them.
512 # It accepts 1 to 4 arguments:
513 # 1: List of possible completion words, separated by a single newline.
514 # 2: A prefix to be added to each possible completion word (optional).
515 # 3: Generate possible completion matches for this word (optional).
516 # 4: A suffix to be appended to each possible completion word instead of
517 # the default space (optional). If specified but empty, nothing is
518 # appended.
519 __gitcomp_nl ()
521 COMPREPLY=()
522 __gitcomp_nl_append "$@"
525 # Fills the COMPREPLY array with prefiltered paths without any additional
526 # processing.
527 # Callers must take care of providing only paths that match the current path
528 # to be completed and adding any prefix path components, if necessary.
529 # 1: List of newline-separated matching paths, complete with all prefix
530 # path components.
531 __gitcomp_file_direct ()
533 local IFS=$'\n'
535 COMPREPLY=($1)
537 # use a hack to enable file mode in bash < 4
538 compopt -o filenames +o nospace 2>/dev/null ||
539 compgen -f /non-existing-dir/ >/dev/null ||
540 true
543 # Generates completion reply with compgen from newline-separated possible
544 # completion filenames.
545 # It accepts 1 to 3 arguments:
546 # 1: List of possible completion filenames, separated by a single newline.
547 # 2: A directory prefix to be added to each possible completion filename
548 # (optional).
549 # 3: Generate possible completion matches for this word (optional).
550 __gitcomp_file ()
552 local IFS=$'\n'
554 # XXX does not work when the directory prefix contains a tilde,
555 # since tilde expansion is not applied.
556 # This means that COMPREPLY will be empty and Bash default
557 # completion will be used.
558 __gitcompadd "$1" "${2-}" "${3-$cur}" ""
560 # use a hack to enable file mode in bash < 4
561 compopt -o filenames +o nospace 2>/dev/null ||
562 compgen -f /non-existing-dir/ >/dev/null ||
563 true
566 # Execute 'git ls-files', unless the --committable option is specified, in
567 # which case it runs 'git diff-index' to find out the files that can be
568 # committed. It return paths relative to the directory specified in the first
569 # argument, and using the options specified in the second argument.
570 __git_ls_files_helper ()
572 if [ "$2" = "--committable" ]; then
573 __git -C "$1" -c core.quotePath=false diff-index \
574 --name-only --relative HEAD -- "${3//\\/\\\\}*"
575 else
576 # NOTE: $2 is not quoted in order to support multiple options
577 __git -C "$1" -c core.quotePath=false ls-files \
578 --exclude-standard $2 -- "${3//\\/\\\\}*"
583 # __git_index_files accepts 1 or 2 arguments:
584 # 1: Options to pass to ls-files (required).
585 # 2: A directory path (optional).
586 # If provided, only files within the specified directory are listed.
587 # Sub directories are never recursed. Path must have a trailing
588 # slash.
589 # 3: List only paths matching this path component (optional).
590 __git_index_files ()
592 local root="$2" match="$3"
594 __git_ls_files_helper "$root" "$1" "${match:-?}" |
595 awk -F / -v pfx="${2//\\/\\\\}" '{
596 paths[$1] = 1
598 END {
599 for (p in paths) {
600 if (substr(p, 1, 1) != "\"") {
601 # No special characters, easy!
602 print pfx p
603 continue
606 # The path is quoted.
607 p = dequote(p)
608 if (p == "")
609 continue
611 # Even when a directory name itself does not contain
612 # any special characters, it will still be quoted if
613 # any of its (stripped) trailing path components do.
614 # Because of this we may have seen the same directory
615 # both quoted and unquoted.
616 if (p in paths)
617 # We have seen the same directory unquoted,
618 # skip it.
619 continue
620 else
621 print pfx p
624 function dequote(p, bs_idx, out, esc, esc_idx, dec) {
625 # Skip opening double quote.
626 p = substr(p, 2)
628 # Interpret backslash escape sequences.
629 while ((bs_idx = index(p, "\\")) != 0) {
630 out = out substr(p, 1, bs_idx - 1)
631 esc = substr(p, bs_idx + 1, 1)
632 p = substr(p, bs_idx + 2)
634 if ((esc_idx = index("abtvfr\"\\", esc)) != 0) {
635 # C-style one-character escape sequence.
636 out = out substr("\a\b\t\v\f\r\"\\",
637 esc_idx, 1)
638 } else if (esc == "n") {
639 # Uh-oh, a newline character.
640 # We cannot reliably put a pathname
641 # containing a newline into COMPREPLY,
642 # and the newline would create a mess.
643 # Skip this path.
644 return ""
645 } else {
646 # Must be a \nnn octal value, then.
647 dec = esc * 64 + \
648 substr(p, 1, 1) * 8 + \
649 substr(p, 2, 1)
650 out = out sprintf("%c", dec)
651 p = substr(p, 3)
654 # Drop closing double quote, if there is one.
655 # (There is not any if this is a directory, as it was
656 # already stripped with the trailing path components.)
657 if (substr(p, length(p), 1) == "\"")
658 out = out substr(p, 1, length(p) - 1)
659 else
660 out = out p
662 return out
666 # __git_complete_index_file requires 1 argument:
667 # 1: the options to pass to ls-file
669 # The exception is --committable, which finds the files appropriate commit.
670 __git_complete_index_file ()
672 local dequoted_word pfx="" cur_
674 __git_dequote "$cur"
676 case "$dequoted_word" in
677 ?*/*)
678 pfx="${dequoted_word%/*}/"
679 cur_="${dequoted_word##*/}"
682 cur_="$dequoted_word"
683 esac
685 __gitcomp_file_direct "$(__git_index_files "$1" "$pfx" "$cur_")"
688 # Lists branches from the local repository.
689 # 1: A prefix to be added to each listed branch (optional).
690 # 2: List only branches matching this word (optional; list all branches if
691 # unset or empty).
692 # 3: A suffix to be appended to each listed branch (optional).
693 __git_heads ()
695 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
697 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
698 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
699 "refs/heads/$cur_*" "refs/heads/$cur_*/**"
702 # Lists branches from remote repositories.
703 # 1: A prefix to be added to each listed branch (optional).
704 # 2: List only branches matching this word (optional; list all branches if
705 # unset or empty).
706 # 3: A suffix to be appended to each listed branch (optional).
707 __git_remote_heads ()
709 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
711 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
712 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
713 "refs/remotes/$cur_*" "refs/remotes/$cur_*/**"
716 # Lists tags from the local repository.
717 # Accepts the same positional parameters as __git_heads() above.
718 __git_tags ()
720 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
722 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
723 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
724 "refs/tags/$cur_*" "refs/tags/$cur_*/**"
727 # List unique branches from refs/remotes used for 'git checkout' and 'git
728 # switch' tracking DWIMery.
729 # 1: A prefix to be added to each listed branch (optional)
730 # 2: List only branches matching this word (optional; list all branches if
731 # unset or empty).
732 # 3: A suffix to be appended to each listed branch (optional).
733 __git_dwim_remote_heads ()
735 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
736 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
738 # employ the heuristic used by git checkout and git switch
739 # Try to find a remote branch that cur_es the completion word
740 # but only output if the branch name is unique
741 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
742 --sort="refname:strip=3" \
743 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
744 "refs/remotes/*/$cur_*" "refs/remotes/*/$cur_*/**" | \
745 uniq -u
748 # Lists refs from the local (by default) or from a remote repository.
749 # It accepts 0, 1 or 2 arguments:
750 # 1: The remote to list refs from (optional; ignored, if set but empty).
751 # Can be the name of a configured remote, a path, or a URL.
752 # 2: In addition to local refs, list unique branches from refs/remotes/ for
753 # 'git checkout's tracking DWIMery (optional; ignored, if set but empty).
754 # 3: A prefix to be added to each listed ref (optional).
755 # 4: List only refs matching this word (optional; list all refs if unset or
756 # empty).
757 # 5: A suffix to be appended to each listed ref (optional; ignored, if set
758 # but empty).
760 # Use __git_complete_refs() instead.
761 __git_refs ()
763 local i hash dir track="${2-}"
764 local list_refs_from=path remote="${1-}"
765 local format refs
766 local pfx="${3-}" cur_="${4-$cur}" sfx="${5-}"
767 local match="${4-}"
768 local umatch="${4-}"
769 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
771 __git_find_repo_path
772 dir="$__git_repo_path"
774 if [ -z "$remote" ]; then
775 if [ -z "$dir" ]; then
776 return
778 else
779 if __git_is_configured_remote "$remote"; then
780 # configured remote takes precedence over a
781 # local directory with the same name
782 list_refs_from=remote
783 elif [ -d "$remote/.git" ]; then
784 dir="$remote/.git"
785 elif [ -d "$remote" ]; then
786 dir="$remote"
787 else
788 list_refs_from=url
792 if test "${GIT_COMPLETION_IGNORE_CASE:+1}" = "1"
793 then
794 # uppercase with tr instead of ${match,^^} for bash 3.2 compatibility
795 umatch=$(echo "$match" | tr a-z A-Z 2>/dev/null || echo "$match")
798 if [ "$list_refs_from" = path ]; then
799 if [[ "$cur_" == ^* ]]; then
800 pfx="$pfx^"
801 fer_pfx="$fer_pfx^"
802 cur_=${cur_#^}
803 match=${match#^}
804 umatch=${umatch#^}
806 case "$cur_" in
807 refs|refs/*)
808 format="refname"
809 refs=("$match*" "$match*/**")
810 track=""
813 for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD REBASE_HEAD CHERRY_PICK_HEAD REVERT_HEAD BISECT_HEAD AUTO_MERGE; do
814 case "$i" in
815 $match*|$umatch*)
816 if [ -e "$dir/$i" ]; then
817 echo "$pfx$i$sfx"
820 esac
821 done
822 format="refname:strip=2"
823 refs=("refs/tags/$match*" "refs/tags/$match*/**"
824 "refs/heads/$match*" "refs/heads/$match*/**"
825 "refs/remotes/$match*" "refs/remotes/$match*/**")
827 esac
828 __git_dir="$dir" __git for-each-ref --format="$fer_pfx%($format)$sfx" \
829 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
830 "${refs[@]}"
831 if [ -n "$track" ]; then
832 __git_dwim_remote_heads "$pfx" "$match" "$sfx"
834 return
836 case "$cur_" in
837 refs|refs/*)
838 __git ls-remote "$remote" "$match*" | \
839 while read -r hash i; do
840 case "$i" in
841 *^{}) ;;
842 *) echo "$pfx$i$sfx" ;;
843 esac
844 done
847 if [ "$list_refs_from" = remote ]; then
848 case "HEAD" in
849 $match*|$umatch*) echo "${pfx}HEAD$sfx" ;;
850 esac
851 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
852 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
853 "refs/remotes/$remote/$match*" \
854 "refs/remotes/$remote/$match*/**"
855 else
856 local query_symref
857 case "HEAD" in
858 $match*|$umatch*) query_symref="HEAD" ;;
859 esac
860 __git ls-remote "$remote" $query_symref \
861 "refs/tags/$match*" "refs/heads/$match*" \
862 "refs/remotes/$match*" |
863 while read -r hash i; do
864 case "$i" in
865 *^{}) ;;
866 refs/*) echo "$pfx${i#refs/*/}$sfx" ;;
867 *) echo "$pfx$i$sfx" ;; # symbolic refs
868 esac
869 done
872 esac
875 # Completes refs, short and long, local and remote, symbolic and pseudo.
877 # Usage: __git_complete_refs [<option>]...
878 # --remote=<remote>: The remote to list refs from, can be the name of a
879 # configured remote, a path, or a URL.
880 # --dwim: List unique remote branches for 'git switch's tracking DWIMery.
881 # --pfx=<prefix>: A prefix to be added to each ref.
882 # --cur=<word>: The current ref to be completed. Defaults to the current
883 # word to be completed.
884 # --sfx=<suffix>: A suffix to be appended to each ref instead of the default
885 # space.
886 # --mode=<mode>: What set of refs to complete, one of 'refs' (the default) to
887 # complete all refs, 'heads' to complete only branches, or
888 # 'remote-heads' to complete only remote branches. Note that
889 # --remote is only compatible with --mode=refs.
890 __git_complete_refs ()
892 local remote= dwim= pfx= cur_="$cur" sfx=" " mode="refs"
894 while test $# != 0; do
895 case "$1" in
896 --remote=*) remote="${1##--remote=}" ;;
897 --dwim) dwim="yes" ;;
898 # --track is an old spelling of --dwim
899 --track) dwim="yes" ;;
900 --pfx=*) pfx="${1##--pfx=}" ;;
901 --cur=*) cur_="${1##--cur=}" ;;
902 --sfx=*) sfx="${1##--sfx=}" ;;
903 --mode=*) mode="${1##--mode=}" ;;
904 *) return 1 ;;
905 esac
906 shift
907 done
909 # complete references based on the specified mode
910 case "$mode" in
911 refs)
912 __gitcomp_direct "$(__git_refs "$remote" "" "$pfx" "$cur_" "$sfx")" ;;
913 heads)
914 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" "$sfx")" ;;
915 remote-heads)
916 __gitcomp_direct "$(__git_remote_heads "$pfx" "$cur_" "$sfx")" ;;
918 return 1 ;;
919 esac
921 # Append DWIM remote branch names if requested
922 if [ "$dwim" = "yes" ]; then
923 __gitcomp_direct_append "$(__git_dwim_remote_heads "$pfx" "$cur_" "$sfx")"
927 # __git_refs2 requires 1 argument (to pass to __git_refs)
928 # Deprecated: use __git_complete_fetch_refspecs() instead.
929 __git_refs2 ()
931 local i
932 for i in $(__git_refs "$1"); do
933 echo "$i:$i"
934 done
937 # Completes refspecs for fetching from a remote repository.
938 # 1: The remote repository.
939 # 2: A prefix to be added to each listed refspec (optional).
940 # 3: The ref to be completed as a refspec instead of the current word to be
941 # completed (optional)
942 # 4: A suffix to be appended to each listed refspec instead of the default
943 # space (optional).
944 __git_complete_fetch_refspecs ()
946 local i remote="$1" pfx="${2-}" cur_="${3-$cur}" sfx="${4- }"
948 __gitcomp_direct "$(
949 for i in $(__git_refs "$remote" "" "" "$cur_") ; do
950 echo "$pfx$i:$i$sfx"
951 done
955 # __git_refs_remotes requires 1 argument (to pass to ls-remote)
956 __git_refs_remotes ()
958 local i hash
959 __git ls-remote "$1" 'refs/heads/*' | \
960 while read -r hash i; do
961 echo "$i:refs/remotes/$1/${i#refs/heads/}"
962 done
965 __git_remotes ()
967 __git_find_repo_path
968 test -d "$__git_repo_path/remotes" && ls -1 "$__git_repo_path/remotes"
969 __git remote
972 # Returns true if $1 matches the name of a configured remote, false otherwise.
973 __git_is_configured_remote ()
975 local remote
976 for remote in $(__git_remotes); do
977 if [ "$remote" = "$1" ]; then
978 return 0
980 done
981 return 1
984 __git_list_merge_strategies ()
986 LANG=C LC_ALL=C git merge -s help 2>&1 |
987 sed -n -e '/[Aa]vailable strategies are: /,/^$/{
988 s/\.$//
989 s/.*://
990 s/^[ ]*//
991 s/[ ]*$//
996 __git_merge_strategies=
997 # 'git merge -s help' (and thus detection of the merge strategy
998 # list) fails, unfortunately, if run outside of any git working
999 # tree. __git_merge_strategies is set to the empty string in
1000 # that case, and the detection will be repeated the next time it
1001 # is needed.
1002 __git_compute_merge_strategies ()
1004 test -n "$__git_merge_strategies" ||
1005 __git_merge_strategies=$(__git_list_merge_strategies)
1008 __git_merge_strategy_options="ours theirs subtree subtree= patience
1009 histogram diff-algorithm= ignore-space-change ignore-all-space
1010 ignore-space-at-eol renormalize no-renormalize no-renames
1011 find-renames find-renames= rename-threshold="
1013 __git_complete_revlist_file ()
1015 local dequoted_word pfx ls ref cur_="$cur"
1016 case "$cur_" in
1017 *..?*:*)
1018 return
1020 ?*:*)
1021 ref="${cur_%%:*}"
1022 cur_="${cur_#*:}"
1024 __git_dequote "$cur_"
1026 case "$dequoted_word" in
1027 ?*/*)
1028 pfx="${dequoted_word%/*}"
1029 cur_="${dequoted_word##*/}"
1030 ls="$ref:$pfx"
1031 pfx="$pfx/"
1034 cur_="$dequoted_word"
1035 ls="$ref"
1037 esac
1039 case "$COMP_WORDBREAKS" in
1040 *:*) : great ;;
1041 *) pfx="$ref:$pfx" ;;
1042 esac
1044 __gitcomp_file "$(__git ls-tree "$ls" \
1045 | sed 's/^.* //
1046 s/$//')" \
1047 "$pfx" "$cur_"
1049 *...*)
1050 pfx="${cur_%...*}..."
1051 cur_="${cur_#*...}"
1052 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1054 *..*)
1055 pfx="${cur_%..*}.."
1056 cur_="${cur_#*..}"
1057 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1060 __git_complete_refs
1062 esac
1065 __git_complete_file ()
1067 __git_complete_revlist_file
1070 __git_complete_revlist ()
1072 __git_complete_revlist_file
1075 __git_complete_remote_or_refspec ()
1077 local cur_="$cur" cmd="${words[__git_cmd_idx]}"
1078 local i c=$((__git_cmd_idx+1)) remote="" pfx="" lhs=1 no_complete_refspec=0
1079 if [ "$cmd" = "remote" ]; then
1080 ((c++))
1082 while [ $c -lt $cword ]; do
1083 i="${words[c]}"
1084 case "$i" in
1085 --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
1086 -d|--delete) [ "$cmd" = "push" ] && lhs=0 ;;
1087 --all)
1088 case "$cmd" in
1089 push) no_complete_refspec=1 ;;
1090 fetch)
1091 return
1093 *) ;;
1094 esac
1096 --multiple) no_complete_refspec=1; break ;;
1097 -*) ;;
1098 *) remote="$i"; break ;;
1099 esac
1100 ((c++))
1101 done
1102 if [ -z "$remote" ]; then
1103 __gitcomp_nl "$(__git_remotes)"
1104 return
1106 if [ $no_complete_refspec = 1 ]; then
1107 return
1109 [ "$remote" = "." ] && remote=
1110 case "$cur_" in
1111 *:*)
1112 case "$COMP_WORDBREAKS" in
1113 *:*) : great ;;
1114 *) pfx="${cur_%%:*}:" ;;
1115 esac
1116 cur_="${cur_#*:}"
1117 lhs=0
1120 pfx="+"
1121 cur_="${cur_#+}"
1123 esac
1124 case "$cmd" in
1125 fetch)
1126 if [ $lhs = 1 ]; then
1127 __git_complete_fetch_refspecs "$remote" "$pfx" "$cur_"
1128 else
1129 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1132 pull|remote)
1133 if [ $lhs = 1 ]; then
1134 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
1135 else
1136 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1139 push)
1140 if [ $lhs = 1 ]; then
1141 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1142 else
1143 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
1146 esac
1149 __git_complete_strategy ()
1151 __git_compute_merge_strategies
1152 case "$prev" in
1153 -s|--strategy)
1154 __gitcomp "$__git_merge_strategies"
1155 return 0
1158 __gitcomp "$__git_merge_strategy_options"
1159 return 0
1161 esac
1162 case "$cur" in
1163 --strategy=*)
1164 __gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
1165 return 0
1167 --strategy-option=*)
1168 __gitcomp "$__git_merge_strategy_options" "" "${cur##--strategy-option=}"
1169 return 0
1171 esac
1172 return 1
1175 __git_all_commands=
1176 __git_compute_all_commands ()
1178 test -n "$__git_all_commands" ||
1179 __git_all_commands=$(__git --list-cmds=main,others,alias,nohelpers)
1182 # Lists all set config variables starting with the given section prefix,
1183 # with the prefix removed.
1184 __git_get_config_variables ()
1186 local section="$1" i IFS=$'\n'
1187 for i in $(__git config --name-only --get-regexp "^$section\..*"); do
1188 echo "${i#$section.}"
1189 done
1192 __git_pretty_aliases ()
1194 __git_get_config_variables "pretty"
1197 # __git_aliased_command requires 1 argument
1198 __git_aliased_command ()
1200 local cur=$1 last list= word cmdline
1202 while [[ -n "$cur" ]]; do
1203 if [[ "$list" == *" $cur "* ]]; then
1204 # loop detected
1205 return
1208 cmdline=$(__git config --get "alias.$cur")
1209 list=" $cur $list"
1210 last=$cur
1211 cur=
1213 for word in $cmdline; do
1214 case "$word" in
1215 \!gitk|gitk)
1216 cur="gitk"
1217 break
1219 \!*) : shell command alias ;;
1220 -*) : option ;;
1221 *=*) : setting env ;;
1222 git) : git itself ;;
1223 \(\)) : skip parens of shell function definition ;;
1224 {) : skip start of shell helper function ;;
1225 :) : skip null command ;;
1226 \'*) : skip opening quote after sh -c ;;
1228 cur="${word%;}"
1229 break
1230 esac
1231 done
1232 done
1234 cur=$last
1235 if [[ "$cur" != "$1" ]]; then
1236 echo "$cur"
1240 # Check whether one of the given words is present on the command line,
1241 # and print the first word found.
1243 # Usage: __git_find_on_cmdline [<option>]... "<wordlist>"
1244 # --show-idx: Optionally show the index of the found word in the $words array.
1245 __git_find_on_cmdline ()
1247 local word c="$__git_cmd_idx" show_idx
1249 while test $# -gt 1; do
1250 case "$1" in
1251 --show-idx) show_idx=y ;;
1252 *) return 1 ;;
1253 esac
1254 shift
1255 done
1256 local wordlist="$1"
1258 while [ $c -lt $cword ]; do
1259 for word in $wordlist; do
1260 if [ "$word" = "${words[c]}" ]; then
1261 if [ -n "${show_idx-}" ]; then
1262 echo "$c $word"
1263 else
1264 echo "$word"
1266 return
1268 done
1269 ((c++))
1270 done
1273 # Similar to __git_find_on_cmdline, except that it loops backwards and thus
1274 # prints the *last* word found. Useful for finding which of two options that
1275 # supersede each other came last, such as "--guess" and "--no-guess".
1277 # Usage: __git_find_last_on_cmdline [<option>]... "<wordlist>"
1278 # --show-idx: Optionally show the index of the found word in the $words array.
1279 __git_find_last_on_cmdline ()
1281 local word c=$cword show_idx
1283 while test $# -gt 1; do
1284 case "$1" in
1285 --show-idx) show_idx=y ;;
1286 *) return 1 ;;
1287 esac
1288 shift
1289 done
1290 local wordlist="$1"
1292 while [ $c -gt "$__git_cmd_idx" ]; do
1293 ((c--))
1294 for word in $wordlist; do
1295 if [ "$word" = "${words[c]}" ]; then
1296 if [ -n "$show_idx" ]; then
1297 echo "$c $word"
1298 else
1299 echo "$word"
1301 return
1303 done
1304 done
1307 # Echo the value of an option set on the command line or config
1309 # $1: short option name
1310 # $2: long option name including =
1311 # $3: list of possible values
1312 # $4: config string (optional)
1314 # example:
1315 # result="$(__git_get_option_value "-d" "--do-something=" \
1316 # "yes no" "core.doSomething")"
1318 # result is then either empty (no option set) or "yes" or "no"
1320 # __git_get_option_value requires 3 arguments
1321 __git_get_option_value ()
1323 local c short_opt long_opt val
1324 local result= values config_key word
1326 short_opt="$1"
1327 long_opt="$2"
1328 values="$3"
1329 config_key="$4"
1331 ((c = $cword - 1))
1332 while [ $c -ge 0 ]; do
1333 word="${words[c]}"
1334 for val in $values; do
1335 if [ "$short_opt$val" = "$word" ] ||
1336 [ "$long_opt$val" = "$word" ]; then
1337 result="$val"
1338 break 2
1340 done
1341 ((c--))
1342 done
1344 if [ -n "$config_key" ] && [ -z "$result" ]; then
1345 result="$(__git config "$config_key")"
1348 echo "$result"
1351 __git_has_doubledash ()
1353 local c=1
1354 while [ $c -lt $cword ]; do
1355 if [ "--" = "${words[c]}" ]; then
1356 return 0
1358 ((c++))
1359 done
1360 return 1
1363 # Try to count non option arguments passed on the command line for the
1364 # specified git command.
1365 # When options are used, it is necessary to use the special -- option to
1366 # tell the implementation were non option arguments begin.
1367 # XXX this can not be improved, since options can appear everywhere, as
1368 # an example:
1369 # git mv x -n y
1371 # __git_count_arguments requires 1 argument: the git command executed.
1372 __git_count_arguments ()
1374 local word i c=0
1376 # Skip "git" (first argument)
1377 for ((i=$__git_cmd_idx; i < ${#words[@]}; i++)); do
1378 word="${words[i]}"
1380 case "$word" in
1382 # Good; we can assume that the following are only non
1383 # option arguments.
1384 ((c = 0))
1386 "$1")
1387 # Skip the specified git command and discard git
1388 # main options
1389 ((c = 0))
1392 ((c++))
1394 esac
1395 done
1397 printf "%d" $c
1400 __git_whitespacelist="nowarn warn error error-all fix"
1401 __git_patchformat="mbox stgit stgit-series hg mboxrd"
1402 __git_showcurrentpatch="diff raw"
1403 __git_am_inprogress_options="--skip --continue --resolved --abort --quit --show-current-patch"
1404 __git_quoted_cr="nowarn warn strip"
1406 _git_am ()
1408 __git_find_repo_path
1409 if [ -d "$__git_repo_path"/rebase-apply ]; then
1410 __gitcomp "$__git_am_inprogress_options"
1411 return
1413 case "$cur" in
1414 --whitespace=*)
1415 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1416 return
1418 --patch-format=*)
1419 __gitcomp "$__git_patchformat" "" "${cur##--patch-format=}"
1420 return
1422 --show-current-patch=*)
1423 __gitcomp "$__git_showcurrentpatch" "" "${cur##--show-current-patch=}"
1424 return
1426 --quoted-cr=*)
1427 __gitcomp "$__git_quoted_cr" "" "${cur##--quoted-cr=}"
1428 return
1430 --*)
1431 __gitcomp_builtin am "" \
1432 "$__git_am_inprogress_options"
1433 return
1434 esac
1437 _git_apply ()
1439 case "$cur" in
1440 --whitespace=*)
1441 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1442 return
1444 --*)
1445 __gitcomp_builtin apply
1446 return
1447 esac
1450 _git_add ()
1452 case "$cur" in
1453 --chmod=*)
1454 __gitcomp "+x -x" "" "${cur##--chmod=}"
1455 return
1457 --*)
1458 __gitcomp_builtin add
1459 return
1460 esac
1462 local complete_opt="--others --modified --directory --no-empty-directory"
1463 if test -n "$(__git_find_on_cmdline "-u --update")"
1464 then
1465 complete_opt="--modified"
1467 __git_complete_index_file "$complete_opt"
1470 _git_archive ()
1472 case "$cur" in
1473 --format=*)
1474 __gitcomp "$(git archive --list)" "" "${cur##--format=}"
1475 return
1477 --remote=*)
1478 __gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
1479 return
1481 --*)
1482 __gitcomp_builtin archive "--format= --list --verbose --prefix= --worktree-attributes"
1483 return
1485 esac
1486 __git_complete_file
1489 _git_bisect ()
1491 __git_has_doubledash && return
1493 __git_find_repo_path
1495 # If a bisection is in progress get the terms being used.
1496 local term_bad term_good
1497 if [ -f "$__git_repo_path"/BISECT_TERMS ]; then
1498 term_bad=$(__git bisect terms --term-bad)
1499 term_good=$(__git bisect terms --term-good)
1502 # We will complete any custom terms, but still always complete the
1503 # more usual bad/new/good/old because git bisect gives a good error
1504 # message if these are given when not in use, and that's better than
1505 # silent refusal to complete if the user is confused.
1507 # We want to recognize 'view' but not complete it, because it overlaps
1508 # with 'visualize' too much and is just an alias for it.
1510 local completable_subcommands="start bad new $term_bad good old $term_good terms skip reset visualize replay log run help"
1511 local all_subcommands="$completable_subcommands view"
1513 local subcommand="$(__git_find_on_cmdline "$all_subcommands")"
1515 if [ -z "$subcommand" ]; then
1516 __git_find_repo_path
1517 if [ -f "$__git_repo_path"/BISECT_START ]; then
1518 __gitcomp "$completable_subcommands"
1519 else
1520 __gitcomp "replay start"
1522 return
1525 case "$subcommand" in
1526 start)
1527 case "$cur" in
1528 --*)
1529 __gitcomp "--first-parent --no-checkout --term-new --term-bad --term-old --term-good"
1530 return
1533 __git_complete_refs
1535 esac
1537 terms)
1538 __gitcomp "--term-good --term-old --term-bad --term-new"
1539 return
1541 visualize|view)
1542 __git_complete_log_opts
1543 return
1545 bad|new|"$term_bad"|good|old|"$term_good"|reset|skip)
1546 __git_complete_refs
1550 esac
1553 __git_ref_fieldlist="refname objecttype objectsize objectname upstream push HEAD symref"
1555 _git_branch ()
1557 local i c="$__git_cmd_idx" only_local_ref="n" has_r="n"
1559 while [ $c -lt $cword ]; do
1560 i="${words[c]}"
1561 case "$i" in
1562 -d|-D|--delete|-m|-M|--move|-c|-C|--copy)
1563 only_local_ref="y" ;;
1564 -r|--remotes)
1565 has_r="y" ;;
1566 esac
1567 ((c++))
1568 done
1570 case "$cur" in
1571 --set-upstream-to=*)
1572 __git_complete_refs --cur="${cur##--set-upstream-to=}"
1574 --*)
1575 __gitcomp_builtin branch
1578 if [ $only_local_ref = "y" -a $has_r = "n" ]; then
1579 __gitcomp_direct "$(__git_heads "" "$cur" " ")"
1580 else
1581 __git_complete_refs
1584 esac
1587 _git_bundle ()
1589 local cmd="${words[__git_cmd_idx+1]}"
1590 case "$cword" in
1591 $((__git_cmd_idx+1)))
1592 __gitcomp "create list-heads verify unbundle"
1594 $((__git_cmd_idx+2)))
1595 # looking for a file
1598 case "$cmd" in
1599 create)
1600 __git_complete_revlist
1602 esac
1604 esac
1607 # Helper function to decide whether or not we should enable DWIM logic for
1608 # git-switch and git-checkout.
1610 # To decide between the following rules in decreasing priority order:
1611 # - the last provided of "--guess" or "--no-guess" explicitly enable or
1612 # disable completion of DWIM logic respectively.
1613 # - If checkout.guess is false, disable completion of DWIM logic.
1614 # - If the --no-track option is provided, take this as a hint to disable the
1615 # DWIM completion logic
1616 # - If GIT_COMPLETION_CHECKOUT_NO_GUESS is set, disable the DWIM completion
1617 # logic, as requested by the user.
1618 # - Enable DWIM logic otherwise.
1620 __git_checkout_default_dwim_mode ()
1622 local last_option dwim_opt="--dwim"
1624 if [ "${GIT_COMPLETION_CHECKOUT_NO_GUESS-}" = "1" ]; then
1625 dwim_opt=""
1628 # --no-track disables DWIM, but with lower priority than
1629 # --guess/--no-guess/checkout.guess
1630 if [ -n "$(__git_find_on_cmdline "--no-track")" ]; then
1631 dwim_opt=""
1634 # checkout.guess = false disables DWIM, but with lower priority than
1635 # --guess/--no-guess
1636 if [ "$(__git config --type=bool checkout.guess)" = "false" ]; then
1637 dwim_opt=""
1640 # Find the last provided --guess or --no-guess
1641 last_option="$(__git_find_last_on_cmdline "--guess --no-guess")"
1642 case "$last_option" in
1643 --guess)
1644 dwim_opt="--dwim"
1646 --no-guess)
1647 dwim_opt=""
1649 esac
1651 echo "$dwim_opt"
1654 _git_checkout ()
1656 __git_has_doubledash && return
1658 local dwim_opt="$(__git_checkout_default_dwim_mode)"
1660 case "$prev" in
1661 -b|-B|--orphan)
1662 # Complete local branches (and DWIM branch
1663 # remote branch names) for an option argument
1664 # specifying a new branch name. This is for
1665 # convenience, assuming new branches are
1666 # possibly based on pre-existing branch names.
1667 __git_complete_refs $dwim_opt --mode="heads"
1668 return
1672 esac
1674 case "$cur" in
1675 --conflict=*)
1676 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
1678 --*)
1679 __gitcomp_builtin checkout
1682 # At this point, we've already handled special completion for
1683 # the arguments to -b/-B, and --orphan. There are 3 main
1684 # things left we can possibly complete:
1685 # 1) a start-point for -b/-B, -d/--detach, or --orphan
1686 # 2) a remote head, for --track
1687 # 3) an arbitrary reference, possibly including DWIM names
1690 if [ -n "$(__git_find_on_cmdline "-b -B -d --detach --orphan")" ]; then
1691 __git_complete_refs --mode="refs"
1692 elif [ -n "$(__git_find_on_cmdline "-t --track")" ]; then
1693 __git_complete_refs --mode="remote-heads"
1694 else
1695 __git_complete_refs $dwim_opt --mode="refs"
1698 esac
1701 __git_sequencer_inprogress_options="--continue --quit --abort --skip"
1703 __git_cherry_pick_inprogress_options=$__git_sequencer_inprogress_options
1705 _git_cherry_pick ()
1707 if __git_pseudoref_exists CHERRY_PICK_HEAD; then
1708 __gitcomp "$__git_cherry_pick_inprogress_options"
1709 return
1712 __git_complete_strategy && return
1714 case "$cur" in
1715 --*)
1716 __gitcomp_builtin cherry-pick "" \
1717 "$__git_cherry_pick_inprogress_options"
1720 __git_complete_refs
1722 esac
1725 _git_clean ()
1727 case "$cur" in
1728 --*)
1729 __gitcomp_builtin clean
1730 return
1732 esac
1734 # XXX should we check for -x option ?
1735 __git_complete_index_file "--others --directory"
1738 _git_clone ()
1740 case "$prev" in
1741 -c|--config)
1742 __git_complete_config_variable_name_and_value
1743 return
1745 esac
1746 case "$cur" in
1747 --config=*)
1748 __git_complete_config_variable_name_and_value \
1749 --cur="${cur##--config=}"
1750 return
1752 --*)
1753 __gitcomp_builtin clone
1754 return
1756 esac
1759 __git_untracked_file_modes="all no normal"
1761 __git_trailer_tokens ()
1763 __git config --name-only --get-regexp '^trailer\..*\.key$' | cut -d. -f 2- | rev | cut -d. -f2- | rev
1766 _git_commit ()
1768 case "$prev" in
1769 -c|-C)
1770 __git_complete_refs
1771 return
1773 esac
1775 case "$cur" in
1776 --cleanup=*)
1777 __gitcomp "default scissors strip verbatim whitespace
1778 " "" "${cur##--cleanup=}"
1779 return
1781 --reuse-message=*|--reedit-message=*|\
1782 --fixup=*|--squash=*)
1783 __git_complete_refs --cur="${cur#*=}"
1784 return
1786 --untracked-files=*)
1787 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
1788 return
1790 --trailer=*)
1791 __gitcomp_nl "$(__git_trailer_tokens)" "" "${cur##--trailer=}" ":"
1792 return
1794 --*)
1795 __gitcomp_builtin commit
1796 return
1797 esac
1799 if __git rev-parse --verify --quiet HEAD >/dev/null; then
1800 __git_complete_index_file "--committable"
1801 else
1802 # This is the first commit
1803 __git_complete_index_file "--cached"
1807 _git_describe ()
1809 case "$cur" in
1810 --*)
1811 __gitcomp_builtin describe
1812 return
1813 esac
1814 __git_complete_refs
1817 __git_diff_algorithms="myers minimal patience histogram"
1819 __git_diff_submodule_formats="diff log short"
1821 __git_color_moved_opts="no default plain blocks zebra dimmed-zebra"
1823 __git_color_moved_ws_opts="no ignore-space-at-eol ignore-space-change
1824 ignore-all-space allow-indentation-change"
1826 __git_ws_error_highlight_opts="context old new all default"
1828 # Options for the diff machinery (diff, log, show, stash, range-diff, ...)
1829 __git_diff_common_options="--stat --numstat --shortstat --summary
1830 --patch-with-stat --name-only --name-status --color
1831 --no-color --color-words --no-renames --check
1832 --color-moved --color-moved= --no-color-moved
1833 --color-moved-ws= --no-color-moved-ws
1834 --full-index --binary --abbrev --diff-filter=
1835 --find-copies --find-object --find-renames
1836 --no-relative --relative
1837 --find-copies-harder --ignore-cr-at-eol
1838 --text --ignore-space-at-eol --ignore-space-change
1839 --ignore-all-space --ignore-blank-lines --exit-code
1840 --quiet --ext-diff --no-ext-diff --unified=
1841 --no-prefix --src-prefix= --dst-prefix=
1842 --inter-hunk-context= --function-context
1843 --patience --histogram --minimal
1844 --raw --word-diff --word-diff-regex=
1845 --dirstat --dirstat= --dirstat-by-file
1846 --dirstat-by-file= --cumulative
1847 --diff-algorithm= --default-prefix
1848 --submodule --submodule= --ignore-submodules
1849 --indent-heuristic --no-indent-heuristic
1850 --textconv --no-textconv --break-rewrites
1851 --patch --no-patch --cc --combined-all-paths
1852 --anchored= --compact-summary --ignore-matching-lines=
1853 --irreversible-delete --line-prefix --no-stat
1854 --output= --output-indicator-context=
1855 --output-indicator-new= --output-indicator-old=
1856 --ws-error-highlight=
1857 --pickaxe-all --pickaxe-regex --patch-with-raw
1860 # Options for diff/difftool
1861 __git_diff_difftool_options="--cached --staged
1862 --base --ours --theirs --no-index --merge-base
1863 --ita-invisible-in-index --ita-visible-in-index
1864 $__git_diff_common_options"
1866 _git_diff ()
1868 __git_has_doubledash && return
1870 case "$cur" in
1871 --diff-algorithm=*)
1872 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1873 return
1875 --submodule=*)
1876 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1877 return
1879 --color-moved=*)
1880 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
1881 return
1883 --color-moved-ws=*)
1884 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
1885 return
1887 --ws-error-highlight=*)
1888 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
1889 return
1891 --*)
1892 __gitcomp "$__git_diff_difftool_options"
1893 return
1895 esac
1896 __git_complete_revlist_file
1899 __git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff
1900 tkdiff vimdiff nvimdiff gvimdiff xxdiff araxis p4merge
1901 bc codecompare smerge
1904 _git_difftool ()
1906 __git_has_doubledash && return
1908 case "$cur" in
1909 --tool=*)
1910 __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1911 return
1913 --*)
1914 __gitcomp_builtin difftool "$__git_diff_difftool_options"
1915 return
1917 esac
1918 __git_complete_revlist_file
1921 __git_fetch_recurse_submodules="yes on-demand no"
1923 _git_fetch ()
1925 case "$cur" in
1926 --recurse-submodules=*)
1927 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1928 return
1930 --filter=*)
1931 __gitcomp "blob:none blob:limit= sparse:oid=" "" "${cur##--filter=}"
1932 return
1934 --*)
1935 __gitcomp_builtin fetch
1936 return
1938 esac
1939 __git_complete_remote_or_refspec
1942 __git_format_patch_extra_options="
1943 --full-index --not --all --no-prefix --src-prefix=
1944 --dst-prefix= --notes
1947 _git_format_patch ()
1949 case "$cur" in
1950 --thread=*)
1951 __gitcomp "
1952 deep shallow
1953 " "" "${cur##--thread=}"
1954 return
1956 --base=*|--interdiff=*|--range-diff=*)
1957 __git_complete_refs --cur="${cur#--*=}"
1958 return
1960 --*)
1961 __gitcomp_builtin format-patch "$__git_format_patch_extra_options"
1962 return
1964 esac
1965 __git_complete_revlist
1968 _git_fsck ()
1970 case "$cur" in
1971 --*)
1972 __gitcomp_builtin fsck
1973 return
1975 esac
1978 _git_gitk ()
1980 __gitk_main
1983 # Lists matching symbol names from a tag (as in ctags) file.
1984 # 1: List symbol names matching this word.
1985 # 2: The tag file to list symbol names from.
1986 # 3: A prefix to be added to each listed symbol name (optional).
1987 # 4: A suffix to be appended to each listed symbol name (optional).
1988 __git_match_ctag () {
1989 awk -v pfx="${3-}" -v sfx="${4-}" "
1990 /^${1//\//\\/}/ { print pfx \$1 sfx }
1991 " "$2"
1994 # Complete symbol names from a tag file.
1995 # Usage: __git_complete_symbol [<option>]...
1996 # --tags=<file>: The tag file to list symbol names from instead of the
1997 # default "tags".
1998 # --pfx=<prefix>: A prefix to be added to each symbol name.
1999 # --cur=<word>: The current symbol name to be completed. Defaults to
2000 # the current word to be completed.
2001 # --sfx=<suffix>: A suffix to be appended to each symbol name instead
2002 # of the default space.
2003 __git_complete_symbol () {
2004 local tags=tags pfx="" cur_="${cur-}" sfx=" "
2006 while test $# != 0; do
2007 case "$1" in
2008 --tags=*) tags="${1##--tags=}" ;;
2009 --pfx=*) pfx="${1##--pfx=}" ;;
2010 --cur=*) cur_="${1##--cur=}" ;;
2011 --sfx=*) sfx="${1##--sfx=}" ;;
2012 *) return 1 ;;
2013 esac
2014 shift
2015 done
2017 if test -r "$tags"; then
2018 __gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
2022 _git_grep ()
2024 __git_has_doubledash && return
2026 case "$cur" in
2027 --*)
2028 __gitcomp_builtin grep
2029 return
2031 esac
2033 case "$cword,$prev" in
2034 $((__git_cmd_idx+1)),*|*,-*)
2035 __git_complete_symbol && return
2037 esac
2039 __git_complete_refs
2042 _git_help ()
2044 case "$cur" in
2045 --*)
2046 __gitcomp_builtin help
2047 return
2049 esac
2050 if test -n "${GIT_TESTING_ALL_COMMAND_LIST-}"
2051 then
2052 __gitcomp "$GIT_TESTING_ALL_COMMAND_LIST $(__git --list-cmds=alias,list-guide) gitk"
2053 else
2054 __gitcomp "$(__git --list-cmds=main,nohelpers,alias,list-guide) gitk"
2058 _git_init ()
2060 case "$cur" in
2061 --shared=*)
2062 __gitcomp "
2063 false true umask group all world everybody
2064 " "" "${cur##--shared=}"
2065 return
2067 --*)
2068 __gitcomp_builtin init
2069 return
2071 esac
2074 _git_ls_files ()
2076 case "$cur" in
2077 --*)
2078 __gitcomp_builtin ls-files
2079 return
2081 esac
2083 # XXX ignore options like --modified and always suggest all cached
2084 # files.
2085 __git_complete_index_file "--cached"
2088 _git_ls_remote ()
2090 case "$cur" in
2091 --*)
2092 __gitcomp_builtin ls-remote
2093 return
2095 esac
2096 __gitcomp_nl "$(__git_remotes)"
2099 _git_ls_tree ()
2101 case "$cur" in
2102 --*)
2103 __gitcomp_builtin ls-tree
2104 return
2106 esac
2108 __git_complete_file
2111 # Options that go well for log, shortlog and gitk
2112 __git_log_common_options="
2113 --not --all
2114 --branches --tags --remotes
2115 --first-parent --merges --no-merges
2116 --max-count=
2117 --max-age= --since= --after=
2118 --min-age= --until= --before=
2119 --min-parents= --max-parents=
2120 --no-min-parents --no-max-parents
2121 --alternate-refs --ancestry-path
2122 --author-date-order --basic-regexp
2123 --bisect --boundary --exclude-first-parent-only
2124 --exclude-hidden --extended-regexp
2125 --fixed-strings --grep-reflog
2126 --ignore-missing --left-only --perl-regexp
2127 --reflog --regexp-ignore-case --remove-empty
2128 --right-only --show-linear-break
2129 --show-notes-by-default --show-pulls
2130 --since-as-filter --single-worktree
2132 # Options that go well for log and gitk (not shortlog)
2133 __git_log_gitk_options="
2134 --dense --sparse --full-history
2135 --simplify-merges --simplify-by-decoration
2136 --left-right --notes --no-notes
2138 # Options that go well for log and shortlog (not gitk)
2139 __git_log_shortlog_options="
2140 --author= --committer= --grep=
2141 --all-match --invert-grep
2143 # Options accepted by log and show
2144 __git_log_show_options="
2145 --diff-merges --diff-merges= --no-diff-merges --dd --remerge-diff
2146 --encoding=
2149 __git_diff_merges_opts="off none on first-parent 1 separate m combined c dense-combined cc remerge r"
2151 __git_log_pretty_formats="oneline short medium full fuller reference email raw format: tformat: mboxrd"
2152 __git_log_date_formats="relative iso8601 iso8601-strict rfc2822 short local default human raw unix auto: format:"
2154 # Complete porcelain (i.e. not git-rev-list) options and at least some
2155 # option arguments accepted by git-log. Note that this same set of options
2156 # are also accepted by some other git commands besides git-log.
2157 __git_complete_log_opts ()
2159 COMPREPLY=()
2161 local merge=""
2162 if __git_pseudoref_exists MERGE_HEAD; then
2163 merge="--merge"
2165 case "$prev,$cur" in
2166 -L,:*:*)
2167 return # fall back to Bash filename completion
2169 -L,:*)
2170 __git_complete_symbol --cur="${cur#:}" --sfx=":"
2171 return
2173 -G,*|-S,*)
2174 __git_complete_symbol
2175 return
2177 esac
2178 case "$cur" in
2179 --pretty=*|--format=*)
2180 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2181 " "" "${cur#*=}"
2182 return
2184 --date=*)
2185 __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
2186 return
2188 --decorate=*)
2189 __gitcomp "full short no" "" "${cur##--decorate=}"
2190 return
2192 --diff-algorithm=*)
2193 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
2194 return
2196 --submodule=*)
2197 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
2198 return
2200 --ws-error-highlight=*)
2201 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
2202 return
2204 --no-walk=*)
2205 __gitcomp "sorted unsorted" "" "${cur##--no-walk=}"
2206 return
2208 --diff-merges=*)
2209 __gitcomp "$__git_diff_merges_opts" "" "${cur##--diff-merges=}"
2210 return
2212 --*)
2213 __gitcomp "
2214 $__git_log_common_options
2215 $__git_log_shortlog_options
2216 $__git_log_gitk_options
2217 $__git_log_show_options
2218 --root --topo-order --date-order --reverse
2219 --follow --full-diff
2220 --abbrev-commit --no-abbrev-commit --abbrev=
2221 --relative-date --date=
2222 --pretty= --format= --oneline
2223 --show-signature
2224 --cherry-mark
2225 --cherry-pick
2226 --graph
2227 --decorate --decorate= --no-decorate
2228 --walk-reflogs
2229 --no-walk --no-walk= --do-walk
2230 --parents --children
2231 --expand-tabs --expand-tabs= --no-expand-tabs
2232 --clear-decorations --decorate-refs=
2233 --decorate-refs-exclude=
2234 $merge
2235 $__git_diff_common_options
2237 return
2239 -L:*:*)
2240 return # fall back to Bash filename completion
2242 -L:*)
2243 __git_complete_symbol --cur="${cur#-L:}" --sfx=":"
2244 return
2246 -G*)
2247 __git_complete_symbol --pfx="-G" --cur="${cur#-G}"
2248 return
2250 -S*)
2251 __git_complete_symbol --pfx="-S" --cur="${cur#-S}"
2252 return
2254 esac
2257 _git_log ()
2259 __git_has_doubledash && return
2260 __git_find_repo_path
2262 __git_complete_log_opts
2263 [ ${#COMPREPLY[@]} -eq 0 ] || return
2265 __git_complete_revlist
2268 _git_merge ()
2270 __git_complete_strategy && return
2272 case "$cur" in
2273 --*)
2274 __gitcomp_builtin merge
2275 return
2276 esac
2277 __git_complete_refs
2280 _git_mergetool ()
2282 case "$cur" in
2283 --tool=*)
2284 __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
2285 return
2287 --*)
2288 __gitcomp "--tool= --prompt --no-prompt --gui --no-gui"
2289 return
2291 esac
2294 _git_merge_base ()
2296 case "$cur" in
2297 --*)
2298 __gitcomp_builtin merge-base
2299 return
2301 esac
2302 __git_complete_refs
2305 _git_mv ()
2307 case "$cur" in
2308 --*)
2309 __gitcomp_builtin mv
2310 return
2312 esac
2314 if [ $(__git_count_arguments "mv") -gt 0 ]; then
2315 # We need to show both cached and untracked files (including
2316 # empty directories) since this may not be the last argument.
2317 __git_complete_index_file "--cached --others --directory"
2318 else
2319 __git_complete_index_file "--cached"
2323 _git_notes ()
2325 local subcommands='add append copy edit get-ref list merge prune remove show'
2326 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2328 case "$subcommand,$cur" in
2329 ,--*)
2330 __gitcomp_builtin notes
2333 case "$prev" in
2334 --ref)
2335 __git_complete_refs
2338 __gitcomp "$subcommands --ref"
2340 esac
2342 *,--reuse-message=*|*,--reedit-message=*)
2343 __git_complete_refs --cur="${cur#*=}"
2345 *,--*)
2346 __gitcomp_builtin notes_$subcommand
2348 prune,*|get-ref,*)
2349 # this command does not take a ref, do not complete it
2352 case "$prev" in
2353 -m|-F)
2356 __git_complete_refs
2358 esac
2360 esac
2363 _git_pull ()
2365 __git_complete_strategy && return
2367 case "$cur" in
2368 --recurse-submodules=*)
2369 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
2370 return
2372 --*)
2373 __gitcomp_builtin pull
2375 return
2377 esac
2378 __git_complete_remote_or_refspec
2381 __git_push_recurse_submodules="check on-demand only"
2383 __git_complete_force_with_lease ()
2385 local cur_=$1
2387 case "$cur_" in
2388 --*=)
2390 *:*)
2391 __git_complete_refs --cur="${cur_#*:}"
2394 __git_complete_refs --cur="$cur_"
2396 esac
2399 _git_push ()
2401 case "$prev" in
2402 --repo)
2403 __gitcomp_nl "$(__git_remotes)"
2404 return
2406 --recurse-submodules)
2407 __gitcomp "$__git_push_recurse_submodules"
2408 return
2410 esac
2411 case "$cur" in
2412 --repo=*)
2413 __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
2414 return
2416 --recurse-submodules=*)
2417 __gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
2418 return
2420 --force-with-lease=*)
2421 __git_complete_force_with_lease "${cur##--force-with-lease=}"
2422 return
2424 --*)
2425 __gitcomp_builtin push
2426 return
2428 esac
2429 __git_complete_remote_or_refspec
2432 _git_range_diff ()
2434 case "$cur" in
2435 --*)
2436 __gitcomp "
2437 --creation-factor= --no-dual-color
2438 $__git_diff_common_options
2440 return
2442 esac
2443 __git_complete_revlist
2446 __git_rebase_inprogress_options="--continue --skip --abort --quit --show-current-patch"
2447 __git_rebase_interactive_inprogress_options="$__git_rebase_inprogress_options --edit-todo"
2449 _git_rebase ()
2451 __git_find_repo_path
2452 if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
2453 __gitcomp "$__git_rebase_interactive_inprogress_options"
2454 return
2455 elif [ -d "$__git_repo_path"/rebase-apply ] || \
2456 [ -d "$__git_repo_path"/rebase-merge ]; then
2457 __gitcomp "$__git_rebase_inprogress_options"
2458 return
2460 __git_complete_strategy && return
2461 case "$cur" in
2462 --whitespace=*)
2463 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
2464 return
2466 --onto=*)
2467 __git_complete_refs --cur="${cur##--onto=}"
2468 return
2470 --*)
2471 __gitcomp_builtin rebase "" \
2472 "$__git_rebase_interactive_inprogress_options"
2474 return
2475 esac
2476 __git_complete_refs
2479 _git_reflog ()
2481 local subcommands="show delete expire"
2482 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2484 if [ -z "$subcommand" ]; then
2485 __gitcomp "$subcommands"
2486 else
2487 __git_complete_refs
2491 __git_send_email_confirm_options="always never auto cc compose"
2492 __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
2494 _git_send_email ()
2496 case "$prev" in
2497 --to|--cc|--bcc|--from)
2498 __gitcomp "$(__git send-email --dump-aliases)"
2499 return
2501 esac
2503 case "$cur" in
2504 --confirm=*)
2505 __gitcomp "
2506 $__git_send_email_confirm_options
2507 " "" "${cur##--confirm=}"
2508 return
2510 --suppress-cc=*)
2511 __gitcomp "
2512 $__git_send_email_suppresscc_options
2513 " "" "${cur##--suppress-cc=}"
2515 return
2517 --smtp-encryption=*)
2518 __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
2519 return
2521 --thread=*)
2522 __gitcomp "
2523 deep shallow
2524 " "" "${cur##--thread=}"
2525 return
2527 --to=*|--cc=*|--bcc=*|--from=*)
2528 __gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
2529 return
2531 --*)
2532 __gitcomp_builtin send-email "$__git_format_patch_extra_options"
2533 return
2535 esac
2536 __git_complete_revlist
2539 _git_stage ()
2541 _git_add
2544 _git_status ()
2546 local complete_opt
2547 local untracked_state
2549 case "$cur" in
2550 --ignore-submodules=*)
2551 __gitcomp "none untracked dirty all" "" "${cur##--ignore-submodules=}"
2552 return
2554 --untracked-files=*)
2555 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
2556 return
2558 --column=*)
2559 __gitcomp "
2560 always never auto column row plain dense nodense
2561 " "" "${cur##--column=}"
2562 return
2564 --*)
2565 __gitcomp_builtin status
2566 return
2568 esac
2570 untracked_state="$(__git_get_option_value "-u" "--untracked-files=" \
2571 "$__git_untracked_file_modes" "status.showUntrackedFiles")"
2573 case "$untracked_state" in
2575 # --ignored option does not matter
2576 complete_opt=
2578 all|normal|*)
2579 complete_opt="--cached --directory --no-empty-directory --others"
2581 if [ -n "$(__git_find_on_cmdline "--ignored")" ]; then
2582 complete_opt="$complete_opt --ignored --exclude=*"
2585 esac
2587 __git_complete_index_file "$complete_opt"
2590 _git_switch ()
2592 local dwim_opt="$(__git_checkout_default_dwim_mode)"
2594 case "$prev" in
2595 -c|-C|--orphan)
2596 # Complete local branches (and DWIM branch
2597 # remote branch names) for an option argument
2598 # specifying a new branch name. This is for
2599 # convenience, assuming new branches are
2600 # possibly based on pre-existing branch names.
2601 __git_complete_refs $dwim_opt --mode="heads"
2602 return
2606 esac
2608 case "$cur" in
2609 --conflict=*)
2610 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
2612 --*)
2613 __gitcomp_builtin switch
2616 # Unlike in git checkout, git switch --orphan does not take
2617 # a start point. Thus we really have nothing to complete after
2618 # the branch name.
2619 if [ -n "$(__git_find_on_cmdline "--orphan")" ]; then
2620 return
2623 # At this point, we've already handled special completion for
2624 # -c/-C, and --orphan. There are 3 main things left to
2625 # complete:
2626 # 1) a start-point for -c/-C or -d/--detach
2627 # 2) a remote head, for --track
2628 # 3) a branch name, possibly including DWIM remote branches
2630 if [ -n "$(__git_find_on_cmdline "-c -C -d --detach")" ]; then
2631 __git_complete_refs --mode="refs"
2632 elif [ -n "$(__git_find_on_cmdline "-t --track")" ]; then
2633 __git_complete_refs --mode="remote-heads"
2634 else
2635 __git_complete_refs $dwim_opt --mode="heads"
2638 esac
2641 __git_config_get_set_variables ()
2643 local prevword word config_file= c=$cword
2644 while [ $c -gt "$__git_cmd_idx" ]; do
2645 word="${words[c]}"
2646 case "$word" in
2647 --system|--global|--local|--file=*)
2648 config_file="$word"
2649 break
2651 -f|--file)
2652 config_file="$word $prevword"
2653 break
2655 esac
2656 prevword=$word
2657 c=$((--c))
2658 done
2660 __git config $config_file --name-only --list
2663 __git_config_vars=
2664 __git_compute_config_vars ()
2666 test -n "$__git_config_vars" ||
2667 __git_config_vars="$(git help --config-for-completion)"
2670 __git_config_vars_all=
2671 __git_compute_config_vars_all ()
2673 test -n "$__git_config_vars_all" ||
2674 __git_config_vars_all="$(git --no-pager help --config)"
2677 __git_compute_first_level_config_vars_for_section ()
2679 local section="$1"
2680 __git_compute_config_vars
2681 local this_section="__git_first_level_config_vars_for_section_${section}"
2682 test -n "${!this_section}" ||
2683 printf -v "__git_first_level_config_vars_for_section_${section}" %s "$(echo "$__git_config_vars" | grep -E "^${section}\.[a-z]" | awk -F. '{print $2}')"
2686 __git_compute_second_level_config_vars_for_section ()
2688 local section="$1"
2689 __git_compute_config_vars_all
2690 local this_section="__git_second_level_config_vars_for_section_${section}"
2691 test -n "${!this_section}" ||
2692 printf -v "__git_second_level_config_vars_for_section_${section}" %s "$(echo "$__git_config_vars_all" | grep -E "^${section}\.<" | awk -F. '{print $3}')"
2695 __git_config_sections=
2696 __git_compute_config_sections ()
2698 test -n "$__git_config_sections" ||
2699 __git_config_sections="$(git help --config-sections-for-completion)"
2702 # Completes possible values of various configuration variables.
2704 # Usage: __git_complete_config_variable_value [<option>]...
2705 # --varname=<word>: The name of the configuration variable whose value is
2706 # to be completed. Defaults to the previous word on the
2707 # command line.
2708 # --cur=<word>: The current value to be completed. Defaults to the current
2709 # word to be completed.
2710 __git_complete_config_variable_value ()
2712 local varname="$prev" cur_="$cur"
2714 while test $# != 0; do
2715 case "$1" in
2716 --varname=*) varname="${1##--varname=}" ;;
2717 --cur=*) cur_="${1##--cur=}" ;;
2718 *) return 1 ;;
2719 esac
2720 shift
2721 done
2723 if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
2724 varname="${varname,,}"
2725 else
2726 varname="$(echo "$varname" |tr A-Z a-z)"
2729 case "$varname" in
2730 branch.*.remote|branch.*.pushremote)
2731 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2732 return
2734 branch.*.merge)
2735 __git_complete_refs --cur="$cur_"
2736 return
2738 branch.*.rebase)
2739 __gitcomp "false true merges interactive" "" "$cur_"
2740 return
2742 remote.pushdefault)
2743 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2744 return
2746 remote.*.fetch)
2747 local remote="${varname#remote.}"
2748 remote="${remote%.fetch}"
2749 if [ -z "$cur_" ]; then
2750 __gitcomp_nl "refs/heads/" "" "" ""
2751 return
2753 __gitcomp_nl "$(__git_refs_remotes "$remote")" "" "$cur_"
2754 return
2756 remote.*.push)
2757 local remote="${varname#remote.}"
2758 remote="${remote%.push}"
2759 __gitcomp_nl "$(__git for-each-ref \
2760 --format='%(refname):%(refname)' refs/heads)" "" "$cur_"
2761 return
2763 pull.twohead|pull.octopus)
2764 __git_compute_merge_strategies
2765 __gitcomp "$__git_merge_strategies" "" "$cur_"
2766 return
2768 color.pager)
2769 __gitcomp "false true" "" "$cur_"
2770 return
2772 color.*.*)
2773 __gitcomp "
2774 normal black red green yellow blue magenta cyan white
2775 bold dim ul blink reverse
2776 " "" "$cur_"
2777 return
2779 color.*)
2780 __gitcomp "false true always never auto" "" "$cur_"
2781 return
2783 diff.submodule)
2784 __gitcomp "$__git_diff_submodule_formats" "" "$cur_"
2785 return
2787 help.format)
2788 __gitcomp "man info web html" "" "$cur_"
2789 return
2791 log.date)
2792 __gitcomp "$__git_log_date_formats" "" "$cur_"
2793 return
2795 sendemail.aliasfiletype)
2796 __gitcomp "mutt mailrc pine elm gnus" "" "$cur_"
2797 return
2799 sendemail.confirm)
2800 __gitcomp "$__git_send_email_confirm_options" "" "$cur_"
2801 return
2803 sendemail.suppresscc)
2804 __gitcomp "$__git_send_email_suppresscc_options" "" "$cur_"
2805 return
2807 sendemail.transferencoding)
2808 __gitcomp "7bit 8bit quoted-printable base64" "" "$cur_"
2809 return
2811 *.*)
2812 return
2814 esac
2817 # Completes configuration sections, subsections, variable names.
2819 # Usage: __git_complete_config_variable_name [<option>]...
2820 # --cur=<word>: The current configuration section/variable name to be
2821 # completed. Defaults to the current word to be completed.
2822 # --sfx=<suffix>: A suffix to be appended to each fully completed
2823 # configuration variable name (but not to sections or
2824 # subsections) instead of the default space.
2825 __git_complete_config_variable_name ()
2827 local cur_="$cur" sfx
2829 while test $# != 0; do
2830 case "$1" in
2831 --cur=*) cur_="${1##--cur=}" ;;
2832 --sfx=*) sfx="${1##--sfx=}" ;;
2833 *) return 1 ;;
2834 esac
2835 shift
2836 done
2838 case "$cur_" in
2839 branch.*.*|guitool.*.*|difftool.*.*|man.*.*|mergetool.*.*|remote.*.*|submodule.*.*|url.*.*)
2840 local pfx="${cur_%.*}."
2841 cur_="${cur_##*.}"
2842 local section="${pfx%.*.}"
2843 __git_compute_second_level_config_vars_for_section "${section}"
2844 local this_section="__git_second_level_config_vars_for_section_${section}"
2845 __gitcomp "${!this_section}" "$pfx" "$cur_" "$sfx"
2846 return
2848 branch.*)
2849 local pfx="${cur_%.*}."
2850 cur_="${cur_#*.}"
2851 local section="${pfx%.}"
2852 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
2853 __git_compute_first_level_config_vars_for_section "${section}"
2854 local this_section="__git_first_level_config_vars_for_section_${section}"
2855 __gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
2856 return
2858 pager.*)
2859 local pfx="${cur_%.*}."
2860 cur_="${cur_#*.}"
2861 __git_compute_all_commands
2862 __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_" "${sfx:- }"
2863 return
2865 remote.*)
2866 local pfx="${cur_%.*}."
2867 cur_="${cur_#*.}"
2868 local section="${pfx%.}"
2869 __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
2870 __git_compute_first_level_config_vars_for_section "${section}"
2871 local this_section="__git_first_level_config_vars_for_section_${section}"
2872 __gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
2873 return
2875 submodule.*)
2876 local pfx="${cur_%.*}."
2877 cur_="${cur_#*.}"
2878 local section="${pfx%.}"
2879 __gitcomp_nl "$(__git config -f "$(__git rev-parse --show-toplevel)/.gitmodules" --get-regexp 'submodule.*.path' | awk -F. '{print $2}')" "$pfx" "$cur_" "."
2880 __git_compute_first_level_config_vars_for_section "${section}"
2881 local this_section="__git_first_level_config_vars_for_section_${section}"
2882 __gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
2883 return
2885 *.*)
2886 __git_compute_config_vars
2887 __gitcomp "$__git_config_vars" "" "$cur_" "$sfx"
2890 __git_compute_config_sections
2891 __gitcomp "$__git_config_sections" "" "$cur_" "."
2893 esac
2896 # Completes '='-separated configuration sections/variable names and values
2897 # for 'git -c section.name=value'.
2899 # Usage: __git_complete_config_variable_name_and_value [<option>]...
2900 # --cur=<word>: The current configuration section/variable name/value to be
2901 # completed. Defaults to the current word to be completed.
2902 __git_complete_config_variable_name_and_value ()
2904 local cur_="$cur"
2906 while test $# != 0; do
2907 case "$1" in
2908 --cur=*) cur_="${1##--cur=}" ;;
2909 *) return 1 ;;
2910 esac
2911 shift
2912 done
2914 case "$cur_" in
2915 *=*)
2916 __git_complete_config_variable_value \
2917 --varname="${cur_%%=*}" --cur="${cur_#*=}"
2920 __git_complete_config_variable_name --cur="$cur_" --sfx='='
2922 esac
2925 _git_config ()
2927 case "$prev" in
2928 --get|--get-all|--unset|--unset-all)
2929 __gitcomp_nl "$(__git_config_get_set_variables)"
2930 return
2932 *.*)
2933 __git_complete_config_variable_value
2934 return
2936 esac
2937 case "$cur" in
2938 --*)
2939 __gitcomp_builtin config
2942 __git_complete_config_variable_name
2944 esac
2947 _git_remote ()
2949 local subcommands="
2950 add rename remove set-head set-branches
2951 get-url set-url show prune update
2953 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2954 if [ -z "$subcommand" ]; then
2955 case "$cur" in
2956 --*)
2957 __gitcomp_builtin remote
2960 __gitcomp "$subcommands"
2962 esac
2963 return
2966 case "$subcommand,$cur" in
2967 add,--*)
2968 __gitcomp_builtin remote_add
2970 add,*)
2972 set-head,--*)
2973 __gitcomp_builtin remote_set-head
2975 set-branches,--*)
2976 __gitcomp_builtin remote_set-branches
2978 set-head,*|set-branches,*)
2979 __git_complete_remote_or_refspec
2981 update,--*)
2982 __gitcomp_builtin remote_update
2984 update,*)
2985 __gitcomp "$(__git_remotes) $(__git_get_config_variables "remotes")"
2987 set-url,--*)
2988 __gitcomp_builtin remote_set-url
2990 get-url,--*)
2991 __gitcomp_builtin remote_get-url
2993 prune,--*)
2994 __gitcomp_builtin remote_prune
2997 __gitcomp_nl "$(__git_remotes)"
2999 esac
3002 _git_replace ()
3004 case "$cur" in
3005 --format=*)
3006 __gitcomp "short medium long" "" "${cur##--format=}"
3007 return
3009 --*)
3010 __gitcomp_builtin replace
3011 return
3013 esac
3014 __git_complete_refs
3017 _git_rerere ()
3019 local subcommands="clear forget diff remaining status gc"
3020 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3021 if test -z "$subcommand"
3022 then
3023 __gitcomp "$subcommands"
3024 return
3028 _git_reset ()
3030 __git_has_doubledash && return
3032 case "$cur" in
3033 --*)
3034 __gitcomp_builtin reset
3035 return
3037 esac
3038 __git_complete_refs
3041 _git_restore ()
3043 case "$prev" in
3045 __git_complete_refs
3046 return
3048 esac
3050 case "$cur" in
3051 --conflict=*)
3052 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
3054 --source=*)
3055 __git_complete_refs --cur="${cur##--source=}"
3057 --*)
3058 __gitcomp_builtin restore
3061 if __git_pseudoref_exists HEAD; then
3062 __git_complete_index_file "--modified"
3064 esac
3067 __git_revert_inprogress_options=$__git_sequencer_inprogress_options
3069 _git_revert ()
3071 if __git_pseudoref_exists REVERT_HEAD; then
3072 __gitcomp "$__git_revert_inprogress_options"
3073 return
3075 __git_complete_strategy && return
3076 case "$cur" in
3077 --*)
3078 __gitcomp_builtin revert "" \
3079 "$__git_revert_inprogress_options"
3080 return
3082 esac
3083 __git_complete_refs
3086 _git_rm ()
3088 case "$cur" in
3089 --*)
3090 __gitcomp_builtin rm
3091 return
3093 esac
3095 __git_complete_index_file "--cached"
3098 _git_shortlog ()
3100 __git_has_doubledash && return
3102 case "$cur" in
3103 --*)
3104 __gitcomp "
3105 $__git_log_common_options
3106 $__git_log_shortlog_options
3107 --numbered --summary --email
3109 return
3111 esac
3112 __git_complete_revlist
3115 _git_show ()
3117 __git_has_doubledash && return
3119 case "$cur" in
3120 --pretty=*|--format=*)
3121 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
3122 " "" "${cur#*=}"
3123 return
3125 --diff-algorithm=*)
3126 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
3127 return
3129 --submodule=*)
3130 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
3131 return
3133 --color-moved=*)
3134 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
3135 return
3137 --color-moved-ws=*)
3138 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
3139 return
3141 --ws-error-highlight=*)
3142 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
3143 return
3145 --diff-merges=*)
3146 __gitcomp "$__git_diff_merges_opts" "" "${cur##--diff-merges=}"
3147 return
3149 --*)
3150 __gitcomp "--pretty= --format= --abbrev-commit --no-abbrev-commit
3151 --oneline --show-signature
3152 --expand-tabs --expand-tabs= --no-expand-tabs
3153 $__git_log_show_options
3154 $__git_diff_common_options
3156 return
3158 esac
3159 __git_complete_revlist_file
3162 _git_show_branch ()
3164 case "$cur" in
3165 --*)
3166 __gitcomp_builtin show-branch
3167 return
3169 esac
3170 __git_complete_revlist
3173 __gitcomp_directories ()
3175 local _tmp_dir _tmp_completions _found=0
3177 # Get the directory of the current token; this differs from dirname
3178 # in that it keeps up to the final trailing slash. If no slash found
3179 # that's fine too.
3180 [[ "$cur" =~ .*/ ]]
3181 _tmp_dir=$BASH_REMATCH
3183 # Find possible directory completions, adding trailing '/' characters,
3184 # de-quoting, and handling unusual characters.
3185 while IFS= read -r -d $'\0' c ; do
3186 # If there are directory completions, find ones that start
3187 # with "$cur", the current token, and put those in COMPREPLY
3188 if [[ $c == "$cur"* ]]; then
3189 COMPREPLY+=("$c/")
3190 _found=1
3192 done < <(__git ls-tree -z -d --name-only HEAD $_tmp_dir)
3194 if [[ $_found == 0 ]] && [[ "$cur" =~ /$ ]]; then
3195 # No possible further completions any deeper, so assume we're at
3196 # a leaf directory and just consider it complete
3197 __gitcomp_direct_append "$cur "
3198 elif [[ $_found == 0 ]]; then
3199 # No possible completions found. Avoid falling back to
3200 # bash's default file and directory completion, because all
3201 # valid completions have already been searched and the
3202 # fallbacks can do nothing but mislead. In fact, they can
3203 # mislead in three different ways:
3204 # 1) Fallback file completion makes no sense when asking
3205 # for directory completions, as this function does.
3206 # 2) Fallback directory completion is bad because
3207 # e.g. "/pro" is invalid and should NOT complete to
3208 # "/proc".
3209 # 3) Fallback file/directory completion only completes
3210 # on paths that exist in the current working tree,
3211 # i.e. which are *already* part of their
3212 # sparse-checkout. Thus, normal file and directory
3213 # completion is always useless for "git
3214 # sparse-checkout add" and is also probelmatic for
3215 # "git sparse-checkout set" unless using it to
3216 # strictly narrow the checkout.
3217 COMPREPLY=( "" )
3221 # In non-cone mode, the arguments to {set,add} are supposed to be
3222 # patterns, relative to the toplevel directory. These can be any kind
3223 # of general pattern, like 'subdir/*.c' and we can't complete on all
3224 # of those. However, if the user presses Tab to get tab completion, we
3225 # presume that they are trying to provide a pattern that names a specific
3226 # path.
3227 __gitcomp_slash_leading_paths ()
3229 local dequoted_word pfx="" cur_ toplevel
3231 # Since we are dealing with a sparse-checkout, subdirectories may not
3232 # exist in the local working copy. Therefore, we want to run all
3233 # ls-files commands relative to the repository toplevel.
3234 toplevel="$(git rev-parse --show-toplevel)/"
3236 __git_dequote "$cur"
3238 # If the paths provided by the user already start with '/', then
3239 # they are considered relative to the toplevel of the repository
3240 # already. If they do not start with /, then we need to adjust
3241 # them to start with the appropriate prefix.
3242 case "$cur" in
3244 cur="${cur:1}"
3247 pfx="$(__git rev-parse --show-prefix)"
3248 esac
3250 # Since sparse-index is limited to cone-mode, in non-cone-mode the
3251 # list of valid paths is precisely the cached files in the index.
3253 # NEEDSWORK:
3254 # 1) We probably need to take care of cases where ls-files
3255 # responds with special quoting.
3256 # 2) We probably need to take care of cases where ${cur} has
3257 # some kind of special quoting.
3258 # 3) On top of any quoting from 1 & 2, we have to provide an extra
3259 # level of quoting for any paths that contain a '*', '?', '\',
3260 # '[', ']', or leading '#' or '!' since those will be
3261 # interpreted by sparse-checkout as something other than a
3262 # literal path character.
3263 # Since there are two types of quoting here, this might get really
3264 # complex. For now, just punt on all of this...
3265 completions="$(__git -C "${toplevel}" -c core.quotePath=false \
3266 ls-files --cached -- "${pfx}${cur}*" \
3267 | sed -e s%^%/% -e 's%$% %')"
3268 # Note, above, though that we needed all of the completions to be
3269 # prefixed with a '/', and we want to add a space so that bash
3270 # completion will actually complete an entry and let us move on to
3271 # the next one.
3273 # Return what we've found.
3274 if test -n "$completions"; then
3275 # We found some completions; return them
3276 local IFS=$'\n'
3277 COMPREPLY=($completions)
3278 else
3279 # Do NOT fall back to bash-style all-local-files-and-dirs
3280 # when we find no match. Such options are worse than
3281 # useless:
3282 # 1. "git sparse-checkout add" needs paths that are NOT
3283 # currently in the working copy. "git
3284 # sparse-checkout set" does as well, except in the
3285 # special cases when users are only trying to narrow
3286 # their sparse checkout to a subset of what they
3287 # already have.
3289 # 2. A path like '.config' is ambiguous as to whether
3290 # the user wants all '.config' files throughout the
3291 # tree, or just the one under the current directory.
3292 # It would result in a warning from the
3293 # sparse-checkout command due to this. As such, all
3294 # completions of paths should be prefixed with a
3295 # '/'.
3297 # 3. We don't want paths prefixed with a '/' to
3298 # complete files in the system root directory, we
3299 # want it to complete on files relative to the
3300 # repository root.
3302 # As such, make sure that NO completions are offered rather
3303 # than falling back to bash's default completions.
3304 COMPREPLY=( "" )
3308 _git_sparse_checkout ()
3310 local subcommands="list init set disable add reapply"
3311 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3312 local using_cone=true
3313 if [ -z "$subcommand" ]; then
3314 __gitcomp "$subcommands"
3315 return
3318 case "$subcommand,$cur" in
3319 *,--*)
3320 __gitcomp_builtin sparse-checkout_$subcommand "" "--"
3322 set,*|add,*)
3323 if [[ "$(__git config core.sparseCheckout)" == "true" &&
3324 "$(__git config core.sparseCheckoutCone)" == "false" &&
3325 -z "$(__git_find_on_cmdline --cone)" ]]; then
3326 using_cone=false
3328 if [[ -n "$(__git_find_on_cmdline --no-cone)" ]]; then
3329 using_cone=false
3331 if [[ "$using_cone" == "true" ]]; then
3332 __gitcomp_directories
3333 else
3334 __gitcomp_slash_leading_paths
3336 esac
3339 _git_stash ()
3341 local subcommands='push list show apply clear drop pop create branch'
3342 local subcommand="$(__git_find_on_cmdline "$subcommands save")"
3344 if [ -z "$subcommand" ]; then
3345 case "$((cword - __git_cmd_idx)),$cur" in
3346 *,--*)
3347 __gitcomp_builtin stash_push
3349 1,sa*)
3350 __gitcomp "save"
3352 1,*)
3353 __gitcomp "$subcommands"
3355 esac
3356 return
3359 case "$subcommand,$cur" in
3360 list,--*)
3361 # NEEDSWORK: can we somehow unify this with the options in _git_log() and _git_show()
3362 __gitcomp_builtin stash_list "$__git_log_common_options $__git_diff_common_options"
3364 show,--*)
3365 __gitcomp_builtin stash_show "$__git_diff_common_options"
3367 *,--*)
3368 __gitcomp_builtin "stash_$subcommand"
3370 branch,*)
3371 if [ $cword -eq $((__git_cmd_idx+2)) ]; then
3372 __git_complete_refs
3373 else
3374 __gitcomp_nl "$(__git stash list \
3375 | sed -n -e 's/:.*//p')"
3378 show,*|apply,*|drop,*|pop,*)
3379 __gitcomp_nl "$(__git stash list \
3380 | sed -n -e 's/:.*//p')"
3382 esac
3385 _git_submodule ()
3387 __git_has_doubledash && return
3389 local subcommands="add status init deinit update set-branch set-url summary foreach sync absorbgitdirs"
3390 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3391 if [ -z "$subcommand" ]; then
3392 case "$cur" in
3393 --*)
3394 __gitcomp "--quiet"
3397 __gitcomp "$subcommands"
3399 esac
3400 return
3403 case "$subcommand,$cur" in
3404 add,--*)
3405 __gitcomp "--branch --force --name --reference --depth"
3407 status,--*)
3408 __gitcomp "--cached --recursive"
3410 deinit,--*)
3411 __gitcomp "--force --all"
3413 update,--*)
3414 __gitcomp "
3415 --init --remote --no-fetch
3416 --recommend-shallow --no-recommend-shallow
3417 --force --rebase --merge --reference --depth --recursive --jobs
3420 set-branch,--*)
3421 __gitcomp "--default --branch"
3423 summary,--*)
3424 __gitcomp "--cached --files --summary-limit"
3426 foreach,--*|sync,--*)
3427 __gitcomp "--recursive"
3431 esac
3434 _git_svn ()
3436 local subcommands="
3437 init fetch clone rebase dcommit log find-rev
3438 set-tree commit-diff info create-ignore propget
3439 proplist show-ignore show-externals branch tag blame
3440 migrate mkdirs reset gc
3442 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3443 if [ -z "$subcommand" ]; then
3444 __gitcomp "$subcommands"
3445 else
3446 local remote_opts="--username= --config-dir= --no-auth-cache"
3447 local fc_opts="
3448 --follow-parent --authors-file= --repack=
3449 --no-metadata --use-svm-props --use-svnsync-props
3450 --log-window-size= --no-checkout --quiet
3451 --repack-flags --use-log-author --localtime
3452 --add-author-from
3453 --recursive
3454 --ignore-paths= --include-paths= $remote_opts
3456 local init_opts="
3457 --template= --shared= --trunk= --tags=
3458 --branches= --stdlayout --minimize-url
3459 --no-metadata --use-svm-props --use-svnsync-props
3460 --rewrite-root= --prefix= $remote_opts
3462 local cmt_opts="
3463 --edit --rmdir --find-copies-harder --copy-similarity=
3466 case "$subcommand,$cur" in
3467 fetch,--*)
3468 __gitcomp "--revision= --fetch-all $fc_opts"
3470 clone,--*)
3471 __gitcomp "--revision= $fc_opts $init_opts"
3473 init,--*)
3474 __gitcomp "$init_opts"
3476 dcommit,--*)
3477 __gitcomp "
3478 --merge --strategy= --verbose --dry-run
3479 --fetch-all --no-rebase --commit-url
3480 --revision --interactive $cmt_opts $fc_opts
3483 set-tree,--*)
3484 __gitcomp "--stdin $cmt_opts $fc_opts"
3486 create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
3487 show-externals,--*|mkdirs,--*)
3488 __gitcomp "--revision="
3490 log,--*)
3491 __gitcomp "
3492 --limit= --revision= --verbose --incremental
3493 --oneline --show-commit --non-recursive
3494 --authors-file= --color
3497 rebase,--*)
3498 __gitcomp "
3499 --merge --verbose --strategy= --local
3500 --fetch-all --dry-run $fc_opts
3503 commit-diff,--*)
3504 __gitcomp "--message= --file= --revision= $cmt_opts"
3506 info,--*)
3507 __gitcomp "--url"
3509 branch,--*)
3510 __gitcomp "--dry-run --message --tag"
3512 tag,--*)
3513 __gitcomp "--dry-run --message"
3515 blame,--*)
3516 __gitcomp "--git-format"
3518 migrate,--*)
3519 __gitcomp "
3520 --config-dir= --ignore-paths= --minimize
3521 --no-auth-cache --username=
3524 reset,--*)
3525 __gitcomp "--revision= --parent"
3529 esac
3533 _git_symbolic_ref () {
3534 case "$cur" in
3535 --*)
3536 __gitcomp_builtin symbolic-ref
3537 return
3539 esac
3541 __git_complete_refs
3544 _git_tag ()
3546 local i c="$__git_cmd_idx" f=0
3547 while [ $c -lt $cword ]; do
3548 i="${words[c]}"
3549 case "$i" in
3550 -d|--delete|-v|--verify)
3551 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3552 return
3557 esac
3558 ((c++))
3559 done
3561 case "$prev" in
3562 -m|-F)
3564 -*|tag)
3565 if [ $f = 1 ]; then
3566 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3570 __git_complete_refs
3572 esac
3574 case "$cur" in
3575 --*)
3576 __gitcomp_builtin tag
3578 esac
3581 _git_whatchanged ()
3583 _git_log
3586 __git_complete_worktree_paths ()
3588 local IFS=$'\n'
3589 # Generate completion reply from worktree list skipping the first
3590 # entry: it's the path of the main worktree, which can't be moved,
3591 # removed, locked, etc.
3592 __gitcomp_nl "$(git worktree list --porcelain |
3593 sed -n -e '2,$ s/^worktree //p')"
3596 _git_worktree ()
3598 local subcommands="add list lock move prune remove unlock"
3599 local subcommand subcommand_idx
3601 subcommand="$(__git_find_on_cmdline --show-idx "$subcommands")"
3602 subcommand_idx="${subcommand% *}"
3603 subcommand="${subcommand#* }"
3605 case "$subcommand,$cur" in
3607 __gitcomp "$subcommands"
3609 *,--*)
3610 __gitcomp_builtin worktree_$subcommand
3612 add,*) # usage: git worktree add [<options>] <path> [<commit-ish>]
3613 # Here we are not completing an --option, it's either the
3614 # path or a ref.
3615 case "$prev" in
3616 -b|-B) # Complete refs for branch to be created/reseted.
3617 __git_complete_refs
3619 -*) # The previous word is an -o|--option without an
3620 # unstuck argument: have to complete the path for
3621 # the new worktree, so don't list anything, but let
3622 # Bash fall back to filename completion.
3624 *) # The previous word is not an --option, so it must
3625 # be either the 'add' subcommand, the unstuck
3626 # argument of an option (e.g. branch for -b|-B), or
3627 # the path for the new worktree.
3628 if [ $cword -eq $((subcommand_idx+1)) ]; then
3629 # Right after the 'add' subcommand: have to
3630 # complete the path, so fall back to Bash
3631 # filename completion.
3633 else
3634 case "${words[cword-2]}" in
3635 -b|-B) # After '-b <branch>': have to
3636 # complete the path, so fall back
3637 # to Bash filename completion.
3639 *) # After the path: have to complete
3640 # the ref to be checked out.
3641 __git_complete_refs
3643 esac
3646 esac
3648 lock,*|remove,*|unlock,*)
3649 __git_complete_worktree_paths
3651 move,*)
3652 if [ $cword -eq $((subcommand_idx+1)) ]; then
3653 # The first parameter must be an existing working
3654 # tree to be moved.
3655 __git_complete_worktree_paths
3656 else
3657 # The second parameter is the destination: it could
3658 # be any path, so don't list anything, but let Bash
3659 # fall back to filename completion.
3663 esac
3666 __git_complete_common () {
3667 local command="$1"
3669 case "$cur" in
3670 --*)
3671 __gitcomp_builtin "$command"
3673 esac
3676 __git_cmds_with_parseopt_helper=
3677 __git_support_parseopt_helper () {
3678 test -n "$__git_cmds_with_parseopt_helper" ||
3679 __git_cmds_with_parseopt_helper="$(__git --list-cmds=parseopt)"
3681 case " $__git_cmds_with_parseopt_helper " in
3682 *" $1 "*)
3683 return 0
3686 return 1
3688 esac
3691 __git_have_func () {
3692 declare -f -- "$1" >/dev/null 2>&1
3695 __git_complete_command () {
3696 local command="$1"
3697 local completion_func="_git_${command//-/_}"
3698 if ! __git_have_func $completion_func &&
3699 __git_have_func _completion_loader
3700 then
3701 _completion_loader "git-$command"
3703 if __git_have_func $completion_func
3704 then
3705 $completion_func
3706 return 0
3707 elif __git_support_parseopt_helper "$command"
3708 then
3709 __git_complete_common "$command"
3710 return 0
3711 else
3712 return 1
3716 __git_main ()
3718 local i c=1 command __git_dir __git_repo_path
3719 local __git_C_args C_args_count=0
3720 local __git_cmd_idx
3722 while [ $c -lt $cword ]; do
3723 i="${words[c]}"
3724 case "$i" in
3725 --git-dir=*)
3726 __git_dir="${i#--git-dir=}"
3728 --git-dir)
3729 ((c++))
3730 __git_dir="${words[c]}"
3732 --bare)
3733 __git_dir="."
3735 --help)
3736 command="help"
3737 break
3739 -c|--work-tree|--namespace)
3740 ((c++))
3743 __git_C_args[C_args_count++]=-C
3744 ((c++))
3745 __git_C_args[C_args_count++]="${words[c]}"
3750 command="$i"
3751 __git_cmd_idx="$c"
3752 break
3754 esac
3755 ((c++))
3756 done
3758 if [ -z "${command-}" ]; then
3759 case "$prev" in
3760 --git-dir|-C|--work-tree)
3761 # these need a path argument, let's fall back to
3762 # Bash filename completion
3763 return
3766 __git_complete_config_variable_name_and_value
3767 return
3769 --namespace)
3770 # we don't support completing these options' arguments
3771 return
3773 esac
3774 case "$cur" in
3775 --*)
3776 __gitcomp "
3777 --paginate
3778 --no-pager
3779 --git-dir=
3780 --bare
3781 --version
3782 --exec-path
3783 --exec-path=
3784 --html-path
3785 --man-path
3786 --info-path
3787 --work-tree=
3788 --namespace=
3789 --no-replace-objects
3790 --help
3794 if test -n "${GIT_TESTING_PORCELAIN_COMMAND_LIST-}"
3795 then
3796 __gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
3797 else
3798 local list_cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config
3800 if test "${GIT_COMPLETION_SHOW_ALL_COMMANDS-}" = "1"
3801 then
3802 list_cmds=builtins,$list_cmds
3804 __gitcomp "$(__git --list-cmds=$list_cmds)"
3807 esac
3808 return
3811 __git_complete_command "$command" && return
3813 local expansion=$(__git_aliased_command "$command")
3814 if [ -n "$expansion" ]; then
3815 words[1]=$expansion
3816 __git_complete_command "$expansion"
3820 __gitk_main ()
3822 __git_has_doubledash && return
3824 local __git_repo_path
3825 __git_find_repo_path
3827 local merge=""
3828 if __git_pseudoref_exists MERGE_HEAD; then
3829 merge="--merge"
3831 case "$cur" in
3832 --*)
3833 __gitcomp "
3834 $__git_log_common_options
3835 $__git_log_gitk_options
3836 $merge
3838 return
3840 esac
3841 __git_complete_revlist
3844 if [[ -n ${ZSH_VERSION-} && -z ${GIT_SOURCING_ZSH_COMPLETION-} ]]; then
3845 echo "ERROR: this script is obsolete, please see git-completion.zsh" 1>&2
3846 return
3849 __git_func_wrap ()
3851 local cur words cword prev
3852 local __git_cmd_idx=0
3853 _get_comp_words_by_ref -n =: cur words cword prev
3857 ___git_complete ()
3859 local wrapper="__git_wrap${2}"
3860 eval "$wrapper () { __git_func_wrap $2 ; }"
3861 complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
3862 || complete -o default -o nospace -F $wrapper $1
3865 # Setup the completion for git commands
3866 # 1: command or alias
3867 # 2: function to call (e.g. `git`, `gitk`, `git_fetch`)
3868 __git_complete ()
3870 local func
3872 if __git_have_func $2; then
3873 func=$2
3874 elif __git_have_func __$2_main; then
3875 func=__$2_main
3876 elif __git_have_func _$2; then
3877 func=_$2
3878 else
3879 echo "ERROR: could not find function '$2'" 1>&2
3880 return 1
3882 ___git_complete $1 $func
3885 ___git_complete git __git_main
3886 ___git_complete gitk __gitk_main
3888 # The following are necessary only for Cygwin, and only are needed
3889 # when the user has tab-completed the executable name and consequently
3890 # included the '.exe' suffix.
3892 if [ "$OSTYPE" = cygwin ]; then
3893 ___git_complete git.exe __git_main