75193ded4bdeda01fc440db26b08e40ae2d3a73f
[git.git] / contrib / completion / git-completion.bash
blob75193ded4bdeda01fc440db26b08e40ae2d3a73f
1 # bash/zsh completion support for core Git.
3 # Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
4 # Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
5 # Distributed under the GNU General Public License, version 2.0.
7 # The contained completion routines provide support for completing:
9 # *) local and remote branch names
10 # *) local and remote tag names
11 # *) .git/remotes file names
12 # *) git 'subcommands'
13 # *) git email aliases for git-send-email
14 # *) tree paths within 'ref:path/to/file' expressions
15 # *) file paths within current working directory and index
16 # *) common --long-options
18 # To use these routines:
20 # 1) Copy this file to somewhere (e.g. ~/.git-completion.bash).
21 # 2) Add the following line to your .bashrc/.zshrc:
22 # source ~/.git-completion.bash
23 # 3) Consider changing your PS1 to also show the current branch,
24 # see git-prompt.sh for details.
26 # If you use complex aliases of form '!f() { ... }; f', you can use the null
27 # command ':' as the first command in the function body to declare the desired
28 # completion style. For example '!f() { : git commit ; ... }; f' will
29 # tell the completion to use commit completion. This also works with aliases
30 # of form "!sh -c '...'". For example, "!sh -c ': git commit ; ... '".
31 # Note that "git" is optional --- '!f() { : commit; ...}; f' would complete
32 # just like the 'git commit' command.
34 # If you have a command that is not part of git, but you would still
35 # like completion, you can use __git_complete:
37 # __git_complete gl git_log
39 # Or if it's a main command (i.e. git or gitk):
41 # __git_complete gk gitk
43 # Compatible with bash 3.2.57.
45 # You can set the following environment variables to influence the behavior of
46 # the completion routines:
48 # GIT_COMPLETION_CHECKOUT_NO_GUESS
50 # When set to "1", do not include "DWIM" suggestions in git-checkout
51 # and git-switch completion (e.g., completing "foo" when "origin/foo"
52 # exists).
54 # GIT_COMPLETION_SHOW_ALL_COMMANDS
56 # When set to "1" suggest all commands, including plumbing commands
57 # which are hidden by default (e.g. "cat-file" on "git ca<TAB>").
59 # GIT_COMPLETION_SHOW_ALL
61 # When set to "1" suggest all options, including options which are
62 # typically hidden (e.g. '--allow-empty' for 'git commit').
64 # GIT_COMPLETION_IGNORE_CASE
66 # When set, uses for-each-ref '--ignore-case' to find refs that match
67 # case insensitively, even on systems with case sensitive file systems
68 # (e.g., completing tag name "FOO" on "git checkout f<TAB>").
70 case "$COMP_WORDBREAKS" in
71 *:*) : great ;;
72 *) COMP_WORDBREAKS="$COMP_WORDBREAKS:"
73 esac
75 # Discovers the path to the git repository taking any '--git-dir=<path>' and
76 # '-C <path>' options into account and stores it in the $__git_repo_path
77 # variable.
78 __git_find_repo_path ()
80 if [ -n "${__git_repo_path-}" ]; then
81 # we already know where it is
82 return
85 if [ -n "${__git_C_args-}" ]; then
86 __git_repo_path="$(git "${__git_C_args[@]}" \
87 ${__git_dir:+--git-dir="$__git_dir"} \
88 rev-parse --absolute-git-dir 2>/dev/null)"
89 elif [ -n "${__git_dir-}" ]; then
90 test -d "$__git_dir" &&
91 __git_repo_path="$__git_dir"
92 elif [ -n "${GIT_DIR-}" ]; then
93 test -d "$GIT_DIR" &&
94 __git_repo_path="$GIT_DIR"
95 elif [ -d .git ]; then
96 __git_repo_path=.git
97 else
98 __git_repo_path="$(git rev-parse --git-dir 2>/dev/null)"
102 # Deprecated: use __git_find_repo_path() and $__git_repo_path instead
103 # __gitdir accepts 0 or 1 arguments (i.e., location)
104 # returns location of .git repo
105 __gitdir ()
107 if [ -z "${1-}" ]; then
108 __git_find_repo_path || return 1
109 echo "$__git_repo_path"
110 elif [ -d "$1/.git" ]; then
111 echo "$1/.git"
112 else
113 echo "$1"
117 # Runs git with all the options given as argument, respecting any
118 # '--git-dir=<path>' and '-C <path>' options present on the command line
119 __git ()
121 git ${__git_C_args:+"${__git_C_args[@]}"} \
122 ${__git_dir:+--git-dir="$__git_dir"} "$@" 2>/dev/null
125 # Helper function to read the first line of a file into a variable.
126 # __git_eread requires 2 arguments, the file path and the name of the
127 # variable, in that order.
129 # This is taken from git-prompt.sh.
130 __git_eread ()
132 test -r "$1" && IFS=$'\r\n' read -r "$2" <"$1"
135 # Runs git in $__git_repo_path to determine whether a pseudoref exists.
136 # 1: The pseudo-ref to search
137 __git_pseudoref_exists ()
139 local ref=$1
140 local head
142 __git_find_repo_path
144 # If the reftable is in use, we have to shell out to 'git rev-parse'
145 # to determine whether the ref exists instead of looking directly in
146 # the filesystem to determine whether the ref exists. Otherwise, use
147 # Bash builtins since executing Git commands are expensive on some
148 # platforms.
149 if __git_eread "$__git_repo_path/HEAD" head; then
150 if [ "$head" == "ref: refs/heads/.invalid" ]; then
151 __git show-ref --exists "$ref"
152 return $?
156 [ -f "$__git_repo_path/$ref" ]
159 # Removes backslash escaping, single quotes and double quotes from a word,
160 # stores the result in the variable $dequoted_word.
161 # 1: The word to dequote.
162 __git_dequote ()
164 local rest="$1" len ch
166 dequoted_word=""
168 while test -n "$rest"; do
169 len=${#dequoted_word}
170 dequoted_word="$dequoted_word${rest%%[\\\'\"]*}"
171 rest="${rest:$((${#dequoted_word}-$len))}"
173 case "${rest:0:1}" in
175 ch="${rest:1:1}"
176 case "$ch" in
177 $'\n')
180 dequoted_word="$dequoted_word$ch"
182 esac
183 rest="${rest:2}"
186 rest="${rest:1}"
187 len=${#dequoted_word}
188 dequoted_word="$dequoted_word${rest%%\'*}"
189 rest="${rest:$((${#dequoted_word}-$len+1))}"
192 rest="${rest:1}"
193 while test -n "$rest" ; do
194 len=${#dequoted_word}
195 dequoted_word="$dequoted_word${rest%%[\\\"]*}"
196 rest="${rest:$((${#dequoted_word}-$len))}"
197 case "${rest:0:1}" in
199 ch="${rest:1:1}"
200 case "$ch" in
201 \"|\\|\$|\`)
202 dequoted_word="$dequoted_word$ch"
204 $'\n')
207 dequoted_word="$dequoted_word\\$ch"
209 esac
210 rest="${rest:2}"
213 rest="${rest:1}"
214 break
216 esac
217 done
219 esac
220 done
223 # The following function is based on code from:
225 # bash_completion - programmable completion functions for bash 3.2+
227 # Copyright © 2006-2008, Ian Macdonald <ian@caliban.org>
228 # © 2009-2010, Bash Completion Maintainers
229 # <bash-completion-devel@lists.alioth.debian.org>
231 # This program is free software; you can redistribute it and/or modify
232 # it under the terms of the GNU General Public License as published by
233 # the Free Software Foundation; either version 2, or (at your option)
234 # any later version.
236 # This program is distributed in the hope that it will be useful,
237 # but WITHOUT ANY WARRANTY; without even the implied warranty of
238 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
239 # GNU General Public License for more details.
241 # You should have received a copy of the GNU General Public License
242 # along with this program; if not, see <http://www.gnu.org/licenses/>.
244 # The latest version of this software can be obtained here:
246 # http://bash-completion.alioth.debian.org/
248 # RELEASE: 2.x
250 # This function can be used to access a tokenized list of words
251 # on the command line:
253 # __git_reassemble_comp_words_by_ref '=:'
254 # if test "${words_[cword_-1]}" = -w
255 # then
256 # ...
257 # fi
259 # The argument should be a collection of characters from the list of
260 # word completion separators (COMP_WORDBREAKS) to treat as ordinary
261 # characters.
263 # This is roughly equivalent to going back in time and setting
264 # COMP_WORDBREAKS to exclude those characters. The intent is to
265 # make option types like --date=<type> and <rev>:<path> easy to
266 # recognize by treating each shell word as a single token.
268 # It is best not to set COMP_WORDBREAKS directly because the value is
269 # shared with other completion scripts. By the time the completion
270 # function gets called, COMP_WORDS has already been populated so local
271 # changes to COMP_WORDBREAKS have no effect.
273 # Output: words_, cword_, cur_.
275 __git_reassemble_comp_words_by_ref()
277 local exclude i j first
278 # Which word separators to exclude?
279 exclude="${1//[^$COMP_WORDBREAKS]}"
280 cword_=$COMP_CWORD
281 if [ -z "$exclude" ]; then
282 words_=("${COMP_WORDS[@]}")
283 return
285 # List of word completion separators has shrunk;
286 # re-assemble words to complete.
287 for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
288 # Append each nonempty word consisting of just
289 # word separator characters to the current word.
290 first=t
291 while
292 [ $i -gt 0 ] &&
293 [ -n "${COMP_WORDS[$i]}" ] &&
294 # word consists of excluded word separators
295 [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
297 # Attach to the previous token,
298 # unless the previous token is the command name.
299 if [ $j -ge 2 ] && [ -n "$first" ]; then
300 ((j--))
302 first=
303 words_[$j]=${words_[j]}${COMP_WORDS[i]}
304 if [ $i = $COMP_CWORD ]; then
305 cword_=$j
307 if (($i < ${#COMP_WORDS[@]} - 1)); then
308 ((i++))
309 else
310 # Done.
311 return
313 done
314 words_[$j]=${words_[j]}${COMP_WORDS[i]}
315 if [ $i = $COMP_CWORD ]; then
316 cword_=$j
318 done
321 if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
322 _get_comp_words_by_ref ()
324 local exclude cur_ words_ cword_
325 if [ "$1" = "-n" ]; then
326 exclude=$2
327 shift 2
329 __git_reassemble_comp_words_by_ref "$exclude"
330 cur_=${words_[cword_]}
331 while [ $# -gt 0 ]; do
332 case "$1" in
333 cur)
334 cur=$cur_
336 prev)
337 prev=${words_[$cword_-1]}
339 words)
340 words=("${words_[@]}")
342 cword)
343 cword=$cword_
345 esac
346 shift
347 done
351 # Fills the COMPREPLY array with prefiltered words without any additional
352 # processing.
353 # Callers must take care of providing only words that match the current word
354 # to be completed and adding any prefix and/or suffix (trailing space!), if
355 # necessary.
356 # 1: List of newline-separated matching completion words, complete with
357 # prefix and suffix.
358 __gitcomp_direct ()
360 local IFS=$'\n'
362 COMPREPLY=($1)
365 # Similar to __gitcomp_direct, but appends to COMPREPLY instead.
366 # Callers must take care of providing only words that match the current word
367 # to be completed and adding any prefix and/or suffix (trailing space!), if
368 # necessary.
369 # 1: List of newline-separated matching completion words, complete with
370 # prefix and suffix.
371 __gitcomp_direct_append ()
373 local IFS=$'\n'
375 COMPREPLY+=($1)
378 __gitcompappend ()
380 local x i=${#COMPREPLY[@]}
381 for x in $1; do
382 if [[ "$x" == "$3"* ]]; then
383 COMPREPLY[i++]="$2$x$4"
385 done
388 __gitcompadd ()
390 COMPREPLY=()
391 __gitcompappend "$@"
394 # Generates completion reply, appending a space to possible completion words,
395 # if necessary.
396 # It accepts 1 to 4 arguments:
397 # 1: List of possible completion words.
398 # 2: A prefix to be added to each possible completion word (optional).
399 # 3: Generate possible completion matches for this word (optional).
400 # 4: A suffix to be appended to each possible completion word (optional).
401 __gitcomp ()
403 local cur_="${3-$cur}"
405 case "$cur_" in
408 --no-*)
409 local c i=0 IFS=$' \t\n'
410 for c in $1; do
411 if [[ $c == "--" ]]; then
412 continue
414 c="$c${4-}"
415 if [[ $c == "$cur_"* ]]; then
416 case $c in
417 --*=|*.) ;;
418 *) c="$c " ;;
419 esac
420 COMPREPLY[i++]="${2-}$c"
422 done
425 local c i=0 IFS=$' \t\n'
426 for c in $1; do
427 if [[ $c == "--" ]]; then
428 c="--no-...${4-}"
429 if [[ $c == "$cur_"* ]]; then
430 COMPREPLY[i++]="${2-}$c "
432 break
434 c="$c${4-}"
435 if [[ $c == "$cur_"* ]]; then
436 case $c in
437 *=|*.) ;;
438 *) c="$c " ;;
439 esac
440 COMPREPLY[i++]="${2-}$c"
442 done
444 esac
447 # Clear the variables caching builtins' options when (re-)sourcing
448 # the completion script.
449 if [[ -n ${ZSH_VERSION-} ]]; then
450 unset ${(M)${(k)parameters[@]}:#__gitcomp_builtin_*} 2>/dev/null
451 else
452 unset $(compgen -v __gitcomp_builtin_)
455 # This function is equivalent to
457 # ___git_resolved_builtins=$(git xxx --git-completion-helper)
459 # except that the result of the execution is cached.
461 # Accept 1-3 arguments:
462 # 1: the git command to execute, this is also the cache key
463 # (use "_" when the command contains spaces, e.g. "remote add"
464 # becomes "remote_add")
465 # 2: extra options to be added on top (e.g. negative forms)
466 # 3: options to be excluded
467 __git_resolve_builtins ()
469 local cmd="$1"
470 local incl="${2-}"
471 local excl="${3-}"
473 local var=__gitcomp_builtin_"${cmd//-/_}"
474 local options
475 eval "options=\${$var-}"
477 if [ -z "$options" ]; then
478 local completion_helper
479 if [ "${GIT_COMPLETION_SHOW_ALL-}" = "1" ]; then
480 completion_helper="--git-completion-helper-all"
481 else
482 completion_helper="--git-completion-helper"
484 # leading and trailing spaces are significant to make
485 # option removal work correctly.
486 options=" $incl $(__git ${cmd/_/ } $completion_helper) " || return
488 for i in $excl; do
489 options="${options/ $i / }"
490 done
491 eval "$var=\"$options\""
494 ___git_resolved_builtins="$options"
497 # This function is equivalent to
499 # __gitcomp "$(git xxx --git-completion-helper) ..."
501 # except that the output is cached. Accept 1-3 arguments:
502 # 1: the git command to execute, this is also the cache key
503 # (use "_" when the command contains spaces, e.g. "remote add"
504 # becomes "remote_add")
505 # 2: extra options to be added on top (e.g. negative forms)
506 # 3: options to be excluded
507 __gitcomp_builtin ()
509 __git_resolve_builtins "$1" "$2" "$3"
511 __gitcomp "$___git_resolved_builtins"
514 # Variation of __gitcomp_nl () that appends to the existing list of
515 # completion candidates, COMPREPLY.
516 __gitcomp_nl_append ()
518 local IFS=$'\n'
519 __gitcompappend "$1" "${2-}" "${3-$cur}" "${4- }"
522 # Generates completion reply from newline-separated possible completion words
523 # by appending a space to all of them.
524 # It accepts 1 to 4 arguments:
525 # 1: List of possible completion words, separated by a single newline.
526 # 2: A prefix to be added to each possible completion word (optional).
527 # 3: Generate possible completion matches for this word (optional).
528 # 4: A suffix to be appended to each possible completion word instead of
529 # the default space (optional). If specified but empty, nothing is
530 # appended.
531 __gitcomp_nl ()
533 COMPREPLY=()
534 __gitcomp_nl_append "$@"
537 # Fills the COMPREPLY array with prefiltered paths without any additional
538 # processing.
539 # Callers must take care of providing only paths that match the current path
540 # to be completed and adding any prefix path components, if necessary.
541 # 1: List of newline-separated matching paths, complete with all prefix
542 # path components.
543 __gitcomp_file_direct ()
545 local IFS=$'\n'
547 COMPREPLY=($1)
549 # use a hack to enable file mode in bash < 4
550 compopt -o filenames +o nospace 2>/dev/null ||
551 compgen -f /non-existing-dir/ >/dev/null ||
552 true
555 # Generates completion reply with compgen from newline-separated possible
556 # completion filenames.
557 # It accepts 1 to 3 arguments:
558 # 1: List of possible completion filenames, separated by a single newline.
559 # 2: A directory prefix to be added to each possible completion filename
560 # (optional).
561 # 3: Generate possible completion matches for this word (optional).
562 __gitcomp_file ()
564 local IFS=$'\n'
566 # XXX does not work when the directory prefix contains a tilde,
567 # since tilde expansion is not applied.
568 # This means that COMPREPLY will be empty and Bash default
569 # completion will be used.
570 __gitcompadd "$1" "${2-}" "${3-$cur}" ""
572 # use a hack to enable file mode in bash < 4
573 compopt -o filenames +o nospace 2>/dev/null ||
574 compgen -f /non-existing-dir/ >/dev/null ||
575 true
578 # Find the current subcommand for commands that follow the syntax:
580 # git <command> <subcommand>
582 # 1: List of possible subcommands.
583 # 2: Optional subcommand to return when none is found.
584 __git_find_subcommand ()
586 local subcommand subcommands="$1" default_subcommand="$2"
588 for subcommand in $subcommands; do
589 if [ "$subcommand" = "${words[__git_cmd_idx+1]}" ]; then
590 echo $subcommand
591 return
593 done
595 echo $default_subcommand
598 # Execute 'git ls-files', unless the --committable option is specified, in
599 # which case it runs 'git diff-index' to find out the files that can be
600 # committed. It return paths relative to the directory specified in the first
601 # argument, and using the options specified in the second argument.
602 __git_ls_files_helper ()
604 if [ "$2" = "--committable" ]; then
605 __git -C "$1" -c core.quotePath=false diff-index \
606 --name-only --relative HEAD -- "${3//\\/\\\\}*"
607 else
608 # NOTE: $2 is not quoted in order to support multiple options
609 __git -C "$1" -c core.quotePath=false ls-files \
610 --exclude-standard $2 -- "${3//\\/\\\\}*"
615 # __git_index_files accepts 1 or 2 arguments:
616 # 1: Options to pass to ls-files (required).
617 # 2: A directory path (optional).
618 # If provided, only files within the specified directory are listed.
619 # Sub directories are never recursed. Path must have a trailing
620 # slash.
621 # 3: List only paths matching this path component (optional).
622 __git_index_files ()
624 local root="$2" match="$3"
626 __git_ls_files_helper "$root" "$1" "${match:-?}" |
627 awk -F / -v pfx="${2//\\/\\\\}" '{
628 paths[$1] = 1
630 END {
631 for (p in paths) {
632 if (substr(p, 1, 1) != "\"") {
633 # No special characters, easy!
634 print pfx p
635 continue
638 # The path is quoted.
639 p = dequote(p)
640 if (p == "")
641 continue
643 # Even when a directory name itself does not contain
644 # any special characters, it will still be quoted if
645 # any of its (stripped) trailing path components do.
646 # Because of this we may have seen the same directory
647 # both quoted and unquoted.
648 if (p in paths)
649 # We have seen the same directory unquoted,
650 # skip it.
651 continue
652 else
653 print pfx p
656 function dequote(p, bs_idx, out, esc, esc_idx, dec) {
657 # Skip opening double quote.
658 p = substr(p, 2)
660 # Interpret backslash escape sequences.
661 while ((bs_idx = index(p, "\\")) != 0) {
662 out = out substr(p, 1, bs_idx - 1)
663 esc = substr(p, bs_idx + 1, 1)
664 p = substr(p, bs_idx + 2)
666 if ((esc_idx = index("abtvfr\"\\", esc)) != 0) {
667 # C-style one-character escape sequence.
668 out = out substr("\a\b\t\v\f\r\"\\",
669 esc_idx, 1)
670 } else if (esc == "n") {
671 # Uh-oh, a newline character.
672 # We cannot reliably put a pathname
673 # containing a newline into COMPREPLY,
674 # and the newline would create a mess.
675 # Skip this path.
676 return ""
677 } else {
678 # Must be a \nnn octal value, then.
679 dec = esc * 64 + \
680 substr(p, 1, 1) * 8 + \
681 substr(p, 2, 1)
682 out = out sprintf("%c", dec)
683 p = substr(p, 3)
686 # Drop closing double quote, if there is one.
687 # (There is not any if this is a directory, as it was
688 # already stripped with the trailing path components.)
689 if (substr(p, length(p), 1) == "\"")
690 out = out substr(p, 1, length(p) - 1)
691 else
692 out = out p
694 return out
698 # __git_complete_index_file requires 1 argument:
699 # 1: the options to pass to ls-file
701 # The exception is --committable, which finds the files appropriate commit.
702 __git_complete_index_file ()
704 local dequoted_word pfx="" cur_
706 __git_dequote "$cur"
708 case "$dequoted_word" in
709 ?*/*)
710 pfx="${dequoted_word%/*}/"
711 cur_="${dequoted_word##*/}"
714 cur_="$dequoted_word"
715 esac
717 __gitcomp_file_direct "$(__git_index_files "$1" "$pfx" "$cur_")"
720 # Lists branches from the local repository.
721 # 1: A prefix to be added to each listed branch (optional).
722 # 2: List only branches matching this word (optional; list all branches if
723 # unset or empty).
724 # 3: A suffix to be appended to each listed branch (optional).
725 __git_heads ()
727 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
729 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
730 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
731 "refs/heads/$cur_*" "refs/heads/$cur_*/**"
734 # Lists branches from remote repositories.
735 # 1: A prefix to be added to each listed branch (optional).
736 # 2: List only branches matching this word (optional; list all branches if
737 # unset or empty).
738 # 3: A suffix to be appended to each listed branch (optional).
739 __git_remote_heads ()
741 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
743 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
744 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
745 "refs/remotes/$cur_*" "refs/remotes/$cur_*/**"
748 # Lists tags from the local repository.
749 # Accepts the same positional parameters as __git_heads() above.
750 __git_tags ()
752 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
754 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
755 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
756 "refs/tags/$cur_*" "refs/tags/$cur_*/**"
759 # List unique branches from refs/remotes used for 'git checkout' and 'git
760 # switch' tracking DWIMery.
761 # 1: A prefix to be added to each listed branch (optional)
762 # 2: List only branches matching this word (optional; list all branches if
763 # unset or empty).
764 # 3: A suffix to be appended to each listed branch (optional).
765 __git_dwim_remote_heads ()
767 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
768 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
770 # employ the heuristic used by git checkout and git switch
771 # Try to find a remote branch that cur_es the completion word
772 # but only output if the branch name is unique
773 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
774 --sort="refname:strip=3" \
775 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
776 "refs/remotes/*/$cur_*" "refs/remotes/*/$cur_*/**" | \
777 uniq -u
780 # Lists refs from the local (by default) or from a remote repository.
781 # It accepts 0, 1 or 2 arguments:
782 # 1: The remote to list refs from (optional; ignored, if set but empty).
783 # Can be the name of a configured remote, a path, or a URL.
784 # 2: In addition to local refs, list unique branches from refs/remotes/ for
785 # 'git checkout's tracking DWIMery (optional; ignored, if set but empty).
786 # 3: A prefix to be added to each listed ref (optional).
787 # 4: List only refs matching this word (optional; list all refs if unset or
788 # empty).
789 # 5: A suffix to be appended to each listed ref (optional; ignored, if set
790 # but empty).
792 # Use __git_complete_refs() instead.
793 __git_refs ()
795 local i hash dir track="${2-}"
796 local list_refs_from=path remote="${1-}"
797 local format refs
798 local pfx="${3-}" cur_="${4-$cur}" sfx="${5-}"
799 local match="${4-}"
800 local umatch="${4-}"
801 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
803 __git_find_repo_path
804 dir="$__git_repo_path"
806 if [ -z "$remote" ]; then
807 if [ -z "$dir" ]; then
808 return
810 else
811 if __git_is_configured_remote "$remote"; then
812 # configured remote takes precedence over a
813 # local directory with the same name
814 list_refs_from=remote
815 elif [ -d "$remote/.git" ]; then
816 dir="$remote/.git"
817 elif [ -d "$remote" ]; then
818 dir="$remote"
819 else
820 list_refs_from=url
824 if test "${GIT_COMPLETION_IGNORE_CASE:+1}" = "1"
825 then
826 # uppercase with tr instead of ${match,^^} for bash 3.2 compatibility
827 umatch=$(echo "$match" | tr a-z A-Z 2>/dev/null || echo "$match")
830 if [ "$list_refs_from" = path ]; then
831 if [[ "$cur_" == ^* ]]; then
832 pfx="$pfx^"
833 fer_pfx="$fer_pfx^"
834 cur_=${cur_#^}
835 match=${match#^}
836 umatch=${umatch#^}
838 case "$cur_" in
839 refs|refs/*)
840 format="refname"
841 refs=("$match*" "$match*/**")
842 track=""
845 for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD REBASE_HEAD CHERRY_PICK_HEAD REVERT_HEAD BISECT_HEAD AUTO_MERGE; do
846 case "$i" in
847 $match*|$umatch*)
848 if [ -e "$dir/$i" ]; then
849 echo "$pfx$i$sfx"
852 esac
853 done
854 format="refname:strip=2"
855 refs=("refs/tags/$match*" "refs/tags/$match*/**"
856 "refs/heads/$match*" "refs/heads/$match*/**"
857 "refs/remotes/$match*" "refs/remotes/$match*/**")
859 esac
860 __git_dir="$dir" __git for-each-ref --format="$fer_pfx%($format)$sfx" \
861 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
862 "${refs[@]}"
863 if [ -n "$track" ]; then
864 __git_dwim_remote_heads "$pfx" "$match" "$sfx"
866 return
868 case "$cur_" in
869 refs|refs/*)
870 __git ls-remote "$remote" "$match*" | \
871 while read -r hash i; do
872 case "$i" in
873 *^{}) ;;
874 *) echo "$pfx$i$sfx" ;;
875 esac
876 done
879 if [ "$list_refs_from" = remote ]; then
880 case "HEAD" in
881 $match*|$umatch*) echo "${pfx}HEAD$sfx" ;;
882 esac
883 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
884 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
885 "refs/remotes/$remote/$match*" \
886 "refs/remotes/$remote/$match*/**"
887 else
888 local query_symref
889 case "HEAD" in
890 $match*|$umatch*) query_symref="HEAD" ;;
891 esac
892 __git ls-remote "$remote" $query_symref \
893 "refs/tags/$match*" "refs/heads/$match*" \
894 "refs/remotes/$match*" |
895 while read -r hash i; do
896 case "$i" in
897 *^{}) ;;
898 refs/*) echo "$pfx${i#refs/*/}$sfx" ;;
899 *) echo "$pfx$i$sfx" ;; # symbolic refs
900 esac
901 done
904 esac
907 # Completes refs, short and long, local and remote, symbolic and pseudo.
909 # Usage: __git_complete_refs [<option>]...
910 # --remote=<remote>: The remote to list refs from, can be the name of a
911 # configured remote, a path, or a URL.
912 # --dwim: List unique remote branches for 'git switch's tracking DWIMery.
913 # --pfx=<prefix>: A prefix to be added to each ref.
914 # --cur=<word>: The current ref to be completed. Defaults to the current
915 # word to be completed.
916 # --sfx=<suffix>: A suffix to be appended to each ref instead of the default
917 # space.
918 # --mode=<mode>: What set of refs to complete, one of 'refs' (the default) to
919 # complete all refs, 'heads' to complete only branches, or
920 # 'remote-heads' to complete only remote branches. Note that
921 # --remote is only compatible with --mode=refs.
922 __git_complete_refs ()
924 local remote= dwim= pfx= cur_="$cur" sfx=" " mode="refs"
926 while test $# != 0; do
927 case "$1" in
928 --remote=*) remote="${1##--remote=}" ;;
929 --dwim) dwim="yes" ;;
930 # --track is an old spelling of --dwim
931 --track) dwim="yes" ;;
932 --pfx=*) pfx="${1##--pfx=}" ;;
933 --cur=*) cur_="${1##--cur=}" ;;
934 --sfx=*) sfx="${1##--sfx=}" ;;
935 --mode=*) mode="${1##--mode=}" ;;
936 *) return 1 ;;
937 esac
938 shift
939 done
941 # complete references based on the specified mode
942 case "$mode" in
943 refs)
944 __gitcomp_direct "$(__git_refs "$remote" "" "$pfx" "$cur_" "$sfx")" ;;
945 heads)
946 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" "$sfx")" ;;
947 remote-heads)
948 __gitcomp_direct "$(__git_remote_heads "$pfx" "$cur_" "$sfx")" ;;
950 return 1 ;;
951 esac
953 # Append DWIM remote branch names if requested
954 if [ "$dwim" = "yes" ]; then
955 __gitcomp_direct_append "$(__git_dwim_remote_heads "$pfx" "$cur_" "$sfx")"
959 # __git_refs2 requires 1 argument (to pass to __git_refs)
960 # Deprecated: use __git_complete_fetch_refspecs() instead.
961 __git_refs2 ()
963 local i
964 for i in $(__git_refs "$1"); do
965 echo "$i:$i"
966 done
969 # Completes refspecs for fetching from a remote repository.
970 # 1: The remote repository.
971 # 2: A prefix to be added to each listed refspec (optional).
972 # 3: The ref to be completed as a refspec instead of the current word to be
973 # completed (optional)
974 # 4: A suffix to be appended to each listed refspec instead of the default
975 # space (optional).
976 __git_complete_fetch_refspecs ()
978 local i remote="$1" pfx="${2-}" cur_="${3-$cur}" sfx="${4- }"
980 __gitcomp_direct "$(
981 for i in $(__git_refs "$remote" "" "" "$cur_") ; do
982 echo "$pfx$i:$i$sfx"
983 done
987 # __git_refs_remotes requires 1 argument (to pass to ls-remote)
988 __git_refs_remotes ()
990 local i hash
991 __git ls-remote "$1" 'refs/heads/*' | \
992 while read -r hash i; do
993 echo "$i:refs/remotes/$1/${i#refs/heads/}"
994 done
997 __git_remotes ()
999 __git_find_repo_path
1000 test -d "$__git_repo_path/remotes" && ls -1 "$__git_repo_path/remotes"
1001 __git remote
1004 # Returns true if $1 matches the name of a configured remote, false otherwise.
1005 __git_is_configured_remote ()
1007 local remote
1008 for remote in $(__git_remotes); do
1009 if [ "$remote" = "$1" ]; then
1010 return 0
1012 done
1013 return 1
1016 __git_list_merge_strategies ()
1018 LANG=C LC_ALL=C git merge -s help 2>&1 |
1019 sed -n -e '/[Aa]vailable strategies are: /,/^$/{
1020 s/\.$//
1021 s/.*://
1022 s/^[ ]*//
1023 s/[ ]*$//
1028 __git_merge_strategies=
1029 # 'git merge -s help' (and thus detection of the merge strategy
1030 # list) fails, unfortunately, if run outside of any git working
1031 # tree. __git_merge_strategies is set to the empty string in
1032 # that case, and the detection will be repeated the next time it
1033 # is needed.
1034 __git_compute_merge_strategies ()
1036 test -n "$__git_merge_strategies" ||
1037 __git_merge_strategies=$(__git_list_merge_strategies)
1040 __git_merge_strategy_options="ours theirs subtree subtree= patience
1041 histogram diff-algorithm= ignore-space-change ignore-all-space
1042 ignore-space-at-eol renormalize no-renormalize no-renames
1043 find-renames find-renames= rename-threshold="
1045 __git_complete_revlist_file ()
1047 local dequoted_word pfx ls ref cur_="$cur"
1048 case "$cur_" in
1049 *..?*:*)
1050 return
1052 ?*:*)
1053 ref="${cur_%%:*}"
1054 cur_="${cur_#*:}"
1056 __git_dequote "$cur_"
1058 case "$dequoted_word" in
1059 ?*/*)
1060 pfx="${dequoted_word%/*}"
1061 cur_="${dequoted_word##*/}"
1062 ls="$ref:$pfx"
1063 pfx="$pfx/"
1066 cur_="$dequoted_word"
1067 ls="$ref"
1069 esac
1071 case "$COMP_WORDBREAKS" in
1072 *:*) : great ;;
1073 *) pfx="$ref:$pfx" ;;
1074 esac
1076 __gitcomp_file "$(__git ls-tree "$ls" \
1077 | sed 's/^.* //
1078 s/$//')" \
1079 "$pfx" "$cur_"
1081 *...*)
1082 pfx="${cur_%...*}..."
1083 cur_="${cur_#*...}"
1084 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1086 *..*)
1087 pfx="${cur_%..*}.."
1088 cur_="${cur_#*..}"
1089 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1092 __git_complete_refs
1094 esac
1097 __git_complete_file ()
1099 __git_complete_revlist_file
1102 __git_complete_revlist ()
1104 __git_complete_revlist_file
1107 __git_complete_remote_or_refspec ()
1109 local cur_="$cur" cmd="${words[__git_cmd_idx]}"
1110 local i c=$((__git_cmd_idx+1)) remote="" pfx="" lhs=1 no_complete_refspec=0
1111 if [ "$cmd" = "remote" ]; then
1112 ((c++))
1114 while [ $c -lt $cword ]; do
1115 i="${words[c]}"
1116 case "$i" in
1117 --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
1118 -d|--delete) [ "$cmd" = "push" ] && lhs=0 ;;
1119 --all)
1120 case "$cmd" in
1121 push) no_complete_refspec=1 ;;
1122 fetch)
1123 return
1125 *) ;;
1126 esac
1128 --multiple) no_complete_refspec=1; break ;;
1129 -*) ;;
1130 *) remote="$i"; break ;;
1131 esac
1132 ((c++))
1133 done
1134 if [ -z "$remote" ]; then
1135 __gitcomp_nl "$(__git_remotes)"
1136 return
1138 if [ $no_complete_refspec = 1 ]; then
1139 return
1141 [ "$remote" = "." ] && remote=
1142 case "$cur_" in
1143 *:*)
1144 case "$COMP_WORDBREAKS" in
1145 *:*) : great ;;
1146 *) pfx="${cur_%%:*}:" ;;
1147 esac
1148 cur_="${cur_#*:}"
1149 lhs=0
1152 pfx="+"
1153 cur_="${cur_#+}"
1155 esac
1156 case "$cmd" in
1157 fetch)
1158 if [ $lhs = 1 ]; then
1159 __git_complete_fetch_refspecs "$remote" "$pfx" "$cur_"
1160 else
1161 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1164 pull|remote)
1165 if [ $lhs = 1 ]; then
1166 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
1167 else
1168 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1171 push)
1172 if [ $lhs = 1 ]; then
1173 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1174 else
1175 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
1178 esac
1181 __git_complete_strategy ()
1183 __git_compute_merge_strategies
1184 case "$prev" in
1185 -s|--strategy)
1186 __gitcomp "$__git_merge_strategies"
1187 return 0
1190 __gitcomp "$__git_merge_strategy_options"
1191 return 0
1193 esac
1194 case "$cur" in
1195 --strategy=*)
1196 __gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
1197 return 0
1199 --strategy-option=*)
1200 __gitcomp "$__git_merge_strategy_options" "" "${cur##--strategy-option=}"
1201 return 0
1203 esac
1204 return 1
1207 __git_all_commands=
1208 __git_compute_all_commands ()
1210 test -n "$__git_all_commands" ||
1211 __git_all_commands=$(__git --list-cmds=main,others,alias,nohelpers)
1214 # Lists all set config variables starting with the given section prefix,
1215 # with the prefix removed.
1216 __git_get_config_variables ()
1218 local section="$1" i IFS=$'\n'
1219 for i in $(__git config --name-only --get-regexp "^$section\..*"); do
1220 echo "${i#$section.}"
1221 done
1224 __git_pretty_aliases ()
1226 __git_get_config_variables "pretty"
1229 # __git_aliased_command requires 1 argument
1230 __git_aliased_command ()
1232 local cur=$1 last list= word cmdline
1234 while [[ -n "$cur" ]]; do
1235 if [[ "$list" == *" $cur "* ]]; then
1236 # loop detected
1237 return
1240 cmdline=$(__git config --get "alias.$cur")
1241 list=" $cur $list"
1242 last=$cur
1243 cur=
1245 for word in $cmdline; do
1246 case "$word" in
1247 \!gitk|gitk)
1248 cur="gitk"
1249 break
1251 \!*) : shell command alias ;;
1252 -*) : option ;;
1253 *=*) : setting env ;;
1254 git) : git itself ;;
1255 \(\)) : skip parens of shell function definition ;;
1256 {) : skip start of shell helper function ;;
1257 :) : skip null command ;;
1258 \'*) : skip opening quote after sh -c ;;
1260 cur="${word%;}"
1261 break
1262 esac
1263 done
1264 done
1266 cur=$last
1267 if [[ "$cur" != "$1" ]]; then
1268 echo "$cur"
1272 # Check whether one of the given words is present on the command line,
1273 # and print the first word found.
1275 # Usage: __git_find_on_cmdline [<option>]... "<wordlist>"
1276 # --show-idx: Optionally show the index of the found word in the $words array.
1277 __git_find_on_cmdline ()
1279 local word c="$__git_cmd_idx" show_idx
1281 while test $# -gt 1; do
1282 case "$1" in
1283 --show-idx) show_idx=y ;;
1284 *) return 1 ;;
1285 esac
1286 shift
1287 done
1288 local wordlist="$1"
1290 while [ $c -lt $cword ]; do
1291 for word in $wordlist; do
1292 if [ "$word" = "${words[c]}" ]; then
1293 if [ -n "${show_idx-}" ]; then
1294 echo "$c $word"
1295 else
1296 echo "$word"
1298 return
1300 done
1301 ((c++))
1302 done
1305 # Similar to __git_find_on_cmdline, except that it loops backwards and thus
1306 # prints the *last* word found. Useful for finding which of two options that
1307 # supersede each other came last, such as "--guess" and "--no-guess".
1309 # Usage: __git_find_last_on_cmdline [<option>]... "<wordlist>"
1310 # --show-idx: Optionally show the index of the found word in the $words array.
1311 __git_find_last_on_cmdline ()
1313 local word c=$cword show_idx
1315 while test $# -gt 1; do
1316 case "$1" in
1317 --show-idx) show_idx=y ;;
1318 *) return 1 ;;
1319 esac
1320 shift
1321 done
1322 local wordlist="$1"
1324 while [ $c -gt "$__git_cmd_idx" ]; do
1325 ((c--))
1326 for word in $wordlist; do
1327 if [ "$word" = "${words[c]}" ]; then
1328 if [ -n "$show_idx" ]; then
1329 echo "$c $word"
1330 else
1331 echo "$word"
1333 return
1335 done
1336 done
1339 # Echo the value of an option set on the command line or config
1341 # $1: short option name
1342 # $2: long option name including =
1343 # $3: list of possible values
1344 # $4: config string (optional)
1346 # example:
1347 # result="$(__git_get_option_value "-d" "--do-something=" \
1348 # "yes no" "core.doSomething")"
1350 # result is then either empty (no option set) or "yes" or "no"
1352 # __git_get_option_value requires 3 arguments
1353 __git_get_option_value ()
1355 local c short_opt long_opt val
1356 local result= values config_key word
1358 short_opt="$1"
1359 long_opt="$2"
1360 values="$3"
1361 config_key="$4"
1363 ((c = $cword - 1))
1364 while [ $c -ge 0 ]; do
1365 word="${words[c]}"
1366 for val in $values; do
1367 if [ "$short_opt$val" = "$word" ] ||
1368 [ "$long_opt$val" = "$word" ]; then
1369 result="$val"
1370 break 2
1372 done
1373 ((c--))
1374 done
1376 if [ -n "$config_key" ] && [ -z "$result" ]; then
1377 result="$(__git config "$config_key")"
1380 echo "$result"
1383 __git_has_doubledash ()
1385 local c=1
1386 while [ $c -lt $cword ]; do
1387 if [ "--" = "${words[c]}" ]; then
1388 return 0
1390 ((c++))
1391 done
1392 return 1
1395 # Try to count non option arguments passed on the command line for the
1396 # specified git command.
1397 # When options are used, it is necessary to use the special -- option to
1398 # tell the implementation were non option arguments begin.
1399 # XXX this can not be improved, since options can appear everywhere, as
1400 # an example:
1401 # git mv x -n y
1403 # __git_count_arguments requires 1 argument: the git command executed.
1404 __git_count_arguments ()
1406 local word i c=0
1408 # Skip "git" (first argument)
1409 for ((i=$__git_cmd_idx; i < ${#words[@]}; i++)); do
1410 word="${words[i]}"
1412 case "$word" in
1414 # Good; we can assume that the following are only non
1415 # option arguments.
1416 ((c = 0))
1418 "$1")
1419 # Skip the specified git command and discard git
1420 # main options
1421 ((c = 0))
1424 ((c++))
1426 esac
1427 done
1429 printf "%d" $c
1432 __git_whitespacelist="nowarn warn error error-all fix"
1433 __git_patchformat="mbox stgit stgit-series hg mboxrd"
1434 __git_showcurrentpatch="diff raw"
1435 __git_am_inprogress_options="--skip --continue --resolved --abort --quit --show-current-patch"
1436 __git_quoted_cr="nowarn warn strip"
1438 _git_am ()
1440 __git_find_repo_path
1441 if [ -d "$__git_repo_path"/rebase-apply ]; then
1442 __gitcomp "$__git_am_inprogress_options"
1443 return
1445 case "$cur" in
1446 --whitespace=*)
1447 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1448 return
1450 --patch-format=*)
1451 __gitcomp "$__git_patchformat" "" "${cur##--patch-format=}"
1452 return
1454 --show-current-patch=*)
1455 __gitcomp "$__git_showcurrentpatch" "" "${cur##--show-current-patch=}"
1456 return
1458 --quoted-cr=*)
1459 __gitcomp "$__git_quoted_cr" "" "${cur##--quoted-cr=}"
1460 return
1462 --*)
1463 __gitcomp_builtin am "" \
1464 "$__git_am_inprogress_options"
1465 return
1466 esac
1469 _git_apply ()
1471 case "$cur" in
1472 --whitespace=*)
1473 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1474 return
1476 --*)
1477 __gitcomp_builtin apply
1478 return
1479 esac
1482 _git_add ()
1484 case "$cur" in
1485 --chmod=*)
1486 __gitcomp "+x -x" "" "${cur##--chmod=}"
1487 return
1489 --*)
1490 __gitcomp_builtin add
1491 return
1492 esac
1494 local complete_opt="--others --modified --directory --no-empty-directory"
1495 if test -n "$(__git_find_on_cmdline "-u --update")"
1496 then
1497 complete_opt="--modified"
1499 __git_complete_index_file "$complete_opt"
1502 _git_archive ()
1504 case "$cur" in
1505 --format=*)
1506 __gitcomp "$(git archive --list)" "" "${cur##--format=}"
1507 return
1509 --remote=*)
1510 __gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
1511 return
1513 --*)
1514 __gitcomp_builtin archive "--format= --list --verbose --prefix= --worktree-attributes"
1515 return
1517 esac
1518 __git_complete_file
1521 _git_bisect ()
1523 __git_has_doubledash && return
1525 __git_find_repo_path
1527 # If a bisection is in progress get the terms being used.
1528 local term_bad term_good
1529 if [ -f "$__git_repo_path"/BISECT_TERMS ]; then
1530 term_bad=$(__git bisect terms --term-bad)
1531 term_good=$(__git bisect terms --term-good)
1534 # We will complete any custom terms, but still always complete the
1535 # more usual bad/new/good/old because git bisect gives a good error
1536 # message if these are given when not in use, and that's better than
1537 # silent refusal to complete if the user is confused.
1539 # We want to recognize 'view' but not complete it, because it overlaps
1540 # with 'visualize' too much and is just an alias for it.
1542 local completable_subcommands="start bad new $term_bad good old $term_good terms skip reset visualize replay log run help"
1543 local all_subcommands="$completable_subcommands view"
1545 local subcommand="$(__git_find_on_cmdline "$all_subcommands")"
1547 if [ -z "$subcommand" ]; then
1548 __git_find_repo_path
1549 if [ -f "$__git_repo_path"/BISECT_START ]; then
1550 __gitcomp "$completable_subcommands"
1551 else
1552 __gitcomp "replay start"
1554 return
1557 case "$subcommand" in
1558 start)
1559 case "$cur" in
1560 --*)
1561 __gitcomp "--first-parent --no-checkout --term-new --term-bad --term-old --term-good"
1562 return
1565 __git_complete_refs
1567 esac
1569 terms)
1570 __gitcomp "--term-good --term-old --term-bad --term-new"
1571 return
1573 visualize|view)
1574 __git_complete_log_opts
1575 return
1577 bad|new|"$term_bad"|good|old|"$term_good"|reset|skip)
1578 __git_complete_refs
1582 esac
1585 __git_ref_fieldlist="refname objecttype objectsize objectname upstream push HEAD symref"
1587 _git_branch ()
1589 local i c="$__git_cmd_idx" only_local_ref="n" has_r="n"
1591 while [ $c -lt $cword ]; do
1592 i="${words[c]}"
1593 case "$i" in
1594 -d|-D|--delete|-m|-M|--move|-c|-C|--copy)
1595 only_local_ref="y" ;;
1596 -r|--remotes)
1597 has_r="y" ;;
1598 esac
1599 ((c++))
1600 done
1602 case "$cur" in
1603 --set-upstream-to=*)
1604 __git_complete_refs --cur="${cur##--set-upstream-to=}"
1606 --*)
1607 __gitcomp_builtin branch
1610 if [ $only_local_ref = "y" -a $has_r = "n" ]; then
1611 __gitcomp_direct "$(__git_heads "" "$cur" " ")"
1612 else
1613 __git_complete_refs
1616 esac
1619 _git_bundle ()
1621 local cmd="${words[__git_cmd_idx+1]}"
1622 case "$cword" in
1623 $((__git_cmd_idx+1)))
1624 __gitcomp "create list-heads verify unbundle"
1626 $((__git_cmd_idx+2)))
1627 # looking for a file
1630 case "$cmd" in
1631 create)
1632 __git_complete_revlist
1634 esac
1636 esac
1639 # Helper function to decide whether or not we should enable DWIM logic for
1640 # git-switch and git-checkout.
1642 # To decide between the following rules in decreasing priority order:
1643 # - the last provided of "--guess" or "--no-guess" explicitly enable or
1644 # disable completion of DWIM logic respectively.
1645 # - If checkout.guess is false, disable completion of DWIM logic.
1646 # - If the --no-track option is provided, take this as a hint to disable the
1647 # DWIM completion logic
1648 # - If GIT_COMPLETION_CHECKOUT_NO_GUESS is set, disable the DWIM completion
1649 # logic, as requested by the user.
1650 # - Enable DWIM logic otherwise.
1652 __git_checkout_default_dwim_mode ()
1654 local last_option dwim_opt="--dwim"
1656 if [ "${GIT_COMPLETION_CHECKOUT_NO_GUESS-}" = "1" ]; then
1657 dwim_opt=""
1660 # --no-track disables DWIM, but with lower priority than
1661 # --guess/--no-guess/checkout.guess
1662 if [ -n "$(__git_find_on_cmdline "--no-track")" ]; then
1663 dwim_opt=""
1666 # checkout.guess = false disables DWIM, but with lower priority than
1667 # --guess/--no-guess
1668 if [ "$(__git config --type=bool checkout.guess)" = "false" ]; then
1669 dwim_opt=""
1672 # Find the last provided --guess or --no-guess
1673 last_option="$(__git_find_last_on_cmdline "--guess --no-guess")"
1674 case "$last_option" in
1675 --guess)
1676 dwim_opt="--dwim"
1678 --no-guess)
1679 dwim_opt=""
1681 esac
1683 echo "$dwim_opt"
1686 _git_checkout ()
1688 __git_has_doubledash && return
1690 local dwim_opt="$(__git_checkout_default_dwim_mode)"
1692 case "$prev" in
1693 -b|-B|--orphan)
1694 # Complete local branches (and DWIM branch
1695 # remote branch names) for an option argument
1696 # specifying a new branch name. This is for
1697 # convenience, assuming new branches are
1698 # possibly based on pre-existing branch names.
1699 __git_complete_refs $dwim_opt --mode="heads"
1700 return
1704 esac
1706 case "$cur" in
1707 --conflict=*)
1708 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
1710 --*)
1711 __gitcomp_builtin checkout
1714 # At this point, we've already handled special completion for
1715 # the arguments to -b/-B, and --orphan. There are 3 main
1716 # things left we can possibly complete:
1717 # 1) a start-point for -b/-B, -d/--detach, or --orphan
1718 # 2) a remote head, for --track
1719 # 3) an arbitrary reference, possibly including DWIM names
1722 if [ -n "$(__git_find_on_cmdline "-b -B -d --detach --orphan")" ]; then
1723 __git_complete_refs --mode="refs"
1724 elif [ -n "$(__git_find_on_cmdline "-t --track")" ]; then
1725 __git_complete_refs --mode="remote-heads"
1726 else
1727 __git_complete_refs $dwim_opt --mode="refs"
1730 esac
1733 __git_sequencer_inprogress_options="--continue --quit --abort --skip"
1735 __git_cherry_pick_inprogress_options=$__git_sequencer_inprogress_options
1737 _git_cherry_pick ()
1739 if __git_pseudoref_exists CHERRY_PICK_HEAD; then
1740 __gitcomp "$__git_cherry_pick_inprogress_options"
1741 return
1744 __git_complete_strategy && return
1746 case "$cur" in
1747 --*)
1748 __gitcomp_builtin cherry-pick "" \
1749 "$__git_cherry_pick_inprogress_options"
1752 __git_complete_refs
1754 esac
1757 _git_clean ()
1759 case "$cur" in
1760 --*)
1761 __gitcomp_builtin clean
1762 return
1764 esac
1766 # XXX should we check for -x option ?
1767 __git_complete_index_file "--others --directory"
1770 _git_clone ()
1772 case "$prev" in
1773 -c|--config)
1774 __git_complete_config_variable_name_and_value
1775 return
1777 esac
1778 case "$cur" in
1779 --config=*)
1780 __git_complete_config_variable_name_and_value \
1781 --cur="${cur##--config=}"
1782 return
1784 --*)
1785 __gitcomp_builtin clone
1786 return
1788 esac
1791 __git_untracked_file_modes="all no normal"
1793 __git_trailer_tokens ()
1795 __git config --name-only --get-regexp '^trailer\..*\.key$' | cut -d. -f 2- | rev | cut -d. -f2- | rev
1798 _git_commit ()
1800 case "$prev" in
1801 -c|-C)
1802 __git_complete_refs
1803 return
1805 esac
1807 case "$cur" in
1808 --cleanup=*)
1809 __gitcomp "default scissors strip verbatim whitespace
1810 " "" "${cur##--cleanup=}"
1811 return
1813 --reuse-message=*|--reedit-message=*|\
1814 --fixup=*|--squash=*)
1815 __git_complete_refs --cur="${cur#*=}"
1816 return
1818 --untracked-files=*)
1819 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
1820 return
1822 --trailer=*)
1823 __gitcomp_nl "$(__git_trailer_tokens)" "" "${cur##--trailer=}" ":"
1824 return
1826 --*)
1827 __gitcomp_builtin commit
1828 return
1829 esac
1831 if __git rev-parse --verify --quiet HEAD >/dev/null; then
1832 __git_complete_index_file "--committable"
1833 else
1834 # This is the first commit
1835 __git_complete_index_file "--cached"
1839 _git_describe ()
1841 case "$cur" in
1842 --*)
1843 __gitcomp_builtin describe
1844 return
1845 esac
1846 __git_complete_refs
1849 __git_diff_algorithms="myers minimal patience histogram"
1851 __git_diff_submodule_formats="diff log short"
1853 __git_color_moved_opts="no default plain blocks zebra dimmed-zebra"
1855 __git_color_moved_ws_opts="no ignore-space-at-eol ignore-space-change
1856 ignore-all-space allow-indentation-change"
1858 __git_ws_error_highlight_opts="context old new all default"
1860 # Options for the diff machinery (diff, log, show, stash, range-diff, ...)
1861 __git_diff_common_options="--stat --numstat --shortstat --summary
1862 --patch-with-stat --name-only --name-status --color
1863 --no-color --color-words --no-renames --check
1864 --color-moved --color-moved= --no-color-moved
1865 --color-moved-ws= --no-color-moved-ws
1866 --full-index --binary --abbrev --diff-filter=
1867 --find-copies --find-object --find-renames
1868 --no-relative --relative
1869 --find-copies-harder --ignore-cr-at-eol
1870 --text --ignore-space-at-eol --ignore-space-change
1871 --ignore-all-space --ignore-blank-lines --exit-code
1872 --quiet --ext-diff --no-ext-diff --unified=
1873 --no-prefix --src-prefix= --dst-prefix=
1874 --inter-hunk-context= --function-context
1875 --patience --histogram --minimal
1876 --raw --word-diff --word-diff-regex=
1877 --dirstat --dirstat= --dirstat-by-file
1878 --dirstat-by-file= --cumulative
1879 --diff-algorithm= --default-prefix
1880 --submodule --submodule= --ignore-submodules
1881 --indent-heuristic --no-indent-heuristic
1882 --textconv --no-textconv --break-rewrites
1883 --patch --no-patch --cc --combined-all-paths
1884 --anchored= --compact-summary --ignore-matching-lines=
1885 --irreversible-delete --line-prefix --no-stat
1886 --output= --output-indicator-context=
1887 --output-indicator-new= --output-indicator-old=
1888 --ws-error-highlight=
1889 --pickaxe-all --pickaxe-regex --patch-with-raw
1892 # Options for diff/difftool
1893 __git_diff_difftool_options="--cached --staged
1894 --base --ours --theirs --no-index --merge-base
1895 --ita-invisible-in-index --ita-visible-in-index
1896 $__git_diff_common_options"
1898 _git_diff ()
1900 __git_has_doubledash && return
1902 case "$cur" in
1903 --diff-algorithm=*)
1904 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1905 return
1907 --submodule=*)
1908 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1909 return
1911 --color-moved=*)
1912 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
1913 return
1915 --color-moved-ws=*)
1916 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
1917 return
1919 --ws-error-highlight=*)
1920 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
1921 return
1923 --*)
1924 __gitcomp "$__git_diff_difftool_options"
1925 return
1927 esac
1928 __git_complete_revlist_file
1931 __git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff
1932 tkdiff vimdiff nvimdiff gvimdiff xxdiff araxis p4merge
1933 bc codecompare smerge
1936 _git_difftool ()
1938 __git_has_doubledash && return
1940 case "$cur" in
1941 --tool=*)
1942 __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1943 return
1945 --*)
1946 __gitcomp_builtin difftool "$__git_diff_difftool_options"
1947 return
1949 esac
1950 __git_complete_revlist_file
1953 __git_fetch_recurse_submodules="yes on-demand no"
1955 _git_fetch ()
1957 case "$cur" in
1958 --recurse-submodules=*)
1959 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1960 return
1962 --filter=*)
1963 __gitcomp "blob:none blob:limit= sparse:oid=" "" "${cur##--filter=}"
1964 return
1966 --*)
1967 __gitcomp_builtin fetch
1968 return
1970 esac
1971 __git_complete_remote_or_refspec
1974 __git_format_patch_extra_options="
1975 --full-index --not --all --no-prefix --src-prefix=
1976 --dst-prefix= --notes
1979 _git_format_patch ()
1981 case "$cur" in
1982 --thread=*)
1983 __gitcomp "
1984 deep shallow
1985 " "" "${cur##--thread=}"
1986 return
1988 --base=*|--interdiff=*|--range-diff=*)
1989 __git_complete_refs --cur="${cur#--*=}"
1990 return
1992 --*)
1993 __gitcomp_builtin format-patch "$__git_format_patch_extra_options"
1994 return
1996 esac
1997 __git_complete_revlist
2000 _git_fsck ()
2002 case "$cur" in
2003 --*)
2004 __gitcomp_builtin fsck
2005 return
2007 esac
2010 _git_gitk ()
2012 __gitk_main
2015 # Lists matching symbol names from a tag (as in ctags) file.
2016 # 1: List symbol names matching this word.
2017 # 2: The tag file to list symbol names from.
2018 # 3: A prefix to be added to each listed symbol name (optional).
2019 # 4: A suffix to be appended to each listed symbol name (optional).
2020 __git_match_ctag () {
2021 awk -v pfx="${3-}" -v sfx="${4-}" "
2022 /^${1//\//\\/}/ { print pfx \$1 sfx }
2023 " "$2"
2026 # Complete symbol names from a tag file.
2027 # Usage: __git_complete_symbol [<option>]...
2028 # --tags=<file>: The tag file to list symbol names from instead of the
2029 # default "tags".
2030 # --pfx=<prefix>: A prefix to be added to each symbol name.
2031 # --cur=<word>: The current symbol name to be completed. Defaults to
2032 # the current word to be completed.
2033 # --sfx=<suffix>: A suffix to be appended to each symbol name instead
2034 # of the default space.
2035 __git_complete_symbol () {
2036 local tags=tags pfx="" cur_="${cur-}" sfx=" "
2038 while test $# != 0; do
2039 case "$1" in
2040 --tags=*) tags="${1##--tags=}" ;;
2041 --pfx=*) pfx="${1##--pfx=}" ;;
2042 --cur=*) cur_="${1##--cur=}" ;;
2043 --sfx=*) sfx="${1##--sfx=}" ;;
2044 *) return 1 ;;
2045 esac
2046 shift
2047 done
2049 if test -r "$tags"; then
2050 __gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
2054 _git_grep ()
2056 __git_has_doubledash && return
2058 case "$cur" in
2059 --*)
2060 __gitcomp_builtin grep
2061 return
2063 esac
2065 case "$cword,$prev" in
2066 $((__git_cmd_idx+1)),*|*,-*)
2067 __git_complete_symbol && return
2069 esac
2071 __git_complete_refs
2074 _git_help ()
2076 case "$cur" in
2077 --*)
2078 __gitcomp_builtin help
2079 return
2081 esac
2082 if test -n "${GIT_TESTING_ALL_COMMAND_LIST-}"
2083 then
2084 __gitcomp "$GIT_TESTING_ALL_COMMAND_LIST $(__git --list-cmds=alias,list-guide) gitk"
2085 else
2086 __gitcomp "$(__git --list-cmds=main,nohelpers,alias,list-guide) gitk"
2090 _git_init ()
2092 case "$cur" in
2093 --shared=*)
2094 __gitcomp "
2095 false true umask group all world everybody
2096 " "" "${cur##--shared=}"
2097 return
2099 --*)
2100 __gitcomp_builtin init
2101 return
2103 esac
2106 _git_ls_files ()
2108 case "$cur" in
2109 --*)
2110 __gitcomp_builtin ls-files
2111 return
2113 esac
2115 # XXX ignore options like --modified and always suggest all cached
2116 # files.
2117 __git_complete_index_file "--cached"
2120 _git_ls_remote ()
2122 case "$cur" in
2123 --*)
2124 __gitcomp_builtin ls-remote
2125 return
2127 esac
2128 __gitcomp_nl "$(__git_remotes)"
2131 _git_ls_tree ()
2133 case "$cur" in
2134 --*)
2135 __gitcomp_builtin ls-tree
2136 return
2138 esac
2140 __git_complete_file
2143 # Options that go well for log, shortlog and gitk
2144 __git_log_common_options="
2145 --not --all
2146 --branches --tags --remotes
2147 --first-parent --merges --no-merges
2148 --max-count=
2149 --max-age= --since= --after=
2150 --min-age= --until= --before=
2151 --min-parents= --max-parents=
2152 --no-min-parents --no-max-parents
2153 --alternate-refs --ancestry-path
2154 --author-date-order --basic-regexp
2155 --bisect --boundary --exclude-first-parent-only
2156 --exclude-hidden --extended-regexp
2157 --fixed-strings --grep-reflog
2158 --ignore-missing --left-only --perl-regexp
2159 --reflog --regexp-ignore-case --remove-empty
2160 --right-only --show-linear-break
2161 --show-notes-by-default --show-pulls
2162 --since-as-filter --single-worktree
2164 # Options that go well for log and gitk (not shortlog)
2165 __git_log_gitk_options="
2166 --dense --sparse --full-history
2167 --simplify-merges --simplify-by-decoration
2168 --left-right --notes --no-notes
2170 # Options that go well for log and shortlog (not gitk)
2171 __git_log_shortlog_options="
2172 --author= --committer= --grep=
2173 --all-match --invert-grep
2175 # Options accepted by log and show
2176 __git_log_show_options="
2177 --diff-merges --diff-merges= --no-diff-merges --dd --remerge-diff
2178 --encoding=
2181 __git_diff_merges_opts="off none on first-parent 1 separate m combined c dense-combined cc remerge r"
2183 __git_log_pretty_formats="oneline short medium full fuller reference email raw format: tformat: mboxrd"
2184 __git_log_date_formats="relative iso8601 iso8601-strict rfc2822 short local default human raw unix auto: format:"
2186 # Complete porcelain (i.e. not git-rev-list) options and at least some
2187 # option arguments accepted by git-log. Note that this same set of options
2188 # are also accepted by some other git commands besides git-log.
2189 __git_complete_log_opts ()
2191 COMPREPLY=()
2193 local merge=""
2194 if __git_pseudoref_exists MERGE_HEAD; then
2195 merge="--merge"
2197 case "$prev,$cur" in
2198 -L,:*:*)
2199 return # fall back to Bash filename completion
2201 -L,:*)
2202 __git_complete_symbol --cur="${cur#:}" --sfx=":"
2203 return
2205 -G,*|-S,*)
2206 __git_complete_symbol
2207 return
2209 esac
2210 case "$cur" in
2211 --pretty=*|--format=*)
2212 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2213 " "" "${cur#*=}"
2214 return
2216 --date=*)
2217 __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
2218 return
2220 --decorate=*)
2221 __gitcomp "full short no" "" "${cur##--decorate=}"
2222 return
2224 --diff-algorithm=*)
2225 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
2226 return
2228 --submodule=*)
2229 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
2230 return
2232 --ws-error-highlight=*)
2233 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
2234 return
2236 --no-walk=*)
2237 __gitcomp "sorted unsorted" "" "${cur##--no-walk=}"
2238 return
2240 --diff-merges=*)
2241 __gitcomp "$__git_diff_merges_opts" "" "${cur##--diff-merges=}"
2242 return
2244 --*)
2245 __gitcomp "
2246 $__git_log_common_options
2247 $__git_log_shortlog_options
2248 $__git_log_gitk_options
2249 $__git_log_show_options
2250 --root --topo-order --date-order --reverse
2251 --follow --full-diff
2252 --abbrev-commit --no-abbrev-commit --abbrev=
2253 --relative-date --date=
2254 --pretty= --format= --oneline
2255 --show-signature
2256 --cherry-mark
2257 --cherry-pick
2258 --graph
2259 --decorate --decorate= --no-decorate
2260 --walk-reflogs
2261 --no-walk --no-walk= --do-walk
2262 --parents --children
2263 --expand-tabs --expand-tabs= --no-expand-tabs
2264 --clear-decorations --decorate-refs=
2265 --decorate-refs-exclude=
2266 $merge
2267 $__git_diff_common_options
2269 return
2271 -L:*:*)
2272 return # fall back to Bash filename completion
2274 -L:*)
2275 __git_complete_symbol --cur="${cur#-L:}" --sfx=":"
2276 return
2278 -G*)
2279 __git_complete_symbol --pfx="-G" --cur="${cur#-G}"
2280 return
2282 -S*)
2283 __git_complete_symbol --pfx="-S" --cur="${cur#-S}"
2284 return
2286 esac
2289 _git_log ()
2291 __git_has_doubledash && return
2292 __git_find_repo_path
2294 __git_complete_log_opts
2295 [ ${#COMPREPLY[@]} -eq 0 ] || return
2297 __git_complete_revlist
2300 _git_merge ()
2302 __git_complete_strategy && return
2304 case "$cur" in
2305 --*)
2306 __gitcomp_builtin merge
2307 return
2308 esac
2309 __git_complete_refs
2312 _git_mergetool ()
2314 case "$cur" in
2315 --tool=*)
2316 __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
2317 return
2319 --*)
2320 __gitcomp "--tool= --prompt --no-prompt --gui --no-gui"
2321 return
2323 esac
2326 _git_merge_base ()
2328 case "$cur" in
2329 --*)
2330 __gitcomp_builtin merge-base
2331 return
2333 esac
2334 __git_complete_refs
2337 _git_mv ()
2339 case "$cur" in
2340 --*)
2341 __gitcomp_builtin mv
2342 return
2344 esac
2346 if [ $(__git_count_arguments "mv") -gt 0 ]; then
2347 # We need to show both cached and untracked files (including
2348 # empty directories) since this may not be the last argument.
2349 __git_complete_index_file "--cached --others --directory"
2350 else
2351 __git_complete_index_file "--cached"
2355 _git_notes ()
2357 local subcommands='add append copy edit get-ref list merge prune remove show'
2358 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2360 case "$subcommand,$cur" in
2361 ,--*)
2362 __gitcomp_builtin notes
2365 case "$prev" in
2366 --ref)
2367 __git_complete_refs
2370 __gitcomp "$subcommands --ref"
2372 esac
2374 *,--reuse-message=*|*,--reedit-message=*)
2375 __git_complete_refs --cur="${cur#*=}"
2377 *,--*)
2378 __gitcomp_builtin notes_$subcommand
2380 prune,*|get-ref,*)
2381 # this command does not take a ref, do not complete it
2384 case "$prev" in
2385 -m|-F)
2388 __git_complete_refs
2390 esac
2392 esac
2395 _git_pull ()
2397 __git_complete_strategy && return
2399 case "$cur" in
2400 --recurse-submodules=*)
2401 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
2402 return
2404 --*)
2405 __gitcomp_builtin pull
2407 return
2409 esac
2410 __git_complete_remote_or_refspec
2413 __git_push_recurse_submodules="check on-demand only"
2415 __git_complete_force_with_lease ()
2417 local cur_=$1
2419 case "$cur_" in
2420 --*=)
2422 *:*)
2423 __git_complete_refs --cur="${cur_#*:}"
2426 __git_complete_refs --cur="$cur_"
2428 esac
2431 _git_push ()
2433 case "$prev" in
2434 --repo)
2435 __gitcomp_nl "$(__git_remotes)"
2436 return
2438 --recurse-submodules)
2439 __gitcomp "$__git_push_recurse_submodules"
2440 return
2442 esac
2443 case "$cur" in
2444 --repo=*)
2445 __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
2446 return
2448 --recurse-submodules=*)
2449 __gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
2450 return
2452 --force-with-lease=*)
2453 __git_complete_force_with_lease "${cur##--force-with-lease=}"
2454 return
2456 --*)
2457 __gitcomp_builtin push
2458 return
2460 esac
2461 __git_complete_remote_or_refspec
2464 _git_range_diff ()
2466 case "$cur" in
2467 --*)
2468 __gitcomp "
2469 --creation-factor= --no-dual-color
2470 $__git_diff_common_options
2472 return
2474 esac
2475 __git_complete_revlist
2478 __git_rebase_inprogress_options="--continue --skip --abort --quit --show-current-patch"
2479 __git_rebase_interactive_inprogress_options="$__git_rebase_inprogress_options --edit-todo"
2481 _git_rebase ()
2483 __git_find_repo_path
2484 if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
2485 __gitcomp "$__git_rebase_interactive_inprogress_options"
2486 return
2487 elif [ -d "$__git_repo_path"/rebase-apply ] || \
2488 [ -d "$__git_repo_path"/rebase-merge ]; then
2489 __gitcomp "$__git_rebase_inprogress_options"
2490 return
2492 __git_complete_strategy && return
2493 case "$cur" in
2494 --whitespace=*)
2495 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
2496 return
2498 --onto=*)
2499 __git_complete_refs --cur="${cur##--onto=}"
2500 return
2502 --*)
2503 __gitcomp_builtin rebase "" \
2504 "$__git_rebase_interactive_inprogress_options"
2506 return
2507 esac
2508 __git_complete_refs
2511 _git_reflog ()
2513 local subcommands subcommand
2515 __git_resolve_builtins "reflog"
2517 subcommands="$___git_resolved_builtins"
2518 subcommand="$(__git_find_subcommand "$subcommands" "show")"
2520 case "$subcommand,$cur" in
2521 show,--*)
2522 __gitcomp "
2523 $__git_log_common_options
2525 return
2527 $subcommand,--*)
2528 __gitcomp_builtin "reflog_$subcommand"
2529 return
2531 esac
2533 __git_complete_refs
2535 if [ $((cword - __git_cmd_idx)) -eq 1 ]; then
2536 __gitcompappend "$subcommands" "" "$cur" " "
2540 __git_send_email_confirm_options="always never auto cc compose"
2541 __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
2543 _git_send_email ()
2545 case "$prev" in
2546 --to|--cc|--bcc|--from)
2547 __gitcomp "$(__git send-email --dump-aliases)"
2548 return
2550 esac
2552 case "$cur" in
2553 --confirm=*)
2554 __gitcomp "
2555 $__git_send_email_confirm_options
2556 " "" "${cur##--confirm=}"
2557 return
2559 --suppress-cc=*)
2560 __gitcomp "
2561 $__git_send_email_suppresscc_options
2562 " "" "${cur##--suppress-cc=}"
2564 return
2566 --smtp-encryption=*)
2567 __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
2568 return
2570 --thread=*)
2571 __gitcomp "
2572 deep shallow
2573 " "" "${cur##--thread=}"
2574 return
2576 --to=*|--cc=*|--bcc=*|--from=*)
2577 __gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
2578 return
2580 --*)
2581 __gitcomp_builtin send-email "$__git_format_patch_extra_options"
2582 return
2584 esac
2585 __git_complete_revlist
2588 _git_stage ()
2590 _git_add
2593 _git_status ()
2595 local complete_opt
2596 local untracked_state
2598 case "$cur" in
2599 --ignore-submodules=*)
2600 __gitcomp "none untracked dirty all" "" "${cur##--ignore-submodules=}"
2601 return
2603 --untracked-files=*)
2604 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
2605 return
2607 --column=*)
2608 __gitcomp "
2609 always never auto column row plain dense nodense
2610 " "" "${cur##--column=}"
2611 return
2613 --*)
2614 __gitcomp_builtin status
2615 return
2617 esac
2619 untracked_state="$(__git_get_option_value "-u" "--untracked-files=" \
2620 "$__git_untracked_file_modes" "status.showUntrackedFiles")"
2622 case "$untracked_state" in
2624 # --ignored option does not matter
2625 complete_opt=
2627 all|normal|*)
2628 complete_opt="--cached --directory --no-empty-directory --others"
2630 if [ -n "$(__git_find_on_cmdline "--ignored")" ]; then
2631 complete_opt="$complete_opt --ignored --exclude=*"
2634 esac
2636 __git_complete_index_file "$complete_opt"
2639 _git_switch ()
2641 local dwim_opt="$(__git_checkout_default_dwim_mode)"
2643 case "$prev" in
2644 -c|-C|--orphan)
2645 # Complete local branches (and DWIM branch
2646 # remote branch names) for an option argument
2647 # specifying a new branch name. This is for
2648 # convenience, assuming new branches are
2649 # possibly based on pre-existing branch names.
2650 __git_complete_refs $dwim_opt --mode="heads"
2651 return
2655 esac
2657 case "$cur" in
2658 --conflict=*)
2659 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
2661 --*)
2662 __gitcomp_builtin switch
2665 # Unlike in git checkout, git switch --orphan does not take
2666 # a start point. Thus we really have nothing to complete after
2667 # the branch name.
2668 if [ -n "$(__git_find_on_cmdline "--orphan")" ]; then
2669 return
2672 # At this point, we've already handled special completion for
2673 # -c/-C, and --orphan. There are 3 main things left to
2674 # complete:
2675 # 1) a start-point for -c/-C or -d/--detach
2676 # 2) a remote head, for --track
2677 # 3) a branch name, possibly including DWIM remote branches
2679 if [ -n "$(__git_find_on_cmdline "-c -C -d --detach")" ]; then
2680 __git_complete_refs --mode="refs"
2681 elif [ -n "$(__git_find_on_cmdline "-t --track")" ]; then
2682 __git_complete_refs --mode="remote-heads"
2683 else
2684 __git_complete_refs $dwim_opt --mode="heads"
2687 esac
2690 __git_config_get_set_variables ()
2692 local prevword word config_file= c=$cword
2693 while [ $c -gt "$__git_cmd_idx" ]; do
2694 word="${words[c]}"
2695 case "$word" in
2696 --system|--global|--local|--file=*)
2697 config_file="$word"
2698 break
2700 -f|--file)
2701 config_file="$word $prevword"
2702 break
2704 esac
2705 prevword=$word
2706 c=$((--c))
2707 done
2709 __git config $config_file --name-only --list
2712 __git_config_vars=
2713 __git_compute_config_vars ()
2715 test -n "$__git_config_vars" ||
2716 __git_config_vars="$(git help --config-for-completion)"
2719 __git_config_vars_all=
2720 __git_compute_config_vars_all ()
2722 test -n "$__git_config_vars_all" ||
2723 __git_config_vars_all="$(git --no-pager help --config)"
2726 __git_compute_first_level_config_vars_for_section ()
2728 local section="$1"
2729 __git_compute_config_vars
2730 local this_section="__git_first_level_config_vars_for_section_${section}"
2731 test -n "${!this_section}" ||
2732 printf -v "__git_first_level_config_vars_for_section_${section}" %s \
2733 "$(echo "$__git_config_vars" | awk -F. "/^${section}\.[a-z]/ { print \$2 }")"
2736 __git_compute_second_level_config_vars_for_section ()
2738 local section="$1"
2739 __git_compute_config_vars_all
2740 local this_section="__git_second_level_config_vars_for_section_${section}"
2741 test -n "${!this_section}" ||
2742 printf -v "__git_second_level_config_vars_for_section_${section}" %s \
2743 "$(echo "$__git_config_vars_all" | awk -F. "/^${section}\.</ { print \$3 }")"
2746 __git_config_sections=
2747 __git_compute_config_sections ()
2749 test -n "$__git_config_sections" ||
2750 __git_config_sections="$(git help --config-sections-for-completion)"
2753 # Completes possible values of various configuration variables.
2755 # Usage: __git_complete_config_variable_value [<option>]...
2756 # --varname=<word>: The name of the configuration variable whose value is
2757 # to be completed. Defaults to the previous word on the
2758 # command line.
2759 # --cur=<word>: The current value to be completed. Defaults to the current
2760 # word to be completed.
2761 __git_complete_config_variable_value ()
2763 local varname="$prev" cur_="$cur"
2765 while test $# != 0; do
2766 case "$1" in
2767 --varname=*) varname="${1##--varname=}" ;;
2768 --cur=*) cur_="${1##--cur=}" ;;
2769 *) return 1 ;;
2770 esac
2771 shift
2772 done
2774 if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
2775 varname="${varname,,}"
2776 else
2777 varname="$(echo "$varname" |tr A-Z a-z)"
2780 case "$varname" in
2781 branch.*.remote|branch.*.pushremote)
2782 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2783 return
2785 branch.*.merge)
2786 __git_complete_refs --cur="$cur_"
2787 return
2789 branch.*.rebase)
2790 __gitcomp "false true merges interactive" "" "$cur_"
2791 return
2793 remote.pushdefault)
2794 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2795 return
2797 remote.*.fetch)
2798 local remote="${varname#remote.}"
2799 remote="${remote%.fetch}"
2800 if [ -z "$cur_" ]; then
2801 __gitcomp_nl "refs/heads/" "" "" ""
2802 return
2804 __gitcomp_nl "$(__git_refs_remotes "$remote")" "" "$cur_"
2805 return
2807 remote.*.push)
2808 local remote="${varname#remote.}"
2809 remote="${remote%.push}"
2810 __gitcomp_nl "$(__git for-each-ref \
2811 --format='%(refname):%(refname)' refs/heads)" "" "$cur_"
2812 return
2814 pull.twohead|pull.octopus)
2815 __git_compute_merge_strategies
2816 __gitcomp "$__git_merge_strategies" "" "$cur_"
2817 return
2819 color.pager)
2820 __gitcomp "false true" "" "$cur_"
2821 return
2823 color.*.*)
2824 __gitcomp "
2825 normal black red green yellow blue magenta cyan white
2826 bold dim ul blink reverse
2827 " "" "$cur_"
2828 return
2830 color.*)
2831 __gitcomp "false true always never auto" "" "$cur_"
2832 return
2834 diff.submodule)
2835 __gitcomp "$__git_diff_submodule_formats" "" "$cur_"
2836 return
2838 help.format)
2839 __gitcomp "man info web html" "" "$cur_"
2840 return
2842 log.date)
2843 __gitcomp "$__git_log_date_formats" "" "$cur_"
2844 return
2846 sendemail.aliasfiletype)
2847 __gitcomp "mutt mailrc pine elm gnus" "" "$cur_"
2848 return
2850 sendemail.confirm)
2851 __gitcomp "$__git_send_email_confirm_options" "" "$cur_"
2852 return
2854 sendemail.suppresscc)
2855 __gitcomp "$__git_send_email_suppresscc_options" "" "$cur_"
2856 return
2858 sendemail.transferencoding)
2859 __gitcomp "7bit 8bit quoted-printable base64" "" "$cur_"
2860 return
2862 *.*)
2863 return
2865 esac
2868 # Completes configuration sections, subsections, variable names.
2870 # Usage: __git_complete_config_variable_name [<option>]...
2871 # --cur=<word>: The current configuration section/variable name to be
2872 # completed. Defaults to the current word to be completed.
2873 # --sfx=<suffix>: A suffix to be appended to each fully completed
2874 # configuration variable name (but not to sections or
2875 # subsections) instead of the default space.
2876 __git_complete_config_variable_name ()
2878 local cur_="$cur" sfx
2880 while test $# != 0; do
2881 case "$1" in
2882 --cur=*) cur_="${1##--cur=}" ;;
2883 --sfx=*) sfx="${1##--sfx=}" ;;
2884 *) return 1 ;;
2885 esac
2886 shift
2887 done
2889 case "$cur_" in
2890 branch.*.*|guitool.*.*|difftool.*.*|man.*.*|mergetool.*.*|remote.*.*|submodule.*.*|url.*.*)
2891 local pfx="${cur_%.*}."
2892 cur_="${cur_##*.}"
2893 local section="${pfx%.*.}"
2894 __git_compute_second_level_config_vars_for_section "${section}"
2895 local this_section="__git_second_level_config_vars_for_section_${section}"
2896 __gitcomp "${!this_section}" "$pfx" "$cur_" "$sfx"
2897 return
2899 branch.*)
2900 local pfx="${cur_%.*}."
2901 cur_="${cur_#*.}"
2902 local section="${pfx%.}"
2903 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
2904 __git_compute_first_level_config_vars_for_section "${section}"
2905 local this_section="__git_first_level_config_vars_for_section_${section}"
2906 __gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
2907 return
2909 pager.*)
2910 local pfx="${cur_%.*}."
2911 cur_="${cur_#*.}"
2912 __git_compute_all_commands
2913 __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_" "${sfx:- }"
2914 return
2916 remote.*)
2917 local pfx="${cur_%.*}."
2918 cur_="${cur_#*.}"
2919 local section="${pfx%.}"
2920 __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
2921 __git_compute_first_level_config_vars_for_section "${section}"
2922 local this_section="__git_first_level_config_vars_for_section_${section}"
2923 __gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
2924 return
2926 submodule.*)
2927 local pfx="${cur_%.*}."
2928 cur_="${cur_#*.}"
2929 local section="${pfx%.}"
2930 __gitcomp_nl "$(__git config -f "$(__git rev-parse --show-toplevel)/.gitmodules" --get-regexp 'submodule.*.path' | awk -F. '{print $2}')" "$pfx" "$cur_" "."
2931 __git_compute_first_level_config_vars_for_section "${section}"
2932 local this_section="__git_first_level_config_vars_for_section_${section}"
2933 __gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
2934 return
2936 *.*)
2937 __git_compute_config_vars
2938 __gitcomp "$__git_config_vars" "" "$cur_" "$sfx"
2941 __git_compute_config_sections
2942 __gitcomp "$__git_config_sections" "" "$cur_" "."
2944 esac
2947 # Completes '='-separated configuration sections/variable names and values
2948 # for 'git -c section.name=value'.
2950 # Usage: __git_complete_config_variable_name_and_value [<option>]...
2951 # --cur=<word>: The current configuration section/variable name/value to be
2952 # completed. Defaults to the current word to be completed.
2953 __git_complete_config_variable_name_and_value ()
2955 local cur_="$cur"
2957 while test $# != 0; do
2958 case "$1" in
2959 --cur=*) cur_="${1##--cur=}" ;;
2960 *) return 1 ;;
2961 esac
2962 shift
2963 done
2965 case "$cur_" in
2966 *=*)
2967 __git_complete_config_variable_value \
2968 --varname="${cur_%%=*}" --cur="${cur_#*=}"
2971 __git_complete_config_variable_name --cur="$cur_" --sfx='='
2973 esac
2976 _git_config ()
2978 case "$prev" in
2979 --get|--get-all|--unset|--unset-all)
2980 __gitcomp_nl "$(__git_config_get_set_variables)"
2981 return
2983 *.*)
2984 __git_complete_config_variable_value
2985 return
2987 esac
2988 case "$cur" in
2989 --*)
2990 __gitcomp_builtin config
2993 __git_complete_config_variable_name
2995 esac
2998 _git_remote ()
3000 local subcommands="
3001 add rename remove set-head set-branches
3002 get-url set-url show prune update
3004 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3005 if [ -z "$subcommand" ]; then
3006 case "$cur" in
3007 --*)
3008 __gitcomp_builtin remote
3011 __gitcomp "$subcommands"
3013 esac
3014 return
3017 case "$subcommand,$cur" in
3018 add,--*)
3019 __gitcomp_builtin remote_add
3021 add,*)
3023 set-head,--*)
3024 __gitcomp_builtin remote_set-head
3026 set-branches,--*)
3027 __gitcomp_builtin remote_set-branches
3029 set-head,*|set-branches,*)
3030 __git_complete_remote_or_refspec
3032 update,--*)
3033 __gitcomp_builtin remote_update
3035 update,*)
3036 __gitcomp "$(__git_remotes) $(__git_get_config_variables "remotes")"
3038 set-url,--*)
3039 __gitcomp_builtin remote_set-url
3041 get-url,--*)
3042 __gitcomp_builtin remote_get-url
3044 prune,--*)
3045 __gitcomp_builtin remote_prune
3048 __gitcomp_nl "$(__git_remotes)"
3050 esac
3053 _git_replace ()
3055 case "$cur" in
3056 --format=*)
3057 __gitcomp "short medium long" "" "${cur##--format=}"
3058 return
3060 --*)
3061 __gitcomp_builtin replace
3062 return
3064 esac
3065 __git_complete_refs
3068 _git_rerere ()
3070 local subcommands="clear forget diff remaining status gc"
3071 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3072 if test -z "$subcommand"
3073 then
3074 __gitcomp "$subcommands"
3075 return
3079 _git_reset ()
3081 __git_has_doubledash && return
3083 case "$cur" in
3084 --*)
3085 __gitcomp_builtin reset
3086 return
3088 esac
3089 __git_complete_refs
3092 _git_restore ()
3094 case "$prev" in
3096 __git_complete_refs
3097 return
3099 esac
3101 case "$cur" in
3102 --conflict=*)
3103 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
3105 --source=*)
3106 __git_complete_refs --cur="${cur##--source=}"
3108 --*)
3109 __gitcomp_builtin restore
3112 if __git_pseudoref_exists HEAD; then
3113 __git_complete_index_file "--modified"
3115 esac
3118 __git_revert_inprogress_options=$__git_sequencer_inprogress_options
3120 _git_revert ()
3122 if __git_pseudoref_exists REVERT_HEAD; then
3123 __gitcomp "$__git_revert_inprogress_options"
3124 return
3126 __git_complete_strategy && return
3127 case "$cur" in
3128 --*)
3129 __gitcomp_builtin revert "" \
3130 "$__git_revert_inprogress_options"
3131 return
3133 esac
3134 __git_complete_refs
3137 _git_rm ()
3139 case "$cur" in
3140 --*)
3141 __gitcomp_builtin rm
3142 return
3144 esac
3146 __git_complete_index_file "--cached"
3149 _git_shortlog ()
3151 __git_has_doubledash && return
3153 case "$cur" in
3154 --*)
3155 __gitcomp "
3156 $__git_log_common_options
3157 $__git_log_shortlog_options
3158 --numbered --summary --email
3160 return
3162 esac
3163 __git_complete_revlist
3166 _git_show ()
3168 __git_has_doubledash && return
3170 case "$cur" in
3171 --pretty=*|--format=*)
3172 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
3173 " "" "${cur#*=}"
3174 return
3176 --diff-algorithm=*)
3177 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
3178 return
3180 --submodule=*)
3181 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
3182 return
3184 --color-moved=*)
3185 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
3186 return
3188 --color-moved-ws=*)
3189 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
3190 return
3192 --ws-error-highlight=*)
3193 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
3194 return
3196 --diff-merges=*)
3197 __gitcomp "$__git_diff_merges_opts" "" "${cur##--diff-merges=}"
3198 return
3200 --*)
3201 __gitcomp "--pretty= --format= --abbrev-commit --no-abbrev-commit
3202 --oneline --show-signature
3203 --expand-tabs --expand-tabs= --no-expand-tabs
3204 $__git_log_show_options
3205 $__git_diff_common_options
3207 return
3209 esac
3210 __git_complete_revlist_file
3213 _git_show_branch ()
3215 case "$cur" in
3216 --*)
3217 __gitcomp_builtin show-branch
3218 return
3220 esac
3221 __git_complete_revlist
3224 __gitcomp_directories ()
3226 local _tmp_dir _tmp_completions _found=0
3228 # Get the directory of the current token; this differs from dirname
3229 # in that it keeps up to the final trailing slash. If no slash found
3230 # that's fine too.
3231 [[ "$cur" =~ .*/ ]]
3232 _tmp_dir=$BASH_REMATCH
3234 # Find possible directory completions, adding trailing '/' characters,
3235 # de-quoting, and handling unusual characters.
3236 while IFS= read -r -d $'\0' c ; do
3237 # If there are directory completions, find ones that start
3238 # with "$cur", the current token, and put those in COMPREPLY
3239 if [[ $c == "$cur"* ]]; then
3240 COMPREPLY+=("$c/")
3241 _found=1
3243 done < <(__git ls-tree -z -d --name-only HEAD $_tmp_dir)
3245 if [[ $_found == 0 ]] && [[ "$cur" =~ /$ ]]; then
3246 # No possible further completions any deeper, so assume we're at
3247 # a leaf directory and just consider it complete
3248 __gitcomp_direct_append "$cur "
3249 elif [[ $_found == 0 ]]; then
3250 # No possible completions found. Avoid falling back to
3251 # bash's default file and directory completion, because all
3252 # valid completions have already been searched and the
3253 # fallbacks can do nothing but mislead. In fact, they can
3254 # mislead in three different ways:
3255 # 1) Fallback file completion makes no sense when asking
3256 # for directory completions, as this function does.
3257 # 2) Fallback directory completion is bad because
3258 # e.g. "/pro" is invalid and should NOT complete to
3259 # "/proc".
3260 # 3) Fallback file/directory completion only completes
3261 # on paths that exist in the current working tree,
3262 # i.e. which are *already* part of their
3263 # sparse-checkout. Thus, normal file and directory
3264 # completion is always useless for "git
3265 # sparse-checkout add" and is also probelmatic for
3266 # "git sparse-checkout set" unless using it to
3267 # strictly narrow the checkout.
3268 COMPREPLY=( "" )
3272 # In non-cone mode, the arguments to {set,add} are supposed to be
3273 # patterns, relative to the toplevel directory. These can be any kind
3274 # of general pattern, like 'subdir/*.c' and we can't complete on all
3275 # of those. However, if the user presses Tab to get tab completion, we
3276 # presume that they are trying to provide a pattern that names a specific
3277 # path.
3278 __gitcomp_slash_leading_paths ()
3280 local dequoted_word pfx="" cur_ toplevel
3282 # Since we are dealing with a sparse-checkout, subdirectories may not
3283 # exist in the local working copy. Therefore, we want to run all
3284 # ls-files commands relative to the repository toplevel.
3285 toplevel="$(git rev-parse --show-toplevel)/"
3287 __git_dequote "$cur"
3289 # If the paths provided by the user already start with '/', then
3290 # they are considered relative to the toplevel of the repository
3291 # already. If they do not start with /, then we need to adjust
3292 # them to start with the appropriate prefix.
3293 case "$cur" in
3295 cur="${cur:1}"
3298 pfx="$(__git rev-parse --show-prefix)"
3299 esac
3301 # Since sparse-index is limited to cone-mode, in non-cone-mode the
3302 # list of valid paths is precisely the cached files in the index.
3304 # NEEDSWORK:
3305 # 1) We probably need to take care of cases where ls-files
3306 # responds with special quoting.
3307 # 2) We probably need to take care of cases where ${cur} has
3308 # some kind of special quoting.
3309 # 3) On top of any quoting from 1 & 2, we have to provide an extra
3310 # level of quoting for any paths that contain a '*', '?', '\',
3311 # '[', ']', or leading '#' or '!' since those will be
3312 # interpreted by sparse-checkout as something other than a
3313 # literal path character.
3314 # Since there are two types of quoting here, this might get really
3315 # complex. For now, just punt on all of this...
3316 completions="$(__git -C "${toplevel}" -c core.quotePath=false \
3317 ls-files --cached -- "${pfx}${cur}*" \
3318 | sed -e s%^%/% -e 's%$% %')"
3319 # Note, above, though that we needed all of the completions to be
3320 # prefixed with a '/', and we want to add a space so that bash
3321 # completion will actually complete an entry and let us move on to
3322 # the next one.
3324 # Return what we've found.
3325 if test -n "$completions"; then
3326 # We found some completions; return them
3327 local IFS=$'\n'
3328 COMPREPLY=($completions)
3329 else
3330 # Do NOT fall back to bash-style all-local-files-and-dirs
3331 # when we find no match. Such options are worse than
3332 # useless:
3333 # 1. "git sparse-checkout add" needs paths that are NOT
3334 # currently in the working copy. "git
3335 # sparse-checkout set" does as well, except in the
3336 # special cases when users are only trying to narrow
3337 # their sparse checkout to a subset of what they
3338 # already have.
3340 # 2. A path like '.config' is ambiguous as to whether
3341 # the user wants all '.config' files throughout the
3342 # tree, or just the one under the current directory.
3343 # It would result in a warning from the
3344 # sparse-checkout command due to this. As such, all
3345 # completions of paths should be prefixed with a
3346 # '/'.
3348 # 3. We don't want paths prefixed with a '/' to
3349 # complete files in the system root directory, we
3350 # want it to complete on files relative to the
3351 # repository root.
3353 # As such, make sure that NO completions are offered rather
3354 # than falling back to bash's default completions.
3355 COMPREPLY=( "" )
3359 _git_sparse_checkout ()
3361 local subcommands="list init set disable add reapply"
3362 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3363 local using_cone=true
3364 if [ -z "$subcommand" ]; then
3365 __gitcomp "$subcommands"
3366 return
3369 case "$subcommand,$cur" in
3370 *,--*)
3371 __gitcomp_builtin sparse-checkout_$subcommand "" "--"
3373 set,*|add,*)
3374 if [[ "$(__git config core.sparseCheckout)" == "true" &&
3375 "$(__git config core.sparseCheckoutCone)" == "false" &&
3376 -z "$(__git_find_on_cmdline --cone)" ]]; then
3377 using_cone=false
3379 if [[ -n "$(__git_find_on_cmdline --no-cone)" ]]; then
3380 using_cone=false
3382 if [[ "$using_cone" == "true" ]]; then
3383 __gitcomp_directories
3384 else
3385 __gitcomp_slash_leading_paths
3387 esac
3390 _git_stash ()
3392 local subcommands='push list show apply clear drop pop create branch'
3393 local subcommand="$(__git_find_on_cmdline "$subcommands save")"
3395 if [ -z "$subcommand" ]; then
3396 case "$((cword - __git_cmd_idx)),$cur" in
3397 *,--*)
3398 __gitcomp_builtin stash_push
3400 1,sa*)
3401 __gitcomp "save"
3403 1,*)
3404 __gitcomp "$subcommands"
3406 esac
3407 return
3410 case "$subcommand,$cur" in
3411 list,--*)
3412 # NEEDSWORK: can we somehow unify this with the options in _git_log() and _git_show()
3413 __gitcomp_builtin stash_list "$__git_log_common_options $__git_diff_common_options"
3415 show,--*)
3416 __gitcomp_builtin stash_show "$__git_diff_common_options"
3418 *,--*)
3419 __gitcomp_builtin "stash_$subcommand"
3421 branch,*)
3422 if [ $cword -eq $((__git_cmd_idx+2)) ]; then
3423 __git_complete_refs
3424 else
3425 __gitcomp_nl "$(__git stash list \
3426 | sed -n -e 's/:.*//p')"
3429 show,*|apply,*|drop,*|pop,*)
3430 __gitcomp_nl "$(__git stash list \
3431 | sed -n -e 's/:.*//p')"
3433 esac
3436 _git_submodule ()
3438 __git_has_doubledash && return
3440 local subcommands="add status init deinit update set-branch set-url summary foreach sync absorbgitdirs"
3441 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3442 if [ -z "$subcommand" ]; then
3443 case "$cur" in
3444 --*)
3445 __gitcomp "--quiet"
3448 __gitcomp "$subcommands"
3450 esac
3451 return
3454 case "$subcommand,$cur" in
3455 add,--*)
3456 __gitcomp "--branch --force --name --reference --depth"
3458 status,--*)
3459 __gitcomp "--cached --recursive"
3461 deinit,--*)
3462 __gitcomp "--force --all"
3464 update,--*)
3465 __gitcomp "
3466 --init --remote --no-fetch
3467 --recommend-shallow --no-recommend-shallow
3468 --force --rebase --merge --reference --depth --recursive --jobs
3471 set-branch,--*)
3472 __gitcomp "--default --branch"
3474 summary,--*)
3475 __gitcomp "--cached --files --summary-limit"
3477 foreach,--*|sync,--*)
3478 __gitcomp "--recursive"
3482 esac
3485 _git_svn ()
3487 local subcommands="
3488 init fetch clone rebase dcommit log find-rev
3489 set-tree commit-diff info create-ignore propget
3490 proplist show-ignore show-externals branch tag blame
3491 migrate mkdirs reset gc
3493 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3494 if [ -z "$subcommand" ]; then
3495 __gitcomp "$subcommands"
3496 else
3497 local remote_opts="--username= --config-dir= --no-auth-cache"
3498 local fc_opts="
3499 --follow-parent --authors-file= --repack=
3500 --no-metadata --use-svm-props --use-svnsync-props
3501 --log-window-size= --no-checkout --quiet
3502 --repack-flags --use-log-author --localtime
3503 --add-author-from
3504 --recursive
3505 --ignore-paths= --include-paths= $remote_opts
3507 local init_opts="
3508 --template= --shared= --trunk= --tags=
3509 --branches= --stdlayout --minimize-url
3510 --no-metadata --use-svm-props --use-svnsync-props
3511 --rewrite-root= --prefix= $remote_opts
3513 local cmt_opts="
3514 --edit --rmdir --find-copies-harder --copy-similarity=
3517 case "$subcommand,$cur" in
3518 fetch,--*)
3519 __gitcomp "--revision= --fetch-all $fc_opts"
3521 clone,--*)
3522 __gitcomp "--revision= $fc_opts $init_opts"
3524 init,--*)
3525 __gitcomp "$init_opts"
3527 dcommit,--*)
3528 __gitcomp "
3529 --merge --strategy= --verbose --dry-run
3530 --fetch-all --no-rebase --commit-url
3531 --revision --interactive $cmt_opts $fc_opts
3534 set-tree,--*)
3535 __gitcomp "--stdin $cmt_opts $fc_opts"
3537 create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
3538 show-externals,--*|mkdirs,--*)
3539 __gitcomp "--revision="
3541 log,--*)
3542 __gitcomp "
3543 --limit= --revision= --verbose --incremental
3544 --oneline --show-commit --non-recursive
3545 --authors-file= --color
3548 rebase,--*)
3549 __gitcomp "
3550 --merge --verbose --strategy= --local
3551 --fetch-all --dry-run $fc_opts
3554 commit-diff,--*)
3555 __gitcomp "--message= --file= --revision= $cmt_opts"
3557 info,--*)
3558 __gitcomp "--url"
3560 branch,--*)
3561 __gitcomp "--dry-run --message --tag"
3563 tag,--*)
3564 __gitcomp "--dry-run --message"
3566 blame,--*)
3567 __gitcomp "--git-format"
3569 migrate,--*)
3570 __gitcomp "
3571 --config-dir= --ignore-paths= --minimize
3572 --no-auth-cache --username=
3575 reset,--*)
3576 __gitcomp "--revision= --parent"
3580 esac
3584 _git_tag ()
3586 local i c="$__git_cmd_idx" f=0
3587 while [ $c -lt $cword ]; do
3588 i="${words[c]}"
3589 case "$i" in
3590 -d|--delete|-v|--verify)
3591 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3592 return
3597 esac
3598 ((c++))
3599 done
3601 case "$prev" in
3602 -m|-F)
3604 -*|tag)
3605 if [ $f = 1 ]; then
3606 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3610 __git_complete_refs
3612 esac
3614 case "$cur" in
3615 --*)
3616 __gitcomp_builtin tag
3618 esac
3621 _git_whatchanged ()
3623 _git_log
3626 __git_complete_worktree_paths ()
3628 local IFS=$'\n'
3629 # Generate completion reply from worktree list skipping the first
3630 # entry: it's the path of the main worktree, which can't be moved,
3631 # removed, locked, etc.
3632 __gitcomp_nl "$(__git worktree list --porcelain |
3633 sed -n -e '2,$ s/^worktree //p')"
3636 _git_worktree ()
3638 local subcommands="add list lock move prune remove unlock"
3639 local subcommand subcommand_idx
3641 subcommand="$(__git_find_on_cmdline --show-idx "$subcommands")"
3642 subcommand_idx="${subcommand% *}"
3643 subcommand="${subcommand#* }"
3645 case "$subcommand,$cur" in
3647 __gitcomp "$subcommands"
3649 *,--*)
3650 __gitcomp_builtin worktree_$subcommand
3652 add,*) # usage: git worktree add [<options>] <path> [<commit-ish>]
3653 # Here we are not completing an --option, it's either the
3654 # path or a ref.
3655 case "$prev" in
3656 -b|-B) # Complete refs for branch to be created/reseted.
3657 __git_complete_refs
3659 -*) # The previous word is an -o|--option without an
3660 # unstuck argument: have to complete the path for
3661 # the new worktree, so don't list anything, but let
3662 # Bash fall back to filename completion.
3664 *) # The previous word is not an --option, so it must
3665 # be either the 'add' subcommand, the unstuck
3666 # argument of an option (e.g. branch for -b|-B), or
3667 # the path for the new worktree.
3668 if [ $cword -eq $((subcommand_idx+1)) ]; then
3669 # Right after the 'add' subcommand: have to
3670 # complete the path, so fall back to Bash
3671 # filename completion.
3673 else
3674 case "${words[cword-2]}" in
3675 -b|-B) # After '-b <branch>': have to
3676 # complete the path, so fall back
3677 # to Bash filename completion.
3679 *) # After the path: have to complete
3680 # the ref to be checked out.
3681 __git_complete_refs
3683 esac
3686 esac
3688 lock,*|remove,*|unlock,*)
3689 __git_complete_worktree_paths
3691 move,*)
3692 if [ $cword -eq $((subcommand_idx+1)) ]; then
3693 # The first parameter must be an existing working
3694 # tree to be moved.
3695 __git_complete_worktree_paths
3696 else
3697 # The second parameter is the destination: it could
3698 # be any path, so don't list anything, but let Bash
3699 # fall back to filename completion.
3703 esac
3706 __git_complete_common () {
3707 local command="$1"
3709 case "$cur" in
3710 --*)
3711 __gitcomp_builtin "$command"
3713 esac
3716 __git_cmds_with_parseopt_helper=
3717 __git_support_parseopt_helper () {
3718 test -n "$__git_cmds_with_parseopt_helper" ||
3719 __git_cmds_with_parseopt_helper="$(__git --list-cmds=parseopt)"
3721 case " $__git_cmds_with_parseopt_helper " in
3722 *" $1 "*)
3723 return 0
3726 return 1
3728 esac
3731 __git_have_func () {
3732 declare -f -- "$1" >/dev/null 2>&1
3735 __git_complete_command () {
3736 local command="$1"
3737 local completion_func="_git_${command//-/_}"
3738 if ! __git_have_func $completion_func &&
3739 __git_have_func _completion_loader
3740 then
3741 _completion_loader "git-$command"
3743 if __git_have_func $completion_func
3744 then
3745 $completion_func
3746 return 0
3747 elif __git_support_parseopt_helper "$command"
3748 then
3749 __git_complete_common "$command"
3750 return 0
3751 else
3752 return 1
3756 __git_main ()
3758 local i c=1 command __git_dir __git_repo_path
3759 local __git_C_args C_args_count=0
3760 local __git_cmd_idx
3762 while [ $c -lt $cword ]; do
3763 i="${words[c]}"
3764 case "$i" in
3765 --git-dir=*)
3766 __git_dir="${i#--git-dir=}"
3768 --git-dir)
3769 ((c++))
3770 __git_dir="${words[c]}"
3772 --bare)
3773 __git_dir="."
3775 --help)
3776 command="help"
3777 break
3779 -c|--work-tree|--namespace)
3780 ((c++))
3783 __git_C_args[C_args_count++]=-C
3784 ((c++))
3785 __git_C_args[C_args_count++]="${words[c]}"
3790 command="$i"
3791 __git_cmd_idx="$c"
3792 break
3794 esac
3795 ((c++))
3796 done
3798 if [ -z "${command-}" ]; then
3799 case "$prev" in
3800 --git-dir|-C|--work-tree)
3801 # these need a path argument, let's fall back to
3802 # Bash filename completion
3803 return
3806 __git_complete_config_variable_name_and_value
3807 return
3809 --namespace)
3810 # we don't support completing these options' arguments
3811 return
3813 esac
3814 case "$cur" in
3815 --*)
3816 __gitcomp "
3817 --paginate
3818 --no-pager
3819 --git-dir=
3820 --bare
3821 --version
3822 --exec-path
3823 --exec-path=
3824 --html-path
3825 --man-path
3826 --info-path
3827 --work-tree=
3828 --namespace=
3829 --no-replace-objects
3830 --help
3834 if test -n "${GIT_TESTING_PORCELAIN_COMMAND_LIST-}"
3835 then
3836 __gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
3837 else
3838 local list_cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config
3840 if test "${GIT_COMPLETION_SHOW_ALL_COMMANDS-}" = "1"
3841 then
3842 list_cmds=builtins,$list_cmds
3844 __gitcomp "$(__git --list-cmds=$list_cmds)"
3847 esac
3848 return
3851 __git_complete_command "$command" && return
3853 local expansion=$(__git_aliased_command "$command")
3854 if [ -n "$expansion" ]; then
3855 words[1]=$expansion
3856 __git_complete_command "$expansion"
3860 __gitk_main ()
3862 __git_has_doubledash && return
3864 local __git_repo_path
3865 __git_find_repo_path
3867 local merge=""
3868 if __git_pseudoref_exists MERGE_HEAD; then
3869 merge="--merge"
3871 case "$cur" in
3872 --*)
3873 __gitcomp "
3874 $__git_log_common_options
3875 $__git_log_gitk_options
3876 $merge
3878 return
3880 esac
3881 __git_complete_revlist
3884 if [[ -n ${ZSH_VERSION-} && -z ${GIT_SOURCING_ZSH_COMPLETION-} ]]; then
3885 echo "ERROR: this script is obsolete, please see git-completion.zsh" 1>&2
3886 return
3889 __git_func_wrap ()
3891 local cur words cword prev
3892 local __git_cmd_idx=0
3893 _get_comp_words_by_ref -n =: cur words cword prev
3897 ___git_complete ()
3899 local wrapper="__git_wrap${2}"
3900 eval "$wrapper () { __git_func_wrap $2 ; }"
3901 complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
3902 || complete -o default -o nospace -F $wrapper $1
3905 # Setup the completion for git commands
3906 # 1: command or alias
3907 # 2: function to call (e.g. `git`, `gitk`, `git_fetch`)
3908 __git_complete ()
3910 local func
3912 if __git_have_func $2; then
3913 func=$2
3914 elif __git_have_func __$2_main; then
3915 func=__$2_main
3916 elif __git_have_func _$2; then
3917 func=_$2
3918 else
3919 echo "ERROR: could not find function '$2'" 1>&2
3920 return 1
3922 ___git_complete $1 $func
3925 ___git_complete git __git_main
3926 ___git_complete gitk __gitk_main
3928 # The following are necessary only for Cygwin, and only are needed
3929 # when the user has tab-completed the executable name and consequently
3930 # included the '.exe' suffix.
3932 if [ "$OSTYPE" = cygwin ]; then
3933 ___git_complete git.exe __git_main