completion: use __git when calling --list-cmds
[git.git] / contrib / completion / git-completion.bash
blob93b5e972cb633fe6dd9009d05acbb7be0c976294
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 # Compatible with bash 3.2.57.
34 # You can set the following environment variables to influence the behavior of
35 # the completion routines:
37 # GIT_COMPLETION_CHECKOUT_NO_GUESS
39 # When set to "1", do not include "DWIM" suggestions in git-checkout
40 # completion (e.g., completing "foo" when "origin/foo" exists).
42 case "$COMP_WORDBREAKS" in
43 *:*) : great ;;
44 *) COMP_WORDBREAKS="$COMP_WORDBREAKS:"
45 esac
47 # Discovers the path to the git repository taking any '--git-dir=<path>' and
48 # '-C <path>' options into account and stores it in the $__git_repo_path
49 # variable.
50 __git_find_repo_path ()
52 if [ -n "$__git_repo_path" ]; then
53 # we already know where it is
54 return
57 if [ -n "${__git_C_args-}" ]; then
58 __git_repo_path="$(git "${__git_C_args[@]}" \
59 ${__git_dir:+--git-dir="$__git_dir"} \
60 rev-parse --absolute-git-dir 2>/dev/null)"
61 elif [ -n "${__git_dir-}" ]; then
62 test -d "$__git_dir" &&
63 __git_repo_path="$__git_dir"
64 elif [ -n "${GIT_DIR-}" ]; then
65 test -d "${GIT_DIR-}" &&
66 __git_repo_path="$GIT_DIR"
67 elif [ -d .git ]; then
68 __git_repo_path=.git
69 else
70 __git_repo_path="$(git rev-parse --git-dir 2>/dev/null)"
74 # Deprecated: use __git_find_repo_path() and $__git_repo_path instead
75 # __gitdir accepts 0 or 1 arguments (i.e., location)
76 # returns location of .git repo
77 __gitdir ()
79 if [ -z "${1-}" ]; then
80 __git_find_repo_path || return 1
81 echo "$__git_repo_path"
82 elif [ -d "$1/.git" ]; then
83 echo "$1/.git"
84 else
85 echo "$1"
89 # Runs git with all the options given as argument, respecting any
90 # '--git-dir=<path>' and '-C <path>' options present on the command line
91 __git ()
93 git ${__git_C_args:+"${__git_C_args[@]}"} \
94 ${__git_dir:+--git-dir="$__git_dir"} "$@" 2>/dev/null
97 # Removes backslash escaping, single quotes and double quotes from a word,
98 # stores the result in the variable $dequoted_word.
99 # 1: The word to dequote.
100 __git_dequote ()
102 local rest="$1" len ch
104 dequoted_word=""
106 while test -n "$rest"; do
107 len=${#dequoted_word}
108 dequoted_word="$dequoted_word${rest%%[\\\'\"]*}"
109 rest="${rest:$((${#dequoted_word}-$len))}"
111 case "${rest:0:1}" in
113 ch="${rest:1:1}"
114 case "$ch" in
115 $'\n')
118 dequoted_word="$dequoted_word$ch"
120 esac
121 rest="${rest:2}"
124 rest="${rest:1}"
125 len=${#dequoted_word}
126 dequoted_word="$dequoted_word${rest%%\'*}"
127 rest="${rest:$((${#dequoted_word}-$len+1))}"
130 rest="${rest:1}"
131 while test -n "$rest" ; do
132 len=${#dequoted_word}
133 dequoted_word="$dequoted_word${rest%%[\\\"]*}"
134 rest="${rest:$((${#dequoted_word}-$len))}"
135 case "${rest:0:1}" in
137 ch="${rest:1:1}"
138 case "$ch" in
139 \"|\\|\$|\`)
140 dequoted_word="$dequoted_word$ch"
142 $'\n')
145 dequoted_word="$dequoted_word\\$ch"
147 esac
148 rest="${rest:2}"
151 rest="${rest:1}"
152 break
154 esac
155 done
157 esac
158 done
161 # The following function is based on code from:
163 # bash_completion - programmable completion functions for bash 3.2+
165 # Copyright © 2006-2008, Ian Macdonald <ian@caliban.org>
166 # © 2009-2010, Bash Completion Maintainers
167 # <bash-completion-devel@lists.alioth.debian.org>
169 # This program is free software; you can redistribute it and/or modify
170 # it under the terms of the GNU General Public License as published by
171 # the Free Software Foundation; either version 2, or (at your option)
172 # any later version.
174 # This program is distributed in the hope that it will be useful,
175 # but WITHOUT ANY WARRANTY; without even the implied warranty of
176 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
177 # GNU General Public License for more details.
179 # You should have received a copy of the GNU General Public License
180 # along with this program; if not, see <http://www.gnu.org/licenses/>.
182 # The latest version of this software can be obtained here:
184 # http://bash-completion.alioth.debian.org/
186 # RELEASE: 2.x
188 # This function can be used to access a tokenized list of words
189 # on the command line:
191 # __git_reassemble_comp_words_by_ref '=:'
192 # if test "${words_[cword_-1]}" = -w
193 # then
194 # ...
195 # fi
197 # The argument should be a collection of characters from the list of
198 # word completion separators (COMP_WORDBREAKS) to treat as ordinary
199 # characters.
201 # This is roughly equivalent to going back in time and setting
202 # COMP_WORDBREAKS to exclude those characters. The intent is to
203 # make option types like --date=<type> and <rev>:<path> easy to
204 # recognize by treating each shell word as a single token.
206 # It is best not to set COMP_WORDBREAKS directly because the value is
207 # shared with other completion scripts. By the time the completion
208 # function gets called, COMP_WORDS has already been populated so local
209 # changes to COMP_WORDBREAKS have no effect.
211 # Output: words_, cword_, cur_.
213 __git_reassemble_comp_words_by_ref()
215 local exclude i j first
216 # Which word separators to exclude?
217 exclude="${1//[^$COMP_WORDBREAKS]}"
218 cword_=$COMP_CWORD
219 if [ -z "$exclude" ]; then
220 words_=("${COMP_WORDS[@]}")
221 return
223 # List of word completion separators has shrunk;
224 # re-assemble words to complete.
225 for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
226 # Append each nonempty word consisting of just
227 # word separator characters to the current word.
228 first=t
229 while
230 [ $i -gt 0 ] &&
231 [ -n "${COMP_WORDS[$i]}" ] &&
232 # word consists of excluded word separators
233 [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
235 # Attach to the previous token,
236 # unless the previous token is the command name.
237 if [ $j -ge 2 ] && [ -n "$first" ]; then
238 ((j--))
240 first=
241 words_[$j]=${words_[j]}${COMP_WORDS[i]}
242 if [ $i = $COMP_CWORD ]; then
243 cword_=$j
245 if (($i < ${#COMP_WORDS[@]} - 1)); then
246 ((i++))
247 else
248 # Done.
249 return
251 done
252 words_[$j]=${words_[j]}${COMP_WORDS[i]}
253 if [ $i = $COMP_CWORD ]; then
254 cword_=$j
256 done
259 if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
260 _get_comp_words_by_ref ()
262 local exclude cur_ words_ cword_
263 if [ "$1" = "-n" ]; then
264 exclude=$2
265 shift 2
267 __git_reassemble_comp_words_by_ref "$exclude"
268 cur_=${words_[cword_]}
269 while [ $# -gt 0 ]; do
270 case "$1" in
271 cur)
272 cur=$cur_
274 prev)
275 prev=${words_[$cword_-1]}
277 words)
278 words=("${words_[@]}")
280 cword)
281 cword=$cword_
283 esac
284 shift
285 done
289 # Fills the COMPREPLY array with prefiltered words without any additional
290 # processing.
291 # Callers must take care of providing only words that match the current word
292 # to be completed and adding any prefix and/or suffix (trailing space!), if
293 # necessary.
294 # 1: List of newline-separated matching completion words, complete with
295 # prefix and suffix.
296 __gitcomp_direct ()
298 local IFS=$'\n'
300 COMPREPLY=($1)
303 __gitcompappend ()
305 local x i=${#COMPREPLY[@]}
306 for x in $1; do
307 if [[ "$x" == "$3"* ]]; then
308 COMPREPLY[i++]="$2$x$4"
310 done
313 __gitcompadd ()
315 COMPREPLY=()
316 __gitcompappend "$@"
319 # Generates completion reply, appending a space to possible completion words,
320 # if necessary.
321 # It accepts 1 to 4 arguments:
322 # 1: List of possible completion words.
323 # 2: A prefix to be added to each possible completion word (optional).
324 # 3: Generate possible completion matches for this word (optional).
325 # 4: A suffix to be appended to each possible completion word (optional).
326 __gitcomp ()
328 local cur_="${3-$cur}"
330 case "$cur_" in
331 --*=)
333 --no-*)
334 local c i=0 IFS=$' \t\n'
335 for c in $1; do
336 if [[ $c == "--" ]]; then
337 continue
339 c="$c${4-}"
340 if [[ $c == "$cur_"* ]]; then
341 case $c in
342 --*=*|*.) ;;
343 *) c="$c " ;;
344 esac
345 COMPREPLY[i++]="${2-}$c"
347 done
350 local c i=0 IFS=$' \t\n'
351 for c in $1; do
352 if [[ $c == "--" ]]; then
353 c="--no-...${4-}"
354 if [[ $c == "$cur_"* ]]; then
355 COMPREPLY[i++]="${2-}$c "
357 break
359 c="$c${4-}"
360 if [[ $c == "$cur_"* ]]; then
361 case $c in
362 --*=*|*.) ;;
363 *) c="$c " ;;
364 esac
365 COMPREPLY[i++]="${2-}$c"
367 done
369 esac
372 # Clear the variables caching builtins' options when (re-)sourcing
373 # the completion script.
374 if [[ -n ${ZSH_VERSION-} ]]; then
375 unset $(set |sed -ne 's/^\(__gitcomp_builtin_[a-zA-Z0-9_][a-zA-Z0-9_]*\)=.*/\1/p') 2>/dev/null
376 else
377 unset $(compgen -v __gitcomp_builtin_)
380 # This function is equivalent to
382 # __gitcomp "$(git xxx --git-completion-helper) ..."
384 # except that the output is cached. Accept 1-3 arguments:
385 # 1: the git command to execute, this is also the cache key
386 # 2: extra options to be added on top (e.g. negative forms)
387 # 3: options to be excluded
388 __gitcomp_builtin ()
390 # spaces must be replaced with underscore for multi-word
391 # commands, e.g. "git remote add" becomes remote_add.
392 local cmd="$1"
393 local incl="$2"
394 local excl="$3"
396 local var=__gitcomp_builtin_"${cmd/-/_}"
397 local options
398 eval "options=\$$var"
400 if [ -z "$options" ]; then
401 # leading and trailing spaces are significant to make
402 # option removal work correctly.
403 options=" $incl $(__git ${cmd/_/ } --git-completion-helper) "
404 for i in $excl; do
405 options="${options/ $i / }"
406 done
407 eval "$var=\"$options\""
410 __gitcomp "$options"
413 # Variation of __gitcomp_nl () that appends to the existing list of
414 # completion candidates, COMPREPLY.
415 __gitcomp_nl_append ()
417 local IFS=$'\n'
418 __gitcompappend "$1" "${2-}" "${3-$cur}" "${4- }"
421 # Generates completion reply from newline-separated possible completion words
422 # by appending a space to all of them.
423 # It accepts 1 to 4 arguments:
424 # 1: List of possible completion words, separated by a single newline.
425 # 2: A prefix to be added to each possible completion word (optional).
426 # 3: Generate possible completion matches for this word (optional).
427 # 4: A suffix to be appended to each possible completion word instead of
428 # the default space (optional). If specified but empty, nothing is
429 # appended.
430 __gitcomp_nl ()
432 COMPREPLY=()
433 __gitcomp_nl_append "$@"
436 # Fills the COMPREPLY array with prefiltered paths without any additional
437 # processing.
438 # Callers must take care of providing only paths that match the current path
439 # to be completed and adding any prefix path components, if necessary.
440 # 1: List of newline-separated matching paths, complete with all prefix
441 # path components.
442 __gitcomp_file_direct ()
444 local IFS=$'\n'
446 COMPREPLY=($1)
448 # use a hack to enable file mode in bash < 4
449 compopt -o filenames +o nospace 2>/dev/null ||
450 compgen -f /non-existing-dir/ >/dev/null ||
451 true
454 # Generates completion reply with compgen from newline-separated possible
455 # completion filenames.
456 # It accepts 1 to 3 arguments:
457 # 1: List of possible completion filenames, separated by a single newline.
458 # 2: A directory prefix to be added to each possible completion filename
459 # (optional).
460 # 3: Generate possible completion matches for this word (optional).
461 __gitcomp_file ()
463 local IFS=$'\n'
465 # XXX does not work when the directory prefix contains a tilde,
466 # since tilde expansion is not applied.
467 # This means that COMPREPLY will be empty and Bash default
468 # completion will be used.
469 __gitcompadd "$1" "${2-}" "${3-$cur}" ""
471 # use a hack to enable file mode in bash < 4
472 compopt -o filenames +o nospace 2>/dev/null ||
473 compgen -f /non-existing-dir/ >/dev/null ||
474 true
477 # Execute 'git ls-files', unless the --committable option is specified, in
478 # which case it runs 'git diff-index' to find out the files that can be
479 # committed. It return paths relative to the directory specified in the first
480 # argument, and using the options specified in the second argument.
481 __git_ls_files_helper ()
483 if [ "$2" == "--committable" ]; then
484 __git -C "$1" -c core.quotePath=false diff-index \
485 --name-only --relative HEAD -- "${3//\\/\\\\}*"
486 else
487 # NOTE: $2 is not quoted in order to support multiple options
488 __git -C "$1" -c core.quotePath=false ls-files \
489 --exclude-standard $2 -- "${3//\\/\\\\}*"
494 # __git_index_files accepts 1 or 2 arguments:
495 # 1: Options to pass to ls-files (required).
496 # 2: A directory path (optional).
497 # If provided, only files within the specified directory are listed.
498 # Sub directories are never recursed. Path must have a trailing
499 # slash.
500 # 3: List only paths matching this path component (optional).
501 __git_index_files ()
503 local root="$2" match="$3"
505 __git_ls_files_helper "$root" "$1" "$match" |
506 awk -F / -v pfx="${2//\\/\\\\}" '{
507 paths[$1] = 1
509 END {
510 for (p in paths) {
511 if (substr(p, 1, 1) != "\"") {
512 # No special characters, easy!
513 print pfx p
514 continue
517 # The path is quoted.
518 p = dequote(p)
519 if (p == "")
520 continue
522 # Even when a directory name itself does not contain
523 # any special characters, it will still be quoted if
524 # any of its (stripped) trailing path components do.
525 # Because of this we may have seen the same direcory
526 # both quoted and unquoted.
527 if (p in paths)
528 # We have seen the same directory unquoted,
529 # skip it.
530 continue
531 else
532 print pfx p
535 function dequote(p, bs_idx, out, esc, esc_idx, dec) {
536 # Skip opening double quote.
537 p = substr(p, 2)
539 # Interpret backslash escape sequences.
540 while ((bs_idx = index(p, "\\")) != 0) {
541 out = out substr(p, 1, bs_idx - 1)
542 esc = substr(p, bs_idx + 1, 1)
543 p = substr(p, bs_idx + 2)
545 if ((esc_idx = index("abtvfr\"\\", esc)) != 0) {
546 # C-style one-character escape sequence.
547 out = out substr("\a\b\t\v\f\r\"\\",
548 esc_idx, 1)
549 } else if (esc == "n") {
550 # Uh-oh, a newline character.
551 # We cant reliably put a pathname
552 # containing a newline into COMPREPLY,
553 # and the newline would create a mess.
554 # Skip this path.
555 return ""
556 } else {
557 # Must be a \nnn octal value, then.
558 dec = esc * 64 + \
559 substr(p, 1, 1) * 8 + \
560 substr(p, 2, 1)
561 out = out sprintf("%c", dec)
562 p = substr(p, 3)
565 # Drop closing double quote, if there is one.
566 # (There isnt any if this is a directory, as it was
567 # already stripped with the trailing path components.)
568 if (substr(p, length(p), 1) == "\"")
569 out = out substr(p, 1, length(p) - 1)
570 else
571 out = out p
573 return out
577 # __git_complete_index_file requires 1 argument:
578 # 1: the options to pass to ls-file
580 # The exception is --committable, which finds the files appropriate commit.
581 __git_complete_index_file ()
583 local dequoted_word pfx="" cur_
585 __git_dequote "$cur"
587 case "$dequoted_word" in
588 ?*/*)
589 pfx="${dequoted_word%/*}/"
590 cur_="${dequoted_word##*/}"
593 cur_="$dequoted_word"
594 esac
596 __gitcomp_file_direct "$(__git_index_files "$1" "$pfx" "$cur_")"
599 # Lists branches from the local repository.
600 # 1: A prefix to be added to each listed branch (optional).
601 # 2: List only branches matching this word (optional; list all branches if
602 # unset or empty).
603 # 3: A suffix to be appended to each listed branch (optional).
604 __git_heads ()
606 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
608 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
609 "refs/heads/$cur_*" "refs/heads/$cur_*/**"
612 # Lists tags from the local repository.
613 # Accepts the same positional parameters as __git_heads() above.
614 __git_tags ()
616 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
618 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
619 "refs/tags/$cur_*" "refs/tags/$cur_*/**"
622 # Lists refs from the local (by default) or from a remote repository.
623 # It accepts 0, 1 or 2 arguments:
624 # 1: The remote to list refs from (optional; ignored, if set but empty).
625 # Can be the name of a configured remote, a path, or a URL.
626 # 2: In addition to local refs, list unique branches from refs/remotes/ for
627 # 'git checkout's tracking DWIMery (optional; ignored, if set but empty).
628 # 3: A prefix to be added to each listed ref (optional).
629 # 4: List only refs matching this word (optional; list all refs if unset or
630 # empty).
631 # 5: A suffix to be appended to each listed ref (optional; ignored, if set
632 # but empty).
634 # Use __git_complete_refs() instead.
635 __git_refs ()
637 local i hash dir track="${2-}"
638 local list_refs_from=path remote="${1-}"
639 local format refs
640 local pfx="${3-}" cur_="${4-$cur}" sfx="${5-}"
641 local match="${4-}"
642 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
644 __git_find_repo_path
645 dir="$__git_repo_path"
647 if [ -z "$remote" ]; then
648 if [ -z "$dir" ]; then
649 return
651 else
652 if __git_is_configured_remote "$remote"; then
653 # configured remote takes precedence over a
654 # local directory with the same name
655 list_refs_from=remote
656 elif [ -d "$remote/.git" ]; then
657 dir="$remote/.git"
658 elif [ -d "$remote" ]; then
659 dir="$remote"
660 else
661 list_refs_from=url
665 if [ "$list_refs_from" = path ]; then
666 if [[ "$cur_" == ^* ]]; then
667 pfx="$pfx^"
668 fer_pfx="$fer_pfx^"
669 cur_=${cur_#^}
670 match=${match#^}
672 case "$cur_" in
673 refs|refs/*)
674 format="refname"
675 refs=("$match*" "$match*/**")
676 track=""
679 for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD REBASE_HEAD; do
680 case "$i" in
681 $match*)
682 if [ -e "$dir/$i" ]; then
683 echo "$pfx$i$sfx"
686 esac
687 done
688 format="refname:strip=2"
689 refs=("refs/tags/$match*" "refs/tags/$match*/**"
690 "refs/heads/$match*" "refs/heads/$match*/**"
691 "refs/remotes/$match*" "refs/remotes/$match*/**")
693 esac
694 __git_dir="$dir" __git for-each-ref --format="$fer_pfx%($format)$sfx" \
695 "${refs[@]}"
696 if [ -n "$track" ]; then
697 # employ the heuristic used by git checkout
698 # Try to find a remote branch that matches the completion word
699 # but only output if the branch name is unique
700 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
701 --sort="refname:strip=3" \
702 "refs/remotes/*/$match*" "refs/remotes/*/$match*/**" | \
703 uniq -u
705 return
707 case "$cur_" in
708 refs|refs/*)
709 __git ls-remote "$remote" "$match*" | \
710 while read -r hash i; do
711 case "$i" in
712 *^{}) ;;
713 *) echo "$pfx$i$sfx" ;;
714 esac
715 done
718 if [ "$list_refs_from" = remote ]; then
719 case "HEAD" in
720 $match*) echo "${pfx}HEAD$sfx" ;;
721 esac
722 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
723 "refs/remotes/$remote/$match*" \
724 "refs/remotes/$remote/$match*/**"
725 else
726 local query_symref
727 case "HEAD" in
728 $match*) query_symref="HEAD" ;;
729 esac
730 __git ls-remote "$remote" $query_symref \
731 "refs/tags/$match*" "refs/heads/$match*" \
732 "refs/remotes/$match*" |
733 while read -r hash i; do
734 case "$i" in
735 *^{}) ;;
736 refs/*) echo "$pfx${i#refs/*/}$sfx" ;;
737 *) echo "$pfx$i$sfx" ;; # symbolic refs
738 esac
739 done
742 esac
745 # Completes refs, short and long, local and remote, symbolic and pseudo.
747 # Usage: __git_complete_refs [<option>]...
748 # --remote=<remote>: The remote to list refs from, can be the name of a
749 # configured remote, a path, or a URL.
750 # --track: List unique remote branches for 'git checkout's tracking DWIMery.
751 # --pfx=<prefix>: A prefix to be added to each ref.
752 # --cur=<word>: The current ref to be completed. Defaults to the current
753 # word to be completed.
754 # --sfx=<suffix>: A suffix to be appended to each ref instead of the default
755 # space.
756 __git_complete_refs ()
758 local remote track pfx cur_="$cur" sfx=" "
760 while test $# != 0; do
761 case "$1" in
762 --remote=*) remote="${1##--remote=}" ;;
763 --track) track="yes" ;;
764 --pfx=*) pfx="${1##--pfx=}" ;;
765 --cur=*) cur_="${1##--cur=}" ;;
766 --sfx=*) sfx="${1##--sfx=}" ;;
767 *) return 1 ;;
768 esac
769 shift
770 done
772 __gitcomp_direct "$(__git_refs "$remote" "$track" "$pfx" "$cur_" "$sfx")"
775 # __git_refs2 requires 1 argument (to pass to __git_refs)
776 # Deprecated: use __git_complete_fetch_refspecs() instead.
777 __git_refs2 ()
779 local i
780 for i in $(__git_refs "$1"); do
781 echo "$i:$i"
782 done
785 # Completes refspecs for fetching from a remote repository.
786 # 1: The remote repository.
787 # 2: A prefix to be added to each listed refspec (optional).
788 # 3: The ref to be completed as a refspec instead of the current word to be
789 # completed (optional)
790 # 4: A suffix to be appended to each listed refspec instead of the default
791 # space (optional).
792 __git_complete_fetch_refspecs ()
794 local i remote="$1" pfx="${2-}" cur_="${3-$cur}" sfx="${4- }"
796 __gitcomp_direct "$(
797 for i in $(__git_refs "$remote" "" "" "$cur_") ; do
798 echo "$pfx$i:$i$sfx"
799 done
803 # __git_refs_remotes requires 1 argument (to pass to ls-remote)
804 __git_refs_remotes ()
806 local i hash
807 __git ls-remote "$1" 'refs/heads/*' | \
808 while read -r hash i; do
809 echo "$i:refs/remotes/$1/${i#refs/heads/}"
810 done
813 __git_remotes ()
815 __git_find_repo_path
816 test -d "$__git_repo_path/remotes" && ls -1 "$__git_repo_path/remotes"
817 __git remote
820 # Returns true if $1 matches the name of a configured remote, false otherwise.
821 __git_is_configured_remote ()
823 local remote
824 for remote in $(__git_remotes); do
825 if [ "$remote" = "$1" ]; then
826 return 0
828 done
829 return 1
832 __git_list_merge_strategies ()
834 LANG=C LC_ALL=C git merge -s help 2>&1 |
835 sed -n -e '/[Aa]vailable strategies are: /,/^$/{
836 s/\.$//
837 s/.*://
838 s/^[ ]*//
839 s/[ ]*$//
844 __git_merge_strategies=
845 # 'git merge -s help' (and thus detection of the merge strategy
846 # list) fails, unfortunately, if run outside of any git working
847 # tree. __git_merge_strategies is set to the empty string in
848 # that case, and the detection will be repeated the next time it
849 # is needed.
850 __git_compute_merge_strategies ()
852 test -n "$__git_merge_strategies" ||
853 __git_merge_strategies=$(__git_list_merge_strategies)
856 __git_merge_strategy_options="ours theirs subtree subtree= patience
857 histogram diff-algorithm= ignore-space-change ignore-all-space
858 ignore-space-at-eol renormalize no-renormalize no-renames
859 find-renames find-renames= rename-threshold="
861 __git_complete_revlist_file ()
863 local dequoted_word pfx ls ref cur_="$cur"
864 case "$cur_" in
865 *..?*:*)
866 return
868 ?*:*)
869 ref="${cur_%%:*}"
870 cur_="${cur_#*:}"
872 __git_dequote "$cur_"
874 case "$dequoted_word" in
875 ?*/*)
876 pfx="${dequoted_word%/*}"
877 cur_="${dequoted_word##*/}"
878 ls="$ref:$pfx"
879 pfx="$pfx/"
882 cur_="$dequoted_word"
883 ls="$ref"
885 esac
887 case "$COMP_WORDBREAKS" in
888 *:*) : great ;;
889 *) pfx="$ref:$pfx" ;;
890 esac
892 __gitcomp_file "$(__git ls-tree "$ls" \
893 | sed 's/^.* //
894 s/$//')" \
895 "$pfx" "$cur_"
897 *...*)
898 pfx="${cur_%...*}..."
899 cur_="${cur_#*...}"
900 __git_complete_refs --pfx="$pfx" --cur="$cur_"
902 *..*)
903 pfx="${cur_%..*}.."
904 cur_="${cur_#*..}"
905 __git_complete_refs --pfx="$pfx" --cur="$cur_"
908 __git_complete_refs
910 esac
913 __git_complete_file ()
915 __git_complete_revlist_file
918 __git_complete_revlist ()
920 __git_complete_revlist_file
923 __git_complete_remote_or_refspec ()
925 local cur_="$cur" cmd="${words[1]}"
926 local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
927 if [ "$cmd" = "remote" ]; then
928 ((c++))
930 while [ $c -lt $cword ]; do
931 i="${words[c]}"
932 case "$i" in
933 --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
934 -d|--delete) [ "$cmd" = "push" ] && lhs=0 ;;
935 --all)
936 case "$cmd" in
937 push) no_complete_refspec=1 ;;
938 fetch)
939 return
941 *) ;;
942 esac
944 --multiple) no_complete_refspec=1; break ;;
945 -*) ;;
946 *) remote="$i"; break ;;
947 esac
948 ((c++))
949 done
950 if [ -z "$remote" ]; then
951 __gitcomp_nl "$(__git_remotes)"
952 return
954 if [ $no_complete_refspec = 1 ]; then
955 return
957 [ "$remote" = "." ] && remote=
958 case "$cur_" in
959 *:*)
960 case "$COMP_WORDBREAKS" in
961 *:*) : great ;;
962 *) pfx="${cur_%%:*}:" ;;
963 esac
964 cur_="${cur_#*:}"
965 lhs=0
968 pfx="+"
969 cur_="${cur_#+}"
971 esac
972 case "$cmd" in
973 fetch)
974 if [ $lhs = 1 ]; then
975 __git_complete_fetch_refspecs "$remote" "$pfx" "$cur_"
976 else
977 __git_complete_refs --pfx="$pfx" --cur="$cur_"
980 pull|remote)
981 if [ $lhs = 1 ]; then
982 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
983 else
984 __git_complete_refs --pfx="$pfx" --cur="$cur_"
987 push)
988 if [ $lhs = 1 ]; then
989 __git_complete_refs --pfx="$pfx" --cur="$cur_"
990 else
991 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
994 esac
997 __git_complete_strategy ()
999 __git_compute_merge_strategies
1000 case "$prev" in
1001 -s|--strategy)
1002 __gitcomp "$__git_merge_strategies"
1003 return 0
1006 __gitcomp "$__git_merge_strategy_options"
1007 return 0
1009 esac
1010 case "$cur" in
1011 --strategy=*)
1012 __gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
1013 return 0
1015 --strategy-option=*)
1016 __gitcomp "$__git_merge_strategy_options" "" "${cur##--strategy-option=}"
1017 return 0
1019 esac
1020 return 1
1023 __git_all_commands=
1024 __git_compute_all_commands ()
1026 test -n "$__git_all_commands" ||
1027 __git_all_commands=$(__git --list-cmds=main,others,alias,nohelpers)
1030 # Lists all set config variables starting with the given section prefix,
1031 # with the prefix removed.
1032 __git_get_config_variables ()
1034 local section="$1" i IFS=$'\n'
1035 for i in $(__git config --name-only --get-regexp "^$section\..*"); do
1036 echo "${i#$section.}"
1037 done
1040 __git_pretty_aliases ()
1042 __git_get_config_variables "pretty"
1045 # __git_aliased_command requires 1 argument
1046 __git_aliased_command ()
1048 local word cmdline=$(__git config --get "alias.$1")
1049 for word in $cmdline; do
1050 case "$word" in
1051 \!gitk|gitk)
1052 echo "gitk"
1053 return
1055 \!*) : shell command alias ;;
1056 -*) : option ;;
1057 *=*) : setting env ;;
1058 git) : git itself ;;
1059 \(\)) : skip parens of shell function definition ;;
1060 {) : skip start of shell helper function ;;
1061 :) : skip null command ;;
1062 \'*) : skip opening quote after sh -c ;;
1064 echo "$word"
1065 return
1066 esac
1067 done
1070 # __git_find_on_cmdline requires 1 argument
1071 __git_find_on_cmdline ()
1073 local word subcommand c=1
1074 while [ $c -lt $cword ]; do
1075 word="${words[c]}"
1076 for subcommand in $1; do
1077 if [ "$subcommand" = "$word" ]; then
1078 echo "$subcommand"
1079 return
1081 done
1082 ((c++))
1083 done
1086 # Echo the value of an option set on the command line or config
1088 # $1: short option name
1089 # $2: long option name including =
1090 # $3: list of possible values
1091 # $4: config string (optional)
1093 # example:
1094 # result="$(__git_get_option_value "-d" "--do-something=" \
1095 # "yes no" "core.doSomething")"
1097 # result is then either empty (no option set) or "yes" or "no"
1099 # __git_get_option_value requires 3 arguments
1100 __git_get_option_value ()
1102 local c short_opt long_opt val
1103 local result= values config_key word
1105 short_opt="$1"
1106 long_opt="$2"
1107 values="$3"
1108 config_key="$4"
1110 ((c = $cword - 1))
1111 while [ $c -ge 0 ]; do
1112 word="${words[c]}"
1113 for val in $values; do
1114 if [ "$short_opt$val" = "$word" ] ||
1115 [ "$long_opt$val" = "$word" ]; then
1116 result="$val"
1117 break 2
1119 done
1120 ((c--))
1121 done
1123 if [ -n "$config_key" ] && [ -z "$result" ]; then
1124 result="$(__git config "$config_key")"
1127 echo "$result"
1130 __git_has_doubledash ()
1132 local c=1
1133 while [ $c -lt $cword ]; do
1134 if [ "--" = "${words[c]}" ]; then
1135 return 0
1137 ((c++))
1138 done
1139 return 1
1142 # Try to count non option arguments passed on the command line for the
1143 # specified git command.
1144 # When options are used, it is necessary to use the special -- option to
1145 # tell the implementation were non option arguments begin.
1146 # XXX this can not be improved, since options can appear everywhere, as
1147 # an example:
1148 # git mv x -n y
1150 # __git_count_arguments requires 1 argument: the git command executed.
1151 __git_count_arguments ()
1153 local word i c=0
1155 # Skip "git" (first argument)
1156 for ((i=1; i < ${#words[@]}; i++)); do
1157 word="${words[i]}"
1159 case "$word" in
1161 # Good; we can assume that the following are only non
1162 # option arguments.
1163 ((c = 0))
1165 "$1")
1166 # Skip the specified git command and discard git
1167 # main options
1168 ((c = 0))
1171 ((c++))
1173 esac
1174 done
1176 printf "%d" $c
1179 __git_whitespacelist="nowarn warn error error-all fix"
1180 __git_patchformat="mbox stgit stgit-series hg mboxrd"
1181 __git_am_inprogress_options="--skip --continue --resolved --abort --quit --show-current-patch"
1183 _git_am ()
1185 __git_find_repo_path
1186 if [ -d "$__git_repo_path"/rebase-apply ]; then
1187 __gitcomp "$__git_am_inprogress_options"
1188 return
1190 case "$cur" in
1191 --whitespace=*)
1192 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1193 return
1195 --patch-format=*)
1196 __gitcomp "$__git_patchformat" "" "${cur##--patch-format=}"
1197 return
1199 --*)
1200 __gitcomp_builtin am "" \
1201 "$__git_am_inprogress_options"
1202 return
1203 esac
1206 _git_apply ()
1208 case "$cur" in
1209 --whitespace=*)
1210 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1211 return
1213 --*)
1214 __gitcomp_builtin apply
1215 return
1216 esac
1219 _git_add ()
1221 case "$cur" in
1222 --chmod=*)
1223 __gitcomp "+x -x" "" "${cur##--chmod=}"
1224 return
1226 --*)
1227 __gitcomp_builtin add
1228 return
1229 esac
1231 local complete_opt="--others --modified --directory --no-empty-directory"
1232 if test -n "$(__git_find_on_cmdline "-u --update")"
1233 then
1234 complete_opt="--modified"
1236 __git_complete_index_file "$complete_opt"
1239 _git_archive ()
1241 case "$cur" in
1242 --format=*)
1243 __gitcomp "$(git archive --list)" "" "${cur##--format=}"
1244 return
1246 --remote=*)
1247 __gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
1248 return
1250 --*)
1251 __gitcomp "
1252 --format= --list --verbose
1253 --prefix= --remote= --exec= --output
1255 return
1257 esac
1258 __git_complete_file
1261 _git_bisect ()
1263 __git_has_doubledash && return
1265 local subcommands="start bad good skip reset visualize replay log run"
1266 local subcommand="$(__git_find_on_cmdline "$subcommands")"
1267 if [ -z "$subcommand" ]; then
1268 __git_find_repo_path
1269 if [ -f "$__git_repo_path"/BISECT_START ]; then
1270 __gitcomp "$subcommands"
1271 else
1272 __gitcomp "replay start"
1274 return
1277 case "$subcommand" in
1278 bad|good|reset|skip|start)
1279 __git_complete_refs
1283 esac
1286 __git_ref_fieldlist="refname objecttype objectsize objectname upstream push HEAD symref"
1288 _git_branch ()
1290 local i c=1 only_local_ref="n" has_r="n"
1292 while [ $c -lt $cword ]; do
1293 i="${words[c]}"
1294 case "$i" in
1295 -d|--delete|-m|--move) only_local_ref="y" ;;
1296 -r|--remotes) has_r="y" ;;
1297 esac
1298 ((c++))
1299 done
1301 case "$cur" in
1302 --set-upstream-to=*)
1303 __git_complete_refs --cur="${cur##--set-upstream-to=}"
1305 --*)
1306 __gitcomp_builtin branch
1309 if [ $only_local_ref = "y" -a $has_r = "n" ]; then
1310 __gitcomp_direct "$(__git_heads "" "$cur" " ")"
1311 else
1312 __git_complete_refs
1315 esac
1318 _git_bundle ()
1320 local cmd="${words[2]}"
1321 case "$cword" in
1323 __gitcomp "create list-heads verify unbundle"
1326 # looking for a file
1329 case "$cmd" in
1330 create)
1331 __git_complete_revlist
1333 esac
1335 esac
1338 _git_checkout ()
1340 __git_has_doubledash && return
1342 case "$cur" in
1343 --conflict=*)
1344 __gitcomp "diff3 merge" "" "${cur##--conflict=}"
1346 --*)
1347 __gitcomp_builtin checkout
1350 # check if --track, --no-track, or --no-guess was specified
1351 # if so, disable DWIM mode
1352 local flags="--track --no-track --no-guess" track_opt="--track"
1353 if [ "$GIT_COMPLETION_CHECKOUT_NO_GUESS" = "1" ] ||
1354 [ -n "$(__git_find_on_cmdline "$flags")" ]; then
1355 track_opt=''
1357 __git_complete_refs $track_opt
1359 esac
1362 __git_cherry_pick_inprogress_options="--continue --quit --abort"
1364 _git_cherry_pick ()
1366 __git_find_repo_path
1367 if [ -f "$__git_repo_path"/CHERRY_PICK_HEAD ]; then
1368 __gitcomp "$__git_cherry_pick_inprogress_options"
1369 return
1372 __git_complete_strategy && return
1374 case "$cur" in
1375 --*)
1376 __gitcomp_builtin cherry-pick "" \
1377 "$__git_cherry_pick_inprogress_options"
1380 __git_complete_refs
1382 esac
1385 _git_clean ()
1387 case "$cur" in
1388 --*)
1389 __gitcomp_builtin clean
1390 return
1392 esac
1394 # XXX should we check for -x option ?
1395 __git_complete_index_file "--others --directory"
1398 _git_clone ()
1400 case "$cur" in
1401 --*)
1402 __gitcomp_builtin clone
1403 return
1405 esac
1408 __git_untracked_file_modes="all no normal"
1410 _git_commit ()
1412 case "$prev" in
1413 -c|-C)
1414 __git_complete_refs
1415 return
1417 esac
1419 case "$cur" in
1420 --cleanup=*)
1421 __gitcomp "default scissors strip verbatim whitespace
1422 " "" "${cur##--cleanup=}"
1423 return
1425 --reuse-message=*|--reedit-message=*|\
1426 --fixup=*|--squash=*)
1427 __git_complete_refs --cur="${cur#*=}"
1428 return
1430 --untracked-files=*)
1431 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
1432 return
1434 --*)
1435 __gitcomp_builtin commit
1436 return
1437 esac
1439 if __git rev-parse --verify --quiet HEAD >/dev/null; then
1440 __git_complete_index_file "--committable"
1441 else
1442 # This is the first commit
1443 __git_complete_index_file "--cached"
1447 _git_describe ()
1449 case "$cur" in
1450 --*)
1451 __gitcomp_builtin describe
1452 return
1453 esac
1454 __git_complete_refs
1457 __git_diff_algorithms="myers minimal patience histogram"
1459 __git_diff_submodule_formats="diff log short"
1461 __git_diff_common_options="--stat --numstat --shortstat --summary
1462 --patch-with-stat --name-only --name-status --color
1463 --no-color --color-words --no-renames --check
1464 --full-index --binary --abbrev --diff-filter=
1465 --find-copies-harder --ignore-cr-at-eol
1466 --text --ignore-space-at-eol --ignore-space-change
1467 --ignore-all-space --ignore-blank-lines --exit-code
1468 --quiet --ext-diff --no-ext-diff
1469 --no-prefix --src-prefix= --dst-prefix=
1470 --inter-hunk-context=
1471 --patience --histogram --minimal
1472 --raw --word-diff --word-diff-regex=
1473 --dirstat --dirstat= --dirstat-by-file
1474 --dirstat-by-file= --cumulative
1475 --diff-algorithm=
1476 --submodule --submodule= --ignore-submodules
1479 _git_diff ()
1481 __git_has_doubledash && return
1483 case "$cur" in
1484 --diff-algorithm=*)
1485 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1486 return
1488 --submodule=*)
1489 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1490 return
1492 --*)
1493 __gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
1494 --base --ours --theirs --no-index
1495 $__git_diff_common_options
1497 return
1499 esac
1500 __git_complete_revlist_file
1503 __git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff
1504 tkdiff vimdiff gvimdiff xxdiff araxis p4merge bc codecompare
1507 _git_difftool ()
1509 __git_has_doubledash && return
1511 case "$cur" in
1512 --tool=*)
1513 __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1514 return
1516 --*)
1517 __gitcomp_builtin difftool "$__git_diff_common_options
1518 --base --cached --ours --theirs
1519 --pickaxe-all --pickaxe-regex
1520 --relative --staged
1522 return
1524 esac
1525 __git_complete_revlist_file
1528 __git_fetch_recurse_submodules="yes on-demand no"
1530 _git_fetch ()
1532 case "$cur" in
1533 --recurse-submodules=*)
1534 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1535 return
1537 --filter=*)
1538 __gitcomp "blob:none blob:limit= sparse:oid= sparse:path=" "" "${cur##--filter=}"
1539 return
1541 --*)
1542 __gitcomp_builtin fetch
1543 return
1545 esac
1546 __git_complete_remote_or_refspec
1549 __git_format_patch_extra_options="
1550 --full-index --not --all --no-prefix --src-prefix=
1551 --dst-prefix= --notes
1554 _git_format_patch ()
1556 case "$cur" in
1557 --thread=*)
1558 __gitcomp "
1559 deep shallow
1560 " "" "${cur##--thread=}"
1561 return
1563 --*)
1564 __gitcomp_builtin format-patch "$__git_format_patch_extra_options"
1565 return
1567 esac
1568 __git_complete_revlist
1571 _git_fsck ()
1573 case "$cur" in
1574 --*)
1575 __gitcomp_builtin fsck
1576 return
1578 esac
1581 _git_gitk ()
1583 _gitk
1586 # Lists matching symbol names from a tag (as in ctags) file.
1587 # 1: List symbol names matching this word.
1588 # 2: The tag file to list symbol names from.
1589 # 3: A prefix to be added to each listed symbol name (optional).
1590 # 4: A suffix to be appended to each listed symbol name (optional).
1591 __git_match_ctag () {
1592 awk -v pfx="${3-}" -v sfx="${4-}" "
1593 /^${1//\//\\/}/ { print pfx \$1 sfx }
1594 " "$2"
1597 # Complete symbol names from a tag file.
1598 # Usage: __git_complete_symbol [<option>]...
1599 # --tags=<file>: The tag file to list symbol names from instead of the
1600 # default "tags".
1601 # --pfx=<prefix>: A prefix to be added to each symbol name.
1602 # --cur=<word>: The current symbol name to be completed. Defaults to
1603 # the current word to be completed.
1604 # --sfx=<suffix>: A suffix to be appended to each symbol name instead
1605 # of the default space.
1606 __git_complete_symbol () {
1607 local tags=tags pfx="" cur_="${cur-}" sfx=" "
1609 while test $# != 0; do
1610 case "$1" in
1611 --tags=*) tags="${1##--tags=}" ;;
1612 --pfx=*) pfx="${1##--pfx=}" ;;
1613 --cur=*) cur_="${1##--cur=}" ;;
1614 --sfx=*) sfx="${1##--sfx=}" ;;
1615 *) return 1 ;;
1616 esac
1617 shift
1618 done
1620 if test -r "$tags"; then
1621 __gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
1625 _git_grep ()
1627 __git_has_doubledash && return
1629 case "$cur" in
1630 --*)
1631 __gitcomp_builtin grep
1632 return
1634 esac
1636 case "$cword,$prev" in
1637 2,*|*,-*)
1638 __git_complete_symbol && return
1640 esac
1642 __git_complete_refs
1645 _git_help ()
1647 case "$cur" in
1648 --*)
1649 __gitcomp_builtin help
1650 return
1652 esac
1653 if test -n "$GIT_TESTING_ALL_COMMAND_LIST"
1654 then
1655 __gitcomp "$GIT_TESTING_ALL_COMMAND_LIST $(__git --list-cmds=alias,list-guide) gitk"
1656 else
1657 __gitcomp "$(__git --list-cmds=main,nohelpers,alias,list-guide) gitk"
1661 _git_init ()
1663 case "$cur" in
1664 --shared=*)
1665 __gitcomp "
1666 false true umask group all world everybody
1667 " "" "${cur##--shared=}"
1668 return
1670 --*)
1671 __gitcomp_builtin init
1672 return
1674 esac
1677 _git_ls_files ()
1679 case "$cur" in
1680 --*)
1681 __gitcomp_builtin ls-files
1682 return
1684 esac
1686 # XXX ignore options like --modified and always suggest all cached
1687 # files.
1688 __git_complete_index_file "--cached"
1691 _git_ls_remote ()
1693 case "$cur" in
1694 --*)
1695 __gitcomp_builtin ls-remote
1696 return
1698 esac
1699 __gitcomp_nl "$(__git_remotes)"
1702 _git_ls_tree ()
1704 case "$cur" in
1705 --*)
1706 __gitcomp_builtin ls-tree
1707 return
1709 esac
1711 __git_complete_file
1714 # Options that go well for log, shortlog and gitk
1715 __git_log_common_options="
1716 --not --all
1717 --branches --tags --remotes
1718 --first-parent --merges --no-merges
1719 --max-count=
1720 --max-age= --since= --after=
1721 --min-age= --until= --before=
1722 --min-parents= --max-parents=
1723 --no-min-parents --no-max-parents
1725 # Options that go well for log and gitk (not shortlog)
1726 __git_log_gitk_options="
1727 --dense --sparse --full-history
1728 --simplify-merges --simplify-by-decoration
1729 --left-right --notes --no-notes
1731 # Options that go well for log and shortlog (not gitk)
1732 __git_log_shortlog_options="
1733 --author= --committer= --grep=
1734 --all-match --invert-grep
1737 __git_log_pretty_formats="oneline short medium full fuller email raw format: mboxrd"
1738 __git_log_date_formats="relative iso8601 iso8601-strict rfc2822 short local default raw unix format:"
1740 _git_log ()
1742 __git_has_doubledash && return
1743 __git_find_repo_path
1745 local merge=""
1746 if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
1747 merge="--merge"
1749 case "$prev,$cur" in
1750 -L,:*:*)
1751 return # fall back to Bash filename completion
1753 -L,:*)
1754 __git_complete_symbol --cur="${cur#:}" --sfx=":"
1755 return
1757 -G,*|-S,*)
1758 __git_complete_symbol
1759 return
1761 esac
1762 case "$cur" in
1763 --pretty=*|--format=*)
1764 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
1765 " "" "${cur#*=}"
1766 return
1768 --date=*)
1769 __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
1770 return
1772 --decorate=*)
1773 __gitcomp "full short no" "" "${cur##--decorate=}"
1774 return
1776 --diff-algorithm=*)
1777 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1778 return
1780 --submodule=*)
1781 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1782 return
1784 --*)
1785 __gitcomp "
1786 $__git_log_common_options
1787 $__git_log_shortlog_options
1788 $__git_log_gitk_options
1789 --root --topo-order --date-order --reverse
1790 --follow --full-diff
1791 --abbrev-commit --abbrev=
1792 --relative-date --date=
1793 --pretty= --format= --oneline
1794 --show-signature
1795 --cherry-mark
1796 --cherry-pick
1797 --graph
1798 --decorate --decorate=
1799 --walk-reflogs
1800 --parents --children
1801 $merge
1802 $__git_diff_common_options
1803 --pickaxe-all --pickaxe-regex
1805 return
1807 -L:*:*)
1808 return # fall back to Bash filename completion
1810 -L:*)
1811 __git_complete_symbol --cur="${cur#-L:}" --sfx=":"
1812 return
1814 -G*)
1815 __git_complete_symbol --pfx="-G" --cur="${cur#-G}"
1816 return
1818 -S*)
1819 __git_complete_symbol --pfx="-S" --cur="${cur#-S}"
1820 return
1822 esac
1823 __git_complete_revlist
1826 _git_merge ()
1828 __git_complete_strategy && return
1830 case "$cur" in
1831 --*)
1832 __gitcomp_builtin merge
1833 return
1834 esac
1835 __git_complete_refs
1838 _git_mergetool ()
1840 case "$cur" in
1841 --tool=*)
1842 __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
1843 return
1845 --*)
1846 __gitcomp "--tool= --prompt --no-prompt --gui --no-gui"
1847 return
1849 esac
1852 _git_merge_base ()
1854 case "$cur" in
1855 --*)
1856 __gitcomp_builtin merge-base
1857 return
1859 esac
1860 __git_complete_refs
1863 _git_mv ()
1865 case "$cur" in
1866 --*)
1867 __gitcomp_builtin mv
1868 return
1870 esac
1872 if [ $(__git_count_arguments "mv") -gt 0 ]; then
1873 # We need to show both cached and untracked files (including
1874 # empty directories) since this may not be the last argument.
1875 __git_complete_index_file "--cached --others --directory"
1876 else
1877 __git_complete_index_file "--cached"
1881 _git_notes ()
1883 local subcommands='add append copy edit get-ref list merge prune remove show'
1884 local subcommand="$(__git_find_on_cmdline "$subcommands")"
1886 case "$subcommand,$cur" in
1887 ,--*)
1888 __gitcomp_builtin notes
1891 case "$prev" in
1892 --ref)
1893 __git_complete_refs
1896 __gitcomp "$subcommands --ref"
1898 esac
1900 *,--reuse-message=*|*,--reedit-message=*)
1901 __git_complete_refs --cur="${cur#*=}"
1903 *,--*)
1904 __gitcomp_builtin notes_$subcommand
1906 prune,*|get-ref,*)
1907 # this command does not take a ref, do not complete it
1910 case "$prev" in
1911 -m|-F)
1914 __git_complete_refs
1916 esac
1918 esac
1921 _git_pull ()
1923 __git_complete_strategy && return
1925 case "$cur" in
1926 --recurse-submodules=*)
1927 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1928 return
1930 --*)
1931 __gitcomp_builtin pull
1933 return
1935 esac
1936 __git_complete_remote_or_refspec
1939 __git_push_recurse_submodules="check on-demand only"
1941 __git_complete_force_with_lease ()
1943 local cur_=$1
1945 case "$cur_" in
1946 --*=)
1948 *:*)
1949 __git_complete_refs --cur="${cur_#*:}"
1952 __git_complete_refs --cur="$cur_"
1954 esac
1957 _git_push ()
1959 case "$prev" in
1960 --repo)
1961 __gitcomp_nl "$(__git_remotes)"
1962 return
1964 --recurse-submodules)
1965 __gitcomp "$__git_push_recurse_submodules"
1966 return
1968 esac
1969 case "$cur" in
1970 --repo=*)
1971 __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
1972 return
1974 --recurse-submodules=*)
1975 __gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
1976 return
1978 --force-with-lease=*)
1979 __git_complete_force_with_lease "${cur##--force-with-lease=}"
1980 return
1982 --*)
1983 __gitcomp_builtin push
1984 return
1986 esac
1987 __git_complete_remote_or_refspec
1990 _git_range_diff ()
1992 case "$cur" in
1993 --*)
1994 __gitcomp "
1995 --creation-factor= --no-dual-color
1996 $__git_diff_common_options
1998 return
2000 esac
2001 __git_complete_revlist
2004 _git_rebase ()
2006 __git_find_repo_path
2007 if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
2008 __gitcomp "--continue --skip --abort --quit --edit-todo --show-current-patch"
2009 return
2010 elif [ -d "$__git_repo_path"/rebase-apply ] || \
2011 [ -d "$__git_repo_path"/rebase-merge ]; then
2012 __gitcomp "--continue --skip --abort --quit --show-current-patch"
2013 return
2015 __git_complete_strategy && return
2016 case "$cur" in
2017 --whitespace=*)
2018 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
2019 return
2021 --*)
2022 __gitcomp "
2023 --onto --merge --strategy --interactive
2024 --rebase-merges --preserve-merges --stat --no-stat
2025 --committer-date-is-author-date --ignore-date
2026 --ignore-whitespace --whitespace=
2027 --autosquash --no-autosquash
2028 --fork-point --no-fork-point
2029 --autostash --no-autostash
2030 --verify --no-verify
2031 --keep-empty --root --force-rebase --no-ff
2032 --rerere-autoupdate
2033 --exec
2036 return
2037 esac
2038 __git_complete_refs
2041 _git_reflog ()
2043 local subcommands="show delete expire"
2044 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2046 if [ -z "$subcommand" ]; then
2047 __gitcomp "$subcommands"
2048 else
2049 __git_complete_refs
2053 __git_send_email_confirm_options="always never auto cc compose"
2054 __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
2056 _git_send_email ()
2058 case "$prev" in
2059 --to|--cc|--bcc|--from)
2060 __gitcomp "$(__git send-email --dump-aliases)"
2061 return
2063 esac
2065 case "$cur" in
2066 --confirm=*)
2067 __gitcomp "
2068 $__git_send_email_confirm_options
2069 " "" "${cur##--confirm=}"
2070 return
2072 --suppress-cc=*)
2073 __gitcomp "
2074 $__git_send_email_suppresscc_options
2075 " "" "${cur##--suppress-cc=}"
2077 return
2079 --smtp-encryption=*)
2080 __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
2081 return
2083 --thread=*)
2084 __gitcomp "
2085 deep shallow
2086 " "" "${cur##--thread=}"
2087 return
2089 --to=*|--cc=*|--bcc=*|--from=*)
2090 __gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
2091 return
2093 --*)
2094 __gitcomp_builtin send-email "--annotate --bcc --cc --cc-cmd --chain-reply-to
2095 --compose --confirm= --dry-run --envelope-sender
2096 --from --identity
2097 --in-reply-to --no-chain-reply-to --no-signed-off-by-cc
2098 --no-suppress-from --no-thread --quiet --reply-to
2099 --signed-off-by-cc --smtp-pass --smtp-server
2100 --smtp-server-port --smtp-encryption= --smtp-user
2101 --subject --suppress-cc= --suppress-from --thread --to
2102 --validate --no-validate
2103 $__git_format_patch_extra_options"
2104 return
2106 esac
2107 __git_complete_revlist
2110 _git_stage ()
2112 _git_add
2115 _git_status ()
2117 local complete_opt
2118 local untracked_state
2120 case "$cur" in
2121 --ignore-submodules=*)
2122 __gitcomp "none untracked dirty all" "" "${cur##--ignore-submodules=}"
2123 return
2125 --untracked-files=*)
2126 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
2127 return
2129 --column=*)
2130 __gitcomp "
2131 always never auto column row plain dense nodense
2132 " "" "${cur##--column=}"
2133 return
2135 --*)
2136 __gitcomp_builtin status
2137 return
2139 esac
2141 untracked_state="$(__git_get_option_value "-u" "--untracked-files=" \
2142 "$__git_untracked_file_modes" "status.showUntrackedFiles")"
2144 case "$untracked_state" in
2146 # --ignored option does not matter
2147 complete_opt=
2149 all|normal|*)
2150 complete_opt="--cached --directory --no-empty-directory --others"
2152 if [ -n "$(__git_find_on_cmdline "--ignored")" ]; then
2153 complete_opt="$complete_opt --ignored --exclude=*"
2156 esac
2158 __git_complete_index_file "$complete_opt"
2161 __git_config_get_set_variables ()
2163 local prevword word config_file= c=$cword
2164 while [ $c -gt 1 ]; do
2165 word="${words[c]}"
2166 case "$word" in
2167 --system|--global|--local|--file=*)
2168 config_file="$word"
2169 break
2171 -f|--file)
2172 config_file="$word $prevword"
2173 break
2175 esac
2176 prevword=$word
2177 c=$((--c))
2178 done
2180 __git config $config_file --name-only --list
2183 __git_config_vars=
2184 __git_compute_config_vars ()
2186 test -n "$__git_config_vars" ||
2187 __git_config_vars="$(git help --config-for-completion | sort | uniq)"
2190 _git_config ()
2192 local varname
2194 if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
2195 varname="${prev,,}"
2196 else
2197 varname="$(echo "$prev" |tr A-Z a-z)"
2200 case "$varname" in
2201 branch.*.remote|branch.*.pushremote)
2202 __gitcomp_nl "$(__git_remotes)"
2203 return
2205 branch.*.merge)
2206 __git_complete_refs
2207 return
2209 branch.*.rebase)
2210 __gitcomp "false true merges preserve interactive"
2211 return
2213 remote.pushdefault)
2214 __gitcomp_nl "$(__git_remotes)"
2215 return
2217 remote.*.fetch)
2218 local remote="${prev#remote.}"
2219 remote="${remote%.fetch}"
2220 if [ -z "$cur" ]; then
2221 __gitcomp_nl "refs/heads/" "" "" ""
2222 return
2224 __gitcomp_nl "$(__git_refs_remotes "$remote")"
2225 return
2227 remote.*.push)
2228 local remote="${prev#remote.}"
2229 remote="${remote%.push}"
2230 __gitcomp_nl "$(__git for-each-ref \
2231 --format='%(refname):%(refname)' refs/heads)"
2232 return
2234 pull.twohead|pull.octopus)
2235 __git_compute_merge_strategies
2236 __gitcomp "$__git_merge_strategies"
2237 return
2239 color.branch|color.diff|color.interactive|\
2240 color.showbranch|color.status|color.ui)
2241 __gitcomp "always never auto"
2242 return
2244 color.pager)
2245 __gitcomp "false true"
2246 return
2248 color.*.*)
2249 __gitcomp "
2250 normal black red green yellow blue magenta cyan white
2251 bold dim ul blink reverse
2253 return
2255 diff.submodule)
2256 __gitcomp "$__git_diff_submodule_formats"
2257 return
2259 help.format)
2260 __gitcomp "man info web html"
2261 return
2263 log.date)
2264 __gitcomp "$__git_log_date_formats"
2265 return
2267 sendemail.aliasfiletype)
2268 __gitcomp "mutt mailrc pine elm gnus"
2269 return
2271 sendemail.confirm)
2272 __gitcomp "$__git_send_email_confirm_options"
2273 return
2275 sendemail.suppresscc)
2276 __gitcomp "$__git_send_email_suppresscc_options"
2277 return
2279 sendemail.transferencoding)
2280 __gitcomp "7bit 8bit quoted-printable base64"
2281 return
2283 --get|--get-all|--unset|--unset-all)
2284 __gitcomp_nl "$(__git_config_get_set_variables)"
2285 return
2287 *.*)
2288 return
2290 esac
2291 case "$cur" in
2292 --*)
2293 __gitcomp_builtin config
2294 return
2296 branch.*.*)
2297 local pfx="${cur%.*}." cur_="${cur##*.}"
2298 __gitcomp "remote pushRemote merge mergeOptions rebase" "$pfx" "$cur_"
2299 return
2301 branch.*)
2302 local pfx="${cur%.*}." cur_="${cur#*.}"
2303 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
2304 __gitcomp_nl_append $'autoSetupMerge\nautoSetupRebase\n' "$pfx" "$cur_"
2305 return
2307 guitool.*.*)
2308 local pfx="${cur%.*}." cur_="${cur##*.}"
2309 __gitcomp "
2310 argPrompt cmd confirm needsFile noConsole noRescan
2311 prompt revPrompt revUnmerged title
2312 " "$pfx" "$cur_"
2313 return
2315 difftool.*.*)
2316 local pfx="${cur%.*}." cur_="${cur##*.}"
2317 __gitcomp "cmd path" "$pfx" "$cur_"
2318 return
2320 man.*.*)
2321 local pfx="${cur%.*}." cur_="${cur##*.}"
2322 __gitcomp "cmd path" "$pfx" "$cur_"
2323 return
2325 mergetool.*.*)
2326 local pfx="${cur%.*}." cur_="${cur##*.}"
2327 __gitcomp "cmd path trustExitCode" "$pfx" "$cur_"
2328 return
2330 pager.*)
2331 local pfx="${cur%.*}." cur_="${cur#*.}"
2332 __git_compute_all_commands
2333 __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_"
2334 return
2336 remote.*.*)
2337 local pfx="${cur%.*}." cur_="${cur##*.}"
2338 __gitcomp "
2339 url proxy fetch push mirror skipDefaultUpdate
2340 receivepack uploadpack tagOpt pushurl
2341 " "$pfx" "$cur_"
2342 return
2344 remote.*)
2345 local pfx="${cur%.*}." cur_="${cur#*.}"
2346 __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
2347 __gitcomp_nl_append "pushDefault" "$pfx" "$cur_"
2348 return
2350 url.*.*)
2351 local pfx="${cur%.*}." cur_="${cur##*.}"
2352 __gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_"
2353 return
2355 *.*)
2356 __git_compute_config_vars
2357 __gitcomp "$__git_config_vars"
2360 __git_compute_config_vars
2361 __gitcomp "$(echo "$__git_config_vars" | sed 's/\.[^ ]*/./g')"
2362 esac
2365 _git_remote ()
2367 local subcommands="
2368 add rename remove set-head set-branches
2369 get-url set-url show prune update
2371 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2372 if [ -z "$subcommand" ]; then
2373 case "$cur" in
2374 --*)
2375 __gitcomp_builtin remote
2378 __gitcomp "$subcommands"
2380 esac
2381 return
2384 case "$subcommand,$cur" in
2385 add,--*)
2386 __gitcomp_builtin remote_add
2388 add,*)
2390 set-head,--*)
2391 __gitcomp_builtin remote_set-head
2393 set-branches,--*)
2394 __gitcomp_builtin remote_set-branches
2396 set-head,*|set-branches,*)
2397 __git_complete_remote_or_refspec
2399 update,--*)
2400 __gitcomp_builtin remote_update
2402 update,*)
2403 __gitcomp "$(__git_remotes) $(__git_get_config_variables "remotes")"
2405 set-url,--*)
2406 __gitcomp_builtin remote_set-url
2408 get-url,--*)
2409 __gitcomp_builtin remote_get-url
2411 prune,--*)
2412 __gitcomp_builtin remote_prune
2415 __gitcomp_nl "$(__git_remotes)"
2417 esac
2420 _git_replace ()
2422 case "$cur" in
2423 --format=*)
2424 __gitcomp "short medium long" "" "${cur##--format=}"
2425 return
2427 --*)
2428 __gitcomp_builtin replace
2429 return
2431 esac
2432 __git_complete_refs
2435 _git_rerere ()
2437 local subcommands="clear forget diff remaining status gc"
2438 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2439 if test -z "$subcommand"
2440 then
2441 __gitcomp "$subcommands"
2442 return
2446 _git_reset ()
2448 __git_has_doubledash && return
2450 case "$cur" in
2451 --*)
2452 __gitcomp_builtin reset
2453 return
2455 esac
2456 __git_complete_refs
2459 __git_revert_inprogress_options="--continue --quit --abort"
2461 _git_revert ()
2463 __git_find_repo_path
2464 if [ -f "$__git_repo_path"/REVERT_HEAD ]; then
2465 __gitcomp "$__git_revert_inprogress_options"
2466 return
2468 __git_complete_strategy && return
2469 case "$cur" in
2470 --*)
2471 __gitcomp_builtin revert "" \
2472 "$__git_revert_inprogress_options"
2473 return
2475 esac
2476 __git_complete_refs
2479 _git_rm ()
2481 case "$cur" in
2482 --*)
2483 __gitcomp_builtin rm
2484 return
2486 esac
2488 __git_complete_index_file "--cached"
2491 _git_shortlog ()
2493 __git_has_doubledash && return
2495 case "$cur" in
2496 --*)
2497 __gitcomp "
2498 $__git_log_common_options
2499 $__git_log_shortlog_options
2500 --numbered --summary --email
2502 return
2504 esac
2505 __git_complete_revlist
2508 _git_show ()
2510 __git_has_doubledash && return
2512 case "$cur" in
2513 --pretty=*|--format=*)
2514 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2515 " "" "${cur#*=}"
2516 return
2518 --diff-algorithm=*)
2519 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
2520 return
2522 --submodule=*)
2523 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
2524 return
2526 --*)
2527 __gitcomp "--pretty= --format= --abbrev-commit --oneline
2528 --show-signature
2529 $__git_diff_common_options
2531 return
2533 esac
2534 __git_complete_revlist_file
2537 _git_show_branch ()
2539 case "$cur" in
2540 --*)
2541 __gitcomp_builtin show-branch
2542 return
2544 esac
2545 __git_complete_revlist
2548 _git_stash ()
2550 local save_opts='--all --keep-index --no-keep-index --quiet --patch --include-untracked'
2551 local subcommands='push list show apply clear drop pop create branch'
2552 local subcommand="$(__git_find_on_cmdline "$subcommands save")"
2553 if [ -n "$(__git_find_on_cmdline "-p")" ]; then
2554 subcommand="push"
2556 if [ -z "$subcommand" ]; then
2557 case "$cur" in
2558 --*)
2559 __gitcomp "$save_opts"
2561 sa*)
2562 if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
2563 __gitcomp "save"
2567 if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
2568 __gitcomp "$subcommands"
2571 esac
2572 else
2573 case "$subcommand,$cur" in
2574 push,--*)
2575 __gitcomp "$save_opts --message"
2577 save,--*)
2578 __gitcomp "$save_opts"
2580 apply,--*|pop,--*)
2581 __gitcomp "--index --quiet"
2583 drop,--*)
2584 __gitcomp "--quiet"
2586 list,--*)
2587 __gitcomp "--name-status --oneline --patch-with-stat"
2589 show,--*|branch,--*)
2591 branch,*)
2592 if [ $cword -eq 3 ]; then
2593 __git_complete_refs
2594 else
2595 __gitcomp_nl "$(__git stash list \
2596 | sed -n -e 's/:.*//p')"
2599 show,*|apply,*|drop,*|pop,*)
2600 __gitcomp_nl "$(__git stash list \
2601 | sed -n -e 's/:.*//p')"
2605 esac
2609 _git_submodule ()
2611 __git_has_doubledash && return
2613 local subcommands="add status init deinit update summary foreach sync absorbgitdirs"
2614 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2615 if [ -z "$subcommand" ]; then
2616 case "$cur" in
2617 --*)
2618 __gitcomp "--quiet"
2621 __gitcomp "$subcommands"
2623 esac
2624 return
2627 case "$subcommand,$cur" in
2628 add,--*)
2629 __gitcomp "--branch --force --name --reference --depth"
2631 status,--*)
2632 __gitcomp "--cached --recursive"
2634 deinit,--*)
2635 __gitcomp "--force --all"
2637 update,--*)
2638 __gitcomp "
2639 --init --remote --no-fetch
2640 --recommend-shallow --no-recommend-shallow
2641 --force --rebase --merge --reference --depth --recursive --jobs
2644 summary,--*)
2645 __gitcomp "--cached --files --summary-limit"
2647 foreach,--*|sync,--*)
2648 __gitcomp "--recursive"
2652 esac
2655 _git_svn ()
2657 local subcommands="
2658 init fetch clone rebase dcommit log find-rev
2659 set-tree commit-diff info create-ignore propget
2660 proplist show-ignore show-externals branch tag blame
2661 migrate mkdirs reset gc
2663 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2664 if [ -z "$subcommand" ]; then
2665 __gitcomp "$subcommands"
2666 else
2667 local remote_opts="--username= --config-dir= --no-auth-cache"
2668 local fc_opts="
2669 --follow-parent --authors-file= --repack=
2670 --no-metadata --use-svm-props --use-svnsync-props
2671 --log-window-size= --no-checkout --quiet
2672 --repack-flags --use-log-author --localtime
2673 --add-author-from
2674 --ignore-paths= --include-paths= $remote_opts
2676 local init_opts="
2677 --template= --shared= --trunk= --tags=
2678 --branches= --stdlayout --minimize-url
2679 --no-metadata --use-svm-props --use-svnsync-props
2680 --rewrite-root= --prefix= $remote_opts
2682 local cmt_opts="
2683 --edit --rmdir --find-copies-harder --copy-similarity=
2686 case "$subcommand,$cur" in
2687 fetch,--*)
2688 __gitcomp "--revision= --fetch-all $fc_opts"
2690 clone,--*)
2691 __gitcomp "--revision= $fc_opts $init_opts"
2693 init,--*)
2694 __gitcomp "$init_opts"
2696 dcommit,--*)
2697 __gitcomp "
2698 --merge --strategy= --verbose --dry-run
2699 --fetch-all --no-rebase --commit-url
2700 --revision --interactive $cmt_opts $fc_opts
2703 set-tree,--*)
2704 __gitcomp "--stdin $cmt_opts $fc_opts"
2706 create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
2707 show-externals,--*|mkdirs,--*)
2708 __gitcomp "--revision="
2710 log,--*)
2711 __gitcomp "
2712 --limit= --revision= --verbose --incremental
2713 --oneline --show-commit --non-recursive
2714 --authors-file= --color
2717 rebase,--*)
2718 __gitcomp "
2719 --merge --verbose --strategy= --local
2720 --fetch-all --dry-run $fc_opts
2723 commit-diff,--*)
2724 __gitcomp "--message= --file= --revision= $cmt_opts"
2726 info,--*)
2727 __gitcomp "--url"
2729 branch,--*)
2730 __gitcomp "--dry-run --message --tag"
2732 tag,--*)
2733 __gitcomp "--dry-run --message"
2735 blame,--*)
2736 __gitcomp "--git-format"
2738 migrate,--*)
2739 __gitcomp "
2740 --config-dir= --ignore-paths= --minimize
2741 --no-auth-cache --username=
2744 reset,--*)
2745 __gitcomp "--revision= --parent"
2749 esac
2753 _git_tag ()
2755 local i c=1 f=0
2756 while [ $c -lt $cword ]; do
2757 i="${words[c]}"
2758 case "$i" in
2759 -d|--delete|-v|--verify)
2760 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
2761 return
2766 esac
2767 ((c++))
2768 done
2770 case "$prev" in
2771 -m|-F)
2773 -*|tag)
2774 if [ $f = 1 ]; then
2775 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
2779 __git_complete_refs
2781 esac
2783 case "$cur" in
2784 --*)
2785 __gitcomp_builtin tag
2787 esac
2790 _git_whatchanged ()
2792 _git_log
2795 _git_worktree ()
2797 local subcommands="add list lock move prune remove unlock"
2798 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2799 if [ -z "$subcommand" ]; then
2800 __gitcomp "$subcommands"
2801 else
2802 case "$subcommand,$cur" in
2803 add,--*)
2804 __gitcomp_builtin worktree_add
2806 list,--*)
2807 __gitcomp_builtin worktree_list
2809 lock,--*)
2810 __gitcomp_builtin worktree_lock
2812 prune,--*)
2813 __gitcomp_builtin worktree_prune
2815 remove,--*)
2816 __gitcomp "--force"
2820 esac
2824 __git_complete_common () {
2825 local command="$1"
2827 case "$cur" in
2828 --*)
2829 __gitcomp_builtin "$command"
2831 esac
2834 __git_cmds_with_parseopt_helper=
2835 __git_support_parseopt_helper () {
2836 test -n "$__git_cmds_with_parseopt_helper" ||
2837 __git_cmds_with_parseopt_helper="$(__git --list-cmds=parseopt)"
2839 case " $__git_cmds_with_parseopt_helper " in
2840 *" $1 "*)
2841 return 0
2844 return 1
2846 esac
2849 __git_complete_command () {
2850 local command="$1"
2851 local completion_func="_git_${command//-/_}"
2852 if ! declare -f $completion_func >/dev/null 2>/dev/null &&
2853 declare -f _completion_loader >/dev/null 2>/dev/null
2854 then
2855 _completion_loader "git-$command"
2857 if declare -f $completion_func >/dev/null 2>/dev/null
2858 then
2859 $completion_func
2860 return 0
2861 elif __git_support_parseopt_helper "$command"
2862 then
2863 __git_complete_common "$command"
2864 return 0
2865 else
2866 return 1
2870 __git_main ()
2872 local i c=1 command __git_dir __git_repo_path
2873 local __git_C_args C_args_count=0
2875 while [ $c -lt $cword ]; do
2876 i="${words[c]}"
2877 case "$i" in
2878 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
2879 --git-dir) ((c++)) ; __git_dir="${words[c]}" ;;
2880 --bare) __git_dir="." ;;
2881 --help) command="help"; break ;;
2882 -c|--work-tree|--namespace) ((c++)) ;;
2883 -C) __git_C_args[C_args_count++]=-C
2884 ((c++))
2885 __git_C_args[C_args_count++]="${words[c]}"
2887 -*) ;;
2888 *) command="$i"; break ;;
2889 esac
2890 ((c++))
2891 done
2893 if [ -z "$command" ]; then
2894 case "$prev" in
2895 --git-dir|-C|--work-tree)
2896 # these need a path argument, let's fall back to
2897 # Bash filename completion
2898 return
2900 -c|--namespace)
2901 # we don't support completing these options' arguments
2902 return
2904 esac
2905 case "$cur" in
2906 --*) __gitcomp "
2907 --paginate
2908 --no-pager
2909 --git-dir=
2910 --bare
2911 --version
2912 --exec-path
2913 --exec-path=
2914 --html-path
2915 --man-path
2916 --info-path
2917 --work-tree=
2918 --namespace=
2919 --no-replace-objects
2920 --help
2924 if test -n "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
2925 then
2926 __gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
2927 else
2928 __gitcomp "$(__git --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config)"
2931 esac
2932 return
2935 __git_complete_command "$command" && return
2937 local expansion=$(__git_aliased_command "$command")
2938 if [ -n "$expansion" ]; then
2939 words[1]=$expansion
2940 __git_complete_command "$expansion"
2944 __gitk_main ()
2946 __git_has_doubledash && return
2948 local __git_repo_path
2949 __git_find_repo_path
2951 local merge=""
2952 if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
2953 merge="--merge"
2955 case "$cur" in
2956 --*)
2957 __gitcomp "
2958 $__git_log_common_options
2959 $__git_log_gitk_options
2960 $merge
2962 return
2964 esac
2965 __git_complete_revlist
2968 if [[ -n ${ZSH_VERSION-} ]] &&
2969 # Don't define these functions when sourced from 'git-completion.zsh',
2970 # it has its own implementations.
2971 [[ -z ${GIT_SOURCING_ZSH_COMPLETION-} ]]; then
2972 echo "WARNING: this script is deprecated, please see git-completion.zsh" 1>&2
2974 autoload -U +X compinit && compinit
2976 __gitcomp ()
2978 emulate -L zsh
2980 local cur_="${3-$cur}"
2982 case "$cur_" in
2983 --*=)
2986 local c IFS=$' \t\n'
2987 local -a array
2988 for c in ${=1}; do
2989 c="$c${4-}"
2990 case $c in
2991 --*=*|*.) ;;
2992 *) c="$c " ;;
2993 esac
2994 array[${#array[@]}+1]="$c"
2995 done
2996 compset -P '*[=:]'
2997 compadd -Q -S '' -p "${2-}" -a -- array && _ret=0
2999 esac
3002 __gitcomp_direct ()
3004 emulate -L zsh
3006 local IFS=$'\n'
3007 compset -P '*[=:]'
3008 compadd -Q -- ${=1} && _ret=0
3011 __gitcomp_nl ()
3013 emulate -L zsh
3015 local IFS=$'\n'
3016 compset -P '*[=:]'
3017 compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
3020 __gitcomp_file_direct ()
3022 emulate -L zsh
3024 local IFS=$'\n'
3025 compset -P '*[=:]'
3026 compadd -f -- ${=1} && _ret=0
3029 __gitcomp_file ()
3031 emulate -L zsh
3033 local IFS=$'\n'
3034 compset -P '*[=:]'
3035 compadd -p "${2-}" -f -- ${=1} && _ret=0
3038 _git ()
3040 local _ret=1 cur cword prev
3041 cur=${words[CURRENT]}
3042 prev=${words[CURRENT-1]}
3043 let cword=CURRENT-1
3044 emulate ksh -c __${service}_main
3045 let _ret && _default && _ret=0
3046 return _ret
3049 compdef _git git gitk
3050 return
3053 __git_func_wrap ()
3055 local cur words cword prev
3056 _get_comp_words_by_ref -n =: cur words cword prev
3060 # Setup completion for certain functions defined above by setting common
3061 # variables and workarounds.
3062 # This is NOT a public function; use at your own risk.
3063 __git_complete ()
3065 local wrapper="__git_wrap${2}"
3066 eval "$wrapper () { __git_func_wrap $2 ; }"
3067 complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
3068 || complete -o default -o nospace -F $wrapper $1
3071 # wrapper for backwards compatibility
3072 _git ()
3074 __git_wrap__git_main
3077 # wrapper for backwards compatibility
3078 _gitk ()
3080 __git_wrap__gitk_main
3083 __git_complete git __git_main
3084 __git_complete gitk __gitk_main
3086 # The following are necessary only for Cygwin, and only are needed
3087 # when the user has tab-completed the executable name and consequently
3088 # included the '.exe' suffix.
3090 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
3091 __git_complete git.exe __git_main