Merge branch 'du/cherry-is-plumbing'
[git.git] / contrib / completion / git-completion.bash
blob5f556075d3cbce74c60e81b97d4923a1ff18f78d
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=" $(__git ${cmd/_/ } --git-completion-helper) $incl "
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 componens.
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_complete_revlist_file ()
858 local pfx ls ref cur_="$cur"
859 case "$cur_" in
860 *..?*:*)
861 return
863 ?*:*)
864 ref="${cur_%%:*}"
865 cur_="${cur_#*:}"
866 case "$cur_" in
867 ?*/*)
868 pfx="${cur_%/*}"
869 cur_="${cur_##*/}"
870 ls="$ref:$pfx"
871 pfx="$pfx/"
874 ls="$ref"
876 esac
878 case "$COMP_WORDBREAKS" in
879 *:*) : great ;;
880 *) pfx="$ref:$pfx" ;;
881 esac
883 __gitcomp_nl "$(__git ls-tree "$ls" \
884 | sed '/^100... blob /{
885 s,^.* ,,
886 s,$, ,
888 /^120000 blob /{
889 s,^.* ,,
890 s,$, ,
892 /^040000 tree /{
893 s,^.* ,,
894 s,$,/,
896 s/^.* //')" \
897 "$pfx" "$cur_" ""
899 *...*)
900 pfx="${cur_%...*}..."
901 cur_="${cur_#*...}"
902 __git_complete_refs --pfx="$pfx" --cur="$cur_"
904 *..*)
905 pfx="${cur_%..*}.."
906 cur_="${cur_#*..}"
907 __git_complete_refs --pfx="$pfx" --cur="$cur_"
910 __git_complete_refs
912 esac
915 __git_complete_file ()
917 __git_complete_revlist_file
920 __git_complete_revlist ()
922 __git_complete_revlist_file
925 __git_complete_remote_or_refspec ()
927 local cur_="$cur" cmd="${words[1]}"
928 local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
929 if [ "$cmd" = "remote" ]; then
930 ((c++))
932 while [ $c -lt $cword ]; do
933 i="${words[c]}"
934 case "$i" in
935 --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
936 -d|--delete) [ "$cmd" = "push" ] && lhs=0 ;;
937 --all)
938 case "$cmd" in
939 push) no_complete_refspec=1 ;;
940 fetch)
941 return
943 *) ;;
944 esac
946 --multiple) no_complete_refspec=1; break ;;
947 -*) ;;
948 *) remote="$i"; break ;;
949 esac
950 ((c++))
951 done
952 if [ -z "$remote" ]; then
953 __gitcomp_nl "$(__git_remotes)"
954 return
956 if [ $no_complete_refspec = 1 ]; then
957 return
959 [ "$remote" = "." ] && remote=
960 case "$cur_" in
961 *:*)
962 case "$COMP_WORDBREAKS" in
963 *:*) : great ;;
964 *) pfx="${cur_%%:*}:" ;;
965 esac
966 cur_="${cur_#*:}"
967 lhs=0
970 pfx="+"
971 cur_="${cur_#+}"
973 esac
974 case "$cmd" in
975 fetch)
976 if [ $lhs = 1 ]; then
977 __git_complete_fetch_refspecs "$remote" "$pfx" "$cur_"
978 else
979 __git_complete_refs --pfx="$pfx" --cur="$cur_"
982 pull|remote)
983 if [ $lhs = 1 ]; then
984 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
985 else
986 __git_complete_refs --pfx="$pfx" --cur="$cur_"
989 push)
990 if [ $lhs = 1 ]; then
991 __git_complete_refs --pfx="$pfx" --cur="$cur_"
992 else
993 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
996 esac
999 __git_complete_strategy ()
1001 __git_compute_merge_strategies
1002 case "$prev" in
1003 -s|--strategy)
1004 __gitcomp "$__git_merge_strategies"
1005 return 0
1006 esac
1007 case "$cur" in
1008 --strategy=*)
1009 __gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
1010 return 0
1012 esac
1013 return 1
1016 __git_all_commands=
1017 __git_compute_all_commands ()
1019 test -n "$__git_all_commands" ||
1020 __git_all_commands=$(git --list-cmds=main,others,alias,nohelpers)
1023 # Lists all set config variables starting with the given section prefix,
1024 # with the prefix removed.
1025 __git_get_config_variables ()
1027 local section="$1" i IFS=$'\n'
1028 for i in $(__git config --name-only --get-regexp "^$section\..*"); do
1029 echo "${i#$section.}"
1030 done
1033 __git_pretty_aliases ()
1035 __git_get_config_variables "pretty"
1038 # __git_aliased_command requires 1 argument
1039 __git_aliased_command ()
1041 local word cmdline=$(__git config --get "alias.$1")
1042 for word in $cmdline; do
1043 case "$word" in
1044 \!gitk|gitk)
1045 echo "gitk"
1046 return
1048 \!*) : shell command alias ;;
1049 -*) : option ;;
1050 *=*) : setting env ;;
1051 git) : git itself ;;
1052 \(\)) : skip parens of shell function definition ;;
1053 {) : skip start of shell helper function ;;
1054 :) : skip null command ;;
1055 \'*) : skip opening quote after sh -c ;;
1057 echo "$word"
1058 return
1059 esac
1060 done
1063 # __git_find_on_cmdline requires 1 argument
1064 __git_find_on_cmdline ()
1066 local word subcommand c=1
1067 while [ $c -lt $cword ]; do
1068 word="${words[c]}"
1069 for subcommand in $1; do
1070 if [ "$subcommand" = "$word" ]; then
1071 echo "$subcommand"
1072 return
1074 done
1075 ((c++))
1076 done
1079 # Echo the value of an option set on the command line or config
1081 # $1: short option name
1082 # $2: long option name including =
1083 # $3: list of possible values
1084 # $4: config string (optional)
1086 # example:
1087 # result="$(__git_get_option_value "-d" "--do-something=" \
1088 # "yes no" "core.doSomething")"
1090 # result is then either empty (no option set) or "yes" or "no"
1092 # __git_get_option_value requires 3 arguments
1093 __git_get_option_value ()
1095 local c short_opt long_opt val
1096 local result= values config_key word
1098 short_opt="$1"
1099 long_opt="$2"
1100 values="$3"
1101 config_key="$4"
1103 ((c = $cword - 1))
1104 while [ $c -ge 0 ]; do
1105 word="${words[c]}"
1106 for val in $values; do
1107 if [ "$short_opt$val" = "$word" ] ||
1108 [ "$long_opt$val" = "$word" ]; then
1109 result="$val"
1110 break 2
1112 done
1113 ((c--))
1114 done
1116 if [ -n "$config_key" ] && [ -z "$result" ]; then
1117 result="$(__git config "$config_key")"
1120 echo "$result"
1123 __git_has_doubledash ()
1125 local c=1
1126 while [ $c -lt $cword ]; do
1127 if [ "--" = "${words[c]}" ]; then
1128 return 0
1130 ((c++))
1131 done
1132 return 1
1135 # Try to count non option arguments passed on the command line for the
1136 # specified git command.
1137 # When options are used, it is necessary to use the special -- option to
1138 # tell the implementation were non option arguments begin.
1139 # XXX this can not be improved, since options can appear everywhere, as
1140 # an example:
1141 # git mv x -n y
1143 # __git_count_arguments requires 1 argument: the git command executed.
1144 __git_count_arguments ()
1146 local word i c=0
1148 # Skip "git" (first argument)
1149 for ((i=1; i < ${#words[@]}; i++)); do
1150 word="${words[i]}"
1152 case "$word" in
1154 # Good; we can assume that the following are only non
1155 # option arguments.
1156 ((c = 0))
1158 "$1")
1159 # Skip the specified git command and discard git
1160 # main options
1161 ((c = 0))
1164 ((c++))
1166 esac
1167 done
1169 printf "%d" $c
1172 __git_whitespacelist="nowarn warn error error-all fix"
1173 __git_am_inprogress_options="--skip --continue --resolved --abort --quit --show-current-patch"
1175 _git_am ()
1177 __git_find_repo_path
1178 if [ -d "$__git_repo_path"/rebase-apply ]; then
1179 __gitcomp "$__git_am_inprogress_options"
1180 return
1182 case "$cur" in
1183 --whitespace=*)
1184 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1185 return
1187 --*)
1188 __gitcomp_builtin am "" \
1189 "$__git_am_inprogress_options"
1190 return
1191 esac
1194 _git_apply ()
1196 case "$cur" in
1197 --whitespace=*)
1198 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1199 return
1201 --*)
1202 __gitcomp_builtin apply
1203 return
1204 esac
1207 _git_add ()
1209 case "$cur" in
1210 --*)
1211 __gitcomp_builtin add
1212 return
1213 esac
1215 local complete_opt="--others --modified --directory --no-empty-directory"
1216 if test -n "$(__git_find_on_cmdline "-u --update")"
1217 then
1218 complete_opt="--modified"
1220 __git_complete_index_file "$complete_opt"
1223 _git_archive ()
1225 case "$cur" in
1226 --format=*)
1227 __gitcomp "$(git archive --list)" "" "${cur##--format=}"
1228 return
1230 --remote=*)
1231 __gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
1232 return
1234 --*)
1235 __gitcomp "
1236 --format= --list --verbose
1237 --prefix= --remote= --exec= --output
1239 return
1241 esac
1242 __git_complete_file
1245 _git_bisect ()
1247 __git_has_doubledash && return
1249 local subcommands="start bad good skip reset visualize replay log run"
1250 local subcommand="$(__git_find_on_cmdline "$subcommands")"
1251 if [ -z "$subcommand" ]; then
1252 __git_find_repo_path
1253 if [ -f "$__git_repo_path"/BISECT_START ]; then
1254 __gitcomp "$subcommands"
1255 else
1256 __gitcomp "replay start"
1258 return
1261 case "$subcommand" in
1262 bad|good|reset|skip|start)
1263 __git_complete_refs
1267 esac
1270 _git_branch ()
1272 local i c=1 only_local_ref="n" has_r="n"
1274 while [ $c -lt $cword ]; do
1275 i="${words[c]}"
1276 case "$i" in
1277 -d|--delete|-m|--move) only_local_ref="y" ;;
1278 -r|--remotes) has_r="y" ;;
1279 esac
1280 ((c++))
1281 done
1283 case "$cur" in
1284 --set-upstream-to=*)
1285 __git_complete_refs --cur="${cur##--set-upstream-to=}"
1287 --*)
1288 __gitcomp_builtin branch
1291 if [ $only_local_ref = "y" -a $has_r = "n" ]; then
1292 __gitcomp_direct "$(__git_heads "" "$cur" " ")"
1293 else
1294 __git_complete_refs
1297 esac
1300 _git_bundle ()
1302 local cmd="${words[2]}"
1303 case "$cword" in
1305 __gitcomp "create list-heads verify unbundle"
1308 # looking for a file
1311 case "$cmd" in
1312 create)
1313 __git_complete_revlist
1315 esac
1317 esac
1320 _git_checkout ()
1322 __git_has_doubledash && return
1324 case "$cur" in
1325 --conflict=*)
1326 __gitcomp "diff3 merge" "" "${cur##--conflict=}"
1328 --*)
1329 __gitcomp_builtin checkout
1332 # check if --track, --no-track, or --no-guess was specified
1333 # if so, disable DWIM mode
1334 local flags="--track --no-track --no-guess" track_opt="--track"
1335 if [ "$GIT_COMPLETION_CHECKOUT_NO_GUESS" = "1" ] ||
1336 [ -n "$(__git_find_on_cmdline "$flags")" ]; then
1337 track_opt=''
1339 __git_complete_refs $track_opt
1341 esac
1344 __git_cherry_pick_inprogress_options="--continue --quit --abort"
1346 _git_cherry_pick ()
1348 __git_find_repo_path
1349 if [ -f "$__git_repo_path"/CHERRY_PICK_HEAD ]; then
1350 __gitcomp "$__git_cherry_pick_inprogress_options"
1351 return
1353 case "$cur" in
1354 --*)
1355 __gitcomp_builtin cherry-pick "" \
1356 "$__git_cherry_pick_inprogress_options"
1359 __git_complete_refs
1361 esac
1364 _git_clean ()
1366 case "$cur" in
1367 --*)
1368 __gitcomp_builtin clean
1369 return
1371 esac
1373 # XXX should we check for -x option ?
1374 __git_complete_index_file "--others --directory"
1377 _git_clone ()
1379 case "$cur" in
1380 --*)
1381 __gitcomp_builtin clone
1382 return
1384 esac
1387 __git_untracked_file_modes="all no normal"
1389 _git_commit ()
1391 case "$prev" in
1392 -c|-C)
1393 __git_complete_refs
1394 return
1396 esac
1398 case "$cur" in
1399 --cleanup=*)
1400 __gitcomp "default scissors strip verbatim whitespace
1401 " "" "${cur##--cleanup=}"
1402 return
1404 --reuse-message=*|--reedit-message=*|\
1405 --fixup=*|--squash=*)
1406 __git_complete_refs --cur="${cur#*=}"
1407 return
1409 --untracked-files=*)
1410 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
1411 return
1413 --*)
1414 __gitcomp_builtin commit
1415 return
1416 esac
1418 if __git rev-parse --verify --quiet HEAD >/dev/null; then
1419 __git_complete_index_file "--committable"
1420 else
1421 # This is the first commit
1422 __git_complete_index_file "--cached"
1426 _git_describe ()
1428 case "$cur" in
1429 --*)
1430 __gitcomp_builtin describe
1431 return
1432 esac
1433 __git_complete_refs
1436 __git_diff_algorithms="myers minimal patience histogram"
1438 __git_diff_submodule_formats="diff log short"
1440 __git_diff_common_options="--stat --numstat --shortstat --summary
1441 --patch-with-stat --name-only --name-status --color
1442 --no-color --color-words --no-renames --check
1443 --full-index --binary --abbrev --diff-filter=
1444 --find-copies-harder --ignore-cr-at-eol
1445 --text --ignore-space-at-eol --ignore-space-change
1446 --ignore-all-space --ignore-blank-lines --exit-code
1447 --quiet --ext-diff --no-ext-diff
1448 --no-prefix --src-prefix= --dst-prefix=
1449 --inter-hunk-context=
1450 --patience --histogram --minimal
1451 --raw --word-diff --word-diff-regex=
1452 --dirstat --dirstat= --dirstat-by-file
1453 --dirstat-by-file= --cumulative
1454 --diff-algorithm=
1455 --submodule --submodule= --ignore-submodules
1458 _git_diff ()
1460 __git_has_doubledash && return
1462 case "$cur" in
1463 --diff-algorithm=*)
1464 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1465 return
1467 --submodule=*)
1468 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1469 return
1471 --*)
1472 __gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
1473 --base --ours --theirs --no-index
1474 $__git_diff_common_options
1476 return
1478 esac
1479 __git_complete_revlist_file
1482 __git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff
1483 tkdiff vimdiff gvimdiff xxdiff araxis p4merge bc codecompare
1486 _git_difftool ()
1488 __git_has_doubledash && return
1490 case "$cur" in
1491 --tool=*)
1492 __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1493 return
1495 --*)
1496 __gitcomp_builtin difftool "$__git_diff_common_options
1497 --base --cached --ours --theirs
1498 --pickaxe-all --pickaxe-regex
1499 --relative --staged
1501 return
1503 esac
1504 __git_complete_revlist_file
1507 __git_fetch_recurse_submodules="yes on-demand no"
1509 _git_fetch ()
1511 case "$cur" in
1512 --recurse-submodules=*)
1513 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1514 return
1516 --*)
1517 __gitcomp_builtin fetch
1518 return
1520 esac
1521 __git_complete_remote_or_refspec
1524 __git_format_patch_options="
1525 --stdout --attach --no-attach --thread --thread= --no-thread
1526 --numbered --start-number --numbered-files --keep-subject --signoff
1527 --signature --no-signature --in-reply-to= --cc= --full-index --binary
1528 --not --all --cover-letter --no-prefix --src-prefix= --dst-prefix=
1529 --inline --suffix= --ignore-if-in-upstream --subject-prefix=
1530 --output-directory --reroll-count --to= --quiet --notes
1533 _git_format_patch ()
1535 case "$cur" in
1536 --thread=*)
1537 __gitcomp "
1538 deep shallow
1539 " "" "${cur##--thread=}"
1540 return
1542 --*)
1543 __gitcomp "$__git_format_patch_options"
1544 return
1546 esac
1547 __git_complete_revlist
1550 _git_fsck ()
1552 case "$cur" in
1553 --*)
1554 __gitcomp_builtin fsck
1555 return
1557 esac
1560 _git_gitk ()
1562 _gitk
1565 # Lists matching symbol names from a tag (as in ctags) file.
1566 # 1: List symbol names matching this word.
1567 # 2: The tag file to list symbol names from.
1568 # 3: A prefix to be added to each listed symbol name (optional).
1569 # 4: A suffix to be appended to each listed symbol name (optional).
1570 __git_match_ctag () {
1571 awk -v pfx="${3-}" -v sfx="${4-}" "
1572 /^${1//\//\\/}/ { print pfx \$1 sfx }
1573 " "$2"
1576 # Complete symbol names from a tag file.
1577 # Usage: __git_complete_symbol [<option>]...
1578 # --tags=<file>: The tag file to list symbol names from instead of the
1579 # default "tags".
1580 # --pfx=<prefix>: A prefix to be added to each symbol name.
1581 # --cur=<word>: The current symbol name to be completed. Defaults to
1582 # the current word to be completed.
1583 # --sfx=<suffix>: A suffix to be appended to each symbol name instead
1584 # of the default space.
1585 __git_complete_symbol () {
1586 local tags=tags pfx="" cur_="${cur-}" sfx=" "
1588 while test $# != 0; do
1589 case "$1" in
1590 --tags=*) tags="${1##--tags=}" ;;
1591 --pfx=*) pfx="${1##--pfx=}" ;;
1592 --cur=*) cur_="${1##--cur=}" ;;
1593 --sfx=*) sfx="${1##--sfx=}" ;;
1594 *) return 1 ;;
1595 esac
1596 shift
1597 done
1599 if test -r "$tags"; then
1600 __gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
1604 _git_grep ()
1606 __git_has_doubledash && return
1608 case "$cur" in
1609 --*)
1610 __gitcomp_builtin grep
1611 return
1613 esac
1615 case "$cword,$prev" in
1616 2,*|*,-*)
1617 __git_complete_symbol && return
1619 esac
1621 __git_complete_refs
1624 _git_help ()
1626 case "$cur" in
1627 --*)
1628 __gitcomp_builtin help
1629 return
1631 esac
1632 if test -n "$GIT_TESTING_ALL_COMMAND_LIST"
1633 then
1634 __gitcomp "$GIT_TESTING_ALL_COMMAND_LIST $(git --list-cmds=alias,list-guide) gitk"
1635 else
1636 __gitcomp "$(git --list-cmds=main,nohelpers,alias,list-guide) gitk"
1640 _git_init ()
1642 case "$cur" in
1643 --shared=*)
1644 __gitcomp "
1645 false true umask group all world everybody
1646 " "" "${cur##--shared=}"
1647 return
1649 --*)
1650 __gitcomp_builtin init
1651 return
1653 esac
1656 _git_ls_files ()
1658 case "$cur" in
1659 --*)
1660 __gitcomp_builtin ls-files
1661 return
1663 esac
1665 # XXX ignore options like --modified and always suggest all cached
1666 # files.
1667 __git_complete_index_file "--cached"
1670 _git_ls_remote ()
1672 case "$cur" in
1673 --*)
1674 __gitcomp_builtin ls-remote
1675 return
1677 esac
1678 __gitcomp_nl "$(__git_remotes)"
1681 _git_ls_tree ()
1683 case "$cur" in
1684 --*)
1685 __gitcomp_builtin ls-tree
1686 return
1688 esac
1690 __git_complete_file
1693 # Options that go well for log, shortlog and gitk
1694 __git_log_common_options="
1695 --not --all
1696 --branches --tags --remotes
1697 --first-parent --merges --no-merges
1698 --max-count=
1699 --max-age= --since= --after=
1700 --min-age= --until= --before=
1701 --min-parents= --max-parents=
1702 --no-min-parents --no-max-parents
1704 # Options that go well for log and gitk (not shortlog)
1705 __git_log_gitk_options="
1706 --dense --sparse --full-history
1707 --simplify-merges --simplify-by-decoration
1708 --left-right --notes --no-notes
1710 # Options that go well for log and shortlog (not gitk)
1711 __git_log_shortlog_options="
1712 --author= --committer= --grep=
1713 --all-match --invert-grep
1716 __git_log_pretty_formats="oneline short medium full fuller email raw format:"
1717 __git_log_date_formats="relative iso8601 rfc2822 short local default raw"
1719 _git_log ()
1721 __git_has_doubledash && return
1722 __git_find_repo_path
1724 local merge=""
1725 if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
1726 merge="--merge"
1728 case "$prev,$cur" in
1729 -L,:*:*)
1730 return # fall back to Bash filename completion
1732 -L,:*)
1733 __git_complete_symbol --cur="${cur#:}" --sfx=":"
1734 return
1736 -G,*|-S,*)
1737 __git_complete_symbol
1738 return
1740 esac
1741 case "$cur" in
1742 --pretty=*|--format=*)
1743 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
1744 " "" "${cur#*=}"
1745 return
1747 --date=*)
1748 __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
1749 return
1751 --decorate=*)
1752 __gitcomp "full short no" "" "${cur##--decorate=}"
1753 return
1755 --diff-algorithm=*)
1756 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1757 return
1759 --submodule=*)
1760 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1761 return
1763 --*)
1764 __gitcomp "
1765 $__git_log_common_options
1766 $__git_log_shortlog_options
1767 $__git_log_gitk_options
1768 --root --topo-order --date-order --reverse
1769 --follow --full-diff
1770 --abbrev-commit --abbrev=
1771 --relative-date --date=
1772 --pretty= --format= --oneline
1773 --show-signature
1774 --cherry-mark
1775 --cherry-pick
1776 --graph
1777 --decorate --decorate=
1778 --walk-reflogs
1779 --parents --children
1780 $merge
1781 $__git_diff_common_options
1782 --pickaxe-all --pickaxe-regex
1784 return
1786 -L:*:*)
1787 return # fall back to Bash filename completion
1789 -L:*)
1790 __git_complete_symbol --cur="${cur#-L:}" --sfx=":"
1791 return
1793 -G*)
1794 __git_complete_symbol --pfx="-G" --cur="${cur#-G}"
1795 return
1797 -S*)
1798 __git_complete_symbol --pfx="-S" --cur="${cur#-S}"
1799 return
1801 esac
1802 __git_complete_revlist
1805 _git_merge ()
1807 __git_complete_strategy && return
1809 case "$cur" in
1810 --*)
1811 __gitcomp_builtin merge
1812 return
1813 esac
1814 __git_complete_refs
1817 _git_mergetool ()
1819 case "$cur" in
1820 --tool=*)
1821 __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
1822 return
1824 --*)
1825 __gitcomp "--tool= --prompt --no-prompt"
1826 return
1828 esac
1831 _git_merge_base ()
1833 case "$cur" in
1834 --*)
1835 __gitcomp_builtin merge-base
1836 return
1838 esac
1839 __git_complete_refs
1842 _git_mv ()
1844 case "$cur" in
1845 --*)
1846 __gitcomp_builtin mv
1847 return
1849 esac
1851 if [ $(__git_count_arguments "mv") -gt 0 ]; then
1852 # We need to show both cached and untracked files (including
1853 # empty directories) since this may not be the last argument.
1854 __git_complete_index_file "--cached --others --directory"
1855 else
1856 __git_complete_index_file "--cached"
1860 _git_notes ()
1862 local subcommands='add append copy edit get-ref list merge prune remove show'
1863 local subcommand="$(__git_find_on_cmdline "$subcommands")"
1865 case "$subcommand,$cur" in
1866 ,--*)
1867 __gitcomp_builtin notes
1870 case "$prev" in
1871 --ref)
1872 __git_complete_refs
1875 __gitcomp "$subcommands --ref"
1877 esac
1879 *,--reuse-message=*|*,--reedit-message=*)
1880 __git_complete_refs --cur="${cur#*=}"
1882 *,--*)
1883 __gitcomp_builtin notes_$subcommand
1885 prune,*|get-ref,*)
1886 # this command does not take a ref, do not complete it
1889 case "$prev" in
1890 -m|-F)
1893 __git_complete_refs
1895 esac
1897 esac
1900 _git_pull ()
1902 __git_complete_strategy && return
1904 case "$cur" in
1905 --recurse-submodules=*)
1906 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1907 return
1909 --*)
1910 __gitcomp_builtin pull
1912 return
1914 esac
1915 __git_complete_remote_or_refspec
1918 __git_push_recurse_submodules="check on-demand only"
1920 __git_complete_force_with_lease ()
1922 local cur_=$1
1924 case "$cur_" in
1925 --*=)
1927 *:*)
1928 __git_complete_refs --cur="${cur_#*:}"
1931 __git_complete_refs --cur="$cur_"
1933 esac
1936 _git_push ()
1938 case "$prev" in
1939 --repo)
1940 __gitcomp_nl "$(__git_remotes)"
1941 return
1943 --recurse-submodules)
1944 __gitcomp "$__git_push_recurse_submodules"
1945 return
1947 esac
1948 case "$cur" in
1949 --repo=*)
1950 __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
1951 return
1953 --recurse-submodules=*)
1954 __gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
1955 return
1957 --force-with-lease=*)
1958 __git_complete_force_with_lease "${cur##--force-with-lease=}"
1959 return
1961 --*)
1962 __gitcomp_builtin push
1963 return
1965 esac
1966 __git_complete_remote_or_refspec
1969 _git_range_diff ()
1971 case "$cur" in
1972 --*)
1973 __gitcomp "
1974 --creation-factor= --no-dual-color
1975 $__git_diff_common_options
1977 return
1979 esac
1980 __git_complete_revlist
1983 _git_rebase ()
1985 __git_find_repo_path
1986 if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
1987 __gitcomp "--continue --skip --abort --quit --edit-todo --show-current-patch"
1988 return
1989 elif [ -d "$__git_repo_path"/rebase-apply ] || \
1990 [ -d "$__git_repo_path"/rebase-merge ]; then
1991 __gitcomp "--continue --skip --abort --quit --show-current-patch"
1992 return
1994 __git_complete_strategy && return
1995 case "$cur" in
1996 --whitespace=*)
1997 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1998 return
2000 --*)
2001 __gitcomp "
2002 --onto --merge --strategy --interactive
2003 --rebase-merges --preserve-merges --stat --no-stat
2004 --committer-date-is-author-date --ignore-date
2005 --ignore-whitespace --whitespace=
2006 --autosquash --no-autosquash
2007 --fork-point --no-fork-point
2008 --autostash --no-autostash
2009 --verify --no-verify
2010 --keep-empty --root --force-rebase --no-ff
2011 --rerere-autoupdate
2012 --exec
2015 return
2016 esac
2017 __git_complete_refs
2020 _git_reflog ()
2022 local subcommands="show delete expire"
2023 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2025 if [ -z "$subcommand" ]; then
2026 __gitcomp "$subcommands"
2027 else
2028 __git_complete_refs
2032 __git_send_email_confirm_options="always never auto cc compose"
2033 __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
2035 _git_send_email ()
2037 case "$prev" in
2038 --to|--cc|--bcc|--from)
2039 __gitcomp "$(__git send-email --dump-aliases)"
2040 return
2042 esac
2044 case "$cur" in
2045 --confirm=*)
2046 __gitcomp "
2047 $__git_send_email_confirm_options
2048 " "" "${cur##--confirm=}"
2049 return
2051 --suppress-cc=*)
2052 __gitcomp "
2053 $__git_send_email_suppresscc_options
2054 " "" "${cur##--suppress-cc=}"
2056 return
2058 --smtp-encryption=*)
2059 __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
2060 return
2062 --thread=*)
2063 __gitcomp "
2064 deep shallow
2065 " "" "${cur##--thread=}"
2066 return
2068 --to=*|--cc=*|--bcc=*|--from=*)
2069 __gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
2070 return
2072 --*)
2073 __gitcomp "--annotate --bcc --cc --cc-cmd --chain-reply-to
2074 --compose --confirm= --dry-run --envelope-sender
2075 --from --identity
2076 --in-reply-to --no-chain-reply-to --no-signed-off-by-cc
2077 --no-suppress-from --no-thread --quiet --reply-to
2078 --signed-off-by-cc --smtp-pass --smtp-server
2079 --smtp-server-port --smtp-encryption= --smtp-user
2080 --subject --suppress-cc= --suppress-from --thread --to
2081 --validate --no-validate
2082 $__git_format_patch_options"
2083 return
2085 esac
2086 __git_complete_revlist
2089 _git_stage ()
2091 _git_add
2094 _git_status ()
2096 local complete_opt
2097 local untracked_state
2099 case "$cur" in
2100 --ignore-submodules=*)
2101 __gitcomp "none untracked dirty all" "" "${cur##--ignore-submodules=}"
2102 return
2104 --untracked-files=*)
2105 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
2106 return
2108 --column=*)
2109 __gitcomp "
2110 always never auto column row plain dense nodense
2111 " "" "${cur##--column=}"
2112 return
2114 --*)
2115 __gitcomp_builtin status
2116 return
2118 esac
2120 untracked_state="$(__git_get_option_value "-u" "--untracked-files=" \
2121 "$__git_untracked_file_modes" "status.showUntrackedFiles")"
2123 case "$untracked_state" in
2125 # --ignored option does not matter
2126 complete_opt=
2128 all|normal|*)
2129 complete_opt="--cached --directory --no-empty-directory --others"
2131 if [ -n "$(__git_find_on_cmdline "--ignored")" ]; then
2132 complete_opt="$complete_opt --ignored --exclude=*"
2135 esac
2137 __git_complete_index_file "$complete_opt"
2140 __git_config_get_set_variables ()
2142 local prevword word config_file= c=$cword
2143 while [ $c -gt 1 ]; do
2144 word="${words[c]}"
2145 case "$word" in
2146 --system|--global|--local|--file=*)
2147 config_file="$word"
2148 break
2150 -f|--file)
2151 config_file="$word $prevword"
2152 break
2154 esac
2155 prevword=$word
2156 c=$((--c))
2157 done
2159 __git config $config_file --name-only --list
2162 __git_config_vars=
2163 __git_compute_config_vars ()
2165 test -n "$__git_config_vars" ||
2166 __git_config_vars="$(git help --config-for-completion | sort | uniq)"
2169 _git_config ()
2171 local varname
2173 if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
2174 varname="${prev,,}"
2175 else
2176 varname="$(echo "$prev" |tr A-Z a-z)"
2179 case "$varname" in
2180 branch.*.remote|branch.*.pushremote)
2181 __gitcomp_nl "$(__git_remotes)"
2182 return
2184 branch.*.merge)
2185 __git_complete_refs
2186 return
2188 branch.*.rebase)
2189 __gitcomp "false true merges preserve interactive"
2190 return
2192 remote.pushdefault)
2193 __gitcomp_nl "$(__git_remotes)"
2194 return
2196 remote.*.fetch)
2197 local remote="${prev#remote.}"
2198 remote="${remote%.fetch}"
2199 if [ -z "$cur" ]; then
2200 __gitcomp_nl "refs/heads/" "" "" ""
2201 return
2203 __gitcomp_nl "$(__git_refs_remotes "$remote")"
2204 return
2206 remote.*.push)
2207 local remote="${prev#remote.}"
2208 remote="${remote%.push}"
2209 __gitcomp_nl "$(__git for-each-ref \
2210 --format='%(refname):%(refname)' refs/heads)"
2211 return
2213 pull.twohead|pull.octopus)
2214 __git_compute_merge_strategies
2215 __gitcomp "$__git_merge_strategies"
2216 return
2218 color.branch|color.diff|color.interactive|\
2219 color.showbranch|color.status|color.ui)
2220 __gitcomp "always never auto"
2221 return
2223 color.pager)
2224 __gitcomp "false true"
2225 return
2227 color.*.*)
2228 __gitcomp "
2229 normal black red green yellow blue magenta cyan white
2230 bold dim ul blink reverse
2232 return
2234 diff.submodule)
2235 __gitcomp "log short"
2236 return
2238 help.format)
2239 __gitcomp "man info web html"
2240 return
2242 log.date)
2243 __gitcomp "$__git_log_date_formats"
2244 return
2246 sendemail.aliasfiletype)
2247 __gitcomp "mutt mailrc pine elm gnus"
2248 return
2250 sendemail.confirm)
2251 __gitcomp "$__git_send_email_confirm_options"
2252 return
2254 sendemail.suppresscc)
2255 __gitcomp "$__git_send_email_suppresscc_options"
2256 return
2258 sendemail.transferencoding)
2259 __gitcomp "7bit 8bit quoted-printable base64"
2260 return
2262 --get|--get-all|--unset|--unset-all)
2263 __gitcomp_nl "$(__git_config_get_set_variables)"
2264 return
2266 *.*)
2267 return
2269 esac
2270 case "$cur" in
2271 --*)
2272 __gitcomp_builtin config
2273 return
2275 branch.*.*)
2276 local pfx="${cur%.*}." cur_="${cur##*.}"
2277 __gitcomp "remote pushRemote merge mergeOptions rebase" "$pfx" "$cur_"
2278 return
2280 branch.*)
2281 local pfx="${cur%.*}." cur_="${cur#*.}"
2282 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
2283 __gitcomp_nl_append $'autoSetupMerge\nautoSetupRebase\n' "$pfx" "$cur_"
2284 return
2286 guitool.*.*)
2287 local pfx="${cur%.*}." cur_="${cur##*.}"
2288 __gitcomp "
2289 argPrompt cmd confirm needsFile noConsole noRescan
2290 prompt revPrompt revUnmerged title
2291 " "$pfx" "$cur_"
2292 return
2294 difftool.*.*)
2295 local pfx="${cur%.*}." cur_="${cur##*.}"
2296 __gitcomp "cmd path" "$pfx" "$cur_"
2297 return
2299 man.*.*)
2300 local pfx="${cur%.*}." cur_="${cur##*.}"
2301 __gitcomp "cmd path" "$pfx" "$cur_"
2302 return
2304 mergetool.*.*)
2305 local pfx="${cur%.*}." cur_="${cur##*.}"
2306 __gitcomp "cmd path trustExitCode" "$pfx" "$cur_"
2307 return
2309 pager.*)
2310 local pfx="${cur%.*}." cur_="${cur#*.}"
2311 __git_compute_all_commands
2312 __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_"
2313 return
2315 remote.*.*)
2316 local pfx="${cur%.*}." cur_="${cur##*.}"
2317 __gitcomp "
2318 url proxy fetch push mirror skipDefaultUpdate
2319 receivepack uploadpack tagOpt pushurl
2320 " "$pfx" "$cur_"
2321 return
2323 remote.*)
2324 local pfx="${cur%.*}." cur_="${cur#*.}"
2325 __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
2326 __gitcomp_nl_append "pushDefault" "$pfx" "$cur_"
2327 return
2329 url.*.*)
2330 local pfx="${cur%.*}." cur_="${cur##*.}"
2331 __gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_"
2332 return
2334 *.*)
2335 __git_compute_config_vars
2336 __gitcomp "$__git_config_vars"
2339 __git_compute_config_vars
2340 __gitcomp "$(echo "$__git_config_vars" | sed 's/\.[^ ]*/./g')"
2341 esac
2344 _git_remote ()
2346 local subcommands="
2347 add rename remove set-head set-branches
2348 get-url set-url show prune update
2350 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2351 if [ -z "$subcommand" ]; then
2352 case "$cur" in
2353 --*)
2354 __gitcomp_builtin remote
2357 __gitcomp "$subcommands"
2359 esac
2360 return
2363 case "$subcommand,$cur" in
2364 add,--*)
2365 __gitcomp_builtin remote_add
2367 add,*)
2369 set-head,--*)
2370 __gitcomp_builtin remote_set-head
2372 set-branches,--*)
2373 __gitcomp_builtin remote_set-branches
2375 set-head,*|set-branches,*)
2376 __git_complete_remote_or_refspec
2378 update,--*)
2379 __gitcomp_builtin remote_update
2381 update,*)
2382 __gitcomp "$(__git_remotes) $(__git_get_config_variables "remotes")"
2384 set-url,--*)
2385 __gitcomp_builtin remote_set-url
2387 get-url,--*)
2388 __gitcomp_builtin remote_get-url
2390 prune,--*)
2391 __gitcomp_builtin remote_prune
2394 __gitcomp_nl "$(__git_remotes)"
2396 esac
2399 _git_replace ()
2401 case "$cur" in
2402 --*)
2403 __gitcomp_builtin replace
2404 return
2406 esac
2407 __git_complete_refs
2410 _git_rerere ()
2412 local subcommands="clear forget diff remaining status gc"
2413 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2414 if test -z "$subcommand"
2415 then
2416 __gitcomp "$subcommands"
2417 return
2421 _git_reset ()
2423 __git_has_doubledash && return
2425 case "$cur" in
2426 --*)
2427 __gitcomp_builtin reset
2428 return
2430 esac
2431 __git_complete_refs
2434 __git_revert_inprogress_options="--continue --quit --abort"
2436 _git_revert ()
2438 __git_find_repo_path
2439 if [ -f "$__git_repo_path"/REVERT_HEAD ]; then
2440 __gitcomp "$__git_revert_inprogress_options"
2441 return
2443 case "$cur" in
2444 --*)
2445 __gitcomp_builtin revert "" \
2446 "$__git_revert_inprogress_options"
2447 return
2449 esac
2450 __git_complete_refs
2453 _git_rm ()
2455 case "$cur" in
2456 --*)
2457 __gitcomp_builtin rm
2458 return
2460 esac
2462 __git_complete_index_file "--cached"
2465 _git_shortlog ()
2467 __git_has_doubledash && return
2469 case "$cur" in
2470 --*)
2471 __gitcomp "
2472 $__git_log_common_options
2473 $__git_log_shortlog_options
2474 --numbered --summary --email
2476 return
2478 esac
2479 __git_complete_revlist
2482 _git_show ()
2484 __git_has_doubledash && return
2486 case "$cur" in
2487 --pretty=*|--format=*)
2488 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2489 " "" "${cur#*=}"
2490 return
2492 --diff-algorithm=*)
2493 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
2494 return
2496 --submodule=*)
2497 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
2498 return
2500 --*)
2501 __gitcomp "--pretty= --format= --abbrev-commit --oneline
2502 --show-signature
2503 $__git_diff_common_options
2505 return
2507 esac
2508 __git_complete_revlist_file
2511 _git_show_branch ()
2513 case "$cur" in
2514 --*)
2515 __gitcomp_builtin show-branch
2516 return
2518 esac
2519 __git_complete_revlist
2522 _git_stash ()
2524 local save_opts='--all --keep-index --no-keep-index --quiet --patch --include-untracked'
2525 local subcommands='push list show apply clear drop pop create branch'
2526 local subcommand="$(__git_find_on_cmdline "$subcommands save")"
2527 if [ -n "$(__git_find_on_cmdline "-p")" ]; then
2528 subcommand="push"
2530 if [ -z "$subcommand" ]; then
2531 case "$cur" in
2532 --*)
2533 __gitcomp "$save_opts"
2535 sa*)
2536 if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
2537 __gitcomp "save"
2541 if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
2542 __gitcomp "$subcommands"
2545 esac
2546 else
2547 case "$subcommand,$cur" in
2548 push,--*)
2549 __gitcomp "$save_opts --message"
2551 save,--*)
2552 __gitcomp "$save_opts"
2554 apply,--*|pop,--*)
2555 __gitcomp "--index --quiet"
2557 drop,--*)
2558 __gitcomp "--quiet"
2560 list,--*)
2561 __gitcomp "--name-status --oneline --patch-with-stat"
2563 show,--*|branch,--*)
2565 branch,*)
2566 if [ $cword -eq 3 ]; then
2567 __git_complete_refs
2568 else
2569 __gitcomp_nl "$(__git stash list \
2570 | sed -n -e 's/:.*//p')"
2573 show,*|apply,*|drop,*|pop,*)
2574 __gitcomp_nl "$(__git stash list \
2575 | sed -n -e 's/:.*//p')"
2579 esac
2583 _git_submodule ()
2585 __git_has_doubledash && return
2587 local subcommands="add status init deinit update summary foreach sync"
2588 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2589 if [ -z "$subcommand" ]; then
2590 case "$cur" in
2591 --*)
2592 __gitcomp "--quiet"
2595 __gitcomp "$subcommands"
2597 esac
2598 return
2601 case "$subcommand,$cur" in
2602 add,--*)
2603 __gitcomp "--branch --force --name --reference --depth"
2605 status,--*)
2606 __gitcomp "--cached --recursive"
2608 deinit,--*)
2609 __gitcomp "--force --all"
2611 update,--*)
2612 __gitcomp "
2613 --init --remote --no-fetch
2614 --recommend-shallow --no-recommend-shallow
2615 --force --rebase --merge --reference --depth --recursive --jobs
2618 summary,--*)
2619 __gitcomp "--cached --files --summary-limit"
2621 foreach,--*|sync,--*)
2622 __gitcomp "--recursive"
2626 esac
2629 _git_svn ()
2631 local subcommands="
2632 init fetch clone rebase dcommit log find-rev
2633 set-tree commit-diff info create-ignore propget
2634 proplist show-ignore show-externals branch tag blame
2635 migrate mkdirs reset gc
2637 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2638 if [ -z "$subcommand" ]; then
2639 __gitcomp "$subcommands"
2640 else
2641 local remote_opts="--username= --config-dir= --no-auth-cache"
2642 local fc_opts="
2643 --follow-parent --authors-file= --repack=
2644 --no-metadata --use-svm-props --use-svnsync-props
2645 --log-window-size= --no-checkout --quiet
2646 --repack-flags --use-log-author --localtime
2647 --add-author-from
2648 --ignore-paths= --include-paths= $remote_opts
2650 local init_opts="
2651 --template= --shared= --trunk= --tags=
2652 --branches= --stdlayout --minimize-url
2653 --no-metadata --use-svm-props --use-svnsync-props
2654 --rewrite-root= --prefix= $remote_opts
2656 local cmt_opts="
2657 --edit --rmdir --find-copies-harder --copy-similarity=
2660 case "$subcommand,$cur" in
2661 fetch,--*)
2662 __gitcomp "--revision= --fetch-all $fc_opts"
2664 clone,--*)
2665 __gitcomp "--revision= $fc_opts $init_opts"
2667 init,--*)
2668 __gitcomp "$init_opts"
2670 dcommit,--*)
2671 __gitcomp "
2672 --merge --strategy= --verbose --dry-run
2673 --fetch-all --no-rebase --commit-url
2674 --revision --interactive $cmt_opts $fc_opts
2677 set-tree,--*)
2678 __gitcomp "--stdin $cmt_opts $fc_opts"
2680 create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
2681 show-externals,--*|mkdirs,--*)
2682 __gitcomp "--revision="
2684 log,--*)
2685 __gitcomp "
2686 --limit= --revision= --verbose --incremental
2687 --oneline --show-commit --non-recursive
2688 --authors-file= --color
2691 rebase,--*)
2692 __gitcomp "
2693 --merge --verbose --strategy= --local
2694 --fetch-all --dry-run $fc_opts
2697 commit-diff,--*)
2698 __gitcomp "--message= --file= --revision= $cmt_opts"
2700 info,--*)
2701 __gitcomp "--url"
2703 branch,--*)
2704 __gitcomp "--dry-run --message --tag"
2706 tag,--*)
2707 __gitcomp "--dry-run --message"
2709 blame,--*)
2710 __gitcomp "--git-format"
2712 migrate,--*)
2713 __gitcomp "
2714 --config-dir= --ignore-paths= --minimize
2715 --no-auth-cache --username=
2718 reset,--*)
2719 __gitcomp "--revision= --parent"
2723 esac
2727 _git_tag ()
2729 local i c=1 f=0
2730 while [ $c -lt $cword ]; do
2731 i="${words[c]}"
2732 case "$i" in
2733 -d|--delete|-v|--verify)
2734 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
2735 return
2740 esac
2741 ((c++))
2742 done
2744 case "$prev" in
2745 -m|-F)
2747 -*|tag)
2748 if [ $f = 1 ]; then
2749 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
2753 __git_complete_refs
2755 esac
2757 case "$cur" in
2758 --*)
2759 __gitcomp_builtin tag
2761 esac
2764 _git_whatchanged ()
2766 _git_log
2769 _git_worktree ()
2771 local subcommands="add list lock move prune remove unlock"
2772 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2773 if [ -z "$subcommand" ]; then
2774 __gitcomp "$subcommands"
2775 else
2776 case "$subcommand,$cur" in
2777 add,--*)
2778 __gitcomp_builtin worktree_add
2780 list,--*)
2781 __gitcomp_builtin worktree_list
2783 lock,--*)
2784 __gitcomp_builtin worktree_lock
2786 prune,--*)
2787 __gitcomp_builtin worktree_prune
2789 remove,--*)
2790 __gitcomp "--force"
2794 esac
2798 __git_complete_common () {
2799 local command="$1"
2801 case "$cur" in
2802 --*)
2803 __gitcomp_builtin "$command"
2805 esac
2808 __git_cmds_with_parseopt_helper=
2809 __git_support_parseopt_helper () {
2810 test -n "$__git_cmds_with_parseopt_helper" ||
2811 __git_cmds_with_parseopt_helper="$(__git --list-cmds=parseopt)"
2813 case " $__git_cmds_with_parseopt_helper " in
2814 *" $1 "*)
2815 return 0
2818 return 1
2820 esac
2823 __git_complete_command () {
2824 local command="$1"
2825 local completion_func="_git_${command//-/_}"
2826 if ! declare -f $completion_func >/dev/null 2>/dev/null &&
2827 declare -f _completion_loader >/dev/null 2>/dev/null
2828 then
2829 _completion_loader "git-$command"
2831 if declare -f $completion_func >/dev/null 2>/dev/null
2832 then
2833 $completion_func
2834 return 0
2835 elif __git_support_parseopt_helper "$command"
2836 then
2837 __git_complete_common "$command"
2838 return 0
2839 else
2840 return 1
2844 __git_main ()
2846 local i c=1 command __git_dir __git_repo_path
2847 local __git_C_args C_args_count=0
2849 while [ $c -lt $cword ]; do
2850 i="${words[c]}"
2851 case "$i" in
2852 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
2853 --git-dir) ((c++)) ; __git_dir="${words[c]}" ;;
2854 --bare) __git_dir="." ;;
2855 --help) command="help"; break ;;
2856 -c|--work-tree|--namespace) ((c++)) ;;
2857 -C) __git_C_args[C_args_count++]=-C
2858 ((c++))
2859 __git_C_args[C_args_count++]="${words[c]}"
2861 -*) ;;
2862 *) command="$i"; break ;;
2863 esac
2864 ((c++))
2865 done
2867 if [ -z "$command" ]; then
2868 case "$prev" in
2869 --git-dir|-C|--work-tree)
2870 # these need a path argument, let's fall back to
2871 # Bash filename completion
2872 return
2874 -c|--namespace)
2875 # we don't support completing these options' arguments
2876 return
2878 esac
2879 case "$cur" in
2880 --*) __gitcomp "
2881 --paginate
2882 --no-pager
2883 --git-dir=
2884 --bare
2885 --version
2886 --exec-path
2887 --exec-path=
2888 --html-path
2889 --man-path
2890 --info-path
2891 --work-tree=
2892 --namespace=
2893 --no-replace-objects
2894 --help
2898 if test -n "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
2899 then
2900 __gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
2901 else
2902 __gitcomp "$(git --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config)"
2905 esac
2906 return
2909 __git_complete_command "$command" && return
2911 local expansion=$(__git_aliased_command "$command")
2912 if [ -n "$expansion" ]; then
2913 words[1]=$expansion
2914 __git_complete_command "$expansion"
2918 __gitk_main ()
2920 __git_has_doubledash && return
2922 local __git_repo_path
2923 __git_find_repo_path
2925 local merge=""
2926 if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
2927 merge="--merge"
2929 case "$cur" in
2930 --*)
2931 __gitcomp "
2932 $__git_log_common_options
2933 $__git_log_gitk_options
2934 $merge
2936 return
2938 esac
2939 __git_complete_revlist
2942 if [[ -n ${ZSH_VERSION-} ]] &&
2943 # Don't define these functions when sourced from 'git-completion.zsh',
2944 # it has its own implementations.
2945 [[ -z ${GIT_SOURCING_ZSH_COMPLETION-} ]]; then
2946 echo "WARNING: this script is deprecated, please see git-completion.zsh" 1>&2
2948 autoload -U +X compinit && compinit
2950 __gitcomp ()
2952 emulate -L zsh
2954 local cur_="${3-$cur}"
2956 case "$cur_" in
2957 --*=)
2960 local c IFS=$' \t\n'
2961 local -a array
2962 for c in ${=1}; do
2963 c="$c${4-}"
2964 case $c in
2965 --*=*|*.) ;;
2966 *) c="$c " ;;
2967 esac
2968 array[${#array[@]}+1]="$c"
2969 done
2970 compset -P '*[=:]'
2971 compadd -Q -S '' -p "${2-}" -a -- array && _ret=0
2973 esac
2976 __gitcomp_direct ()
2978 emulate -L zsh
2980 local IFS=$'\n'
2981 compset -P '*[=:]'
2982 compadd -Q -- ${=1} && _ret=0
2985 __gitcomp_nl ()
2987 emulate -L zsh
2989 local IFS=$'\n'
2990 compset -P '*[=:]'
2991 compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
2994 __gitcomp_file_direct ()
2996 emulate -L zsh
2998 local IFS=$'\n'
2999 compset -P '*[=:]'
3000 compadd -Q -f -- ${=1} && _ret=0
3003 __gitcomp_file ()
3005 emulate -L zsh
3007 local IFS=$'\n'
3008 compset -P '*[=:]'
3009 compadd -Q -p "${2-}" -f -- ${=1} && _ret=0
3012 _git ()
3014 local _ret=1 cur cword prev
3015 cur=${words[CURRENT]}
3016 prev=${words[CURRENT-1]}
3017 let cword=CURRENT-1
3018 emulate ksh -c __${service}_main
3019 let _ret && _default && _ret=0
3020 return _ret
3023 compdef _git git gitk
3024 return
3027 __git_func_wrap ()
3029 local cur words cword prev
3030 _get_comp_words_by_ref -n =: cur words cword prev
3034 # Setup completion for certain functions defined above by setting common
3035 # variables and workarounds.
3036 # This is NOT a public function; use at your own risk.
3037 __git_complete ()
3039 local wrapper="__git_wrap${2}"
3040 eval "$wrapper () { __git_func_wrap $2 ; }"
3041 complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
3042 || complete -o default -o nospace -F $wrapper $1
3045 # wrapper for backwards compatibility
3046 _git ()
3048 __git_wrap__git_main
3051 # wrapper for backwards compatibility
3052 _gitk ()
3054 __git_wrap__gitk_main
3057 __git_complete git __git_main
3058 __git_complete gitk __gitk_main
3060 # The following are necessary only for Cygwin, and only are needed
3061 # when the user has tab-completed the executable name and consequently
3062 # included the '.exe' suffix.
3064 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
3065 __git_complete git.exe __git_main