Merge branch 'pb/complete-config'
[git/gitster.git] / contrib / completion / git-completion.bash
blob444b3efa6303396acabe67a997fbf6a57fe0bcdb
1 # bash/zsh completion support for core Git.
3 # Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
4 # Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
5 # Distributed under the GNU General Public License, version 2.0.
7 # The contained completion routines provide support for completing:
9 # *) local and remote branch names
10 # *) local and remote tag names
11 # *) .git/remotes file names
12 # *) git 'subcommands'
13 # *) git email aliases for git-send-email
14 # *) tree paths within 'ref:path/to/file' expressions
15 # *) file paths within current working directory and index
16 # *) common --long-options
18 # To use these routines:
20 # 1) Copy this file to somewhere (e.g. ~/.git-completion.bash).
21 # 2) Add the following line to your .bashrc/.zshrc:
22 # source ~/.git-completion.bash
23 # 3) Consider changing your PS1 to also show the current branch,
24 # see git-prompt.sh for details.
26 # If you use complex aliases of form '!f() { ... }; f', you can use the null
27 # command ':' as the first command in the function body to declare the desired
28 # completion style. For example '!f() { : git commit ; ... }; f' will
29 # tell the completion to use commit completion. This also works with aliases
30 # of form "!sh -c '...'". For example, "!sh -c ': git commit ; ... '".
31 # Note that "git" is optional --- '!f() { : commit; ...}; f' would complete
32 # just like the 'git commit' command.
34 # If you have a command that is not part of git, but you would still
35 # like completion, you can use __git_complete:
37 # __git_complete gl git_log
39 # Or if it's a main command (i.e. git or gitk):
41 # __git_complete gk gitk
43 # Compatible with bash 3.2.57.
45 # You can set the following environment variables to influence the behavior of
46 # the completion routines:
48 # GIT_COMPLETION_CHECKOUT_NO_GUESS
50 # When set to "1", do not include "DWIM" suggestions in git-checkout
51 # and git-switch completion (e.g., completing "foo" when "origin/foo"
52 # exists).
54 # GIT_COMPLETION_SHOW_ALL_COMMANDS
56 # When set to "1" suggest all commands, including plumbing commands
57 # which are hidden by default (e.g. "cat-file" on "git ca<TAB>").
59 # GIT_COMPLETION_SHOW_ALL
61 # When set to "1" suggest all options, including options which are
62 # typically hidden (e.g. '--allow-empty' for 'git commit').
64 # GIT_COMPLETION_IGNORE_CASE
66 # When set, uses for-each-ref '--ignore-case' to find refs that match
67 # case insensitively, even on systems with case sensitive file systems
68 # (e.g., completing tag name "FOO" on "git checkout f<TAB>").
70 case "$COMP_WORDBREAKS" in
71 *:*) : great ;;
72 *) COMP_WORDBREAKS="$COMP_WORDBREAKS:"
73 esac
75 # Discovers the path to the git repository taking any '--git-dir=<path>' and
76 # '-C <path>' options into account and stores it in the $__git_repo_path
77 # variable.
78 __git_find_repo_path ()
80 if [ -n "${__git_repo_path-}" ]; then
81 # we already know where it is
82 return
85 if [ -n "${__git_C_args-}" ]; then
86 __git_repo_path="$(git "${__git_C_args[@]}" \
87 ${__git_dir:+--git-dir="$__git_dir"} \
88 rev-parse --absolute-git-dir 2>/dev/null)"
89 elif [ -n "${__git_dir-}" ]; then
90 test -d "$__git_dir" &&
91 __git_repo_path="$__git_dir"
92 elif [ -n "${GIT_DIR-}" ]; then
93 test -d "$GIT_DIR" &&
94 __git_repo_path="$GIT_DIR"
95 elif [ -d .git ]; then
96 __git_repo_path=.git
97 else
98 __git_repo_path="$(git rev-parse --git-dir 2>/dev/null)"
102 # Deprecated: use __git_find_repo_path() and $__git_repo_path instead
103 # __gitdir accepts 0 or 1 arguments (i.e., location)
104 # returns location of .git repo
105 __gitdir ()
107 if [ -z "${1-}" ]; then
108 __git_find_repo_path || return 1
109 echo "$__git_repo_path"
110 elif [ -d "$1/.git" ]; then
111 echo "$1/.git"
112 else
113 echo "$1"
117 # Runs git with all the options given as argument, respecting any
118 # '--git-dir=<path>' and '-C <path>' options present on the command line
119 __git ()
121 git ${__git_C_args:+"${__git_C_args[@]}"} \
122 ${__git_dir:+--git-dir="$__git_dir"} "$@" 2>/dev/null
125 # Helper function to read the first line of a file into a variable.
126 # __git_eread requires 2 arguments, the file path and the name of the
127 # variable, in that order.
129 # This is taken from git-prompt.sh.
130 __git_eread ()
132 test -r "$1" && IFS=$'\r\n' read -r "$2" <"$1"
135 # Runs git in $__git_repo_path to determine whether a pseudoref exists.
136 # 1: The pseudo-ref to search
137 __git_pseudoref_exists ()
139 local ref=$1
140 local head
142 __git_find_repo_path
144 # If the reftable is in use, we have to shell out to 'git rev-parse'
145 # to determine whether the ref exists instead of looking directly in
146 # the filesystem to determine whether the ref exists. Otherwise, use
147 # Bash builtins since executing Git commands are expensive on some
148 # platforms.
149 if __git_eread "$__git_repo_path/HEAD" head; then
150 if [ "$head" == "ref: refs/heads/.invalid" ]; then
151 __git show-ref --exists "$ref"
152 return $?
156 [ -f "$__git_repo_path/$ref" ]
159 # Removes backslash escaping, single quotes and double quotes from a word,
160 # stores the result in the variable $dequoted_word.
161 # 1: The word to dequote.
162 __git_dequote ()
164 local rest="$1" len ch
166 dequoted_word=""
168 while test -n "$rest"; do
169 len=${#dequoted_word}
170 dequoted_word="$dequoted_word${rest%%[\\\'\"]*}"
171 rest="${rest:$((${#dequoted_word}-$len))}"
173 case "${rest:0:1}" in
175 ch="${rest:1:1}"
176 case "$ch" in
177 $'\n')
180 dequoted_word="$dequoted_word$ch"
182 esac
183 rest="${rest:2}"
186 rest="${rest:1}"
187 len=${#dequoted_word}
188 dequoted_word="$dequoted_word${rest%%\'*}"
189 rest="${rest:$((${#dequoted_word}-$len+1))}"
192 rest="${rest:1}"
193 while test -n "$rest" ; do
194 len=${#dequoted_word}
195 dequoted_word="$dequoted_word${rest%%[\\\"]*}"
196 rest="${rest:$((${#dequoted_word}-$len))}"
197 case "${rest:0:1}" in
199 ch="${rest:1:1}"
200 case "$ch" in
201 \"|\\|\$|\`)
202 dequoted_word="$dequoted_word$ch"
204 $'\n')
207 dequoted_word="$dequoted_word\\$ch"
209 esac
210 rest="${rest:2}"
213 rest="${rest:1}"
214 break
216 esac
217 done
219 esac
220 done
223 # The following function is based on code from:
225 # bash_completion - programmable completion functions for bash 3.2+
227 # Copyright © 2006-2008, Ian Macdonald <ian@caliban.org>
228 # © 2009-2010, Bash Completion Maintainers
229 # <bash-completion-devel@lists.alioth.debian.org>
231 # This program is free software; you can redistribute it and/or modify
232 # it under the terms of the GNU General Public License as published by
233 # the Free Software Foundation; either version 2, or (at your option)
234 # any later version.
236 # This program is distributed in the hope that it will be useful,
237 # but WITHOUT ANY WARRANTY; without even the implied warranty of
238 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
239 # GNU General Public License for more details.
241 # You should have received a copy of the GNU General Public License
242 # along with this program; if not, see <http://www.gnu.org/licenses/>.
244 # The latest version of this software can be obtained here:
246 # http://bash-completion.alioth.debian.org/
248 # RELEASE: 2.x
250 # This function can be used to access a tokenized list of words
251 # on the command line:
253 # __git_reassemble_comp_words_by_ref '=:'
254 # if test "${words_[cword_-1]}" = -w
255 # then
256 # ...
257 # fi
259 # The argument should be a collection of characters from the list of
260 # word completion separators (COMP_WORDBREAKS) to treat as ordinary
261 # characters.
263 # This is roughly equivalent to going back in time and setting
264 # COMP_WORDBREAKS to exclude those characters. The intent is to
265 # make option types like --date=<type> and <rev>:<path> easy to
266 # recognize by treating each shell word as a single token.
268 # It is best not to set COMP_WORDBREAKS directly because the value is
269 # shared with other completion scripts. By the time the completion
270 # function gets called, COMP_WORDS has already been populated so local
271 # changes to COMP_WORDBREAKS have no effect.
273 # Output: words_, cword_, cur_.
275 __git_reassemble_comp_words_by_ref()
277 local exclude i j first
278 # Which word separators to exclude?
279 exclude="${1//[^$COMP_WORDBREAKS]}"
280 cword_=$COMP_CWORD
281 if [ -z "$exclude" ]; then
282 words_=("${COMP_WORDS[@]}")
283 return
285 # List of word completion separators has shrunk;
286 # re-assemble words to complete.
287 for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
288 # Append each nonempty word consisting of just
289 # word separator characters to the current word.
290 first=t
291 while
292 [ $i -gt 0 ] &&
293 [ -n "${COMP_WORDS[$i]}" ] &&
294 # word consists of excluded word separators
295 [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
297 # Attach to the previous token,
298 # unless the previous token is the command name.
299 if [ $j -ge 2 ] && [ -n "$first" ]; then
300 ((j--))
302 first=
303 words_[$j]=${words_[j]}${COMP_WORDS[i]}
304 if [ $i = $COMP_CWORD ]; then
305 cword_=$j
307 if (($i < ${#COMP_WORDS[@]} - 1)); then
308 ((i++))
309 else
310 # Done.
311 return
313 done
314 words_[$j]=${words_[j]}${COMP_WORDS[i]}
315 if [ $i = $COMP_CWORD ]; then
316 cword_=$j
318 done
321 if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
322 _get_comp_words_by_ref ()
324 local exclude cur_ words_ cword_
325 if [ "$1" = "-n" ]; then
326 exclude=$2
327 shift 2
329 __git_reassemble_comp_words_by_ref "$exclude"
330 cur_=${words_[cword_]}
331 while [ $# -gt 0 ]; do
332 case "$1" in
333 cur)
334 cur=$cur_
336 prev)
337 prev=${words_[$cword_-1]}
339 words)
340 words=("${words_[@]}")
342 cword)
343 cword=$cword_
345 esac
346 shift
347 done
351 # Fills the COMPREPLY array with prefiltered words without any additional
352 # processing.
353 # Callers must take care of providing only words that match the current word
354 # to be completed and adding any prefix and/or suffix (trailing space!), if
355 # necessary.
356 # 1: List of newline-separated matching completion words, complete with
357 # prefix and suffix.
358 __gitcomp_direct ()
360 local IFS=$'\n'
362 COMPREPLY=($1)
365 # Similar to __gitcomp_direct, but appends to COMPREPLY instead.
366 # Callers must take care of providing only words that match the current word
367 # to be completed and adding any prefix and/or suffix (trailing space!), if
368 # necessary.
369 # 1: List of newline-separated matching completion words, complete with
370 # prefix and suffix.
371 __gitcomp_direct_append ()
373 local IFS=$'\n'
375 COMPREPLY+=($1)
378 __gitcompappend ()
380 local x i=${#COMPREPLY[@]}
381 for x in $1; do
382 if [[ "$x" == "$3"* ]]; then
383 COMPREPLY[i++]="$2$x$4"
385 done
388 __gitcompadd ()
390 COMPREPLY=()
391 __gitcompappend "$@"
394 # Generates completion reply, appending a space to possible completion words,
395 # if necessary.
396 # It accepts 1 to 4 arguments:
397 # 1: List of possible completion words.
398 # 2: A prefix to be added to each possible completion word (optional).
399 # 3: Generate possible completion matches for this word (optional).
400 # 4: A suffix to be appended to each possible completion word (optional).
401 __gitcomp ()
403 local cur_="${3-$cur}"
405 case "$cur_" in
408 --no-*)
409 local c i=0 IFS=$' \t\n'
410 for c in $1; do
411 if [[ $c == "--" ]]; then
412 continue
414 c="$c${4-}"
415 if [[ $c == "$cur_"* ]]; then
416 case $c in
417 --*=|*.) ;;
418 *) c="$c " ;;
419 esac
420 COMPREPLY[i++]="${2-}$c"
422 done
425 local c i=0 IFS=$' \t\n'
426 for c in $1; do
427 if [[ $c == "--" ]]; then
428 c="--no-...${4-}"
429 if [[ $c == "$cur_"* ]]; then
430 COMPREPLY[i++]="${2-}$c "
432 break
434 c="$c${4-}"
435 if [[ $c == "$cur_"* ]]; then
436 case $c in
437 *=|*.) ;;
438 *) c="$c " ;;
439 esac
440 COMPREPLY[i++]="${2-}$c"
442 done
444 esac
447 # Clear the variables caching builtins' options when (re-)sourcing
448 # the completion script.
449 if [[ -n ${ZSH_VERSION-} ]]; then
450 unset ${(M)${(k)parameters[@]}:#__gitcomp_builtin_*} 2>/dev/null
451 else
452 unset $(compgen -v __gitcomp_builtin_)
455 # This function is equivalent to
457 # __gitcomp "$(git xxx --git-completion-helper) ..."
459 # except that the output is cached. Accept 1-3 arguments:
460 # 1: the git command to execute, this is also the cache key
461 # 2: extra options to be added on top (e.g. negative forms)
462 # 3: options to be excluded
463 __gitcomp_builtin ()
465 # spaces must be replaced with underscore for multi-word
466 # commands, e.g. "git remote add" becomes remote_add.
467 local cmd="$1"
468 local incl="${2-}"
469 local excl="${3-}"
471 local var=__gitcomp_builtin_"${cmd//-/_}"
472 local options
473 eval "options=\${$var-}"
475 if [ -z "$options" ]; then
476 local completion_helper
477 if [ "${GIT_COMPLETION_SHOW_ALL-}" = "1" ]; then
478 completion_helper="--git-completion-helper-all"
479 else
480 completion_helper="--git-completion-helper"
482 # leading and trailing spaces are significant to make
483 # option removal work correctly.
484 options=" $incl $(__git ${cmd/_/ } $completion_helper) " || return
486 for i in $excl; do
487 options="${options/ $i / }"
488 done
489 eval "$var=\"$options\""
492 __gitcomp "$options"
495 # Variation of __gitcomp_nl () that appends to the existing list of
496 # completion candidates, COMPREPLY.
497 __gitcomp_nl_append ()
499 local IFS=$'\n'
500 __gitcompappend "$1" "${2-}" "${3-$cur}" "${4- }"
503 # Generates completion reply from newline-separated possible completion words
504 # by appending a space to all of them.
505 # It accepts 1 to 4 arguments:
506 # 1: List of possible completion words, separated by a single newline.
507 # 2: A prefix to be added to each possible completion word (optional).
508 # 3: Generate possible completion matches for this word (optional).
509 # 4: A suffix to be appended to each possible completion word instead of
510 # the default space (optional). If specified but empty, nothing is
511 # appended.
512 __gitcomp_nl ()
514 COMPREPLY=()
515 __gitcomp_nl_append "$@"
518 # Fills the COMPREPLY array with prefiltered paths without any additional
519 # processing.
520 # Callers must take care of providing only paths that match the current path
521 # to be completed and adding any prefix path components, if necessary.
522 # 1: List of newline-separated matching paths, complete with all prefix
523 # path components.
524 __gitcomp_file_direct ()
526 local IFS=$'\n'
528 COMPREPLY=($1)
530 # use a hack to enable file mode in bash < 4
531 compopt -o filenames +o nospace 2>/dev/null ||
532 compgen -f /non-existing-dir/ >/dev/null ||
533 true
536 # Generates completion reply with compgen from newline-separated possible
537 # completion filenames.
538 # It accepts 1 to 3 arguments:
539 # 1: List of possible completion filenames, separated by a single newline.
540 # 2: A directory prefix to be added to each possible completion filename
541 # (optional).
542 # 3: Generate possible completion matches for this word (optional).
543 __gitcomp_file ()
545 local IFS=$'\n'
547 # XXX does not work when the directory prefix contains a tilde,
548 # since tilde expansion is not applied.
549 # This means that COMPREPLY will be empty and Bash default
550 # completion will be used.
551 __gitcompadd "$1" "${2-}" "${3-$cur}" ""
553 # use a hack to enable file mode in bash < 4
554 compopt -o filenames +o nospace 2>/dev/null ||
555 compgen -f /non-existing-dir/ >/dev/null ||
556 true
559 # Execute 'git ls-files', unless the --committable option is specified, in
560 # which case it runs 'git diff-index' to find out the files that can be
561 # committed. It return paths relative to the directory specified in the first
562 # argument, and using the options specified in the second argument.
563 __git_ls_files_helper ()
565 if [ "$2" = "--committable" ]; then
566 __git -C "$1" -c core.quotePath=false diff-index \
567 --name-only --relative HEAD -- "${3//\\/\\\\}*"
568 else
569 # NOTE: $2 is not quoted in order to support multiple options
570 __git -C "$1" -c core.quotePath=false ls-files \
571 --exclude-standard $2 -- "${3//\\/\\\\}*"
576 # __git_index_files accepts 1 or 2 arguments:
577 # 1: Options to pass to ls-files (required).
578 # 2: A directory path (optional).
579 # If provided, only files within the specified directory are listed.
580 # Sub directories are never recursed. Path must have a trailing
581 # slash.
582 # 3: List only paths matching this path component (optional).
583 __git_index_files ()
585 local root="$2" match="$3"
587 __git_ls_files_helper "$root" "$1" "${match:-?}" |
588 awk -F / -v pfx="${2//\\/\\\\}" '{
589 paths[$1] = 1
591 END {
592 for (p in paths) {
593 if (substr(p, 1, 1) != "\"") {
594 # No special characters, easy!
595 print pfx p
596 continue
599 # The path is quoted.
600 p = dequote(p)
601 if (p == "")
602 continue
604 # Even when a directory name itself does not contain
605 # any special characters, it will still be quoted if
606 # any of its (stripped) trailing path components do.
607 # Because of this we may have seen the same directory
608 # both quoted and unquoted.
609 if (p in paths)
610 # We have seen the same directory unquoted,
611 # skip it.
612 continue
613 else
614 print pfx p
617 function dequote(p, bs_idx, out, esc, esc_idx, dec) {
618 # Skip opening double quote.
619 p = substr(p, 2)
621 # Interpret backslash escape sequences.
622 while ((bs_idx = index(p, "\\")) != 0) {
623 out = out substr(p, 1, bs_idx - 1)
624 esc = substr(p, bs_idx + 1, 1)
625 p = substr(p, bs_idx + 2)
627 if ((esc_idx = index("abtvfr\"\\", esc)) != 0) {
628 # C-style one-character escape sequence.
629 out = out substr("\a\b\t\v\f\r\"\\",
630 esc_idx, 1)
631 } else if (esc == "n") {
632 # Uh-oh, a newline character.
633 # We cannot reliably put a pathname
634 # containing a newline into COMPREPLY,
635 # and the newline would create a mess.
636 # Skip this path.
637 return ""
638 } else {
639 # Must be a \nnn octal value, then.
640 dec = esc * 64 + \
641 substr(p, 1, 1) * 8 + \
642 substr(p, 2, 1)
643 out = out sprintf("%c", dec)
644 p = substr(p, 3)
647 # Drop closing double quote, if there is one.
648 # (There is not any if this is a directory, as it was
649 # already stripped with the trailing path components.)
650 if (substr(p, length(p), 1) == "\"")
651 out = out substr(p, 1, length(p) - 1)
652 else
653 out = out p
655 return out
659 # __git_complete_index_file requires 1 argument:
660 # 1: the options to pass to ls-file
662 # The exception is --committable, which finds the files appropriate commit.
663 __git_complete_index_file ()
665 local dequoted_word pfx="" cur_
667 __git_dequote "$cur"
669 case "$dequoted_word" in
670 ?*/*)
671 pfx="${dequoted_word%/*}/"
672 cur_="${dequoted_word##*/}"
675 cur_="$dequoted_word"
676 esac
678 __gitcomp_file_direct "$(__git_index_files "$1" "$pfx" "$cur_")"
681 # Lists branches from the local repository.
682 # 1: A prefix to be added to each listed branch (optional).
683 # 2: List only branches matching this word (optional; list all branches if
684 # unset or empty).
685 # 3: A suffix to be appended to each listed branch (optional).
686 __git_heads ()
688 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
690 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
691 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
692 "refs/heads/$cur_*" "refs/heads/$cur_*/**"
695 # Lists branches from remote repositories.
696 # 1: A prefix to be added to each listed branch (optional).
697 # 2: List only branches matching this word (optional; list all branches if
698 # unset or empty).
699 # 3: A suffix to be appended to each listed branch (optional).
700 __git_remote_heads ()
702 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
704 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
705 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
706 "refs/remotes/$cur_*" "refs/remotes/$cur_*/**"
709 # Lists tags from the local repository.
710 # Accepts the same positional parameters as __git_heads() above.
711 __git_tags ()
713 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
715 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
716 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
717 "refs/tags/$cur_*" "refs/tags/$cur_*/**"
720 # List unique branches from refs/remotes used for 'git checkout' and 'git
721 # switch' tracking DWIMery.
722 # 1: A prefix to be added to each listed branch (optional)
723 # 2: List only branches matching this word (optional; list all branches if
724 # unset or empty).
725 # 3: A suffix to be appended to each listed branch (optional).
726 __git_dwim_remote_heads ()
728 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
729 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
731 # employ the heuristic used by git checkout and git switch
732 # Try to find a remote branch that cur_es the completion word
733 # but only output if the branch name is unique
734 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
735 --sort="refname:strip=3" \
736 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
737 "refs/remotes/*/$cur_*" "refs/remotes/*/$cur_*/**" | \
738 uniq -u
741 # Lists refs from the local (by default) or from a remote repository.
742 # It accepts 0, 1 or 2 arguments:
743 # 1: The remote to list refs from (optional; ignored, if set but empty).
744 # Can be the name of a configured remote, a path, or a URL.
745 # 2: In addition to local refs, list unique branches from refs/remotes/ for
746 # 'git checkout's tracking DWIMery (optional; ignored, if set but empty).
747 # 3: A prefix to be added to each listed ref (optional).
748 # 4: List only refs matching this word (optional; list all refs if unset or
749 # empty).
750 # 5: A suffix to be appended to each listed ref (optional; ignored, if set
751 # but empty).
753 # Use __git_complete_refs() instead.
754 __git_refs ()
756 local i hash dir track="${2-}"
757 local list_refs_from=path remote="${1-}"
758 local format refs
759 local pfx="${3-}" cur_="${4-$cur}" sfx="${5-}"
760 local match="${4-}"
761 local umatch="${4-}"
762 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
764 __git_find_repo_path
765 dir="$__git_repo_path"
767 if [ -z "$remote" ]; then
768 if [ -z "$dir" ]; then
769 return
771 else
772 if __git_is_configured_remote "$remote"; then
773 # configured remote takes precedence over a
774 # local directory with the same name
775 list_refs_from=remote
776 elif [ -d "$remote/.git" ]; then
777 dir="$remote/.git"
778 elif [ -d "$remote" ]; then
779 dir="$remote"
780 else
781 list_refs_from=url
785 if test "${GIT_COMPLETION_IGNORE_CASE:+1}" = "1"
786 then
787 # uppercase with tr instead of ${match,^^} for bash 3.2 compatibility
788 umatch=$(echo "$match" | tr a-z A-Z 2>/dev/null || echo "$match")
791 if [ "$list_refs_from" = path ]; then
792 if [[ "$cur_" == ^* ]]; then
793 pfx="$pfx^"
794 fer_pfx="$fer_pfx^"
795 cur_=${cur_#^}
796 match=${match#^}
797 umatch=${umatch#^}
799 case "$cur_" in
800 refs|refs/*)
801 format="refname"
802 refs=("$match*" "$match*/**")
803 track=""
806 for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD REBASE_HEAD CHERRY_PICK_HEAD REVERT_HEAD BISECT_HEAD AUTO_MERGE; do
807 case "$i" in
808 $match*|$umatch*)
809 if [ -e "$dir/$i" ]; then
810 echo "$pfx$i$sfx"
813 esac
814 done
815 format="refname:strip=2"
816 refs=("refs/tags/$match*" "refs/tags/$match*/**"
817 "refs/heads/$match*" "refs/heads/$match*/**"
818 "refs/remotes/$match*" "refs/remotes/$match*/**")
820 esac
821 __git_dir="$dir" __git for-each-ref --format="$fer_pfx%($format)$sfx" \
822 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
823 "${refs[@]}"
824 if [ -n "$track" ]; then
825 __git_dwim_remote_heads "$pfx" "$match" "$sfx"
827 return
829 case "$cur_" in
830 refs|refs/*)
831 __git ls-remote "$remote" "$match*" | \
832 while read -r hash i; do
833 case "$i" in
834 *^{}) ;;
835 *) echo "$pfx$i$sfx" ;;
836 esac
837 done
840 if [ "$list_refs_from" = remote ]; then
841 case "HEAD" in
842 $match*|$umatch*) echo "${pfx}HEAD$sfx" ;;
843 esac
844 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
845 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
846 "refs/remotes/$remote/$match*" \
847 "refs/remotes/$remote/$match*/**"
848 else
849 local query_symref
850 case "HEAD" in
851 $match*|$umatch*) query_symref="HEAD" ;;
852 esac
853 __git ls-remote "$remote" $query_symref \
854 "refs/tags/$match*" "refs/heads/$match*" \
855 "refs/remotes/$match*" |
856 while read -r hash i; do
857 case "$i" in
858 *^{}) ;;
859 refs/*) echo "$pfx${i#refs/*/}$sfx" ;;
860 *) echo "$pfx$i$sfx" ;; # symbolic refs
861 esac
862 done
865 esac
868 # Completes refs, short and long, local and remote, symbolic and pseudo.
870 # Usage: __git_complete_refs [<option>]...
871 # --remote=<remote>: The remote to list refs from, can be the name of a
872 # configured remote, a path, or a URL.
873 # --dwim: List unique remote branches for 'git switch's tracking DWIMery.
874 # --pfx=<prefix>: A prefix to be added to each ref.
875 # --cur=<word>: The current ref to be completed. Defaults to the current
876 # word to be completed.
877 # --sfx=<suffix>: A suffix to be appended to each ref instead of the default
878 # space.
879 # --mode=<mode>: What set of refs to complete, one of 'refs' (the default) to
880 # complete all refs, 'heads' to complete only branches, or
881 # 'remote-heads' to complete only remote branches. Note that
882 # --remote is only compatible with --mode=refs.
883 __git_complete_refs ()
885 local remote= dwim= pfx= cur_="$cur" sfx=" " mode="refs"
887 while test $# != 0; do
888 case "$1" in
889 --remote=*) remote="${1##--remote=}" ;;
890 --dwim) dwim="yes" ;;
891 # --track is an old spelling of --dwim
892 --track) dwim="yes" ;;
893 --pfx=*) pfx="${1##--pfx=}" ;;
894 --cur=*) cur_="${1##--cur=}" ;;
895 --sfx=*) sfx="${1##--sfx=}" ;;
896 --mode=*) mode="${1##--mode=}" ;;
897 *) return 1 ;;
898 esac
899 shift
900 done
902 # complete references based on the specified mode
903 case "$mode" in
904 refs)
905 __gitcomp_direct "$(__git_refs "$remote" "" "$pfx" "$cur_" "$sfx")" ;;
906 heads)
907 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" "$sfx")" ;;
908 remote-heads)
909 __gitcomp_direct "$(__git_remote_heads "$pfx" "$cur_" "$sfx")" ;;
911 return 1 ;;
912 esac
914 # Append DWIM remote branch names if requested
915 if [ "$dwim" = "yes" ]; then
916 __gitcomp_direct_append "$(__git_dwim_remote_heads "$pfx" "$cur_" "$sfx")"
920 # __git_refs2 requires 1 argument (to pass to __git_refs)
921 # Deprecated: use __git_complete_fetch_refspecs() instead.
922 __git_refs2 ()
924 local i
925 for i in $(__git_refs "$1"); do
926 echo "$i:$i"
927 done
930 # Completes refspecs for fetching from a remote repository.
931 # 1: The remote repository.
932 # 2: A prefix to be added to each listed refspec (optional).
933 # 3: The ref to be completed as a refspec instead of the current word to be
934 # completed (optional)
935 # 4: A suffix to be appended to each listed refspec instead of the default
936 # space (optional).
937 __git_complete_fetch_refspecs ()
939 local i remote="$1" pfx="${2-}" cur_="${3-$cur}" sfx="${4- }"
941 __gitcomp_direct "$(
942 for i in $(__git_refs "$remote" "" "" "$cur_") ; do
943 echo "$pfx$i:$i$sfx"
944 done
948 # __git_refs_remotes requires 1 argument (to pass to ls-remote)
949 __git_refs_remotes ()
951 local i hash
952 __git ls-remote "$1" 'refs/heads/*' | \
953 while read -r hash i; do
954 echo "$i:refs/remotes/$1/${i#refs/heads/}"
955 done
958 __git_remotes ()
960 __git_find_repo_path
961 test -d "$__git_repo_path/remotes" && ls -1 "$__git_repo_path/remotes"
962 __git remote
965 # Returns true if $1 matches the name of a configured remote, false otherwise.
966 __git_is_configured_remote ()
968 local remote
969 for remote in $(__git_remotes); do
970 if [ "$remote" = "$1" ]; then
971 return 0
973 done
974 return 1
977 __git_list_merge_strategies ()
979 LANG=C LC_ALL=C git merge -s help 2>&1 |
980 sed -n -e '/[Aa]vailable strategies are: /,/^$/{
981 s/\.$//
982 s/.*://
983 s/^[ ]*//
984 s/[ ]*$//
989 __git_merge_strategies=
990 # 'git merge -s help' (and thus detection of the merge strategy
991 # list) fails, unfortunately, if run outside of any git working
992 # tree. __git_merge_strategies is set to the empty string in
993 # that case, and the detection will be repeated the next time it
994 # is needed.
995 __git_compute_merge_strategies ()
997 test -n "$__git_merge_strategies" ||
998 __git_merge_strategies=$(__git_list_merge_strategies)
1001 __git_merge_strategy_options="ours theirs subtree subtree= patience
1002 histogram diff-algorithm= ignore-space-change ignore-all-space
1003 ignore-space-at-eol renormalize no-renormalize no-renames
1004 find-renames find-renames= rename-threshold="
1006 __git_complete_revlist_file ()
1008 local dequoted_word pfx ls ref cur_="$cur"
1009 case "$cur_" in
1010 *..?*:*)
1011 return
1013 ?*:*)
1014 ref="${cur_%%:*}"
1015 cur_="${cur_#*:}"
1017 __git_dequote "$cur_"
1019 case "$dequoted_word" in
1020 ?*/*)
1021 pfx="${dequoted_word%/*}"
1022 cur_="${dequoted_word##*/}"
1023 ls="$ref:$pfx"
1024 pfx="$pfx/"
1027 cur_="$dequoted_word"
1028 ls="$ref"
1030 esac
1032 case "$COMP_WORDBREAKS" in
1033 *:*) : great ;;
1034 *) pfx="$ref:$pfx" ;;
1035 esac
1037 __gitcomp_file "$(__git ls-tree "$ls" \
1038 | sed 's/^.* //
1039 s/$//')" \
1040 "$pfx" "$cur_"
1042 *...*)
1043 pfx="${cur_%...*}..."
1044 cur_="${cur_#*...}"
1045 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1047 *..*)
1048 pfx="${cur_%..*}.."
1049 cur_="${cur_#*..}"
1050 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1053 __git_complete_refs
1055 esac
1058 __git_complete_file ()
1060 __git_complete_revlist_file
1063 __git_complete_revlist ()
1065 __git_complete_revlist_file
1068 __git_complete_remote_or_refspec ()
1070 local cur_="$cur" cmd="${words[__git_cmd_idx]}"
1071 local i c=$((__git_cmd_idx+1)) remote="" pfx="" lhs=1 no_complete_refspec=0
1072 if [ "$cmd" = "remote" ]; then
1073 ((c++))
1075 while [ $c -lt $cword ]; do
1076 i="${words[c]}"
1077 case "$i" in
1078 --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
1079 -d|--delete) [ "$cmd" = "push" ] && lhs=0 ;;
1080 --all)
1081 case "$cmd" in
1082 push) no_complete_refspec=1 ;;
1083 fetch)
1084 return
1086 *) ;;
1087 esac
1089 --multiple) no_complete_refspec=1; break ;;
1090 -*) ;;
1091 *) remote="$i"; break ;;
1092 esac
1093 ((c++))
1094 done
1095 if [ -z "$remote" ]; then
1096 __gitcomp_nl "$(__git_remotes)"
1097 return
1099 if [ $no_complete_refspec = 1 ]; then
1100 return
1102 [ "$remote" = "." ] && remote=
1103 case "$cur_" in
1104 *:*)
1105 case "$COMP_WORDBREAKS" in
1106 *:*) : great ;;
1107 *) pfx="${cur_%%:*}:" ;;
1108 esac
1109 cur_="${cur_#*:}"
1110 lhs=0
1113 pfx="+"
1114 cur_="${cur_#+}"
1116 esac
1117 case "$cmd" in
1118 fetch)
1119 if [ $lhs = 1 ]; then
1120 __git_complete_fetch_refspecs "$remote" "$pfx" "$cur_"
1121 else
1122 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1125 pull|remote)
1126 if [ $lhs = 1 ]; then
1127 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
1128 else
1129 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1132 push)
1133 if [ $lhs = 1 ]; then
1134 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1135 else
1136 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
1139 esac
1142 __git_complete_strategy ()
1144 __git_compute_merge_strategies
1145 case "$prev" in
1146 -s|--strategy)
1147 __gitcomp "$__git_merge_strategies"
1148 return 0
1151 __gitcomp "$__git_merge_strategy_options"
1152 return 0
1154 esac
1155 case "$cur" in
1156 --strategy=*)
1157 __gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
1158 return 0
1160 --strategy-option=*)
1161 __gitcomp "$__git_merge_strategy_options" "" "${cur##--strategy-option=}"
1162 return 0
1164 esac
1165 return 1
1168 __git_all_commands=
1169 __git_compute_all_commands ()
1171 test -n "$__git_all_commands" ||
1172 __git_all_commands=$(__git --list-cmds=main,others,alias,nohelpers)
1175 # Lists all set config variables starting with the given section prefix,
1176 # with the prefix removed.
1177 __git_get_config_variables ()
1179 local section="$1" i IFS=$'\n'
1180 for i in $(__git config --name-only --get-regexp "^$section\..*"); do
1181 echo "${i#$section.}"
1182 done
1185 __git_pretty_aliases ()
1187 __git_get_config_variables "pretty"
1190 # __git_aliased_command requires 1 argument
1191 __git_aliased_command ()
1193 local cur=$1 last list= word cmdline
1195 while [[ -n "$cur" ]]; do
1196 if [[ "$list" == *" $cur "* ]]; then
1197 # loop detected
1198 return
1201 cmdline=$(__git config --get "alias.$cur")
1202 list=" $cur $list"
1203 last=$cur
1204 cur=
1206 for word in $cmdline; do
1207 case "$word" in
1208 \!gitk|gitk)
1209 cur="gitk"
1210 break
1212 \!*) : shell command alias ;;
1213 -*) : option ;;
1214 *=*) : setting env ;;
1215 git) : git itself ;;
1216 \(\)) : skip parens of shell function definition ;;
1217 {) : skip start of shell helper function ;;
1218 :) : skip null command ;;
1219 \'*) : skip opening quote after sh -c ;;
1221 cur="${word%;}"
1222 break
1223 esac
1224 done
1225 done
1227 cur=$last
1228 if [[ "$cur" != "$1" ]]; then
1229 echo "$cur"
1233 # Check whether one of the given words is present on the command line,
1234 # and print the first word found.
1236 # Usage: __git_find_on_cmdline [<option>]... "<wordlist>"
1237 # --show-idx: Optionally show the index of the found word in the $words array.
1238 __git_find_on_cmdline ()
1240 local word c="$__git_cmd_idx" show_idx
1242 while test $# -gt 1; do
1243 case "$1" in
1244 --show-idx) show_idx=y ;;
1245 *) return 1 ;;
1246 esac
1247 shift
1248 done
1249 local wordlist="$1"
1251 while [ $c -lt $cword ]; do
1252 for word in $wordlist; do
1253 if [ "$word" = "${words[c]}" ]; then
1254 if [ -n "${show_idx-}" ]; then
1255 echo "$c $word"
1256 else
1257 echo "$word"
1259 return
1261 done
1262 ((c++))
1263 done
1266 # Similar to __git_find_on_cmdline, except that it loops backwards and thus
1267 # prints the *last* word found. Useful for finding which of two options that
1268 # supersede each other came last, such as "--guess" and "--no-guess".
1270 # Usage: __git_find_last_on_cmdline [<option>]... "<wordlist>"
1271 # --show-idx: Optionally show the index of the found word in the $words array.
1272 __git_find_last_on_cmdline ()
1274 local word c=$cword show_idx
1276 while test $# -gt 1; do
1277 case "$1" in
1278 --show-idx) show_idx=y ;;
1279 *) return 1 ;;
1280 esac
1281 shift
1282 done
1283 local wordlist="$1"
1285 while [ $c -gt "$__git_cmd_idx" ]; do
1286 ((c--))
1287 for word in $wordlist; do
1288 if [ "$word" = "${words[c]}" ]; then
1289 if [ -n "$show_idx" ]; then
1290 echo "$c $word"
1291 else
1292 echo "$word"
1294 return
1296 done
1297 done
1300 # Echo the value of an option set on the command line or config
1302 # $1: short option name
1303 # $2: long option name including =
1304 # $3: list of possible values
1305 # $4: config string (optional)
1307 # example:
1308 # result="$(__git_get_option_value "-d" "--do-something=" \
1309 # "yes no" "core.doSomething")"
1311 # result is then either empty (no option set) or "yes" or "no"
1313 # __git_get_option_value requires 3 arguments
1314 __git_get_option_value ()
1316 local c short_opt long_opt val
1317 local result= values config_key word
1319 short_opt="$1"
1320 long_opt="$2"
1321 values="$3"
1322 config_key="$4"
1324 ((c = $cword - 1))
1325 while [ $c -ge 0 ]; do
1326 word="${words[c]}"
1327 for val in $values; do
1328 if [ "$short_opt$val" = "$word" ] ||
1329 [ "$long_opt$val" = "$word" ]; then
1330 result="$val"
1331 break 2
1333 done
1334 ((c--))
1335 done
1337 if [ -n "$config_key" ] && [ -z "$result" ]; then
1338 result="$(__git config "$config_key")"
1341 echo "$result"
1344 __git_has_doubledash ()
1346 local c=1
1347 while [ $c -lt $cword ]; do
1348 if [ "--" = "${words[c]}" ]; then
1349 return 0
1351 ((c++))
1352 done
1353 return 1
1356 # Try to count non option arguments passed on the command line for the
1357 # specified git command.
1358 # When options are used, it is necessary to use the special -- option to
1359 # tell the implementation were non option arguments begin.
1360 # XXX this can not be improved, since options can appear everywhere, as
1361 # an example:
1362 # git mv x -n y
1364 # __git_count_arguments requires 1 argument: the git command executed.
1365 __git_count_arguments ()
1367 local word i c=0
1369 # Skip "git" (first argument)
1370 for ((i=$__git_cmd_idx; i < ${#words[@]}; i++)); do
1371 word="${words[i]}"
1373 case "$word" in
1375 # Good; we can assume that the following are only non
1376 # option arguments.
1377 ((c = 0))
1379 "$1")
1380 # Skip the specified git command and discard git
1381 # main options
1382 ((c = 0))
1385 ((c++))
1387 esac
1388 done
1390 printf "%d" $c
1393 __git_whitespacelist="nowarn warn error error-all fix"
1394 __git_patchformat="mbox stgit stgit-series hg mboxrd"
1395 __git_showcurrentpatch="diff raw"
1396 __git_am_inprogress_options="--skip --continue --resolved --abort --quit --show-current-patch"
1397 __git_quoted_cr="nowarn warn strip"
1399 _git_am ()
1401 __git_find_repo_path
1402 if [ -d "$__git_repo_path"/rebase-apply ]; then
1403 __gitcomp "$__git_am_inprogress_options"
1404 return
1406 case "$cur" in
1407 --whitespace=*)
1408 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1409 return
1411 --patch-format=*)
1412 __gitcomp "$__git_patchformat" "" "${cur##--patch-format=}"
1413 return
1415 --show-current-patch=*)
1416 __gitcomp "$__git_showcurrentpatch" "" "${cur##--show-current-patch=}"
1417 return
1419 --quoted-cr=*)
1420 __gitcomp "$__git_quoted_cr" "" "${cur##--quoted-cr=}"
1421 return
1423 --*)
1424 __gitcomp_builtin am "" \
1425 "$__git_am_inprogress_options"
1426 return
1427 esac
1430 _git_apply ()
1432 case "$cur" in
1433 --whitespace=*)
1434 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1435 return
1437 --*)
1438 __gitcomp_builtin apply
1439 return
1440 esac
1443 _git_add ()
1445 case "$cur" in
1446 --chmod=*)
1447 __gitcomp "+x -x" "" "${cur##--chmod=}"
1448 return
1450 --*)
1451 __gitcomp_builtin add
1452 return
1453 esac
1455 local complete_opt="--others --modified --directory --no-empty-directory"
1456 if test -n "$(__git_find_on_cmdline "-u --update")"
1457 then
1458 complete_opt="--modified"
1460 __git_complete_index_file "$complete_opt"
1463 _git_archive ()
1465 case "$cur" in
1466 --format=*)
1467 __gitcomp "$(git archive --list)" "" "${cur##--format=}"
1468 return
1470 --remote=*)
1471 __gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
1472 return
1474 --*)
1475 __gitcomp_builtin archive "--format= --list --verbose --prefix= --worktree-attributes"
1476 return
1478 esac
1479 __git_complete_file
1482 _git_bisect ()
1484 __git_has_doubledash && return
1486 __git_find_repo_path
1488 # If a bisection is in progress get the terms being used.
1489 local term_bad term_good
1490 if [ -f "$__git_repo_path"/BISECT_TERMS ]; then
1491 term_bad=$(__git bisect terms --term-bad)
1492 term_good=$(__git bisect terms --term-good)
1495 # We will complete any custom terms, but still always complete the
1496 # more usual bad/new/good/old because git bisect gives a good error
1497 # message if these are given when not in use, and that's better than
1498 # silent refusal to complete if the user is confused.
1500 # We want to recognize 'view' but not complete it, because it overlaps
1501 # with 'visualize' too much and is just an alias for it.
1503 local completable_subcommands="start bad new $term_bad good old $term_good terms skip reset visualize replay log run help"
1504 local all_subcommands="$completable_subcommands view"
1506 local subcommand="$(__git_find_on_cmdline "$all_subcommands")"
1508 if [ -z "$subcommand" ]; then
1509 __git_find_repo_path
1510 if [ -f "$__git_repo_path"/BISECT_START ]; then
1511 __gitcomp "$completable_subcommands"
1512 else
1513 __gitcomp "replay start"
1515 return
1518 case "$subcommand" in
1519 start)
1520 case "$cur" in
1521 --*)
1522 __gitcomp "--first-parent --no-checkout --term-new --term-bad --term-old --term-good"
1523 return
1526 __git_complete_refs
1528 esac
1530 terms)
1531 __gitcomp "--term-good --term-old --term-bad --term-new"
1532 return
1534 visualize|view)
1535 __git_complete_log_opts
1536 return
1538 bad|new|"$term_bad"|good|old|"$term_good"|reset|skip)
1539 __git_complete_refs
1543 esac
1546 __git_ref_fieldlist="refname objecttype objectsize objectname upstream push HEAD symref"
1548 _git_branch ()
1550 local i c="$__git_cmd_idx" only_local_ref="n" has_r="n"
1552 while [ $c -lt $cword ]; do
1553 i="${words[c]}"
1554 case "$i" in
1555 -d|-D|--delete|-m|-M|--move|-c|-C|--copy)
1556 only_local_ref="y" ;;
1557 -r|--remotes)
1558 has_r="y" ;;
1559 esac
1560 ((c++))
1561 done
1563 case "$cur" in
1564 --set-upstream-to=*)
1565 __git_complete_refs --cur="${cur##--set-upstream-to=}"
1567 --*)
1568 __gitcomp_builtin branch
1571 if [ $only_local_ref = "y" -a $has_r = "n" ]; then
1572 __gitcomp_direct "$(__git_heads "" "$cur" " ")"
1573 else
1574 __git_complete_refs
1577 esac
1580 _git_bundle ()
1582 local cmd="${words[__git_cmd_idx+1]}"
1583 case "$cword" in
1584 $((__git_cmd_idx+1)))
1585 __gitcomp "create list-heads verify unbundle"
1587 $((__git_cmd_idx+2)))
1588 # looking for a file
1591 case "$cmd" in
1592 create)
1593 __git_complete_revlist
1595 esac
1597 esac
1600 # Helper function to decide whether or not we should enable DWIM logic for
1601 # git-switch and git-checkout.
1603 # To decide between the following rules in decreasing priority order:
1604 # - the last provided of "--guess" or "--no-guess" explicitly enable or
1605 # disable completion of DWIM logic respectively.
1606 # - If checkout.guess is false, disable completion of DWIM logic.
1607 # - If the --no-track option is provided, take this as a hint to disable the
1608 # DWIM completion logic
1609 # - If GIT_COMPLETION_CHECKOUT_NO_GUESS is set, disable the DWIM completion
1610 # logic, as requested by the user.
1611 # - Enable DWIM logic otherwise.
1613 __git_checkout_default_dwim_mode ()
1615 local last_option dwim_opt="--dwim"
1617 if [ "${GIT_COMPLETION_CHECKOUT_NO_GUESS-}" = "1" ]; then
1618 dwim_opt=""
1621 # --no-track disables DWIM, but with lower priority than
1622 # --guess/--no-guess/checkout.guess
1623 if [ -n "$(__git_find_on_cmdline "--no-track")" ]; then
1624 dwim_opt=""
1627 # checkout.guess = false disables DWIM, but with lower priority than
1628 # --guess/--no-guess
1629 if [ "$(__git config --type=bool checkout.guess)" = "false" ]; then
1630 dwim_opt=""
1633 # Find the last provided --guess or --no-guess
1634 last_option="$(__git_find_last_on_cmdline "--guess --no-guess")"
1635 case "$last_option" in
1636 --guess)
1637 dwim_opt="--dwim"
1639 --no-guess)
1640 dwim_opt=""
1642 esac
1644 echo "$dwim_opt"
1647 _git_checkout ()
1649 __git_has_doubledash && return
1651 local dwim_opt="$(__git_checkout_default_dwim_mode)"
1653 case "$prev" in
1654 -b|-B|--orphan)
1655 # Complete local branches (and DWIM branch
1656 # remote branch names) for an option argument
1657 # specifying a new branch name. This is for
1658 # convenience, assuming new branches are
1659 # possibly based on pre-existing branch names.
1660 __git_complete_refs $dwim_opt --mode="heads"
1661 return
1665 esac
1667 case "$cur" in
1668 --conflict=*)
1669 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
1671 --*)
1672 __gitcomp_builtin checkout
1675 # At this point, we've already handled special completion for
1676 # the arguments to -b/-B, and --orphan. There are 3 main
1677 # things left we can possibly complete:
1678 # 1) a start-point for -b/-B, -d/--detach, or --orphan
1679 # 2) a remote head, for --track
1680 # 3) an arbitrary reference, possibly including DWIM names
1683 if [ -n "$(__git_find_on_cmdline "-b -B -d --detach --orphan")" ]; then
1684 __git_complete_refs --mode="refs"
1685 elif [ -n "$(__git_find_on_cmdline "-t --track")" ]; then
1686 __git_complete_refs --mode="remote-heads"
1687 else
1688 __git_complete_refs $dwim_opt --mode="refs"
1691 esac
1694 __git_sequencer_inprogress_options="--continue --quit --abort --skip"
1696 __git_cherry_pick_inprogress_options=$__git_sequencer_inprogress_options
1698 _git_cherry_pick ()
1700 if __git_pseudoref_exists CHERRY_PICK_HEAD; then
1701 __gitcomp "$__git_cherry_pick_inprogress_options"
1702 return
1705 __git_complete_strategy && return
1707 case "$cur" in
1708 --*)
1709 __gitcomp_builtin cherry-pick "" \
1710 "$__git_cherry_pick_inprogress_options"
1713 __git_complete_refs
1715 esac
1718 _git_clean ()
1720 case "$cur" in
1721 --*)
1722 __gitcomp_builtin clean
1723 return
1725 esac
1727 # XXX should we check for -x option ?
1728 __git_complete_index_file "--others --directory"
1731 _git_clone ()
1733 case "$prev" in
1734 -c|--config)
1735 __git_complete_config_variable_name_and_value
1736 return
1738 esac
1739 case "$cur" in
1740 --config=*)
1741 __git_complete_config_variable_name_and_value \
1742 --cur="${cur##--config=}"
1743 return
1745 --*)
1746 __gitcomp_builtin clone
1747 return
1749 esac
1752 __git_untracked_file_modes="all no normal"
1754 __git_trailer_tokens ()
1756 __git config --name-only --get-regexp '^trailer\..*\.key$' | cut -d. -f 2- | rev | cut -d. -f2- | rev
1759 _git_commit ()
1761 case "$prev" in
1762 -c|-C)
1763 __git_complete_refs
1764 return
1766 esac
1768 case "$cur" in
1769 --cleanup=*)
1770 __gitcomp "default scissors strip verbatim whitespace
1771 " "" "${cur##--cleanup=}"
1772 return
1774 --reuse-message=*|--reedit-message=*|\
1775 --fixup=*|--squash=*)
1776 __git_complete_refs --cur="${cur#*=}"
1777 return
1779 --untracked-files=*)
1780 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
1781 return
1783 --trailer=*)
1784 __gitcomp_nl "$(__git_trailer_tokens)" "" "${cur##--trailer=}" ":"
1785 return
1787 --*)
1788 __gitcomp_builtin commit
1789 return
1790 esac
1792 if __git rev-parse --verify --quiet HEAD >/dev/null; then
1793 __git_complete_index_file "--committable"
1794 else
1795 # This is the first commit
1796 __git_complete_index_file "--cached"
1800 _git_describe ()
1802 case "$cur" in
1803 --*)
1804 __gitcomp_builtin describe
1805 return
1806 esac
1807 __git_complete_refs
1810 __git_diff_algorithms="myers minimal patience histogram"
1812 __git_diff_submodule_formats="diff log short"
1814 __git_color_moved_opts="no default plain blocks zebra dimmed-zebra"
1816 __git_color_moved_ws_opts="no ignore-space-at-eol ignore-space-change
1817 ignore-all-space allow-indentation-change"
1819 __git_ws_error_highlight_opts="context old new all default"
1821 # Options for the diff machinery (diff, log, show, stash, range-diff, ...)
1822 __git_diff_common_options="--stat --numstat --shortstat --summary
1823 --patch-with-stat --name-only --name-status --color
1824 --no-color --color-words --no-renames --check
1825 --color-moved --color-moved= --no-color-moved
1826 --color-moved-ws= --no-color-moved-ws
1827 --full-index --binary --abbrev --diff-filter=
1828 --find-copies --find-object --find-renames
1829 --no-relative --relative
1830 --find-copies-harder --ignore-cr-at-eol
1831 --text --ignore-space-at-eol --ignore-space-change
1832 --ignore-all-space --ignore-blank-lines --exit-code
1833 --quiet --ext-diff --no-ext-diff --unified=
1834 --no-prefix --src-prefix= --dst-prefix=
1835 --inter-hunk-context= --function-context
1836 --patience --histogram --minimal
1837 --raw --word-diff --word-diff-regex=
1838 --dirstat --dirstat= --dirstat-by-file
1839 --dirstat-by-file= --cumulative
1840 --diff-algorithm= --default-prefix
1841 --submodule --submodule= --ignore-submodules
1842 --indent-heuristic --no-indent-heuristic
1843 --textconv --no-textconv --break-rewrites
1844 --patch --no-patch --cc --combined-all-paths
1845 --anchored= --compact-summary --ignore-matching-lines=
1846 --irreversible-delete --line-prefix --no-stat
1847 --output= --output-indicator-context=
1848 --output-indicator-new= --output-indicator-old=
1849 --ws-error-highlight=
1850 --pickaxe-all --pickaxe-regex --patch-with-raw
1853 # Options for diff/difftool
1854 __git_diff_difftool_options="--cached --staged
1855 --base --ours --theirs --no-index --merge-base
1856 --ita-invisible-in-index --ita-visible-in-index
1857 $__git_diff_common_options"
1859 _git_diff ()
1861 __git_has_doubledash && return
1863 case "$cur" in
1864 --diff-algorithm=*)
1865 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1866 return
1868 --submodule=*)
1869 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1870 return
1872 --color-moved=*)
1873 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
1874 return
1876 --color-moved-ws=*)
1877 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
1878 return
1880 --ws-error-highlight=*)
1881 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
1882 return
1884 --*)
1885 __gitcomp "$__git_diff_difftool_options"
1886 return
1888 esac
1889 __git_complete_revlist_file
1892 __git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff
1893 tkdiff vimdiff nvimdiff gvimdiff xxdiff araxis p4merge
1894 bc codecompare smerge
1897 _git_difftool ()
1899 __git_has_doubledash && return
1901 case "$cur" in
1902 --tool=*)
1903 __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1904 return
1906 --*)
1907 __gitcomp_builtin difftool "$__git_diff_difftool_options"
1908 return
1910 esac
1911 __git_complete_revlist_file
1914 __git_fetch_recurse_submodules="yes on-demand no"
1916 _git_fetch ()
1918 case "$cur" in
1919 --recurse-submodules=*)
1920 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1921 return
1923 --filter=*)
1924 __gitcomp "blob:none blob:limit= sparse:oid=" "" "${cur##--filter=}"
1925 return
1927 --*)
1928 __gitcomp_builtin fetch
1929 return
1931 esac
1932 __git_complete_remote_or_refspec
1935 __git_format_patch_extra_options="
1936 --full-index --not --all --no-prefix --src-prefix=
1937 --dst-prefix= --notes
1940 _git_format_patch ()
1942 case "$cur" in
1943 --thread=*)
1944 __gitcomp "
1945 deep shallow
1946 " "" "${cur##--thread=}"
1947 return
1949 --base=*|--interdiff=*|--range-diff=*)
1950 __git_complete_refs --cur="${cur#--*=}"
1951 return
1953 --*)
1954 __gitcomp_builtin format-patch "$__git_format_patch_extra_options"
1955 return
1957 esac
1958 __git_complete_revlist
1961 _git_fsck ()
1963 case "$cur" in
1964 --*)
1965 __gitcomp_builtin fsck
1966 return
1968 esac
1971 _git_gitk ()
1973 __gitk_main
1976 # Lists matching symbol names from a tag (as in ctags) file.
1977 # 1: List symbol names matching this word.
1978 # 2: The tag file to list symbol names from.
1979 # 3: A prefix to be added to each listed symbol name (optional).
1980 # 4: A suffix to be appended to each listed symbol name (optional).
1981 __git_match_ctag () {
1982 awk -v pfx="${3-}" -v sfx="${4-}" "
1983 /^${1//\//\\/}/ { print pfx \$1 sfx }
1984 " "$2"
1987 # Complete symbol names from a tag file.
1988 # Usage: __git_complete_symbol [<option>]...
1989 # --tags=<file>: The tag file to list symbol names from instead of the
1990 # default "tags".
1991 # --pfx=<prefix>: A prefix to be added to each symbol name.
1992 # --cur=<word>: The current symbol name to be completed. Defaults to
1993 # the current word to be completed.
1994 # --sfx=<suffix>: A suffix to be appended to each symbol name instead
1995 # of the default space.
1996 __git_complete_symbol () {
1997 local tags=tags pfx="" cur_="${cur-}" sfx=" "
1999 while test $# != 0; do
2000 case "$1" in
2001 --tags=*) tags="${1##--tags=}" ;;
2002 --pfx=*) pfx="${1##--pfx=}" ;;
2003 --cur=*) cur_="${1##--cur=}" ;;
2004 --sfx=*) sfx="${1##--sfx=}" ;;
2005 *) return 1 ;;
2006 esac
2007 shift
2008 done
2010 if test -r "$tags"; then
2011 __gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
2015 _git_grep ()
2017 __git_has_doubledash && return
2019 case "$cur" in
2020 --*)
2021 __gitcomp_builtin grep
2022 return
2024 esac
2026 case "$cword,$prev" in
2027 $((__git_cmd_idx+1)),*|*,-*)
2028 __git_complete_symbol && return
2030 esac
2032 __git_complete_refs
2035 _git_help ()
2037 case "$cur" in
2038 --*)
2039 __gitcomp_builtin help
2040 return
2042 esac
2043 if test -n "${GIT_TESTING_ALL_COMMAND_LIST-}"
2044 then
2045 __gitcomp "$GIT_TESTING_ALL_COMMAND_LIST $(__git --list-cmds=alias,list-guide) gitk"
2046 else
2047 __gitcomp "$(__git --list-cmds=main,nohelpers,alias,list-guide) gitk"
2051 _git_init ()
2053 case "$cur" in
2054 --shared=*)
2055 __gitcomp "
2056 false true umask group all world everybody
2057 " "" "${cur##--shared=}"
2058 return
2060 --*)
2061 __gitcomp_builtin init
2062 return
2064 esac
2067 _git_ls_files ()
2069 case "$cur" in
2070 --*)
2071 __gitcomp_builtin ls-files
2072 return
2074 esac
2076 # XXX ignore options like --modified and always suggest all cached
2077 # files.
2078 __git_complete_index_file "--cached"
2081 _git_ls_remote ()
2083 case "$cur" in
2084 --*)
2085 __gitcomp_builtin ls-remote
2086 return
2088 esac
2089 __gitcomp_nl "$(__git_remotes)"
2092 _git_ls_tree ()
2094 case "$cur" in
2095 --*)
2096 __gitcomp_builtin ls-tree
2097 return
2099 esac
2101 __git_complete_file
2104 # Options that go well for log, shortlog and gitk
2105 __git_log_common_options="
2106 --not --all
2107 --branches --tags --remotes
2108 --first-parent --merges --no-merges
2109 --max-count=
2110 --max-age= --since= --after=
2111 --min-age= --until= --before=
2112 --min-parents= --max-parents=
2113 --no-min-parents --no-max-parents
2114 --alternate-refs --ancestry-path
2115 --author-date-order --basic-regexp
2116 --bisect --boundary --exclude-first-parent-only
2117 --exclude-hidden --extended-regexp
2118 --fixed-strings --grep-reflog
2119 --ignore-missing --left-only --perl-regexp
2120 --reflog --regexp-ignore-case --remove-empty
2121 --right-only --show-linear-break
2122 --show-notes-by-default --show-pulls
2123 --since-as-filter --single-worktree
2125 # Options that go well for log and gitk (not shortlog)
2126 __git_log_gitk_options="
2127 --dense --sparse --full-history
2128 --simplify-merges --simplify-by-decoration
2129 --left-right --notes --no-notes
2131 # Options that go well for log and shortlog (not gitk)
2132 __git_log_shortlog_options="
2133 --author= --committer= --grep=
2134 --all-match --invert-grep
2136 # Options accepted by log and show
2137 __git_log_show_options="
2138 --diff-merges --diff-merges= --no-diff-merges --dd --remerge-diff
2139 --encoding=
2142 __git_diff_merges_opts="off none on first-parent 1 separate m combined c dense-combined cc remerge r"
2144 __git_log_pretty_formats="oneline short medium full fuller reference email raw format: tformat: mboxrd"
2145 __git_log_date_formats="relative iso8601 iso8601-strict rfc2822 short local default human raw unix auto: format:"
2147 # Complete porcelain (i.e. not git-rev-list) options and at least some
2148 # option arguments accepted by git-log. Note that this same set of options
2149 # are also accepted by some other git commands besides git-log.
2150 __git_complete_log_opts ()
2152 COMPREPLY=()
2154 local merge=""
2155 if __git_pseudoref_exists MERGE_HEAD; then
2156 merge="--merge"
2158 case "$prev,$cur" in
2159 -L,:*:*)
2160 return # fall back to Bash filename completion
2162 -L,:*)
2163 __git_complete_symbol --cur="${cur#:}" --sfx=":"
2164 return
2166 -G,*|-S,*)
2167 __git_complete_symbol
2168 return
2170 esac
2171 case "$cur" in
2172 --pretty=*|--format=*)
2173 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2174 " "" "${cur#*=}"
2175 return
2177 --date=*)
2178 __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
2179 return
2181 --decorate=*)
2182 __gitcomp "full short no" "" "${cur##--decorate=}"
2183 return
2185 --diff-algorithm=*)
2186 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
2187 return
2189 --submodule=*)
2190 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
2191 return
2193 --ws-error-highlight=*)
2194 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
2195 return
2197 --no-walk=*)
2198 __gitcomp "sorted unsorted" "" "${cur##--no-walk=}"
2199 return
2201 --diff-merges=*)
2202 __gitcomp "$__git_diff_merges_opts" "" "${cur##--diff-merges=}"
2203 return
2205 --*)
2206 __gitcomp "
2207 $__git_log_common_options
2208 $__git_log_shortlog_options
2209 $__git_log_gitk_options
2210 $__git_log_show_options
2211 --root --topo-order --date-order --reverse
2212 --follow --full-diff
2213 --abbrev-commit --no-abbrev-commit --abbrev=
2214 --relative-date --date=
2215 --pretty= --format= --oneline
2216 --show-signature
2217 --cherry-mark
2218 --cherry-pick
2219 --graph
2220 --decorate --decorate= --no-decorate
2221 --walk-reflogs
2222 --no-walk --no-walk= --do-walk
2223 --parents --children
2224 --expand-tabs --expand-tabs= --no-expand-tabs
2225 --clear-decorations --decorate-refs=
2226 --decorate-refs-exclude=
2227 $merge
2228 $__git_diff_common_options
2230 return
2232 -L:*:*)
2233 return # fall back to Bash filename completion
2235 -L:*)
2236 __git_complete_symbol --cur="${cur#-L:}" --sfx=":"
2237 return
2239 -G*)
2240 __git_complete_symbol --pfx="-G" --cur="${cur#-G}"
2241 return
2243 -S*)
2244 __git_complete_symbol --pfx="-S" --cur="${cur#-S}"
2245 return
2247 esac
2250 _git_log ()
2252 __git_has_doubledash && return
2253 __git_find_repo_path
2255 __git_complete_log_opts
2256 [ ${#COMPREPLY[@]} -eq 0 ] || return
2258 __git_complete_revlist
2261 _git_merge ()
2263 __git_complete_strategy && return
2265 case "$cur" in
2266 --*)
2267 __gitcomp_builtin merge
2268 return
2269 esac
2270 __git_complete_refs
2273 _git_mergetool ()
2275 case "$cur" in
2276 --tool=*)
2277 __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
2278 return
2280 --*)
2281 __gitcomp "--tool= --prompt --no-prompt --gui --no-gui"
2282 return
2284 esac
2287 _git_merge_base ()
2289 case "$cur" in
2290 --*)
2291 __gitcomp_builtin merge-base
2292 return
2294 esac
2295 __git_complete_refs
2298 _git_mv ()
2300 case "$cur" in
2301 --*)
2302 __gitcomp_builtin mv
2303 return
2305 esac
2307 if [ $(__git_count_arguments "mv") -gt 0 ]; then
2308 # We need to show both cached and untracked files (including
2309 # empty directories) since this may not be the last argument.
2310 __git_complete_index_file "--cached --others --directory"
2311 else
2312 __git_complete_index_file "--cached"
2316 _git_notes ()
2318 local subcommands='add append copy edit get-ref list merge prune remove show'
2319 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2321 case "$subcommand,$cur" in
2322 ,--*)
2323 __gitcomp_builtin notes
2326 case "$prev" in
2327 --ref)
2328 __git_complete_refs
2331 __gitcomp "$subcommands --ref"
2333 esac
2335 *,--reuse-message=*|*,--reedit-message=*)
2336 __git_complete_refs --cur="${cur#*=}"
2338 *,--*)
2339 __gitcomp_builtin notes_$subcommand
2341 prune,*|get-ref,*)
2342 # this command does not take a ref, do not complete it
2345 case "$prev" in
2346 -m|-F)
2349 __git_complete_refs
2351 esac
2353 esac
2356 _git_pull ()
2358 __git_complete_strategy && return
2360 case "$cur" in
2361 --recurse-submodules=*)
2362 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
2363 return
2365 --*)
2366 __gitcomp_builtin pull
2368 return
2370 esac
2371 __git_complete_remote_or_refspec
2374 __git_push_recurse_submodules="check on-demand only"
2376 __git_complete_force_with_lease ()
2378 local cur_=$1
2380 case "$cur_" in
2381 --*=)
2383 *:*)
2384 __git_complete_refs --cur="${cur_#*:}"
2387 __git_complete_refs --cur="$cur_"
2389 esac
2392 _git_push ()
2394 case "$prev" in
2395 --repo)
2396 __gitcomp_nl "$(__git_remotes)"
2397 return
2399 --recurse-submodules)
2400 __gitcomp "$__git_push_recurse_submodules"
2401 return
2403 esac
2404 case "$cur" in
2405 --repo=*)
2406 __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
2407 return
2409 --recurse-submodules=*)
2410 __gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
2411 return
2413 --force-with-lease=*)
2414 __git_complete_force_with_lease "${cur##--force-with-lease=}"
2415 return
2417 --*)
2418 __gitcomp_builtin push
2419 return
2421 esac
2422 __git_complete_remote_or_refspec
2425 _git_range_diff ()
2427 case "$cur" in
2428 --*)
2429 __gitcomp "
2430 --creation-factor= --no-dual-color
2431 $__git_diff_common_options
2433 return
2435 esac
2436 __git_complete_revlist
2439 __git_rebase_inprogress_options="--continue --skip --abort --quit --show-current-patch"
2440 __git_rebase_interactive_inprogress_options="$__git_rebase_inprogress_options --edit-todo"
2442 _git_rebase ()
2444 __git_find_repo_path
2445 if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
2446 __gitcomp "$__git_rebase_interactive_inprogress_options"
2447 return
2448 elif [ -d "$__git_repo_path"/rebase-apply ] || \
2449 [ -d "$__git_repo_path"/rebase-merge ]; then
2450 __gitcomp "$__git_rebase_inprogress_options"
2451 return
2453 __git_complete_strategy && return
2454 case "$cur" in
2455 --whitespace=*)
2456 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
2457 return
2459 --onto=*)
2460 __git_complete_refs --cur="${cur##--onto=}"
2461 return
2463 --*)
2464 __gitcomp_builtin rebase "" \
2465 "$__git_rebase_interactive_inprogress_options"
2467 return
2468 esac
2469 __git_complete_refs
2472 _git_reflog ()
2474 local subcommands="show delete expire"
2475 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2477 if [ -z "$subcommand" ]; then
2478 __gitcomp "$subcommands"
2479 else
2480 __git_complete_refs
2484 __git_send_email_confirm_options="always never auto cc compose"
2485 __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
2487 _git_send_email ()
2489 case "$prev" in
2490 --to|--cc|--bcc|--from)
2491 __gitcomp "$(__git send-email --dump-aliases)"
2492 return
2494 esac
2496 case "$cur" in
2497 --confirm=*)
2498 __gitcomp "
2499 $__git_send_email_confirm_options
2500 " "" "${cur##--confirm=}"
2501 return
2503 --suppress-cc=*)
2504 __gitcomp "
2505 $__git_send_email_suppresscc_options
2506 " "" "${cur##--suppress-cc=}"
2508 return
2510 --smtp-encryption=*)
2511 __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
2512 return
2514 --thread=*)
2515 __gitcomp "
2516 deep shallow
2517 " "" "${cur##--thread=}"
2518 return
2520 --to=*|--cc=*|--bcc=*|--from=*)
2521 __gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
2522 return
2524 --*)
2525 __gitcomp_builtin send-email "$__git_format_patch_extra_options"
2526 return
2528 esac
2529 __git_complete_revlist
2532 _git_stage ()
2534 _git_add
2537 _git_status ()
2539 local complete_opt
2540 local untracked_state
2542 case "$cur" in
2543 --ignore-submodules=*)
2544 __gitcomp "none untracked dirty all" "" "${cur##--ignore-submodules=}"
2545 return
2547 --untracked-files=*)
2548 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
2549 return
2551 --column=*)
2552 __gitcomp "
2553 always never auto column row plain dense nodense
2554 " "" "${cur##--column=}"
2555 return
2557 --*)
2558 __gitcomp_builtin status
2559 return
2561 esac
2563 untracked_state="$(__git_get_option_value "-u" "--untracked-files=" \
2564 "$__git_untracked_file_modes" "status.showUntrackedFiles")"
2566 case "$untracked_state" in
2568 # --ignored option does not matter
2569 complete_opt=
2571 all|normal|*)
2572 complete_opt="--cached --directory --no-empty-directory --others"
2574 if [ -n "$(__git_find_on_cmdline "--ignored")" ]; then
2575 complete_opt="$complete_opt --ignored --exclude=*"
2578 esac
2580 __git_complete_index_file "$complete_opt"
2583 _git_switch ()
2585 local dwim_opt="$(__git_checkout_default_dwim_mode)"
2587 case "$prev" in
2588 -c|-C|--orphan)
2589 # Complete local branches (and DWIM branch
2590 # remote branch names) for an option argument
2591 # specifying a new branch name. This is for
2592 # convenience, assuming new branches are
2593 # possibly based on pre-existing branch names.
2594 __git_complete_refs $dwim_opt --mode="heads"
2595 return
2599 esac
2601 case "$cur" in
2602 --conflict=*)
2603 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
2605 --*)
2606 __gitcomp_builtin switch
2609 # Unlike in git checkout, git switch --orphan does not take
2610 # a start point. Thus we really have nothing to complete after
2611 # the branch name.
2612 if [ -n "$(__git_find_on_cmdline "--orphan")" ]; then
2613 return
2616 # At this point, we've already handled special completion for
2617 # -c/-C, and --orphan. There are 3 main things left to
2618 # complete:
2619 # 1) a start-point for -c/-C or -d/--detach
2620 # 2) a remote head, for --track
2621 # 3) a branch name, possibly including DWIM remote branches
2623 if [ -n "$(__git_find_on_cmdline "-c -C -d --detach")" ]; then
2624 __git_complete_refs --mode="refs"
2625 elif [ -n "$(__git_find_on_cmdline "-t --track")" ]; then
2626 __git_complete_refs --mode="remote-heads"
2627 else
2628 __git_complete_refs $dwim_opt --mode="heads"
2631 esac
2634 __git_config_get_set_variables ()
2636 local prevword word config_file= c=$cword
2637 while [ $c -gt "$__git_cmd_idx" ]; do
2638 word="${words[c]}"
2639 case "$word" in
2640 --system|--global|--local|--file=*)
2641 config_file="$word"
2642 break
2644 -f|--file)
2645 config_file="$word $prevword"
2646 break
2648 esac
2649 prevword=$word
2650 c=$((--c))
2651 done
2653 __git config $config_file --name-only --list
2656 __git_config_vars=
2657 __git_compute_config_vars ()
2659 test -n "$__git_config_vars" ||
2660 __git_config_vars="$(git help --config-for-completion)"
2663 __git_config_vars_all=
2664 __git_compute_config_vars_all ()
2666 test -n "$__git_config_vars_all" ||
2667 __git_config_vars_all="$(git --no-pager help --config)"
2670 __git_compute_first_level_config_vars_for_section ()
2672 local section="$1"
2673 __git_compute_config_vars
2674 local this_section="__git_first_level_config_vars_for_section_${section}"
2675 test -n "${!this_section}" ||
2676 printf -v "__git_first_level_config_vars_for_section_${section}" %s "$(echo "$__git_config_vars" | grep -E "^${section}\.[a-z]" | awk -F. '{print $2}')"
2679 __git_compute_second_level_config_vars_for_section ()
2681 local section="$1"
2682 __git_compute_config_vars_all
2683 local this_section="__git_second_level_config_vars_for_section_${section}"
2684 test -n "${!this_section}" ||
2685 printf -v "__git_second_level_config_vars_for_section_${section}" %s "$(echo "$__git_config_vars_all" | grep -E "^${section}\.<" | awk -F. '{print $3}')"
2688 __git_config_sections=
2689 __git_compute_config_sections ()
2691 test -n "$__git_config_sections" ||
2692 __git_config_sections="$(git help --config-sections-for-completion)"
2695 # Completes possible values of various configuration variables.
2697 # Usage: __git_complete_config_variable_value [<option>]...
2698 # --varname=<word>: The name of the configuration variable whose value is
2699 # to be completed. Defaults to the previous word on the
2700 # command line.
2701 # --cur=<word>: The current value to be completed. Defaults to the current
2702 # word to be completed.
2703 __git_complete_config_variable_value ()
2705 local varname="$prev" cur_="$cur"
2707 while test $# != 0; do
2708 case "$1" in
2709 --varname=*) varname="${1##--varname=}" ;;
2710 --cur=*) cur_="${1##--cur=}" ;;
2711 *) return 1 ;;
2712 esac
2713 shift
2714 done
2716 if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
2717 varname="${varname,,}"
2718 else
2719 varname="$(echo "$varname" |tr A-Z a-z)"
2722 case "$varname" in
2723 branch.*.remote|branch.*.pushremote)
2724 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2725 return
2727 branch.*.merge)
2728 __git_complete_refs --cur="$cur_"
2729 return
2731 branch.*.rebase)
2732 __gitcomp "false true merges interactive" "" "$cur_"
2733 return
2735 remote.pushdefault)
2736 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2737 return
2739 remote.*.fetch)
2740 local remote="${varname#remote.}"
2741 remote="${remote%.fetch}"
2742 if [ -z "$cur_" ]; then
2743 __gitcomp_nl "refs/heads/" "" "" ""
2744 return
2746 __gitcomp_nl "$(__git_refs_remotes "$remote")" "" "$cur_"
2747 return
2749 remote.*.push)
2750 local remote="${varname#remote.}"
2751 remote="${remote%.push}"
2752 __gitcomp_nl "$(__git for-each-ref \
2753 --format='%(refname):%(refname)' refs/heads)" "" "$cur_"
2754 return
2756 pull.twohead|pull.octopus)
2757 __git_compute_merge_strategies
2758 __gitcomp "$__git_merge_strategies" "" "$cur_"
2759 return
2761 color.pager)
2762 __gitcomp "false true" "" "$cur_"
2763 return
2765 color.*.*)
2766 __gitcomp "
2767 normal black red green yellow blue magenta cyan white
2768 bold dim ul blink reverse
2769 " "" "$cur_"
2770 return
2772 color.*)
2773 __gitcomp "false true always never auto" "" "$cur_"
2774 return
2776 diff.submodule)
2777 __gitcomp "$__git_diff_submodule_formats" "" "$cur_"
2778 return
2780 help.format)
2781 __gitcomp "man info web html" "" "$cur_"
2782 return
2784 log.date)
2785 __gitcomp "$__git_log_date_formats" "" "$cur_"
2786 return
2788 sendemail.aliasfiletype)
2789 __gitcomp "mutt mailrc pine elm gnus" "" "$cur_"
2790 return
2792 sendemail.confirm)
2793 __gitcomp "$__git_send_email_confirm_options" "" "$cur_"
2794 return
2796 sendemail.suppresscc)
2797 __gitcomp "$__git_send_email_suppresscc_options" "" "$cur_"
2798 return
2800 sendemail.transferencoding)
2801 __gitcomp "7bit 8bit quoted-printable base64" "" "$cur_"
2802 return
2804 *.*)
2805 return
2807 esac
2810 # Completes configuration sections, subsections, variable names.
2812 # Usage: __git_complete_config_variable_name [<option>]...
2813 # --cur=<word>: The current configuration section/variable name to be
2814 # completed. Defaults to the current word to be completed.
2815 # --sfx=<suffix>: A suffix to be appended to each fully completed
2816 # configuration variable name (but not to sections or
2817 # subsections) instead of the default space.
2818 __git_complete_config_variable_name ()
2820 local cur_="$cur" sfx
2822 while test $# != 0; do
2823 case "$1" in
2824 --cur=*) cur_="${1##--cur=}" ;;
2825 --sfx=*) sfx="${1##--sfx=}" ;;
2826 *) return 1 ;;
2827 esac
2828 shift
2829 done
2831 case "$cur_" in
2832 branch.*.*|guitool.*.*|difftool.*.*|man.*.*|mergetool.*.*|remote.*.*|submodule.*.*|url.*.*)
2833 local pfx="${cur_%.*}."
2834 cur_="${cur_##*.}"
2835 local section="${pfx%.*.}"
2836 __git_compute_second_level_config_vars_for_section "${section}"
2837 local this_section="__git_second_level_config_vars_for_section_${section}"
2838 __gitcomp "${!this_section}" "$pfx" "$cur_" "$sfx"
2839 return
2841 branch.*)
2842 local pfx="${cur_%.*}."
2843 cur_="${cur_#*.}"
2844 local section="${pfx%.}"
2845 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
2846 __git_compute_first_level_config_vars_for_section "${section}"
2847 local this_section="__git_first_level_config_vars_for_section_${section}"
2848 __gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
2849 return
2851 pager.*)
2852 local pfx="${cur_%.*}."
2853 cur_="${cur_#*.}"
2854 __git_compute_all_commands
2855 __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_" "${sfx:- }"
2856 return
2858 remote.*)
2859 local pfx="${cur_%.*}."
2860 cur_="${cur_#*.}"
2861 local section="${pfx%.}"
2862 __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
2863 __git_compute_first_level_config_vars_for_section "${section}"
2864 local this_section="__git_first_level_config_vars_for_section_${section}"
2865 __gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
2866 return
2868 submodule.*)
2869 local pfx="${cur_%.*}."
2870 cur_="${cur_#*.}"
2871 local section="${pfx%.}"
2872 __gitcomp_nl "$(__git config -f "$(__git rev-parse --show-toplevel)/.gitmodules" --get-regexp 'submodule.*.path' | awk -F. '{print $2}')" "$pfx" "$cur_" "."
2873 __git_compute_first_level_config_vars_for_section "${section}"
2874 local this_section="__git_first_level_config_vars_for_section_${section}"
2875 __gitcomp_nl_append "${!this_section}" "$pfx" "$cur_" "${sfx:- }"
2876 return
2878 *.*)
2879 __git_compute_config_vars
2880 __gitcomp "$__git_config_vars" "" "$cur_" "$sfx"
2883 __git_compute_config_sections
2884 __gitcomp "$__git_config_sections" "" "$cur_" "."
2886 esac
2889 # Completes '='-separated configuration sections/variable names and values
2890 # for 'git -c section.name=value'.
2892 # Usage: __git_complete_config_variable_name_and_value [<option>]...
2893 # --cur=<word>: The current configuration section/variable name/value to be
2894 # completed. Defaults to the current word to be completed.
2895 __git_complete_config_variable_name_and_value ()
2897 local cur_="$cur"
2899 while test $# != 0; do
2900 case "$1" in
2901 --cur=*) cur_="${1##--cur=}" ;;
2902 *) return 1 ;;
2903 esac
2904 shift
2905 done
2907 case "$cur_" in
2908 *=*)
2909 __git_complete_config_variable_value \
2910 --varname="${cur_%%=*}" --cur="${cur_#*=}"
2913 __git_complete_config_variable_name --cur="$cur_" --sfx='='
2915 esac
2918 _git_config ()
2920 case "$prev" in
2921 --get|--get-all|--unset|--unset-all)
2922 __gitcomp_nl "$(__git_config_get_set_variables)"
2923 return
2925 *.*)
2926 __git_complete_config_variable_value
2927 return
2929 esac
2930 case "$cur" in
2931 --*)
2932 __gitcomp_builtin config
2935 __git_complete_config_variable_name
2937 esac
2940 _git_remote ()
2942 local subcommands="
2943 add rename remove set-head set-branches
2944 get-url set-url show prune update
2946 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2947 if [ -z "$subcommand" ]; then
2948 case "$cur" in
2949 --*)
2950 __gitcomp_builtin remote
2953 __gitcomp "$subcommands"
2955 esac
2956 return
2959 case "$subcommand,$cur" in
2960 add,--*)
2961 __gitcomp_builtin remote_add
2963 add,*)
2965 set-head,--*)
2966 __gitcomp_builtin remote_set-head
2968 set-branches,--*)
2969 __gitcomp_builtin remote_set-branches
2971 set-head,*|set-branches,*)
2972 __git_complete_remote_or_refspec
2974 update,--*)
2975 __gitcomp_builtin remote_update
2977 update,*)
2978 __gitcomp "$(__git_remotes) $(__git_get_config_variables "remotes")"
2980 set-url,--*)
2981 __gitcomp_builtin remote_set-url
2983 get-url,--*)
2984 __gitcomp_builtin remote_get-url
2986 prune,--*)
2987 __gitcomp_builtin remote_prune
2990 __gitcomp_nl "$(__git_remotes)"
2992 esac
2995 _git_replace ()
2997 case "$cur" in
2998 --format=*)
2999 __gitcomp "short medium long" "" "${cur##--format=}"
3000 return
3002 --*)
3003 __gitcomp_builtin replace
3004 return
3006 esac
3007 __git_complete_refs
3010 _git_rerere ()
3012 local subcommands="clear forget diff remaining status gc"
3013 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3014 if test -z "$subcommand"
3015 then
3016 __gitcomp "$subcommands"
3017 return
3021 _git_reset ()
3023 __git_has_doubledash && return
3025 case "$cur" in
3026 --*)
3027 __gitcomp_builtin reset
3028 return
3030 esac
3031 __git_complete_refs
3034 _git_restore ()
3036 case "$prev" in
3038 __git_complete_refs
3039 return
3041 esac
3043 case "$cur" in
3044 --conflict=*)
3045 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
3047 --source=*)
3048 __git_complete_refs --cur="${cur##--source=}"
3050 --*)
3051 __gitcomp_builtin restore
3054 if __git_pseudoref_exists HEAD; then
3055 __git_complete_index_file "--modified"
3057 esac
3060 __git_revert_inprogress_options=$__git_sequencer_inprogress_options
3062 _git_revert ()
3064 if __git_pseudoref_exists REVERT_HEAD; then
3065 __gitcomp "$__git_revert_inprogress_options"
3066 return
3068 __git_complete_strategy && return
3069 case "$cur" in
3070 --*)
3071 __gitcomp_builtin revert "" \
3072 "$__git_revert_inprogress_options"
3073 return
3075 esac
3076 __git_complete_refs
3079 _git_rm ()
3081 case "$cur" in
3082 --*)
3083 __gitcomp_builtin rm
3084 return
3086 esac
3088 __git_complete_index_file "--cached"
3091 _git_shortlog ()
3093 __git_has_doubledash && return
3095 case "$cur" in
3096 --*)
3097 __gitcomp "
3098 $__git_log_common_options
3099 $__git_log_shortlog_options
3100 --numbered --summary --email
3102 return
3104 esac
3105 __git_complete_revlist
3108 _git_show ()
3110 __git_has_doubledash && return
3112 case "$cur" in
3113 --pretty=*|--format=*)
3114 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
3115 " "" "${cur#*=}"
3116 return
3118 --diff-algorithm=*)
3119 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
3120 return
3122 --submodule=*)
3123 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
3124 return
3126 --color-moved=*)
3127 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
3128 return
3130 --color-moved-ws=*)
3131 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
3132 return
3134 --ws-error-highlight=*)
3135 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
3136 return
3138 --diff-merges=*)
3139 __gitcomp "$__git_diff_merges_opts" "" "${cur##--diff-merges=}"
3140 return
3142 --*)
3143 __gitcomp "--pretty= --format= --abbrev-commit --no-abbrev-commit
3144 --oneline --show-signature
3145 --expand-tabs --expand-tabs= --no-expand-tabs
3146 $__git_log_show_options
3147 $__git_diff_common_options
3149 return
3151 esac
3152 __git_complete_revlist_file
3155 _git_show_branch ()
3157 case "$cur" in
3158 --*)
3159 __gitcomp_builtin show-branch
3160 return
3162 esac
3163 __git_complete_revlist
3166 __gitcomp_directories ()
3168 local _tmp_dir _tmp_completions _found=0
3170 # Get the directory of the current token; this differs from dirname
3171 # in that it keeps up to the final trailing slash. If no slash found
3172 # that's fine too.
3173 [[ "$cur" =~ .*/ ]]
3174 _tmp_dir=$BASH_REMATCH
3176 # Find possible directory completions, adding trailing '/' characters,
3177 # de-quoting, and handling unusual characters.
3178 while IFS= read -r -d $'\0' c ; do
3179 # If there are directory completions, find ones that start
3180 # with "$cur", the current token, and put those in COMPREPLY
3181 if [[ $c == "$cur"* ]]; then
3182 COMPREPLY+=("$c/")
3183 _found=1
3185 done < <(__git ls-tree -z -d --name-only HEAD $_tmp_dir)
3187 if [[ $_found == 0 ]] && [[ "$cur" =~ /$ ]]; then
3188 # No possible further completions any deeper, so assume we're at
3189 # a leaf directory and just consider it complete
3190 __gitcomp_direct_append "$cur "
3191 elif [[ $_found == 0 ]]; then
3192 # No possible completions found. Avoid falling back to
3193 # bash's default file and directory completion, because all
3194 # valid completions have already been searched and the
3195 # fallbacks can do nothing but mislead. In fact, they can
3196 # mislead in three different ways:
3197 # 1) Fallback file completion makes no sense when asking
3198 # for directory completions, as this function does.
3199 # 2) Fallback directory completion is bad because
3200 # e.g. "/pro" is invalid and should NOT complete to
3201 # "/proc".
3202 # 3) Fallback file/directory completion only completes
3203 # on paths that exist in the current working tree,
3204 # i.e. which are *already* part of their
3205 # sparse-checkout. Thus, normal file and directory
3206 # completion is always useless for "git
3207 # sparse-checkout add" and is also probelmatic for
3208 # "git sparse-checkout set" unless using it to
3209 # strictly narrow the checkout.
3210 COMPREPLY=( "" )
3214 # In non-cone mode, the arguments to {set,add} are supposed to be
3215 # patterns, relative to the toplevel directory. These can be any kind
3216 # of general pattern, like 'subdir/*.c' and we can't complete on all
3217 # of those. However, if the user presses Tab to get tab completion, we
3218 # presume that they are trying to provide a pattern that names a specific
3219 # path.
3220 __gitcomp_slash_leading_paths ()
3222 local dequoted_word pfx="" cur_ toplevel
3224 # Since we are dealing with a sparse-checkout, subdirectories may not
3225 # exist in the local working copy. Therefore, we want to run all
3226 # ls-files commands relative to the repository toplevel.
3227 toplevel="$(git rev-parse --show-toplevel)/"
3229 __git_dequote "$cur"
3231 # If the paths provided by the user already start with '/', then
3232 # they are considered relative to the toplevel of the repository
3233 # already. If they do not start with /, then we need to adjust
3234 # them to start with the appropriate prefix.
3235 case "$cur" in
3237 cur="${cur:1}"
3240 pfx="$(__git rev-parse --show-prefix)"
3241 esac
3243 # Since sparse-index is limited to cone-mode, in non-cone-mode the
3244 # list of valid paths is precisely the cached files in the index.
3246 # NEEDSWORK:
3247 # 1) We probably need to take care of cases where ls-files
3248 # responds with special quoting.
3249 # 2) We probably need to take care of cases where ${cur} has
3250 # some kind of special quoting.
3251 # 3) On top of any quoting from 1 & 2, we have to provide an extra
3252 # level of quoting for any paths that contain a '*', '?', '\',
3253 # '[', ']', or leading '#' or '!' since those will be
3254 # interpreted by sparse-checkout as something other than a
3255 # literal path character.
3256 # Since there are two types of quoting here, this might get really
3257 # complex. For now, just punt on all of this...
3258 completions="$(__git -C "${toplevel}" -c core.quotePath=false \
3259 ls-files --cached -- "${pfx}${cur}*" \
3260 | sed -e s%^%/% -e 's%$% %')"
3261 # Note, above, though that we needed all of the completions to be
3262 # prefixed with a '/', and we want to add a space so that bash
3263 # completion will actually complete an entry and let us move on to
3264 # the next one.
3266 # Return what we've found.
3267 if test -n "$completions"; then
3268 # We found some completions; return them
3269 local IFS=$'\n'
3270 COMPREPLY=($completions)
3271 else
3272 # Do NOT fall back to bash-style all-local-files-and-dirs
3273 # when we find no match. Such options are worse than
3274 # useless:
3275 # 1. "git sparse-checkout add" needs paths that are NOT
3276 # currently in the working copy. "git
3277 # sparse-checkout set" does as well, except in the
3278 # special cases when users are only trying to narrow
3279 # their sparse checkout to a subset of what they
3280 # already have.
3282 # 2. A path like '.config' is ambiguous as to whether
3283 # the user wants all '.config' files throughout the
3284 # tree, or just the one under the current directory.
3285 # It would result in a warning from the
3286 # sparse-checkout command due to this. As such, all
3287 # completions of paths should be prefixed with a
3288 # '/'.
3290 # 3. We don't want paths prefixed with a '/' to
3291 # complete files in the system root directory, we
3292 # want it to complete on files relative to the
3293 # repository root.
3295 # As such, make sure that NO completions are offered rather
3296 # than falling back to bash's default completions.
3297 COMPREPLY=( "" )
3301 _git_sparse_checkout ()
3303 local subcommands="list init set disable add reapply"
3304 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3305 local using_cone=true
3306 if [ -z "$subcommand" ]; then
3307 __gitcomp "$subcommands"
3308 return
3311 case "$subcommand,$cur" in
3312 *,--*)
3313 __gitcomp_builtin sparse-checkout_$subcommand "" "--"
3315 set,*|add,*)
3316 if [[ "$(__git config core.sparseCheckout)" == "true" &&
3317 "$(__git config core.sparseCheckoutCone)" == "false" &&
3318 -z "$(__git_find_on_cmdline --cone)" ]]; then
3319 using_cone=false
3321 if [[ -n "$(__git_find_on_cmdline --no-cone)" ]]; then
3322 using_cone=false
3324 if [[ "$using_cone" == "true" ]]; then
3325 __gitcomp_directories
3326 else
3327 __gitcomp_slash_leading_paths
3329 esac
3332 _git_stash ()
3334 local subcommands='push list show apply clear drop pop create branch'
3335 local subcommand="$(__git_find_on_cmdline "$subcommands save")"
3337 if [ -z "$subcommand" ]; then
3338 case "$((cword - __git_cmd_idx)),$cur" in
3339 *,--*)
3340 __gitcomp_builtin stash_push
3342 1,sa*)
3343 __gitcomp "save"
3345 1,*)
3346 __gitcomp "$subcommands"
3348 esac
3349 return
3352 case "$subcommand,$cur" in
3353 list,--*)
3354 # NEEDSWORK: can we somehow unify this with the options in _git_log() and _git_show()
3355 __gitcomp_builtin stash_list "$__git_log_common_options $__git_diff_common_options"
3357 show,--*)
3358 __gitcomp_builtin stash_show "$__git_diff_common_options"
3360 *,--*)
3361 __gitcomp_builtin "stash_$subcommand"
3363 branch,*)
3364 if [ $cword -eq $((__git_cmd_idx+2)) ]; then
3365 __git_complete_refs
3366 else
3367 __gitcomp_nl "$(__git stash list \
3368 | sed -n -e 's/:.*//p')"
3371 show,*|apply,*|drop,*|pop,*)
3372 __gitcomp_nl "$(__git stash list \
3373 | sed -n -e 's/:.*//p')"
3375 esac
3378 _git_submodule ()
3380 __git_has_doubledash && return
3382 local subcommands="add status init deinit update set-branch set-url summary foreach sync absorbgitdirs"
3383 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3384 if [ -z "$subcommand" ]; then
3385 case "$cur" in
3386 --*)
3387 __gitcomp "--quiet"
3390 __gitcomp "$subcommands"
3392 esac
3393 return
3396 case "$subcommand,$cur" in
3397 add,--*)
3398 __gitcomp "--branch --force --name --reference --depth"
3400 status,--*)
3401 __gitcomp "--cached --recursive"
3403 deinit,--*)
3404 __gitcomp "--force --all"
3406 update,--*)
3407 __gitcomp "
3408 --init --remote --no-fetch
3409 --recommend-shallow --no-recommend-shallow
3410 --force --rebase --merge --reference --depth --recursive --jobs
3413 set-branch,--*)
3414 __gitcomp "--default --branch"
3416 summary,--*)
3417 __gitcomp "--cached --files --summary-limit"
3419 foreach,--*|sync,--*)
3420 __gitcomp "--recursive"
3424 esac
3427 _git_svn ()
3429 local subcommands="
3430 init fetch clone rebase dcommit log find-rev
3431 set-tree commit-diff info create-ignore propget
3432 proplist show-ignore show-externals branch tag blame
3433 migrate mkdirs reset gc
3435 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3436 if [ -z "$subcommand" ]; then
3437 __gitcomp "$subcommands"
3438 else
3439 local remote_opts="--username= --config-dir= --no-auth-cache"
3440 local fc_opts="
3441 --follow-parent --authors-file= --repack=
3442 --no-metadata --use-svm-props --use-svnsync-props
3443 --log-window-size= --no-checkout --quiet
3444 --repack-flags --use-log-author --localtime
3445 --add-author-from
3446 --recursive
3447 --ignore-paths= --include-paths= $remote_opts
3449 local init_opts="
3450 --template= --shared= --trunk= --tags=
3451 --branches= --stdlayout --minimize-url
3452 --no-metadata --use-svm-props --use-svnsync-props
3453 --rewrite-root= --prefix= $remote_opts
3455 local cmt_opts="
3456 --edit --rmdir --find-copies-harder --copy-similarity=
3459 case "$subcommand,$cur" in
3460 fetch,--*)
3461 __gitcomp "--revision= --fetch-all $fc_opts"
3463 clone,--*)
3464 __gitcomp "--revision= $fc_opts $init_opts"
3466 init,--*)
3467 __gitcomp "$init_opts"
3469 dcommit,--*)
3470 __gitcomp "
3471 --merge --strategy= --verbose --dry-run
3472 --fetch-all --no-rebase --commit-url
3473 --revision --interactive $cmt_opts $fc_opts
3476 set-tree,--*)
3477 __gitcomp "--stdin $cmt_opts $fc_opts"
3479 create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
3480 show-externals,--*|mkdirs,--*)
3481 __gitcomp "--revision="
3483 log,--*)
3484 __gitcomp "
3485 --limit= --revision= --verbose --incremental
3486 --oneline --show-commit --non-recursive
3487 --authors-file= --color
3490 rebase,--*)
3491 __gitcomp "
3492 --merge --verbose --strategy= --local
3493 --fetch-all --dry-run $fc_opts
3496 commit-diff,--*)
3497 __gitcomp "--message= --file= --revision= $cmt_opts"
3499 info,--*)
3500 __gitcomp "--url"
3502 branch,--*)
3503 __gitcomp "--dry-run --message --tag"
3505 tag,--*)
3506 __gitcomp "--dry-run --message"
3508 blame,--*)
3509 __gitcomp "--git-format"
3511 migrate,--*)
3512 __gitcomp "
3513 --config-dir= --ignore-paths= --minimize
3514 --no-auth-cache --username=
3517 reset,--*)
3518 __gitcomp "--revision= --parent"
3522 esac
3526 _git_tag ()
3528 local i c="$__git_cmd_idx" f=0
3529 while [ $c -lt $cword ]; do
3530 i="${words[c]}"
3531 case "$i" in
3532 -d|--delete|-v|--verify)
3533 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3534 return
3539 esac
3540 ((c++))
3541 done
3543 case "$prev" in
3544 -m|-F)
3546 -*|tag)
3547 if [ $f = 1 ]; then
3548 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3552 __git_complete_refs
3554 esac
3556 case "$cur" in
3557 --*)
3558 __gitcomp_builtin tag
3560 esac
3563 _git_whatchanged ()
3565 _git_log
3568 __git_complete_worktree_paths ()
3570 local IFS=$'\n'
3571 # Generate completion reply from worktree list skipping the first
3572 # entry: it's the path of the main worktree, which can't be moved,
3573 # removed, locked, etc.
3574 __gitcomp_nl "$(git worktree list --porcelain |
3575 sed -n -e '2,$ s/^worktree //p')"
3578 _git_worktree ()
3580 local subcommands="add list lock move prune remove unlock"
3581 local subcommand subcommand_idx
3583 subcommand="$(__git_find_on_cmdline --show-idx "$subcommands")"
3584 subcommand_idx="${subcommand% *}"
3585 subcommand="${subcommand#* }"
3587 case "$subcommand,$cur" in
3589 __gitcomp "$subcommands"
3591 *,--*)
3592 __gitcomp_builtin worktree_$subcommand
3594 add,*) # usage: git worktree add [<options>] <path> [<commit-ish>]
3595 # Here we are not completing an --option, it's either the
3596 # path or a ref.
3597 case "$prev" in
3598 -b|-B) # Complete refs for branch to be created/reseted.
3599 __git_complete_refs
3601 -*) # The previous word is an -o|--option without an
3602 # unstuck argument: have to complete the path for
3603 # the new worktree, so don't list anything, but let
3604 # Bash fall back to filename completion.
3606 *) # The previous word is not an --option, so it must
3607 # be either the 'add' subcommand, the unstuck
3608 # argument of an option (e.g. branch for -b|-B), or
3609 # the path for the new worktree.
3610 if [ $cword -eq $((subcommand_idx+1)) ]; then
3611 # Right after the 'add' subcommand: have to
3612 # complete the path, so fall back to Bash
3613 # filename completion.
3615 else
3616 case "${words[cword-2]}" in
3617 -b|-B) # After '-b <branch>': have to
3618 # complete the path, so fall back
3619 # to Bash filename completion.
3621 *) # After the path: have to complete
3622 # the ref to be checked out.
3623 __git_complete_refs
3625 esac
3628 esac
3630 lock,*|remove,*|unlock,*)
3631 __git_complete_worktree_paths
3633 move,*)
3634 if [ $cword -eq $((subcommand_idx+1)) ]; then
3635 # The first parameter must be an existing working
3636 # tree to be moved.
3637 __git_complete_worktree_paths
3638 else
3639 # The second parameter is the destination: it could
3640 # be any path, so don't list anything, but let Bash
3641 # fall back to filename completion.
3645 esac
3648 __git_complete_common () {
3649 local command="$1"
3651 case "$cur" in
3652 --*)
3653 __gitcomp_builtin "$command"
3655 esac
3658 __git_cmds_with_parseopt_helper=
3659 __git_support_parseopt_helper () {
3660 test -n "$__git_cmds_with_parseopt_helper" ||
3661 __git_cmds_with_parseopt_helper="$(__git --list-cmds=parseopt)"
3663 case " $__git_cmds_with_parseopt_helper " in
3664 *" $1 "*)
3665 return 0
3668 return 1
3670 esac
3673 __git_have_func () {
3674 declare -f -- "$1" >/dev/null 2>&1
3677 __git_complete_command () {
3678 local command="$1"
3679 local completion_func="_git_${command//-/_}"
3680 if ! __git_have_func $completion_func &&
3681 __git_have_func _completion_loader
3682 then
3683 _completion_loader "git-$command"
3685 if __git_have_func $completion_func
3686 then
3687 $completion_func
3688 return 0
3689 elif __git_support_parseopt_helper "$command"
3690 then
3691 __git_complete_common "$command"
3692 return 0
3693 else
3694 return 1
3698 __git_main ()
3700 local i c=1 command __git_dir __git_repo_path
3701 local __git_C_args C_args_count=0
3702 local __git_cmd_idx
3704 while [ $c -lt $cword ]; do
3705 i="${words[c]}"
3706 case "$i" in
3707 --git-dir=*)
3708 __git_dir="${i#--git-dir=}"
3710 --git-dir)
3711 ((c++))
3712 __git_dir="${words[c]}"
3714 --bare)
3715 __git_dir="."
3717 --help)
3718 command="help"
3719 break
3721 -c|--work-tree|--namespace)
3722 ((c++))
3725 __git_C_args[C_args_count++]=-C
3726 ((c++))
3727 __git_C_args[C_args_count++]="${words[c]}"
3732 command="$i"
3733 __git_cmd_idx="$c"
3734 break
3736 esac
3737 ((c++))
3738 done
3740 if [ -z "${command-}" ]; then
3741 case "$prev" in
3742 --git-dir|-C|--work-tree)
3743 # these need a path argument, let's fall back to
3744 # Bash filename completion
3745 return
3748 __git_complete_config_variable_name_and_value
3749 return
3751 --namespace)
3752 # we don't support completing these options' arguments
3753 return
3755 esac
3756 case "$cur" in
3757 --*)
3758 __gitcomp "
3759 --paginate
3760 --no-pager
3761 --git-dir=
3762 --bare
3763 --version
3764 --exec-path
3765 --exec-path=
3766 --html-path
3767 --man-path
3768 --info-path
3769 --work-tree=
3770 --namespace=
3771 --no-replace-objects
3772 --help
3776 if test -n "${GIT_TESTING_PORCELAIN_COMMAND_LIST-}"
3777 then
3778 __gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
3779 else
3780 local list_cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config
3782 if test "${GIT_COMPLETION_SHOW_ALL_COMMANDS-}" = "1"
3783 then
3784 list_cmds=builtins,$list_cmds
3786 __gitcomp "$(__git --list-cmds=$list_cmds)"
3789 esac
3790 return
3793 __git_complete_command "$command" && return
3795 local expansion=$(__git_aliased_command "$command")
3796 if [ -n "$expansion" ]; then
3797 words[1]=$expansion
3798 __git_complete_command "$expansion"
3802 __gitk_main ()
3804 __git_has_doubledash && return
3806 local __git_repo_path
3807 __git_find_repo_path
3809 local merge=""
3810 if __git_pseudoref_exists MERGE_HEAD; then
3811 merge="--merge"
3813 case "$cur" in
3814 --*)
3815 __gitcomp "
3816 $__git_log_common_options
3817 $__git_log_gitk_options
3818 $merge
3820 return
3822 esac
3823 __git_complete_revlist
3826 if [[ -n ${ZSH_VERSION-} && -z ${GIT_SOURCING_ZSH_COMPLETION-} ]]; then
3827 echo "ERROR: this script is obsolete, please see git-completion.zsh" 1>&2
3828 return
3831 __git_func_wrap ()
3833 local cur words cword prev
3834 local __git_cmd_idx=0
3835 _get_comp_words_by_ref -n =: cur words cword prev
3839 ___git_complete ()
3841 local wrapper="__git_wrap${2}"
3842 eval "$wrapper () { __git_func_wrap $2 ; }"
3843 complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
3844 || complete -o default -o nospace -F $wrapper $1
3847 # Setup the completion for git commands
3848 # 1: command or alias
3849 # 2: function to call (e.g. `git`, `gitk`, `git_fetch`)
3850 __git_complete ()
3852 local func
3854 if __git_have_func $2; then
3855 func=$2
3856 elif __git_have_func __$2_main; then
3857 func=__$2_main
3858 elif __git_have_func _$2; then
3859 func=_$2
3860 else
3861 echo "ERROR: could not find function '$2'" 1>&2
3862 return 1
3864 ___git_complete $1 $func
3867 ___git_complete git __git_main
3868 ___git_complete gitk __gitk_main
3870 # The following are necessary only for Cygwin, and only are needed
3871 # when the user has tab-completed the executable name and consequently
3872 # included the '.exe' suffix.
3874 if [ "$OSTYPE" = cygwin ]; then
3875 ___git_complete git.exe __git_main