dotfilesClean dotfiles |
git clone git://git.dimitrijedobrota.com/dotfiles.git |
Log | Files | Refs |
git-completion (82153B)
0 # bash/zsh completion support for core Git.
1 #
2 # Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
3 # Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
4 # Distributed under the GNU General Public License, version 2.0.
5 #
6 # The contained completion routines provide support for completing:
7 #
8 # *) local and remote branch names
9 # *) local and remote tag names
10 # *) .git/remotes file names
11 # *) git 'subcommands'
12 # *) git email aliases for git-send-email
13 # *) tree paths within 'ref:path/to/file' expressions
14 # *) file paths within current working directory and index
15 # *) common --long-options
16 #
17 # To use these routines:
18 #
19 # 1) Copy this file to somewhere (e.g. ~/.git-completion.bash).
20 # 2) Add the following line to your .bashrc/.zshrc:
21 # source ~/.git-completion.bash
22 # 3) Consider changing your PS1 to also show the current branch,
23 # see git-prompt.sh for details.
24 #
25 # If you use complex aliases of form '!f() { ... }; f', you can use the null
26 # command ':' as the first command in the function body to declare the desired
27 # completion style. For example '!f() { : git commit ; ... }; f' will
28 # tell the completion to use commit completion. This also works with aliases
29 # of form "!sh -c '...'". For example, "!sh -c ': git commit ; ... '".
30 #
31 # If you have a command that is not part of git, but you would still
32 # like completion, you can use __git_complete:
33 #
34 # __git_complete gl git_log
35 #
36 # Or if it's a main command (i.e. git or gitk):
37 #
38 # __git_complete gk gitk
39 #
40 # Compatible with bash 3.2.57.
41 #
42 # You can set the following environment variables to influence the behavior of
43 # the completion routines:
44 #
45 # GIT_COMPLETION_CHECKOUT_NO_GUESS
46 #
47 # When set to "1", do not include "DWIM" suggestions in git-checkout
48 # and git-switch completion (e.g., completing "foo" when "origin/foo"
49 # exists).
50 #
51 # GIT_COMPLETION_SHOW_ALL_COMMANDS
52 #
53 # When set to "1" suggest all commands, including plumbing commands
54 # which are hidden by default (e.g. "cat-file" on "git ca<TAB>").
55 #
56 # GIT_COMPLETION_SHOW_ALL
57 #
58 # When set to "1" suggest all options, including options which are
59 # typically hidden (e.g. '--allow-empty' for 'git commit').
60 #
61 # GIT_COMPLETION_IGNORE_CASE
62 #
63 # When set, uses for-each-ref '--ignore-case' to find refs that match
64 # case insensitively, even on systems with case sensitive file systems
65 # (e.g., completing tag name "FOO" on "git checkout f<TAB>").
67 case "$COMP_WORDBREAKS" in
68 *:*) : great ;;
69 *) COMP_WORDBREAKS="$COMP_WORDBREAKS:"
70 esac
72 # Discovers the path to the git repository taking any '--git-dir=<path>' and
73 # '-C <path>' options into account and stores it in the $__git_repo_path
74 # variable.
75 __git_find_repo_path ()
76 {
77 if [ -n "${__git_repo_path-}" ]; then
78 # we already know where it is
79 return
80 fi
82 if [ -n "${__git_C_args-}" ]; then
83 __git_repo_path="$(git "${__git_C_args[@]}" \
84 ${__git_dir:+--git-dir="$__git_dir"} \
85 rev-parse --absolute-git-dir 2>/dev/null)"
86 elif [ -n "${__git_dir-}" ]; then
87 test -d "$__git_dir" &&
88 __git_repo_path="$__git_dir"
89 elif [ -n "${GIT_DIR-}" ]; then
90 test -d "$GIT_DIR" &&
91 __git_repo_path="$GIT_DIR"
92 elif [ -d .git ]; then
93 __git_repo_path=.git
94 else
95 __git_repo_path="$(git rev-parse --git-dir 2>/dev/null)"
96 fi
97 }
99 # Deprecated: use __git_find_repo_path() and $__git_repo_path instead
100 # __gitdir accepts 0 or 1 arguments (i.e., location)
101 # returns location of .git repo
102 __gitdir ()
103 {
104 if [ -z "${1-}" ]; then
105 __git_find_repo_path || return 1
106 echo "$__git_repo_path"
107 elif [ -d "$1/.git" ]; then
108 echo "$1/.git"
109 else
110 echo "$1"
111 fi
112 }
114 # Runs git with all the options given as argument, respecting any
115 # '--git-dir=<path>' and '-C <path>' options present on the command line
116 __git ()
117 {
118 git ${__git_C_args:+"${__git_C_args[@]}"} \
119 ${__git_dir:+--git-dir="$__git_dir"} "$@" 2>/dev/null
120 }
122 # Removes backslash escaping, single quotes and double quotes from a word,
123 # stores the result in the variable $dequoted_word.
124 # 1: The word to dequote.
125 __git_dequote ()
126 {
127 local rest="$1" len ch
129 dequoted_word=""
131 while test -n "$rest"; do
132 len=${#dequoted_word}
133 dequoted_word="$dequoted_word${rest%%[\\\'\"]*}"
134 rest="${rest:$((${#dequoted_word}-$len))}"
136 case "${rest:0:1}" in
137 \\)
138 ch="${rest:1:1}"
139 case "$ch" in
140 $'\n')
141 ;;
142 *)
143 dequoted_word="$dequoted_word$ch"
144 ;;
145 esac
146 rest="${rest:2}"
147 ;;
148 \')
149 rest="${rest:1}"
150 len=${#dequoted_word}
151 dequoted_word="$dequoted_word${rest%%\'*}"
152 rest="${rest:$((${#dequoted_word}-$len+1))}"
153 ;;
154 \")
155 rest="${rest:1}"
156 while test -n "$rest" ; do
157 len=${#dequoted_word}
158 dequoted_word="$dequoted_word${rest%%[\\\"]*}"
159 rest="${rest:$((${#dequoted_word}-$len))}"
160 case "${rest:0:1}" in
161 \\)
162 ch="${rest:1:1}"
163 case "$ch" in
164 \"|\\|\$|\`)
165 dequoted_word="$dequoted_word$ch"
166 ;;
167 $'\n')
168 ;;
169 *)
170 dequoted_word="$dequoted_word\\$ch"
171 ;;
172 esac
173 rest="${rest:2}"
174 ;;
175 \")
176 rest="${rest:1}"
177 break
178 ;;
179 esac
180 done
181 ;;
182 esac
183 done
184 }
186 # The following function is based on code from:
187 #
188 # bash_completion - programmable completion functions for bash 3.2+
189 #
190 # Copyright © 2006-2008, Ian Macdonald <ian@caliban.org>
191 # © 2009-2010, Bash Completion Maintainers
192 # <bash-completion-devel@lists.alioth.debian.org>
193 #
194 # This program is free software; you can redistribute it and/or modify
195 # it under the terms of the GNU General Public License as published by
196 # the Free Software Foundation; either version 2, or (at your option)
197 # any later version.
198 #
199 # This program is distributed in the hope that it will be useful,
200 # but WITHOUT ANY WARRANTY; without even the implied warranty of
201 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
202 # GNU General Public License for more details.
203 #
204 # You should have received a copy of the GNU General Public License
205 # along with this program; if not, see <http://www.gnu.org/licenses/>.
206 #
207 # The latest version of this software can be obtained here:
208 #
209 # http://bash-completion.alioth.debian.org/
210 #
211 # RELEASE: 2.x
213 # This function can be used to access a tokenized list of words
214 # on the command line:
215 #
216 # __git_reassemble_comp_words_by_ref '=:'
217 # if test "${words_[cword_-1]}" = -w
218 # then
219 # ...
220 # fi
221 #
222 # The argument should be a collection of characters from the list of
223 # word completion separators (COMP_WORDBREAKS) to treat as ordinary
224 # characters.
225 #
226 # This is roughly equivalent to going back in time and setting
227 # COMP_WORDBREAKS to exclude those characters. The intent is to
228 # make option types like --date=<type> and <rev>:<path> easy to
229 # recognize by treating each shell word as a single token.
230 #
231 # It is best not to set COMP_WORDBREAKS directly because the value is
232 # shared with other completion scripts. By the time the completion
233 # function gets called, COMP_WORDS has already been populated so local
234 # changes to COMP_WORDBREAKS have no effect.
235 #
236 # Output: words_, cword_, cur_.
238 __git_reassemble_comp_words_by_ref()
239 {
240 local exclude i j first
241 # Which word separators to exclude?
242 exclude="${1//[^$COMP_WORDBREAKS]}"
243 cword_=$COMP_CWORD
244 if [ -z "$exclude" ]; then
245 words_=("${COMP_WORDS[@]}")
246 return
247 fi
248 # List of word completion separators has shrunk;
249 # re-assemble words to complete.
250 for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
251 # Append each nonempty word consisting of just
252 # word separator characters to the current word.
253 first=t
254 while
255 [ $i -gt 0 ] &&
256 [ -n "${COMP_WORDS[$i]}" ] &&
257 # word consists of excluded word separators
258 [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
259 do
260 # Attach to the previous token,
261 # unless the previous token is the command name.
262 if [ $j -ge 2 ] && [ -n "$first" ]; then
263 ((j--))
264 fi
265 first=
266 words_[$j]=${words_[j]}${COMP_WORDS[i]}
267 if [ $i = $COMP_CWORD ]; then
268 cword_=$j
269 fi
270 if (($i < ${#COMP_WORDS[@]} - 1)); then
271 ((i++))
272 else
273 # Done.
274 return
275 fi
276 done
277 words_[$j]=${words_[j]}${COMP_WORDS[i]}
278 if [ $i = $COMP_CWORD ]; then
279 cword_=$j
280 fi
281 done
282 }
284 if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
285 _get_comp_words_by_ref ()
286 {
287 local exclude cur_ words_ cword_
288 if [ "$1" = "-n" ]; then
289 exclude=$2
290 shift 2
291 fi
292 __git_reassemble_comp_words_by_ref "$exclude"
293 cur_=${words_[cword_]}
294 while [ $# -gt 0 ]; do
295 case "$1" in
296 cur)
297 cur=$cur_
298 ;;
299 prev)
300 prev=${words_[$cword_-1]}
301 ;;
302 words)
303 words=("${words_[@]}")
304 ;;
305 cword)
306 cword=$cword_
307 ;;
308 esac
309 shift
310 done
311 }
312 fi
314 # Fills the COMPREPLY array with prefiltered words without any additional
315 # processing.
316 # Callers must take care of providing only words that match the current word
317 # to be completed and adding any prefix and/or suffix (trailing space!), if
318 # necessary.
319 # 1: List of newline-separated matching completion words, complete with
320 # prefix and suffix.
321 __gitcomp_direct ()
322 {
323 local IFS=$'\n'
325 COMPREPLY=($1)
326 }
328 # Similar to __gitcomp_direct, but appends to COMPREPLY instead.
329 # Callers must take care of providing only words that match the current word
330 # to be completed and adding any prefix and/or suffix (trailing space!), if
331 # necessary.
332 # 1: List of newline-separated matching completion words, complete with
333 # prefix and suffix.
334 __gitcomp_direct_append ()
335 {
336 local IFS=$'\n'
338 COMPREPLY+=($1)
339 }
341 __gitcompappend ()
342 {
343 local x i=${#COMPREPLY[@]}
344 for x in $1; do
345 if [[ "$x" == "$3"* ]]; then
346 COMPREPLY[i++]="$2$x$4"
347 fi
348 done
349 }
351 __gitcompadd ()
352 {
353 COMPREPLY=()
354 __gitcompappend "$@"
355 }
357 # Generates completion reply, appending a space to possible completion words,
358 # if necessary.
359 # It accepts 1 to 4 arguments:
360 # 1: List of possible completion words.
361 # 2: A prefix to be added to each possible completion word (optional).
362 # 3: Generate possible completion matches for this word (optional).
363 # 4: A suffix to be appended to each possible completion word (optional).
364 __gitcomp ()
365 {
366 local cur_="${3-$cur}"
368 case "$cur_" in
369 *=)
370 ;;
371 --no-*)
372 local c i=0 IFS=$' \t\n'
373 for c in $1; do
374 if [[ $c == "--" ]]; then
375 continue
376 fi
377 c="$c${4-}"
378 if [[ $c == "$cur_"* ]]; then
379 case $c in
380 --*=|*.) ;;
381 *) c="$c " ;;
382 esac
383 COMPREPLY[i++]="${2-}$c"
384 fi
385 done
386 ;;
387 *)
388 local c i=0 IFS=$' \t\n'
389 for c in $1; do
390 if [[ $c == "--" ]]; then
391 c="--no-...${4-}"
392 if [[ $c == "$cur_"* ]]; then
393 COMPREPLY[i++]="${2-}$c "
394 fi
395 break
396 fi
397 c="$c${4-}"
398 if [[ $c == "$cur_"* ]]; then
399 case $c in
400 *=|*.) ;;
401 *) c="$c " ;;
402 esac
403 COMPREPLY[i++]="${2-}$c"
404 fi
405 done
406 ;;
407 esac
408 }
410 # Clear the variables caching builtins' options when (re-)sourcing
411 # the completion script.
412 if [[ -n ${ZSH_VERSION-} ]]; then
413 unset ${(M)${(k)parameters[@]}:#__gitcomp_builtin_*} 2>/dev/null
414 else
415 unset $(compgen -v __gitcomp_builtin_)
416 fi
418 # This function is equivalent to
419 #
420 # __gitcomp "$(git xxx --git-completion-helper) ..."
421 #
422 # except that the output is cached. Accept 1-3 arguments:
423 # 1: the git command to execute, this is also the cache key
424 # 2: extra options to be added on top (e.g. negative forms)
425 # 3: options to be excluded
426 __gitcomp_builtin ()
427 {
428 # spaces must be replaced with underscore for multi-word
429 # commands, e.g. "git remote add" becomes remote_add.
430 local cmd="$1"
431 local incl="${2-}"
432 local excl="${3-}"
434 local var=__gitcomp_builtin_"${cmd//-/_}"
435 local options
436 eval "options=\${$var-}"
438 if [ -z "$options" ]; then
439 local completion_helper
440 if [ "${GIT_COMPLETION_SHOW_ALL-}" = "1" ]; then
441 completion_helper="--git-completion-helper-all"
442 else
443 completion_helper="--git-completion-helper"
444 fi
445 # leading and trailing spaces are significant to make
446 # option removal work correctly.
447 options=" $incl $(__git ${cmd/_/ } $completion_helper) " || return
449 for i in $excl; do
450 options="${options/ $i / }"
451 done
452 eval "$var=\"$options\""
453 fi
455 __gitcomp "$options"
456 }
458 # Variation of __gitcomp_nl () that appends to the existing list of
459 # completion candidates, COMPREPLY.
460 __gitcomp_nl_append ()
461 {
462 local IFS=$'\n'
463 __gitcompappend "$1" "${2-}" "${3-$cur}" "${4- }"
464 }
466 # Generates completion reply from newline-separated possible completion words
467 # by appending a space to all of them.
468 # It accepts 1 to 4 arguments:
469 # 1: List of possible completion words, separated by a single newline.
470 # 2: A prefix to be added to each possible completion word (optional).
471 # 3: Generate possible completion matches for this word (optional).
472 # 4: A suffix to be appended to each possible completion word instead of
473 # the default space (optional). If specified but empty, nothing is
474 # appended.
475 __gitcomp_nl ()
476 {
477 COMPREPLY=()
478 __gitcomp_nl_append "$@"
479 }
481 # Fills the COMPREPLY array with prefiltered paths without any additional
482 # processing.
483 # Callers must take care of providing only paths that match the current path
484 # to be completed and adding any prefix path components, if necessary.
485 # 1: List of newline-separated matching paths, complete with all prefix
486 # path components.
487 __gitcomp_file_direct ()
488 {
489 local IFS=$'\n'
491 COMPREPLY=($1)
493 # use a hack to enable file mode in bash < 4
494 compopt -o filenames +o nospace 2>/dev/null ||
495 compgen -f /non-existing-dir/ >/dev/null ||
496 true
497 }
499 # Generates completion reply with compgen from newline-separated possible
500 # completion filenames.
501 # It accepts 1 to 3 arguments:
502 # 1: List of possible completion filenames, separated by a single newline.
503 # 2: A directory prefix to be added to each possible completion filename
504 # (optional).
505 # 3: Generate possible completion matches for this word (optional).
506 __gitcomp_file ()
507 {
508 local IFS=$'\n'
510 # XXX does not work when the directory prefix contains a tilde,
511 # since tilde expansion is not applied.
512 # This means that COMPREPLY will be empty and Bash default
513 # completion will be used.
514 __gitcompadd "$1" "${2-}" "${3-$cur}" ""
516 # use a hack to enable file mode in bash < 4
517 compopt -o filenames +o nospace 2>/dev/null ||
518 compgen -f /non-existing-dir/ >/dev/null ||
519 true
520 }
522 # Execute 'git ls-files', unless the --committable option is specified, in
523 # which case it runs 'git diff-index' to find out the files that can be
524 # committed. It return paths relative to the directory specified in the first
525 # argument, and using the options specified in the second argument.
526 __git_ls_files_helper ()
527 {
528 if [ "$2" = "--committable" ]; then
529 __git -C "$1" -c core.quotePath=false diff-index \
530 --name-only --relative HEAD -- "${3//\\/\\\\}*"
531 else
532 # NOTE: $2 is not quoted in order to support multiple options
533 __git -C "$1" -c core.quotePath=false ls-files \
534 --exclude-standard $2 -- "${3//\\/\\\\}*"
535 fi
536 }
539 # __git_index_files accepts 1 or 2 arguments:
540 # 1: Options to pass to ls-files (required).
541 # 2: A directory path (optional).
542 # If provided, only files within the specified directory are listed.
543 # Sub directories are never recursed. Path must have a trailing
544 # slash.
545 # 3: List only paths matching this path component (optional).
546 __git_index_files ()
547 {
548 local root="$2" match="$3"
550 __git_ls_files_helper "$root" "$1" "${match:-?}" |
551 awk -F / -v pfx="${2//\\/\\\\}" '{
552 paths[$1] = 1
553 }
554 END {
555 for (p in paths) {
556 if (substr(p, 1, 1) != "\"") {
557 # No special characters, easy!
558 print pfx p
559 continue
560 }
562 # The path is quoted.
563 p = dequote(p)
564 if (p == "")
565 continue
567 # Even when a directory name itself does not contain
568 # any special characters, it will still be quoted if
569 # any of its (stripped) trailing path components do.
570 # Because of this we may have seen the same directory
571 # both quoted and unquoted.
572 if (p in paths)
573 # We have seen the same directory unquoted,
574 # skip it.
575 continue
576 else
577 print pfx p
578 }
579 }
580 function dequote(p, bs_idx, out, esc, esc_idx, dec) {
581 # Skip opening double quote.
582 p = substr(p, 2)
584 # Interpret backslash escape sequences.
585 while ((bs_idx = index(p, "\\")) != 0) {
586 out = out substr(p, 1, bs_idx - 1)
587 esc = substr(p, bs_idx + 1, 1)
588 p = substr(p, bs_idx + 2)
590 if ((esc_idx = index("abtvfr\"\\", esc)) != 0) {
591 # C-style one-character escape sequence.
592 out = out substr("\a\b\t\v\f\r\"\\",
593 esc_idx, 1)
594 } else if (esc == "n") {
595 # Uh-oh, a newline character.
596 # We cannot reliably put a pathname
597 # containing a newline into COMPREPLY,
598 # and the newline would create a mess.
599 # Skip this path.
600 return ""
601 } else {
602 # Must be a \nnn octal value, then.
603 dec = esc * 64 + \
604 substr(p, 1, 1) * 8 + \
605 substr(p, 2, 1)
606 out = out sprintf("%c", dec)
607 p = substr(p, 3)
608 }
609 }
610 # Drop closing double quote, if there is one.
611 # (There is not any if this is a directory, as it was
612 # already stripped with the trailing path components.)
613 if (substr(p, length(p), 1) == "\"")
614 out = out substr(p, 1, length(p) - 1)
615 else
616 out = out p
618 return out
619 }'
620 }
622 # __git_complete_index_file requires 1 argument:
623 # 1: the options to pass to ls-file
624 #
625 # The exception is --committable, which finds the files appropriate commit.
626 __git_complete_index_file ()
627 {
628 local dequoted_word pfx="" cur_
630 __git_dequote "$cur"
632 case "$dequoted_word" in
633 ?*/*)
634 pfx="${dequoted_word%/*}/"
635 cur_="${dequoted_word##*/}"
636 ;;
637 *)
638 cur_="$dequoted_word"
639 esac
641 __gitcomp_file_direct "$(__git_index_files "$1" "$pfx" "$cur_")"
642 }
644 # Lists branches from the local repository.
645 # 1: A prefix to be added to each listed branch (optional).
646 # 2: List only branches matching this word (optional; list all branches if
647 # unset or empty).
648 # 3: A suffix to be appended to each listed branch (optional).
649 __git_heads ()
650 {
651 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
653 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
654 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
655 "refs/heads/$cur_*" "refs/heads/$cur_*/**"
656 }
658 # Lists branches from remote repositories.
659 # 1: A prefix to be added to each listed branch (optional).
660 # 2: List only branches matching this word (optional; list all branches if
661 # unset or empty).
662 # 3: A suffix to be appended to each listed branch (optional).
663 __git_remote_heads ()
664 {
665 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
667 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
668 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
669 "refs/remotes/$cur_*" "refs/remotes/$cur_*/**"
670 }
672 # Lists tags from the local repository.
673 # Accepts the same positional parameters as __git_heads() above.
674 __git_tags ()
675 {
676 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
678 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
679 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
680 "refs/tags/$cur_*" "refs/tags/$cur_*/**"
681 }
683 # List unique branches from refs/remotes used for 'git checkout' and 'git
684 # switch' tracking DWIMery.
685 # 1: A prefix to be added to each listed branch (optional)
686 # 2: List only branches matching this word (optional; list all branches if
687 # unset or empty).
688 # 3: A suffix to be appended to each listed branch (optional).
689 __git_dwim_remote_heads ()
690 {
691 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
692 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
694 # employ the heuristic used by git checkout and git switch
695 # Try to find a remote branch that cur_es the completion word
696 # but only output if the branch name is unique
697 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
698 --sort="refname:strip=3" \
699 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
700 "refs/remotes/*/$cur_*" "refs/remotes/*/$cur_*/**" | \
701 uniq -u
702 }
704 # Lists refs from the local (by default) or from a remote repository.
705 # It accepts 0, 1 or 2 arguments:
706 # 1: The remote to list refs from (optional; ignored, if set but empty).
707 # Can be the name of a configured remote, a path, or a URL.
708 # 2: In addition to local refs, list unique branches from refs/remotes/ for
709 # 'git checkout's tracking DWIMery (optional; ignored, if set but empty).
710 # 3: A prefix to be added to each listed ref (optional).
711 # 4: List only refs matching this word (optional; list all refs if unset or
712 # empty).
713 # 5: A suffix to be appended to each listed ref (optional; ignored, if set
714 # but empty).
715 #
716 # Use __git_complete_refs() instead.
717 __git_refs ()
718 {
719 local i hash dir track="${2-}"
720 local list_refs_from=path remote="${1-}"
721 local format refs
722 local pfx="${3-}" cur_="${4-$cur}" sfx="${5-}"
723 local match="${4-}"
724 local umatch="${4-}"
725 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
727 __git_find_repo_path
728 dir="$__git_repo_path"
730 if [ -z "$remote" ]; then
731 if [ -z "$dir" ]; then
732 return
733 fi
734 else
735 if __git_is_configured_remote "$remote"; then
736 # configured remote takes precedence over a
737 # local directory with the same name
738 list_refs_from=remote
739 elif [ -d "$remote/.git" ]; then
740 dir="$remote/.git"
741 elif [ -d "$remote" ]; then
742 dir="$remote"
743 else
744 list_refs_from=url
745 fi
746 fi
748 if test "${GIT_COMPLETION_IGNORE_CASE:+1}" = "1"
749 then
750 # uppercase with tr instead of ${match,^^} for bash 3.2 compatibility
751 umatch=$(echo "$match" | tr a-z A-Z 2>/dev/null || echo "$match")
752 fi
754 if [ "$list_refs_from" = path ]; then
755 if [[ "$cur_" == ^* ]]; then
756 pfx="$pfx^"
757 fer_pfx="$fer_pfx^"
758 cur_=${cur_#^}
759 match=${match#^}
760 umatch=${umatch#^}
761 fi
762 case "$cur_" in
763 refs|refs/*)
764 format="refname"
765 refs=("$match*" "$match*/**")
766 track=""
767 ;;
768 *)
769 for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD REBASE_HEAD CHERRY_PICK_HEAD REVERT_HEAD BISECT_HEAD AUTO_MERGE; do
770 case "$i" in
771 $match*|$umatch*)
772 if [ -e "$dir/$i" ]; then
773 echo "$pfx$i$sfx"
774 fi
775 ;;
776 esac
777 done
778 format="refname:strip=2"
779 refs=("refs/tags/$match*" "refs/tags/$match*/**"
780 "refs/heads/$match*" "refs/heads/$match*/**"
781 "refs/remotes/$match*" "refs/remotes/$match*/**")
782 ;;
783 esac
784 __git_dir="$dir" __git for-each-ref --format="$fer_pfx%($format)$sfx" \
785 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
786 "${refs[@]}"
787 if [ -n "$track" ]; then
788 __git_dwim_remote_heads "$pfx" "$match" "$sfx"
789 fi
790 return
791 fi
792 case "$cur_" in
793 refs|refs/*)
794 __git ls-remote "$remote" "$match*" | \
795 while read -r hash i; do
796 case "$i" in
797 *^{}) ;;
798 *) echo "$pfx$i$sfx" ;;
799 esac
800 done
801 ;;
802 *)
803 if [ "$list_refs_from" = remote ]; then
804 case "HEAD" in
805 $match*|$umatch*) echo "${pfx}HEAD$sfx" ;;
806 esac
807 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
808 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
809 "refs/remotes/$remote/$match*" \
810 "refs/remotes/$remote/$match*/**"
811 else
812 local query_symref
813 case "HEAD" in
814 $match*|$umatch*) query_symref="HEAD" ;;
815 esac
816 __git ls-remote "$remote" $query_symref \
817 "refs/tags/$match*" "refs/heads/$match*" \
818 "refs/remotes/$match*" |
819 while read -r hash i; do
820 case "$i" in
821 *^{}) ;;
822 refs/*) echo "$pfx${i#refs/*/}$sfx" ;;
823 *) echo "$pfx$i$sfx" ;; # symbolic refs
824 esac
825 done
826 fi
827 ;;
828 esac
829 }
831 # Completes refs, short and long, local and remote, symbolic and pseudo.
832 #
833 # Usage: __git_complete_refs [<option>]...
834 # --remote=<remote>: The remote to list refs from, can be the name of a
835 # configured remote, a path, or a URL.
836 # --dwim: List unique remote branches for 'git switch's tracking DWIMery.
837 # --pfx=<prefix>: A prefix to be added to each ref.
838 # --cur=<word>: The current ref to be completed. Defaults to the current
839 # word to be completed.
840 # --sfx=<suffix>: A suffix to be appended to each ref instead of the default
841 # space.
842 # --mode=<mode>: What set of refs to complete, one of 'refs' (the default) to
843 # complete all refs, 'heads' to complete only branches, or
844 # 'remote-heads' to complete only remote branches. Note that
845 # --remote is only compatible with --mode=refs.
846 __git_complete_refs ()
847 {
848 local remote= dwim= pfx= cur_="$cur" sfx=" " mode="refs"
850 while test $# != 0; do
851 case "$1" in
852 --remote=*) remote="${1##--remote=}" ;;
853 --dwim) dwim="yes" ;;
854 # --track is an old spelling of --dwim
855 --track) dwim="yes" ;;
856 --pfx=*) pfx="${1##--pfx=}" ;;
857 --cur=*) cur_="${1##--cur=}" ;;
858 --sfx=*) sfx="${1##--sfx=}" ;;
859 --mode=*) mode="${1##--mode=}" ;;
860 *) return 1 ;;
861 esac
862 shift
863 done
865 # complete references based on the specified mode
866 case "$mode" in
867 refs)
868 __gitcomp_direct "$(__git_refs "$remote" "" "$pfx" "$cur_" "$sfx")" ;;
869 heads)
870 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" "$sfx")" ;;
871 remote-heads)
872 __gitcomp_direct "$(__git_remote_heads "$pfx" "$cur_" "$sfx")" ;;
873 *)
874 return 1 ;;
875 esac
877 # Append DWIM remote branch names if requested
878 if [ "$dwim" = "yes" ]; then
879 __gitcomp_direct_append "$(__git_dwim_remote_heads "$pfx" "$cur_" "$sfx")"
880 fi
881 }
883 # __git_refs2 requires 1 argument (to pass to __git_refs)
884 # Deprecated: use __git_complete_fetch_refspecs() instead.
885 __git_refs2 ()
886 {
887 local i
888 for i in $(__git_refs "$1"); do
889 echo "$i:$i"
890 done
891 }
893 # Completes refspecs for fetching from a remote repository.
894 # 1: The remote repository.
895 # 2: A prefix to be added to each listed refspec (optional).
896 # 3: The ref to be completed as a refspec instead of the current word to be
897 # completed (optional)
898 # 4: A suffix to be appended to each listed refspec instead of the default
899 # space (optional).
900 __git_complete_fetch_refspecs ()
901 {
902 local i remote="$1" pfx="${2-}" cur_="${3-$cur}" sfx="${4- }"
904 __gitcomp_direct "$(
905 for i in $(__git_refs "$remote" "" "" "$cur_") ; do
906 echo "$pfx$i:$i$sfx"
907 done
908 )"
909 }
911 # __git_refs_remotes requires 1 argument (to pass to ls-remote)
912 __git_refs_remotes ()
913 {
914 local i hash
915 __git ls-remote "$1" 'refs/heads/*' | \
916 while read -r hash i; do
917 echo "$i:refs/remotes/$1/${i#refs/heads/}"
918 done
919 }
921 __git_remotes ()
922 {
923 __git_find_repo_path
924 test -d "$__git_repo_path/remotes" && ls -1 "$__git_repo_path/remotes"
925 __git remote
926 }
928 # Returns true if $1 matches the name of a configured remote, false otherwise.
929 __git_is_configured_remote ()
930 {
931 local remote
932 for remote in $(__git_remotes); do
933 if [ "$remote" = "$1" ]; then
934 return 0
935 fi
936 done
937 return 1
938 }
940 __git_list_merge_strategies ()
941 {
942 LANG=C LC_ALL=C git merge -s help 2>&1 |
943 sed -n -e '/[Aa]vailable strategies are: /,/^$/{
944 s/\.$//
945 s/.*://
946 s/^[ ]*//
947 s/[ ]*$//
948 p
949 }'
950 }
952 __git_merge_strategies=
953 # 'git merge -s help' (and thus detection of the merge strategy
954 # list) fails, unfortunately, if run outside of any git working
955 # tree. __git_merge_strategies is set to the empty string in
956 # that case, and the detection will be repeated the next time it
957 # is needed.
958 __git_compute_merge_strategies ()
959 {
960 test -n "$__git_merge_strategies" ||
961 __git_merge_strategies=$(__git_list_merge_strategies)
962 }
964 __git_merge_strategy_options="ours theirs subtree subtree= patience
965 histogram diff-algorithm= ignore-space-change ignore-all-space
966 ignore-space-at-eol renormalize no-renormalize no-renames
967 find-renames find-renames= rename-threshold="
969 __git_complete_revlist_file ()
970 {
971 local dequoted_word pfx ls ref cur_="$cur"
972 case "$cur_" in
973 *..?*:*)
974 return
975 ;;
976 ?*:*)
977 ref="${cur_%%:*}"
978 cur_="${cur_#*:}"
980 __git_dequote "$cur_"
982 case "$dequoted_word" in
983 ?*/*)
984 pfx="${dequoted_word%/*}"
985 cur_="${dequoted_word##*/}"
986 ls="$ref:$pfx"
987 pfx="$pfx/"
988 ;;
989 *)
990 cur_="$dequoted_word"
991 ls="$ref"
992 ;;
993 esac
995 case "$COMP_WORDBREAKS" in
996 *:*) : great ;;
997 *) pfx="$ref:$pfx" ;;
998 esac
1000 __gitcomp_file "$(__git ls-tree "$ls" \
1001 | sed 's/^.* //
1002 s/$//')" \
1003 "$pfx" "$cur_"
1004 ;;
1005 *...*)
1006 pfx="${cur_%...*}..."
1007 cur_="${cur_#*...}"
1008 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1009 ;;
1010 *..*)
1011 pfx="${cur_%..*}.."
1012 cur_="${cur_#*..}"
1013 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1014 ;;
1015 *)
1016 __git_complete_refs
1017 ;;
1018 esac
1019 }
1021 __git_complete_file ()
1022 {
1023 __git_complete_revlist_file
1024 }
1026 __git_complete_revlist ()
1027 {
1028 __git_complete_revlist_file
1029 }
1031 __git_complete_remote_or_refspec ()
1032 {
1033 local cur_="$cur" cmd="${words[__git_cmd_idx]}"
1034 local i c=$((__git_cmd_idx+1)) remote="" pfx="" lhs=1 no_complete_refspec=0
1035 if [ "$cmd" = "remote" ]; then
1036 ((c++))
1037 fi
1038 while [ $c -lt $cword ]; do
1039 i="${words[c]}"
1040 case "$i" in
1041 --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
1042 -d|--delete) [ "$cmd" = "push" ] && lhs=0 ;;
1043 --all)
1044 case "$cmd" in
1045 push) no_complete_refspec=1 ;;
1046 fetch)
1047 return
1048 ;;
1049 *) ;;
1050 esac
1051 ;;
1052 --multiple) no_complete_refspec=1; break ;;
1053 -*) ;;
1054 *) remote="$i"; break ;;
1055 esac
1056 ((c++))
1057 done
1058 if [ -z "$remote" ]; then
1059 __gitcomp_nl "$(__git_remotes)"
1060 return
1061 fi
1062 if [ $no_complete_refspec = 1 ]; then
1063 return
1064 fi
1065 [ "$remote" = "." ] && remote=
1066 case "$cur_" in
1067 *:*)
1068 case "$COMP_WORDBREAKS" in
1069 *:*) : great ;;
1070 *) pfx="${cur_%%:*}:" ;;
1071 esac
1072 cur_="${cur_#*:}"
1073 lhs=0
1074 ;;
1075 +*)
1076 pfx="+"
1077 cur_="${cur_#+}"
1078 ;;
1079 esac
1080 case "$cmd" in
1081 fetch)
1082 if [ $lhs = 1 ]; then
1083 __git_complete_fetch_refspecs "$remote" "$pfx" "$cur_"
1084 else
1085 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1086 fi
1087 ;;
1088 pull|remote)
1089 if [ $lhs = 1 ]; then
1090 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
1091 else
1092 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1093 fi
1094 ;;
1095 push)
1096 if [ $lhs = 1 ]; then
1097 __git_complete_refs --pfx="$pfx" --cur="$cur_"
1098 else
1099 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
1100 fi
1101 ;;
1102 esac
1103 }
1105 __git_complete_strategy ()
1106 {
1107 __git_compute_merge_strategies
1108 case "$prev" in
1109 -s|--strategy)
1110 __gitcomp "$__git_merge_strategies"
1111 return 0
1112 ;;
1113 -X)
1114 __gitcomp "$__git_merge_strategy_options"
1115 return 0
1116 ;;
1117 esac
1118 case "$cur" in
1119 --strategy=*)
1120 __gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
1121 return 0
1122 ;;
1123 --strategy-option=*)
1124 __gitcomp "$__git_merge_strategy_options" "" "${cur##--strategy-option=}"
1125 return 0
1126 ;;
1127 esac
1128 return 1
1129 }
1131 __git_all_commands=
1132 __git_compute_all_commands ()
1133 {
1134 test -n "$__git_all_commands" ||
1135 __git_all_commands=$(__git --list-cmds=main,others,alias,nohelpers)
1136 }
1138 # Lists all set config variables starting with the given section prefix,
1139 # with the prefix removed.
1140 __git_get_config_variables ()
1141 {
1142 local section="$1" i IFS=$'\n'
1143 for i in $(__git config --name-only --get-regexp "^$section\..*"); do
1144 echo "${i#$section.}"
1145 done
1146 }
1148 __git_pretty_aliases ()
1149 {
1150 __git_get_config_variables "pretty"
1151 }
1153 # __git_aliased_command requires 1 argument
1154 __git_aliased_command ()
1155 {
1156 local cur=$1 last list= word cmdline
1158 while [[ -n "$cur" ]]; do
1159 if [[ "$list" == *" $cur "* ]]; then
1160 # loop detected
1161 return
1162 fi
1164 cmdline=$(__git config --get "alias.$cur")
1165 list=" $cur $list"
1166 last=$cur
1167 cur=
1169 for word in $cmdline; do
1170 case "$word" in
1171 \!gitk|gitk)
1172 cur="gitk"
1173 break
1174 ;;
1175 \!*) : shell command alias ;;
1176 -*) : option ;;
1177 *=*) : setting env ;;
1178 git) : git itself ;;
1179 \(\)) : skip parens of shell function definition ;;
1180 {) : skip start of shell helper function ;;
1181 :) : skip null command ;;
1182 \'*) : skip opening quote after sh -c ;;
1183 *)
1184 cur="$word"
1185 break
1186 esac
1187 done
1188 done
1190 cur=$last
1191 if [[ "$cur" != "$1" ]]; then
1192 echo "$cur"
1193 fi
1194 }
1196 # Check whether one of the given words is present on the command line,
1197 # and print the first word found.
1198 #
1199 # Usage: __git_find_on_cmdline [<option>]... "<wordlist>"
1200 # --show-idx: Optionally show the index of the found word in the $words array.
1201 __git_find_on_cmdline ()
1202 {
1203 local word c="$__git_cmd_idx" show_idx
1205 while test $# -gt 1; do
1206 case "$1" in
1207 --show-idx) show_idx=y ;;
1208 *) return 1 ;;
1209 esac
1210 shift
1211 done
1212 local wordlist="$1"
1214 while [ $c -lt $cword ]; do
1215 for word in $wordlist; do
1216 if [ "$word" = "${words[c]}" ]; then
1217 if [ -n "${show_idx-}" ]; then
1218 echo "$c $word"
1219 else
1220 echo "$word"
1221 fi
1222 return
1223 fi
1224 done
1225 ((c++))
1226 done
1227 }
1229 # Similar to __git_find_on_cmdline, except that it loops backwards and thus
1230 # prints the *last* word found. Useful for finding which of two options that
1231 # supersede each other came last, such as "--guess" and "--no-guess".
1232 #
1233 # Usage: __git_find_last_on_cmdline [<option>]... "<wordlist>"
1234 # --show-idx: Optionally show the index of the found word in the $words array.
1235 __git_find_last_on_cmdline ()
1236 {
1237 local word c=$cword show_idx
1239 while test $# -gt 1; do
1240 case "$1" in
1241 --show-idx) show_idx=y ;;
1242 *) return 1 ;;
1243 esac
1244 shift
1245 done
1246 local wordlist="$1"
1248 while [ $c -gt "$__git_cmd_idx" ]; do
1249 ((c--))
1250 for word in $wordlist; do
1251 if [ "$word" = "${words[c]}" ]; then
1252 if [ -n "$show_idx" ]; then
1253 echo "$c $word"
1254 else
1255 echo "$word"
1256 fi
1257 return
1258 fi
1259 done
1260 done
1261 }
1263 # Echo the value of an option set on the command line or config
1264 #
1265 # $1: short option name
1266 # $2: long option name including =
1267 # $3: list of possible values
1268 # $4: config string (optional)
1269 #
1270 # example:
1271 # result="$(__git_get_option_value "-d" "--do-something=" \
1272 # "yes no" "core.doSomething")"
1273 #
1274 # result is then either empty (no option set) or "yes" or "no"
1275 #
1276 # __git_get_option_value requires 3 arguments
1277 __git_get_option_value ()
1278 {
1279 local c short_opt long_opt val
1280 local result= values config_key word
1282 short_opt="$1"
1283 long_opt="$2"
1284 values="$3"
1285 config_key="$4"
1287 ((c = $cword - 1))
1288 while [ $c -ge 0 ]; do
1289 word="${words[c]}"
1290 for val in $values; do
1291 if [ "$short_opt$val" = "$word" ] ||
1292 [ "$long_opt$val" = "$word" ]; then
1293 result="$val"
1294 break 2
1295 fi
1296 done
1297 ((c--))
1298 done
1300 if [ -n "$config_key" ] && [ -z "$result" ]; then
1301 result="$(__git config "$config_key")"
1302 fi
1304 echo "$result"
1305 }
1307 __git_has_doubledash ()
1308 {
1309 local c=1
1310 while [ $c -lt $cword ]; do
1311 if [ "--" = "${words[c]}" ]; then
1312 return 0
1313 fi
1314 ((c++))
1315 done
1316 return 1
1317 }
1319 # Try to count non option arguments passed on the command line for the
1320 # specified git command.
1321 # When options are used, it is necessary to use the special -- option to
1322 # tell the implementation were non option arguments begin.
1323 # XXX this can not be improved, since options can appear everywhere, as
1324 # an example:
1325 # git mv x -n y
1326 #
1327 # __git_count_arguments requires 1 argument: the git command executed.
1328 __git_count_arguments ()
1329 {
1330 local word i c=0
1332 # Skip "git" (first argument)
1333 for ((i=$__git_cmd_idx; i < ${#words[@]}; i++)); do
1334 word="${words[i]}"
1336 case "$word" in
1337 --)
1338 # Good; we can assume that the following are only non
1339 # option arguments.
1340 ((c = 0))
1341 ;;
1342 "$1")
1343 # Skip the specified git command and discard git
1344 # main options
1345 ((c = 0))
1346 ;;
1347 ?*)
1348 ((c++))
1349 ;;
1350 esac
1351 done
1353 printf "%d" $c
1354 }
1356 __git_whitespacelist="nowarn warn error error-all fix"
1357 __git_patchformat="mbox stgit stgit-series hg mboxrd"
1358 __git_showcurrentpatch="diff raw"
1359 __git_am_inprogress_options="--skip --continue --resolved --abort --quit --show-current-patch"
1360 __git_quoted_cr="nowarn warn strip"
1362 _git_am ()
1363 {
1364 __git_find_repo_path
1365 if [ -d "$__git_repo_path"/rebase-apply ]; then
1366 __gitcomp "$__git_am_inprogress_options"
1367 return
1368 fi
1369 case "$cur" in
1370 --whitespace=*)
1371 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1372 return
1373 ;;
1374 --patch-format=*)
1375 __gitcomp "$__git_patchformat" "" "${cur##--patch-format=}"
1376 return
1377 ;;
1378 --show-current-patch=*)
1379 __gitcomp "$__git_showcurrentpatch" "" "${cur##--show-current-patch=}"
1380 return
1381 ;;
1382 --quoted-cr=*)
1383 __gitcomp "$__git_quoted_cr" "" "${cur##--quoted-cr=}"
1384 return
1385 ;;
1386 --*)
1387 __gitcomp_builtin am "" \
1388 "$__git_am_inprogress_options"
1389 return
1390 esac
1391 }
1393 _git_apply ()
1394 {
1395 case "$cur" in
1396 --whitespace=*)
1397 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1398 return
1399 ;;
1400 --*)
1401 __gitcomp_builtin apply
1402 return
1403 esac
1404 }
1406 _git_add ()
1407 {
1408 case "$cur" in
1409 --chmod=*)
1410 __gitcomp "+x -x" "" "${cur##--chmod=}"
1411 return
1412 ;;
1413 --*)
1414 __gitcomp_builtin add
1415 return
1416 esac
1418 local complete_opt="--others --modified --directory --no-empty-directory"
1419 if test -n "$(__git_find_on_cmdline "-u --update")"
1420 then
1421 complete_opt="--modified"
1422 fi
1423 __git_complete_index_file "$complete_opt"
1424 }
1426 _git_archive ()
1427 {
1428 case "$cur" in
1429 --format=*)
1430 __gitcomp "$(git archive --list)" "" "${cur##--format=}"
1431 return
1432 ;;
1433 --remote=*)
1434 __gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
1435 return
1436 ;;
1437 --*)
1438 __gitcomp_builtin archive "--format= --list --verbose --prefix= --worktree-attributes"
1439 return
1440 ;;
1441 esac
1442 __git_complete_file
1443 }
1445 _git_bisect ()
1446 {
1447 __git_has_doubledash && return
1449 local subcommands="start bad good skip reset visualize replay log run"
1450 local subcommand="$(__git_find_on_cmdline "$subcommands")"
1451 if [ -z "$subcommand" ]; then
1452 __git_find_repo_path
1453 if [ -f "$__git_repo_path"/BISECT_START ]; then
1454 __gitcomp "$subcommands"
1455 else
1456 __gitcomp "replay start"
1457 fi
1458 return
1459 fi
1461 case "$subcommand" in
1462 bad|good|reset|skip|start)
1463 __git_complete_refs
1464 ;;
1465 *)
1466 ;;
1467 esac
1468 }
1470 __git_ref_fieldlist="refname objecttype objectsize objectname upstream push HEAD symref"
1472 _git_branch ()
1473 {
1474 local i c="$__git_cmd_idx" only_local_ref="n" has_r="n"
1476 while [ $c -lt $cword ]; do
1477 i="${words[c]}"
1478 case "$i" in
1479 -d|-D|--delete|-m|-M|--move|-c|-C|--copy)
1480 only_local_ref="y" ;;
1481 -r|--remotes)
1482 has_r="y" ;;
1483 esac
1484 ((c++))
1485 done
1487 case "$cur" in
1488 --set-upstream-to=*)
1489 __git_complete_refs --cur="${cur##--set-upstream-to=}"
1490 ;;
1491 --*)
1492 __gitcomp_builtin branch
1493 ;;
1494 *)
1495 if [ $only_local_ref = "y" -a $has_r = "n" ]; then
1496 __gitcomp_direct "$(__git_heads "" "$cur" " ")"
1497 else
1498 __git_complete_refs
1499 fi
1500 ;;
1501 esac
1502 }
1504 _git_bundle ()
1505 {
1506 local cmd="${words[__git_cmd_idx+1]}"
1507 case "$cword" in
1508 $((__git_cmd_idx+1)))
1509 __gitcomp "create list-heads verify unbundle"
1510 ;;
1511 $((__git_cmd_idx+2)))
1512 # looking for a file
1513 ;;
1514 *)
1515 case "$cmd" in
1516 create)
1517 __git_complete_revlist
1518 ;;
1519 esac
1520 ;;
1521 esac
1522 }
1524 # Helper function to decide whether or not we should enable DWIM logic for
1525 # git-switch and git-checkout.
1526 #
1527 # To decide between the following rules in decreasing priority order:
1528 # - the last provided of "--guess" or "--no-guess" explicitly enable or
1529 # disable completion of DWIM logic respectively.
1530 # - If checkout.guess is false, disable completion of DWIM logic.
1531 # - If the --no-track option is provided, take this as a hint to disable the
1532 # DWIM completion logic
1533 # - If GIT_COMPLETION_CHECKOUT_NO_GUESS is set, disable the DWIM completion
1534 # logic, as requested by the user.
1535 # - Enable DWIM logic otherwise.
1536 #
1537 __git_checkout_default_dwim_mode ()
1538 {
1539 local last_option dwim_opt="--dwim"
1541 if [ "${GIT_COMPLETION_CHECKOUT_NO_GUESS-}" = "1" ]; then
1542 dwim_opt=""
1543 fi
1545 # --no-track disables DWIM, but with lower priority than
1546 # --guess/--no-guess/checkout.guess
1547 if [ -n "$(__git_find_on_cmdline "--no-track")" ]; then
1548 dwim_opt=""
1549 fi
1551 # checkout.guess = false disables DWIM, but with lower priority than
1552 # --guess/--no-guess
1553 if [ "$(__git config --type=bool checkout.guess)" = "false" ]; then
1554 dwim_opt=""
1555 fi
1557 # Find the last provided --guess or --no-guess
1558 last_option="$(__git_find_last_on_cmdline "--guess --no-guess")"
1559 case "$last_option" in
1560 --guess)
1561 dwim_opt="--dwim"
1562 ;;
1563 --no-guess)
1564 dwim_opt=""
1565 ;;
1566 esac
1568 echo "$dwim_opt"
1569 }
1571 _git_checkout ()
1572 {
1573 __git_has_doubledash && return
1575 local dwim_opt="$(__git_checkout_default_dwim_mode)"
1577 case "$prev" in
1578 -b|-B|--orphan)
1579 # Complete local branches (and DWIM branch
1580 # remote branch names) for an option argument
1581 # specifying a new branch name. This is for
1582 # convenience, assuming new branches are
1583 # possibly based on pre-existing branch names.
1584 __git_complete_refs $dwim_opt --mode="heads"
1585 return
1586 ;;
1587 *)
1588 ;;
1589 esac
1591 case "$cur" in
1592 --conflict=*)
1593 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
1594 ;;
1595 --*)
1596 __gitcomp_builtin checkout
1597 ;;
1598 *)
1599 # At this point, we've already handled special completion for
1600 # the arguments to -b/-B, and --orphan. There are 3 main
1601 # things left we can possibly complete:
1602 # 1) a start-point for -b/-B, -d/--detach, or --orphan
1603 # 2) a remote head, for --track
1604 # 3) an arbitrary reference, possibly including DWIM names
1605 #
1607 if [ -n "$(__git_find_on_cmdline "-b -B -d --detach --orphan")" ]; then
1608 __git_complete_refs --mode="refs"
1609 elif [ -n "$(__git_find_on_cmdline "--track")" ]; then
1610 __git_complete_refs --mode="remote-heads"
1611 else
1612 __git_complete_refs $dwim_opt --mode="refs"
1613 fi
1614 ;;
1615 esac
1616 }
1618 __git_sequencer_inprogress_options="--continue --quit --abort --skip"
1620 __git_cherry_pick_inprogress_options=$__git_sequencer_inprogress_options
1622 _git_cherry_pick ()
1623 {
1624 __git_find_repo_path
1625 if [ -f "$__git_repo_path"/CHERRY_PICK_HEAD ]; then
1626 __gitcomp "$__git_cherry_pick_inprogress_options"
1627 return
1628 fi
1630 __git_complete_strategy && return
1632 case "$cur" in
1633 --*)
1634 __gitcomp_builtin cherry-pick "" \
1635 "$__git_cherry_pick_inprogress_options"
1636 ;;
1637 *)
1638 __git_complete_refs
1639 ;;
1640 esac
1641 }
1643 _git_clean ()
1644 {
1645 case "$cur" in
1646 --*)
1647 __gitcomp_builtin clean
1648 return
1649 ;;
1650 esac
1652 # XXX should we check for -x option ?
1653 __git_complete_index_file "--others --directory"
1654 }
1656 _git_clone ()
1657 {
1658 case "$prev" in
1659 -c|--config)
1660 __git_complete_config_variable_name_and_value
1661 return
1662 ;;
1663 esac
1664 case "$cur" in
1665 --config=*)
1666 __git_complete_config_variable_name_and_value \
1667 --cur="${cur##--config=}"
1668 return
1669 ;;
1670 --*)
1671 __gitcomp_builtin clone
1672 return
1673 ;;
1674 esac
1675 }
1677 __git_untracked_file_modes="all no normal"
1679 _git_commit ()
1680 {
1681 case "$prev" in
1682 -c|-C)
1683 __git_complete_refs
1684 return
1685 ;;
1686 esac
1688 case "$cur" in
1689 --cleanup=*)
1690 __gitcomp "default scissors strip verbatim whitespace
1691 " "" "${cur##--cleanup=}"
1692 return
1693 ;;
1694 --reuse-message=*|--reedit-message=*|\
1695 --fixup=*|--squash=*)
1696 __git_complete_refs --cur="${cur#*=}"
1697 return
1698 ;;
1699 --untracked-files=*)
1700 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
1701 return
1702 ;;
1703 --*)
1704 __gitcomp_builtin commit
1705 return
1706 esac
1708 if __git rev-parse --verify --quiet HEAD >/dev/null; then
1709 __git_complete_index_file "--committable"
1710 else
1711 # This is the first commit
1712 __git_complete_index_file "--cached"
1713 fi
1714 }
1716 _git_describe ()
1717 {
1718 case "$cur" in
1719 --*)
1720 __gitcomp_builtin describe
1721 return
1722 esac
1723 __git_complete_refs
1724 }
1726 __git_diff_algorithms="myers minimal patience histogram"
1728 __git_diff_submodule_formats="diff log short"
1730 __git_color_moved_opts="no default plain blocks zebra dimmed-zebra"
1732 __git_color_moved_ws_opts="no ignore-space-at-eol ignore-space-change
1733 ignore-all-space allow-indentation-change"
1735 __git_ws_error_highlight_opts="context old new all default"
1737 # Options for the diff machinery (diff, log, show, stash, range-diff, ...)
1738 __git_diff_common_options="--stat --numstat --shortstat --summary
1739 --patch-with-stat --name-only --name-status --color
1740 --no-color --color-words --no-renames --check
1741 --color-moved --color-moved= --no-color-moved
1742 --color-moved-ws= --no-color-moved-ws
1743 --full-index --binary --abbrev --diff-filter=
1744 --find-copies --find-object --find-renames
1745 --no-relative --relative
1746 --find-copies-harder --ignore-cr-at-eol
1747 --text --ignore-space-at-eol --ignore-space-change
1748 --ignore-all-space --ignore-blank-lines --exit-code
1749 --quiet --ext-diff --no-ext-diff --unified=
1750 --no-prefix --src-prefix= --dst-prefix=
1751 --inter-hunk-context= --function-context
1752 --patience --histogram --minimal
1753 --raw --word-diff --word-diff-regex=
1754 --dirstat --dirstat= --dirstat-by-file
1755 --dirstat-by-file= --cumulative
1756 --diff-algorithm= --default-prefix
1757 --submodule --submodule= --ignore-submodules
1758 --indent-heuristic --no-indent-heuristic
1759 --textconv --no-textconv --break-rewrites
1760 --patch --no-patch --cc --combined-all-paths
1761 --anchored= --compact-summary --ignore-matching-lines=
1762 --irreversible-delete --line-prefix --no-stat
1763 --output= --output-indicator-context=
1764 --output-indicator-new= --output-indicator-old=
1765 --ws-error-highlight=
1766 --pickaxe-all --pickaxe-regex
1767 "
1769 # Options for diff/difftool
1770 __git_diff_difftool_options="--cached --staged
1771 --base --ours --theirs --no-index --merge-base
1772 --ita-invisible-in-index --ita-visible-in-index
1773 $__git_diff_common_options"
1775 _git_diff ()
1776 {
1777 __git_has_doubledash && return
1779 case "$cur" in
1780 --diff-algorithm=*)
1781 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1782 return
1783 ;;
1784 --submodule=*)
1785 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1786 return
1787 ;;
1788 --color-moved=*)
1789 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
1790 return
1791 ;;
1792 --color-moved-ws=*)
1793 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
1794 return
1795 ;;
1796 --ws-error-highlight=*)
1797 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
1798 return
1799 ;;
1800 --*)
1801 __gitcomp "$__git_diff_difftool_options"
1802 return
1803 ;;
1804 esac
1805 __git_complete_revlist_file
1806 }
1808 __git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff
1809 tkdiff vimdiff nvimdiff gvimdiff xxdiff araxis p4merge
1810 bc codecompare smerge
1811 "
1813 _git_difftool ()
1814 {
1815 __git_has_doubledash && return
1817 case "$cur" in
1818 --tool=*)
1819 __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1820 return
1821 ;;
1822 --*)
1823 __gitcomp_builtin difftool "$__git_diff_difftool_options"
1824 return
1825 ;;
1826 esac
1827 __git_complete_revlist_file
1828 }
1830 __git_fetch_recurse_submodules="yes on-demand no"
1832 _git_fetch ()
1833 {
1834 case "$cur" in
1835 --recurse-submodules=*)
1836 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1837 return
1838 ;;
1839 --filter=*)
1840 __gitcomp "blob:none blob:limit= sparse:oid=" "" "${cur##--filter=}"
1841 return
1842 ;;
1843 --*)
1844 __gitcomp_builtin fetch
1845 return
1846 ;;
1847 esac
1848 __git_complete_remote_or_refspec
1849 }
1851 __git_format_patch_extra_options="
1852 --full-index --not --all --no-prefix --src-prefix=
1853 --dst-prefix= --notes
1854 "
1856 _git_format_patch ()
1857 {
1858 case "$cur" in
1859 --thread=*)
1860 __gitcomp "
1861 deep shallow
1862 " "" "${cur##--thread=}"
1863 return
1864 ;;
1865 --base=*|--interdiff=*|--range-diff=*)
1866 __git_complete_refs --cur="${cur#--*=}"
1867 return
1868 ;;
1869 --*)
1870 __gitcomp_builtin format-patch "$__git_format_patch_extra_options"
1871 return
1872 ;;
1873 esac
1874 __git_complete_revlist
1875 }
1877 _git_fsck ()
1878 {
1879 case "$cur" in
1880 --*)
1881 __gitcomp_builtin fsck
1882 return
1883 ;;
1884 esac
1885 }
1887 _git_gitk ()
1888 {
1889 __gitk_main
1890 }
1892 # Lists matching symbol names from a tag (as in ctags) file.
1893 # 1: List symbol names matching this word.
1894 # 2: The tag file to list symbol names from.
1895 # 3: A prefix to be added to each listed symbol name (optional).
1896 # 4: A suffix to be appended to each listed symbol name (optional).
1897 __git_match_ctag () {
1898 awk -v pfx="${3-}" -v sfx="${4-}" "
1899 /^${1//\//\\/}/ { print pfx \$1 sfx }
1900 " "$2"
1901 }
1903 # Complete symbol names from a tag file.
1904 # Usage: __git_complete_symbol [<option>]...
1905 # --tags=<file>: The tag file to list symbol names from instead of the
1906 # default "tags".
1907 # --pfx=<prefix>: A prefix to be added to each symbol name.
1908 # --cur=<word>: The current symbol name to be completed. Defaults to
1909 # the current word to be completed.
1910 # --sfx=<suffix>: A suffix to be appended to each symbol name instead
1911 # of the default space.
1912 __git_complete_symbol () {
1913 local tags=tags pfx="" cur_="${cur-}" sfx=" "
1915 while test $# != 0; do
1916 case "$1" in
1917 --tags=*) tags="${1##--tags=}" ;;
1918 --pfx=*) pfx="${1##--pfx=}" ;;
1919 --cur=*) cur_="${1##--cur=}" ;;
1920 --sfx=*) sfx="${1##--sfx=}" ;;
1921 *) return 1 ;;
1922 esac
1923 shift
1924 done
1926 if test -r "$tags"; then
1927 __gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
1928 fi
1929 }
1931 _git_grep ()
1932 {
1933 __git_has_doubledash && return
1935 case "$cur" in
1936 --*)
1937 __gitcomp_builtin grep
1938 return
1939 ;;
1940 esac
1942 case "$cword,$prev" in
1943 $((__git_cmd_idx+1)),*|*,-*)
1944 __git_complete_symbol && return
1945 ;;
1946 esac
1948 __git_complete_refs
1949 }
1951 _git_help ()
1952 {
1953 case "$cur" in
1954 --*)
1955 __gitcomp_builtin help
1956 return
1957 ;;
1958 esac
1959 if test -n "${GIT_TESTING_ALL_COMMAND_LIST-}"
1960 then
1961 __gitcomp "$GIT_TESTING_ALL_COMMAND_LIST $(__git --list-cmds=alias,list-guide) gitk"
1962 else
1963 __gitcomp "$(__git --list-cmds=main,nohelpers,alias,list-guide) gitk"
1964 fi
1965 }
1967 _git_init ()
1968 {
1969 case "$cur" in
1970 --shared=*)
1971 __gitcomp "
1972 false true umask group all world everybody
1973 " "" "${cur##--shared=}"
1974 return
1975 ;;
1976 --*)
1977 __gitcomp_builtin init
1978 return
1979 ;;
1980 esac
1981 }
1983 _git_ls_files ()
1984 {
1985 case "$cur" in
1986 --*)
1987 __gitcomp_builtin ls-files
1988 return
1989 ;;
1990 esac
1992 # XXX ignore options like --modified and always suggest all cached
1993 # files.
1994 __git_complete_index_file "--cached"
1995 }
1997 _git_ls_remote ()
1998 {
1999 case "$cur" in
2000 --*)
2001 __gitcomp_builtin ls-remote
2002 return
2003 ;;
2004 esac
2005 __gitcomp_nl "$(__git_remotes)"
2006 }
2008 _git_ls_tree ()
2009 {
2010 case "$cur" in
2011 --*)
2012 __gitcomp_builtin ls-tree
2013 return
2014 ;;
2015 esac
2017 __git_complete_file
2018 }
2020 # Options that go well for log, shortlog and gitk
2021 __git_log_common_options="
2022 --not --all
2023 --branches --tags --remotes
2024 --first-parent --merges --no-merges
2025 --max-count=
2026 --max-age= --since= --after=
2027 --min-age= --until= --before=
2028 --min-parents= --max-parents=
2029 --no-min-parents --no-max-parents
2030 "
2031 # Options that go well for log and gitk (not shortlog)
2032 __git_log_gitk_options="
2033 --dense --sparse --full-history
2034 --simplify-merges --simplify-by-decoration
2035 --left-right --notes --no-notes
2036 "
2037 # Options that go well for log and shortlog (not gitk)
2038 __git_log_shortlog_options="
2039 --author= --committer= --grep=
2040 --all-match --invert-grep
2041 "
2042 # Options accepted by log and show
2043 __git_log_show_options="
2044 --diff-merges --diff-merges= --no-diff-merges --remerge-diff
2045 "
2047 __git_diff_merges_opts="off none on first-parent 1 separate m combined c dense-combined cc remerge r"
2049 __git_log_pretty_formats="oneline short medium full fuller reference email raw format: tformat: mboxrd"
2050 __git_log_date_formats="relative iso8601 iso8601-strict rfc2822 short local default human raw unix auto: format:"
2052 _git_log ()
2053 {
2054 __git_has_doubledash && return
2055 __git_find_repo_path
2057 local merge=""
2058 if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
2059 merge="--merge"
2060 fi
2061 case "$prev,$cur" in
2062 -L,:*:*)
2063 return # fall back to Bash filename completion
2064 ;;
2065 -L,:*)
2066 __git_complete_symbol --cur="${cur#:}" --sfx=":"
2067 return
2068 ;;
2069 -G,*|-S,*)
2070 __git_complete_symbol
2071 return
2072 ;;
2073 esac
2074 case "$cur" in
2075 --pretty=*|--format=*)
2076 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2077 " "" "${cur#*=}"
2078 return
2079 ;;
2080 --date=*)
2081 __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
2082 return
2083 ;;
2084 --decorate=*)
2085 __gitcomp "full short no" "" "${cur##--decorate=}"
2086 return
2087 ;;
2088 --diff-algorithm=*)
2089 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
2090 return
2091 ;;
2092 --submodule=*)
2093 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
2094 return
2095 ;;
2096 --ws-error-highlight=*)
2097 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
2098 return
2099 ;;
2100 --no-walk=*)
2101 __gitcomp "sorted unsorted" "" "${cur##--no-walk=}"
2102 return
2103 ;;
2104 --diff-merges=*)
2105 __gitcomp "$__git_diff_merges_opts" "" "${cur##--diff-merges=}"
2106 return
2107 ;;
2108 --*)
2109 __gitcomp "
2110 $__git_log_common_options
2111 $__git_log_shortlog_options
2112 $__git_log_gitk_options
2113 $__git_log_show_options
2114 --root --topo-order --date-order --reverse
2115 --follow --full-diff
2116 --abbrev-commit --no-abbrev-commit --abbrev=
2117 --relative-date --date=
2118 --pretty= --format= --oneline
2119 --show-signature
2120 --cherry-mark
2121 --cherry-pick
2122 --graph
2123 --decorate --decorate= --no-decorate
2124 --walk-reflogs
2125 --no-walk --no-walk= --do-walk
2126 --parents --children
2127 --expand-tabs --expand-tabs= --no-expand-tabs
2128 $merge
2129 $__git_diff_common_options
2130 "
2131 return
2132 ;;
2133 -L:*:*)
2134 return # fall back to Bash filename completion
2135 ;;
2136 -L:*)
2137 __git_complete_symbol --cur="${cur#-L:}" --sfx=":"
2138 return
2139 ;;
2140 -G*)
2141 __git_complete_symbol --pfx="-G" --cur="${cur#-G}"
2142 return
2143 ;;
2144 -S*)
2145 __git_complete_symbol --pfx="-S" --cur="${cur#-S}"
2146 return
2147 ;;
2148 esac
2149 __git_complete_revlist
2150 }
2152 _git_merge ()
2153 {
2154 __git_complete_strategy && return
2156 case "$cur" in
2157 --*)
2158 __gitcomp_builtin merge
2159 return
2160 esac
2161 __git_complete_refs
2162 }
2164 _git_mergetool ()
2165 {
2166 case "$cur" in
2167 --tool=*)
2168 __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
2169 return
2170 ;;
2171 --*)
2172 __gitcomp "--tool= --prompt --no-prompt --gui --no-gui"
2173 return
2174 ;;
2175 esac
2176 }
2178 _git_merge_base ()
2179 {
2180 case "$cur" in
2181 --*)
2182 __gitcomp_builtin merge-base
2183 return
2184 ;;
2185 esac
2186 __git_complete_refs
2187 }
2189 _git_mv ()
2190 {
2191 case "$cur" in
2192 --*)
2193 __gitcomp_builtin mv
2194 return
2195 ;;
2196 esac
2198 if [ $(__git_count_arguments "mv") -gt 0 ]; then
2199 # We need to show both cached and untracked files (including
2200 # empty directories) since this may not be the last argument.
2201 __git_complete_index_file "--cached --others --directory"
2202 else
2203 __git_complete_index_file "--cached"
2204 fi
2205 }
2207 _git_notes ()
2208 {
2209 local subcommands='add append copy edit get-ref list merge prune remove show'
2210 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2212 case "$subcommand,$cur" in
2213 ,--*)
2214 __gitcomp_builtin notes
2215 ;;
2216 ,*)
2217 case "$prev" in
2218 --ref)
2219 __git_complete_refs
2220 ;;
2221 *)
2222 __gitcomp "$subcommands --ref"
2223 ;;
2224 esac
2225 ;;
2226 *,--reuse-message=*|*,--reedit-message=*)
2227 __git_complete_refs --cur="${cur#*=}"
2228 ;;
2229 *,--*)
2230 __gitcomp_builtin notes_$subcommand
2231 ;;
2232 prune,*|get-ref,*)
2233 # this command does not take a ref, do not complete it
2234 ;;
2235 *)
2236 case "$prev" in
2237 -m|-F)
2238 ;;
2239 *)
2240 __git_complete_refs
2241 ;;
2242 esac
2243 ;;
2244 esac
2245 }
2247 _git_pull ()
2248 {
2249 __git_complete_strategy && return
2251 case "$cur" in
2252 --recurse-submodules=*)
2253 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
2254 return
2255 ;;
2256 --*)
2257 __gitcomp_builtin pull
2259 return
2260 ;;
2261 esac
2262 __git_complete_remote_or_refspec
2263 }
2265 __git_push_recurse_submodules="check on-demand only"
2267 __git_complete_force_with_lease ()
2268 {
2269 local cur_=$1
2271 case "$cur_" in
2272 --*=)
2273 ;;
2274 *:*)
2275 __git_complete_refs --cur="${cur_#*:}"
2276 ;;
2277 *)
2278 __git_complete_refs --cur="$cur_"
2279 ;;
2280 esac
2281 }
2283 _git_push ()
2284 {
2285 case "$prev" in
2286 --repo)
2287 __gitcomp_nl "$(__git_remotes)"
2288 return
2289 ;;
2290 --recurse-submodules)
2291 __gitcomp "$__git_push_recurse_submodules"
2292 return
2293 ;;
2294 esac
2295 case "$cur" in
2296 --repo=*)
2297 __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
2298 return
2299 ;;
2300 --recurse-submodules=*)
2301 __gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
2302 return
2303 ;;
2304 --force-with-lease=*)
2305 __git_complete_force_with_lease "${cur##--force-with-lease=}"
2306 return
2307 ;;
2308 --*)
2309 __gitcomp_builtin push
2310 return
2311 ;;
2312 esac
2313 __git_complete_remote_or_refspec
2314 }
2316 _git_range_diff ()
2317 {
2318 case "$cur" in
2319 --*)
2320 __gitcomp "
2321 --creation-factor= --no-dual-color
2322 $__git_diff_common_options
2323 "
2324 return
2325 ;;
2326 esac
2327 __git_complete_revlist
2328 }
2330 __git_rebase_inprogress_options="--continue --skip --abort --quit --show-current-patch"
2331 __git_rebase_interactive_inprogress_options="$__git_rebase_inprogress_options --edit-todo"
2333 _git_rebase ()
2334 {
2335 __git_find_repo_path
2336 if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
2337 __gitcomp "$__git_rebase_interactive_inprogress_options"
2338 return
2339 elif [ -d "$__git_repo_path"/rebase-apply ] || \
2340 [ -d "$__git_repo_path"/rebase-merge ]; then
2341 __gitcomp "$__git_rebase_inprogress_options"
2342 return
2343 fi
2344 __git_complete_strategy && return
2345 case "$cur" in
2346 --whitespace=*)
2347 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
2348 return
2349 ;;
2350 --onto=*)
2351 __git_complete_refs --cur="${cur##--onto=}"
2352 return
2353 ;;
2354 --*)
2355 __gitcomp_builtin rebase "" \
2356 "$__git_rebase_interactive_inprogress_options"
2358 return
2359 esac
2360 __git_complete_refs
2361 }
2363 _git_reflog ()
2364 {
2365 local subcommands="show delete expire"
2366 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2368 if [ -z "$subcommand" ]; then
2369 __gitcomp "$subcommands"
2370 else
2371 __git_complete_refs
2372 fi
2373 }
2375 __git_send_email_confirm_options="always never auto cc compose"
2376 __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
2378 _git_send_email ()
2379 {
2380 case "$prev" in
2381 --to|--cc|--bcc|--from)
2382 __gitcomp "$(__git send-email --dump-aliases)"
2383 return
2384 ;;
2385 esac
2387 case "$cur" in
2388 --confirm=*)
2389 __gitcomp "
2390 $__git_send_email_confirm_options
2391 " "" "${cur##--confirm=}"
2392 return
2393 ;;
2394 --suppress-cc=*)
2395 __gitcomp "
2396 $__git_send_email_suppresscc_options
2397 " "" "${cur##--suppress-cc=}"
2399 return
2400 ;;
2401 --smtp-encryption=*)
2402 __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
2403 return
2404 ;;
2405 --thread=*)
2406 __gitcomp "
2407 deep shallow
2408 " "" "${cur##--thread=}"
2409 return
2410 ;;
2411 --to=*|--cc=*|--bcc=*|--from=*)
2412 __gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
2413 return
2414 ;;
2415 --*)
2416 __gitcomp_builtin send-email "$__git_format_patch_extra_options"
2417 return
2418 ;;
2419 esac
2420 __git_complete_revlist
2421 }
2423 _git_stage ()
2424 {
2425 _git_add
2426 }
2428 _git_status ()
2429 {
2430 local complete_opt
2431 local untracked_state
2433 case "$cur" in
2434 --ignore-submodules=*)
2435 __gitcomp "none untracked dirty all" "" "${cur##--ignore-submodules=}"
2436 return
2437 ;;
2438 --untracked-files=*)
2439 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
2440 return
2441 ;;
2442 --column=*)
2443 __gitcomp "
2444 always never auto column row plain dense nodense
2445 " "" "${cur##--column=}"
2446 return
2447 ;;
2448 --*)
2449 __gitcomp_builtin status
2450 return
2451 ;;
2452 esac
2454 untracked_state="$(__git_get_option_value "-u" "--untracked-files=" \
2455 "$__git_untracked_file_modes" "status.showUntrackedFiles")"
2457 case "$untracked_state" in
2458 no)
2459 # --ignored option does not matter
2460 complete_opt=
2461 ;;
2462 all|normal|*)
2463 complete_opt="--cached --directory --no-empty-directory --others"
2465 if [ -n "$(__git_find_on_cmdline "--ignored")" ]; then
2466 complete_opt="$complete_opt --ignored --exclude=*"
2467 fi
2468 ;;
2469 esac
2471 __git_complete_index_file "$complete_opt"
2472 }
2474 _git_switch ()
2475 {
2476 local dwim_opt="$(__git_checkout_default_dwim_mode)"
2478 case "$prev" in
2479 -c|-C|--orphan)
2480 # Complete local branches (and DWIM branch
2481 # remote branch names) for an option argument
2482 # specifying a new branch name. This is for
2483 # convenience, assuming new branches are
2484 # possibly based on pre-existing branch names.
2485 __git_complete_refs $dwim_opt --mode="heads"
2486 return
2487 ;;
2488 *)
2489 ;;
2490 esac
2492 case "$cur" in
2493 --conflict=*)
2494 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
2495 ;;
2496 --*)
2497 __gitcomp_builtin switch
2498 ;;
2499 *)
2500 # Unlike in git checkout, git switch --orphan does not take
2501 # a start point. Thus we really have nothing to complete after
2502 # the branch name.
2503 if [ -n "$(__git_find_on_cmdline "--orphan")" ]; then
2504 return
2505 fi
2507 # At this point, we've already handled special completion for
2508 # -c/-C, and --orphan. There are 3 main things left to
2509 # complete:
2510 # 1) a start-point for -c/-C or -d/--detach
2511 # 2) a remote head, for --track
2512 # 3) a branch name, possibly including DWIM remote branches
2514 if [ -n "$(__git_find_on_cmdline "-c -C -d --detach")" ]; then
2515 __git_complete_refs --mode="refs"
2516 elif [ -n "$(__git_find_on_cmdline "--track")" ]; then
2517 __git_complete_refs --mode="remote-heads"
2518 else
2519 __git_complete_refs $dwim_opt --mode="heads"
2520 fi
2521 ;;
2522 esac
2523 }
2525 __git_config_get_set_variables ()
2526 {
2527 local prevword word config_file= c=$cword
2528 while [ $c -gt "$__git_cmd_idx" ]; do
2529 word="${words[c]}"
2530 case "$word" in
2531 --system|--global|--local|--file=*)
2532 config_file="$word"
2533 break
2534 ;;
2535 -f|--file)
2536 config_file="$word $prevword"
2537 break
2538 ;;
2539 esac
2540 prevword=$word
2541 c=$((--c))
2542 done
2544 __git config $config_file --name-only --list
2545 }
2547 __git_config_vars=
2548 __git_compute_config_vars ()
2549 {
2550 test -n "$__git_config_vars" ||
2551 __git_config_vars="$(git help --config-for-completion)"
2552 }
2554 __git_config_sections=
2555 __git_compute_config_sections ()
2556 {
2557 test -n "$__git_config_sections" ||
2558 __git_config_sections="$(git help --config-sections-for-completion)"
2559 }
2561 # Completes possible values of various configuration variables.
2562 #
2563 # Usage: __git_complete_config_variable_value [<option>]...
2564 # --varname=<word>: The name of the configuration variable whose value is
2565 # to be completed. Defaults to the previous word on the
2566 # command line.
2567 # --cur=<word>: The current value to be completed. Defaults to the current
2568 # word to be completed.
2569 __git_complete_config_variable_value ()
2570 {
2571 local varname="$prev" cur_="$cur"
2573 while test $# != 0; do
2574 case "$1" in
2575 --varname=*) varname="${1##--varname=}" ;;
2576 --cur=*) cur_="${1##--cur=}" ;;
2577 *) return 1 ;;
2578 esac
2579 shift
2580 done
2582 if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
2583 varname="${varname,,}"
2584 else
2585 varname="$(echo "$varname" |tr A-Z a-z)"
2586 fi
2588 case "$varname" in
2589 branch.*.remote|branch.*.pushremote)
2590 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2591 return
2592 ;;
2593 branch.*.merge)
2594 __git_complete_refs --cur="$cur_"
2595 return
2596 ;;
2597 branch.*.rebase)
2598 __gitcomp "false true merges interactive" "" "$cur_"
2599 return
2600 ;;
2601 remote.pushdefault)
2602 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2603 return
2604 ;;
2605 remote.*.fetch)
2606 local remote="${varname#remote.}"
2607 remote="${remote%.fetch}"
2608 if [ -z "$cur_" ]; then
2609 __gitcomp_nl "refs/heads/" "" "" ""
2610 return
2611 fi
2612 __gitcomp_nl "$(__git_refs_remotes "$remote")" "" "$cur_"
2613 return
2614 ;;
2615 remote.*.push)
2616 local remote="${varname#remote.}"
2617 remote="${remote%.push}"
2618 __gitcomp_nl "$(__git for-each-ref \
2619 --format='%(refname):%(refname)' refs/heads)" "" "$cur_"
2620 return
2621 ;;
2622 pull.twohead|pull.octopus)
2623 __git_compute_merge_strategies
2624 __gitcomp "$__git_merge_strategies" "" "$cur_"
2625 return
2626 ;;
2627 color.pager)
2628 __gitcomp "false true" "" "$cur_"
2629 return
2630 ;;
2631 color.*.*)
2632 __gitcomp "
2633 normal black red green yellow blue magenta cyan white
2634 bold dim ul blink reverse
2635 " "" "$cur_"
2636 return
2637 ;;
2638 color.*)
2639 __gitcomp "false true always never auto" "" "$cur_"
2640 return
2641 ;;
2642 diff.submodule)
2643 __gitcomp "$__git_diff_submodule_formats" "" "$cur_"
2644 return
2645 ;;
2646 help.format)
2647 __gitcomp "man info web html" "" "$cur_"
2648 return
2649 ;;
2650 log.date)
2651 __gitcomp "$__git_log_date_formats" "" "$cur_"
2652 return
2653 ;;
2654 sendemail.aliasfiletype)
2655 __gitcomp "mutt mailrc pine elm gnus" "" "$cur_"
2656 return
2657 ;;
2658 sendemail.confirm)
2659 __gitcomp "$__git_send_email_confirm_options" "" "$cur_"
2660 return
2661 ;;
2662 sendemail.suppresscc)
2663 __gitcomp "$__git_send_email_suppresscc_options" "" "$cur_"
2664 return
2665 ;;
2666 sendemail.transferencoding)
2667 __gitcomp "7bit 8bit quoted-printable base64" "" "$cur_"
2668 return
2669 ;;
2670 *.*)
2671 return
2672 ;;
2673 esac
2674 }
2676 # Completes configuration sections, subsections, variable names.
2677 #
2678 # Usage: __git_complete_config_variable_name [<option>]...
2679 # --cur=<word>: The current configuration section/variable name to be
2680 # completed. Defaults to the current word to be completed.
2681 # --sfx=<suffix>: A suffix to be appended to each fully completed
2682 # configuration variable name (but not to sections or
2683 # subsections) instead of the default space.
2684 __git_complete_config_variable_name ()
2685 {
2686 local cur_="$cur" sfx
2688 while test $# != 0; do
2689 case "$1" in
2690 --cur=*) cur_="${1##--cur=}" ;;
2691 --sfx=*) sfx="${1##--sfx=}" ;;
2692 *) return 1 ;;
2693 esac
2694 shift
2695 done
2697 case "$cur_" in
2698 branch.*.*)
2699 local pfx="${cur_%.*}."
2700 cur_="${cur_##*.}"
2701 __gitcomp "remote pushRemote merge mergeOptions rebase" "$pfx" "$cur_" "$sfx"
2702 return
2703 ;;
2704 branch.*)
2705 local pfx="${cur_%.*}."
2706 cur_="${cur_#*.}"
2707 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
2708 __gitcomp_nl_append $'autoSetupMerge\nautoSetupRebase\n' "$pfx" "$cur_" "${sfx- }"
2709 return
2710 ;;
2711 guitool.*.*)
2712 local pfx="${cur_%.*}."
2713 cur_="${cur_##*.}"
2714 __gitcomp "
2715 argPrompt cmd confirm needsFile noConsole noRescan
2716 prompt revPrompt revUnmerged title
2717 " "$pfx" "$cur_" "$sfx"
2718 return
2719 ;;
2720 difftool.*.*)
2721 local pfx="${cur_%.*}."
2722 cur_="${cur_##*.}"
2723 __gitcomp "cmd path" "$pfx" "$cur_" "$sfx"
2724 return
2725 ;;
2726 man.*.*)
2727 local pfx="${cur_%.*}."
2728 cur_="${cur_##*.}"
2729 __gitcomp "cmd path" "$pfx" "$cur_" "$sfx"
2730 return
2731 ;;
2732 mergetool.*.*)
2733 local pfx="${cur_%.*}."
2734 cur_="${cur_##*.}"
2735 __gitcomp "cmd path trustExitCode" "$pfx" "$cur_" "$sfx"
2736 return
2737 ;;
2738 pager.*)
2739 local pfx="${cur_%.*}."
2740 cur_="${cur_#*.}"
2741 __git_compute_all_commands
2742 __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_" "${sfx- }"
2743 return
2744 ;;
2745 remote.*.*)
2746 local pfx="${cur_%.*}."
2747 cur_="${cur_##*.}"
2748 __gitcomp "
2749 url proxy fetch push mirror skipDefaultUpdate
2750 receivepack uploadpack tagOpt pushurl
2751 " "$pfx" "$cur_" "$sfx"
2752 return
2753 ;;
2754 remote.*)
2755 local pfx="${cur_%.*}."
2756 cur_="${cur_#*.}"
2757 __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
2758 __gitcomp_nl_append "pushDefault" "$pfx" "$cur_" "${sfx- }"
2759 return
2760 ;;
2761 url.*.*)
2762 local pfx="${cur_%.*}."
2763 cur_="${cur_##*.}"
2764 __gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_" "$sfx"
2765 return
2766 ;;
2767 *.*)
2768 __git_compute_config_vars
2769 __gitcomp "$__git_config_vars" "" "$cur_" "$sfx"
2770 ;;
2771 *)
2772 __git_compute_config_sections
2773 __gitcomp "$__git_config_sections" "" "$cur_" "."
2774 ;;
2775 esac
2776 }
2778 # Completes '='-separated configuration sections/variable names and values
2779 # for 'git -c section.name=value'.
2780 #
2781 # Usage: __git_complete_config_variable_name_and_value [<option>]...
2782 # --cur=<word>: The current configuration section/variable name/value to be
2783 # completed. Defaults to the current word to be completed.
2784 __git_complete_config_variable_name_and_value ()
2785 {
2786 local cur_="$cur"
2788 while test $# != 0; do
2789 case "$1" in
2790 --cur=*) cur_="${1##--cur=}" ;;
2791 *) return 1 ;;
2792 esac
2793 shift
2794 done
2796 case "$cur_" in
2797 *=*)
2798 __git_complete_config_variable_value \
2799 --varname="${cur_%%=*}" --cur="${cur_#*=}"
2800 ;;
2801 *)
2802 __git_complete_config_variable_name --cur="$cur_" --sfx='='
2803 ;;
2804 esac
2805 }
2807 _git_config ()
2808 {
2809 case "$prev" in
2810 --get|--get-all|--unset|--unset-all)
2811 __gitcomp_nl "$(__git_config_get_set_variables)"
2812 return
2813 ;;
2814 *.*)
2815 __git_complete_config_variable_value
2816 return
2817 ;;
2818 esac
2819 case "$cur" in
2820 --*)
2821 __gitcomp_builtin config
2822 ;;
2823 *)
2824 __git_complete_config_variable_name
2825 ;;
2826 esac
2827 }
2829 _git_remote ()
2830 {
2831 local subcommands="
2832 add rename remove set-head set-branches
2833 get-url set-url show prune update
2834 "
2835 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2836 if [ -z "$subcommand" ]; then
2837 case "$cur" in
2838 --*)
2839 __gitcomp_builtin remote
2840 ;;
2841 *)
2842 __gitcomp "$subcommands"
2843 ;;
2844 esac
2845 return
2846 fi
2848 case "$subcommand,$cur" in
2849 add,--*)
2850 __gitcomp_builtin remote_add
2851 ;;
2852 add,*)
2853 ;;
2854 set-head,--*)
2855 __gitcomp_builtin remote_set-head
2856 ;;
2857 set-branches,--*)
2858 __gitcomp_builtin remote_set-branches
2859 ;;
2860 set-head,*|set-branches,*)
2861 __git_complete_remote_or_refspec
2862 ;;
2863 update,--*)
2864 __gitcomp_builtin remote_update
2865 ;;
2866 update,*)
2867 __gitcomp "$(__git_remotes) $(__git_get_config_variables "remotes")"
2868 ;;
2869 set-url,--*)
2870 __gitcomp_builtin remote_set-url
2871 ;;
2872 get-url,--*)
2873 __gitcomp_builtin remote_get-url
2874 ;;
2875 prune,--*)
2876 __gitcomp_builtin remote_prune
2877 ;;
2878 *)
2879 __gitcomp_nl "$(__git_remotes)"
2880 ;;
2881 esac
2882 }
2884 _git_replace ()
2885 {
2886 case "$cur" in
2887 --format=*)
2888 __gitcomp "short medium long" "" "${cur##--format=}"
2889 return
2890 ;;
2891 --*)
2892 __gitcomp_builtin replace
2893 return
2894 ;;
2895 esac
2896 __git_complete_refs
2897 }
2899 _git_rerere ()
2900 {
2901 local subcommands="clear forget diff remaining status gc"
2902 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2903 if test -z "$subcommand"
2904 then
2905 __gitcomp "$subcommands"
2906 return
2907 fi
2908 }
2910 _git_reset ()
2911 {
2912 __git_has_doubledash && return
2914 case "$cur" in
2915 --*)
2916 __gitcomp_builtin reset
2917 return
2918 ;;
2919 esac
2920 __git_complete_refs
2921 }
2923 _git_restore ()
2924 {
2925 case "$prev" in
2926 -s)
2927 __git_complete_refs
2928 return
2929 ;;
2930 esac
2932 case "$cur" in
2933 --conflict=*)
2934 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
2935 ;;
2936 --source=*)
2937 __git_complete_refs --cur="${cur##--source=}"
2938 ;;
2939 --*)
2940 __gitcomp_builtin restore
2941 ;;
2942 *)
2943 if __git rev-parse --verify --quiet HEAD >/dev/null; then
2944 __git_complete_index_file "--modified"
2945 fi
2946 esac
2947 }
2949 __git_revert_inprogress_options=$__git_sequencer_inprogress_options
2951 _git_revert ()
2952 {
2953 __git_find_repo_path
2954 if [ -f "$__git_repo_path"/REVERT_HEAD ]; then
2955 __gitcomp "$__git_revert_inprogress_options"
2956 return
2957 fi
2958 __git_complete_strategy && return
2959 case "$cur" in
2960 --*)
2961 __gitcomp_builtin revert "" \
2962 "$__git_revert_inprogress_options"
2963 return
2964 ;;
2965 esac
2966 __git_complete_refs
2967 }
2969 _git_rm ()
2970 {
2971 case "$cur" in
2972 --*)
2973 __gitcomp_builtin rm
2974 return
2975 ;;
2976 esac
2978 __git_complete_index_file "--cached"
2979 }
2981 _git_shortlog ()
2982 {
2983 __git_has_doubledash && return
2985 case "$cur" in
2986 --*)
2987 __gitcomp "
2988 $__git_log_common_options
2989 $__git_log_shortlog_options
2990 --numbered --summary --email
2991 "
2992 return
2993 ;;
2994 esac
2995 __git_complete_revlist
2996 }
2998 _git_show ()
2999 {
3000 __git_has_doubledash && return
3002 case "$cur" in
3003 --pretty=*|--format=*)
3004 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
3005 " "" "${cur#*=}"
3006 return
3007 ;;
3008 --diff-algorithm=*)
3009 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
3010 return
3011 ;;
3012 --submodule=*)
3013 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
3014 return
3015 ;;
3016 --color-moved=*)
3017 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
3018 return
3019 ;;
3020 --color-moved-ws=*)
3021 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
3022 return
3023 ;;
3024 --ws-error-highlight=*)
3025 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
3026 return
3027 ;;
3028 --diff-merges=*)
3029 __gitcomp "$__git_diff_merges_opts" "" "${cur##--diff-merges=}"
3030 return
3031 ;;
3032 --*)
3033 __gitcomp "--pretty= --format= --abbrev-commit --no-abbrev-commit
3034 --oneline --show-signature
3035 --expand-tabs --expand-tabs= --no-expand-tabs
3036 $__git_log_show_options
3037 $__git_diff_common_options
3038 "
3039 return
3040 ;;
3041 esac
3042 __git_complete_revlist_file
3043 }
3045 _git_show_branch ()
3046 {
3047 case "$cur" in
3048 --*)
3049 __gitcomp_builtin show-branch
3050 return
3051 ;;
3052 esac
3053 __git_complete_revlist
3054 }
3056 __gitcomp_directories ()
3057 {
3058 local _tmp_dir _tmp_completions _found=0
3060 # Get the directory of the current token; this differs from dirname
3061 # in that it keeps up to the final trailing slash. If no slash found
3062 # that's fine too.
3063 [[ "$cur" =~ .*/ ]]
3064 _tmp_dir=$BASH_REMATCH
3066 # Find possible directory completions, adding trailing '/' characters,
3067 # de-quoting, and handling unusual characters.
3068 while IFS= read -r -d $'\0' c ; do
3069 # If there are directory completions, find ones that start
3070 # with "$cur", the current token, and put those in COMPREPLY
3071 if [[ $c == "$cur"* ]]; then
3072 COMPREPLY+=("$c/")
3073 _found=1
3074 fi
3075 done < <(git ls-tree -z -d --name-only HEAD $_tmp_dir)
3077 if [[ $_found == 0 ]] && [[ "$cur" =~ /$ ]]; then
3078 # No possible further completions any deeper, so assume we're at
3079 # a leaf directory and just consider it complete
3080 __gitcomp_direct_append "$cur "
3081 fi
3082 }
3084 _git_sparse_checkout ()
3085 {
3086 local subcommands="list init set disable add reapply"
3087 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3088 if [ -z "$subcommand" ]; then
3089 __gitcomp "$subcommands"
3090 return
3091 fi
3093 case "$subcommand,$cur" in
3094 *,--*)
3095 __gitcomp_builtin sparse-checkout_$subcommand "" "--"
3096 ;;
3097 set,*|add,*)
3098 if [ "$(__git config core.sparseCheckoutCone)" == "true" ] ||
3099 [ -n "$(__git_find_on_cmdline --cone)" ]; then
3100 __gitcomp_directories
3101 fi
3102 esac
3103 }
3105 _git_stash ()
3106 {
3107 local subcommands='push list show apply clear drop pop create branch'
3108 local subcommand="$(__git_find_on_cmdline "$subcommands save")"
3110 if [ -z "$subcommand" ]; then
3111 case "$((cword - __git_cmd_idx)),$cur" in
3112 *,--*)
3113 __gitcomp_builtin stash_push
3114 ;;
3115 1,sa*)
3116 __gitcomp "save"
3117 ;;
3118 1,*)
3119 __gitcomp "$subcommands"
3120 ;;
3121 esac
3122 return
3123 fi
3125 case "$subcommand,$cur" in
3126 list,--*)
3127 # NEEDSWORK: can we somehow unify this with the options in _git_log() and _git_show()
3128 __gitcomp_builtin stash_list "$__git_log_common_options $__git_diff_common_options"
3129 ;;
3130 show,--*)
3131 __gitcomp_builtin stash_show "$__git_diff_common_options"
3132 ;;
3133 *,--*)
3134 __gitcomp_builtin "stash_$subcommand"
3135 ;;
3136 branch,*)
3137 if [ $cword -eq $((__git_cmd_idx+2)) ]; then
3138 __git_complete_refs
3139 else
3140 __gitcomp_nl "$(__git stash list \
3141 | sed -n -e 's/:.*//p')"
3142 fi
3143 ;;
3144 show,*|apply,*|drop,*|pop,*)
3145 __gitcomp_nl "$(__git stash list \
3146 | sed -n -e 's/:.*//p')"
3147 ;;
3148 esac
3149 }
3151 _git_submodule ()
3152 {
3153 __git_has_doubledash && return
3155 local subcommands="add status init deinit update set-branch set-url summary foreach sync absorbgitdirs"
3156 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3157 if [ -z "$subcommand" ]; then
3158 case "$cur" in
3159 --*)
3160 __gitcomp "--quiet"
3161 ;;
3162 *)
3163 __gitcomp "$subcommands"
3164 ;;
3165 esac
3166 return
3167 fi
3169 case "$subcommand,$cur" in
3170 add,--*)
3171 __gitcomp "--branch --force --name --reference --depth"
3172 ;;
3173 status,--*)
3174 __gitcomp "--cached --recursive"
3175 ;;
3176 deinit,--*)
3177 __gitcomp "--force --all"
3178 ;;
3179 update,--*)
3180 __gitcomp "
3181 --init --remote --no-fetch
3182 --recommend-shallow --no-recommend-shallow
3183 --force --rebase --merge --reference --depth --recursive --jobs
3184 "
3185 ;;
3186 set-branch,--*)
3187 __gitcomp "--default --branch"
3188 ;;
3189 summary,--*)
3190 __gitcomp "--cached --files --summary-limit"
3191 ;;
3192 foreach,--*|sync,--*)
3193 __gitcomp "--recursive"
3194 ;;
3195 *)
3196 ;;
3197 esac
3198 }
3200 _git_svn ()
3201 {
3202 local subcommands="
3203 init fetch clone rebase dcommit log find-rev
3204 set-tree commit-diff info create-ignore propget
3205 proplist show-ignore show-externals branch tag blame
3206 migrate mkdirs reset gc
3207 "
3208 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3209 if [ -z "$subcommand" ]; then
3210 __gitcomp "$subcommands"
3211 else
3212 local remote_opts="--username= --config-dir= --no-auth-cache"
3213 local fc_opts="
3214 --follow-parent --authors-file= --repack=
3215 --no-metadata --use-svm-props --use-svnsync-props
3216 --log-window-size= --no-checkout --quiet
3217 --repack-flags --use-log-author --localtime
3218 --add-author-from
3219 --recursive
3220 --ignore-paths= --include-paths= $remote_opts
3221 "
3222 local init_opts="
3223 --template= --shared= --trunk= --tags=
3224 --branches= --stdlayout --minimize-url
3225 --no-metadata --use-svm-props --use-svnsync-props
3226 --rewrite-root= --prefix= $remote_opts
3227 "
3228 local cmt_opts="
3229 --edit --rmdir --find-copies-harder --copy-similarity=
3230 "
3232 case "$subcommand,$cur" in
3233 fetch,--*)
3234 __gitcomp "--revision= --fetch-all $fc_opts"
3235 ;;
3236 clone,--*)
3237 __gitcomp "--revision= $fc_opts $init_opts"
3238 ;;
3239 init,--*)
3240 __gitcomp "$init_opts"
3241 ;;
3242 dcommit,--*)
3243 __gitcomp "
3244 --merge --strategy= --verbose --dry-run
3245 --fetch-all --no-rebase --commit-url
3246 --revision --interactive $cmt_opts $fc_opts
3247 "
3248 ;;
3249 set-tree,--*)
3250 __gitcomp "--stdin $cmt_opts $fc_opts"
3251 ;;
3252 create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
3253 show-externals,--*|mkdirs,--*)
3254 __gitcomp "--revision="
3255 ;;
3256 log,--*)
3257 __gitcomp "
3258 --limit= --revision= --verbose --incremental
3259 --oneline --show-commit --non-recursive
3260 --authors-file= --color
3261 "
3262 ;;
3263 rebase,--*)
3264 __gitcomp "
3265 --merge --verbose --strategy= --local
3266 --fetch-all --dry-run $fc_opts
3267 "
3268 ;;
3269 commit-diff,--*)
3270 __gitcomp "--message= --file= --revision= $cmt_opts"
3271 ;;
3272 info,--*)
3273 __gitcomp "--url"
3274 ;;
3275 branch,--*)
3276 __gitcomp "--dry-run --message --tag"
3277 ;;
3278 tag,--*)
3279 __gitcomp "--dry-run --message"
3280 ;;
3281 blame,--*)
3282 __gitcomp "--git-format"
3283 ;;
3284 migrate,--*)
3285 __gitcomp "
3286 --config-dir= --ignore-paths= --minimize
3287 --no-auth-cache --username=
3288 "
3289 ;;
3290 reset,--*)
3291 __gitcomp "--revision= --parent"
3292 ;;
3293 *)
3294 ;;
3295 esac
3296 fi
3297 }
3299 _git_tag ()
3300 {
3301 local i c="$__git_cmd_idx" f=0
3302 while [ $c -lt $cword ]; do
3303 i="${words[c]}"
3304 case "$i" in
3305 -d|--delete|-v|--verify)
3306 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3307 return
3308 ;;
3309 -f)
3310 f=1
3311 ;;
3312 esac
3313 ((c++))
3314 done
3316 case "$prev" in
3317 -m|-F)
3318 ;;
3319 -*|tag)
3320 if [ $f = 1 ]; then
3321 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3322 fi
3323 ;;
3324 *)
3325 __git_complete_refs
3326 ;;
3327 esac
3329 case "$cur" in
3330 --*)
3331 __gitcomp_builtin tag
3332 ;;
3333 esac
3334 }
3336 _git_whatchanged ()
3337 {
3338 _git_log
3339 }
3341 __git_complete_worktree_paths ()
3342 {
3343 local IFS=$'\n'
3344 # Generate completion reply from worktree list skipping the first
3345 # entry: it's the path of the main worktree, which can't be moved,
3346 # removed, locked, etc.
3347 __gitcomp_nl "$(git worktree list --porcelain |
3348 sed -n -e '2,$ s/^worktree //p')"
3349 }
3351 _git_worktree ()
3352 {
3353 local subcommands="add list lock move prune remove unlock"
3354 local subcommand subcommand_idx
3356 subcommand="$(__git_find_on_cmdline --show-idx "$subcommands")"
3357 subcommand_idx="${subcommand% *}"
3358 subcommand="${subcommand#* }"
3360 case "$subcommand,$cur" in
3361 ,*)
3362 __gitcomp "$subcommands"
3363 ;;
3364 *,--*)
3365 __gitcomp_builtin worktree_$subcommand
3366 ;;
3367 add,*) # usage: git worktree add [<options>] <path> [<commit-ish>]
3368 # Here we are not completing an --option, it's either the
3369 # path or a ref.
3370 case "$prev" in
3371 -b|-B) # Complete refs for branch to be created/reseted.
3372 __git_complete_refs
3373 ;;
3374 -*) # The previous word is an -o|--option without an
3375 # unstuck argument: have to complete the path for
3376 # the new worktree, so don't list anything, but let
3377 # Bash fall back to filename completion.
3378 ;;
3379 *) # The previous word is not an --option, so it must
3380 # be either the 'add' subcommand, the unstuck
3381 # argument of an option (e.g. branch for -b|-B), or
3382 # the path for the new worktree.
3383 if [ $cword -eq $((subcommand_idx+1)) ]; then
3384 # Right after the 'add' subcommand: have to
3385 # complete the path, so fall back to Bash
3386 # filename completion.
3387 :
3388 else
3389 case "${words[cword-2]}" in
3390 -b|-B) # After '-b <branch>': have to
3391 # complete the path, so fall back
3392 # to Bash filename completion.
3393 ;;
3394 *) # After the path: have to complete
3395 # the ref to be checked out.
3396 __git_complete_refs
3397 ;;
3398 esac
3399 fi
3400 ;;
3401 esac
3402 ;;
3403 lock,*|remove,*|unlock,*)
3404 __git_complete_worktree_paths
3405 ;;
3406 move,*)
3407 if [ $cword -eq $((subcommand_idx+1)) ]; then
3408 # The first parameter must be an existing working
3409 # tree to be moved.
3410 __git_complete_worktree_paths
3411 else
3412 # The second parameter is the destination: it could
3413 # be any path, so don't list anything, but let Bash
3414 # fall back to filename completion.
3415 :
3416 fi
3417 ;;
3418 esac
3419 }
3421 __git_complete_common () {
3422 local command="$1"
3424 case "$cur" in
3425 --*)
3426 __gitcomp_builtin "$command"
3427 ;;
3428 esac
3429 }
3431 __git_cmds_with_parseopt_helper=
3432 __git_support_parseopt_helper () {
3433 test -n "$__git_cmds_with_parseopt_helper" ||
3434 __git_cmds_with_parseopt_helper="$(__git --list-cmds=parseopt)"
3436 case " $__git_cmds_with_parseopt_helper " in
3437 *" $1 "*)
3438 return 0
3439 ;;
3440 *)
3441 return 1
3442 ;;
3443 esac
3444 }
3446 __git_have_func () {
3447 declare -f -- "$1" >/dev/null 2>&1
3448 }
3450 __git_complete_command () {
3451 local command="$1"
3452 local completion_func="_git_${command//-/_}"
3453 if ! __git_have_func $completion_func &&
3454 __git_have_func _completion_loader
3455 then
3456 _completion_loader "git-$command"
3457 fi
3458 if __git_have_func $completion_func
3459 then
3460 $completion_func
3461 return 0
3462 elif __git_support_parseopt_helper "$command"
3463 then
3464 __git_complete_common "$command"
3465 return 0
3466 else
3467 return 1
3468 fi
3469 }
3471 __git_main ()
3472 {
3473 local i c=1 command __git_dir __git_repo_path
3474 local __git_C_args C_args_count=0
3475 local __git_cmd_idx
3477 while [ $c -lt $cword ]; do
3478 i="${words[c]}"
3479 case "$i" in
3480 --git-dir=*)
3481 __git_dir="${i#--git-dir=}"
3482 ;;
3483 --git-dir)
3484 ((c++))
3485 __git_dir="${words[c]}"
3486 ;;
3487 --bare)
3488 __git_dir="."
3489 ;;
3490 --help)
3491 command="help"
3492 break
3493 ;;
3494 -c|--work-tree|--namespace)
3495 ((c++))
3496 ;;
3497 -C)
3498 __git_C_args[C_args_count++]=-C
3499 ((c++))
3500 __git_C_args[C_args_count++]="${words[c]}"
3501 ;;
3502 -*)
3503 ;;
3504 *)
3505 command="$i"
3506 __git_cmd_idx="$c"
3507 break
3508 ;;
3509 esac
3510 ((c++))
3511 done
3513 if [ -z "${command-}" ]; then
3514 case "$prev" in
3515 --git-dir|-C|--work-tree)
3516 # these need a path argument, let's fall back to
3517 # Bash filename completion
3518 return
3519 ;;
3520 -c)
3521 __git_complete_config_variable_name_and_value
3522 return
3523 ;;
3524 --namespace)
3525 # we don't support completing these options' arguments
3526 return
3527 ;;
3528 esac
3529 case "$cur" in
3530 --*)
3531 __gitcomp "
3532 --paginate
3533 --no-pager
3534 --git-dir=
3535 --bare
3536 --version
3537 --exec-path
3538 --exec-path=
3539 --html-path
3540 --man-path
3541 --info-path
3542 --work-tree=
3543 --namespace=
3544 --no-replace-objects
3545 --help
3546 "
3547 ;;
3548 *)
3549 if test -n "${GIT_TESTING_PORCELAIN_COMMAND_LIST-}"
3550 then
3551 __gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
3552 else
3553 local list_cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config
3555 if test "${GIT_COMPLETION_SHOW_ALL_COMMANDS-}" = "1"
3556 then
3557 list_cmds=builtins,$list_cmds
3558 fi
3559 __gitcomp "$(__git --list-cmds=$list_cmds)"
3560 fi
3561 ;;
3562 esac
3563 return
3564 fi
3566 __git_complete_command "$command" && return
3568 local expansion=$(__git_aliased_command "$command")
3569 if [ -n "$expansion" ]; then
3570 words[1]=$expansion
3571 __git_complete_command "$expansion"
3572 fi
3573 }
3575 __gitk_main ()
3576 {
3577 __git_has_doubledash && return
3579 local __git_repo_path
3580 __git_find_repo_path
3582 local merge=""
3583 if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
3584 merge="--merge"
3585 fi
3586 case "$cur" in
3587 --*)
3588 __gitcomp "
3589 $__git_log_common_options
3590 $__git_log_gitk_options
3591 $merge
3592 "
3593 return
3594 ;;
3595 esac
3596 __git_complete_revlist
3597 }
3599 if [[ -n ${ZSH_VERSION-} && -z ${GIT_SOURCING_ZSH_COMPLETION-} ]]; then
3600 echo "ERROR: this script is obsolete, please see git-completion.zsh" 1>&2
3601 return
3602 fi
3604 __git_func_wrap ()
3605 {
3606 local cur words cword prev
3607 local __git_cmd_idx=0
3608 _get_comp_words_by_ref -n =: cur words cword prev
3609 $1
3610 }
3612 ___git_complete ()
3613 {
3614 local wrapper="__git_wrap${2}"
3615 eval "$wrapper () { __git_func_wrap $2 ; }"
3616 complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
3617 || complete -o default -o nospace -F $wrapper $1
3618 }
3620 # Setup the completion for git commands
3621 # 1: command or alias
3622 # 2: function to call (e.g. `git`, `gitk`, `git_fetch`)
3623 __git_complete ()
3624 {
3625 local func
3627 if __git_have_func $2; then
3628 func=$2
3629 elif __git_have_func __$2_main; then
3630 func=__$2_main
3631 elif __git_have_func _$2; then
3632 func=_$2
3633 else
3634 echo "ERROR: could not find function '$2'" 1>&2
3635 return 1
3636 fi
3637 ___git_complete $1 $func
3638 }
3640 ___git_complete git __git_main
3641 ___git_complete gitk __gitk_main
3643 # The following are necessary only for Cygwin, and only are needed
3644 # when the user has tab-completed the executable name and consequently
3645 # included the '.exe' suffix.
3646 #
3647 if [ "$OSTYPE" = cygwin ]; then
3648 ___git_complete git.exe __git_main
3649 fi