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