git-completion.bash: separate some commands onto their own line
[alt-git.git] / contrib / completion / git-completion.bash
blob1dedb14b47d3f2a969f42829c0acd984bcc852a9
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 ; ... '".
32 # If you have a command that is not part of git, but you would still
33 # like completion, you can use __git_complete:
35 # __git_complete gl git_log
37 # Or if it's a main command (i.e. git or gitk):
39 # __git_complete gk gitk
41 # Compatible with bash 3.2.57.
43 # You can set the following environment variables to influence the behavior of
44 # the completion routines:
46 # GIT_COMPLETION_CHECKOUT_NO_GUESS
48 # When set to "1", do not include "DWIM" suggestions in git-checkout
49 # and git-switch completion (e.g., completing "foo" when "origin/foo"
50 # exists).
52 # GIT_COMPLETION_SHOW_ALL
54 # When set to "1" suggest all options, including options which are
55 # typically hidden (e.g. '--allow-empty' for 'git commit').
57 case "$COMP_WORDBREAKS" in
58 *:*) : great ;;
59 *) COMP_WORDBREAKS="$COMP_WORDBREAKS:"
60 esac
62 # Discovers the path to the git repository taking any '--git-dir=<path>' and
63 # '-C <path>' options into account and stores it in the $__git_repo_path
64 # variable.
65 __git_find_repo_path ()
67 if [ -n "${__git_repo_path-}" ]; then
68 # we already know where it is
69 return
72 if [ -n "${__git_C_args-}" ]; then
73 __git_repo_path="$(git "${__git_C_args[@]}" \
74 ${__git_dir:+--git-dir="$__git_dir"} \
75 rev-parse --absolute-git-dir 2>/dev/null)"
76 elif [ -n "${__git_dir-}" ]; then
77 test -d "$__git_dir" &&
78 __git_repo_path="$__git_dir"
79 elif [ -n "${GIT_DIR-}" ]; then
80 test -d "${GIT_DIR-}" &&
81 __git_repo_path="$GIT_DIR"
82 elif [ -d .git ]; then
83 __git_repo_path=.git
84 else
85 __git_repo_path="$(git rev-parse --git-dir 2>/dev/null)"
89 # Deprecated: use __git_find_repo_path() and $__git_repo_path instead
90 # __gitdir accepts 0 or 1 arguments (i.e., location)
91 # returns location of .git repo
92 __gitdir ()
94 if [ -z "${1-}" ]; then
95 __git_find_repo_path || return 1
96 echo "$__git_repo_path"
97 elif [ -d "$1/.git" ]; then
98 echo "$1/.git"
99 else
100 echo "$1"
104 # Runs git with all the options given as argument, respecting any
105 # '--git-dir=<path>' and '-C <path>' options present on the command line
106 __git ()
108 git ${__git_C_args:+"${__git_C_args[@]}"} \
109 ${__git_dir:+--git-dir="$__git_dir"} "$@" 2>/dev/null
112 # Removes backslash escaping, single quotes and double quotes from a word,
113 # stores the result in the variable $dequoted_word.
114 # 1: The word to dequote.
115 __git_dequote ()
117 local rest="$1" len ch
119 dequoted_word=""
121 while test -n "$rest"; do
122 len=${#dequoted_word}
123 dequoted_word="$dequoted_word${rest%%[\\\'\"]*}"
124 rest="${rest:$((${#dequoted_word}-$len))}"
126 case "${rest:0:1}" in
128 ch="${rest:1:1}"
129 case "$ch" in
130 $'\n')
133 dequoted_word="$dequoted_word$ch"
135 esac
136 rest="${rest:2}"
139 rest="${rest:1}"
140 len=${#dequoted_word}
141 dequoted_word="$dequoted_word${rest%%\'*}"
142 rest="${rest:$((${#dequoted_word}-$len+1))}"
145 rest="${rest:1}"
146 while test -n "$rest" ; do
147 len=${#dequoted_word}
148 dequoted_word="$dequoted_word${rest%%[\\\"]*}"
149 rest="${rest:$((${#dequoted_word}-$len))}"
150 case "${rest:0:1}" in
152 ch="${rest:1:1}"
153 case "$ch" in
154 \"|\\|\$|\`)
155 dequoted_word="$dequoted_word$ch"
157 $'\n')
160 dequoted_word="$dequoted_word\\$ch"
162 esac
163 rest="${rest:2}"
166 rest="${rest:1}"
167 break
169 esac
170 done
172 esac
173 done
176 # The following function is based on code from:
178 # bash_completion - programmable completion functions for bash 3.2+
180 # Copyright © 2006-2008, Ian Macdonald <ian@caliban.org>
181 # © 2009-2010, Bash Completion Maintainers
182 # <bash-completion-devel@lists.alioth.debian.org>
184 # This program is free software; you can redistribute it and/or modify
185 # it under the terms of the GNU General Public License as published by
186 # the Free Software Foundation; either version 2, or (at your option)
187 # any later version.
189 # This program is distributed in the hope that it will be useful,
190 # but WITHOUT ANY WARRANTY; without even the implied warranty of
191 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
192 # GNU General Public License for more details.
194 # You should have received a copy of the GNU General Public License
195 # along with this program; if not, see <http://www.gnu.org/licenses/>.
197 # The latest version of this software can be obtained here:
199 # http://bash-completion.alioth.debian.org/
201 # RELEASE: 2.x
203 # This function can be used to access a tokenized list of words
204 # on the command line:
206 # __git_reassemble_comp_words_by_ref '=:'
207 # if test "${words_[cword_-1]}" = -w
208 # then
209 # ...
210 # fi
212 # The argument should be a collection of characters from the list of
213 # word completion separators (COMP_WORDBREAKS) to treat as ordinary
214 # characters.
216 # This is roughly equivalent to going back in time and setting
217 # COMP_WORDBREAKS to exclude those characters. The intent is to
218 # make option types like --date=<type> and <rev>:<path> easy to
219 # recognize by treating each shell word as a single token.
221 # It is best not to set COMP_WORDBREAKS directly because the value is
222 # shared with other completion scripts. By the time the completion
223 # function gets called, COMP_WORDS has already been populated so local
224 # changes to COMP_WORDBREAKS have no effect.
226 # Output: words_, cword_, cur_.
228 __git_reassemble_comp_words_by_ref()
230 local exclude i j first
231 # Which word separators to exclude?
232 exclude="${1//[^$COMP_WORDBREAKS]}"
233 cword_=$COMP_CWORD
234 if [ -z "$exclude" ]; then
235 words_=("${COMP_WORDS[@]}")
236 return
238 # List of word completion separators has shrunk;
239 # re-assemble words to complete.
240 for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
241 # Append each nonempty word consisting of just
242 # word separator characters to the current word.
243 first=t
244 while
245 [ $i -gt 0 ] &&
246 [ -n "${COMP_WORDS[$i]}" ] &&
247 # word consists of excluded word separators
248 [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
250 # Attach to the previous token,
251 # unless the previous token is the command name.
252 if [ $j -ge 2 ] && [ -n "$first" ]; then
253 ((j--))
255 first=
256 words_[$j]=${words_[j]}${COMP_WORDS[i]}
257 if [ $i = $COMP_CWORD ]; then
258 cword_=$j
260 if (($i < ${#COMP_WORDS[@]} - 1)); then
261 ((i++))
262 else
263 # Done.
264 return
266 done
267 words_[$j]=${words_[j]}${COMP_WORDS[i]}
268 if [ $i = $COMP_CWORD ]; then
269 cword_=$j
271 done
274 if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
275 _get_comp_words_by_ref ()
277 local exclude cur_ words_ cword_
278 if [ "$1" = "-n" ]; then
279 exclude=$2
280 shift 2
282 __git_reassemble_comp_words_by_ref "$exclude"
283 cur_=${words_[cword_]}
284 while [ $# -gt 0 ]; do
285 case "$1" in
286 cur)
287 cur=$cur_
289 prev)
290 prev=${words_[$cword_-1]}
292 words)
293 words=("${words_[@]}")
295 cword)
296 cword=$cword_
298 esac
299 shift
300 done
304 # Fills the COMPREPLY array with prefiltered words without any additional
305 # processing.
306 # Callers must take care of providing only words that match the current word
307 # to be completed and adding any prefix and/or suffix (trailing space!), if
308 # necessary.
309 # 1: List of newline-separated matching completion words, complete with
310 # prefix and suffix.
311 __gitcomp_direct ()
313 local IFS=$'\n'
315 COMPREPLY=($1)
318 # Similar to __gitcomp_direct, but appends to COMPREPLY instead.
319 # Callers must take care of providing only words that match the current word
320 # to be completed and adding any prefix and/or suffix (trailing space!), if
321 # necessary.
322 # 1: List of newline-separated matching completion words, complete with
323 # prefix and suffix.
324 __gitcomp_direct_append ()
326 local IFS=$'\n'
328 COMPREPLY+=($1)
331 __gitcompappend ()
333 local x i=${#COMPREPLY[@]}
334 for x in $1; do
335 if [[ "$x" == "$3"* ]]; then
336 COMPREPLY[i++]="$2$x$4"
338 done
341 __gitcompadd ()
343 COMPREPLY=()
344 __gitcompappend "$@"
347 # Generates completion reply, appending a space to possible completion words,
348 # if necessary.
349 # It accepts 1 to 4 arguments:
350 # 1: List of possible completion words.
351 # 2: A prefix to be added to each possible completion word (optional).
352 # 3: Generate possible completion matches for this word (optional).
353 # 4: A suffix to be appended to each possible completion word (optional).
354 __gitcomp ()
356 local cur_="${3-$cur}"
358 case "$cur_" in
359 --*=)
361 --no-*)
362 local c i=0 IFS=$' \t\n'
363 for c in $1; do
364 if [[ $c == "--" ]]; then
365 continue
367 c="$c${4-}"
368 if [[ $c == "$cur_"* ]]; then
369 case $c in
370 --*=|*.) ;;
371 *) c="$c " ;;
372 esac
373 COMPREPLY[i++]="${2-}$c"
375 done
378 local c i=0 IFS=$' \t\n'
379 for c in $1; do
380 if [[ $c == "--" ]]; then
381 c="--no-...${4-}"
382 if [[ $c == "$cur_"* ]]; then
383 COMPREPLY[i++]="${2-}$c "
385 break
387 c="$c${4-}"
388 if [[ $c == "$cur_"* ]]; then
389 case $c in
390 *=|*.) ;;
391 *) c="$c " ;;
392 esac
393 COMPREPLY[i++]="${2-}$c"
395 done
397 esac
400 # Clear the variables caching builtins' options when (re-)sourcing
401 # the completion script.
402 if [[ -n ${ZSH_VERSION-} ]]; then
403 unset ${(M)${(k)parameters[@]}:#__gitcomp_builtin_*} 2>/dev/null
404 else
405 unset $(compgen -v __gitcomp_builtin_)
408 # This function is equivalent to
410 # __gitcomp "$(git xxx --git-completion-helper) ..."
412 # except that the output is cached. Accept 1-3 arguments:
413 # 1: the git command to execute, this is also the cache key
414 # 2: extra options to be added on top (e.g. negative forms)
415 # 3: options to be excluded
416 __gitcomp_builtin ()
418 # spaces must be replaced with underscore for multi-word
419 # commands, e.g. "git remote add" becomes remote_add.
420 local cmd="$1"
421 local incl="${2-}"
422 local excl="${3-}"
424 local var=__gitcomp_builtin_"${cmd/-/_}"
425 local options
426 eval "options=\${$var-}"
428 if [ -z "$options" ]; then
429 local completion_helper
430 if [ "$GIT_COMPLETION_SHOW_ALL" = "1" ]; then
431 completion_helper="--git-completion-helper-all"
432 else
433 completion_helper="--git-completion-helper"
435 # leading and trailing spaces are significant to make
436 # option removal work correctly.
437 options=" $incl $(__git ${cmd/_/ } $completion_helper) " || return
439 for i in $excl; do
440 options="${options/ $i / }"
441 done
442 eval "$var=\"$options\""
445 __gitcomp "$options"
448 # Variation of __gitcomp_nl () that appends to the existing list of
449 # completion candidates, COMPREPLY.
450 __gitcomp_nl_append ()
452 local IFS=$'\n'
453 __gitcompappend "$1" "${2-}" "${3-$cur}" "${4- }"
456 # Generates completion reply from newline-separated possible completion words
457 # by appending a space to all of them.
458 # It accepts 1 to 4 arguments:
459 # 1: List of possible completion words, separated by a single newline.
460 # 2: A prefix to be added to each possible completion word (optional).
461 # 3: Generate possible completion matches for this word (optional).
462 # 4: A suffix to be appended to each possible completion word instead of
463 # the default space (optional). If specified but empty, nothing is
464 # appended.
465 __gitcomp_nl ()
467 COMPREPLY=()
468 __gitcomp_nl_append "$@"
471 # Fills the COMPREPLY array with prefiltered paths without any additional
472 # processing.
473 # Callers must take care of providing only paths that match the current path
474 # to be completed and adding any prefix path components, if necessary.
475 # 1: List of newline-separated matching paths, complete with all prefix
476 # path components.
477 __gitcomp_file_direct ()
479 local IFS=$'\n'
481 COMPREPLY=($1)
483 # use a hack to enable file mode in bash < 4
484 compopt -o filenames +o nospace 2>/dev/null ||
485 compgen -f /non-existing-dir/ >/dev/null ||
486 true
489 # Generates completion reply with compgen from newline-separated possible
490 # completion filenames.
491 # It accepts 1 to 3 arguments:
492 # 1: List of possible completion filenames, separated by a single newline.
493 # 2: A directory prefix to be added to each possible completion filename
494 # (optional).
495 # 3: Generate possible completion matches for this word (optional).
496 __gitcomp_file ()
498 local IFS=$'\n'
500 # XXX does not work when the directory prefix contains a tilde,
501 # since tilde expansion is not applied.
502 # This means that COMPREPLY will be empty and Bash default
503 # completion will be used.
504 __gitcompadd "$1" "${2-}" "${3-$cur}" ""
506 # use a hack to enable file mode in bash < 4
507 compopt -o filenames +o nospace 2>/dev/null ||
508 compgen -f /non-existing-dir/ >/dev/null ||
509 true
512 # Execute 'git ls-files', unless the --committable option is specified, in
513 # which case it runs 'git diff-index' to find out the files that can be
514 # committed. It return paths relative to the directory specified in the first
515 # argument, and using the options specified in the second argument.
516 __git_ls_files_helper ()
518 if [ "$2" == "--committable" ]; then
519 __git -C "$1" -c core.quotePath=false diff-index \
520 --name-only --relative HEAD -- "${3//\\/\\\\}*"
521 else
522 # NOTE: $2 is not quoted in order to support multiple options
523 __git -C "$1" -c core.quotePath=false ls-files \
524 --exclude-standard $2 -- "${3//\\/\\\\}*"
529 # __git_index_files accepts 1 or 2 arguments:
530 # 1: Options to pass to ls-files (required).
531 # 2: A directory path (optional).
532 # If provided, only files within the specified directory are listed.
533 # Sub directories are never recursed. Path must have a trailing
534 # slash.
535 # 3: List only paths matching this path component (optional).
536 __git_index_files ()
538 local root="$2" match="$3"
540 __git_ls_files_helper "$root" "$1" "${match:-?}" |
541 awk -F / -v pfx="${2//\\/\\\\}" '{
542 paths[$1] = 1
544 END {
545 for (p in paths) {
546 if (substr(p, 1, 1) != "\"") {
547 # No special characters, easy!
548 print pfx p
549 continue
552 # The path is quoted.
553 p = dequote(p)
554 if (p == "")
555 continue
557 # Even when a directory name itself does not contain
558 # any special characters, it will still be quoted if
559 # any of its (stripped) trailing path components do.
560 # Because of this we may have seen the same directory
561 # both quoted and unquoted.
562 if (p in paths)
563 # We have seen the same directory unquoted,
564 # skip it.
565 continue
566 else
567 print pfx p
570 function dequote(p, bs_idx, out, esc, esc_idx, dec) {
571 # Skip opening double quote.
572 p = substr(p, 2)
574 # Interpret backslash escape sequences.
575 while ((bs_idx = index(p, "\\")) != 0) {
576 out = out substr(p, 1, bs_idx - 1)
577 esc = substr(p, bs_idx + 1, 1)
578 p = substr(p, bs_idx + 2)
580 if ((esc_idx = index("abtvfr\"\\", esc)) != 0) {
581 # C-style one-character escape sequence.
582 out = out substr("\a\b\t\v\f\r\"\\",
583 esc_idx, 1)
584 } else if (esc == "n") {
585 # Uh-oh, a newline character.
586 # We cannot reliably put a pathname
587 # containing a newline into COMPREPLY,
588 # and the newline would create a mess.
589 # Skip this path.
590 return ""
591 } else {
592 # Must be a \nnn octal value, then.
593 dec = esc * 64 + \
594 substr(p, 1, 1) * 8 + \
595 substr(p, 2, 1)
596 out = out sprintf("%c", dec)
597 p = substr(p, 3)
600 # Drop closing double quote, if there is one.
601 # (There is not any if this is a directory, as it was
602 # already stripped with the trailing path components.)
603 if (substr(p, length(p), 1) == "\"")
604 out = out substr(p, 1, length(p) - 1)
605 else
606 out = out p
608 return out
612 # __git_complete_index_file requires 1 argument:
613 # 1: the options to pass to ls-file
615 # The exception is --committable, which finds the files appropriate commit.
616 __git_complete_index_file ()
618 local dequoted_word pfx="" cur_
620 __git_dequote "$cur"
622 case "$dequoted_word" in
623 ?*/*)
624 pfx="${dequoted_word%/*}/"
625 cur_="${dequoted_word##*/}"
628 cur_="$dequoted_word"
629 esac
631 __gitcomp_file_direct "$(__git_index_files "$1" "$pfx" "$cur_")"
634 # Lists branches from the local repository.
635 # 1: A prefix to be added to each listed branch (optional).
636 # 2: List only branches matching this word (optional; list all branches if
637 # unset or empty).
638 # 3: A suffix to be appended to each listed branch (optional).
639 __git_heads ()
641 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
643 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
644 "refs/heads/$cur_*" "refs/heads/$cur_*/**"
647 # Lists branches from remote repositories.
648 # 1: A prefix to be added to each listed branch (optional).
649 # 2: List only branches matching this word (optional; list all branches if
650 # unset or empty).
651 # 3: A suffix to be appended to each listed branch (optional).
652 __git_remote_heads ()
654 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
656 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
657 "refs/remotes/$cur_*" "refs/remotes/$cur_*/**"
660 # Lists tags from the local repository.
661 # Accepts the same positional parameters as __git_heads() above.
662 __git_tags ()
664 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
666 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
667 "refs/tags/$cur_*" "refs/tags/$cur_*/**"
670 # List unique branches from refs/remotes used for 'git checkout' and 'git
671 # switch' tracking DWIMery.
672 # 1: A prefix to be added to each listed branch (optional)
673 # 2: List only branches matching this word (optional; list all branches if
674 # unset or empty).
675 # 3: A suffix to be appended to each listed branch (optional).
676 __git_dwim_remote_heads ()
678 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
679 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
681 # employ the heuristic used by git checkout and git switch
682 # Try to find a remote branch that cur_es the completion word
683 # but only output if the branch name is unique
684 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
685 --sort="refname:strip=3" \
686 "refs/remotes/*/$cur_*" "refs/remotes/*/$cur_*/**" | \
687 uniq -u
690 # Lists refs from the local (by default) or from a remote repository.
691 # It accepts 0, 1 or 2 arguments:
692 # 1: The remote to list refs from (optional; ignored, if set but empty).
693 # Can be the name of a configured remote, a path, or a URL.
694 # 2: In addition to local refs, list unique branches from refs/remotes/ for
695 # 'git checkout's tracking DWIMery (optional; ignored, if set but empty).
696 # 3: A prefix to be added to each listed ref (optional).
697 # 4: List only refs matching this word (optional; list all refs if unset or
698 # empty).
699 # 5: A suffix to be appended to each listed ref (optional; ignored, if set
700 # but empty).
702 # Use __git_complete_refs() instead.
703 __git_refs ()
705 local i hash dir track="${2-}"
706 local list_refs_from=path remote="${1-}"
707 local format refs
708 local pfx="${3-}" cur_="${4-$cur}" sfx="${5-}"
709 local match="${4-}"
710 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
712 __git_find_repo_path
713 dir="$__git_repo_path"
715 if [ -z "$remote" ]; then
716 if [ -z "$dir" ]; then
717 return
719 else
720 if __git_is_configured_remote "$remote"; then
721 # configured remote takes precedence over a
722 # local directory with the same name
723 list_refs_from=remote
724 elif [ -d "$remote/.git" ]; then
725 dir="$remote/.git"
726 elif [ -d "$remote" ]; then
727 dir="$remote"
728 else
729 list_refs_from=url
733 if [ "$list_refs_from" = path ]; then
734 if [[ "$cur_" == ^* ]]; then
735 pfx="$pfx^"
736 fer_pfx="$fer_pfx^"
737 cur_=${cur_#^}
738 match=${match#^}
740 case "$cur_" in
741 refs|refs/*)
742 format="refname"
743 refs=("$match*" "$match*/**")
744 track=""
747 for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD REBASE_HEAD; do
748 case "$i" in
749 $match*)
750 if [ -e "$dir/$i" ]; then
751 echo "$pfx$i$sfx"
754 esac
755 done
756 format="refname:strip=2"
757 refs=("refs/tags/$match*" "refs/tags/$match*/**"
758 "refs/heads/$match*" "refs/heads/$match*/**"
759 "refs/remotes/$match*" "refs/remotes/$match*/**")
761 esac
762 __git_dir="$dir" __git for-each-ref --format="$fer_pfx%($format)$sfx" \
763 "${refs[@]}"
764 if [ -n "$track" ]; then
765 __git_dwim_remote_heads "$pfx" "$match" "$sfx"
767 return
769 case "$cur_" in
770 refs|refs/*)
771 __git ls-remote "$remote" "$match*" | \
772 while read -r hash i; do
773 case "$i" in
774 *^{}) ;;
775 *) echo "$pfx$i$sfx" ;;
776 esac
777 done
780 if [ "$list_refs_from" = remote ]; then
781 case "HEAD" in
782 $match*) echo "${pfx}HEAD$sfx" ;;
783 esac
784 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
785 "refs/remotes/$remote/$match*" \
786 "refs/remotes/$remote/$match*/**"
787 else
788 local query_symref
789 case "HEAD" in
790 $match*) query_symref="HEAD" ;;
791 esac
792 __git ls-remote "$remote" $query_symref \
793 "refs/tags/$match*" "refs/heads/$match*" \
794 "refs/remotes/$match*" |
795 while read -r hash i; do
796 case "$i" in
797 *^{}) ;;
798 refs/*) echo "$pfx${i#refs/*/}$sfx" ;;
799 *) echo "$pfx$i$sfx" ;; # symbolic refs
800 esac
801 done
804 esac
807 # Completes refs, short and long, local and remote, symbolic and pseudo.
809 # Usage: __git_complete_refs [<option>]...
810 # --remote=<remote>: The remote to list refs from, can be the name of a
811 # configured remote, a path, or a URL.
812 # --dwim: List unique remote branches for 'git switch's tracking DWIMery.
813 # --pfx=<prefix>: A prefix to be added to each ref.
814 # --cur=<word>: The current ref to be completed. Defaults to the current
815 # word to be completed.
816 # --sfx=<suffix>: A suffix to be appended to each ref instead of the default
817 # space.
818 # --mode=<mode>: What set of refs to complete, one of 'refs' (the default) to
819 # complete all refs, 'heads' to complete only branches, or
820 # 'remote-heads' to complete only remote branches. Note that
821 # --remote is only compatible with --mode=refs.
822 __git_complete_refs ()
824 local remote= dwim= pfx= cur_="$cur" sfx=" " mode="refs"
826 while test $# != 0; do
827 case "$1" in
828 --remote=*) remote="${1##--remote=}" ;;
829 --dwim) dwim="yes" ;;
830 # --track is an old spelling of --dwim
831 --track) dwim="yes" ;;
832 --pfx=*) pfx="${1##--pfx=}" ;;
833 --cur=*) cur_="${1##--cur=}" ;;
834 --sfx=*) sfx="${1##--sfx=}" ;;
835 --mode=*) mode="${1##--mode=}" ;;
836 *) return 1 ;;
837 esac
838 shift
839 done
841 # complete references based on the specified mode
842 case "$mode" in
843 refs)
844 __gitcomp_direct "$(__git_refs "$remote" "" "$pfx" "$cur_" "$sfx")" ;;
845 heads)
846 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" "$sfx")" ;;
847 remote-heads)
848 __gitcomp_direct "$(__git_remote_heads "$pfx" "$cur_" "$sfx")" ;;
850 return 1 ;;
851 esac
853 # Append DWIM remote branch names if requested
854 if [ "$dwim" = "yes" ]; then
855 __gitcomp_direct_append "$(__git_dwim_remote_heads "$pfx" "$cur_" "$sfx")"
859 # __git_refs2 requires 1 argument (to pass to __git_refs)
860 # Deprecated: use __git_complete_fetch_refspecs() instead.
861 __git_refs2 ()
863 local i
864 for i in $(__git_refs "$1"); do
865 echo "$i:$i"
866 done
869 # Completes refspecs for fetching from a remote repository.
870 # 1: The remote repository.
871 # 2: A prefix to be added to each listed refspec (optional).
872 # 3: The ref to be completed as a refspec instead of the current word to be
873 # completed (optional)
874 # 4: A suffix to be appended to each listed refspec instead of the default
875 # space (optional).
876 __git_complete_fetch_refspecs ()
878 local i remote="$1" pfx="${2-}" cur_="${3-$cur}" sfx="${4- }"
880 __gitcomp_direct "$(
881 for i in $(__git_refs "$remote" "" "" "$cur_") ; do
882 echo "$pfx$i:$i$sfx"
883 done
887 # __git_refs_remotes requires 1 argument (to pass to ls-remote)
888 __git_refs_remotes ()
890 local i hash
891 __git ls-remote "$1" 'refs/heads/*' | \
892 while read -r hash i; do
893 echo "$i:refs/remotes/$1/${i#refs/heads/}"
894 done
897 __git_remotes ()
899 __git_find_repo_path
900 test -d "$__git_repo_path/remotes" && ls -1 "$__git_repo_path/remotes"
901 __git remote
904 # Returns true if $1 matches the name of a configured remote, false otherwise.
905 __git_is_configured_remote ()
907 local remote
908 for remote in $(__git_remotes); do
909 if [ "$remote" = "$1" ]; then
910 return 0
912 done
913 return 1
916 __git_list_merge_strategies ()
918 LANG=C LC_ALL=C git merge -s help 2>&1 |
919 sed -n -e '/[Aa]vailable strategies are: /,/^$/{
920 s/\.$//
921 s/.*://
922 s/^[ ]*//
923 s/[ ]*$//
928 __git_merge_strategies=
929 # 'git merge -s help' (and thus detection of the merge strategy
930 # list) fails, unfortunately, if run outside of any git working
931 # tree. __git_merge_strategies is set to the empty string in
932 # that case, and the detection will be repeated the next time it
933 # is needed.
934 __git_compute_merge_strategies ()
936 test -n "$__git_merge_strategies" ||
937 __git_merge_strategies=$(__git_list_merge_strategies)
940 __git_merge_strategy_options="ours theirs subtree subtree= patience
941 histogram diff-algorithm= ignore-space-change ignore-all-space
942 ignore-space-at-eol renormalize no-renormalize no-renames
943 find-renames find-renames= rename-threshold="
945 __git_complete_revlist_file ()
947 local dequoted_word pfx ls ref cur_="$cur"
948 case "$cur_" in
949 *..?*:*)
950 return
952 ?*:*)
953 ref="${cur_%%:*}"
954 cur_="${cur_#*:}"
956 __git_dequote "$cur_"
958 case "$dequoted_word" in
959 ?*/*)
960 pfx="${dequoted_word%/*}"
961 cur_="${dequoted_word##*/}"
962 ls="$ref:$pfx"
963 pfx="$pfx/"
966 cur_="$dequoted_word"
967 ls="$ref"
969 esac
971 case "$COMP_WORDBREAKS" in
972 *:*) : great ;;
973 *) pfx="$ref:$pfx" ;;
974 esac
976 __gitcomp_file "$(__git ls-tree "$ls" \
977 | sed 's/^.* //
978 s/$//')" \
979 "$pfx" "$cur_"
981 *...*)
982 pfx="${cur_%...*}..."
983 cur_="${cur_#*...}"
984 __git_complete_refs --pfx="$pfx" --cur="$cur_"
986 *..*)
987 pfx="${cur_%..*}.."
988 cur_="${cur_#*..}"
989 __git_complete_refs --pfx="$pfx" --cur="$cur_"
992 __git_complete_refs
994 esac
997 __git_complete_file ()
999 __git_complete_revlist_file
1002 __git_complete_revlist ()
1004 __git_complete_revlist_file
1007 __git_complete_remote_or_refspec ()
1009 local cur_="$cur" cmd="${words[1]}"
1010 local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
1011 if [ "$cmd" = "remote" ]; then
1012 ((c++))
1014 while [ $c -lt $cword ]; do
1015 i="${words[c]}"
1016 case "$i" in
1017 --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
1018 -d|--delete) [ "$cmd" = "push" ] && lhs=0 ;;
1019 --all)
1020 case "$cmd" in
1021 push) no_complete_refspec=1 ;;
1022 fetch)
1023 return
1025 *) ;;
1026 esac
1028 --multiple) no_complete_refspec=1; break ;;
1029 -*) ;;
1030 *) remote="$i"; break ;;
1031 esac
1032 ((c++))
1033 done
1034 if [ -z "$remote" ]; then
1035 __gitcomp_nl "$(__git_remotes)"
1036 return
1038 if [ $no_complete_refspec = 1 ]; then
1039 return
1041 [ "$remote" = "." ] && remote=
1042 case "$cur_" in
1043 *:*)
1044 case "$COMP_WORDBREAKS" in
1045 *:*) : great ;;
1046 *) pfx="${cur_%%:*}:" ;;
1047 esac
1048 cur_="${cur_#*:}"
1049 lhs=0
1052 pfx="+"
1053 cur_="${cur_#+}"
1055 esac
1056 case "$cmd" in
1057 fetch)
1058 if [ $lhs = 1 ]; then
1059 __git_complete_fetch_refspecs "$remote" "$pfx" "$cur_"
1060 else
1061 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1064 pull|remote)
1065 if [ $lhs = 1 ]; then
1066 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
1067 else
1068 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1071 push)
1072 if [ $lhs = 1 ]; then
1073 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1074 else
1075 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
1078 esac
1081 __git_complete_strategy ()
1083 __git_compute_merge_strategies
1084 case "$prev" in
1085 -s|--strategy)
1086 __gitcomp "$__git_merge_strategies"
1087 return 0
1090 __gitcomp "$__git_merge_strategy_options"
1091 return 0
1093 esac
1094 case "$cur" in
1095 --strategy=*)
1096 __gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
1097 return 0
1099 --strategy-option=*)
1100 __gitcomp "$__git_merge_strategy_options" "" "${cur##--strategy-option=}"
1101 return 0
1103 esac
1104 return 1
1107 __git_all_commands=
1108 __git_compute_all_commands ()
1110 test -n "$__git_all_commands" ||
1111 __git_all_commands=$(__git --list-cmds=main,others,alias,nohelpers)
1114 # Lists all set config variables starting with the given section prefix,
1115 # with the prefix removed.
1116 __git_get_config_variables ()
1118 local section="$1" i IFS=$'\n'
1119 for i in $(__git config --name-only --get-regexp "^$section\..*"); do
1120 echo "${i#$section.}"
1121 done
1124 __git_pretty_aliases ()
1126 __git_get_config_variables "pretty"
1129 # __git_aliased_command requires 1 argument
1130 __git_aliased_command ()
1132 local cur=$1 last list word cmdline
1134 while [[ -n "$cur" ]]; do
1135 if [[ "$list" == *" $cur "* ]]; then
1136 # loop detected
1137 return
1140 cmdline=$(__git config --get "alias.$cur")
1141 list=" $cur $list"
1142 last=$cur
1143 cur=
1145 for word in $cmdline; do
1146 case "$word" in
1147 \!gitk|gitk)
1148 cur="gitk"
1149 break
1151 \!*) : shell command alias ;;
1152 -*) : option ;;
1153 *=*) : setting env ;;
1154 git) : git itself ;;
1155 \(\)) : skip parens of shell function definition ;;
1156 {) : skip start of shell helper function ;;
1157 :) : skip null command ;;
1158 \'*) : skip opening quote after sh -c ;;
1160 cur="$word"
1161 break
1162 esac
1163 done
1164 done
1166 cur=$last
1167 if [[ "$cur" != "$1" ]]; then
1168 echo "$cur"
1172 # Check whether one of the given words is present on the command line,
1173 # and print the first word found.
1175 # Usage: __git_find_on_cmdline [<option>]... "<wordlist>"
1176 # --show-idx: Optionally show the index of the found word in the $words array.
1177 __git_find_on_cmdline ()
1179 local word c=1 show_idx
1181 while test $# -gt 1; do
1182 case "$1" in
1183 --show-idx) show_idx=y ;;
1184 *) return 1 ;;
1185 esac
1186 shift
1187 done
1188 local wordlist="$1"
1190 while [ $c -lt $cword ]; do
1191 for word in $wordlist; do
1192 if [ "$word" = "${words[c]}" ]; then
1193 if [ -n "${show_idx-}" ]; then
1194 echo "$c $word"
1195 else
1196 echo "$word"
1198 return
1200 done
1201 ((c++))
1202 done
1205 # Similar to __git_find_on_cmdline, except that it loops backwards and thus
1206 # prints the *last* word found. Useful for finding which of two options that
1207 # supersede each other came last, such as "--guess" and "--no-guess".
1209 # Usage: __git_find_last_on_cmdline [<option>]... "<wordlist>"
1210 # --show-idx: Optionally show the index of the found word in the $words array.
1211 __git_find_last_on_cmdline ()
1213 local word c=$cword show_idx
1215 while test $# -gt 1; do
1216 case "$1" in
1217 --show-idx) show_idx=y ;;
1218 *) return 1 ;;
1219 esac
1220 shift
1221 done
1222 local wordlist="$1"
1224 while [ $c -gt 1 ]; do
1225 ((c--))
1226 for word in $wordlist; do
1227 if [ "$word" = "${words[c]}" ]; then
1228 if [ -n "$show_idx" ]; then
1229 echo "$c $word"
1230 else
1231 echo "$word"
1233 return
1235 done
1236 done
1239 # Echo the value of an option set on the command line or config
1241 # $1: short option name
1242 # $2: long option name including =
1243 # $3: list of possible values
1244 # $4: config string (optional)
1246 # example:
1247 # result="$(__git_get_option_value "-d" "--do-something=" \
1248 # "yes no" "core.doSomething")"
1250 # result is then either empty (no option set) or "yes" or "no"
1252 # __git_get_option_value requires 3 arguments
1253 __git_get_option_value ()
1255 local c short_opt long_opt val
1256 local result= values config_key word
1258 short_opt="$1"
1259 long_opt="$2"
1260 values="$3"
1261 config_key="$4"
1263 ((c = $cword - 1))
1264 while [ $c -ge 0 ]; do
1265 word="${words[c]}"
1266 for val in $values; do
1267 if [ "$short_opt$val" = "$word" ] ||
1268 [ "$long_opt$val" = "$word" ]; then
1269 result="$val"
1270 break 2
1272 done
1273 ((c--))
1274 done
1276 if [ -n "$config_key" ] && [ -z "$result" ]; then
1277 result="$(__git config "$config_key")"
1280 echo "$result"
1283 __git_has_doubledash ()
1285 local c=1
1286 while [ $c -lt $cword ]; do
1287 if [ "--" = "${words[c]}" ]; then
1288 return 0
1290 ((c++))
1291 done
1292 return 1
1295 # Try to count non option arguments passed on the command line for the
1296 # specified git command.
1297 # When options are used, it is necessary to use the special -- option to
1298 # tell the implementation were non option arguments begin.
1299 # XXX this can not be improved, since options can appear everywhere, as
1300 # an example:
1301 # git mv x -n y
1303 # __git_count_arguments requires 1 argument: the git command executed.
1304 __git_count_arguments ()
1306 local word i c=0
1308 # Skip "git" (first argument)
1309 for ((i=1; i < ${#words[@]}; i++)); do
1310 word="${words[i]}"
1312 case "$word" in
1314 # Good; we can assume that the following are only non
1315 # option arguments.
1316 ((c = 0))
1318 "$1")
1319 # Skip the specified git command and discard git
1320 # main options
1321 ((c = 0))
1324 ((c++))
1326 esac
1327 done
1329 printf "%d" $c
1332 __git_whitespacelist="nowarn warn error error-all fix"
1333 __git_patchformat="mbox stgit stgit-series hg mboxrd"
1334 __git_showcurrentpatch="diff raw"
1335 __git_am_inprogress_options="--skip --continue --resolved --abort --quit --show-current-patch"
1337 _git_am ()
1339 __git_find_repo_path
1340 if [ -d "$__git_repo_path"/rebase-apply ]; then
1341 __gitcomp "$__git_am_inprogress_options"
1342 return
1344 case "$cur" in
1345 --whitespace=*)
1346 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1347 return
1349 --patch-format=*)
1350 __gitcomp "$__git_patchformat" "" "${cur##--patch-format=}"
1351 return
1353 --show-current-patch=*)
1354 __gitcomp "$__git_showcurrentpatch" "" "${cur##--show-current-patch=}"
1355 return
1357 --*)
1358 __gitcomp_builtin am "" \
1359 "$__git_am_inprogress_options"
1360 return
1361 esac
1364 _git_apply ()
1366 case "$cur" in
1367 --whitespace=*)
1368 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1369 return
1371 --*)
1372 __gitcomp_builtin apply
1373 return
1374 esac
1377 _git_add ()
1379 case "$cur" in
1380 --chmod=*)
1381 __gitcomp "+x -x" "" "${cur##--chmod=}"
1382 return
1384 --*)
1385 __gitcomp_builtin add
1386 return
1387 esac
1389 local complete_opt="--others --modified --directory --no-empty-directory"
1390 if test -n "$(__git_find_on_cmdline "-u --update")"
1391 then
1392 complete_opt="--modified"
1394 __git_complete_index_file "$complete_opt"
1397 _git_archive ()
1399 case "$cur" in
1400 --format=*)
1401 __gitcomp "$(git archive --list)" "" "${cur##--format=}"
1402 return
1404 --remote=*)
1405 __gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
1406 return
1408 --*)
1409 __gitcomp_builtin archive "--format= --list --verbose --prefix= --worktree-attributes"
1410 return
1412 esac
1413 __git_complete_file
1416 _git_bisect ()
1418 __git_has_doubledash && return
1420 local subcommands="start bad good skip reset visualize replay log run"
1421 local subcommand="$(__git_find_on_cmdline "$subcommands")"
1422 if [ -z "$subcommand" ]; then
1423 __git_find_repo_path
1424 if [ -f "$__git_repo_path"/BISECT_START ]; then
1425 __gitcomp "$subcommands"
1426 else
1427 __gitcomp "replay start"
1429 return
1432 case "$subcommand" in
1433 bad|good|reset|skip|start)
1434 __git_complete_refs
1438 esac
1441 __git_ref_fieldlist="refname objecttype objectsize objectname upstream push HEAD symref"
1443 _git_branch ()
1445 local i c=1 only_local_ref="n" has_r="n"
1447 while [ $c -lt $cword ]; do
1448 i="${words[c]}"
1449 case "$i" in
1450 -d|-D|--delete|-m|-M|--move|-c|-C|--copy)
1451 only_local_ref="y" ;;
1452 -r|--remotes)
1453 has_r="y" ;;
1454 esac
1455 ((c++))
1456 done
1458 case "$cur" in
1459 --set-upstream-to=*)
1460 __git_complete_refs --cur="${cur##--set-upstream-to=}"
1462 --*)
1463 __gitcomp_builtin branch
1466 if [ $only_local_ref = "y" -a $has_r = "n" ]; then
1467 __gitcomp_direct "$(__git_heads "" "$cur" " ")"
1468 else
1469 __git_complete_refs
1472 esac
1475 _git_bundle ()
1477 local cmd="${words[__git_subcommand_idx+1]}"
1478 case "$cword" in
1479 $((__git_subcommand_idx+1)))
1480 __gitcomp "create list-heads verify unbundle"
1482 $((__git_subcommand_idx+2)))
1483 # looking for a file
1486 case "$cmd" in
1487 create)
1488 __git_complete_revlist
1490 esac
1492 esac
1495 # Helper function to decide whether or not we should enable DWIM logic for
1496 # git-switch and git-checkout.
1498 # To decide between the following rules in decreasing priority order:
1499 # - the last provided of "--guess" or "--no-guess" explicitly enable or
1500 # disable completion of DWIM logic respectively.
1501 # - If checkout.guess is false, disable completion of DWIM logic.
1502 # - If the --no-track option is provided, take this as a hint to disable the
1503 # DWIM completion logic
1504 # - If GIT_COMPLETION_CHECKOUT_NO_GUESS is set, disable the DWIM completion
1505 # logic, as requested by the user.
1506 # - Enable DWIM logic otherwise.
1508 __git_checkout_default_dwim_mode ()
1510 local last_option dwim_opt="--dwim"
1512 if [ "${GIT_COMPLETION_CHECKOUT_NO_GUESS-}" = "1" ]; then
1513 dwim_opt=""
1516 # --no-track disables DWIM, but with lower priority than
1517 # --guess/--no-guess/checkout.guess
1518 if [ -n "$(__git_find_on_cmdline "--no-track")" ]; then
1519 dwim_opt=""
1522 # checkout.guess = false disables DWIM, but with lower priority than
1523 # --guess/--no-guess
1524 if [ "$(__git config --type=bool checkout.guess)" = "false" ]; then
1525 dwim_opt=""
1528 # Find the last provided --guess or --no-guess
1529 last_option="$(__git_find_last_on_cmdline "--guess --no-guess")"
1530 case "$last_option" in
1531 --guess)
1532 dwim_opt="--dwim"
1534 --no-guess)
1535 dwim_opt=""
1537 esac
1539 echo "$dwim_opt"
1542 _git_checkout ()
1544 __git_has_doubledash && return
1546 local dwim_opt="$(__git_checkout_default_dwim_mode)"
1548 case "$prev" in
1549 -b|-B|--orphan)
1550 # Complete local branches (and DWIM branch
1551 # remote branch names) for an option argument
1552 # specifying a new branch name. This is for
1553 # convenience, assuming new branches are
1554 # possibly based on pre-existing branch names.
1555 __git_complete_refs $dwim_opt --mode="heads"
1556 return
1560 esac
1562 case "$cur" in
1563 --conflict=*)
1564 __gitcomp "diff3 merge" "" "${cur##--conflict=}"
1566 --*)
1567 __gitcomp_builtin checkout
1570 # At this point, we've already handled special completion for
1571 # the arguments to -b/-B, and --orphan. There are 3 main
1572 # things left we can possibly complete:
1573 # 1) a start-point for -b/-B, -d/--detach, or --orphan
1574 # 2) a remote head, for --track
1575 # 3) an arbitrary reference, possibly including DWIM names
1578 if [ -n "$(__git_find_on_cmdline "-b -B -d --detach --orphan")" ]; then
1579 __git_complete_refs --mode="refs"
1580 elif [ -n "$(__git_find_on_cmdline "--track")" ]; then
1581 __git_complete_refs --mode="remote-heads"
1582 else
1583 __git_complete_refs $dwim_opt --mode="refs"
1586 esac
1589 __git_sequencer_inprogress_options="--continue --quit --abort --skip"
1591 __git_cherry_pick_inprogress_options=$__git_sequencer_inprogress_options
1593 _git_cherry_pick ()
1595 __git_find_repo_path
1596 if [ -f "$__git_repo_path"/CHERRY_PICK_HEAD ]; then
1597 __gitcomp "$__git_cherry_pick_inprogress_options"
1598 return
1601 __git_complete_strategy && return
1603 case "$cur" in
1604 --*)
1605 __gitcomp_builtin cherry-pick "" \
1606 "$__git_cherry_pick_inprogress_options"
1609 __git_complete_refs
1611 esac
1614 _git_clean ()
1616 case "$cur" in
1617 --*)
1618 __gitcomp_builtin clean
1619 return
1621 esac
1623 # XXX should we check for -x option ?
1624 __git_complete_index_file "--others --directory"
1627 _git_clone ()
1629 case "$prev" in
1630 -c|--config)
1631 __git_complete_config_variable_name_and_value
1632 return
1634 esac
1635 case "$cur" in
1636 --config=*)
1637 __git_complete_config_variable_name_and_value \
1638 --cur="${cur##--config=}"
1639 return
1641 --*)
1642 __gitcomp_builtin clone
1643 return
1645 esac
1648 __git_untracked_file_modes="all no normal"
1650 _git_commit ()
1652 case "$prev" in
1653 -c|-C)
1654 __git_complete_refs
1655 return
1657 esac
1659 case "$cur" in
1660 --cleanup=*)
1661 __gitcomp "default scissors strip verbatim whitespace
1662 " "" "${cur##--cleanup=}"
1663 return
1665 --reuse-message=*|--reedit-message=*|\
1666 --fixup=*|--squash=*)
1667 __git_complete_refs --cur="${cur#*=}"
1668 return
1670 --untracked-files=*)
1671 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
1672 return
1674 --*)
1675 __gitcomp_builtin commit
1676 return
1677 esac
1679 if __git rev-parse --verify --quiet HEAD >/dev/null; then
1680 __git_complete_index_file "--committable"
1681 else
1682 # This is the first commit
1683 __git_complete_index_file "--cached"
1687 _git_describe ()
1689 case "$cur" in
1690 --*)
1691 __gitcomp_builtin describe
1692 return
1693 esac
1694 __git_complete_refs
1697 __git_diff_algorithms="myers minimal patience histogram"
1699 __git_diff_submodule_formats="diff log short"
1701 __git_color_moved_opts="no default plain blocks zebra dimmed-zebra"
1703 __git_color_moved_ws_opts="no ignore-space-at-eol ignore-space-change
1704 ignore-all-space allow-indentation-change"
1706 __git_diff_common_options="--stat --numstat --shortstat --summary
1707 --patch-with-stat --name-only --name-status --color
1708 --no-color --color-words --no-renames --check
1709 --color-moved --color-moved= --no-color-moved
1710 --color-moved-ws= --no-color-moved-ws
1711 --full-index --binary --abbrev --diff-filter=
1712 --find-copies-harder --ignore-cr-at-eol
1713 --text --ignore-space-at-eol --ignore-space-change
1714 --ignore-all-space --ignore-blank-lines --exit-code
1715 --quiet --ext-diff --no-ext-diff
1716 --no-prefix --src-prefix= --dst-prefix=
1717 --inter-hunk-context=
1718 --patience --histogram --minimal
1719 --raw --word-diff --word-diff-regex=
1720 --dirstat --dirstat= --dirstat-by-file
1721 --dirstat-by-file= --cumulative
1722 --diff-algorithm=
1723 --submodule --submodule= --ignore-submodules
1724 --indent-heuristic --no-indent-heuristic
1725 --textconv --no-textconv
1726 --patch --no-patch
1729 __git_diff_difftool_options="--cached --staged --pickaxe-all --pickaxe-regex
1730 --base --ours --theirs --no-index --relative --merge-base
1731 $__git_diff_common_options"
1733 _git_diff ()
1735 __git_has_doubledash && return
1737 case "$cur" in
1738 --diff-algorithm=*)
1739 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1740 return
1742 --submodule=*)
1743 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1744 return
1746 --color-moved=*)
1747 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
1748 return
1750 --color-moved-ws=*)
1751 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
1752 return
1754 --*)
1755 __gitcomp "$__git_diff_difftool_options"
1756 return
1758 esac
1759 __git_complete_revlist_file
1762 __git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff
1763 tkdiff vimdiff nvimdiff gvimdiff xxdiff araxis p4merge
1764 bc codecompare smerge
1767 _git_difftool ()
1769 __git_has_doubledash && return
1771 case "$cur" in
1772 --tool=*)
1773 __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1774 return
1776 --*)
1777 __gitcomp_builtin difftool "$__git_diff_difftool_options"
1778 return
1780 esac
1781 __git_complete_revlist_file
1784 __git_fetch_recurse_submodules="yes on-demand no"
1786 _git_fetch ()
1788 case "$cur" in
1789 --recurse-submodules=*)
1790 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1791 return
1793 --filter=*)
1794 __gitcomp "blob:none blob:limit= sparse:oid=" "" "${cur##--filter=}"
1795 return
1797 --*)
1798 __gitcomp_builtin fetch
1799 return
1801 esac
1802 __git_complete_remote_or_refspec
1805 __git_format_patch_extra_options="
1806 --full-index --not --all --no-prefix --src-prefix=
1807 --dst-prefix= --notes
1810 _git_format_patch ()
1812 case "$cur" in
1813 --thread=*)
1814 __gitcomp "
1815 deep shallow
1816 " "" "${cur##--thread=}"
1817 return
1819 --base=*|--interdiff=*|--range-diff=*)
1820 __git_complete_refs --cur="${cur#--*=}"
1821 return
1823 --*)
1824 __gitcomp_builtin format-patch "$__git_format_patch_extra_options"
1825 return
1827 esac
1828 __git_complete_revlist
1831 _git_fsck ()
1833 case "$cur" in
1834 --*)
1835 __gitcomp_builtin fsck
1836 return
1838 esac
1841 _git_gitk ()
1843 __gitk_main
1846 # Lists matching symbol names from a tag (as in ctags) file.
1847 # 1: List symbol names matching this word.
1848 # 2: The tag file to list symbol names from.
1849 # 3: A prefix to be added to each listed symbol name (optional).
1850 # 4: A suffix to be appended to each listed symbol name (optional).
1851 __git_match_ctag () {
1852 awk -v pfx="${3-}" -v sfx="${4-}" "
1853 /^${1//\//\\/}/ { print pfx \$1 sfx }
1854 " "$2"
1857 # Complete symbol names from a tag file.
1858 # Usage: __git_complete_symbol [<option>]...
1859 # --tags=<file>: The tag file to list symbol names from instead of the
1860 # default "tags".
1861 # --pfx=<prefix>: A prefix to be added to each symbol name.
1862 # --cur=<word>: The current symbol name to be completed. Defaults to
1863 # the current word to be completed.
1864 # --sfx=<suffix>: A suffix to be appended to each symbol name instead
1865 # of the default space.
1866 __git_complete_symbol () {
1867 local tags=tags pfx="" cur_="${cur-}" sfx=" "
1869 while test $# != 0; do
1870 case "$1" in
1871 --tags=*) tags="${1##--tags=}" ;;
1872 --pfx=*) pfx="${1##--pfx=}" ;;
1873 --cur=*) cur_="${1##--cur=}" ;;
1874 --sfx=*) sfx="${1##--sfx=}" ;;
1875 *) return 1 ;;
1876 esac
1877 shift
1878 done
1880 if test -r "$tags"; then
1881 __gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
1885 _git_grep ()
1887 __git_has_doubledash && return
1889 case "$cur" in
1890 --*)
1891 __gitcomp_builtin grep
1892 return
1894 esac
1896 case "$cword,$prev" in
1897 $((__git_subcommand_idx+1)),*|*,-*)
1898 __git_complete_symbol && return
1900 esac
1902 __git_complete_refs
1905 _git_help ()
1907 case "$cur" in
1908 --*)
1909 __gitcomp_builtin help
1910 return
1912 esac
1913 if test -n "$GIT_TESTING_ALL_COMMAND_LIST"
1914 then
1915 __gitcomp "$GIT_TESTING_ALL_COMMAND_LIST $(__git --list-cmds=alias,list-guide) gitk"
1916 else
1917 __gitcomp "$(__git --list-cmds=main,nohelpers,alias,list-guide) gitk"
1921 _git_init ()
1923 case "$cur" in
1924 --shared=*)
1925 __gitcomp "
1926 false true umask group all world everybody
1927 " "" "${cur##--shared=}"
1928 return
1930 --*)
1931 __gitcomp_builtin init
1932 return
1934 esac
1937 _git_ls_files ()
1939 case "$cur" in
1940 --*)
1941 __gitcomp_builtin ls-files
1942 return
1944 esac
1946 # XXX ignore options like --modified and always suggest all cached
1947 # files.
1948 __git_complete_index_file "--cached"
1951 _git_ls_remote ()
1953 case "$cur" in
1954 --*)
1955 __gitcomp_builtin ls-remote
1956 return
1958 esac
1959 __gitcomp_nl "$(__git_remotes)"
1962 _git_ls_tree ()
1964 case "$cur" in
1965 --*)
1966 __gitcomp_builtin ls-tree
1967 return
1969 esac
1971 __git_complete_file
1974 # Options that go well for log, shortlog and gitk
1975 __git_log_common_options="
1976 --not --all
1977 --branches --tags --remotes
1978 --first-parent --merges --no-merges
1979 --max-count=
1980 --max-age= --since= --after=
1981 --min-age= --until= --before=
1982 --min-parents= --max-parents=
1983 --no-min-parents --no-max-parents
1985 # Options that go well for log and gitk (not shortlog)
1986 __git_log_gitk_options="
1987 --dense --sparse --full-history
1988 --simplify-merges --simplify-by-decoration
1989 --left-right --notes --no-notes
1991 # Options that go well for log and shortlog (not gitk)
1992 __git_log_shortlog_options="
1993 --author= --committer= --grep=
1994 --all-match --invert-grep
1997 __git_log_pretty_formats="oneline short medium full fuller reference email raw format: tformat: mboxrd"
1998 __git_log_date_formats="relative iso8601 iso8601-strict rfc2822 short local default raw unix format:"
2000 _git_log ()
2002 __git_has_doubledash && return
2003 __git_find_repo_path
2005 local merge=""
2006 if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
2007 merge="--merge"
2009 case "$prev,$cur" in
2010 -L,:*:*)
2011 return # fall back to Bash filename completion
2013 -L,:*)
2014 __git_complete_symbol --cur="${cur#:}" --sfx=":"
2015 return
2017 -G,*|-S,*)
2018 __git_complete_symbol
2019 return
2021 esac
2022 case "$cur" in
2023 --pretty=*|--format=*)
2024 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2025 " "" "${cur#*=}"
2026 return
2028 --date=*)
2029 __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
2030 return
2032 --decorate=*)
2033 __gitcomp "full short no" "" "${cur##--decorate=}"
2034 return
2036 --diff-algorithm=*)
2037 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
2038 return
2040 --submodule=*)
2041 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
2042 return
2044 --no-walk=*)
2045 __gitcomp "sorted unsorted" "" "${cur##--no-walk=}"
2046 return
2048 --*)
2049 __gitcomp "
2050 $__git_log_common_options
2051 $__git_log_shortlog_options
2052 $__git_log_gitk_options
2053 --root --topo-order --date-order --reverse
2054 --follow --full-diff
2055 --abbrev-commit --no-abbrev-commit --abbrev=
2056 --relative-date --date=
2057 --pretty= --format= --oneline
2058 --show-signature
2059 --cherry-mark
2060 --cherry-pick
2061 --graph
2062 --decorate --decorate= --no-decorate
2063 --walk-reflogs
2064 --no-walk --no-walk= --do-walk
2065 --parents --children
2066 --expand-tabs --expand-tabs= --no-expand-tabs
2067 $merge
2068 $__git_diff_common_options
2069 --pickaxe-all --pickaxe-regex
2071 return
2073 -L:*:*)
2074 return # fall back to Bash filename completion
2076 -L:*)
2077 __git_complete_symbol --cur="${cur#-L:}" --sfx=":"
2078 return
2080 -G*)
2081 __git_complete_symbol --pfx="-G" --cur="${cur#-G}"
2082 return
2084 -S*)
2085 __git_complete_symbol --pfx="-S" --cur="${cur#-S}"
2086 return
2088 esac
2089 __git_complete_revlist
2092 _git_merge ()
2094 __git_complete_strategy && return
2096 case "$cur" in
2097 --*)
2098 __gitcomp_builtin merge
2099 return
2100 esac
2101 __git_complete_refs
2104 _git_mergetool ()
2106 case "$cur" in
2107 --tool=*)
2108 __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
2109 return
2111 --*)
2112 __gitcomp "--tool= --prompt --no-prompt --gui --no-gui"
2113 return
2115 esac
2118 _git_merge_base ()
2120 case "$cur" in
2121 --*)
2122 __gitcomp_builtin merge-base
2123 return
2125 esac
2126 __git_complete_refs
2129 _git_mv ()
2131 case "$cur" in
2132 --*)
2133 __gitcomp_builtin mv
2134 return
2136 esac
2138 if [ $(__git_count_arguments "mv") -gt 0 ]; then
2139 # We need to show both cached and untracked files (including
2140 # empty directories) since this may not be the last argument.
2141 __git_complete_index_file "--cached --others --directory"
2142 else
2143 __git_complete_index_file "--cached"
2147 _git_notes ()
2149 local subcommands='add append copy edit get-ref list merge prune remove show'
2150 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2152 case "$subcommand,$cur" in
2153 ,--*)
2154 __gitcomp_builtin notes
2157 case "$prev" in
2158 --ref)
2159 __git_complete_refs
2162 __gitcomp "$subcommands --ref"
2164 esac
2166 *,--reuse-message=*|*,--reedit-message=*)
2167 __git_complete_refs --cur="${cur#*=}"
2169 *,--*)
2170 __gitcomp_builtin notes_$subcommand
2172 prune,*|get-ref,*)
2173 # this command does not take a ref, do not complete it
2176 case "$prev" in
2177 -m|-F)
2180 __git_complete_refs
2182 esac
2184 esac
2187 _git_pull ()
2189 __git_complete_strategy && return
2191 case "$cur" in
2192 --recurse-submodules=*)
2193 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
2194 return
2196 --*)
2197 __gitcomp_builtin pull
2199 return
2201 esac
2202 __git_complete_remote_or_refspec
2205 __git_push_recurse_submodules="check on-demand only"
2207 __git_complete_force_with_lease ()
2209 local cur_=$1
2211 case "$cur_" in
2212 --*=)
2214 *:*)
2215 __git_complete_refs --cur="${cur_#*:}"
2218 __git_complete_refs --cur="$cur_"
2220 esac
2223 _git_push ()
2225 case "$prev" in
2226 --repo)
2227 __gitcomp_nl "$(__git_remotes)"
2228 return
2230 --recurse-submodules)
2231 __gitcomp "$__git_push_recurse_submodules"
2232 return
2234 esac
2235 case "$cur" in
2236 --repo=*)
2237 __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
2238 return
2240 --recurse-submodules=*)
2241 __gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
2242 return
2244 --force-with-lease=*)
2245 __git_complete_force_with_lease "${cur##--force-with-lease=}"
2246 return
2248 --*)
2249 __gitcomp_builtin push
2250 return
2252 esac
2253 __git_complete_remote_or_refspec
2256 _git_range_diff ()
2258 case "$cur" in
2259 --*)
2260 __gitcomp "
2261 --creation-factor= --no-dual-color
2262 $__git_diff_common_options
2264 return
2266 esac
2267 __git_complete_revlist
2270 __git_rebase_inprogress_options="--continue --skip --abort --quit --show-current-patch"
2271 __git_rebase_interactive_inprogress_options="$__git_rebase_inprogress_options --edit-todo"
2273 _git_rebase ()
2275 __git_find_repo_path
2276 if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
2277 __gitcomp "$__git_rebase_interactive_inprogress_options"
2278 return
2279 elif [ -d "$__git_repo_path"/rebase-apply ] || \
2280 [ -d "$__git_repo_path"/rebase-merge ]; then
2281 __gitcomp "$__git_rebase_inprogress_options"
2282 return
2284 __git_complete_strategy && return
2285 case "$cur" in
2286 --whitespace=*)
2287 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
2288 return
2290 --onto=*)
2291 __git_complete_refs --cur="${cur##--onto=}"
2292 return
2294 --*)
2295 __gitcomp_builtin rebase "" \
2296 "$__git_rebase_interactive_inprogress_options"
2298 return
2299 esac
2300 __git_complete_refs
2303 _git_reflog ()
2305 local subcommands="show delete expire"
2306 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2308 if [ -z "$subcommand" ]; then
2309 __gitcomp "$subcommands"
2310 else
2311 __git_complete_refs
2315 __git_send_email_confirm_options="always never auto cc compose"
2316 __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
2318 _git_send_email ()
2320 case "$prev" in
2321 --to|--cc|--bcc|--from)
2322 __gitcomp "$(__git send-email --dump-aliases)"
2323 return
2325 esac
2327 case "$cur" in
2328 --confirm=*)
2329 __gitcomp "
2330 $__git_send_email_confirm_options
2331 " "" "${cur##--confirm=}"
2332 return
2334 --suppress-cc=*)
2335 __gitcomp "
2336 $__git_send_email_suppresscc_options
2337 " "" "${cur##--suppress-cc=}"
2339 return
2341 --smtp-encryption=*)
2342 __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
2343 return
2345 --thread=*)
2346 __gitcomp "
2347 deep shallow
2348 " "" "${cur##--thread=}"
2349 return
2351 --to=*|--cc=*|--bcc=*|--from=*)
2352 __gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
2353 return
2355 --*)
2356 __gitcomp_builtin send-email "--annotate --bcc --cc --cc-cmd --chain-reply-to
2357 --compose --confirm= --dry-run --envelope-sender
2358 --from --identity
2359 --in-reply-to --no-chain-reply-to --no-signed-off-by-cc
2360 --no-suppress-from --no-thread --quiet --reply-to
2361 --signed-off-by-cc --smtp-pass --smtp-server
2362 --smtp-server-port --smtp-encryption= --smtp-user
2363 --subject --suppress-cc= --suppress-from --thread --to
2364 --validate --no-validate
2365 $__git_format_patch_extra_options"
2366 return
2368 esac
2369 __git_complete_revlist
2372 _git_stage ()
2374 _git_add
2377 _git_status ()
2379 local complete_opt
2380 local untracked_state
2382 case "$cur" in
2383 --ignore-submodules=*)
2384 __gitcomp "none untracked dirty all" "" "${cur##--ignore-submodules=}"
2385 return
2387 --untracked-files=*)
2388 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
2389 return
2391 --column=*)
2392 __gitcomp "
2393 always never auto column row plain dense nodense
2394 " "" "${cur##--column=}"
2395 return
2397 --*)
2398 __gitcomp_builtin status
2399 return
2401 esac
2403 untracked_state="$(__git_get_option_value "-u" "--untracked-files=" \
2404 "$__git_untracked_file_modes" "status.showUntrackedFiles")"
2406 case "$untracked_state" in
2408 # --ignored option does not matter
2409 complete_opt=
2411 all|normal|*)
2412 complete_opt="--cached --directory --no-empty-directory --others"
2414 if [ -n "$(__git_find_on_cmdline "--ignored")" ]; then
2415 complete_opt="$complete_opt --ignored --exclude=*"
2418 esac
2420 __git_complete_index_file "$complete_opt"
2423 _git_switch ()
2425 local dwim_opt="$(__git_checkout_default_dwim_mode)"
2427 case "$prev" in
2428 -c|-C|--orphan)
2429 # Complete local branches (and DWIM branch
2430 # remote branch names) for an option argument
2431 # specifying a new branch name. This is for
2432 # convenience, assuming new branches are
2433 # possibly based on pre-existing branch names.
2434 __git_complete_refs $dwim_opt --mode="heads"
2435 return
2439 esac
2441 case "$cur" in
2442 --conflict=*)
2443 __gitcomp "diff3 merge" "" "${cur##--conflict=}"
2445 --*)
2446 __gitcomp_builtin switch
2449 # Unlike in git checkout, git switch --orphan does not take
2450 # a start point. Thus we really have nothing to complete after
2451 # the branch name.
2452 if [ -n "$(__git_find_on_cmdline "--orphan")" ]; then
2453 return
2456 # At this point, we've already handled special completion for
2457 # -c/-C, and --orphan. There are 3 main things left to
2458 # complete:
2459 # 1) a start-point for -c/-C or -d/--detach
2460 # 2) a remote head, for --track
2461 # 3) a branch name, possibly including DWIM remote branches
2463 if [ -n "$(__git_find_on_cmdline "-c -C -d --detach")" ]; then
2464 __git_complete_refs --mode="refs"
2465 elif [ -n "$(__git_find_on_cmdline "--track")" ]; then
2466 __git_complete_refs --mode="remote-heads"
2467 else
2468 __git_complete_refs $dwim_opt --mode="heads"
2471 esac
2474 __git_config_get_set_variables ()
2476 local prevword word config_file= c=$cword
2477 while [ $c -gt 1 ]; do
2478 word="${words[c]}"
2479 case "$word" in
2480 --system|--global|--local|--file=*)
2481 config_file="$word"
2482 break
2484 -f|--file)
2485 config_file="$word $prevword"
2486 break
2488 esac
2489 prevword=$word
2490 c=$((--c))
2491 done
2493 __git config $config_file --name-only --list
2496 __git_config_vars=
2497 __git_compute_config_vars ()
2499 test -n "$__git_config_vars" ||
2500 __git_config_vars="$(git help --config-for-completion | sort -u)"
2503 # Completes possible values of various configuration variables.
2505 # Usage: __git_complete_config_variable_value [<option>]...
2506 # --varname=<word>: The name of the configuration variable whose value is
2507 # to be completed. Defaults to the previous word on the
2508 # command line.
2509 # --cur=<word>: The current value to be completed. Defaults to the current
2510 # word to be completed.
2511 __git_complete_config_variable_value ()
2513 local varname="$prev" cur_="$cur"
2515 while test $# != 0; do
2516 case "$1" in
2517 --varname=*) varname="${1##--varname=}" ;;
2518 --cur=*) cur_="${1##--cur=}" ;;
2519 *) return 1 ;;
2520 esac
2521 shift
2522 done
2524 if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
2525 varname="${varname,,}"
2526 else
2527 varname="$(echo "$varname" |tr A-Z a-z)"
2530 case "$varname" in
2531 branch.*.remote|branch.*.pushremote)
2532 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2533 return
2535 branch.*.merge)
2536 __git_complete_refs --cur="$cur_"
2537 return
2539 branch.*.rebase)
2540 __gitcomp "false true merges preserve interactive" "" "$cur_"
2541 return
2543 remote.pushdefault)
2544 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2545 return
2547 remote.*.fetch)
2548 local remote="${varname#remote.}"
2549 remote="${remote%.fetch}"
2550 if [ -z "$cur_" ]; then
2551 __gitcomp_nl "refs/heads/" "" "" ""
2552 return
2554 __gitcomp_nl "$(__git_refs_remotes "$remote")" "" "$cur_"
2555 return
2557 remote.*.push)
2558 local remote="${varname#remote.}"
2559 remote="${remote%.push}"
2560 __gitcomp_nl "$(__git for-each-ref \
2561 --format='%(refname):%(refname)' refs/heads)" "" "$cur_"
2562 return
2564 pull.twohead|pull.octopus)
2565 __git_compute_merge_strategies
2566 __gitcomp "$__git_merge_strategies" "" "$cur_"
2567 return
2569 color.pager)
2570 __gitcomp "false true" "" "$cur_"
2571 return
2573 color.*.*)
2574 __gitcomp "
2575 normal black red green yellow blue magenta cyan white
2576 bold dim ul blink reverse
2577 " "" "$cur_"
2578 return
2580 color.*)
2581 __gitcomp "false true always never auto" "" "$cur_"
2582 return
2584 diff.submodule)
2585 __gitcomp "$__git_diff_submodule_formats" "" "$cur_"
2586 return
2588 help.format)
2589 __gitcomp "man info web html" "" "$cur_"
2590 return
2592 log.date)
2593 __gitcomp "$__git_log_date_formats" "" "$cur_"
2594 return
2596 sendemail.aliasfiletype)
2597 __gitcomp "mutt mailrc pine elm gnus" "" "$cur_"
2598 return
2600 sendemail.confirm)
2601 __gitcomp "$__git_send_email_confirm_options" "" "$cur_"
2602 return
2604 sendemail.suppresscc)
2605 __gitcomp "$__git_send_email_suppresscc_options" "" "$cur_"
2606 return
2608 sendemail.transferencoding)
2609 __gitcomp "7bit 8bit quoted-printable base64" "" "$cur_"
2610 return
2612 *.*)
2613 return
2615 esac
2618 # Completes configuration sections, subsections, variable names.
2620 # Usage: __git_complete_config_variable_name [<option>]...
2621 # --cur=<word>: The current configuration section/variable name to be
2622 # completed. Defaults to the current word to be completed.
2623 # --sfx=<suffix>: A suffix to be appended to each fully completed
2624 # configuration variable name (but not to sections or
2625 # subsections) instead of the default space.
2626 __git_complete_config_variable_name ()
2628 local cur_="$cur" sfx
2630 while test $# != 0; do
2631 case "$1" in
2632 --cur=*) cur_="${1##--cur=}" ;;
2633 --sfx=*) sfx="${1##--sfx=}" ;;
2634 *) return 1 ;;
2635 esac
2636 shift
2637 done
2639 case "$cur_" in
2640 branch.*.*)
2641 local pfx="${cur_%.*}."
2642 cur_="${cur_##*.}"
2643 __gitcomp "remote pushRemote merge mergeOptions rebase" "$pfx" "$cur_" "$sfx"
2644 return
2646 branch.*)
2647 local pfx="${cur%.*}."
2648 cur_="${cur#*.}"
2649 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
2650 __gitcomp_nl_append $'autoSetupMerge\nautoSetupRebase\n' "$pfx" "$cur_" "$sfx"
2651 return
2653 guitool.*.*)
2654 local pfx="${cur_%.*}."
2655 cur_="${cur_##*.}"
2656 __gitcomp "
2657 argPrompt cmd confirm needsFile noConsole noRescan
2658 prompt revPrompt revUnmerged title
2659 " "$pfx" "$cur_" "$sfx"
2660 return
2662 difftool.*.*)
2663 local pfx="${cur_%.*}."
2664 cur_="${cur_##*.}"
2665 __gitcomp "cmd path" "$pfx" "$cur_" "$sfx"
2666 return
2668 man.*.*)
2669 local pfx="${cur_%.*}."
2670 cur_="${cur_##*.}"
2671 __gitcomp "cmd path" "$pfx" "$cur_" "$sfx"
2672 return
2674 mergetool.*.*)
2675 local pfx="${cur_%.*}."
2676 cur_="${cur_##*.}"
2677 __gitcomp "cmd path trustExitCode" "$pfx" "$cur_" "$sfx"
2678 return
2680 pager.*)
2681 local pfx="${cur_%.*}."
2682 cur_="${cur_#*.}"
2683 __git_compute_all_commands
2684 __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_" "$sfx"
2685 return
2687 remote.*.*)
2688 local pfx="${cur_%.*}."
2689 cur_="${cur_##*.}"
2690 __gitcomp "
2691 url proxy fetch push mirror skipDefaultUpdate
2692 receivepack uploadpack tagOpt pushurl
2693 " "$pfx" "$cur_" "$sfx"
2694 return
2696 remote.*)
2697 local pfx="${cur_%.*}."
2698 cur_="${cur_#*.}"
2699 __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
2700 __gitcomp_nl_append "pushDefault" "$pfx" "$cur_" "$sfx"
2701 return
2703 url.*.*)
2704 local pfx="${cur_%.*}."
2705 cur_="${cur_##*.}"
2706 __gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_" "$sfx"
2707 return
2709 *.*)
2710 __git_compute_config_vars
2711 __gitcomp "$__git_config_vars" "" "$cur_" "$sfx"
2714 __git_compute_config_vars
2715 __gitcomp "$(echo "$__git_config_vars" |
2716 awk -F . '{
2717 sections[$1] = 1
2719 END {
2720 for (s in sections)
2721 print s "."
2723 ')" "" "$cur_"
2725 esac
2728 # Completes '='-separated configuration sections/variable names and values
2729 # for 'git -c section.name=value'.
2731 # Usage: __git_complete_config_variable_name_and_value [<option>]...
2732 # --cur=<word>: The current configuration section/variable name/value to be
2733 # completed. Defaults to the current word to be completed.
2734 __git_complete_config_variable_name_and_value ()
2736 local cur_="$cur"
2738 while test $# != 0; do
2739 case "$1" in
2740 --cur=*) cur_="${1##--cur=}" ;;
2741 *) return 1 ;;
2742 esac
2743 shift
2744 done
2746 case "$cur_" in
2747 *=*)
2748 __git_complete_config_variable_value \
2749 --varname="${cur_%%=*}" --cur="${cur_#*=}"
2752 __git_complete_config_variable_name --cur="$cur_" --sfx='='
2754 esac
2757 _git_config ()
2759 case "$prev" in
2760 --get|--get-all|--unset|--unset-all)
2761 __gitcomp_nl "$(__git_config_get_set_variables)"
2762 return
2764 *.*)
2765 __git_complete_config_variable_value
2766 return
2768 esac
2769 case "$cur" in
2770 --*)
2771 __gitcomp_builtin config
2774 __git_complete_config_variable_name
2776 esac
2779 _git_remote ()
2781 local subcommands="
2782 add rename remove set-head set-branches
2783 get-url set-url show prune update
2785 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2786 if [ -z "$subcommand" ]; then
2787 case "$cur" in
2788 --*)
2789 __gitcomp_builtin remote
2792 __gitcomp "$subcommands"
2794 esac
2795 return
2798 case "$subcommand,$cur" in
2799 add,--*)
2800 __gitcomp_builtin remote_add
2802 add,*)
2804 set-head,--*)
2805 __gitcomp_builtin remote_set-head
2807 set-branches,--*)
2808 __gitcomp_builtin remote_set-branches
2810 set-head,*|set-branches,*)
2811 __git_complete_remote_or_refspec
2813 update,--*)
2814 __gitcomp_builtin remote_update
2816 update,*)
2817 __gitcomp "$(__git_remotes) $(__git_get_config_variables "remotes")"
2819 set-url,--*)
2820 __gitcomp_builtin remote_set-url
2822 get-url,--*)
2823 __gitcomp_builtin remote_get-url
2825 prune,--*)
2826 __gitcomp_builtin remote_prune
2829 __gitcomp_nl "$(__git_remotes)"
2831 esac
2834 _git_replace ()
2836 case "$cur" in
2837 --format=*)
2838 __gitcomp "short medium long" "" "${cur##--format=}"
2839 return
2841 --*)
2842 __gitcomp_builtin replace
2843 return
2845 esac
2846 __git_complete_refs
2849 _git_rerere ()
2851 local subcommands="clear forget diff remaining status gc"
2852 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2853 if test -z "$subcommand"
2854 then
2855 __gitcomp "$subcommands"
2856 return
2860 _git_reset ()
2862 __git_has_doubledash && return
2864 case "$cur" in
2865 --*)
2866 __gitcomp_builtin reset
2867 return
2869 esac
2870 __git_complete_refs
2873 _git_restore ()
2875 case "$prev" in
2877 __git_complete_refs
2878 return
2880 esac
2882 case "$cur" in
2883 --conflict=*)
2884 __gitcomp "diff3 merge" "" "${cur##--conflict=}"
2886 --source=*)
2887 __git_complete_refs --cur="${cur##--source=}"
2889 --*)
2890 __gitcomp_builtin restore
2892 esac
2895 __git_revert_inprogress_options=$__git_sequencer_inprogress_options
2897 _git_revert ()
2899 __git_find_repo_path
2900 if [ -f "$__git_repo_path"/REVERT_HEAD ]; then
2901 __gitcomp "$__git_revert_inprogress_options"
2902 return
2904 __git_complete_strategy && return
2905 case "$cur" in
2906 --*)
2907 __gitcomp_builtin revert "" \
2908 "$__git_revert_inprogress_options"
2909 return
2911 esac
2912 __git_complete_refs
2915 _git_rm ()
2917 case "$cur" in
2918 --*)
2919 __gitcomp_builtin rm
2920 return
2922 esac
2924 __git_complete_index_file "--cached"
2927 _git_shortlog ()
2929 __git_has_doubledash && return
2931 case "$cur" in
2932 --*)
2933 __gitcomp "
2934 $__git_log_common_options
2935 $__git_log_shortlog_options
2936 --numbered --summary --email
2938 return
2940 esac
2941 __git_complete_revlist
2944 _git_show ()
2946 __git_has_doubledash && return
2948 case "$cur" in
2949 --pretty=*|--format=*)
2950 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2951 " "" "${cur#*=}"
2952 return
2954 --diff-algorithm=*)
2955 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
2956 return
2958 --submodule=*)
2959 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
2960 return
2962 --color-moved=*)
2963 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
2964 return
2966 --color-moved-ws=*)
2967 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
2968 return
2970 --*)
2971 __gitcomp "--pretty= --format= --abbrev-commit --no-abbrev-commit
2972 --oneline --show-signature
2973 --expand-tabs --expand-tabs= --no-expand-tabs
2974 $__git_diff_common_options
2976 return
2978 esac
2979 __git_complete_revlist_file
2982 _git_show_branch ()
2984 case "$cur" in
2985 --*)
2986 __gitcomp_builtin show-branch
2987 return
2989 esac
2990 __git_complete_revlist
2993 _git_sparse_checkout ()
2995 local subcommands="list init set disable"
2996 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2997 if [ -z "$subcommand" ]; then
2998 __gitcomp "$subcommands"
2999 return
3002 case "$subcommand,$cur" in
3003 init,--*)
3004 __gitcomp "--cone"
3006 set,--*)
3007 __gitcomp "--stdin"
3011 esac
3014 _git_stash ()
3016 local subcommands='push list show apply clear drop pop create branch'
3017 local subcommand="$(__git_find_on_cmdline "$subcommands save")"
3019 if [ -z "$subcommand" ]; then
3020 case "$((cword - __git_subcommand_idx)),$cur" in
3021 *,--*)
3022 __gitcomp_builtin stash_push
3024 1,sa*)
3025 __gitcomp "save"
3027 1,*)
3028 __gitcomp "$subcommands"
3030 esac
3031 return
3034 case "$subcommand,$cur" in
3035 push,--*)
3036 __gitcomp_builtin stash_push
3038 save,--*)
3039 __gitcomp_builtin stash_save
3041 pop,--*)
3042 __gitcomp_builtin stash_pop
3044 apply,--*)
3045 __gitcomp_builtin stash_apply
3047 drop,--*)
3048 __gitcomp_builtin stash_drop
3050 list,--*)
3051 # NEEDSWORK: can we somehow unify this with the options in _git_log() and _git_show()
3052 __gitcomp_builtin stash_list "$__git_log_common_options $__git_diff_common_options"
3054 show,--*)
3055 __gitcomp_builtin stash_show "$__git_diff_common_options"
3057 branch,--*)
3058 __gitcomp_builtin stash_branch
3060 branch,*)
3061 if [ $cword -eq $((__git_subcommand_idx+2)) ]; then
3062 __git_complete_refs
3063 else
3064 __gitcomp_nl "$(__git stash list \
3065 | sed -n -e 's/:.*//p')"
3068 show,*|apply,*|drop,*|pop,*)
3069 __gitcomp_nl "$(__git stash list \
3070 | sed -n -e 's/:.*//p')"
3074 esac
3077 _git_submodule ()
3079 __git_has_doubledash && return
3081 local subcommands="add status init deinit update set-branch set-url summary foreach sync absorbgitdirs"
3082 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3083 if [ -z "$subcommand" ]; then
3084 case "$cur" in
3085 --*)
3086 __gitcomp "--quiet"
3089 __gitcomp "$subcommands"
3091 esac
3092 return
3095 case "$subcommand,$cur" in
3096 add,--*)
3097 __gitcomp "--branch --force --name --reference --depth"
3099 status,--*)
3100 __gitcomp "--cached --recursive"
3102 deinit,--*)
3103 __gitcomp "--force --all"
3105 update,--*)
3106 __gitcomp "
3107 --init --remote --no-fetch
3108 --recommend-shallow --no-recommend-shallow
3109 --force --rebase --merge --reference --depth --recursive --jobs
3112 set-branch,--*)
3113 __gitcomp "--default --branch"
3115 summary,--*)
3116 __gitcomp "--cached --files --summary-limit"
3118 foreach,--*|sync,--*)
3119 __gitcomp "--recursive"
3123 esac
3126 _git_svn ()
3128 local subcommands="
3129 init fetch clone rebase dcommit log find-rev
3130 set-tree commit-diff info create-ignore propget
3131 proplist show-ignore show-externals branch tag blame
3132 migrate mkdirs reset gc
3134 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3135 if [ -z "$subcommand" ]; then
3136 __gitcomp "$subcommands"
3137 else
3138 local remote_opts="--username= --config-dir= --no-auth-cache"
3139 local fc_opts="
3140 --follow-parent --authors-file= --repack=
3141 --no-metadata --use-svm-props --use-svnsync-props
3142 --log-window-size= --no-checkout --quiet
3143 --repack-flags --use-log-author --localtime
3144 --add-author-from
3145 --recursive
3146 --ignore-paths= --include-paths= $remote_opts
3148 local init_opts="
3149 --template= --shared= --trunk= --tags=
3150 --branches= --stdlayout --minimize-url
3151 --no-metadata --use-svm-props --use-svnsync-props
3152 --rewrite-root= --prefix= $remote_opts
3154 local cmt_opts="
3155 --edit --rmdir --find-copies-harder --copy-similarity=
3158 case "$subcommand,$cur" in
3159 fetch,--*)
3160 __gitcomp "--revision= --fetch-all $fc_opts"
3162 clone,--*)
3163 __gitcomp "--revision= $fc_opts $init_opts"
3165 init,--*)
3166 __gitcomp "$init_opts"
3168 dcommit,--*)
3169 __gitcomp "
3170 --merge --strategy= --verbose --dry-run
3171 --fetch-all --no-rebase --commit-url
3172 --revision --interactive $cmt_opts $fc_opts
3175 set-tree,--*)
3176 __gitcomp "--stdin $cmt_opts $fc_opts"
3178 create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
3179 show-externals,--*|mkdirs,--*)
3180 __gitcomp "--revision="
3182 log,--*)
3183 __gitcomp "
3184 --limit= --revision= --verbose --incremental
3185 --oneline --show-commit --non-recursive
3186 --authors-file= --color
3189 rebase,--*)
3190 __gitcomp "
3191 --merge --verbose --strategy= --local
3192 --fetch-all --dry-run $fc_opts
3195 commit-diff,--*)
3196 __gitcomp "--message= --file= --revision= $cmt_opts"
3198 info,--*)
3199 __gitcomp "--url"
3201 branch,--*)
3202 __gitcomp "--dry-run --message --tag"
3204 tag,--*)
3205 __gitcomp "--dry-run --message"
3207 blame,--*)
3208 __gitcomp "--git-format"
3210 migrate,--*)
3211 __gitcomp "
3212 --config-dir= --ignore-paths= --minimize
3213 --no-auth-cache --username=
3216 reset,--*)
3217 __gitcomp "--revision= --parent"
3221 esac
3225 _git_tag ()
3227 local i c=1 f=0
3228 while [ $c -lt $cword ]; do
3229 i="${words[c]}"
3230 case "$i" in
3231 -d|--delete|-v|--verify)
3232 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3233 return
3238 esac
3239 ((c++))
3240 done
3242 case "$prev" in
3243 -m|-F)
3245 -*|tag)
3246 if [ $f = 1 ]; then
3247 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3251 __git_complete_refs
3253 esac
3255 case "$cur" in
3256 --*)
3257 __gitcomp_builtin tag
3259 esac
3262 _git_whatchanged ()
3264 _git_log
3267 __git_complete_worktree_paths ()
3269 local IFS=$'\n'
3270 __gitcomp_nl "$(git worktree list --porcelain |
3271 # Skip the first entry: it's the path of the main worktree,
3272 # which can't be moved, removed, locked, etc.
3273 sed -n -e '2,$ s/^worktree //p')"
3276 _git_worktree ()
3278 local subcommands="add list lock move prune remove unlock"
3279 local subcommand
3281 subcommand="$(__git_find_on_cmdline "$subcommands")"
3283 case "$subcommand,$cur" in
3285 __gitcomp "$subcommands"
3287 *,--*)
3288 __gitcomp_builtin worktree_$subcommand
3290 add,*) # usage: git worktree add [<options>] <path> [<commit-ish>]
3291 # Here we are not completing an --option, it's either the
3292 # path or a ref.
3293 case "$prev" in
3294 -b|-B) # Complete refs for branch to be created/reseted.
3295 __git_complete_refs
3297 -*) # The previous word is an -o|--option without an
3298 # unstuck argument: have to complete the path for
3299 # the new worktree, so don't list anything, but let
3300 # Bash fall back to filename completion.
3302 *) # The previous word is not an --option, so it must
3303 # be either the 'add' subcommand, the unstuck
3304 # argument of an option (e.g. branch for -b|-B), or
3305 # the path for the new worktree.
3306 if [ $cword -eq $((__git_subcommand_idx+2)) ]; then
3307 # Right after the 'add' subcommand: have to
3308 # complete the path, so fall back to Bash
3309 # filename completion.
3311 else
3312 case "${words[cword-2]}" in
3313 -b|-B) # After '-b <branch>': have to
3314 # complete the path, so fall back
3315 # to Bash filename completion.
3317 *) # After the path: have to complete
3318 # the ref to be checked out.
3319 __git_complete_refs
3321 esac
3324 esac
3326 lock,*|remove,*|unlock,*)
3327 __git_complete_worktree_paths
3329 move,*)
3330 if [ $cword -eq $((__git_subcommand_idx+2)) ]; then
3331 # The first parameter must be an existing working
3332 # tree to be moved.
3333 __git_complete_worktree_paths
3334 else
3335 # The second parameter is the destination: it could
3336 # be any path, so don't list anything, but let Bash
3337 # fall back to filename completion.
3341 esac
3344 __git_complete_common () {
3345 local command="$1"
3347 case "$cur" in
3348 --*)
3349 __gitcomp_builtin "$command"
3351 esac
3354 __git_cmds_with_parseopt_helper=
3355 __git_support_parseopt_helper () {
3356 test -n "$__git_cmds_with_parseopt_helper" ||
3357 __git_cmds_with_parseopt_helper="$(__git --list-cmds=parseopt)"
3359 case " $__git_cmds_with_parseopt_helper " in
3360 *" $1 "*)
3361 return 0
3364 return 1
3366 esac
3369 __git_have_func () {
3370 declare -f -- "$1" >/dev/null 2>&1
3373 __git_complete_command () {
3374 local command="$1"
3375 local completion_func="_git_${command//-/_}"
3376 if ! __git_have_func $completion_func &&
3377 __git_have_func _completion_loader
3378 then
3379 _completion_loader "git-$command"
3381 if __git_have_func $completion_func
3382 then
3383 $completion_func
3384 return 0
3385 elif __git_support_parseopt_helper "$command"
3386 then
3387 __git_complete_common "$command"
3388 return 0
3389 else
3390 return 1
3394 __git_main ()
3396 local i c=1 command __git_dir __git_repo_path
3397 local __git_C_args C_args_count=0
3398 local __git_subcommand_idx
3400 while [ $c -lt $cword ]; do
3401 i="${words[c]}"
3402 case "$i" in
3403 --git-dir=*)
3404 __git_dir="${i#--git-dir=}"
3406 --git-dir)
3407 ((c++))
3408 __git_dir="${words[c]}"
3410 --bare)
3411 __git_dir="."
3413 --help)
3414 command="help"
3415 break
3417 -c|--work-tree|--namespace)
3418 ((c++))
3421 __git_C_args[C_args_count++]=-C
3422 ((c++))
3423 __git_C_args[C_args_count++]="${words[c]}"
3428 command="$i"
3429 __git_subcommand_idx="$c"
3430 break
3432 esac
3433 ((c++))
3434 done
3436 if [ -z "${command-}" ]; then
3437 case "$prev" in
3438 --git-dir|-C|--work-tree)
3439 # these need a path argument, let's fall back to
3440 # Bash filename completion
3441 return
3444 __git_complete_config_variable_name_and_value
3445 return
3447 --namespace)
3448 # we don't support completing these options' arguments
3449 return
3451 esac
3452 case "$cur" in
3453 --*)
3454 __gitcomp "
3455 --paginate
3456 --no-pager
3457 --git-dir=
3458 --bare
3459 --version
3460 --exec-path
3461 --exec-path=
3462 --html-path
3463 --man-path
3464 --info-path
3465 --work-tree=
3466 --namespace=
3467 --no-replace-objects
3468 --help
3472 if test -n "${GIT_TESTING_PORCELAIN_COMMAND_LIST-}"
3473 then
3474 __gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
3475 else
3476 __gitcomp "$(__git --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config)"
3479 esac
3480 return
3483 __git_complete_command "$command" && return
3485 local expansion=$(__git_aliased_command "$command")
3486 if [ -n "$expansion" ]; then
3487 words[1]=$expansion
3488 __git_complete_command "$expansion"
3492 __gitk_main ()
3494 __git_has_doubledash && return
3496 local __git_repo_path
3497 __git_find_repo_path
3499 local merge=""
3500 if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
3501 merge="--merge"
3503 case "$cur" in
3504 --*)
3505 __gitcomp "
3506 $__git_log_common_options
3507 $__git_log_gitk_options
3508 $merge
3510 return
3512 esac
3513 __git_complete_revlist
3516 if [[ -n ${ZSH_VERSION-} && -z ${GIT_SOURCING_ZSH_COMPLETION-} ]]; then
3517 echo "ERROR: this script is obsolete, please see git-completion.zsh" 1>&2
3518 return
3521 __git_func_wrap ()
3523 local cur words cword prev
3524 _get_comp_words_by_ref -n =: cur words cword prev
3528 ___git_complete ()
3530 local wrapper="__git_wrap${2}"
3531 eval "$wrapper () { __git_func_wrap $2 ; }"
3532 complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
3533 || complete -o default -o nospace -F $wrapper $1
3536 # Setup the completion for git commands
3537 # 1: command or alias
3538 # 2: function to call (e.g. `git`, `gitk`, `git_fetch`)
3539 __git_complete ()
3541 local func
3543 if __git_have_func $2; then
3544 func=$2
3545 elif __git_have_func __$2_main; then
3546 func=__$2_main
3547 elif __git_have_func _$2; then
3548 func=_$2
3549 else
3550 echo "ERROR: could not find function '$2'" 1>&2
3551 return 1
3553 ___git_complete $1 $func
3556 ___git_complete git __git_main
3557 ___git_complete gitk __gitk_main
3559 # The following are necessary only for Cygwin, and only are needed
3560 # when the user has tab-completed the executable name and consequently
3561 # included the '.exe' suffix.
3563 if [ "$OSTYPE" = cygwin ]; then
3564 ___git_complete git.exe __git_main