The third batch
[git.git] / contrib / completion / git-completion.bash
blob5c0ddeb3d4fb60fc6b8e5d08b4b0e779544ff15d
1 # bash/zsh completion support for core Git.
3 # Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
4 # Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
5 # Distributed under the GNU General Public License, version 2.0.
7 # The contained completion routines provide support for completing:
9 # *) local and remote branch names
10 # *) local and remote tag names
11 # *) .git/remotes file names
12 # *) git 'subcommands'
13 # *) git email aliases for git-send-email
14 # *) tree paths within 'ref:path/to/file' expressions
15 # *) file paths within current working directory and index
16 # *) common --long-options
18 # To use these routines:
20 # 1) Copy this file to somewhere (e.g. ~/.git-completion.bash).
21 # 2) Add the following line to your .bashrc/.zshrc:
22 # source ~/.git-completion.bash
23 # 3) Consider changing your PS1 to also show the current branch,
24 # see git-prompt.sh for details.
26 # If you use complex aliases of form '!f() { ... }; f', you can use the null
27 # command ':' as the first command in the function body to declare the desired
28 # completion style. For example '!f() { : git commit ; ... }; f' will
29 # tell the completion to use commit completion. This also works with aliases
30 # of form "!sh -c '...'". For example, "!sh -c ': git commit ; ... '".
31 # Note that "git" is optional --- '!f() { : commit; ...}; f' would complete
32 # just like the 'git commit' command.
34 # To add completion for git subcommands that are implemented in external
35 # scripts, define a function of the form '_git_${subcommand}' while replacing
36 # all dashes with underscores, and the main git completion will make use of it.
37 # For example, to add completion for 'git do-stuff' (which could e.g. live
38 # in /usr/bin/git-do-stuff), name the completion function '_git_do_stuff'.
39 # See _git_show, _git_bisect etc. below for more examples.
41 # If you have a shell command that is not part of git (and is not called as a
42 # git subcommand), but you would still like git-style completion for it, use
43 # __git_complete. For example, to use the same completion as for 'git log' also
44 # for the 'gl' command:
46 # __git_complete gl git_log
48 # Or if the 'gk' command should be completed the same as 'gitk':
50 # __git_complete gk gitk
52 # The second parameter of __git_complete gives the completion function; it is
53 # resolved as a function named "$2", or "__$2_main", or "_$2" in that order.
54 # In the examples above, the actual functions used for completion will be
55 # _git_log and __gitk_main.
57 # Compatible with bash 3.2.57.
59 # You can set the following environment variables to influence the behavior of
60 # the completion routines:
62 # GIT_COMPLETION_CHECKOUT_NO_GUESS
64 # When set to "1", do not include "DWIM" suggestions in git-checkout
65 # and git-switch completion (e.g., completing "foo" when "origin/foo"
66 # exists).
68 # GIT_COMPLETION_SHOW_ALL_COMMANDS
70 # When set to "1" suggest all commands, including plumbing commands
71 # which are hidden by default (e.g. "cat-file" on "git ca<TAB>").
73 # GIT_COMPLETION_SHOW_ALL
75 # When set to "1" suggest all options, including options which are
76 # typically hidden (e.g. '--allow-empty' for 'git commit').
78 # GIT_COMPLETION_IGNORE_CASE
80 # When set, uses for-each-ref '--ignore-case' to find refs that match
81 # case insensitively, even on systems with case sensitive file systems
82 # (e.g., completing tag name "FOO" on "git checkout f<TAB>").
84 case "$COMP_WORDBREAKS" in
85 *:*) : great ;;
86 *) COMP_WORDBREAKS="$COMP_WORDBREAKS:"
87 esac
89 # Discovers the path to the git repository taking any '--git-dir=<path>' and
90 # '-C <path>' options into account and stores it in the $__git_repo_path
91 # variable.
92 __git_find_repo_path ()
94 if [ -n "${__git_repo_path-}" ]; then
95 # we already know where it is
96 return
99 if [ -n "${__git_C_args-}" ]; then
100 __git_repo_path="$(git "${__git_C_args[@]}" \
101 ${__git_dir:+--git-dir="$__git_dir"} \
102 rev-parse --absolute-git-dir 2>/dev/null)"
103 elif [ -n "${__git_dir-}" ]; then
104 test -d "$__git_dir" &&
105 __git_repo_path="$__git_dir"
106 elif [ -n "${GIT_DIR-}" ]; then
107 test -d "$GIT_DIR" &&
108 __git_repo_path="$GIT_DIR"
109 elif [ -d .git ]; then
110 __git_repo_path=.git
111 else
112 __git_repo_path="$(git rev-parse --git-dir 2>/dev/null)"
116 # Deprecated: use __git_find_repo_path() and $__git_repo_path instead
117 # __gitdir accepts 0 or 1 arguments (i.e., location)
118 # returns location of .git repo
119 __gitdir ()
121 if [ -z "${1-}" ]; then
122 __git_find_repo_path || return 1
123 echo "$__git_repo_path"
124 elif [ -d "$1/.git" ]; then
125 echo "$1/.git"
126 else
127 echo "$1"
131 # Runs git with all the options given as argument, respecting any
132 # '--git-dir=<path>' and '-C <path>' options present on the command line
133 __git ()
135 git ${__git_C_args:+"${__git_C_args[@]}"} \
136 ${__git_dir:+--git-dir="$__git_dir"} "$@" 2>/dev/null
139 # Helper function to read the first line of a file into a variable.
140 # __git_eread requires 2 arguments, the file path and the name of the
141 # variable, in that order.
143 # This is taken from git-prompt.sh.
144 __git_eread ()
146 test -r "$1" && IFS=$'\r\n' read -r "$2" <"$1"
149 # Runs git in $__git_repo_path to determine whether a pseudoref exists.
150 # 1: The pseudo-ref to search
151 __git_pseudoref_exists ()
153 local ref=$1
154 local head
156 __git_find_repo_path
158 # If the reftable is in use, we have to shell out to 'git rev-parse'
159 # to determine whether the ref exists instead of looking directly in
160 # the filesystem to determine whether the ref exists. Otherwise, use
161 # Bash builtins since executing Git commands are expensive on some
162 # platforms.
163 if __git_eread "$__git_repo_path/HEAD" head; then
164 if [ "$head" == "ref: refs/heads/.invalid" ]; then
165 __git show-ref --exists "$ref"
166 return $?
170 [ -f "$__git_repo_path/$ref" ]
173 # Removes backslash escaping, single quotes and double quotes from a word,
174 # stores the result in the variable $dequoted_word.
175 # 1: The word to dequote.
176 __git_dequote ()
178 local rest="$1" len ch
180 dequoted_word=""
182 while test -n "$rest"; do
183 len=${#dequoted_word}
184 dequoted_word="$dequoted_word${rest%%[\\\'\"]*}"
185 rest="${rest:$((${#dequoted_word}-$len))}"
187 case "${rest:0:1}" in
189 ch="${rest:1:1}"
190 case "$ch" in
191 $'\n')
194 dequoted_word="$dequoted_word$ch"
196 esac
197 rest="${rest:2}"
200 rest="${rest:1}"
201 len=${#dequoted_word}
202 dequoted_word="$dequoted_word${rest%%\'*}"
203 rest="${rest:$((${#dequoted_word}-$len+1))}"
206 rest="${rest:1}"
207 while test -n "$rest" ; do
208 len=${#dequoted_word}
209 dequoted_word="$dequoted_word${rest%%[\\\"]*}"
210 rest="${rest:$((${#dequoted_word}-$len))}"
211 case "${rest:0:1}" in
213 ch="${rest:1:1}"
214 case "$ch" in
215 \"|\\|\$|\`)
216 dequoted_word="$dequoted_word$ch"
218 $'\n')
221 dequoted_word="$dequoted_word\\$ch"
223 esac
224 rest="${rest:2}"
227 rest="${rest:1}"
228 break
230 esac
231 done
233 esac
234 done
237 # The following function is based on code from:
239 # bash_completion - programmable completion functions for bash 3.2+
241 # Copyright © 2006-2008, Ian Macdonald <ian@caliban.org>
242 # © 2009-2010, Bash Completion Maintainers
243 # <bash-completion-devel@lists.alioth.debian.org>
245 # This program is free software; you can redistribute it and/or modify
246 # it under the terms of the GNU General Public License as published by
247 # the Free Software Foundation; either version 2, or (at your option)
248 # any later version.
250 # This program is distributed in the hope that it will be useful,
251 # but WITHOUT ANY WARRANTY; without even the implied warranty of
252 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
253 # GNU General Public License for more details.
255 # You should have received a copy of the GNU General Public License
256 # along with this program; if not, see <http://www.gnu.org/licenses/>.
258 # The latest version of this software can be obtained here:
260 # http://bash-completion.alioth.debian.org/
262 # RELEASE: 2.x
264 # This function can be used to access a tokenized list of words
265 # on the command line:
267 # __git_reassemble_comp_words_by_ref '=:'
268 # if test "${words_[cword_-1]}" = -w
269 # then
270 # ...
271 # fi
273 # The argument should be a collection of characters from the list of
274 # word completion separators (COMP_WORDBREAKS) to treat as ordinary
275 # characters.
277 # This is roughly equivalent to going back in time and setting
278 # COMP_WORDBREAKS to exclude those characters. The intent is to
279 # make option types like --date=<type> and <rev>:<path> easy to
280 # recognize by treating each shell word as a single token.
282 # It is best not to set COMP_WORDBREAKS directly because the value is
283 # shared with other completion scripts. By the time the completion
284 # function gets called, COMP_WORDS has already been populated so local
285 # changes to COMP_WORDBREAKS have no effect.
287 # Output: words_, cword_, cur_.
289 __git_reassemble_comp_words_by_ref()
291 local exclude i j first
292 # Which word separators to exclude?
293 exclude="${1//[^$COMP_WORDBREAKS]}"
294 cword_=$COMP_CWORD
295 if [ -z "$exclude" ]; then
296 words_=("${COMP_WORDS[@]}")
297 return
299 # List of word completion separators has shrunk;
300 # re-assemble words to complete.
301 for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
302 # Append each nonempty word consisting of just
303 # word separator characters to the current word.
304 first=t
305 while
306 [ $i -gt 0 ] &&
307 [ -n "${COMP_WORDS[$i]}" ] &&
308 # word consists of excluded word separators
309 [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
311 # Attach to the previous token,
312 # unless the previous token is the command name.
313 if [ $j -ge 2 ] && [ -n "$first" ]; then
314 ((j--))
316 first=
317 words_[$j]=${words_[j]}${COMP_WORDS[i]}
318 if [ $i = $COMP_CWORD ]; then
319 cword_=$j
321 if (($i < ${#COMP_WORDS[@]} - 1)); then
322 ((i++))
323 else
324 # Done.
325 return
327 done
328 words_[$j]=${words_[j]}${COMP_WORDS[i]}
329 if [ $i = $COMP_CWORD ]; then
330 cword_=$j
332 done
335 if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
336 _get_comp_words_by_ref ()
338 local exclude cur_ words_ cword_
339 if [ "$1" = "-n" ]; then
340 exclude=$2
341 shift 2
343 __git_reassemble_comp_words_by_ref "$exclude"
344 cur_=${words_[cword_]}
345 while [ $# -gt 0 ]; do
346 case "$1" in
347 cur)
348 cur=$cur_
350 prev)
351 prev=${words_[$cword_-1]}
353 words)
354 words=("${words_[@]}")
356 cword)
357 cword=$cword_
359 esac
360 shift
361 done
365 # Fills the COMPREPLY array with prefiltered words without any additional
366 # processing.
367 # Callers must take care of providing only words that match the current word
368 # to be completed and adding any prefix and/or suffix (trailing space!), if
369 # necessary.
370 # 1: List of newline-separated matching completion words, complete with
371 # prefix and suffix.
372 __gitcomp_direct ()
374 local IFS=$'\n'
376 COMPREPLY=($1)
379 # Similar to __gitcomp_direct, but appends to COMPREPLY instead.
380 # Callers must take care of providing only words that match the current word
381 # to be completed and adding any prefix and/or suffix (trailing space!), if
382 # necessary.
383 # 1: List of newline-separated matching completion words, complete with
384 # prefix and suffix.
385 __gitcomp_direct_append ()
387 local IFS=$'\n'
389 COMPREPLY+=($1)
392 __gitcompappend ()
394 local x i=${#COMPREPLY[@]}
395 for x in $1; do
396 if [[ "$x" == "$3"* ]]; then
397 COMPREPLY[i++]="$2$x$4"
399 done
402 __gitcompadd ()
404 COMPREPLY=()
405 __gitcompappend "$@"
408 # Generates completion reply, appending a space to possible completion words,
409 # if necessary.
410 # It accepts 1 to 4 arguments:
411 # 1: List of possible completion words.
412 # 2: A prefix to be added to each possible completion word (optional).
413 # 3: Generate possible completion matches for this word (optional).
414 # 4: A suffix to be appended to each possible completion word (optional).
415 __gitcomp ()
417 local cur_="${3-$cur}"
419 case "$cur_" in
422 --no-*)
423 local c i=0 IFS=$' \t\n'
424 for c in $1; do
425 if [[ $c == "--" ]]; then
426 continue
428 c="$c${4-}"
429 if [[ $c == "$cur_"* ]]; then
430 case $c in
431 --*=|*.) ;;
432 *) c="$c " ;;
433 esac
434 COMPREPLY[i++]="${2-}$c"
436 done
439 local c i=0 IFS=$' \t\n'
440 for c in $1; do
441 if [[ $c == "--" ]]; then
442 c="--no-...${4-}"
443 if [[ $c == "$cur_"* ]]; then
444 COMPREPLY[i++]="${2-}$c "
446 break
448 c="$c${4-}"
449 if [[ $c == "$cur_"* ]]; then
450 case $c in
451 *=|*.) ;;
452 *) c="$c " ;;
453 esac
454 COMPREPLY[i++]="${2-}$c"
456 done
458 esac
461 # Clear the variables caching builtins' options when (re-)sourcing
462 # the completion script.
463 if [[ -n ${ZSH_VERSION-} ]]; then
464 unset ${(M)${(k)parameters[@]}:#__gitcomp_builtin_*} 2>/dev/null
465 else
466 unset $(compgen -v __gitcomp_builtin_)
469 # This function is equivalent to
471 # ___git_resolved_builtins=$(git xxx --git-completion-helper)
473 # except that the result of the execution is cached.
475 # Accept 1-3 arguments:
476 # 1: the git command to execute, this is also the cache key
477 # (use "_" when the command contains spaces, e.g. "remote add"
478 # becomes "remote_add")
479 # 2: extra options to be added on top (e.g. negative forms)
480 # 3: options to be excluded
481 __git_resolve_builtins ()
483 local cmd="$1"
484 local incl="${2-}"
485 local excl="${3-}"
487 local var=__gitcomp_builtin_"${cmd//-/_}"
488 local options
489 eval "options=\${$var-}"
491 if [ -z "$options" ]; then
492 local completion_helper
493 if [ "${GIT_COMPLETION_SHOW_ALL-}" = "1" ]; then
494 completion_helper="--git-completion-helper-all"
495 else
496 completion_helper="--git-completion-helper"
498 # leading and trailing spaces are significant to make
499 # option removal work correctly.
500 options=" $incl $(__git ${cmd/_/ } $completion_helper) " || return
502 for i in $excl; do
503 options="${options/ $i / }"
504 done
505 eval "$var=\"$options\""
508 ___git_resolved_builtins="$options"
511 # This function is equivalent to
513 # __gitcomp "$(git xxx --git-completion-helper) ..."
515 # except that the output is cached. Accept 1-3 arguments:
516 # 1: the git command to execute, this is also the cache key
517 # (use "_" when the command contains spaces, e.g. "remote add"
518 # becomes "remote_add")
519 # 2: extra options to be added on top (e.g. negative forms)
520 # 3: options to be excluded
521 __gitcomp_builtin ()
523 __git_resolve_builtins "$1" "$2" "$3"
525 __gitcomp "$___git_resolved_builtins"
528 # Variation of __gitcomp_nl () that appends to the existing list of
529 # completion candidates, COMPREPLY.
530 __gitcomp_nl_append ()
532 local IFS=$'\n'
533 __gitcompappend "$1" "${2-}" "${3-$cur}" "${4- }"
536 # Generates completion reply from newline-separated possible completion words
537 # by appending a space to all of them.
538 # It accepts 1 to 4 arguments:
539 # 1: List of possible completion words, separated by a single newline.
540 # 2: A prefix to be added to each possible completion word (optional).
541 # 3: Generate possible completion matches for this word (optional).
542 # 4: A suffix to be appended to each possible completion word instead of
543 # the default space (optional). If specified but empty, nothing is
544 # appended.
545 __gitcomp_nl ()
547 COMPREPLY=()
548 __gitcomp_nl_append "$@"
551 # Fills the COMPREPLY array with prefiltered paths without any additional
552 # processing.
553 # Callers must take care of providing only paths that match the current path
554 # to be completed and adding any prefix path components, if necessary.
555 # 1: List of newline-separated matching paths, complete with all prefix
556 # path components.
557 __gitcomp_file_direct ()
559 local IFS=$'\n'
561 COMPREPLY=($1)
563 # use a hack to enable file mode in bash < 4
564 compopt -o filenames +o nospace 2>/dev/null ||
565 compgen -f /non-existing-dir/ >/dev/null ||
566 true
569 # Generates completion reply with compgen from newline-separated possible
570 # completion filenames.
571 # It accepts 1 to 3 arguments:
572 # 1: List of possible completion filenames, separated by a single newline.
573 # 2: A directory prefix to be added to each possible completion filename
574 # (optional).
575 # 3: Generate possible completion matches for this word (optional).
576 __gitcomp_file ()
578 local IFS=$'\n'
580 # XXX does not work when the directory prefix contains a tilde,
581 # since tilde expansion is not applied.
582 # This means that COMPREPLY will be empty and Bash default
583 # completion will be used.
584 __gitcompadd "$1" "${2-}" "${3-$cur}" ""
586 # use a hack to enable file mode in bash < 4
587 compopt -o filenames +o nospace 2>/dev/null ||
588 compgen -f /non-existing-dir/ >/dev/null ||
589 true
592 # Find the current subcommand for commands that follow the syntax:
594 # git <command> <subcommand>
596 # 1: List of possible subcommands.
597 # 2: Optional subcommand to return when none is found.
598 __git_find_subcommand ()
600 local subcommand subcommands="$1" default_subcommand="$2"
602 for subcommand in $subcommands; do
603 if [ "$subcommand" = "${words[__git_cmd_idx+1]}" ]; then
604 echo $subcommand
605 return
607 done
609 echo $default_subcommand
612 # Execute 'git ls-files', unless the --committable option is specified, in
613 # which case it runs 'git diff-index' to find out the files that can be
614 # committed. It return paths relative to the directory specified in the first
615 # argument, and using the options specified in the second argument.
616 __git_ls_files_helper ()
618 if [ "$2" = "--committable" ]; then
619 __git -C "$1" -c core.quotePath=false diff-index \
620 --name-only --relative HEAD -- "${3//\\/\\\\}*"
621 else
622 # NOTE: $2 is not quoted in order to support multiple options
623 __git -C "$1" -c core.quotePath=false ls-files \
624 --exclude-standard $2 -- "${3//\\/\\\\}*"
629 # __git_index_files accepts 1 or 2 arguments:
630 # 1: Options to pass to ls-files (required).
631 # 2: A directory path (optional).
632 # If provided, only files within the specified directory are listed.
633 # Sub directories are never recursed. Path must have a trailing
634 # slash.
635 # 3: List only paths matching this path component (optional).
636 __git_index_files ()
638 local root="$2" match="$3"
640 __git_ls_files_helper "$root" "$1" "${match:-?}" |
641 awk -F / -v pfx="${2//\\/\\\\}" '{
642 paths[$1] = 1
644 END {
645 for (p in paths) {
646 if (substr(p, 1, 1) != "\"") {
647 # No special characters, easy!
648 print pfx p
649 continue
652 # The path is quoted.
653 p = dequote(p)
654 if (p == "")
655 continue
657 # Even when a directory name itself does not contain
658 # any special characters, it will still be quoted if
659 # any of its (stripped) trailing path components do.
660 # Because of this we may have seen the same directory
661 # both quoted and unquoted.
662 if (p in paths)
663 # We have seen the same directory unquoted,
664 # skip it.
665 continue
666 else
667 print pfx p
670 function dequote(p, bs_idx, out, esc, esc_idx, dec) {
671 # Skip opening double quote.
672 p = substr(p, 2)
674 # Interpret backslash escape sequences.
675 while ((bs_idx = index(p, "\\")) != 0) {
676 out = out substr(p, 1, bs_idx - 1)
677 esc = substr(p, bs_idx + 1, 1)
678 p = substr(p, bs_idx + 2)
680 if ((esc_idx = index("abtvfr\"\\", esc)) != 0) {
681 # C-style one-character escape sequence.
682 out = out substr("\a\b\t\v\f\r\"\\",
683 esc_idx, 1)
684 } else if (esc == "n") {
685 # Uh-oh, a newline character.
686 # We cannot reliably put a pathname
687 # containing a newline into COMPREPLY,
688 # and the newline would create a mess.
689 # Skip this path.
690 return ""
691 } else {
692 # Must be a \nnn octal value, then.
693 dec = esc * 64 + \
694 substr(p, 1, 1) * 8 + \
695 substr(p, 2, 1)
696 out = out sprintf("%c", dec)
697 p = substr(p, 3)
700 # Drop closing double quote, if there is one.
701 # (There is not any if this is a directory, as it was
702 # already stripped with the trailing path components.)
703 if (substr(p, length(p), 1) == "\"")
704 out = out substr(p, 1, length(p) - 1)
705 else
706 out = out p
708 return out
712 # __git_complete_index_file requires 1 argument:
713 # 1: the options to pass to ls-file
715 # The exception is --committable, which finds the files appropriate commit.
716 __git_complete_index_file ()
718 local dequoted_word pfx="" cur_
720 __git_dequote "$cur"
722 case "$dequoted_word" in
723 ?*/*)
724 pfx="${dequoted_word%/*}/"
725 cur_="${dequoted_word##*/}"
728 cur_="$dequoted_word"
729 esac
731 __gitcomp_file_direct "$(__git_index_files "$1" "$pfx" "$cur_")"
734 # Lists branches from the local repository.
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_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/heads/$cur_*" "refs/heads/$cur_*/**"
748 # Lists branches from remote repositories.
749 # 1: A prefix to be added to each listed branch (optional).
750 # 2: List only branches matching this word (optional; list all branches if
751 # unset or empty).
752 # 3: A suffix to be appended to each listed branch (optional).
753 __git_remote_heads ()
755 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
757 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
758 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
759 "refs/remotes/$cur_*" "refs/remotes/$cur_*/**"
762 # Lists tags from the local repository.
763 # Accepts the same positional parameters as __git_heads() above.
764 __git_tags ()
766 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
768 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
769 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
770 "refs/tags/$cur_*" "refs/tags/$cur_*/**"
773 # List unique branches from refs/remotes used for 'git checkout' and 'git
774 # switch' tracking DWIMery.
775 # 1: A prefix to be added to each listed branch (optional)
776 # 2: List only branches matching this word (optional; list all branches if
777 # unset or empty).
778 # 3: A suffix to be appended to each listed branch (optional).
779 __git_dwim_remote_heads ()
781 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
782 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
784 # employ the heuristic used by git checkout and git switch
785 # Try to find a remote branch that cur_es the completion word
786 # but only output if the branch name is unique
787 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
788 --sort="refname:strip=3" \
789 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
790 "refs/remotes/*/$cur_*" "refs/remotes/*/$cur_*/**" | \
791 uniq -u
794 # Lists refs from the local (by default) or from a remote repository.
795 # It accepts 0, 1 or 2 arguments:
796 # 1: The remote to list refs from (optional; ignored, if set but empty).
797 # Can be the name of a configured remote, a path, or a URL.
798 # 2: In addition to local refs, list unique branches from refs/remotes/ for
799 # 'git checkout's tracking DWIMery (optional; ignored, if set but empty).
800 # 3: A prefix to be added to each listed ref (optional).
801 # 4: List only refs matching this word (optional; list all refs if unset or
802 # empty).
803 # 5: A suffix to be appended to each listed ref (optional; ignored, if set
804 # but empty).
806 # Use __git_complete_refs() instead.
807 __git_refs ()
809 local i hash dir track="${2-}"
810 local list_refs_from=path remote="${1-}"
811 local format refs
812 local pfx="${3-}" cur_="${4-$cur}" sfx="${5-}"
813 local match="${4-}"
814 local umatch="${4-}"
815 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
817 __git_find_repo_path
818 dir="$__git_repo_path"
820 if [ -z "$remote" ]; then
821 if [ -z "$dir" ]; then
822 return
824 else
825 if __git_is_configured_remote "$remote"; then
826 # configured remote takes precedence over a
827 # local directory with the same name
828 list_refs_from=remote
829 elif [ -d "$remote/.git" ]; then
830 dir="$remote/.git"
831 elif [ -d "$remote" ]; then
832 dir="$remote"
833 else
834 list_refs_from=url
838 if test "${GIT_COMPLETION_IGNORE_CASE:+1}" = "1"
839 then
840 # uppercase with tr instead of ${match,^^} for bash 3.2 compatibility
841 umatch=$(echo "$match" | tr a-z A-Z 2>/dev/null || echo "$match")
844 if [ "$list_refs_from" = path ]; then
845 if [[ "$cur_" == ^* ]]; then
846 pfx="$pfx^"
847 fer_pfx="$fer_pfx^"
848 cur_=${cur_#^}
849 match=${match#^}
850 umatch=${umatch#^}
852 case "$cur_" in
853 refs|refs/*)
854 format="refname"
855 refs=("$match*" "$match*/**")
856 track=""
859 for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD REBASE_HEAD CHERRY_PICK_HEAD REVERT_HEAD BISECT_HEAD AUTO_MERGE; do
860 case "$i" in
861 $match*|$umatch*)
862 if [ -e "$dir/$i" ]; then
863 echo "$pfx$i$sfx"
866 esac
867 done
868 format="refname:strip=2"
869 refs=("refs/tags/$match*" "refs/tags/$match*/**"
870 "refs/heads/$match*" "refs/heads/$match*/**"
871 "refs/remotes/$match*" "refs/remotes/$match*/**")
873 esac
874 __git_dir="$dir" __git for-each-ref --format="$fer_pfx%($format)$sfx" \
875 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
876 "${refs[@]}"
877 if [ -n "$track" ]; then
878 __git_dwim_remote_heads "$pfx" "$match" "$sfx"
880 return
882 case "$cur_" in
883 refs|refs/*)
884 __git ls-remote "$remote" "$match*" | \
885 while read -r hash i; do
886 case "$i" in
887 *^{}) ;;
888 *) echo "$pfx$i$sfx" ;;
889 esac
890 done
893 if [ "$list_refs_from" = remote ]; then
894 case "HEAD" in
895 $match*|$umatch*) echo "${pfx}HEAD$sfx" ;;
896 esac
897 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
898 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
899 "refs/remotes/$remote/$match*" \
900 "refs/remotes/$remote/$match*/**"
901 else
902 local query_symref
903 case "HEAD" in
904 $match*|$umatch*) query_symref="HEAD" ;;
905 esac
906 __git ls-remote "$remote" $query_symref \
907 "refs/tags/$match*" "refs/heads/$match*" \
908 "refs/remotes/$match*" |
909 while read -r hash i; do
910 case "$i" in
911 *^{}) ;;
912 refs/*) echo "$pfx${i#refs/*/}$sfx" ;;
913 *) echo "$pfx$i$sfx" ;; # symbolic refs
914 esac
915 done
918 esac
921 # Completes refs, short and long, local and remote, symbolic and pseudo.
923 # Usage: __git_complete_refs [<option>]...
924 # --remote=<remote>: The remote to list refs from, can be the name of a
925 # configured remote, a path, or a URL.
926 # --dwim: List unique remote branches for 'git switch's tracking DWIMery.
927 # --pfx=<prefix>: A prefix to be added to each ref.
928 # --cur=<word>: The current ref to be completed. Defaults to the current
929 # word to be completed.
930 # --sfx=<suffix>: A suffix to be appended to each ref instead of the default
931 # space.
932 # --mode=<mode>: What set of refs to complete, one of 'refs' (the default) to
933 # complete all refs, 'heads' to complete only branches, or
934 # 'remote-heads' to complete only remote branches. Note that
935 # --remote is only compatible with --mode=refs.
936 __git_complete_refs ()
938 local remote= dwim= pfx= cur_="$cur" sfx=" " mode="refs"
940 while test $# != 0; do
941 case "$1" in
942 --remote=*) remote="${1##--remote=}" ;;
943 --dwim) dwim="yes" ;;
944 # --track is an old spelling of --dwim
945 --track) dwim="yes" ;;
946 --pfx=*) pfx="${1##--pfx=}" ;;
947 --cur=*) cur_="${1##--cur=}" ;;
948 --sfx=*) sfx="${1##--sfx=}" ;;
949 --mode=*) mode="${1##--mode=}" ;;
950 *) return 1 ;;
951 esac
952 shift
953 done
955 # complete references based on the specified mode
956 case "$mode" in
957 refs)
958 __gitcomp_direct "$(__git_refs "$remote" "" "$pfx" "$cur_" "$sfx")" ;;
959 heads)
960 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" "$sfx")" ;;
961 remote-heads)
962 __gitcomp_direct "$(__git_remote_heads "$pfx" "$cur_" "$sfx")" ;;
964 return 1 ;;
965 esac
967 # Append DWIM remote branch names if requested
968 if [ "$dwim" = "yes" ]; then
969 __gitcomp_direct_append "$(__git_dwim_remote_heads "$pfx" "$cur_" "$sfx")"
973 # __git_refs2 requires 1 argument (to pass to __git_refs)
974 # Deprecated: use __git_complete_fetch_refspecs() instead.
975 __git_refs2 ()
977 local i
978 for i in $(__git_refs "$1"); do
979 echo "$i:$i"
980 done
983 # Completes refspecs for fetching from a remote repository.
984 # 1: The remote repository.
985 # 2: A prefix to be added to each listed refspec (optional).
986 # 3: The ref to be completed as a refspec instead of the current word to be
987 # completed (optional)
988 # 4: A suffix to be appended to each listed refspec instead of the default
989 # space (optional).
990 __git_complete_fetch_refspecs ()
992 local i remote="$1" pfx="${2-}" cur_="${3-$cur}" sfx="${4- }"
994 __gitcomp_direct "$(
995 for i in $(__git_refs "$remote" "" "" "$cur_") ; do
996 echo "$pfx$i:$i$sfx"
997 done
1001 # __git_refs_remotes requires 1 argument (to pass to ls-remote)
1002 __git_refs_remotes ()
1004 local i hash
1005 __git ls-remote "$1" 'refs/heads/*' | \
1006 while read -r hash i; do
1007 echo "$i:refs/remotes/$1/${i#refs/heads/}"
1008 done
1011 __git_remotes ()
1013 __git_find_repo_path
1014 test -d "$__git_repo_path/remotes" && ls -1 "$__git_repo_path/remotes"
1015 __git remote
1018 # Returns true if $1 matches the name of a configured remote, false otherwise.
1019 __git_is_configured_remote ()
1021 local remote
1022 for remote in $(__git_remotes); do
1023 if [ "$remote" = "$1" ]; then
1024 return 0
1026 done
1027 return 1
1030 __git_list_merge_strategies ()
1032 LANG=C LC_ALL=C git merge -s help 2>&1 |
1033 sed -n -e '/[Aa]vailable strategies are: /,/^$/{
1034 s/\.$//
1035 s/.*://
1036 s/^[ ]*//
1037 s/[ ]*$//
1042 __git_merge_strategies=
1043 # 'git merge -s help' (and thus detection of the merge strategy
1044 # list) fails, unfortunately, if run outside of any git working
1045 # tree. __git_merge_strategies is set to the empty string in
1046 # that case, and the detection will be repeated the next time it
1047 # is needed.
1048 __git_compute_merge_strategies ()
1050 test -n "$__git_merge_strategies" ||
1051 __git_merge_strategies=$(__git_list_merge_strategies)
1054 __git_merge_strategy_options="ours theirs subtree subtree= patience
1055 histogram diff-algorithm= ignore-space-change ignore-all-space
1056 ignore-space-at-eol renormalize no-renormalize no-renames
1057 find-renames find-renames= rename-threshold="
1059 __git_complete_revlist_file ()
1061 local dequoted_word pfx ls ref cur_="$cur"
1062 case "$cur_" in
1063 *..?*:*)
1064 return
1066 ?*:*)
1067 ref="${cur_%%:*}"
1068 cur_="${cur_#*:}"
1070 __git_dequote "$cur_"
1072 case "$dequoted_word" in
1073 ?*/*)
1074 pfx="${dequoted_word%/*}"
1075 cur_="${dequoted_word##*/}"
1076 ls="$ref:$pfx"
1077 pfx="$pfx/"
1080 cur_="$dequoted_word"
1081 ls="$ref"
1083 esac
1085 case "$COMP_WORDBREAKS" in
1086 *:*) : great ;;
1087 *) pfx="$ref:$pfx" ;;
1088 esac
1090 __gitcomp_file "$(__git ls-tree "$ls" \
1091 | sed 's/^.* //
1092 s/$//')" \
1093 "$pfx" "$cur_"
1095 *...*)
1096 pfx="${cur_%...*}..."
1097 cur_="${cur_#*...}"
1098 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1100 *..*)
1101 pfx="${cur_%..*}.."
1102 cur_="${cur_#*..}"
1103 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1106 __git_complete_refs
1108 esac
1111 __git_complete_file ()
1113 __git_complete_revlist_file
1116 __git_complete_revlist ()
1118 __git_complete_revlist_file
1121 __git_complete_remote_or_refspec ()
1123 local cur_="$cur" cmd="${words[__git_cmd_idx]}"
1124 local i c=$((__git_cmd_idx+1)) remote="" pfx="" lhs=1 no_complete_refspec=0
1125 if [ "$cmd" = "remote" ]; then
1126 ((c++))
1128 while [ $c -lt $cword ]; do
1129 i="${words[c]}"
1130 case "$i" in
1131 --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
1132 -d|--delete) [ "$cmd" = "push" ] && lhs=0 ;;
1133 --all)
1134 case "$cmd" in
1135 push) no_complete_refspec=1 ;;
1136 fetch)
1137 return
1139 *) ;;
1140 esac
1142 --multiple) no_complete_refspec=1; break ;;
1143 -*) ;;
1144 *) remote="$i"; break ;;
1145 esac
1146 ((c++))
1147 done
1148 if [ -z "$remote" ]; then
1149 __gitcomp_nl "$(__git_remotes)"
1150 return
1152 if [ $no_complete_refspec = 1 ]; then
1153 return
1155 [ "$remote" = "." ] && remote=
1156 case "$cur_" in
1157 *:*)
1158 case "$COMP_WORDBREAKS" in
1159 *:*) : great ;;
1160 *) pfx="${cur_%%:*}:" ;;
1161 esac
1162 cur_="${cur_#*:}"
1163 lhs=0
1166 pfx="+"
1167 cur_="${cur_#+}"
1169 esac
1170 case "$cmd" in
1171 fetch)
1172 if [ $lhs = 1 ]; then
1173 __git_complete_fetch_refspecs "$remote" "$pfx" "$cur_"
1174 else
1175 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1178 pull|remote)
1179 if [ $lhs = 1 ]; then
1180 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
1181 else
1182 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1185 push)
1186 if [ $lhs = 1 ]; then
1187 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1188 else
1189 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
1192 esac
1195 __git_complete_strategy ()
1197 __git_compute_merge_strategies
1198 case "$prev" in
1199 -s|--strategy)
1200 __gitcomp "$__git_merge_strategies"
1201 return 0
1204 __gitcomp "$__git_merge_strategy_options"
1205 return 0
1207 esac
1208 case "$cur" in
1209 --strategy=*)
1210 __gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
1211 return 0
1213 --strategy-option=*)
1214 __gitcomp "$__git_merge_strategy_options" "" "${cur##--strategy-option=}"
1215 return 0
1217 esac
1218 return 1
1221 __git_all_commands=
1222 __git_compute_all_commands ()
1224 test -n "$__git_all_commands" ||
1225 __git_all_commands=$(__git --list-cmds=main,others,alias,nohelpers)
1228 # Lists all set config variables starting with the given section prefix,
1229 # with the prefix removed.
1230 __git_get_config_variables ()
1232 local section="$1" i IFS=$'\n'
1233 for i in $(__git config --name-only --get-regexp "^$section\..*"); do
1234 echo "${i#$section.}"
1235 done
1238 __git_pretty_aliases ()
1240 __git_get_config_variables "pretty"
1243 # __git_aliased_command requires 1 argument
1244 __git_aliased_command ()
1246 local cur=$1 last list= word cmdline
1248 while [[ -n "$cur" ]]; do
1249 if [[ "$list" == *" $cur "* ]]; then
1250 # loop detected
1251 return
1254 cmdline=$(__git config --get "alias.$cur")
1255 list=" $cur $list"
1256 last=$cur
1257 cur=
1259 for word in $cmdline; do
1260 case "$word" in
1261 \!gitk|gitk)
1262 cur="gitk"
1263 break
1265 \!*) : shell command alias ;;
1266 -*) : option ;;
1267 *=*) : setting env ;;
1268 git) : git itself ;;
1269 \(\)) : skip parens of shell function definition ;;
1270 {) : skip start of shell helper function ;;
1271 :) : skip null command ;;
1272 \'*) : skip opening quote after sh -c ;;
1274 cur="${word%;}"
1275 break
1276 esac
1277 done
1278 done
1280 cur=$last
1281 if [[ "$cur" != "$1" ]]; then
1282 echo "$cur"
1286 # Check whether one of the given words is present on the command line,
1287 # and print the first word found.
1289 # Usage: __git_find_on_cmdline [<option>]... "<wordlist>"
1290 # --show-idx: Optionally show the index of the found word in the $words array.
1291 __git_find_on_cmdline ()
1293 local word c="$__git_cmd_idx" show_idx
1295 while test $# -gt 1; do
1296 case "$1" in
1297 --show-idx) show_idx=y ;;
1298 *) return 1 ;;
1299 esac
1300 shift
1301 done
1302 local wordlist="$1"
1304 while [ $c -lt $cword ]; do
1305 for word in $wordlist; do
1306 if [ "$word" = "${words[c]}" ]; then
1307 if [ -n "${show_idx-}" ]; then
1308 echo "$c $word"
1309 else
1310 echo "$word"
1312 return
1314 done
1315 ((c++))
1316 done
1319 # Similar to __git_find_on_cmdline, except that it loops backwards and thus
1320 # prints the *last* word found. Useful for finding which of two options that
1321 # supersede each other came last, such as "--guess" and "--no-guess".
1323 # Usage: __git_find_last_on_cmdline [<option>]... "<wordlist>"
1324 # --show-idx: Optionally show the index of the found word in the $words array.
1325 __git_find_last_on_cmdline ()
1327 local word c=$cword show_idx
1329 while test $# -gt 1; do
1330 case "$1" in
1331 --show-idx) show_idx=y ;;
1332 *) return 1 ;;
1333 esac
1334 shift
1335 done
1336 local wordlist="$1"
1338 while [ $c -gt "$__git_cmd_idx" ]; do
1339 ((c--))
1340 for word in $wordlist; do
1341 if [ "$word" = "${words[c]}" ]; then
1342 if [ -n "$show_idx" ]; then
1343 echo "$c $word"
1344 else
1345 echo "$word"
1347 return
1349 done
1350 done
1353 # Echo the value of an option set on the command line or config
1355 # $1: short option name
1356 # $2: long option name including =
1357 # $3: list of possible values
1358 # $4: config string (optional)
1360 # example:
1361 # result="$(__git_get_option_value "-d" "--do-something=" \
1362 # "yes no" "core.doSomething")"
1364 # result is then either empty (no option set) or "yes" or "no"
1366 # __git_get_option_value requires 3 arguments
1367 __git_get_option_value ()
1369 local c short_opt long_opt val
1370 local result= values config_key word
1372 short_opt="$1"
1373 long_opt="$2"
1374 values="$3"
1375 config_key="$4"
1377 ((c = $cword - 1))
1378 while [ $c -ge 0 ]; do
1379 word="${words[c]}"
1380 for val in $values; do
1381 if [ "$short_opt$val" = "$word" ] ||
1382 [ "$long_opt$val" = "$word" ]; then
1383 result="$val"
1384 break 2
1386 done
1387 ((c--))
1388 done
1390 if [ -n "$config_key" ] && [ -z "$result" ]; then
1391 result="$(__git config "$config_key")"
1394 echo "$result"
1397 __git_has_doubledash ()
1399 local c=1
1400 while [ $c -lt $cword ]; do
1401 if [ "--" = "${words[c]}" ]; then
1402 return 0
1404 ((c++))
1405 done
1406 return 1
1409 # Try to count non option arguments passed on the command line for the
1410 # specified git command.
1411 # When options are used, it is necessary to use the special -- option to
1412 # tell the implementation were non option arguments begin.
1413 # XXX this can not be improved, since options can appear everywhere, as
1414 # an example:
1415 # git mv x -n y
1417 # __git_count_arguments requires 1 argument: the git command executed.
1418 __git_count_arguments ()
1420 local word i c=0
1422 # Skip "git" (first argument)
1423 for ((i=$__git_cmd_idx; i < ${#words[@]}; i++)); do
1424 word="${words[i]}"
1426 case "$word" in
1428 # Good; we can assume that the following are only non
1429 # option arguments.
1430 ((c = 0))
1432 "$1")
1433 # Skip the specified git command and discard git
1434 # main options
1435 ((c = 0))
1438 ((c++))
1440 esac
1441 done
1443 printf "%d" $c
1446 __git_whitespacelist="nowarn warn error error-all fix"
1447 __git_patchformat="mbox stgit stgit-series hg mboxrd"
1448 __git_showcurrentpatch="diff raw"
1449 __git_am_inprogress_options="--skip --continue --resolved --abort --quit --show-current-patch"
1450 __git_quoted_cr="nowarn warn strip"
1452 _git_am ()
1454 __git_find_repo_path
1455 if [ -d "$__git_repo_path"/rebase-apply ]; then
1456 __gitcomp "$__git_am_inprogress_options"
1457 return
1459 case "$cur" in
1460 --whitespace=*)
1461 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1462 return
1464 --patch-format=*)
1465 __gitcomp "$__git_patchformat" "" "${cur##--patch-format=}"
1466 return
1468 --show-current-patch=*)
1469 __gitcomp "$__git_showcurrentpatch" "" "${cur##--show-current-patch=}"
1470 return
1472 --quoted-cr=*)
1473 __gitcomp "$__git_quoted_cr" "" "${cur##--quoted-cr=}"
1474 return
1476 --*)
1477 __gitcomp_builtin am "" \
1478 "$__git_am_inprogress_options"
1479 return
1480 esac
1483 _git_apply ()
1485 case "$cur" in
1486 --whitespace=*)
1487 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1488 return
1490 --*)
1491 __gitcomp_builtin apply
1492 return
1493 esac
1496 _git_add ()
1498 case "$cur" in
1499 --chmod=*)
1500 __gitcomp "+x -x" "" "${cur##--chmod=}"
1501 return
1503 --*)
1504 __gitcomp_builtin add
1505 return
1506 esac
1508 local complete_opt="--others --modified --directory --no-empty-directory"
1509 if test -n "$(__git_find_on_cmdline "-u --update")"
1510 then
1511 complete_opt="--modified"
1513 __git_complete_index_file "$complete_opt"
1516 _git_archive ()
1518 case "$cur" in
1519 --format=*)
1520 __gitcomp "$(git archive --list)" "" "${cur##--format=}"
1521 return
1523 --remote=*)
1524 __gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
1525 return
1527 --*)
1528 __gitcomp_builtin archive "--format= --list --verbose --prefix= --worktree-attributes"
1529 return
1531 esac
1532 __git_complete_file
1535 _git_bisect ()
1537 __git_has_doubledash && return
1539 __git_find_repo_path
1541 # If a bisection is in progress get the terms being used.
1542 local term_bad term_good
1543 if [ -f "$__git_repo_path"/BISECT_TERMS ]; then
1544 term_bad=$(__git bisect terms --term-bad)
1545 term_good=$(__git bisect terms --term-good)
1548 # We will complete any custom terms, but still always complete the
1549 # more usual bad/new/good/old because git bisect gives a good error
1550 # message if these are given when not in use, and that's better than
1551 # silent refusal to complete if the user is confused.
1553 # We want to recognize 'view' but not complete it, because it overlaps
1554 # with 'visualize' too much and is just an alias for it.
1556 local completable_subcommands="start bad new $term_bad good old $term_good terms skip reset visualize replay log run help"
1557 local all_subcommands="$completable_subcommands view"
1559 local subcommand="$(__git_find_on_cmdline "$all_subcommands")"
1561 if [ -z "$subcommand" ]; then
1562 __git_find_repo_path
1563 if [ -f "$__git_repo_path"/BISECT_START ]; then
1564 __gitcomp "$completable_subcommands"
1565 else
1566 __gitcomp "replay start"
1568 return
1571 case "$subcommand" in
1572 start)
1573 case "$cur" in
1574 --*)
1575 __gitcomp "--first-parent --no-checkout --term-new --term-bad --term-old --term-good"
1576 return
1579 __git_complete_refs
1581 esac
1583 terms)
1584 __gitcomp "--term-good --term-old --term-bad --term-new"
1585 return
1587 visualize|view)
1588 __git_complete_log_opts
1589 return
1591 bad|new|"$term_bad"|good|old|"$term_good"|reset|skip)
1592 __git_complete_refs
1596 esac
1599 __git_ref_fieldlist="refname objecttype objectsize objectname upstream push HEAD symref"
1601 _git_branch ()
1603 local i c="$__git_cmd_idx" only_local_ref="n" has_r="n"
1605 while [ $c -lt $cword ]; do
1606 i="${words[c]}"
1607 case "$i" in
1608 -d|-D|--delete|-m|-M|--move|-c|-C|--copy)
1609 only_local_ref="y" ;;
1610 -r|--remotes)
1611 has_r="y" ;;
1612 esac
1613 ((c++))
1614 done
1616 case "$cur" in
1617 --set-upstream-to=*)
1618 __git_complete_refs --cur="${cur##--set-upstream-to=}"
1620 --*)
1621 __gitcomp_builtin branch
1624 if [ $only_local_ref = "y" -a $has_r = "n" ]; then
1625 __gitcomp_direct "$(__git_heads "" "$cur" " ")"
1626 else
1627 __git_complete_refs
1630 esac
1633 _git_bundle ()
1635 local cmd="${words[__git_cmd_idx+1]}"
1636 case "$cword" in
1637 $((__git_cmd_idx+1)))
1638 __gitcomp "create list-heads verify unbundle"
1640 $((__git_cmd_idx+2)))
1641 # looking for a file
1644 case "$cmd" in
1645 create)
1646 __git_complete_revlist
1648 esac
1650 esac
1653 # Helper function to decide whether or not we should enable DWIM logic for
1654 # git-switch and git-checkout.
1656 # To decide between the following rules in decreasing priority order:
1657 # - the last provided of "--guess" or "--no-guess" explicitly enable or
1658 # disable completion of DWIM logic respectively.
1659 # - If checkout.guess is false, disable completion of DWIM logic.
1660 # - If the --no-track option is provided, take this as a hint to disable the
1661 # DWIM completion logic
1662 # - If GIT_COMPLETION_CHECKOUT_NO_GUESS is set, disable the DWIM completion
1663 # logic, as requested by the user.
1664 # - Enable DWIM logic otherwise.
1666 __git_checkout_default_dwim_mode ()
1668 local last_option dwim_opt="--dwim"
1670 if [ "${GIT_COMPLETION_CHECKOUT_NO_GUESS-}" = "1" ]; then
1671 dwim_opt=""
1674 # --no-track disables DWIM, but with lower priority than
1675 # --guess/--no-guess/checkout.guess
1676 if [ -n "$(__git_find_on_cmdline "--no-track")" ]; then
1677 dwim_opt=""
1680 # checkout.guess = false disables DWIM, but with lower priority than
1681 # --guess/--no-guess
1682 if [ "$(__git config --type=bool checkout.guess)" = "false" ]; then
1683 dwim_opt=""
1686 # Find the last provided --guess or --no-guess
1687 last_option="$(__git_find_last_on_cmdline "--guess --no-guess")"
1688 case "$last_option" in
1689 --guess)
1690 dwim_opt="--dwim"
1692 --no-guess)
1693 dwim_opt=""
1695 esac
1697 echo "$dwim_opt"
1700 _git_checkout ()
1702 __git_has_doubledash && return
1704 local dwim_opt="$(__git_checkout_default_dwim_mode)"
1706 case "$prev" in
1707 -b|-B|--orphan)
1708 # Complete local branches (and DWIM branch
1709 # remote branch names) for an option argument
1710 # specifying a new branch name. This is for
1711 # convenience, assuming new branches are
1712 # possibly based on pre-existing branch names.
1713 __git_complete_refs $dwim_opt --mode="heads"
1714 return
1718 esac
1720 case "$cur" in
1721 --conflict=*)
1722 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
1724 --*)
1725 __gitcomp_builtin checkout
1728 # At this point, we've already handled special completion for
1729 # the arguments to -b/-B, and --orphan. There are 3 main
1730 # things left we can possibly complete:
1731 # 1) a start-point for -b/-B, -d/--detach, or --orphan
1732 # 2) a remote head, for --track
1733 # 3) an arbitrary reference, possibly including DWIM names
1736 if [ -n "$(__git_find_on_cmdline "-b -B -d --detach --orphan")" ]; then
1737 __git_complete_refs --mode="refs"
1738 elif [ -n "$(__git_find_on_cmdline "-t --track")" ]; then
1739 __git_complete_refs --mode="remote-heads"
1740 else
1741 __git_complete_refs $dwim_opt --mode="refs"
1744 esac
1747 __git_sequencer_inprogress_options="--continue --quit --abort --skip"
1749 __git_cherry_pick_inprogress_options=$__git_sequencer_inprogress_options
1751 _git_cherry_pick ()
1753 if __git_pseudoref_exists CHERRY_PICK_HEAD; then
1754 __gitcomp "$__git_cherry_pick_inprogress_options"
1755 return
1758 __git_complete_strategy && return
1760 case "$cur" in
1761 --*)
1762 __gitcomp_builtin cherry-pick "" \
1763 "$__git_cherry_pick_inprogress_options"
1766 __git_complete_refs
1768 esac
1771 _git_clean ()
1773 case "$cur" in
1774 --*)
1775 __gitcomp_builtin clean
1776 return
1778 esac
1780 # XXX should we check for -x option ?
1781 __git_complete_index_file "--others --directory"
1784 _git_clone ()
1786 case "$prev" in
1787 -c|--config)
1788 __git_complete_config_variable_name_and_value
1789 return
1791 esac
1792 case "$cur" in
1793 --config=*)
1794 __git_complete_config_variable_name_and_value \
1795 --cur="${cur##--config=}"
1796 return
1798 --*)
1799 __gitcomp_builtin clone
1800 return
1802 esac
1805 __git_untracked_file_modes="all no normal"
1807 __git_trailer_tokens ()
1809 __git config --name-only --get-regexp '^trailer\..*\.key$' | cut -d. -f 2- | rev | cut -d. -f2- | rev
1812 _git_commit ()
1814 case "$prev" in
1815 -c|-C)
1816 __git_complete_refs
1817 return
1819 esac
1821 case "$cur" in
1822 --cleanup=*)
1823 __gitcomp "default scissors strip verbatim whitespace
1824 " "" "${cur##--cleanup=}"
1825 return
1827 --reuse-message=*|--reedit-message=*|\
1828 --fixup=*|--squash=*)
1829 __git_complete_refs --cur="${cur#*=}"
1830 return
1832 --untracked-files=*)
1833 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
1834 return
1836 --trailer=*)
1837 __gitcomp_nl "$(__git_trailer_tokens)" "" "${cur##--trailer=}" ":"
1838 return
1840 --*)
1841 __gitcomp_builtin commit
1842 return
1843 esac
1845 if __git rev-parse --verify --quiet HEAD >/dev/null; then
1846 __git_complete_index_file "--committable"
1847 else
1848 # This is the first commit
1849 __git_complete_index_file "--cached"
1853 _git_describe ()
1855 case "$cur" in
1856 --*)
1857 __gitcomp_builtin describe
1858 return
1859 esac
1860 __git_complete_refs
1863 __git_diff_algorithms="myers minimal patience histogram"
1865 __git_diff_submodule_formats="diff log short"
1867 __git_color_moved_opts="no default plain blocks zebra dimmed-zebra"
1869 __git_color_moved_ws_opts="no ignore-space-at-eol ignore-space-change
1870 ignore-all-space allow-indentation-change"
1872 __git_ws_error_highlight_opts="context old new all default"
1874 # Options for the diff machinery (diff, log, show, stash, range-diff, ...)
1875 __git_diff_common_options="--stat --numstat --shortstat --summary
1876 --patch-with-stat --name-only --name-status --color
1877 --no-color --color-words --no-renames --check
1878 --color-moved --color-moved= --no-color-moved
1879 --color-moved-ws= --no-color-moved-ws
1880 --full-index --binary --abbrev --diff-filter=
1881 --find-copies --find-object --find-renames
1882 --no-relative --relative
1883 --find-copies-harder --ignore-cr-at-eol
1884 --text --ignore-space-at-eol --ignore-space-change
1885 --ignore-all-space --ignore-blank-lines --exit-code
1886 --quiet --ext-diff --no-ext-diff --unified=
1887 --no-prefix --src-prefix= --dst-prefix=
1888 --inter-hunk-context= --function-context
1889 --patience --histogram --minimal
1890 --raw --word-diff --word-diff-regex=
1891 --dirstat --dirstat= --dirstat-by-file
1892 --dirstat-by-file= --cumulative
1893 --diff-algorithm= --default-prefix
1894 --submodule --submodule= --ignore-submodules
1895 --indent-heuristic --no-indent-heuristic
1896 --textconv --no-textconv --break-rewrites
1897 --patch --no-patch --cc --combined-all-paths
1898 --anchored= --compact-summary --ignore-matching-lines=
1899 --irreversible-delete --line-prefix --no-stat
1900 --output= --output-indicator-context=
1901 --output-indicator-new= --output-indicator-old=
1902 --ws-error-highlight=
1903 --pickaxe-all --pickaxe-regex --patch-with-raw
1906 # Options for diff/difftool
1907 __git_diff_difftool_options="--cached --staged
1908 --base --ours --theirs --no-index --merge-base
1909 --ita-invisible-in-index --ita-visible-in-index
1910 $__git_diff_common_options"
1912 _git_diff ()
1914 __git_has_doubledash && return
1916 case "$cur" in
1917 --diff-algorithm=*)
1918 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1919 return
1921 --submodule=*)
1922 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1923 return
1925 --color-moved=*)
1926 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
1927 return
1929 --color-moved-ws=*)
1930 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
1931 return
1933 --ws-error-highlight=*)
1934 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
1935 return
1937 --*)
1938 __gitcomp "$__git_diff_difftool_options"
1939 return
1941 esac
1942 __git_complete_revlist_file
1945 __git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff
1946 tkdiff vimdiff nvimdiff gvimdiff xxdiff araxis p4merge
1947 bc codecompare smerge
1950 _git_difftool ()
1952 __git_has_doubledash && return
1954 case "$cur" in
1955 --tool=*)
1956 __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1957 return
1959 --*)
1960 __gitcomp_builtin difftool "$__git_diff_difftool_options"
1961 return
1963 esac
1964 __git_complete_revlist_file
1967 __git_fetch_recurse_submodules="yes on-demand no"
1969 _git_fetch ()
1971 case "$cur" in
1972 --recurse-submodules=*)
1973 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1974 return
1976 --filter=*)
1977 __gitcomp "blob:none blob:limit= sparse:oid=" "" "${cur##--filter=}"
1978 return
1980 --*)
1981 __gitcomp_builtin fetch
1982 return
1984 esac
1985 __git_complete_remote_or_refspec
1988 __git_format_patch_extra_options="
1989 --full-index --not --all --no-prefix --src-prefix=
1990 --dst-prefix= --notes
1993 _git_format_patch ()
1995 case "$cur" in
1996 --thread=*)
1997 __gitcomp "
1998 deep shallow
1999 " "" "${cur##--thread=}"
2000 return
2002 --base=*|--interdiff=*|--range-diff=*)
2003 __git_complete_refs --cur="${cur#--*=}"
2004 return
2006 --*)
2007 __gitcomp_builtin format-patch "$__git_format_patch_extra_options"
2008 return
2010 esac
2011 __git_complete_revlist
2014 _git_fsck ()
2016 case "$cur" in
2017 --*)
2018 __gitcomp_builtin fsck
2019 return
2021 esac
2024 _git_gitk ()
2026 __gitk_main
2029 # Lists matching symbol names from a tag (as in ctags) file.
2030 # 1: List symbol names matching this word.
2031 # 2: The tag file to list symbol names from.
2032 # 3: A prefix to be added to each listed symbol name (optional).
2033 # 4: A suffix to be appended to each listed symbol name (optional).
2034 __git_match_ctag () {
2035 awk -v pfx="${3-}" -v sfx="${4-}" "
2036 /^${1//\//\\/}/ { print pfx \$1 sfx }
2037 " "$2"
2040 # Complete symbol names from a tag file.
2041 # Usage: __git_complete_symbol [<option>]...
2042 # --tags=<file>: The tag file to list symbol names from instead of the
2043 # default "tags".
2044 # --pfx=<prefix>: A prefix to be added to each symbol name.
2045 # --cur=<word>: The current symbol name to be completed. Defaults to
2046 # the current word to be completed.
2047 # --sfx=<suffix>: A suffix to be appended to each symbol name instead
2048 # of the default space.
2049 __git_complete_symbol () {
2050 local tags=tags pfx="" cur_="${cur-}" sfx=" "
2052 while test $# != 0; do
2053 case "$1" in
2054 --tags=*) tags="${1##--tags=}" ;;
2055 --pfx=*) pfx="${1##--pfx=}" ;;
2056 --cur=*) cur_="${1##--cur=}" ;;
2057 --sfx=*) sfx="${1##--sfx=}" ;;
2058 *) return 1 ;;
2059 esac
2060 shift
2061 done
2063 if test -r "$tags"; then
2064 __gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
2068 _git_grep ()
2070 __git_has_doubledash && return
2072 case "$cur" in
2073 --*)
2074 __gitcomp_builtin grep
2075 return
2077 esac
2079 case "$cword,$prev" in
2080 $((__git_cmd_idx+1)),*|*,-*)
2081 __git_complete_symbol && return
2083 esac
2085 __git_complete_refs
2088 _git_help ()
2090 case "$cur" in
2091 --*)
2092 __gitcomp_builtin help
2093 return
2095 esac
2096 if test -n "${GIT_TESTING_ALL_COMMAND_LIST-}"
2097 then
2098 __gitcomp "$GIT_TESTING_ALL_COMMAND_LIST $(__git --list-cmds=alias,list-guide) gitk"
2099 else
2100 __gitcomp "$(__git --list-cmds=main,nohelpers,alias,list-guide) gitk"
2104 _git_init ()
2106 case "$cur" in
2107 --shared=*)
2108 __gitcomp "
2109 false true umask group all world everybody
2110 " "" "${cur##--shared=}"
2111 return
2113 --*)
2114 __gitcomp_builtin init
2115 return
2117 esac
2120 _git_ls_files ()
2122 case "$cur" in
2123 --*)
2124 __gitcomp_builtin ls-files
2125 return
2127 esac
2129 # XXX ignore options like --modified and always suggest all cached
2130 # files.
2131 __git_complete_index_file "--cached"
2134 _git_ls_remote ()
2136 case "$cur" in
2137 --*)
2138 __gitcomp_builtin ls-remote
2139 return
2141 esac
2142 __gitcomp_nl "$(__git_remotes)"
2145 _git_ls_tree ()
2147 case "$cur" in
2148 --*)
2149 __gitcomp_builtin ls-tree
2150 return
2152 esac
2154 __git_complete_file
2157 # Options that go well for log, shortlog and gitk
2158 __git_log_common_options="
2159 --not --all
2160 --branches --tags --remotes
2161 --first-parent --merges --no-merges
2162 --max-count=
2163 --max-age= --since= --after=
2164 --min-age= --until= --before=
2165 --min-parents= --max-parents=
2166 --no-min-parents --no-max-parents
2167 --alternate-refs --ancestry-path
2168 --author-date-order --basic-regexp
2169 --bisect --boundary --exclude-first-parent-only
2170 --exclude-hidden --extended-regexp
2171 --fixed-strings --grep-reflog
2172 --ignore-missing --left-only --perl-regexp
2173 --reflog --regexp-ignore-case --remove-empty
2174 --right-only --show-linear-break
2175 --show-notes-by-default --show-pulls
2176 --since-as-filter --single-worktree
2178 # Options that go well for log and gitk (not shortlog)
2179 __git_log_gitk_options="
2180 --dense --sparse --full-history
2181 --simplify-merges --simplify-by-decoration
2182 --left-right --notes --no-notes
2184 # Options that go well for log and shortlog (not gitk)
2185 __git_log_shortlog_options="
2186 --author= --committer= --grep=
2187 --all-match --invert-grep
2189 # Options accepted by log and show
2190 __git_log_show_options="
2191 --diff-merges --diff-merges= --no-diff-merges --dd --remerge-diff
2192 --encoding=
2195 __git_diff_merges_opts="off none on first-parent 1 separate m combined c dense-combined cc remerge r"
2197 __git_log_pretty_formats="oneline short medium full fuller reference email raw format: tformat: mboxrd"
2198 __git_log_date_formats="relative iso8601 iso8601-strict rfc2822 short local default human raw unix auto: format:"
2200 # Complete porcelain (i.e. not git-rev-list) options and at least some
2201 # option arguments accepted by git-log. Note that this same set of options
2202 # are also accepted by some other git commands besides git-log.
2203 __git_complete_log_opts ()
2205 COMPREPLY=()
2207 local merge=""
2208 if __git_pseudoref_exists MERGE_HEAD; then
2209 merge="--merge"
2211 case "$prev,$cur" in
2212 -L,:*:*)
2213 return # fall back to Bash filename completion
2215 -L,:*)
2216 __git_complete_symbol --cur="${cur#:}" --sfx=":"
2217 return
2219 -G,*|-S,*)
2220 __git_complete_symbol
2221 return
2223 esac
2224 case "$cur" in
2225 --pretty=*|--format=*)
2226 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2227 " "" "${cur#*=}"
2228 return
2230 --date=*)
2231 __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
2232 return
2234 --decorate=*)
2235 __gitcomp "full short no" "" "${cur##--decorate=}"
2236 return
2238 --diff-algorithm=*)
2239 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
2240 return
2242 --submodule=*)
2243 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
2244 return
2246 --ws-error-highlight=*)
2247 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
2248 return
2250 --no-walk=*)
2251 __gitcomp "sorted unsorted" "" "${cur##--no-walk=}"
2252 return
2254 --diff-merges=*)
2255 __gitcomp "$__git_diff_merges_opts" "" "${cur##--diff-merges=}"
2256 return
2258 --*)
2259 __gitcomp "
2260 $__git_log_common_options
2261 $__git_log_shortlog_options
2262 $__git_log_gitk_options
2263 $__git_log_show_options
2264 --root --topo-order --date-order --reverse
2265 --follow --full-diff
2266 --abbrev-commit --no-abbrev-commit --abbrev=
2267 --relative-date --date=
2268 --pretty= --format= --oneline
2269 --show-signature
2270 --cherry-mark
2271 --cherry-pick
2272 --graph
2273 --decorate --decorate= --no-decorate
2274 --walk-reflogs
2275 --no-walk --no-walk= --do-walk
2276 --parents --children
2277 --expand-tabs --expand-tabs= --no-expand-tabs
2278 --clear-decorations --decorate-refs=
2279 --decorate-refs-exclude=
2280 $merge
2281 $__git_diff_common_options
2283 return
2285 -L:*:*)
2286 return # fall back to Bash filename completion
2288 -L:*)
2289 __git_complete_symbol --cur="${cur#-L:}" --sfx=":"
2290 return
2292 -G*)
2293 __git_complete_symbol --pfx="-G" --cur="${cur#-G}"
2294 return
2296 -S*)
2297 __git_complete_symbol --pfx="-S" --cur="${cur#-S}"
2298 return
2300 esac
2303 _git_log ()
2305 __git_has_doubledash && return
2306 __git_find_repo_path
2308 __git_complete_log_opts
2309 [ ${#COMPREPLY[@]} -eq 0 ] || return
2311 __git_complete_revlist
2314 _git_merge ()
2316 __git_complete_strategy && return
2318 case "$cur" in
2319 --*)
2320 __gitcomp_builtin merge
2321 return
2322 esac
2323 __git_complete_refs
2326 _git_mergetool ()
2328 case "$cur" in
2329 --tool=*)
2330 __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
2331 return
2333 --*)
2334 __gitcomp "--tool= --prompt --no-prompt --gui --no-gui"
2335 return
2337 esac
2340 _git_merge_base ()
2342 case "$cur" in
2343 --*)
2344 __gitcomp_builtin merge-base
2345 return
2347 esac
2348 __git_complete_refs
2351 _git_mv ()
2353 case "$cur" in
2354 --*)
2355 __gitcomp_builtin mv
2356 return
2358 esac
2360 if [ $(__git_count_arguments "mv") -gt 0 ]; then
2361 # We need to show both cached and untracked files (including
2362 # empty directories) since this may not be the last argument.
2363 __git_complete_index_file "--cached --others --directory"
2364 else
2365 __git_complete_index_file "--cached"
2369 _git_notes ()
2371 local subcommands='add append copy edit get-ref list merge prune remove show'
2372 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2374 case "$subcommand,$cur" in
2375 ,--*)
2376 __gitcomp_builtin notes
2379 case "$prev" in
2380 --ref)
2381 __git_complete_refs
2384 __gitcomp "$subcommands --ref"
2386 esac
2388 *,--reuse-message=*|*,--reedit-message=*)
2389 __git_complete_refs --cur="${cur#*=}"
2391 *,--*)
2392 __gitcomp_builtin notes_$subcommand
2394 prune,*|get-ref,*)
2395 # this command does not take a ref, do not complete it
2398 case "$prev" in
2399 -m|-F)
2402 __git_complete_refs
2404 esac
2406 esac
2409 _git_pull ()
2411 __git_complete_strategy && return
2413 case "$cur" in
2414 --recurse-submodules=*)
2415 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
2416 return
2418 --*)
2419 __gitcomp_builtin pull
2421 return
2423 esac
2424 __git_complete_remote_or_refspec
2427 __git_push_recurse_submodules="check on-demand only"
2429 __git_complete_force_with_lease ()
2431 local cur_=$1
2433 case "$cur_" in
2434 --*=)
2436 *:*)
2437 __git_complete_refs --cur="${cur_#*:}"
2440 __git_complete_refs --cur="$cur_"
2442 esac
2445 _git_push ()
2447 case "$prev" in
2448 --repo)
2449 __gitcomp_nl "$(__git_remotes)"
2450 return
2452 --recurse-submodules)
2453 __gitcomp "$__git_push_recurse_submodules"
2454 return
2456 esac
2457 case "$cur" in
2458 --repo=*)
2459 __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
2460 return
2462 --recurse-submodules=*)
2463 __gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
2464 return
2466 --force-with-lease=*)
2467 __git_complete_force_with_lease "${cur##--force-with-lease=}"
2468 return
2470 --*)
2471 __gitcomp_builtin push
2472 return
2474 esac
2475 __git_complete_remote_or_refspec
2478 _git_range_diff ()
2480 case "$cur" in
2481 --*)
2482 __gitcomp "
2483 --creation-factor= --no-dual-color
2484 $__git_diff_common_options
2486 return
2488 esac
2489 __git_complete_revlist
2492 __git_rebase_inprogress_options="--continue --skip --abort --quit --show-current-patch"
2493 __git_rebase_interactive_inprogress_options="$__git_rebase_inprogress_options --edit-todo"
2495 _git_rebase ()
2497 __git_find_repo_path
2498 if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
2499 __gitcomp "$__git_rebase_interactive_inprogress_options"
2500 return
2501 elif [ -d "$__git_repo_path"/rebase-apply ] || \
2502 [ -d "$__git_repo_path"/rebase-merge ]; then
2503 __gitcomp "$__git_rebase_inprogress_options"
2504 return
2506 __git_complete_strategy && return
2507 case "$cur" in
2508 --whitespace=*)
2509 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
2510 return
2512 --onto=*)
2513 __git_complete_refs --cur="${cur##--onto=}"
2514 return
2516 --*)
2517 __gitcomp_builtin rebase "" \
2518 "$__git_rebase_interactive_inprogress_options"
2520 return
2521 esac
2522 __git_complete_refs
2525 _git_reflog ()
2527 local subcommands subcommand
2529 __git_resolve_builtins "reflog"
2531 subcommands="$___git_resolved_builtins"
2532 subcommand="$(__git_find_subcommand "$subcommands" "show")"
2534 case "$subcommand,$cur" in
2535 show,--*)
2536 __gitcomp "
2537 $__git_log_common_options
2539 return
2541 $subcommand,--*)
2542 __gitcomp_builtin "reflog_$subcommand"
2543 return
2545 esac
2547 __git_complete_refs
2549 if [ $((cword - __git_cmd_idx)) -eq 1 ]; then
2550 __gitcompappend "$subcommands" "" "$cur" " "
2554 __git_send_email_confirm_options="always never auto cc compose"
2555 __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
2557 _git_send_email ()
2559 case "$prev" in
2560 --to|--cc|--bcc|--from)
2561 __gitcomp "$(__git send-email --dump-aliases)"
2562 return
2564 esac
2566 case "$cur" in
2567 --confirm=*)
2568 __gitcomp "
2569 $__git_send_email_confirm_options
2570 " "" "${cur##--confirm=}"
2571 return
2573 --suppress-cc=*)
2574 __gitcomp "
2575 $__git_send_email_suppresscc_options
2576 " "" "${cur##--suppress-cc=}"
2578 return
2580 --smtp-encryption=*)
2581 __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
2582 return
2584 --thread=*)
2585 __gitcomp "
2586 deep shallow
2587 " "" "${cur##--thread=}"
2588 return
2590 --to=*|--cc=*|--bcc=*|--from=*)
2591 __gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
2592 return
2594 --*)
2595 __gitcomp_builtin send-email "$__git_format_patch_extra_options"
2596 return
2598 esac
2599 __git_complete_revlist
2602 _git_stage ()
2604 _git_add
2607 _git_status ()
2609 local complete_opt
2610 local untracked_state
2612 case "$cur" in
2613 --ignore-submodules=*)
2614 __gitcomp "none untracked dirty all" "" "${cur##--ignore-submodules=}"
2615 return
2617 --untracked-files=*)
2618 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
2619 return
2621 --column=*)
2622 __gitcomp "
2623 always never auto column row plain dense nodense
2624 " "" "${cur##--column=}"
2625 return
2627 --*)
2628 __gitcomp_builtin status
2629 return
2631 esac
2633 untracked_state="$(__git_get_option_value "-u" "--untracked-files=" \
2634 "$__git_untracked_file_modes" "status.showUntrackedFiles")"
2636 case "$untracked_state" in
2638 # --ignored option does not matter
2639 complete_opt=
2641 all|normal|*)
2642 complete_opt="--cached --directory --no-empty-directory --others"
2644 if [ -n "$(__git_find_on_cmdline "--ignored")" ]; then
2645 complete_opt="$complete_opt --ignored --exclude=*"
2648 esac
2650 __git_complete_index_file "$complete_opt"
2653 _git_switch ()
2655 local dwim_opt="$(__git_checkout_default_dwim_mode)"
2657 case "$prev" in
2658 -c|-C|--orphan)
2659 # Complete local branches (and DWIM branch
2660 # remote branch names) for an option argument
2661 # specifying a new branch name. This is for
2662 # convenience, assuming new branches are
2663 # possibly based on pre-existing branch names.
2664 __git_complete_refs $dwim_opt --mode="heads"
2665 return
2669 esac
2671 case "$cur" in
2672 --conflict=*)
2673 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
2675 --*)
2676 __gitcomp_builtin switch
2679 # Unlike in git checkout, git switch --orphan does not take
2680 # a start point. Thus we really have nothing to complete after
2681 # the branch name.
2682 if [ -n "$(__git_find_on_cmdline "--orphan")" ]; then
2683 return
2686 # At this point, we've already handled special completion for
2687 # -c/-C, and --orphan. There are 3 main things left to
2688 # complete:
2689 # 1) a start-point for -c/-C or -d/--detach
2690 # 2) a remote head, for --track
2691 # 3) a branch name, possibly including DWIM remote branches
2693 if [ -n "$(__git_find_on_cmdline "-c -C -d --detach")" ]; then
2694 __git_complete_refs --mode="refs"
2695 elif [ -n "$(__git_find_on_cmdline "-t --track")" ]; then
2696 __git_complete_refs --mode="remote-heads"
2697 else
2698 __git_complete_refs $dwim_opt --mode="heads"
2701 esac
2704 __git_config_get_set_variables ()
2706 local prevword word config_file= c=$cword
2707 while [ $c -gt "$__git_cmd_idx" ]; do
2708 word="${words[c]}"
2709 case "$word" in
2710 --system|--global|--local|--file=*)
2711 config_file="$word"
2712 break
2714 -f|--file)
2715 config_file="$word $prevword"
2716 break
2718 esac
2719 prevword=$word
2720 c=$((--c))
2721 done
2723 __git config $config_file --name-only --list
2726 __git_config_vars=
2727 __git_compute_config_vars ()
2729 test -n "$__git_config_vars" ||
2730 __git_config_vars="$(git help --config-for-completion)"
2733 __git_config_vars_all=
2734 __git_compute_config_vars_all ()
2736 test -n "$__git_config_vars_all" ||
2737 __git_config_vars_all="$(git --no-pager help --config)"
2740 __git_compute_first_level_config_vars_for_section ()
2742 local section="$1"
2743 __git_compute_config_vars
2744 local this_section="__git_first_level_config_vars_for_section_${section}"
2745 test -n "${!this_section}" ||
2746 printf -v "__git_first_level_config_vars_for_section_${section}" %s \
2747 "$(echo "$__git_config_vars" | awk -F. "/^${section}\.[a-z]/ { print \$2 }")"
2750 __git_compute_second_level_config_vars_for_section ()
2752 local section="$1"
2753 __git_compute_config_vars_all
2754 local this_section="__git_second_level_config_vars_for_section_${section}"
2755 test -n "${!this_section}" ||
2756 printf -v "__git_second_level_config_vars_for_section_${section}" %s \
2757 "$(echo "$__git_config_vars_all" | awk -F. "/^${section}\.</ { print \$3 }")"
2760 __git_config_sections=
2761 __git_compute_config_sections ()
2763 test -n "$__git_config_sections" ||
2764 __git_config_sections="$(git help --config-sections-for-completion)"
2767 # Completes possible values of various configuration variables.
2769 # Usage: __git_complete_config_variable_value [<option>]...
2770 # --varname=<word>: The name of the configuration variable whose value is
2771 # to be completed. Defaults to the previous word on the
2772 # command line.
2773 # --cur=<word>: The current value to be completed. Defaults to the current
2774 # word to be completed.
2775 __git_complete_config_variable_value ()
2777 local varname="$prev" cur_="$cur"
2779 while test $# != 0; do
2780 case "$1" in
2781 --varname=*) varname="${1##--varname=}" ;;
2782 --cur=*) cur_="${1##--cur=}" ;;
2783 *) return 1 ;;
2784 esac
2785 shift
2786 done
2788 if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
2789 varname="${varname,,}"
2790 else
2791 varname="$(echo "$varname" |tr A-Z a-z)"
2794 case "$varname" in
2795 branch.*.remote|branch.*.pushremote)
2796 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2797 return
2799 branch.*.merge)
2800 __git_complete_refs --cur="$cur_"
2801 return
2803 branch.*.rebase)
2804 __gitcomp "false true merges interactive" "" "$cur_"
2805 return
2807 remote.pushdefault)
2808 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2809 return
2811 remote.*.fetch)
2812 local remote="${varname#remote.}"
2813 remote="${remote%.fetch}"
2814 if [ -z "$cur_" ]; then
2815 __gitcomp_nl "refs/heads/" "" "" ""
2816 return
2818 __gitcomp_nl "$(__git_refs_remotes "$remote")" "" "$cur_"
2819 return
2821 remote.*.push)
2822 local remote="${varname#remote.}"
2823 remote="${remote%.push}"
2824 __gitcomp_nl "$(__git for-each-ref \
2825 --format='%(refname):%(refname)' refs/heads)" "" "$cur_"
2826 return
2828 pull.twohead|pull.octopus)
2829 __git_compute_merge_strategies
2830 __gitcomp "$__git_merge_strategies" "" "$cur_"
2831 return
2833 color.pager)
2834 __gitcomp "false true" "" "$cur_"
2835 return
2837 color.*.*)
2838 __gitcomp "
2839 normal black red green yellow blue magenta cyan white
2840 bold dim ul blink reverse
2841 " "" "$cur_"
2842 return
2844 color.*)
2845 __gitcomp "false true always never auto" "" "$cur_"
2846 return
2848 diff.submodule)
2849 __gitcomp "$__git_diff_submodule_formats" "" "$cur_"
2850 return
2852 help.format)
2853 __gitcomp "man info web html" "" "$cur_"
2854 return
2856 log.date)
2857 __gitcomp "$__git_log_date_formats" "" "$cur_"
2858 return
2860 sendemail.aliasfiletype)
2861 __gitcomp "mutt mailrc pine elm gnus" "" "$cur_"
2862 return
2864 sendemail.confirm)
2865 __gitcomp "$__git_send_email_confirm_options" "" "$cur_"
2866 return
2868 sendemail.suppresscc)
2869 __gitcomp "$__git_send_email_suppresscc_options" "" "$cur_"
2870 return
2872 sendemail.transferencoding)
2873 __gitcomp "7bit 8bit quoted-printable base64" "" "$cur_"
2874 return
2876 *.*)
2877 return
2879 esac
2882 # Completes configuration sections, subsections, variable names.
2884 # Usage: __git_complete_config_variable_name [<option>]...
2885 # --cur=<word>: The current configuration section/variable name to be
2886 # completed. Defaults to the current word to be completed.
2887 # --sfx=<suffix>: A suffix to be appended to each fully completed
2888 # configuration variable name (but not to sections or
2889 # subsections) instead of the default space.
2890 __git_complete_config_variable_name ()
2892 local cur_="$cur" sfx
2894 while test $# != 0; do
2895 case "$1" in
2896 --cur=*) cur_="${1##--cur=}" ;;
2897 --sfx=*) sfx="${1##--sfx=}" ;;
2898 *) return 1 ;;
2899 esac
2900 shift
2901 done
2903 case "$cur_" in
2904 branch.*.*|guitool.*.*|difftool.*.*|man.*.*|mergetool.*.*|remote.*.*|submodule.*.*|url.*.*)
2905 local pfx="${cur_%.*}."
2906 cur_="${cur_##*.}"
2907 local section="${pfx%.*.}"
2908 __git_compute_second_level_config_vars_for_section "${section}"
2909 local this_section="__git_second_level_config_vars_for_section_${section}"
2910 __gitcomp "${!this_section}" "$pfx" "$cur_" "$sfx"
2911 return
2913 branch.*)
2914 local pfx="${cur_%.*}."
2915 cur_="${cur_#*.}"
2916 local section="${pfx%.}"
2917 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
2918 __git_compute_first_level_config_vars_for_section "${section}"
2919 local this_section="__git_first_level_config_vars_for_section_${section}"
2920 __gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
2921 return
2923 pager.*)
2924 local pfx="${cur_%.*}."
2925 cur_="${cur_#*.}"
2926 __git_compute_all_commands
2927 __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_" "${sfx:- }"
2928 return
2930 remote.*)
2931 local pfx="${cur_%.*}."
2932 cur_="${cur_#*.}"
2933 local section="${pfx%.}"
2934 __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
2935 __git_compute_first_level_config_vars_for_section "${section}"
2936 local this_section="__git_first_level_config_vars_for_section_${section}"
2937 __gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
2938 return
2940 submodule.*)
2941 local pfx="${cur_%.*}."
2942 cur_="${cur_#*.}"
2943 local section="${pfx%.}"
2944 __gitcomp_nl "$(__git config -f "$(__git rev-parse --show-toplevel)/.gitmodules" --get-regexp 'submodule.*.path' | awk -F. '{print $2}')" "$pfx" "$cur_" "."
2945 __git_compute_first_level_config_vars_for_section "${section}"
2946 local this_section="__git_first_level_config_vars_for_section_${section}"
2947 __gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
2948 return
2950 *.*)
2951 __git_compute_config_vars
2952 __gitcomp "$__git_config_vars" "" "$cur_" "$sfx"
2955 __git_compute_config_sections
2956 __gitcomp "$__git_config_sections" "" "$cur_" "."
2958 esac
2961 # Completes '='-separated configuration sections/variable names and values
2962 # for 'git -c section.name=value'.
2964 # Usage: __git_complete_config_variable_name_and_value [<option>]...
2965 # --cur=<word>: The current configuration section/variable name/value to be
2966 # completed. Defaults to the current word to be completed.
2967 __git_complete_config_variable_name_and_value ()
2969 local cur_="$cur"
2971 while test $# != 0; do
2972 case "$1" in
2973 --cur=*) cur_="${1##--cur=}" ;;
2974 *) return 1 ;;
2975 esac
2976 shift
2977 done
2979 case "$cur_" in
2980 *=*)
2981 __git_complete_config_variable_value \
2982 --varname="${cur_%%=*}" --cur="${cur_#*=}"
2985 __git_complete_config_variable_name --cur="$cur_" --sfx='='
2987 esac
2990 _git_config ()
2992 case "$prev" in
2993 --get|--get-all|--unset|--unset-all)
2994 __gitcomp_nl "$(__git_config_get_set_variables)"
2995 return
2997 *.*)
2998 __git_complete_config_variable_value
2999 return
3001 esac
3002 case "$cur" in
3003 --*)
3004 __gitcomp_builtin config
3007 __git_complete_config_variable_name
3009 esac
3012 _git_remote ()
3014 local subcommands="
3015 add rename remove set-head set-branches
3016 get-url set-url show prune update
3018 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3019 if [ -z "$subcommand" ]; then
3020 case "$cur" in
3021 --*)
3022 __gitcomp_builtin remote
3025 __gitcomp "$subcommands"
3027 esac
3028 return
3031 case "$subcommand,$cur" in
3032 add,--*)
3033 __gitcomp_builtin remote_add
3035 add,*)
3037 set-head,--*)
3038 __gitcomp_builtin remote_set-head
3040 set-branches,--*)
3041 __gitcomp_builtin remote_set-branches
3043 set-head,*|set-branches,*)
3044 __git_complete_remote_or_refspec
3046 update,--*)
3047 __gitcomp_builtin remote_update
3049 update,*)
3050 __gitcomp "$(__git_remotes) $(__git_get_config_variables "remotes")"
3052 set-url,--*)
3053 __gitcomp_builtin remote_set-url
3055 get-url,--*)
3056 __gitcomp_builtin remote_get-url
3058 prune,--*)
3059 __gitcomp_builtin remote_prune
3062 __gitcomp_nl "$(__git_remotes)"
3064 esac
3067 _git_replace ()
3069 case "$cur" in
3070 --format=*)
3071 __gitcomp "short medium long" "" "${cur##--format=}"
3072 return
3074 --*)
3075 __gitcomp_builtin replace
3076 return
3078 esac
3079 __git_complete_refs
3082 _git_rerere ()
3084 local subcommands="clear forget diff remaining status gc"
3085 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3086 if test -z "$subcommand"
3087 then
3088 __gitcomp "$subcommands"
3089 return
3093 _git_reset ()
3095 __git_has_doubledash && return
3097 case "$cur" in
3098 --*)
3099 __gitcomp_builtin reset
3100 return
3102 esac
3103 __git_complete_refs
3106 _git_restore ()
3108 case "$prev" in
3110 __git_complete_refs
3111 return
3113 esac
3115 case "$cur" in
3116 --conflict=*)
3117 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
3119 --source=*)
3120 __git_complete_refs --cur="${cur##--source=}"
3122 --*)
3123 __gitcomp_builtin restore
3126 if __git_pseudoref_exists HEAD; then
3127 __git_complete_index_file "--modified"
3129 esac
3132 __git_revert_inprogress_options=$__git_sequencer_inprogress_options
3134 _git_revert ()
3136 if __git_pseudoref_exists REVERT_HEAD; then
3137 __gitcomp "$__git_revert_inprogress_options"
3138 return
3140 __git_complete_strategy && return
3141 case "$cur" in
3142 --*)
3143 __gitcomp_builtin revert "" \
3144 "$__git_revert_inprogress_options"
3145 return
3147 esac
3148 __git_complete_refs
3151 _git_rm ()
3153 case "$cur" in
3154 --*)
3155 __gitcomp_builtin rm
3156 return
3158 esac
3160 __git_complete_index_file "--cached"
3163 _git_shortlog ()
3165 __git_has_doubledash && return
3167 case "$cur" in
3168 --*)
3169 __gitcomp "
3170 $__git_log_common_options
3171 $__git_log_shortlog_options
3172 --numbered --summary --email
3174 return
3176 esac
3177 __git_complete_revlist
3180 _git_show ()
3182 __git_has_doubledash && return
3184 case "$cur" in
3185 --pretty=*|--format=*)
3186 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
3187 " "" "${cur#*=}"
3188 return
3190 --diff-algorithm=*)
3191 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
3192 return
3194 --submodule=*)
3195 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
3196 return
3198 --color-moved=*)
3199 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
3200 return
3202 --color-moved-ws=*)
3203 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
3204 return
3206 --ws-error-highlight=*)
3207 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
3208 return
3210 --diff-merges=*)
3211 __gitcomp "$__git_diff_merges_opts" "" "${cur##--diff-merges=}"
3212 return
3214 --*)
3215 __gitcomp "--pretty= --format= --abbrev-commit --no-abbrev-commit
3216 --oneline --show-signature
3217 --expand-tabs --expand-tabs= --no-expand-tabs
3218 $__git_log_show_options
3219 $__git_diff_common_options
3221 return
3223 esac
3224 __git_complete_revlist_file
3227 _git_show_branch ()
3229 case "$cur" in
3230 --*)
3231 __gitcomp_builtin show-branch
3232 return
3234 esac
3235 __git_complete_revlist
3238 __gitcomp_directories ()
3240 local _tmp_dir _tmp_completions _found=0
3242 # Get the directory of the current token; this differs from dirname
3243 # in that it keeps up to the final trailing slash. If no slash found
3244 # that's fine too.
3245 [[ "$cur" =~ .*/ ]]
3246 _tmp_dir=$BASH_REMATCH
3248 # Find possible directory completions, adding trailing '/' characters,
3249 # de-quoting, and handling unusual characters.
3250 while IFS= read -r -d $'\0' c ; do
3251 # If there are directory completions, find ones that start
3252 # with "$cur", the current token, and put those in COMPREPLY
3253 if [[ $c == "$cur"* ]]; then
3254 COMPREPLY+=("$c/")
3255 _found=1
3257 done < <(__git ls-tree -z -d --name-only HEAD $_tmp_dir)
3259 if [[ $_found == 0 ]] && [[ "$cur" =~ /$ ]]; then
3260 # No possible further completions any deeper, so assume we're at
3261 # a leaf directory and just consider it complete
3262 __gitcomp_direct_append "$cur "
3263 elif [[ $_found == 0 ]]; then
3264 # No possible completions found. Avoid falling back to
3265 # bash's default file and directory completion, because all
3266 # valid completions have already been searched and the
3267 # fallbacks can do nothing but mislead. In fact, they can
3268 # mislead in three different ways:
3269 # 1) Fallback file completion makes no sense when asking
3270 # for directory completions, as this function does.
3271 # 2) Fallback directory completion is bad because
3272 # e.g. "/pro" is invalid and should NOT complete to
3273 # "/proc".
3274 # 3) Fallback file/directory completion only completes
3275 # on paths that exist in the current working tree,
3276 # i.e. which are *already* part of their
3277 # sparse-checkout. Thus, normal file and directory
3278 # completion is always useless for "git
3279 # sparse-checkout add" and is also probelmatic for
3280 # "git sparse-checkout set" unless using it to
3281 # strictly narrow the checkout.
3282 COMPREPLY=( "" )
3286 # In non-cone mode, the arguments to {set,add} are supposed to be
3287 # patterns, relative to the toplevel directory. These can be any kind
3288 # of general pattern, like 'subdir/*.c' and we can't complete on all
3289 # of those. However, if the user presses Tab to get tab completion, we
3290 # presume that they are trying to provide a pattern that names a specific
3291 # path.
3292 __gitcomp_slash_leading_paths ()
3294 local dequoted_word pfx="" cur_ toplevel
3296 # Since we are dealing with a sparse-checkout, subdirectories may not
3297 # exist in the local working copy. Therefore, we want to run all
3298 # ls-files commands relative to the repository toplevel.
3299 toplevel="$(git rev-parse --show-toplevel)/"
3301 __git_dequote "$cur"
3303 # If the paths provided by the user already start with '/', then
3304 # they are considered relative to the toplevel of the repository
3305 # already. If they do not start with /, then we need to adjust
3306 # them to start with the appropriate prefix.
3307 case "$cur" in
3309 cur="${cur:1}"
3312 pfx="$(__git rev-parse --show-prefix)"
3313 esac
3315 # Since sparse-index is limited to cone-mode, in non-cone-mode the
3316 # list of valid paths is precisely the cached files in the index.
3318 # NEEDSWORK:
3319 # 1) We probably need to take care of cases where ls-files
3320 # responds with special quoting.
3321 # 2) We probably need to take care of cases where ${cur} has
3322 # some kind of special quoting.
3323 # 3) On top of any quoting from 1 & 2, we have to provide an extra
3324 # level of quoting for any paths that contain a '*', '?', '\',
3325 # '[', ']', or leading '#' or '!' since those will be
3326 # interpreted by sparse-checkout as something other than a
3327 # literal path character.
3328 # Since there are two types of quoting here, this might get really
3329 # complex. For now, just punt on all of this...
3330 completions="$(__git -C "${toplevel}" -c core.quotePath=false \
3331 ls-files --cached -- "${pfx}${cur}*" \
3332 | sed -e s%^%/% -e 's%$% %')"
3333 # Note, above, though that we needed all of the completions to be
3334 # prefixed with a '/', and we want to add a space so that bash
3335 # completion will actually complete an entry and let us move on to
3336 # the next one.
3338 # Return what we've found.
3339 if test -n "$completions"; then
3340 # We found some completions; return them
3341 local IFS=$'\n'
3342 COMPREPLY=($completions)
3343 else
3344 # Do NOT fall back to bash-style all-local-files-and-dirs
3345 # when we find no match. Such options are worse than
3346 # useless:
3347 # 1. "git sparse-checkout add" needs paths that are NOT
3348 # currently in the working copy. "git
3349 # sparse-checkout set" does as well, except in the
3350 # special cases when users are only trying to narrow
3351 # their sparse checkout to a subset of what they
3352 # already have.
3354 # 2. A path like '.config' is ambiguous as to whether
3355 # the user wants all '.config' files throughout the
3356 # tree, or just the one under the current directory.
3357 # It would result in a warning from the
3358 # sparse-checkout command due to this. As such, all
3359 # completions of paths should be prefixed with a
3360 # '/'.
3362 # 3. We don't want paths prefixed with a '/' to
3363 # complete files in the system root directory, we
3364 # want it to complete on files relative to the
3365 # repository root.
3367 # As such, make sure that NO completions are offered rather
3368 # than falling back to bash's default completions.
3369 COMPREPLY=( "" )
3373 _git_sparse_checkout ()
3375 local subcommands="list init set disable add reapply"
3376 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3377 local using_cone=true
3378 if [ -z "$subcommand" ]; then
3379 __gitcomp "$subcommands"
3380 return
3383 case "$subcommand,$cur" in
3384 *,--*)
3385 __gitcomp_builtin sparse-checkout_$subcommand "" "--"
3387 set,*|add,*)
3388 if [[ "$(__git config core.sparseCheckout)" == "true" &&
3389 "$(__git config core.sparseCheckoutCone)" == "false" &&
3390 -z "$(__git_find_on_cmdline --cone)" ]]; then
3391 using_cone=false
3393 if [[ -n "$(__git_find_on_cmdline --no-cone)" ]]; then
3394 using_cone=false
3396 if [[ "$using_cone" == "true" ]]; then
3397 __gitcomp_directories
3398 else
3399 __gitcomp_slash_leading_paths
3401 esac
3404 _git_stash ()
3406 local subcommands='push list show apply clear drop pop create branch'
3407 local subcommand="$(__git_find_on_cmdline "$subcommands save")"
3409 if [ -z "$subcommand" ]; then
3410 case "$((cword - __git_cmd_idx)),$cur" in
3411 *,--*)
3412 __gitcomp_builtin stash_push
3414 1,sa*)
3415 __gitcomp "save"
3417 1,*)
3418 __gitcomp "$subcommands"
3420 esac
3421 return
3424 case "$subcommand,$cur" in
3425 list,--*)
3426 # NEEDSWORK: can we somehow unify this with the options in _git_log() and _git_show()
3427 __gitcomp_builtin stash_list "$__git_log_common_options $__git_diff_common_options"
3429 show,--*)
3430 __gitcomp_builtin stash_show "$__git_diff_common_options"
3432 *,--*)
3433 __gitcomp_builtin "stash_$subcommand"
3435 branch,*)
3436 if [ $cword -eq $((__git_cmd_idx+2)) ]; then
3437 __git_complete_refs
3438 else
3439 __gitcomp_nl "$(__git stash list \
3440 | sed -n -e 's/:.*//p')"
3443 show,*|apply,*|drop,*|pop,*)
3444 __gitcomp_nl "$(__git stash list \
3445 | sed -n -e 's/:.*//p')"
3447 esac
3450 _git_submodule ()
3452 __git_has_doubledash && return
3454 local subcommands="add status init deinit update set-branch set-url summary foreach sync absorbgitdirs"
3455 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3456 if [ -z "$subcommand" ]; then
3457 case "$cur" in
3458 --*)
3459 __gitcomp "--quiet"
3462 __gitcomp "$subcommands"
3464 esac
3465 return
3468 case "$subcommand,$cur" in
3469 add,--*)
3470 __gitcomp "--branch --force --name --reference --depth"
3472 status,--*)
3473 __gitcomp "--cached --recursive"
3475 deinit,--*)
3476 __gitcomp "--force --all"
3478 update,--*)
3479 __gitcomp "
3480 --init --remote --no-fetch
3481 --recommend-shallow --no-recommend-shallow
3482 --force --rebase --merge --reference --depth --recursive --jobs
3485 set-branch,--*)
3486 __gitcomp "--default --branch"
3488 summary,--*)
3489 __gitcomp "--cached --files --summary-limit"
3491 foreach,--*|sync,--*)
3492 __gitcomp "--recursive"
3496 esac
3499 _git_svn ()
3501 local subcommands="
3502 init fetch clone rebase dcommit log find-rev
3503 set-tree commit-diff info create-ignore propget
3504 proplist show-ignore show-externals branch tag blame
3505 migrate mkdirs reset gc
3507 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3508 if [ -z "$subcommand" ]; then
3509 __gitcomp "$subcommands"
3510 else
3511 local remote_opts="--username= --config-dir= --no-auth-cache"
3512 local fc_opts="
3513 --follow-parent --authors-file= --repack=
3514 --no-metadata --use-svm-props --use-svnsync-props
3515 --log-window-size= --no-checkout --quiet
3516 --repack-flags --use-log-author --localtime
3517 --add-author-from
3518 --recursive
3519 --ignore-paths= --include-paths= $remote_opts
3521 local init_opts="
3522 --template= --shared= --trunk= --tags=
3523 --branches= --stdlayout --minimize-url
3524 --no-metadata --use-svm-props --use-svnsync-props
3525 --rewrite-root= --prefix= $remote_opts
3527 local cmt_opts="
3528 --edit --rmdir --find-copies-harder --copy-similarity=
3531 case "$subcommand,$cur" in
3532 fetch,--*)
3533 __gitcomp "--revision= --fetch-all $fc_opts"
3535 clone,--*)
3536 __gitcomp "--revision= $fc_opts $init_opts"
3538 init,--*)
3539 __gitcomp "$init_opts"
3541 dcommit,--*)
3542 __gitcomp "
3543 --merge --strategy= --verbose --dry-run
3544 --fetch-all --no-rebase --commit-url
3545 --revision --interactive $cmt_opts $fc_opts
3548 set-tree,--*)
3549 __gitcomp "--stdin $cmt_opts $fc_opts"
3551 create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
3552 show-externals,--*|mkdirs,--*)
3553 __gitcomp "--revision="
3555 log,--*)
3556 __gitcomp "
3557 --limit= --revision= --verbose --incremental
3558 --oneline --show-commit --non-recursive
3559 --authors-file= --color
3562 rebase,--*)
3563 __gitcomp "
3564 --merge --verbose --strategy= --local
3565 --fetch-all --dry-run $fc_opts
3568 commit-diff,--*)
3569 __gitcomp "--message= --file= --revision= $cmt_opts"
3571 info,--*)
3572 __gitcomp "--url"
3574 branch,--*)
3575 __gitcomp "--dry-run --message --tag"
3577 tag,--*)
3578 __gitcomp "--dry-run --message"
3580 blame,--*)
3581 __gitcomp "--git-format"
3583 migrate,--*)
3584 __gitcomp "
3585 --config-dir= --ignore-paths= --minimize
3586 --no-auth-cache --username=
3589 reset,--*)
3590 __gitcomp "--revision= --parent"
3594 esac
3598 _git_symbolic_ref () {
3599 case "$cur" in
3600 --*)
3601 __gitcomp_builtin symbolic-ref
3602 return
3604 esac
3606 __git_complete_refs
3609 _git_tag ()
3611 local i c="$__git_cmd_idx" f=0
3612 while [ $c -lt $cword ]; do
3613 i="${words[c]}"
3614 case "$i" in
3615 -d|--delete|-v|--verify)
3616 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3617 return
3622 esac
3623 ((c++))
3624 done
3626 case "$prev" in
3627 -m|-F)
3629 -*|tag)
3630 if [ $f = 1 ]; then
3631 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3635 __git_complete_refs
3637 esac
3639 case "$cur" in
3640 --*)
3641 __gitcomp_builtin tag
3643 esac
3646 _git_whatchanged ()
3648 _git_log
3651 __git_complete_worktree_paths ()
3653 local IFS=$'\n'
3654 # Generate completion reply from worktree list skipping the first
3655 # entry: it's the path of the main worktree, which can't be moved,
3656 # removed, locked, etc.
3657 __gitcomp_nl "$(__git worktree list --porcelain |
3658 sed -n -e '2,$ s/^worktree //p')"
3661 _git_worktree ()
3663 local subcommands="add list lock move prune remove unlock"
3664 local subcommand subcommand_idx
3666 subcommand="$(__git_find_on_cmdline --show-idx "$subcommands")"
3667 subcommand_idx="${subcommand% *}"
3668 subcommand="${subcommand#* }"
3670 case "$subcommand,$cur" in
3672 __gitcomp "$subcommands"
3674 *,--*)
3675 __gitcomp_builtin worktree_$subcommand
3677 add,*) # usage: git worktree add [<options>] <path> [<commit-ish>]
3678 # Here we are not completing an --option, it's either the
3679 # path or a ref.
3680 case "$prev" in
3681 -b|-B) # Complete refs for branch to be created/reseted.
3682 __git_complete_refs
3684 -*) # The previous word is an -o|--option without an
3685 # unstuck argument: have to complete the path for
3686 # the new worktree, so don't list anything, but let
3687 # Bash fall back to filename completion.
3689 *) # The previous word is not an --option, so it must
3690 # be either the 'add' subcommand, the unstuck
3691 # argument of an option (e.g. branch for -b|-B), or
3692 # the path for the new worktree.
3693 if [ $cword -eq $((subcommand_idx+1)) ]; then
3694 # Right after the 'add' subcommand: have to
3695 # complete the path, so fall back to Bash
3696 # filename completion.
3698 else
3699 case "${words[cword-2]}" in
3700 -b|-B) # After '-b <branch>': have to
3701 # complete the path, so fall back
3702 # to Bash filename completion.
3704 *) # After the path: have to complete
3705 # the ref to be checked out.
3706 __git_complete_refs
3708 esac
3711 esac
3713 lock,*|remove,*|unlock,*)
3714 __git_complete_worktree_paths
3716 move,*)
3717 if [ $cword -eq $((subcommand_idx+1)) ]; then
3718 # The first parameter must be an existing working
3719 # tree to be moved.
3720 __git_complete_worktree_paths
3721 else
3722 # The second parameter is the destination: it could
3723 # be any path, so don't list anything, but let Bash
3724 # fall back to filename completion.
3728 esac
3731 __git_complete_common () {
3732 local command="$1"
3734 case "$cur" in
3735 --*)
3736 __gitcomp_builtin "$command"
3738 esac
3741 __git_cmds_with_parseopt_helper=
3742 __git_support_parseopt_helper () {
3743 test -n "$__git_cmds_with_parseopt_helper" ||
3744 __git_cmds_with_parseopt_helper="$(__git --list-cmds=parseopt)"
3746 case " $__git_cmds_with_parseopt_helper " in
3747 *" $1 "*)
3748 return 0
3751 return 1
3753 esac
3756 __git_have_func () {
3757 declare -f -- "$1" >/dev/null 2>&1
3760 __git_complete_command () {
3761 local command="$1"
3762 local completion_func="_git_${command//-/_}"
3763 if ! __git_have_func $completion_func &&
3764 __git_have_func _completion_loader
3765 then
3766 _completion_loader "git-$command"
3768 if __git_have_func $completion_func
3769 then
3770 $completion_func
3771 return 0
3772 elif __git_support_parseopt_helper "$command"
3773 then
3774 __git_complete_common "$command"
3775 return 0
3776 else
3777 return 1
3781 __git_main ()
3783 local i c=1 command __git_dir __git_repo_path
3784 local __git_C_args C_args_count=0
3785 local __git_cmd_idx
3787 while [ $c -lt $cword ]; do
3788 i="${words[c]}"
3789 case "$i" in
3790 --git-dir=*)
3791 __git_dir="${i#--git-dir=}"
3793 --git-dir)
3794 ((c++))
3795 __git_dir="${words[c]}"
3797 --bare)
3798 __git_dir="."
3800 --help)
3801 command="help"
3802 break
3804 -c|--work-tree|--namespace)
3805 ((c++))
3808 __git_C_args[C_args_count++]=-C
3809 ((c++))
3810 __git_C_args[C_args_count++]="${words[c]}"
3815 command="$i"
3816 __git_cmd_idx="$c"
3817 break
3819 esac
3820 ((c++))
3821 done
3823 if [ -z "${command-}" ]; then
3824 case "$prev" in
3825 --git-dir|-C|--work-tree)
3826 # these need a path argument, let's fall back to
3827 # Bash filename completion
3828 return
3831 __git_complete_config_variable_name_and_value
3832 return
3834 --namespace)
3835 # we don't support completing these options' arguments
3836 return
3838 esac
3839 case "$cur" in
3840 --*)
3841 __gitcomp "
3842 --paginate
3843 --no-pager
3844 --git-dir=
3845 --bare
3846 --version
3847 --exec-path
3848 --exec-path=
3849 --html-path
3850 --man-path
3851 --info-path
3852 --work-tree=
3853 --namespace=
3854 --no-replace-objects
3855 --help
3859 if test -n "${GIT_TESTING_PORCELAIN_COMMAND_LIST-}"
3860 then
3861 __gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
3862 else
3863 local list_cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config
3865 if test "${GIT_COMPLETION_SHOW_ALL_COMMANDS-}" = "1"
3866 then
3867 list_cmds=builtins,$list_cmds
3869 __gitcomp "$(__git --list-cmds=$list_cmds)"
3872 esac
3873 return
3876 __git_complete_command "$command" && return
3878 local expansion=$(__git_aliased_command "$command")
3879 if [ -n "$expansion" ]; then
3880 words[1]=$expansion
3881 __git_complete_command "$expansion"
3885 __gitk_main ()
3887 __git_has_doubledash && return
3889 local __git_repo_path
3890 __git_find_repo_path
3892 local merge=""
3893 if __git_pseudoref_exists MERGE_HEAD; then
3894 merge="--merge"
3896 case "$cur" in
3897 --*)
3898 __gitcomp "
3899 $__git_log_common_options
3900 $__git_log_gitk_options
3901 $merge
3903 return
3905 esac
3906 __git_complete_revlist
3909 if [[ -n ${ZSH_VERSION-} && -z ${GIT_SOURCING_ZSH_COMPLETION-} ]]; then
3910 echo "ERROR: this script is obsolete, please see git-completion.zsh" 1>&2
3911 return
3914 __git_func_wrap ()
3916 local cur words cword prev
3917 local __git_cmd_idx=0
3918 _get_comp_words_by_ref -n =: cur words cword prev
3922 ___git_complete ()
3924 local wrapper="__git_wrap${2}"
3925 eval "$wrapper () { __git_func_wrap $2 ; }"
3926 complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
3927 || complete -o default -o nospace -F $wrapper $1
3930 # Setup the completion for git commands
3931 # 1: command or alias
3932 # 2: function to call (e.g. `git`, `gitk`, `git_fetch`)
3933 __git_complete ()
3935 local func
3937 if __git_have_func $2; then
3938 func=$2
3939 elif __git_have_func __$2_main; then
3940 func=__$2_main
3941 elif __git_have_func _$2; then
3942 func=_$2
3943 else
3944 echo "ERROR: could not find function '$2'" 1>&2
3945 return 1
3947 ___git_complete $1 $func
3950 ___git_complete git __git_main
3951 ___git_complete gitk __gitk_main
3953 # The following are necessary only for Cygwin, and only are needed
3954 # when the user has tab-completed the executable name and consequently
3955 # included the '.exe' suffix.
3957 if [ "$OSTYPE" = cygwin ]; then
3958 ___git_complete git.exe __git_main