completion: introduce __git_find_subcommand
[git/gitster.git] / contrib / completion / git-completion.bash
blobdc5f73a9f399a4a9fed4de01ef9bd37a662ec2b9
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
141 # If the reftable is in use, we have to shell out to 'git rev-parse'
142 # to determine whether the ref exists instead of looking directly in
143 # the filesystem to determine whether the ref exists. Otherwise, use
144 # Bash builtins since executing Git commands are expensive on some
145 # platforms.
146 if __git_eread "$__git_repo_path/HEAD" head; then
147 b="${head#ref: }"
148 if [ "$b" == "refs/heads/.invalid" ]; then
149 __git -C "$__git_repo_path" rev-parse --verify --quiet "$ref" 2>/dev/null
150 return $?
154 [ -f "$__git_repo_path/$ref" ]
157 # Removes backslash escaping, single quotes and double quotes from a word,
158 # stores the result in the variable $dequoted_word.
159 # 1: The word to dequote.
160 __git_dequote ()
162 local rest="$1" len ch
164 dequoted_word=""
166 while test -n "$rest"; do
167 len=${#dequoted_word}
168 dequoted_word="$dequoted_word${rest%%[\\\'\"]*}"
169 rest="${rest:$((${#dequoted_word}-$len))}"
171 case "${rest:0:1}" in
173 ch="${rest:1:1}"
174 case "$ch" in
175 $'\n')
178 dequoted_word="$dequoted_word$ch"
180 esac
181 rest="${rest:2}"
184 rest="${rest:1}"
185 len=${#dequoted_word}
186 dequoted_word="$dequoted_word${rest%%\'*}"
187 rest="${rest:$((${#dequoted_word}-$len+1))}"
190 rest="${rest:1}"
191 while test -n "$rest" ; do
192 len=${#dequoted_word}
193 dequoted_word="$dequoted_word${rest%%[\\\"]*}"
194 rest="${rest:$((${#dequoted_word}-$len))}"
195 case "${rest:0:1}" in
197 ch="${rest:1:1}"
198 case "$ch" in
199 \"|\\|\$|\`)
200 dequoted_word="$dequoted_word$ch"
202 $'\n')
205 dequoted_word="$dequoted_word\\$ch"
207 esac
208 rest="${rest:2}"
211 rest="${rest:1}"
212 break
214 esac
215 done
217 esac
218 done
221 # The following function is based on code from:
223 # bash_completion - programmable completion functions for bash 3.2+
225 # Copyright © 2006-2008, Ian Macdonald <ian@caliban.org>
226 # © 2009-2010, Bash Completion Maintainers
227 # <bash-completion-devel@lists.alioth.debian.org>
229 # This program is free software; you can redistribute it and/or modify
230 # it under the terms of the GNU General Public License as published by
231 # the Free Software Foundation; either version 2, or (at your option)
232 # any later version.
234 # This program is distributed in the hope that it will be useful,
235 # but WITHOUT ANY WARRANTY; without even the implied warranty of
236 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
237 # GNU General Public License for more details.
239 # You should have received a copy of the GNU General Public License
240 # along with this program; if not, see <http://www.gnu.org/licenses/>.
242 # The latest version of this software can be obtained here:
244 # http://bash-completion.alioth.debian.org/
246 # RELEASE: 2.x
248 # This function can be used to access a tokenized list of words
249 # on the command line:
251 # __git_reassemble_comp_words_by_ref '=:'
252 # if test "${words_[cword_-1]}" = -w
253 # then
254 # ...
255 # fi
257 # The argument should be a collection of characters from the list of
258 # word completion separators (COMP_WORDBREAKS) to treat as ordinary
259 # characters.
261 # This is roughly equivalent to going back in time and setting
262 # COMP_WORDBREAKS to exclude those characters. The intent is to
263 # make option types like --date=<type> and <rev>:<path> easy to
264 # recognize by treating each shell word as a single token.
266 # It is best not to set COMP_WORDBREAKS directly because the value is
267 # shared with other completion scripts. By the time the completion
268 # function gets called, COMP_WORDS has already been populated so local
269 # changes to COMP_WORDBREAKS have no effect.
271 # Output: words_, cword_, cur_.
273 __git_reassemble_comp_words_by_ref()
275 local exclude i j first
276 # Which word separators to exclude?
277 exclude="${1//[^$COMP_WORDBREAKS]}"
278 cword_=$COMP_CWORD
279 if [ -z "$exclude" ]; then
280 words_=("${COMP_WORDS[@]}")
281 return
283 # List of word completion separators has shrunk;
284 # re-assemble words to complete.
285 for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
286 # Append each nonempty word consisting of just
287 # word separator characters to the current word.
288 first=t
289 while
290 [ $i -gt 0 ] &&
291 [ -n "${COMP_WORDS[$i]}" ] &&
292 # word consists of excluded word separators
293 [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
295 # Attach to the previous token,
296 # unless the previous token is the command name.
297 if [ $j -ge 2 ] && [ -n "$first" ]; then
298 ((j--))
300 first=
301 words_[$j]=${words_[j]}${COMP_WORDS[i]}
302 if [ $i = $COMP_CWORD ]; then
303 cword_=$j
305 if (($i < ${#COMP_WORDS[@]} - 1)); then
306 ((i++))
307 else
308 # Done.
309 return
311 done
312 words_[$j]=${words_[j]}${COMP_WORDS[i]}
313 if [ $i = $COMP_CWORD ]; then
314 cword_=$j
316 done
319 if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
320 _get_comp_words_by_ref ()
322 local exclude cur_ words_ cword_
323 if [ "$1" = "-n" ]; then
324 exclude=$2
325 shift 2
327 __git_reassemble_comp_words_by_ref "$exclude"
328 cur_=${words_[cword_]}
329 while [ $# -gt 0 ]; do
330 case "$1" in
331 cur)
332 cur=$cur_
334 prev)
335 prev=${words_[$cword_-1]}
337 words)
338 words=("${words_[@]}")
340 cword)
341 cword=$cword_
343 esac
344 shift
345 done
349 # Fills the COMPREPLY array with prefiltered words without any additional
350 # processing.
351 # Callers must take care of providing only words that match the current word
352 # to be completed and adding any prefix and/or suffix (trailing space!), if
353 # necessary.
354 # 1: List of newline-separated matching completion words, complete with
355 # prefix and suffix.
356 __gitcomp_direct ()
358 local IFS=$'\n'
360 COMPREPLY=($1)
363 # Similar to __gitcomp_direct, but appends to COMPREPLY instead.
364 # Callers must take care of providing only words that match the current word
365 # to be completed and adding any prefix and/or suffix (trailing space!), if
366 # necessary.
367 # 1: List of newline-separated matching completion words, complete with
368 # prefix and suffix.
369 __gitcomp_direct_append ()
371 local IFS=$'\n'
373 COMPREPLY+=($1)
376 __gitcompappend ()
378 local x i=${#COMPREPLY[@]}
379 for x in $1; do
380 if [[ "$x" == "$3"* ]]; then
381 COMPREPLY[i++]="$2$x$4"
383 done
386 __gitcompadd ()
388 COMPREPLY=()
389 __gitcompappend "$@"
392 # Generates completion reply, appending a space to possible completion words,
393 # if necessary.
394 # It accepts 1 to 4 arguments:
395 # 1: List of possible completion words.
396 # 2: A prefix to be added to each possible completion word (optional).
397 # 3: Generate possible completion matches for this word (optional).
398 # 4: A suffix to be appended to each possible completion word (optional).
399 __gitcomp ()
401 local cur_="${3-$cur}"
403 case "$cur_" in
406 --no-*)
407 local c i=0 IFS=$' \t\n'
408 for c in $1; do
409 if [[ $c == "--" ]]; then
410 continue
412 c="$c${4-}"
413 if [[ $c == "$cur_"* ]]; then
414 case $c in
415 --*=|*.) ;;
416 *) c="$c " ;;
417 esac
418 COMPREPLY[i++]="${2-}$c"
420 done
423 local c i=0 IFS=$' \t\n'
424 for c in $1; do
425 if [[ $c == "--" ]]; then
426 c="--no-...${4-}"
427 if [[ $c == "$cur_"* ]]; then
428 COMPREPLY[i++]="${2-}$c "
430 break
432 c="$c${4-}"
433 if [[ $c == "$cur_"* ]]; then
434 case $c in
435 *=|*.) ;;
436 *) c="$c " ;;
437 esac
438 COMPREPLY[i++]="${2-}$c"
440 done
442 esac
445 # Clear the variables caching builtins' options when (re-)sourcing
446 # the completion script.
447 if [[ -n ${ZSH_VERSION-} ]]; then
448 unset ${(M)${(k)parameters[@]}:#__gitcomp_builtin_*} 2>/dev/null
449 else
450 unset $(compgen -v __gitcomp_builtin_)
453 # This function is equivalent to
455 # __gitcomp "$(git xxx --git-completion-helper) ..."
457 # except that the output is cached. Accept 1-3 arguments:
458 # 1: the git command to execute, this is also the cache key
459 # 2: extra options to be added on top (e.g. negative forms)
460 # 3: options to be excluded
461 __gitcomp_builtin ()
463 # spaces must be replaced with underscore for multi-word
464 # commands, e.g. "git remote add" becomes remote_add.
465 local cmd="$1"
466 local incl="${2-}"
467 local excl="${3-}"
469 local var=__gitcomp_builtin_"${cmd//-/_}"
470 local options
471 eval "options=\${$var-}"
473 if [ -z "$options" ]; then
474 local completion_helper
475 if [ "${GIT_COMPLETION_SHOW_ALL-}" = "1" ]; then
476 completion_helper="--git-completion-helper-all"
477 else
478 completion_helper="--git-completion-helper"
480 # leading and trailing spaces are significant to make
481 # option removal work correctly.
482 options=" $incl $(__git ${cmd/_/ } $completion_helper) " || return
484 for i in $excl; do
485 options="${options/ $i / }"
486 done
487 eval "$var=\"$options\""
490 __gitcomp "$options"
493 # Variation of __gitcomp_nl () that appends to the existing list of
494 # completion candidates, COMPREPLY.
495 __gitcomp_nl_append ()
497 local IFS=$'\n'
498 __gitcompappend "$1" "${2-}" "${3-$cur}" "${4- }"
501 # Generates completion reply from newline-separated possible completion words
502 # by appending a space to all of them.
503 # It accepts 1 to 4 arguments:
504 # 1: List of possible completion words, separated by a single newline.
505 # 2: A prefix to be added to each possible completion word (optional).
506 # 3: Generate possible completion matches for this word (optional).
507 # 4: A suffix to be appended to each possible completion word instead of
508 # the default space (optional). If specified but empty, nothing is
509 # appended.
510 __gitcomp_nl ()
512 COMPREPLY=()
513 __gitcomp_nl_append "$@"
516 # Fills the COMPREPLY array with prefiltered paths without any additional
517 # processing.
518 # Callers must take care of providing only paths that match the current path
519 # to be completed and adding any prefix path components, if necessary.
520 # 1: List of newline-separated matching paths, complete with all prefix
521 # path components.
522 __gitcomp_file_direct ()
524 local IFS=$'\n'
526 COMPREPLY=($1)
528 # use a hack to enable file mode in bash < 4
529 compopt -o filenames +o nospace 2>/dev/null ||
530 compgen -f /non-existing-dir/ >/dev/null ||
531 true
534 # Generates completion reply with compgen from newline-separated possible
535 # completion filenames.
536 # It accepts 1 to 3 arguments:
537 # 1: List of possible completion filenames, separated by a single newline.
538 # 2: A directory prefix to be added to each possible completion filename
539 # (optional).
540 # 3: Generate possible completion matches for this word (optional).
541 __gitcomp_file ()
543 local IFS=$'\n'
545 # XXX does not work when the directory prefix contains a tilde,
546 # since tilde expansion is not applied.
547 # This means that COMPREPLY will be empty and Bash default
548 # completion will be used.
549 __gitcompadd "$1" "${2-}" "${3-$cur}" ""
551 # use a hack to enable file mode in bash < 4
552 compopt -o filenames +o nospace 2>/dev/null ||
553 compgen -f /non-existing-dir/ >/dev/null ||
554 true
557 # Find the current subcommand for commands that follow the syntax:
559 # git <command> <subcommand>
561 # 1: List of possible subcommands.
562 # 2: Optional subcommand to return when none is found.
563 __git_find_subcommand ()
565 local subcommand subcommands="$1" default_subcommand="$2"
567 for subcommand in $subcommands; do
568 if [ "$subcommand" = "${words[__git_cmd_idx+1]}" ]; then
569 echo $subcommand
570 return
572 done
574 echo $default_subcommand
577 # Execute 'git ls-files', unless the --committable option is specified, in
578 # which case it runs 'git diff-index' to find out the files that can be
579 # committed. It return paths relative to the directory specified in the first
580 # argument, and using the options specified in the second argument.
581 __git_ls_files_helper ()
583 if [ "$2" = "--committable" ]; then
584 __git -C "$1" -c core.quotePath=false diff-index \
585 --name-only --relative HEAD -- "${3//\\/\\\\}*"
586 else
587 # NOTE: $2 is not quoted in order to support multiple options
588 __git -C "$1" -c core.quotePath=false ls-files \
589 --exclude-standard $2 -- "${3//\\/\\\\}*"
594 # __git_index_files accepts 1 or 2 arguments:
595 # 1: Options to pass to ls-files (required).
596 # 2: A directory path (optional).
597 # If provided, only files within the specified directory are listed.
598 # Sub directories are never recursed. Path must have a trailing
599 # slash.
600 # 3: List only paths matching this path component (optional).
601 __git_index_files ()
603 local root="$2" match="$3"
605 __git_ls_files_helper "$root" "$1" "${match:-?}" |
606 awk -F / -v pfx="${2//\\/\\\\}" '{
607 paths[$1] = 1
609 END {
610 for (p in paths) {
611 if (substr(p, 1, 1) != "\"") {
612 # No special characters, easy!
613 print pfx p
614 continue
617 # The path is quoted.
618 p = dequote(p)
619 if (p == "")
620 continue
622 # Even when a directory name itself does not contain
623 # any special characters, it will still be quoted if
624 # any of its (stripped) trailing path components do.
625 # Because of this we may have seen the same directory
626 # both quoted and unquoted.
627 if (p in paths)
628 # We have seen the same directory unquoted,
629 # skip it.
630 continue
631 else
632 print pfx p
635 function dequote(p, bs_idx, out, esc, esc_idx, dec) {
636 # Skip opening double quote.
637 p = substr(p, 2)
639 # Interpret backslash escape sequences.
640 while ((bs_idx = index(p, "\\")) != 0) {
641 out = out substr(p, 1, bs_idx - 1)
642 esc = substr(p, bs_idx + 1, 1)
643 p = substr(p, bs_idx + 2)
645 if ((esc_idx = index("abtvfr\"\\", esc)) != 0) {
646 # C-style one-character escape sequence.
647 out = out substr("\a\b\t\v\f\r\"\\",
648 esc_idx, 1)
649 } else if (esc == "n") {
650 # Uh-oh, a newline character.
651 # We cannot reliably put a pathname
652 # containing a newline into COMPREPLY,
653 # and the newline would create a mess.
654 # Skip this path.
655 return ""
656 } else {
657 # Must be a \nnn octal value, then.
658 dec = esc * 64 + \
659 substr(p, 1, 1) * 8 + \
660 substr(p, 2, 1)
661 out = out sprintf("%c", dec)
662 p = substr(p, 3)
665 # Drop closing double quote, if there is one.
666 # (There is not any if this is a directory, as it was
667 # already stripped with the trailing path components.)
668 if (substr(p, length(p), 1) == "\"")
669 out = out substr(p, 1, length(p) - 1)
670 else
671 out = out p
673 return out
677 # __git_complete_index_file requires 1 argument:
678 # 1: the options to pass to ls-file
680 # The exception is --committable, which finds the files appropriate commit.
681 __git_complete_index_file ()
683 local dequoted_word pfx="" cur_
685 __git_dequote "$cur"
687 case "$dequoted_word" in
688 ?*/*)
689 pfx="${dequoted_word%/*}/"
690 cur_="${dequoted_word##*/}"
693 cur_="$dequoted_word"
694 esac
696 __gitcomp_file_direct "$(__git_index_files "$1" "$pfx" "$cur_")"
699 # Lists branches from the local repository.
700 # 1: A prefix to be added to each listed branch (optional).
701 # 2: List only branches matching this word (optional; list all branches if
702 # unset or empty).
703 # 3: A suffix to be appended to each listed branch (optional).
704 __git_heads ()
706 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
708 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
709 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
710 "refs/heads/$cur_*" "refs/heads/$cur_*/**"
713 # Lists branches from remote repositories.
714 # 1: A prefix to be added to each listed branch (optional).
715 # 2: List only branches matching this word (optional; list all branches if
716 # unset or empty).
717 # 3: A suffix to be appended to each listed branch (optional).
718 __git_remote_heads ()
720 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
722 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
723 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
724 "refs/remotes/$cur_*" "refs/remotes/$cur_*/**"
727 # Lists tags from the local repository.
728 # Accepts the same positional parameters as __git_heads() above.
729 __git_tags ()
731 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
733 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
734 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
735 "refs/tags/$cur_*" "refs/tags/$cur_*/**"
738 # List unique branches from refs/remotes used for 'git checkout' and 'git
739 # switch' tracking DWIMery.
740 # 1: A prefix to be added to each listed branch (optional)
741 # 2: List only branches matching this word (optional; list all branches if
742 # unset or empty).
743 # 3: A suffix to be appended to each listed branch (optional).
744 __git_dwim_remote_heads ()
746 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
747 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
749 # employ the heuristic used by git checkout and git switch
750 # Try to find a remote branch that cur_es the completion word
751 # but only output if the branch name is unique
752 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
753 --sort="refname:strip=3" \
754 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
755 "refs/remotes/*/$cur_*" "refs/remotes/*/$cur_*/**" | \
756 uniq -u
759 # Lists refs from the local (by default) or from a remote repository.
760 # It accepts 0, 1 or 2 arguments:
761 # 1: The remote to list refs from (optional; ignored, if set but empty).
762 # Can be the name of a configured remote, a path, or a URL.
763 # 2: In addition to local refs, list unique branches from refs/remotes/ for
764 # 'git checkout's tracking DWIMery (optional; ignored, if set but empty).
765 # 3: A prefix to be added to each listed ref (optional).
766 # 4: List only refs matching this word (optional; list all refs if unset or
767 # empty).
768 # 5: A suffix to be appended to each listed ref (optional; ignored, if set
769 # but empty).
771 # Use __git_complete_refs() instead.
772 __git_refs ()
774 local i hash dir track="${2-}"
775 local list_refs_from=path remote="${1-}"
776 local format refs
777 local pfx="${3-}" cur_="${4-$cur}" sfx="${5-}"
778 local match="${4-}"
779 local umatch="${4-}"
780 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
782 __git_find_repo_path
783 dir="$__git_repo_path"
785 if [ -z "$remote" ]; then
786 if [ -z "$dir" ]; then
787 return
789 else
790 if __git_is_configured_remote "$remote"; then
791 # configured remote takes precedence over a
792 # local directory with the same name
793 list_refs_from=remote
794 elif [ -d "$remote/.git" ]; then
795 dir="$remote/.git"
796 elif [ -d "$remote" ]; then
797 dir="$remote"
798 else
799 list_refs_from=url
803 if test "${GIT_COMPLETION_IGNORE_CASE:+1}" = "1"
804 then
805 # uppercase with tr instead of ${match,^^} for bash 3.2 compatibility
806 umatch=$(echo "$match" | tr a-z A-Z 2>/dev/null || echo "$match")
809 if [ "$list_refs_from" = path ]; then
810 if [[ "$cur_" == ^* ]]; then
811 pfx="$pfx^"
812 fer_pfx="$fer_pfx^"
813 cur_=${cur_#^}
814 match=${match#^}
815 umatch=${umatch#^}
817 case "$cur_" in
818 refs|refs/*)
819 format="refname"
820 refs=("$match*" "$match*/**")
821 track=""
824 for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD REBASE_HEAD CHERRY_PICK_HEAD REVERT_HEAD BISECT_HEAD AUTO_MERGE; do
825 case "$i" in
826 $match*|$umatch*)
827 if [ -e "$dir/$i" ]; then
828 echo "$pfx$i$sfx"
831 esac
832 done
833 format="refname:strip=2"
834 refs=("refs/tags/$match*" "refs/tags/$match*/**"
835 "refs/heads/$match*" "refs/heads/$match*/**"
836 "refs/remotes/$match*" "refs/remotes/$match*/**")
838 esac
839 __git_dir="$dir" __git for-each-ref --format="$fer_pfx%($format)$sfx" \
840 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
841 "${refs[@]}"
842 if [ -n "$track" ]; then
843 __git_dwim_remote_heads "$pfx" "$match" "$sfx"
845 return
847 case "$cur_" in
848 refs|refs/*)
849 __git ls-remote "$remote" "$match*" | \
850 while read -r hash i; do
851 case "$i" in
852 *^{}) ;;
853 *) echo "$pfx$i$sfx" ;;
854 esac
855 done
858 if [ "$list_refs_from" = remote ]; then
859 case "HEAD" in
860 $match*|$umatch*) echo "${pfx}HEAD$sfx" ;;
861 esac
862 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
863 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
864 "refs/remotes/$remote/$match*" \
865 "refs/remotes/$remote/$match*/**"
866 else
867 local query_symref
868 case "HEAD" in
869 $match*|$umatch*) query_symref="HEAD" ;;
870 esac
871 __git ls-remote "$remote" $query_symref \
872 "refs/tags/$match*" "refs/heads/$match*" \
873 "refs/remotes/$match*" |
874 while read -r hash i; do
875 case "$i" in
876 *^{}) ;;
877 refs/*) echo "$pfx${i#refs/*/}$sfx" ;;
878 *) echo "$pfx$i$sfx" ;; # symbolic refs
879 esac
880 done
883 esac
886 # Completes refs, short and long, local and remote, symbolic and pseudo.
888 # Usage: __git_complete_refs [<option>]...
889 # --remote=<remote>: The remote to list refs from, can be the name of a
890 # configured remote, a path, or a URL.
891 # --dwim: List unique remote branches for 'git switch's tracking DWIMery.
892 # --pfx=<prefix>: A prefix to be added to each ref.
893 # --cur=<word>: The current ref to be completed. Defaults to the current
894 # word to be completed.
895 # --sfx=<suffix>: A suffix to be appended to each ref instead of the default
896 # space.
897 # --mode=<mode>: What set of refs to complete, one of 'refs' (the default) to
898 # complete all refs, 'heads' to complete only branches, or
899 # 'remote-heads' to complete only remote branches. Note that
900 # --remote is only compatible with --mode=refs.
901 __git_complete_refs ()
903 local remote= dwim= pfx= cur_="$cur" sfx=" " mode="refs"
905 while test $# != 0; do
906 case "$1" in
907 --remote=*) remote="${1##--remote=}" ;;
908 --dwim) dwim="yes" ;;
909 # --track is an old spelling of --dwim
910 --track) dwim="yes" ;;
911 --pfx=*) pfx="${1##--pfx=}" ;;
912 --cur=*) cur_="${1##--cur=}" ;;
913 --sfx=*) sfx="${1##--sfx=}" ;;
914 --mode=*) mode="${1##--mode=}" ;;
915 *) return 1 ;;
916 esac
917 shift
918 done
920 # complete references based on the specified mode
921 case "$mode" in
922 refs)
923 __gitcomp_direct "$(__git_refs "$remote" "" "$pfx" "$cur_" "$sfx")" ;;
924 heads)
925 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" "$sfx")" ;;
926 remote-heads)
927 __gitcomp_direct "$(__git_remote_heads "$pfx" "$cur_" "$sfx")" ;;
929 return 1 ;;
930 esac
932 # Append DWIM remote branch names if requested
933 if [ "$dwim" = "yes" ]; then
934 __gitcomp_direct_append "$(__git_dwim_remote_heads "$pfx" "$cur_" "$sfx")"
938 # __git_refs2 requires 1 argument (to pass to __git_refs)
939 # Deprecated: use __git_complete_fetch_refspecs() instead.
940 __git_refs2 ()
942 local i
943 for i in $(__git_refs "$1"); do
944 echo "$i:$i"
945 done
948 # Completes refspecs for fetching from a remote repository.
949 # 1: The remote repository.
950 # 2: A prefix to be added to each listed refspec (optional).
951 # 3: The ref to be completed as a refspec instead of the current word to be
952 # completed (optional)
953 # 4: A suffix to be appended to each listed refspec instead of the default
954 # space (optional).
955 __git_complete_fetch_refspecs ()
957 local i remote="$1" pfx="${2-}" cur_="${3-$cur}" sfx="${4- }"
959 __gitcomp_direct "$(
960 for i in $(__git_refs "$remote" "" "" "$cur_") ; do
961 echo "$pfx$i:$i$sfx"
962 done
966 # __git_refs_remotes requires 1 argument (to pass to ls-remote)
967 __git_refs_remotes ()
969 local i hash
970 __git ls-remote "$1" 'refs/heads/*' | \
971 while read -r hash i; do
972 echo "$i:refs/remotes/$1/${i#refs/heads/}"
973 done
976 __git_remotes ()
978 __git_find_repo_path
979 test -d "$__git_repo_path/remotes" && ls -1 "$__git_repo_path/remotes"
980 __git remote
983 # Returns true if $1 matches the name of a configured remote, false otherwise.
984 __git_is_configured_remote ()
986 local remote
987 for remote in $(__git_remotes); do
988 if [ "$remote" = "$1" ]; then
989 return 0
991 done
992 return 1
995 __git_list_merge_strategies ()
997 LANG=C LC_ALL=C git merge -s help 2>&1 |
998 sed -n -e '/[Aa]vailable strategies are: /,/^$/{
999 s/\.$//
1000 s/.*://
1001 s/^[ ]*//
1002 s/[ ]*$//
1007 __git_merge_strategies=
1008 # 'git merge -s help' (and thus detection of the merge strategy
1009 # list) fails, unfortunately, if run outside of any git working
1010 # tree. __git_merge_strategies is set to the empty string in
1011 # that case, and the detection will be repeated the next time it
1012 # is needed.
1013 __git_compute_merge_strategies ()
1015 test -n "$__git_merge_strategies" ||
1016 __git_merge_strategies=$(__git_list_merge_strategies)
1019 __git_merge_strategy_options="ours theirs subtree subtree= patience
1020 histogram diff-algorithm= ignore-space-change ignore-all-space
1021 ignore-space-at-eol renormalize no-renormalize no-renames
1022 find-renames find-renames= rename-threshold="
1024 __git_complete_revlist_file ()
1026 local dequoted_word pfx ls ref cur_="$cur"
1027 case "$cur_" in
1028 *..?*:*)
1029 return
1031 ?*:*)
1032 ref="${cur_%%:*}"
1033 cur_="${cur_#*:}"
1035 __git_dequote "$cur_"
1037 case "$dequoted_word" in
1038 ?*/*)
1039 pfx="${dequoted_word%/*}"
1040 cur_="${dequoted_word##*/}"
1041 ls="$ref:$pfx"
1042 pfx="$pfx/"
1045 cur_="$dequoted_word"
1046 ls="$ref"
1048 esac
1050 case "$COMP_WORDBREAKS" in
1051 *:*) : great ;;
1052 *) pfx="$ref:$pfx" ;;
1053 esac
1055 __gitcomp_file "$(__git ls-tree "$ls" \
1056 | sed 's/^.* //
1057 s/$//')" \
1058 "$pfx" "$cur_"
1060 *...*)
1061 pfx="${cur_%...*}..."
1062 cur_="${cur_#*...}"
1063 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1065 *..*)
1066 pfx="${cur_%..*}.."
1067 cur_="${cur_#*..}"
1068 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1071 __git_complete_refs
1073 esac
1076 __git_complete_file ()
1078 __git_complete_revlist_file
1081 __git_complete_revlist ()
1083 __git_complete_revlist_file
1086 __git_complete_remote_or_refspec ()
1088 local cur_="$cur" cmd="${words[__git_cmd_idx]}"
1089 local i c=$((__git_cmd_idx+1)) remote="" pfx="" lhs=1 no_complete_refspec=0
1090 if [ "$cmd" = "remote" ]; then
1091 ((c++))
1093 while [ $c -lt $cword ]; do
1094 i="${words[c]}"
1095 case "$i" in
1096 --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
1097 -d|--delete) [ "$cmd" = "push" ] && lhs=0 ;;
1098 --all)
1099 case "$cmd" in
1100 push) no_complete_refspec=1 ;;
1101 fetch)
1102 return
1104 *) ;;
1105 esac
1107 --multiple) no_complete_refspec=1; break ;;
1108 -*) ;;
1109 *) remote="$i"; break ;;
1110 esac
1111 ((c++))
1112 done
1113 if [ -z "$remote" ]; then
1114 __gitcomp_nl "$(__git_remotes)"
1115 return
1117 if [ $no_complete_refspec = 1 ]; then
1118 return
1120 [ "$remote" = "." ] && remote=
1121 case "$cur_" in
1122 *:*)
1123 case "$COMP_WORDBREAKS" in
1124 *:*) : great ;;
1125 *) pfx="${cur_%%:*}:" ;;
1126 esac
1127 cur_="${cur_#*:}"
1128 lhs=0
1131 pfx="+"
1132 cur_="${cur_#+}"
1134 esac
1135 case "$cmd" in
1136 fetch)
1137 if [ $lhs = 1 ]; then
1138 __git_complete_fetch_refspecs "$remote" "$pfx" "$cur_"
1139 else
1140 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1143 pull|remote)
1144 if [ $lhs = 1 ]; then
1145 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
1146 else
1147 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1150 push)
1151 if [ $lhs = 1 ]; then
1152 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1153 else
1154 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
1157 esac
1160 __git_complete_strategy ()
1162 __git_compute_merge_strategies
1163 case "$prev" in
1164 -s|--strategy)
1165 __gitcomp "$__git_merge_strategies"
1166 return 0
1169 __gitcomp "$__git_merge_strategy_options"
1170 return 0
1172 esac
1173 case "$cur" in
1174 --strategy=*)
1175 __gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
1176 return 0
1178 --strategy-option=*)
1179 __gitcomp "$__git_merge_strategy_options" "" "${cur##--strategy-option=}"
1180 return 0
1182 esac
1183 return 1
1186 __git_all_commands=
1187 __git_compute_all_commands ()
1189 test -n "$__git_all_commands" ||
1190 __git_all_commands=$(__git --list-cmds=main,others,alias,nohelpers)
1193 # Lists all set config variables starting with the given section prefix,
1194 # with the prefix removed.
1195 __git_get_config_variables ()
1197 local section="$1" i IFS=$'\n'
1198 for i in $(__git config --name-only --get-regexp "^$section\..*"); do
1199 echo "${i#$section.}"
1200 done
1203 __git_pretty_aliases ()
1205 __git_get_config_variables "pretty"
1208 # __git_aliased_command requires 1 argument
1209 __git_aliased_command ()
1211 local cur=$1 last list= word cmdline
1213 while [[ -n "$cur" ]]; do
1214 if [[ "$list" == *" $cur "* ]]; then
1215 # loop detected
1216 return
1219 cmdline=$(__git config --get "alias.$cur")
1220 list=" $cur $list"
1221 last=$cur
1222 cur=
1224 for word in $cmdline; do
1225 case "$word" in
1226 \!gitk|gitk)
1227 cur="gitk"
1228 break
1230 \!*) : shell command alias ;;
1231 -*) : option ;;
1232 *=*) : setting env ;;
1233 git) : git itself ;;
1234 \(\)) : skip parens of shell function definition ;;
1235 {) : skip start of shell helper function ;;
1236 :) : skip null command ;;
1237 \'*) : skip opening quote after sh -c ;;
1239 cur="${word%;}"
1240 break
1241 esac
1242 done
1243 done
1245 cur=$last
1246 if [[ "$cur" != "$1" ]]; then
1247 echo "$cur"
1251 # Check whether one of the given words is present on the command line,
1252 # and print the first word found.
1254 # Usage: __git_find_on_cmdline [<option>]... "<wordlist>"
1255 # --show-idx: Optionally show the index of the found word in the $words array.
1256 __git_find_on_cmdline ()
1258 local word c="$__git_cmd_idx" show_idx
1260 while test $# -gt 1; do
1261 case "$1" in
1262 --show-idx) show_idx=y ;;
1263 *) return 1 ;;
1264 esac
1265 shift
1266 done
1267 local wordlist="$1"
1269 while [ $c -lt $cword ]; do
1270 for word in $wordlist; do
1271 if [ "$word" = "${words[c]}" ]; then
1272 if [ -n "${show_idx-}" ]; then
1273 echo "$c $word"
1274 else
1275 echo "$word"
1277 return
1279 done
1280 ((c++))
1281 done
1284 # Similar to __git_find_on_cmdline, except that it loops backwards and thus
1285 # prints the *last* word found. Useful for finding which of two options that
1286 # supersede each other came last, such as "--guess" and "--no-guess".
1288 # Usage: __git_find_last_on_cmdline [<option>]... "<wordlist>"
1289 # --show-idx: Optionally show the index of the found word in the $words array.
1290 __git_find_last_on_cmdline ()
1292 local word c=$cword show_idx
1294 while test $# -gt 1; do
1295 case "$1" in
1296 --show-idx) show_idx=y ;;
1297 *) return 1 ;;
1298 esac
1299 shift
1300 done
1301 local wordlist="$1"
1303 while [ $c -gt "$__git_cmd_idx" ]; do
1304 ((c--))
1305 for word in $wordlist; do
1306 if [ "$word" = "${words[c]}" ]; then
1307 if [ -n "$show_idx" ]; then
1308 echo "$c $word"
1309 else
1310 echo "$word"
1312 return
1314 done
1315 done
1318 # Echo the value of an option set on the command line or config
1320 # $1: short option name
1321 # $2: long option name including =
1322 # $3: list of possible values
1323 # $4: config string (optional)
1325 # example:
1326 # result="$(__git_get_option_value "-d" "--do-something=" \
1327 # "yes no" "core.doSomething")"
1329 # result is then either empty (no option set) or "yes" or "no"
1331 # __git_get_option_value requires 3 arguments
1332 __git_get_option_value ()
1334 local c short_opt long_opt val
1335 local result= values config_key word
1337 short_opt="$1"
1338 long_opt="$2"
1339 values="$3"
1340 config_key="$4"
1342 ((c = $cword - 1))
1343 while [ $c -ge 0 ]; do
1344 word="${words[c]}"
1345 for val in $values; do
1346 if [ "$short_opt$val" = "$word" ] ||
1347 [ "$long_opt$val" = "$word" ]; then
1348 result="$val"
1349 break 2
1351 done
1352 ((c--))
1353 done
1355 if [ -n "$config_key" ] && [ -z "$result" ]; then
1356 result="$(__git config "$config_key")"
1359 echo "$result"
1362 __git_has_doubledash ()
1364 local c=1
1365 while [ $c -lt $cword ]; do
1366 if [ "--" = "${words[c]}" ]; then
1367 return 0
1369 ((c++))
1370 done
1371 return 1
1374 # Try to count non option arguments passed on the command line for the
1375 # specified git command.
1376 # When options are used, it is necessary to use the special -- option to
1377 # tell the implementation were non option arguments begin.
1378 # XXX this can not be improved, since options can appear everywhere, as
1379 # an example:
1380 # git mv x -n y
1382 # __git_count_arguments requires 1 argument: the git command executed.
1383 __git_count_arguments ()
1385 local word i c=0
1387 # Skip "git" (first argument)
1388 for ((i=$__git_cmd_idx; i < ${#words[@]}; i++)); do
1389 word="${words[i]}"
1391 case "$word" in
1393 # Good; we can assume that the following are only non
1394 # option arguments.
1395 ((c = 0))
1397 "$1")
1398 # Skip the specified git command and discard git
1399 # main options
1400 ((c = 0))
1403 ((c++))
1405 esac
1406 done
1408 printf "%d" $c
1411 __git_whitespacelist="nowarn warn error error-all fix"
1412 __git_patchformat="mbox stgit stgit-series hg mboxrd"
1413 __git_showcurrentpatch="diff raw"
1414 __git_am_inprogress_options="--skip --continue --resolved --abort --quit --show-current-patch"
1415 __git_quoted_cr="nowarn warn strip"
1417 _git_am ()
1419 __git_find_repo_path
1420 if [ -d "$__git_repo_path"/rebase-apply ]; then
1421 __gitcomp "$__git_am_inprogress_options"
1422 return
1424 case "$cur" in
1425 --whitespace=*)
1426 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1427 return
1429 --patch-format=*)
1430 __gitcomp "$__git_patchformat" "" "${cur##--patch-format=}"
1431 return
1433 --show-current-patch=*)
1434 __gitcomp "$__git_showcurrentpatch" "" "${cur##--show-current-patch=}"
1435 return
1437 --quoted-cr=*)
1438 __gitcomp "$__git_quoted_cr" "" "${cur##--quoted-cr=}"
1439 return
1441 --*)
1442 __gitcomp_builtin am "" \
1443 "$__git_am_inprogress_options"
1444 return
1445 esac
1448 _git_apply ()
1450 case "$cur" in
1451 --whitespace=*)
1452 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1453 return
1455 --*)
1456 __gitcomp_builtin apply
1457 return
1458 esac
1461 _git_add ()
1463 case "$cur" in
1464 --chmod=*)
1465 __gitcomp "+x -x" "" "${cur##--chmod=}"
1466 return
1468 --*)
1469 __gitcomp_builtin add
1470 return
1471 esac
1473 local complete_opt="--others --modified --directory --no-empty-directory"
1474 if test -n "$(__git_find_on_cmdline "-u --update")"
1475 then
1476 complete_opt="--modified"
1478 __git_complete_index_file "$complete_opt"
1481 _git_archive ()
1483 case "$cur" in
1484 --format=*)
1485 __gitcomp "$(git archive --list)" "" "${cur##--format=}"
1486 return
1488 --remote=*)
1489 __gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
1490 return
1492 --*)
1493 __gitcomp_builtin archive "--format= --list --verbose --prefix= --worktree-attributes"
1494 return
1496 esac
1497 __git_complete_file
1500 _git_bisect ()
1502 __git_has_doubledash && return
1504 local subcommands="start bad good skip reset visualize replay log run"
1505 local subcommand="$(__git_find_on_cmdline "$subcommands")"
1506 if [ -z "$subcommand" ]; then
1507 __git_find_repo_path
1508 if [ -f "$__git_repo_path"/BISECT_START ]; then
1509 __gitcomp "$subcommands"
1510 else
1511 __gitcomp "replay start"
1513 return
1516 case "$subcommand" in
1517 bad|good|reset|skip|start)
1518 __git_complete_refs
1522 esac
1525 __git_ref_fieldlist="refname objecttype objectsize objectname upstream push HEAD symref"
1527 _git_branch ()
1529 local i c="$__git_cmd_idx" only_local_ref="n" has_r="n"
1531 while [ $c -lt $cword ]; do
1532 i="${words[c]}"
1533 case "$i" in
1534 -d|-D|--delete|-m|-M|--move|-c|-C|--copy)
1535 only_local_ref="y" ;;
1536 -r|--remotes)
1537 has_r="y" ;;
1538 esac
1539 ((c++))
1540 done
1542 case "$cur" in
1543 --set-upstream-to=*)
1544 __git_complete_refs --cur="${cur##--set-upstream-to=}"
1546 --*)
1547 __gitcomp_builtin branch
1550 if [ $only_local_ref = "y" -a $has_r = "n" ]; then
1551 __gitcomp_direct "$(__git_heads "" "$cur" " ")"
1552 else
1553 __git_complete_refs
1556 esac
1559 _git_bundle ()
1561 local cmd="${words[__git_cmd_idx+1]}"
1562 case "$cword" in
1563 $((__git_cmd_idx+1)))
1564 __gitcomp "create list-heads verify unbundle"
1566 $((__git_cmd_idx+2)))
1567 # looking for a file
1570 case "$cmd" in
1571 create)
1572 __git_complete_revlist
1574 esac
1576 esac
1579 # Helper function to decide whether or not we should enable DWIM logic for
1580 # git-switch and git-checkout.
1582 # To decide between the following rules in decreasing priority order:
1583 # - the last provided of "--guess" or "--no-guess" explicitly enable or
1584 # disable completion of DWIM logic respectively.
1585 # - If checkout.guess is false, disable completion of DWIM logic.
1586 # - If the --no-track option is provided, take this as a hint to disable the
1587 # DWIM completion logic
1588 # - If GIT_COMPLETION_CHECKOUT_NO_GUESS is set, disable the DWIM completion
1589 # logic, as requested by the user.
1590 # - Enable DWIM logic otherwise.
1592 __git_checkout_default_dwim_mode ()
1594 local last_option dwim_opt="--dwim"
1596 if [ "${GIT_COMPLETION_CHECKOUT_NO_GUESS-}" = "1" ]; then
1597 dwim_opt=""
1600 # --no-track disables DWIM, but with lower priority than
1601 # --guess/--no-guess/checkout.guess
1602 if [ -n "$(__git_find_on_cmdline "--no-track")" ]; then
1603 dwim_opt=""
1606 # checkout.guess = false disables DWIM, but with lower priority than
1607 # --guess/--no-guess
1608 if [ "$(__git config --type=bool checkout.guess)" = "false" ]; then
1609 dwim_opt=""
1612 # Find the last provided --guess or --no-guess
1613 last_option="$(__git_find_last_on_cmdline "--guess --no-guess")"
1614 case "$last_option" in
1615 --guess)
1616 dwim_opt="--dwim"
1618 --no-guess)
1619 dwim_opt=""
1621 esac
1623 echo "$dwim_opt"
1626 _git_checkout ()
1628 __git_has_doubledash && return
1630 local dwim_opt="$(__git_checkout_default_dwim_mode)"
1632 case "$prev" in
1633 -b|-B|--orphan)
1634 # Complete local branches (and DWIM branch
1635 # remote branch names) for an option argument
1636 # specifying a new branch name. This is for
1637 # convenience, assuming new branches are
1638 # possibly based on pre-existing branch names.
1639 __git_complete_refs $dwim_opt --mode="heads"
1640 return
1644 esac
1646 case "$cur" in
1647 --conflict=*)
1648 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
1650 --*)
1651 __gitcomp_builtin checkout
1654 # At this point, we've already handled special completion for
1655 # the arguments to -b/-B, and --orphan. There are 3 main
1656 # things left we can possibly complete:
1657 # 1) a start-point for -b/-B, -d/--detach, or --orphan
1658 # 2) a remote head, for --track
1659 # 3) an arbitrary reference, possibly including DWIM names
1662 if [ -n "$(__git_find_on_cmdline "-b -B -d --detach --orphan")" ]; then
1663 __git_complete_refs --mode="refs"
1664 elif [ -n "$(__git_find_on_cmdline "-t --track")" ]; then
1665 __git_complete_refs --mode="remote-heads"
1666 else
1667 __git_complete_refs $dwim_opt --mode="refs"
1670 esac
1673 __git_sequencer_inprogress_options="--continue --quit --abort --skip"
1675 __git_cherry_pick_inprogress_options=$__git_sequencer_inprogress_options
1677 _git_cherry_pick ()
1679 __git_find_repo_path
1680 if __git_pseudoref_exists CHERRY_PICK_HEAD; then
1681 __gitcomp "$__git_cherry_pick_inprogress_options"
1682 return
1685 __git_complete_strategy && return
1687 case "$cur" in
1688 --*)
1689 __gitcomp_builtin cherry-pick "" \
1690 "$__git_cherry_pick_inprogress_options"
1693 __git_complete_refs
1695 esac
1698 _git_clean ()
1700 case "$cur" in
1701 --*)
1702 __gitcomp_builtin clean
1703 return
1705 esac
1707 # XXX should we check for -x option ?
1708 __git_complete_index_file "--others --directory"
1711 _git_clone ()
1713 case "$prev" in
1714 -c|--config)
1715 __git_complete_config_variable_name_and_value
1716 return
1718 esac
1719 case "$cur" in
1720 --config=*)
1721 __git_complete_config_variable_name_and_value \
1722 --cur="${cur##--config=}"
1723 return
1725 --*)
1726 __gitcomp_builtin clone
1727 return
1729 esac
1732 __git_untracked_file_modes="all no normal"
1734 __git_trailer_tokens ()
1736 __git config --name-only --get-regexp '^trailer\..*\.key$' | cut -d. -f 2- | rev | cut -d. -f2- | rev
1739 _git_commit ()
1741 case "$prev" in
1742 -c|-C)
1743 __git_complete_refs
1744 return
1746 esac
1748 case "$cur" in
1749 --cleanup=*)
1750 __gitcomp "default scissors strip verbatim whitespace
1751 " "" "${cur##--cleanup=}"
1752 return
1754 --reuse-message=*|--reedit-message=*|\
1755 --fixup=*|--squash=*)
1756 __git_complete_refs --cur="${cur#*=}"
1757 return
1759 --untracked-files=*)
1760 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
1761 return
1763 --trailer=*)
1764 __gitcomp_nl "$(__git_trailer_tokens)" "" "${cur##--trailer=}" ":"
1765 return
1767 --*)
1768 __gitcomp_builtin commit
1769 return
1770 esac
1772 if __git rev-parse --verify --quiet HEAD >/dev/null; then
1773 __git_complete_index_file "--committable"
1774 else
1775 # This is the first commit
1776 __git_complete_index_file "--cached"
1780 _git_describe ()
1782 case "$cur" in
1783 --*)
1784 __gitcomp_builtin describe
1785 return
1786 esac
1787 __git_complete_refs
1790 __git_diff_algorithms="myers minimal patience histogram"
1792 __git_diff_submodule_formats="diff log short"
1794 __git_color_moved_opts="no default plain blocks zebra dimmed-zebra"
1796 __git_color_moved_ws_opts="no ignore-space-at-eol ignore-space-change
1797 ignore-all-space allow-indentation-change"
1799 __git_ws_error_highlight_opts="context old new all default"
1801 # Options for the diff machinery (diff, log, show, stash, range-diff, ...)
1802 __git_diff_common_options="--stat --numstat --shortstat --summary
1803 --patch-with-stat --name-only --name-status --color
1804 --no-color --color-words --no-renames --check
1805 --color-moved --color-moved= --no-color-moved
1806 --color-moved-ws= --no-color-moved-ws
1807 --full-index --binary --abbrev --diff-filter=
1808 --find-copies --find-object --find-renames
1809 --no-relative --relative
1810 --find-copies-harder --ignore-cr-at-eol
1811 --text --ignore-space-at-eol --ignore-space-change
1812 --ignore-all-space --ignore-blank-lines --exit-code
1813 --quiet --ext-diff --no-ext-diff --unified=
1814 --no-prefix --src-prefix= --dst-prefix=
1815 --inter-hunk-context= --function-context
1816 --patience --histogram --minimal
1817 --raw --word-diff --word-diff-regex=
1818 --dirstat --dirstat= --dirstat-by-file
1819 --dirstat-by-file= --cumulative
1820 --diff-algorithm= --default-prefix
1821 --submodule --submodule= --ignore-submodules
1822 --indent-heuristic --no-indent-heuristic
1823 --textconv --no-textconv --break-rewrites
1824 --patch --no-patch --cc --combined-all-paths
1825 --anchored= --compact-summary --ignore-matching-lines=
1826 --irreversible-delete --line-prefix --no-stat
1827 --output= --output-indicator-context=
1828 --output-indicator-new= --output-indicator-old=
1829 --ws-error-highlight=
1830 --pickaxe-all --pickaxe-regex
1833 # Options for diff/difftool
1834 __git_diff_difftool_options="--cached --staged
1835 --base --ours --theirs --no-index --merge-base
1836 --ita-invisible-in-index --ita-visible-in-index
1837 $__git_diff_common_options"
1839 _git_diff ()
1841 __git_has_doubledash && return
1843 case "$cur" in
1844 --diff-algorithm=*)
1845 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1846 return
1848 --submodule=*)
1849 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1850 return
1852 --color-moved=*)
1853 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
1854 return
1856 --color-moved-ws=*)
1857 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
1858 return
1860 --ws-error-highlight=*)
1861 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
1862 return
1864 --*)
1865 __gitcomp "$__git_diff_difftool_options"
1866 return
1868 esac
1869 __git_complete_revlist_file
1872 __git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff
1873 tkdiff vimdiff nvimdiff gvimdiff xxdiff araxis p4merge
1874 bc codecompare smerge
1877 _git_difftool ()
1879 __git_has_doubledash && return
1881 case "$cur" in
1882 --tool=*)
1883 __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1884 return
1886 --*)
1887 __gitcomp_builtin difftool "$__git_diff_difftool_options"
1888 return
1890 esac
1891 __git_complete_revlist_file
1894 __git_fetch_recurse_submodules="yes on-demand no"
1896 _git_fetch ()
1898 case "$cur" in
1899 --recurse-submodules=*)
1900 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1901 return
1903 --filter=*)
1904 __gitcomp "blob:none blob:limit= sparse:oid=" "" "${cur##--filter=}"
1905 return
1907 --*)
1908 __gitcomp_builtin fetch
1909 return
1911 esac
1912 __git_complete_remote_or_refspec
1915 __git_format_patch_extra_options="
1916 --full-index --not --all --no-prefix --src-prefix=
1917 --dst-prefix= --notes
1920 _git_format_patch ()
1922 case "$cur" in
1923 --thread=*)
1924 __gitcomp "
1925 deep shallow
1926 " "" "${cur##--thread=}"
1927 return
1929 --base=*|--interdiff=*|--range-diff=*)
1930 __git_complete_refs --cur="${cur#--*=}"
1931 return
1933 --*)
1934 __gitcomp_builtin format-patch "$__git_format_patch_extra_options"
1935 return
1937 esac
1938 __git_complete_revlist
1941 _git_fsck ()
1943 case "$cur" in
1944 --*)
1945 __gitcomp_builtin fsck
1946 return
1948 esac
1951 _git_gitk ()
1953 __gitk_main
1956 # Lists matching symbol names from a tag (as in ctags) file.
1957 # 1: List symbol names matching this word.
1958 # 2: The tag file to list symbol names from.
1959 # 3: A prefix to be added to each listed symbol name (optional).
1960 # 4: A suffix to be appended to each listed symbol name (optional).
1961 __git_match_ctag () {
1962 awk -v pfx="${3-}" -v sfx="${4-}" "
1963 /^${1//\//\\/}/ { print pfx \$1 sfx }
1964 " "$2"
1967 # Complete symbol names from a tag file.
1968 # Usage: __git_complete_symbol [<option>]...
1969 # --tags=<file>: The tag file to list symbol names from instead of the
1970 # default "tags".
1971 # --pfx=<prefix>: A prefix to be added to each symbol name.
1972 # --cur=<word>: The current symbol name to be completed. Defaults to
1973 # the current word to be completed.
1974 # --sfx=<suffix>: A suffix to be appended to each symbol name instead
1975 # of the default space.
1976 __git_complete_symbol () {
1977 local tags=tags pfx="" cur_="${cur-}" sfx=" "
1979 while test $# != 0; do
1980 case "$1" in
1981 --tags=*) tags="${1##--tags=}" ;;
1982 --pfx=*) pfx="${1##--pfx=}" ;;
1983 --cur=*) cur_="${1##--cur=}" ;;
1984 --sfx=*) sfx="${1##--sfx=}" ;;
1985 *) return 1 ;;
1986 esac
1987 shift
1988 done
1990 if test -r "$tags"; then
1991 __gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
1995 _git_grep ()
1997 __git_has_doubledash && return
1999 case "$cur" in
2000 --*)
2001 __gitcomp_builtin grep
2002 return
2004 esac
2006 case "$cword,$prev" in
2007 $((__git_cmd_idx+1)),*|*,-*)
2008 __git_complete_symbol && return
2010 esac
2012 __git_complete_refs
2015 _git_help ()
2017 case "$cur" in
2018 --*)
2019 __gitcomp_builtin help
2020 return
2022 esac
2023 if test -n "${GIT_TESTING_ALL_COMMAND_LIST-}"
2024 then
2025 __gitcomp "$GIT_TESTING_ALL_COMMAND_LIST $(__git --list-cmds=alias,list-guide) gitk"
2026 else
2027 __gitcomp "$(__git --list-cmds=main,nohelpers,alias,list-guide) gitk"
2031 _git_init ()
2033 case "$cur" in
2034 --shared=*)
2035 __gitcomp "
2036 false true umask group all world everybody
2037 " "" "${cur##--shared=}"
2038 return
2040 --*)
2041 __gitcomp_builtin init
2042 return
2044 esac
2047 _git_ls_files ()
2049 case "$cur" in
2050 --*)
2051 __gitcomp_builtin ls-files
2052 return
2054 esac
2056 # XXX ignore options like --modified and always suggest all cached
2057 # files.
2058 __git_complete_index_file "--cached"
2061 _git_ls_remote ()
2063 case "$cur" in
2064 --*)
2065 __gitcomp_builtin ls-remote
2066 return
2068 esac
2069 __gitcomp_nl "$(__git_remotes)"
2072 _git_ls_tree ()
2074 case "$cur" in
2075 --*)
2076 __gitcomp_builtin ls-tree
2077 return
2079 esac
2081 __git_complete_file
2084 # Options that go well for log, shortlog and gitk
2085 __git_log_common_options="
2086 --not --all
2087 --branches --tags --remotes
2088 --first-parent --merges --no-merges
2089 --max-count=
2090 --max-age= --since= --after=
2091 --min-age= --until= --before=
2092 --min-parents= --max-parents=
2093 --no-min-parents --no-max-parents
2095 # Options that go well for log and gitk (not shortlog)
2096 __git_log_gitk_options="
2097 --dense --sparse --full-history
2098 --simplify-merges --simplify-by-decoration
2099 --left-right --notes --no-notes
2101 # Options that go well for log and shortlog (not gitk)
2102 __git_log_shortlog_options="
2103 --author= --committer= --grep=
2104 --all-match --invert-grep
2106 # Options accepted by log and show
2107 __git_log_show_options="
2108 --diff-merges --diff-merges= --no-diff-merges --dd --remerge-diff
2111 __git_diff_merges_opts="off none on first-parent 1 separate m combined c dense-combined cc remerge r"
2113 __git_log_pretty_formats="oneline short medium full fuller reference email raw format: tformat: mboxrd"
2114 __git_log_date_formats="relative iso8601 iso8601-strict rfc2822 short local default human raw unix auto: format:"
2116 _git_log ()
2118 __git_has_doubledash && return
2119 __git_find_repo_path
2121 local merge=""
2122 if __git_pseudoref_exists MERGE_HEAD; then
2123 merge="--merge"
2125 case "$prev,$cur" in
2126 -L,:*:*)
2127 return # fall back to Bash filename completion
2129 -L,:*)
2130 __git_complete_symbol --cur="${cur#:}" --sfx=":"
2131 return
2133 -G,*|-S,*)
2134 __git_complete_symbol
2135 return
2137 esac
2138 case "$cur" in
2139 --pretty=*|--format=*)
2140 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2141 " "" "${cur#*=}"
2142 return
2144 --date=*)
2145 __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
2146 return
2148 --decorate=*)
2149 __gitcomp "full short no" "" "${cur##--decorate=}"
2150 return
2152 --diff-algorithm=*)
2153 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
2154 return
2156 --submodule=*)
2157 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
2158 return
2160 --ws-error-highlight=*)
2161 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
2162 return
2164 --no-walk=*)
2165 __gitcomp "sorted unsorted" "" "${cur##--no-walk=}"
2166 return
2168 --diff-merges=*)
2169 __gitcomp "$__git_diff_merges_opts" "" "${cur##--diff-merges=}"
2170 return
2172 --*)
2173 __gitcomp "
2174 $__git_log_common_options
2175 $__git_log_shortlog_options
2176 $__git_log_gitk_options
2177 $__git_log_show_options
2178 --root --topo-order --date-order --reverse
2179 --follow --full-diff
2180 --abbrev-commit --no-abbrev-commit --abbrev=
2181 --relative-date --date=
2182 --pretty= --format= --oneline
2183 --show-signature
2184 --cherry-mark
2185 --cherry-pick
2186 --graph
2187 --decorate --decorate= --no-decorate
2188 --walk-reflogs
2189 --no-walk --no-walk= --do-walk
2190 --parents --children
2191 --expand-tabs --expand-tabs= --no-expand-tabs
2192 $merge
2193 $__git_diff_common_options
2195 return
2197 -L:*:*)
2198 return # fall back to Bash filename completion
2200 -L:*)
2201 __git_complete_symbol --cur="${cur#-L:}" --sfx=":"
2202 return
2204 -G*)
2205 __git_complete_symbol --pfx="-G" --cur="${cur#-G}"
2206 return
2208 -S*)
2209 __git_complete_symbol --pfx="-S" --cur="${cur#-S}"
2210 return
2212 esac
2213 __git_complete_revlist
2216 _git_merge ()
2218 __git_complete_strategy && return
2220 case "$cur" in
2221 --*)
2222 __gitcomp_builtin merge
2223 return
2224 esac
2225 __git_complete_refs
2228 _git_mergetool ()
2230 case "$cur" in
2231 --tool=*)
2232 __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
2233 return
2235 --*)
2236 __gitcomp "--tool= --prompt --no-prompt --gui --no-gui"
2237 return
2239 esac
2242 _git_merge_base ()
2244 case "$cur" in
2245 --*)
2246 __gitcomp_builtin merge-base
2247 return
2249 esac
2250 __git_complete_refs
2253 _git_mv ()
2255 case "$cur" in
2256 --*)
2257 __gitcomp_builtin mv
2258 return
2260 esac
2262 if [ $(__git_count_arguments "mv") -gt 0 ]; then
2263 # We need to show both cached and untracked files (including
2264 # empty directories) since this may not be the last argument.
2265 __git_complete_index_file "--cached --others --directory"
2266 else
2267 __git_complete_index_file "--cached"
2271 _git_notes ()
2273 local subcommands='add append copy edit get-ref list merge prune remove show'
2274 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2276 case "$subcommand,$cur" in
2277 ,--*)
2278 __gitcomp_builtin notes
2281 case "$prev" in
2282 --ref)
2283 __git_complete_refs
2286 __gitcomp "$subcommands --ref"
2288 esac
2290 *,--reuse-message=*|*,--reedit-message=*)
2291 __git_complete_refs --cur="${cur#*=}"
2293 *,--*)
2294 __gitcomp_builtin notes_$subcommand
2296 prune,*|get-ref,*)
2297 # this command does not take a ref, do not complete it
2300 case "$prev" in
2301 -m|-F)
2304 __git_complete_refs
2306 esac
2308 esac
2311 _git_pull ()
2313 __git_complete_strategy && return
2315 case "$cur" in
2316 --recurse-submodules=*)
2317 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
2318 return
2320 --*)
2321 __gitcomp_builtin pull
2323 return
2325 esac
2326 __git_complete_remote_or_refspec
2329 __git_push_recurse_submodules="check on-demand only"
2331 __git_complete_force_with_lease ()
2333 local cur_=$1
2335 case "$cur_" in
2336 --*=)
2338 *:*)
2339 __git_complete_refs --cur="${cur_#*:}"
2342 __git_complete_refs --cur="$cur_"
2344 esac
2347 _git_push ()
2349 case "$prev" in
2350 --repo)
2351 __gitcomp_nl "$(__git_remotes)"
2352 return
2354 --recurse-submodules)
2355 __gitcomp "$__git_push_recurse_submodules"
2356 return
2358 esac
2359 case "$cur" in
2360 --repo=*)
2361 __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
2362 return
2364 --recurse-submodules=*)
2365 __gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
2366 return
2368 --force-with-lease=*)
2369 __git_complete_force_with_lease "${cur##--force-with-lease=}"
2370 return
2372 --*)
2373 __gitcomp_builtin push
2374 return
2376 esac
2377 __git_complete_remote_or_refspec
2380 _git_range_diff ()
2382 case "$cur" in
2383 --*)
2384 __gitcomp "
2385 --creation-factor= --no-dual-color
2386 $__git_diff_common_options
2388 return
2390 esac
2391 __git_complete_revlist
2394 __git_rebase_inprogress_options="--continue --skip --abort --quit --show-current-patch"
2395 __git_rebase_interactive_inprogress_options="$__git_rebase_inprogress_options --edit-todo"
2397 _git_rebase ()
2399 __git_find_repo_path
2400 if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
2401 __gitcomp "$__git_rebase_interactive_inprogress_options"
2402 return
2403 elif [ -d "$__git_repo_path"/rebase-apply ] || \
2404 [ -d "$__git_repo_path"/rebase-merge ]; then
2405 __gitcomp "$__git_rebase_inprogress_options"
2406 return
2408 __git_complete_strategy && return
2409 case "$cur" in
2410 --whitespace=*)
2411 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
2412 return
2414 --onto=*)
2415 __git_complete_refs --cur="${cur##--onto=}"
2416 return
2418 --*)
2419 __gitcomp_builtin rebase "" \
2420 "$__git_rebase_interactive_inprogress_options"
2422 return
2423 esac
2424 __git_complete_refs
2427 _git_reflog ()
2429 local subcommands="show delete expire"
2430 local subcommand="$(__git_find_subcommand "$subcommands" "show")"
2432 case "$subcommand,$cur" in
2433 show,--*)
2434 __gitcomp "
2435 $__git_log_common_options
2437 return
2439 esac
2441 __git_complete_refs
2443 if [ $((cword - __git_cmd_idx)) -eq 1 ]; then
2444 __gitcompappend "$subcommands" "" "$cur" " "
2448 __git_send_email_confirm_options="always never auto cc compose"
2449 __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
2451 _git_send_email ()
2453 case "$prev" in
2454 --to|--cc|--bcc|--from)
2455 __gitcomp "$(__git send-email --dump-aliases)"
2456 return
2458 esac
2460 case "$cur" in
2461 --confirm=*)
2462 __gitcomp "
2463 $__git_send_email_confirm_options
2464 " "" "${cur##--confirm=}"
2465 return
2467 --suppress-cc=*)
2468 __gitcomp "
2469 $__git_send_email_suppresscc_options
2470 " "" "${cur##--suppress-cc=}"
2472 return
2474 --smtp-encryption=*)
2475 __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
2476 return
2478 --thread=*)
2479 __gitcomp "
2480 deep shallow
2481 " "" "${cur##--thread=}"
2482 return
2484 --to=*|--cc=*|--bcc=*|--from=*)
2485 __gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
2486 return
2488 --*)
2489 __gitcomp_builtin send-email "$__git_format_patch_extra_options"
2490 return
2492 esac
2493 __git_complete_revlist
2496 _git_stage ()
2498 _git_add
2501 _git_status ()
2503 local complete_opt
2504 local untracked_state
2506 case "$cur" in
2507 --ignore-submodules=*)
2508 __gitcomp "none untracked dirty all" "" "${cur##--ignore-submodules=}"
2509 return
2511 --untracked-files=*)
2512 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
2513 return
2515 --column=*)
2516 __gitcomp "
2517 always never auto column row plain dense nodense
2518 " "" "${cur##--column=}"
2519 return
2521 --*)
2522 __gitcomp_builtin status
2523 return
2525 esac
2527 untracked_state="$(__git_get_option_value "-u" "--untracked-files=" \
2528 "$__git_untracked_file_modes" "status.showUntrackedFiles")"
2530 case "$untracked_state" in
2532 # --ignored option does not matter
2533 complete_opt=
2535 all|normal|*)
2536 complete_opt="--cached --directory --no-empty-directory --others"
2538 if [ -n "$(__git_find_on_cmdline "--ignored")" ]; then
2539 complete_opt="$complete_opt --ignored --exclude=*"
2542 esac
2544 __git_complete_index_file "$complete_opt"
2547 _git_switch ()
2549 local dwim_opt="$(__git_checkout_default_dwim_mode)"
2551 case "$prev" in
2552 -c|-C|--orphan)
2553 # Complete local branches (and DWIM branch
2554 # remote branch names) for an option argument
2555 # specifying a new branch name. This is for
2556 # convenience, assuming new branches are
2557 # possibly based on pre-existing branch names.
2558 __git_complete_refs $dwim_opt --mode="heads"
2559 return
2563 esac
2565 case "$cur" in
2566 --conflict=*)
2567 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
2569 --*)
2570 __gitcomp_builtin switch
2573 # Unlike in git checkout, git switch --orphan does not take
2574 # a start point. Thus we really have nothing to complete after
2575 # the branch name.
2576 if [ -n "$(__git_find_on_cmdline "--orphan")" ]; then
2577 return
2580 # At this point, we've already handled special completion for
2581 # -c/-C, and --orphan. There are 3 main things left to
2582 # complete:
2583 # 1) a start-point for -c/-C or -d/--detach
2584 # 2) a remote head, for --track
2585 # 3) a branch name, possibly including DWIM remote branches
2587 if [ -n "$(__git_find_on_cmdline "-c -C -d --detach")" ]; then
2588 __git_complete_refs --mode="refs"
2589 elif [ -n "$(__git_find_on_cmdline "-t --track")" ]; then
2590 __git_complete_refs --mode="remote-heads"
2591 else
2592 __git_complete_refs $dwim_opt --mode="heads"
2595 esac
2598 __git_config_get_set_variables ()
2600 local prevword word config_file= c=$cword
2601 while [ $c -gt "$__git_cmd_idx" ]; do
2602 word="${words[c]}"
2603 case "$word" in
2604 --system|--global|--local|--file=*)
2605 config_file="$word"
2606 break
2608 -f|--file)
2609 config_file="$word $prevword"
2610 break
2612 esac
2613 prevword=$word
2614 c=$((--c))
2615 done
2617 __git config $config_file --name-only --list
2620 __git_config_vars=
2621 __git_compute_config_vars ()
2623 test -n "$__git_config_vars" ||
2624 __git_config_vars="$(git help --config-for-completion)"
2627 __git_config_sections=
2628 __git_compute_config_sections ()
2630 test -n "$__git_config_sections" ||
2631 __git_config_sections="$(git help --config-sections-for-completion)"
2634 # Completes possible values of various configuration variables.
2636 # Usage: __git_complete_config_variable_value [<option>]...
2637 # --varname=<word>: The name of the configuration variable whose value is
2638 # to be completed. Defaults to the previous word on the
2639 # command line.
2640 # --cur=<word>: The current value to be completed. Defaults to the current
2641 # word to be completed.
2642 __git_complete_config_variable_value ()
2644 local varname="$prev" cur_="$cur"
2646 while test $# != 0; do
2647 case "$1" in
2648 --varname=*) varname="${1##--varname=}" ;;
2649 --cur=*) cur_="${1##--cur=}" ;;
2650 *) return 1 ;;
2651 esac
2652 shift
2653 done
2655 if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
2656 varname="${varname,,}"
2657 else
2658 varname="$(echo "$varname" |tr A-Z a-z)"
2661 case "$varname" in
2662 branch.*.remote|branch.*.pushremote)
2663 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2664 return
2666 branch.*.merge)
2667 __git_complete_refs --cur="$cur_"
2668 return
2670 branch.*.rebase)
2671 __gitcomp "false true merges interactive" "" "$cur_"
2672 return
2674 remote.pushdefault)
2675 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2676 return
2678 remote.*.fetch)
2679 local remote="${varname#remote.}"
2680 remote="${remote%.fetch}"
2681 if [ -z "$cur_" ]; then
2682 __gitcomp_nl "refs/heads/" "" "" ""
2683 return
2685 __gitcomp_nl "$(__git_refs_remotes "$remote")" "" "$cur_"
2686 return
2688 remote.*.push)
2689 local remote="${varname#remote.}"
2690 remote="${remote%.push}"
2691 __gitcomp_nl "$(__git for-each-ref \
2692 --format='%(refname):%(refname)' refs/heads)" "" "$cur_"
2693 return
2695 pull.twohead|pull.octopus)
2696 __git_compute_merge_strategies
2697 __gitcomp "$__git_merge_strategies" "" "$cur_"
2698 return
2700 color.pager)
2701 __gitcomp "false true" "" "$cur_"
2702 return
2704 color.*.*)
2705 __gitcomp "
2706 normal black red green yellow blue magenta cyan white
2707 bold dim ul blink reverse
2708 " "" "$cur_"
2709 return
2711 color.*)
2712 __gitcomp "false true always never auto" "" "$cur_"
2713 return
2715 diff.submodule)
2716 __gitcomp "$__git_diff_submodule_formats" "" "$cur_"
2717 return
2719 help.format)
2720 __gitcomp "man info web html" "" "$cur_"
2721 return
2723 log.date)
2724 __gitcomp "$__git_log_date_formats" "" "$cur_"
2725 return
2727 sendemail.aliasfiletype)
2728 __gitcomp "mutt mailrc pine elm gnus" "" "$cur_"
2729 return
2731 sendemail.confirm)
2732 __gitcomp "$__git_send_email_confirm_options" "" "$cur_"
2733 return
2735 sendemail.suppresscc)
2736 __gitcomp "$__git_send_email_suppresscc_options" "" "$cur_"
2737 return
2739 sendemail.transferencoding)
2740 __gitcomp "7bit 8bit quoted-printable base64" "" "$cur_"
2741 return
2743 *.*)
2744 return
2746 esac
2749 # Completes configuration sections, subsections, variable names.
2751 # Usage: __git_complete_config_variable_name [<option>]...
2752 # --cur=<word>: The current configuration section/variable name to be
2753 # completed. Defaults to the current word to be completed.
2754 # --sfx=<suffix>: A suffix to be appended to each fully completed
2755 # configuration variable name (but not to sections or
2756 # subsections) instead of the default space.
2757 __git_complete_config_variable_name ()
2759 local cur_="$cur" sfx
2761 while test $# != 0; do
2762 case "$1" in
2763 --cur=*) cur_="${1##--cur=}" ;;
2764 --sfx=*) sfx="${1##--sfx=}" ;;
2765 *) return 1 ;;
2766 esac
2767 shift
2768 done
2770 case "$cur_" in
2771 branch.*.*)
2772 local pfx="${cur_%.*}."
2773 cur_="${cur_##*.}"
2774 __gitcomp "remote pushRemote merge mergeOptions rebase" "$pfx" "$cur_" "$sfx"
2775 return
2777 branch.*)
2778 local pfx="${cur_%.*}."
2779 cur_="${cur_#*.}"
2780 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
2781 __gitcomp_nl_append $'autoSetupMerge\nautoSetupRebase\n' "$pfx" "$cur_" "${sfx- }"
2782 return
2784 guitool.*.*)
2785 local pfx="${cur_%.*}."
2786 cur_="${cur_##*.}"
2787 __gitcomp "
2788 argPrompt cmd confirm needsFile noConsole noRescan
2789 prompt revPrompt revUnmerged title
2790 " "$pfx" "$cur_" "$sfx"
2791 return
2793 difftool.*.*)
2794 local pfx="${cur_%.*}."
2795 cur_="${cur_##*.}"
2796 __gitcomp "cmd path" "$pfx" "$cur_" "$sfx"
2797 return
2799 man.*.*)
2800 local pfx="${cur_%.*}."
2801 cur_="${cur_##*.}"
2802 __gitcomp "cmd path" "$pfx" "$cur_" "$sfx"
2803 return
2805 mergetool.*.*)
2806 local pfx="${cur_%.*}."
2807 cur_="${cur_##*.}"
2808 __gitcomp "cmd path trustExitCode" "$pfx" "$cur_" "$sfx"
2809 return
2811 pager.*)
2812 local pfx="${cur_%.*}."
2813 cur_="${cur_#*.}"
2814 __git_compute_all_commands
2815 __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_" "${sfx- }"
2816 return
2818 remote.*.*)
2819 local pfx="${cur_%.*}."
2820 cur_="${cur_##*.}"
2821 __gitcomp "
2822 url proxy fetch push mirror skipDefaultUpdate
2823 receivepack uploadpack tagOpt pushurl
2824 " "$pfx" "$cur_" "$sfx"
2825 return
2827 remote.*)
2828 local pfx="${cur_%.*}."
2829 cur_="${cur_#*.}"
2830 __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
2831 __gitcomp_nl_append "pushDefault" "$pfx" "$cur_" "${sfx- }"
2832 return
2834 url.*.*)
2835 local pfx="${cur_%.*}."
2836 cur_="${cur_##*.}"
2837 __gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_" "$sfx"
2838 return
2840 *.*)
2841 __git_compute_config_vars
2842 __gitcomp "$__git_config_vars" "" "$cur_" "$sfx"
2845 __git_compute_config_sections
2846 __gitcomp "$__git_config_sections" "" "$cur_" "."
2848 esac
2851 # Completes '='-separated configuration sections/variable names and values
2852 # for 'git -c section.name=value'.
2854 # Usage: __git_complete_config_variable_name_and_value [<option>]...
2855 # --cur=<word>: The current configuration section/variable name/value to be
2856 # completed. Defaults to the current word to be completed.
2857 __git_complete_config_variable_name_and_value ()
2859 local cur_="$cur"
2861 while test $# != 0; do
2862 case "$1" in
2863 --cur=*) cur_="${1##--cur=}" ;;
2864 *) return 1 ;;
2865 esac
2866 shift
2867 done
2869 case "$cur_" in
2870 *=*)
2871 __git_complete_config_variable_value \
2872 --varname="${cur_%%=*}" --cur="${cur_#*=}"
2875 __git_complete_config_variable_name --cur="$cur_" --sfx='='
2877 esac
2880 _git_config ()
2882 case "$prev" in
2883 --get|--get-all|--unset|--unset-all)
2884 __gitcomp_nl "$(__git_config_get_set_variables)"
2885 return
2887 *.*)
2888 __git_complete_config_variable_value
2889 return
2891 esac
2892 case "$cur" in
2893 --*)
2894 __gitcomp_builtin config
2897 __git_complete_config_variable_name
2899 esac
2902 _git_remote ()
2904 local subcommands="
2905 add rename remove set-head set-branches
2906 get-url set-url show prune update
2908 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2909 if [ -z "$subcommand" ]; then
2910 case "$cur" in
2911 --*)
2912 __gitcomp_builtin remote
2915 __gitcomp "$subcommands"
2917 esac
2918 return
2921 case "$subcommand,$cur" in
2922 add,--*)
2923 __gitcomp_builtin remote_add
2925 add,*)
2927 set-head,--*)
2928 __gitcomp_builtin remote_set-head
2930 set-branches,--*)
2931 __gitcomp_builtin remote_set-branches
2933 set-head,*|set-branches,*)
2934 __git_complete_remote_or_refspec
2936 update,--*)
2937 __gitcomp_builtin remote_update
2939 update,*)
2940 __gitcomp "$(__git_remotes) $(__git_get_config_variables "remotes")"
2942 set-url,--*)
2943 __gitcomp_builtin remote_set-url
2945 get-url,--*)
2946 __gitcomp_builtin remote_get-url
2948 prune,--*)
2949 __gitcomp_builtin remote_prune
2952 __gitcomp_nl "$(__git_remotes)"
2954 esac
2957 _git_replace ()
2959 case "$cur" in
2960 --format=*)
2961 __gitcomp "short medium long" "" "${cur##--format=}"
2962 return
2964 --*)
2965 __gitcomp_builtin replace
2966 return
2968 esac
2969 __git_complete_refs
2972 _git_rerere ()
2974 local subcommands="clear forget diff remaining status gc"
2975 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2976 if test -z "$subcommand"
2977 then
2978 __gitcomp "$subcommands"
2979 return
2983 _git_reset ()
2985 __git_has_doubledash && return
2987 case "$cur" in
2988 --*)
2989 __gitcomp_builtin reset
2990 return
2992 esac
2993 __git_complete_refs
2996 _git_restore ()
2998 __git_find_repo_path
2999 case "$prev" in
3001 __git_complete_refs
3002 return
3004 esac
3006 case "$cur" in
3007 --conflict=*)
3008 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
3010 --source=*)
3011 __git_complete_refs --cur="${cur##--source=}"
3013 --*)
3014 __gitcomp_builtin restore
3017 if __git_pseudoref_exists HEAD; then
3018 __git_complete_index_file "--modified"
3020 esac
3023 __git_revert_inprogress_options=$__git_sequencer_inprogress_options
3025 _git_revert ()
3027 __git_find_repo_path
3028 if __git_pseudoref_exists REVERT_HEAD; then
3029 __gitcomp "$__git_revert_inprogress_options"
3030 return
3032 __git_complete_strategy && return
3033 case "$cur" in
3034 --*)
3035 __gitcomp_builtin revert "" \
3036 "$__git_revert_inprogress_options"
3037 return
3039 esac
3040 __git_complete_refs
3043 _git_rm ()
3045 case "$cur" in
3046 --*)
3047 __gitcomp_builtin rm
3048 return
3050 esac
3052 __git_complete_index_file "--cached"
3055 _git_shortlog ()
3057 __git_has_doubledash && return
3059 case "$cur" in
3060 --*)
3061 __gitcomp "
3062 $__git_log_common_options
3063 $__git_log_shortlog_options
3064 --numbered --summary --email
3066 return
3068 esac
3069 __git_complete_revlist
3072 _git_show ()
3074 __git_has_doubledash && return
3076 case "$cur" in
3077 --pretty=*|--format=*)
3078 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
3079 " "" "${cur#*=}"
3080 return
3082 --diff-algorithm=*)
3083 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
3084 return
3086 --submodule=*)
3087 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
3088 return
3090 --color-moved=*)
3091 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
3092 return
3094 --color-moved-ws=*)
3095 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
3096 return
3098 --ws-error-highlight=*)
3099 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
3100 return
3102 --diff-merges=*)
3103 __gitcomp "$__git_diff_merges_opts" "" "${cur##--diff-merges=}"
3104 return
3106 --*)
3107 __gitcomp "--pretty= --format= --abbrev-commit --no-abbrev-commit
3108 --oneline --show-signature
3109 --expand-tabs --expand-tabs= --no-expand-tabs
3110 $__git_log_show_options
3111 $__git_diff_common_options
3113 return
3115 esac
3116 __git_complete_revlist_file
3119 _git_show_branch ()
3121 case "$cur" in
3122 --*)
3123 __gitcomp_builtin show-branch
3124 return
3126 esac
3127 __git_complete_revlist
3130 __gitcomp_directories ()
3132 local _tmp_dir _tmp_completions _found=0
3134 # Get the directory of the current token; this differs from dirname
3135 # in that it keeps up to the final trailing slash. If no slash found
3136 # that's fine too.
3137 [[ "$cur" =~ .*/ ]]
3138 _tmp_dir=$BASH_REMATCH
3140 # Find possible directory completions, adding trailing '/' characters,
3141 # de-quoting, and handling unusual characters.
3142 while IFS= read -r -d $'\0' c ; do
3143 # If there are directory completions, find ones that start
3144 # with "$cur", the current token, and put those in COMPREPLY
3145 if [[ $c == "$cur"* ]]; then
3146 COMPREPLY+=("$c/")
3147 _found=1
3149 done < <(__git ls-tree -z -d --name-only HEAD $_tmp_dir)
3151 if [[ $_found == 0 ]] && [[ "$cur" =~ /$ ]]; then
3152 # No possible further completions any deeper, so assume we're at
3153 # a leaf directory and just consider it complete
3154 __gitcomp_direct_append "$cur "
3155 elif [[ $_found == 0 ]]; then
3156 # No possible completions found. Avoid falling back to
3157 # bash's default file and directory completion, because all
3158 # valid completions have already been searched and the
3159 # fallbacks can do nothing but mislead. In fact, they can
3160 # mislead in three different ways:
3161 # 1) Fallback file completion makes no sense when asking
3162 # for directory completions, as this function does.
3163 # 2) Fallback directory completion is bad because
3164 # e.g. "/pro" is invalid and should NOT complete to
3165 # "/proc".
3166 # 3) Fallback file/directory completion only completes
3167 # on paths that exist in the current working tree,
3168 # i.e. which are *already* part of their
3169 # sparse-checkout. Thus, normal file and directory
3170 # completion is always useless for "git
3171 # sparse-checkout add" and is also probelmatic for
3172 # "git sparse-checkout set" unless using it to
3173 # strictly narrow the checkout.
3174 COMPREPLY=( "" )
3178 # In non-cone mode, the arguments to {set,add} are supposed to be
3179 # patterns, relative to the toplevel directory. These can be any kind
3180 # of general pattern, like 'subdir/*.c' and we can't complete on all
3181 # of those. However, if the user presses Tab to get tab completion, we
3182 # presume that they are trying to provide a pattern that names a specific
3183 # path.
3184 __gitcomp_slash_leading_paths ()
3186 local dequoted_word pfx="" cur_ toplevel
3188 # Since we are dealing with a sparse-checkout, subdirectories may not
3189 # exist in the local working copy. Therefore, we want to run all
3190 # ls-files commands relative to the repository toplevel.
3191 toplevel="$(git rev-parse --show-toplevel)/"
3193 __git_dequote "$cur"
3195 # If the paths provided by the user already start with '/', then
3196 # they are considered relative to the toplevel of the repository
3197 # already. If they do not start with /, then we need to adjust
3198 # them to start with the appropriate prefix.
3199 case "$cur" in
3201 cur="${cur:1}"
3204 pfx="$(__git rev-parse --show-prefix)"
3205 esac
3207 # Since sparse-index is limited to cone-mode, in non-cone-mode the
3208 # list of valid paths is precisely the cached files in the index.
3210 # NEEDSWORK:
3211 # 1) We probably need to take care of cases where ls-files
3212 # responds with special quoting.
3213 # 2) We probably need to take care of cases where ${cur} has
3214 # some kind of special quoting.
3215 # 3) On top of any quoting from 1 & 2, we have to provide an extra
3216 # level of quoting for any paths that contain a '*', '?', '\',
3217 # '[', ']', or leading '#' or '!' since those will be
3218 # interpreted by sparse-checkout as something other than a
3219 # literal path character.
3220 # Since there are two types of quoting here, this might get really
3221 # complex. For now, just punt on all of this...
3222 completions="$(__git -C "${toplevel}" -c core.quotePath=false \
3223 ls-files --cached -- "${pfx}${cur}*" \
3224 | sed -e s%^%/% -e 's%$% %')"
3225 # Note, above, though that we needed all of the completions to be
3226 # prefixed with a '/', and we want to add a space so that bash
3227 # completion will actually complete an entry and let us move on to
3228 # the next one.
3230 # Return what we've found.
3231 if test -n "$completions"; then
3232 # We found some completions; return them
3233 local IFS=$'\n'
3234 COMPREPLY=($completions)
3235 else
3236 # Do NOT fall back to bash-style all-local-files-and-dirs
3237 # when we find no match. Such options are worse than
3238 # useless:
3239 # 1. "git sparse-checkout add" needs paths that are NOT
3240 # currently in the working copy. "git
3241 # sparse-checkout set" does as well, except in the
3242 # special cases when users are only trying to narrow
3243 # their sparse checkout to a subset of what they
3244 # already have.
3246 # 2. A path like '.config' is ambiguous as to whether
3247 # the user wants all '.config' files throughout the
3248 # tree, or just the one under the current directory.
3249 # It would result in a warning from the
3250 # sparse-checkout command due to this. As such, all
3251 # completions of paths should be prefixed with a
3252 # '/'.
3254 # 3. We don't want paths prefixed with a '/' to
3255 # complete files in the system root directory, we
3256 # want it to complete on files relative to the
3257 # repository root.
3259 # As such, make sure that NO completions are offered rather
3260 # than falling back to bash's default completions.
3261 COMPREPLY=( "" )
3265 _git_sparse_checkout ()
3267 local subcommands="list init set disable add reapply"
3268 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3269 local using_cone=true
3270 if [ -z "$subcommand" ]; then
3271 __gitcomp "$subcommands"
3272 return
3275 case "$subcommand,$cur" in
3276 *,--*)
3277 __gitcomp_builtin sparse-checkout_$subcommand "" "--"
3279 set,*|add,*)
3280 if [[ "$(__git config core.sparseCheckout)" == "true" &&
3281 "$(__git config core.sparseCheckoutCone)" == "false" &&
3282 -z "$(__git_find_on_cmdline --cone)" ]]; then
3283 using_cone=false
3285 if [[ -n "$(__git_find_on_cmdline --no-cone)" ]]; then
3286 using_cone=false
3288 if [[ "$using_cone" == "true" ]]; then
3289 __gitcomp_directories
3290 else
3291 __gitcomp_slash_leading_paths
3293 esac
3296 _git_stash ()
3298 local subcommands='push list show apply clear drop pop create branch'
3299 local subcommand="$(__git_find_on_cmdline "$subcommands save")"
3301 if [ -z "$subcommand" ]; then
3302 case "$((cword - __git_cmd_idx)),$cur" in
3303 *,--*)
3304 __gitcomp_builtin stash_push
3306 1,sa*)
3307 __gitcomp "save"
3309 1,*)
3310 __gitcomp "$subcommands"
3312 esac
3313 return
3316 case "$subcommand,$cur" in
3317 list,--*)
3318 # NEEDSWORK: can we somehow unify this with the options in _git_log() and _git_show()
3319 __gitcomp_builtin stash_list "$__git_log_common_options $__git_diff_common_options"
3321 show,--*)
3322 __gitcomp_builtin stash_show "$__git_diff_common_options"
3324 *,--*)
3325 __gitcomp_builtin "stash_$subcommand"
3327 branch,*)
3328 if [ $cword -eq $((__git_cmd_idx+2)) ]; then
3329 __git_complete_refs
3330 else
3331 __gitcomp_nl "$(__git stash list \
3332 | sed -n -e 's/:.*//p')"
3335 show,*|apply,*|drop,*|pop,*)
3336 __gitcomp_nl "$(__git stash list \
3337 | sed -n -e 's/:.*//p')"
3339 esac
3342 _git_submodule ()
3344 __git_has_doubledash && return
3346 local subcommands="add status init deinit update set-branch set-url summary foreach sync absorbgitdirs"
3347 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3348 if [ -z "$subcommand" ]; then
3349 case "$cur" in
3350 --*)
3351 __gitcomp "--quiet"
3354 __gitcomp "$subcommands"
3356 esac
3357 return
3360 case "$subcommand,$cur" in
3361 add,--*)
3362 __gitcomp "--branch --force --name --reference --depth"
3364 status,--*)
3365 __gitcomp "--cached --recursive"
3367 deinit,--*)
3368 __gitcomp "--force --all"
3370 update,--*)
3371 __gitcomp "
3372 --init --remote --no-fetch
3373 --recommend-shallow --no-recommend-shallow
3374 --force --rebase --merge --reference --depth --recursive --jobs
3377 set-branch,--*)
3378 __gitcomp "--default --branch"
3380 summary,--*)
3381 __gitcomp "--cached --files --summary-limit"
3383 foreach,--*|sync,--*)
3384 __gitcomp "--recursive"
3388 esac
3391 _git_svn ()
3393 local subcommands="
3394 init fetch clone rebase dcommit log find-rev
3395 set-tree commit-diff info create-ignore propget
3396 proplist show-ignore show-externals branch tag blame
3397 migrate mkdirs reset gc
3399 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3400 if [ -z "$subcommand" ]; then
3401 __gitcomp "$subcommands"
3402 else
3403 local remote_opts="--username= --config-dir= --no-auth-cache"
3404 local fc_opts="
3405 --follow-parent --authors-file= --repack=
3406 --no-metadata --use-svm-props --use-svnsync-props
3407 --log-window-size= --no-checkout --quiet
3408 --repack-flags --use-log-author --localtime
3409 --add-author-from
3410 --recursive
3411 --ignore-paths= --include-paths= $remote_opts
3413 local init_opts="
3414 --template= --shared= --trunk= --tags=
3415 --branches= --stdlayout --minimize-url
3416 --no-metadata --use-svm-props --use-svnsync-props
3417 --rewrite-root= --prefix= $remote_opts
3419 local cmt_opts="
3420 --edit --rmdir --find-copies-harder --copy-similarity=
3423 case "$subcommand,$cur" in
3424 fetch,--*)
3425 __gitcomp "--revision= --fetch-all $fc_opts"
3427 clone,--*)
3428 __gitcomp "--revision= $fc_opts $init_opts"
3430 init,--*)
3431 __gitcomp "$init_opts"
3433 dcommit,--*)
3434 __gitcomp "
3435 --merge --strategy= --verbose --dry-run
3436 --fetch-all --no-rebase --commit-url
3437 --revision --interactive $cmt_opts $fc_opts
3440 set-tree,--*)
3441 __gitcomp "--stdin $cmt_opts $fc_opts"
3443 create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
3444 show-externals,--*|mkdirs,--*)
3445 __gitcomp "--revision="
3447 log,--*)
3448 __gitcomp "
3449 --limit= --revision= --verbose --incremental
3450 --oneline --show-commit --non-recursive
3451 --authors-file= --color
3454 rebase,--*)
3455 __gitcomp "
3456 --merge --verbose --strategy= --local
3457 --fetch-all --dry-run $fc_opts
3460 commit-diff,--*)
3461 __gitcomp "--message= --file= --revision= $cmt_opts"
3463 info,--*)
3464 __gitcomp "--url"
3466 branch,--*)
3467 __gitcomp "--dry-run --message --tag"
3469 tag,--*)
3470 __gitcomp "--dry-run --message"
3472 blame,--*)
3473 __gitcomp "--git-format"
3475 migrate,--*)
3476 __gitcomp "
3477 --config-dir= --ignore-paths= --minimize
3478 --no-auth-cache --username=
3481 reset,--*)
3482 __gitcomp "--revision= --parent"
3486 esac
3490 _git_tag ()
3492 local i c="$__git_cmd_idx" f=0
3493 while [ $c -lt $cword ]; do
3494 i="${words[c]}"
3495 case "$i" in
3496 -d|--delete|-v|--verify)
3497 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3498 return
3503 esac
3504 ((c++))
3505 done
3507 case "$prev" in
3508 -m|-F)
3510 -*|tag)
3511 if [ $f = 1 ]; then
3512 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3516 __git_complete_refs
3518 esac
3520 case "$cur" in
3521 --*)
3522 __gitcomp_builtin tag
3524 esac
3527 _git_whatchanged ()
3529 _git_log
3532 __git_complete_worktree_paths ()
3534 local IFS=$'\n'
3535 # Generate completion reply from worktree list skipping the first
3536 # entry: it's the path of the main worktree, which can't be moved,
3537 # removed, locked, etc.
3538 __gitcomp_nl "$(git worktree list --porcelain |
3539 sed -n -e '2,$ s/^worktree //p')"
3542 _git_worktree ()
3544 local subcommands="add list lock move prune remove unlock"
3545 local subcommand subcommand_idx
3547 subcommand="$(__git_find_on_cmdline --show-idx "$subcommands")"
3548 subcommand_idx="${subcommand% *}"
3549 subcommand="${subcommand#* }"
3551 case "$subcommand,$cur" in
3553 __gitcomp "$subcommands"
3555 *,--*)
3556 __gitcomp_builtin worktree_$subcommand
3558 add,*) # usage: git worktree add [<options>] <path> [<commit-ish>]
3559 # Here we are not completing an --option, it's either the
3560 # path or a ref.
3561 case "$prev" in
3562 -b|-B) # Complete refs for branch to be created/reseted.
3563 __git_complete_refs
3565 -*) # The previous word is an -o|--option without an
3566 # unstuck argument: have to complete the path for
3567 # the new worktree, so don't list anything, but let
3568 # Bash fall back to filename completion.
3570 *) # The previous word is not an --option, so it must
3571 # be either the 'add' subcommand, the unstuck
3572 # argument of an option (e.g. branch for -b|-B), or
3573 # the path for the new worktree.
3574 if [ $cword -eq $((subcommand_idx+1)) ]; then
3575 # Right after the 'add' subcommand: have to
3576 # complete the path, so fall back to Bash
3577 # filename completion.
3579 else
3580 case "${words[cword-2]}" in
3581 -b|-B) # After '-b <branch>': have to
3582 # complete the path, so fall back
3583 # to Bash filename completion.
3585 *) # After the path: have to complete
3586 # the ref to be checked out.
3587 __git_complete_refs
3589 esac
3592 esac
3594 lock,*|remove,*|unlock,*)
3595 __git_complete_worktree_paths
3597 move,*)
3598 if [ $cword -eq $((subcommand_idx+1)) ]; then
3599 # The first parameter must be an existing working
3600 # tree to be moved.
3601 __git_complete_worktree_paths
3602 else
3603 # The second parameter is the destination: it could
3604 # be any path, so don't list anything, but let Bash
3605 # fall back to filename completion.
3609 esac
3612 __git_complete_common () {
3613 local command="$1"
3615 case "$cur" in
3616 --*)
3617 __gitcomp_builtin "$command"
3619 esac
3622 __git_cmds_with_parseopt_helper=
3623 __git_support_parseopt_helper () {
3624 test -n "$__git_cmds_with_parseopt_helper" ||
3625 __git_cmds_with_parseopt_helper="$(__git --list-cmds=parseopt)"
3627 case " $__git_cmds_with_parseopt_helper " in
3628 *" $1 "*)
3629 return 0
3632 return 1
3634 esac
3637 __git_have_func () {
3638 declare -f -- "$1" >/dev/null 2>&1
3641 __git_complete_command () {
3642 local command="$1"
3643 local completion_func="_git_${command//-/_}"
3644 if ! __git_have_func $completion_func &&
3645 __git_have_func _completion_loader
3646 then
3647 _completion_loader "git-$command"
3649 if __git_have_func $completion_func
3650 then
3651 $completion_func
3652 return 0
3653 elif __git_support_parseopt_helper "$command"
3654 then
3655 __git_complete_common "$command"
3656 return 0
3657 else
3658 return 1
3662 __git_main ()
3664 local i c=1 command __git_dir __git_repo_path
3665 local __git_C_args C_args_count=0
3666 local __git_cmd_idx
3668 while [ $c -lt $cword ]; do
3669 i="${words[c]}"
3670 case "$i" in
3671 --git-dir=*)
3672 __git_dir="${i#--git-dir=}"
3674 --git-dir)
3675 ((c++))
3676 __git_dir="${words[c]}"
3678 --bare)
3679 __git_dir="."
3681 --help)
3682 command="help"
3683 break
3685 -c|--work-tree|--namespace)
3686 ((c++))
3689 __git_C_args[C_args_count++]=-C
3690 ((c++))
3691 __git_C_args[C_args_count++]="${words[c]}"
3696 command="$i"
3697 __git_cmd_idx="$c"
3698 break
3700 esac
3701 ((c++))
3702 done
3704 if [ -z "${command-}" ]; then
3705 case "$prev" in
3706 --git-dir|-C|--work-tree)
3707 # these need a path argument, let's fall back to
3708 # Bash filename completion
3709 return
3712 __git_complete_config_variable_name_and_value
3713 return
3715 --namespace)
3716 # we don't support completing these options' arguments
3717 return
3719 esac
3720 case "$cur" in
3721 --*)
3722 __gitcomp "
3723 --paginate
3724 --no-pager
3725 --git-dir=
3726 --bare
3727 --version
3728 --exec-path
3729 --exec-path=
3730 --html-path
3731 --man-path
3732 --info-path
3733 --work-tree=
3734 --namespace=
3735 --no-replace-objects
3736 --help
3740 if test -n "${GIT_TESTING_PORCELAIN_COMMAND_LIST-}"
3741 then
3742 __gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
3743 else
3744 local list_cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config
3746 if test "${GIT_COMPLETION_SHOW_ALL_COMMANDS-}" = "1"
3747 then
3748 list_cmds=builtins,$list_cmds
3750 __gitcomp "$(__git --list-cmds=$list_cmds)"
3753 esac
3754 return
3757 __git_complete_command "$command" && return
3759 local expansion=$(__git_aliased_command "$command")
3760 if [ -n "$expansion" ]; then
3761 words[1]=$expansion
3762 __git_complete_command "$expansion"
3766 __gitk_main ()
3768 __git_has_doubledash && return
3770 local __git_repo_path
3771 __git_find_repo_path
3773 local merge=""
3774 if __git_pseudoref_exists MERGE_HEAD; then
3775 merge="--merge"
3777 case "$cur" in
3778 --*)
3779 __gitcomp "
3780 $__git_log_common_options
3781 $__git_log_gitk_options
3782 $merge
3784 return
3786 esac
3787 __git_complete_revlist
3790 if [[ -n ${ZSH_VERSION-} && -z ${GIT_SOURCING_ZSH_COMPLETION-} ]]; then
3791 echo "ERROR: this script is obsolete, please see git-completion.zsh" 1>&2
3792 return
3795 __git_func_wrap ()
3797 local cur words cword prev
3798 local __git_cmd_idx=0
3799 _get_comp_words_by_ref -n =: cur words cword prev
3803 ___git_complete ()
3805 local wrapper="__git_wrap${2}"
3806 eval "$wrapper () { __git_func_wrap $2 ; }"
3807 complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
3808 || complete -o default -o nospace -F $wrapper $1
3811 # Setup the completion for git commands
3812 # 1: command or alias
3813 # 2: function to call (e.g. `git`, `gitk`, `git_fetch`)
3814 __git_complete ()
3816 local func
3818 if __git_have_func $2; then
3819 func=$2
3820 elif __git_have_func __$2_main; then
3821 func=__$2_main
3822 elif __git_have_func _$2; then
3823 func=_$2
3824 else
3825 echo "ERROR: could not find function '$2'" 1>&2
3826 return 1
3828 ___git_complete $1 $func
3831 ___git_complete git __git_main
3832 ___git_complete gitk __gitk_main
3834 # The following are necessary only for Cygwin, and only are needed
3835 # when the user has tab-completed the executable name and consequently
3836 # included the '.exe' suffix.
3838 if [ "$OSTYPE" = cygwin ]; then
3839 ___git_complete git.exe __git_main