]> git.ipfire.org Git - thirdparty/git.git/blob - contrib/completion/git-completion.bash
Merge branch 'rs/i18n-cannot-be-used-together' into maint-2.43
[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
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 "
2075 # Options that go well for log and gitk (not shortlog)
2076 __git_log_gitk_options="
2077 --dense --sparse --full-history
2078 --simplify-merges --simplify-by-decoration
2079 --left-right --notes --no-notes
2080 "
2081 # Options that go well for log and shortlog (not gitk)
2082 __git_log_shortlog_options="
2083 --author= --committer= --grep=
2084 --all-match --invert-grep
2085 "
2086 # Options accepted by log and show
2087 __git_log_show_options="
2088 --diff-merges --diff-merges= --no-diff-merges --dd --remerge-diff
2089 "
2090
2091 __git_diff_merges_opts="off none on first-parent 1 separate m combined c dense-combined cc remerge r"
2092
2093 __git_log_pretty_formats="oneline short medium full fuller reference email raw format: tformat: mboxrd"
2094 __git_log_date_formats="relative iso8601 iso8601-strict rfc2822 short local default human raw unix auto: format:"
2095
2096 _git_log ()
2097 {
2098 __git_has_doubledash && return
2099 __git_find_repo_path
2100
2101 local merge=""
2102 if __git_pseudoref_exists MERGE_HEAD; then
2103 merge="--merge"
2104 fi
2105 case "$prev,$cur" in
2106 -L,:*:*)
2107 return # fall back to Bash filename completion
2108 ;;
2109 -L,:*)
2110 __git_complete_symbol --cur="${cur#:}" --sfx=":"
2111 return
2112 ;;
2113 -G,*|-S,*)
2114 __git_complete_symbol
2115 return
2116 ;;
2117 esac
2118 case "$cur" in
2119 --pretty=*|--format=*)
2120 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2121 " "" "${cur#*=}"
2122 return
2123 ;;
2124 --date=*)
2125 __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
2126 return
2127 ;;
2128 --decorate=*)
2129 __gitcomp "full short no" "" "${cur##--decorate=}"
2130 return
2131 ;;
2132 --diff-algorithm=*)
2133 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
2134 return
2135 ;;
2136 --submodule=*)
2137 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
2138 return
2139 ;;
2140 --ws-error-highlight=*)
2141 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
2142 return
2143 ;;
2144 --no-walk=*)
2145 __gitcomp "sorted unsorted" "" "${cur##--no-walk=}"
2146 return
2147 ;;
2148 --diff-merges=*)
2149 __gitcomp "$__git_diff_merges_opts" "" "${cur##--diff-merges=}"
2150 return
2151 ;;
2152 --*)
2153 __gitcomp "
2154 $__git_log_common_options
2155 $__git_log_shortlog_options
2156 $__git_log_gitk_options
2157 $__git_log_show_options
2158 --root --topo-order --date-order --reverse
2159 --follow --full-diff
2160 --abbrev-commit --no-abbrev-commit --abbrev=
2161 --relative-date --date=
2162 --pretty= --format= --oneline
2163 --show-signature
2164 --cherry-mark
2165 --cherry-pick
2166 --graph
2167 --decorate --decorate= --no-decorate
2168 --walk-reflogs
2169 --no-walk --no-walk= --do-walk
2170 --parents --children
2171 --expand-tabs --expand-tabs= --no-expand-tabs
2172 $merge
2173 $__git_diff_common_options
2174 "
2175 return
2176 ;;
2177 -L:*:*)
2178 return # fall back to Bash filename completion
2179 ;;
2180 -L:*)
2181 __git_complete_symbol --cur="${cur#-L:}" --sfx=":"
2182 return
2183 ;;
2184 -G*)
2185 __git_complete_symbol --pfx="-G" --cur="${cur#-G}"
2186 return
2187 ;;
2188 -S*)
2189 __git_complete_symbol --pfx="-S" --cur="${cur#-S}"
2190 return
2191 ;;
2192 esac
2193 __git_complete_revlist
2194 }
2195
2196 _git_merge ()
2197 {
2198 __git_complete_strategy && return
2199
2200 case "$cur" in
2201 --*)
2202 __gitcomp_builtin merge
2203 return
2204 esac
2205 __git_complete_refs
2206 }
2207
2208 _git_mergetool ()
2209 {
2210 case "$cur" in
2211 --tool=*)
2212 __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
2213 return
2214 ;;
2215 --*)
2216 __gitcomp "--tool= --prompt --no-prompt --gui --no-gui"
2217 return
2218 ;;
2219 esac
2220 }
2221
2222 _git_merge_base ()
2223 {
2224 case "$cur" in
2225 --*)
2226 __gitcomp_builtin merge-base
2227 return
2228 ;;
2229 esac
2230 __git_complete_refs
2231 }
2232
2233 _git_mv ()
2234 {
2235 case "$cur" in
2236 --*)
2237 __gitcomp_builtin mv
2238 return
2239 ;;
2240 esac
2241
2242 if [ $(__git_count_arguments "mv") -gt 0 ]; then
2243 # We need to show both cached and untracked files (including
2244 # empty directories) since this may not be the last argument.
2245 __git_complete_index_file "--cached --others --directory"
2246 else
2247 __git_complete_index_file "--cached"
2248 fi
2249 }
2250
2251 _git_notes ()
2252 {
2253 local subcommands='add append copy edit get-ref list merge prune remove show'
2254 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2255
2256 case "$subcommand,$cur" in
2257 ,--*)
2258 __gitcomp_builtin notes
2259 ;;
2260 ,*)
2261 case "$prev" in
2262 --ref)
2263 __git_complete_refs
2264 ;;
2265 *)
2266 __gitcomp "$subcommands --ref"
2267 ;;
2268 esac
2269 ;;
2270 *,--reuse-message=*|*,--reedit-message=*)
2271 __git_complete_refs --cur="${cur#*=}"
2272 ;;
2273 *,--*)
2274 __gitcomp_builtin notes_$subcommand
2275 ;;
2276 prune,*|get-ref,*)
2277 # this command does not take a ref, do not complete it
2278 ;;
2279 *)
2280 case "$prev" in
2281 -m|-F)
2282 ;;
2283 *)
2284 __git_complete_refs
2285 ;;
2286 esac
2287 ;;
2288 esac
2289 }
2290
2291 _git_pull ()
2292 {
2293 __git_complete_strategy && return
2294
2295 case "$cur" in
2296 --recurse-submodules=*)
2297 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
2298 return
2299 ;;
2300 --*)
2301 __gitcomp_builtin pull
2302
2303 return
2304 ;;
2305 esac
2306 __git_complete_remote_or_refspec
2307 }
2308
2309 __git_push_recurse_submodules="check on-demand only"
2310
2311 __git_complete_force_with_lease ()
2312 {
2313 local cur_=$1
2314
2315 case "$cur_" in
2316 --*=)
2317 ;;
2318 *:*)
2319 __git_complete_refs --cur="${cur_#*:}"
2320 ;;
2321 *)
2322 __git_complete_refs --cur="$cur_"
2323 ;;
2324 esac
2325 }
2326
2327 _git_push ()
2328 {
2329 case "$prev" in
2330 --repo)
2331 __gitcomp_nl "$(__git_remotes)"
2332 return
2333 ;;
2334 --recurse-submodules)
2335 __gitcomp "$__git_push_recurse_submodules"
2336 return
2337 ;;
2338 esac
2339 case "$cur" in
2340 --repo=*)
2341 __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
2342 return
2343 ;;
2344 --recurse-submodules=*)
2345 __gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
2346 return
2347 ;;
2348 --force-with-lease=*)
2349 __git_complete_force_with_lease "${cur##--force-with-lease=}"
2350 return
2351 ;;
2352 --*)
2353 __gitcomp_builtin push
2354 return
2355 ;;
2356 esac
2357 __git_complete_remote_or_refspec
2358 }
2359
2360 _git_range_diff ()
2361 {
2362 case "$cur" in
2363 --*)
2364 __gitcomp "
2365 --creation-factor= --no-dual-color
2366 $__git_diff_common_options
2367 "
2368 return
2369 ;;
2370 esac
2371 __git_complete_revlist
2372 }
2373
2374 __git_rebase_inprogress_options="--continue --skip --abort --quit --show-current-patch"
2375 __git_rebase_interactive_inprogress_options="$__git_rebase_inprogress_options --edit-todo"
2376
2377 _git_rebase ()
2378 {
2379 __git_find_repo_path
2380 if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
2381 __gitcomp "$__git_rebase_interactive_inprogress_options"
2382 return
2383 elif [ -d "$__git_repo_path"/rebase-apply ] || \
2384 [ -d "$__git_repo_path"/rebase-merge ]; then
2385 __gitcomp "$__git_rebase_inprogress_options"
2386 return
2387 fi
2388 __git_complete_strategy && return
2389 case "$cur" in
2390 --whitespace=*)
2391 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
2392 return
2393 ;;
2394 --onto=*)
2395 __git_complete_refs --cur="${cur##--onto=}"
2396 return
2397 ;;
2398 --*)
2399 __gitcomp_builtin rebase "" \
2400 "$__git_rebase_interactive_inprogress_options"
2401
2402 return
2403 esac
2404 __git_complete_refs
2405 }
2406
2407 _git_reflog ()
2408 {
2409 local subcommands="show delete expire"
2410 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2411
2412 if [ -z "$subcommand" ]; then
2413 __gitcomp "$subcommands"
2414 else
2415 __git_complete_refs
2416 fi
2417 }
2418
2419 __git_send_email_confirm_options="always never auto cc compose"
2420 __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
2421
2422 _git_send_email ()
2423 {
2424 case "$prev" in
2425 --to|--cc|--bcc|--from)
2426 __gitcomp "$(__git send-email --dump-aliases)"
2427 return
2428 ;;
2429 esac
2430
2431 case "$cur" in
2432 --confirm=*)
2433 __gitcomp "
2434 $__git_send_email_confirm_options
2435 " "" "${cur##--confirm=}"
2436 return
2437 ;;
2438 --suppress-cc=*)
2439 __gitcomp "
2440 $__git_send_email_suppresscc_options
2441 " "" "${cur##--suppress-cc=}"
2442
2443 return
2444 ;;
2445 --smtp-encryption=*)
2446 __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
2447 return
2448 ;;
2449 --thread=*)
2450 __gitcomp "
2451 deep shallow
2452 " "" "${cur##--thread=}"
2453 return
2454 ;;
2455 --to=*|--cc=*|--bcc=*|--from=*)
2456 __gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
2457 return
2458 ;;
2459 --*)
2460 __gitcomp_builtin send-email "$__git_format_patch_extra_options"
2461 return
2462 ;;
2463 esac
2464 __git_complete_revlist
2465 }
2466
2467 _git_stage ()
2468 {
2469 _git_add
2470 }
2471
2472 _git_status ()
2473 {
2474 local complete_opt
2475 local untracked_state
2476
2477 case "$cur" in
2478 --ignore-submodules=*)
2479 __gitcomp "none untracked dirty all" "" "${cur##--ignore-submodules=}"
2480 return
2481 ;;
2482 --untracked-files=*)
2483 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
2484 return
2485 ;;
2486 --column=*)
2487 __gitcomp "
2488 always never auto column row plain dense nodense
2489 " "" "${cur##--column=}"
2490 return
2491 ;;
2492 --*)
2493 __gitcomp_builtin status
2494 return
2495 ;;
2496 esac
2497
2498 untracked_state="$(__git_get_option_value "-u" "--untracked-files=" \
2499 "$__git_untracked_file_modes" "status.showUntrackedFiles")"
2500
2501 case "$untracked_state" in
2502 no)
2503 # --ignored option does not matter
2504 complete_opt=
2505 ;;
2506 all|normal|*)
2507 complete_opt="--cached --directory --no-empty-directory --others"
2508
2509 if [ -n "$(__git_find_on_cmdline "--ignored")" ]; then
2510 complete_opt="$complete_opt --ignored --exclude=*"
2511 fi
2512 ;;
2513 esac
2514
2515 __git_complete_index_file "$complete_opt"
2516 }
2517
2518 _git_switch ()
2519 {
2520 local dwim_opt="$(__git_checkout_default_dwim_mode)"
2521
2522 case "$prev" in
2523 -c|-C|--orphan)
2524 # Complete local branches (and DWIM branch
2525 # remote branch names) for an option argument
2526 # specifying a new branch name. This is for
2527 # convenience, assuming new branches are
2528 # possibly based on pre-existing branch names.
2529 __git_complete_refs $dwim_opt --mode="heads"
2530 return
2531 ;;
2532 *)
2533 ;;
2534 esac
2535
2536 case "$cur" in
2537 --conflict=*)
2538 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
2539 ;;
2540 --*)
2541 __gitcomp_builtin switch
2542 ;;
2543 *)
2544 # Unlike in git checkout, git switch --orphan does not take
2545 # a start point. Thus we really have nothing to complete after
2546 # the branch name.
2547 if [ -n "$(__git_find_on_cmdline "--orphan")" ]; then
2548 return
2549 fi
2550
2551 # At this point, we've already handled special completion for
2552 # -c/-C, and --orphan. There are 3 main things left to
2553 # complete:
2554 # 1) a start-point for -c/-C or -d/--detach
2555 # 2) a remote head, for --track
2556 # 3) a branch name, possibly including DWIM remote branches
2557
2558 if [ -n "$(__git_find_on_cmdline "-c -C -d --detach")" ]; then
2559 __git_complete_refs --mode="refs"
2560 elif [ -n "$(__git_find_on_cmdline "-t --track")" ]; then
2561 __git_complete_refs --mode="remote-heads"
2562 else
2563 __git_complete_refs $dwim_opt --mode="heads"
2564 fi
2565 ;;
2566 esac
2567 }
2568
2569 __git_config_get_set_variables ()
2570 {
2571 local prevword word config_file= c=$cword
2572 while [ $c -gt "$__git_cmd_idx" ]; do
2573 word="${words[c]}"
2574 case "$word" in
2575 --system|--global|--local|--file=*)
2576 config_file="$word"
2577 break
2578 ;;
2579 -f|--file)
2580 config_file="$word $prevword"
2581 break
2582 ;;
2583 esac
2584 prevword=$word
2585 c=$((--c))
2586 done
2587
2588 __git config $config_file --name-only --list
2589 }
2590
2591 __git_config_vars=
2592 __git_compute_config_vars ()
2593 {
2594 test -n "$__git_config_vars" ||
2595 __git_config_vars="$(git help --config-for-completion)"
2596 }
2597
2598 __git_config_sections=
2599 __git_compute_config_sections ()
2600 {
2601 test -n "$__git_config_sections" ||
2602 __git_config_sections="$(git help --config-sections-for-completion)"
2603 }
2604
2605 # Completes possible values of various configuration variables.
2606 #
2607 # Usage: __git_complete_config_variable_value [<option>]...
2608 # --varname=<word>: The name of the configuration variable whose value is
2609 # to be completed. Defaults to the previous word on the
2610 # command line.
2611 # --cur=<word>: The current value to be completed. Defaults to the current
2612 # word to be completed.
2613 __git_complete_config_variable_value ()
2614 {
2615 local varname="$prev" cur_="$cur"
2616
2617 while test $# != 0; do
2618 case "$1" in
2619 --varname=*) varname="${1##--varname=}" ;;
2620 --cur=*) cur_="${1##--cur=}" ;;
2621 *) return 1 ;;
2622 esac
2623 shift
2624 done
2625
2626 if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
2627 varname="${varname,,}"
2628 else
2629 varname="$(echo "$varname" |tr A-Z a-z)"
2630 fi
2631
2632 case "$varname" in
2633 branch.*.remote|branch.*.pushremote)
2634 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2635 return
2636 ;;
2637 branch.*.merge)
2638 __git_complete_refs --cur="$cur_"
2639 return
2640 ;;
2641 branch.*.rebase)
2642 __gitcomp "false true merges interactive" "" "$cur_"
2643 return
2644 ;;
2645 remote.pushdefault)
2646 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2647 return
2648 ;;
2649 remote.*.fetch)
2650 local remote="${varname#remote.}"
2651 remote="${remote%.fetch}"
2652 if [ -z "$cur_" ]; then
2653 __gitcomp_nl "refs/heads/" "" "" ""
2654 return
2655 fi
2656 __gitcomp_nl "$(__git_refs_remotes "$remote")" "" "$cur_"
2657 return
2658 ;;
2659 remote.*.push)
2660 local remote="${varname#remote.}"
2661 remote="${remote%.push}"
2662 __gitcomp_nl "$(__git for-each-ref \
2663 --format='%(refname):%(refname)' refs/heads)" "" "$cur_"
2664 return
2665 ;;
2666 pull.twohead|pull.octopus)
2667 __git_compute_merge_strategies
2668 __gitcomp "$__git_merge_strategies" "" "$cur_"
2669 return
2670 ;;
2671 color.pager)
2672 __gitcomp "false true" "" "$cur_"
2673 return
2674 ;;
2675 color.*.*)
2676 __gitcomp "
2677 normal black red green yellow blue magenta cyan white
2678 bold dim ul blink reverse
2679 " "" "$cur_"
2680 return
2681 ;;
2682 color.*)
2683 __gitcomp "false true always never auto" "" "$cur_"
2684 return
2685 ;;
2686 diff.submodule)
2687 __gitcomp "$__git_diff_submodule_formats" "" "$cur_"
2688 return
2689 ;;
2690 help.format)
2691 __gitcomp "man info web html" "" "$cur_"
2692 return
2693 ;;
2694 log.date)
2695 __gitcomp "$__git_log_date_formats" "" "$cur_"
2696 return
2697 ;;
2698 sendemail.aliasfiletype)
2699 __gitcomp "mutt mailrc pine elm gnus" "" "$cur_"
2700 return
2701 ;;
2702 sendemail.confirm)
2703 __gitcomp "$__git_send_email_confirm_options" "" "$cur_"
2704 return
2705 ;;
2706 sendemail.suppresscc)
2707 __gitcomp "$__git_send_email_suppresscc_options" "" "$cur_"
2708 return
2709 ;;
2710 sendemail.transferencoding)
2711 __gitcomp "7bit 8bit quoted-printable base64" "" "$cur_"
2712 return
2713 ;;
2714 *.*)
2715 return
2716 ;;
2717 esac
2718 }
2719
2720 # Completes configuration sections, subsections, variable names.
2721 #
2722 # Usage: __git_complete_config_variable_name [<option>]...
2723 # --cur=<word>: The current configuration section/variable name to be
2724 # completed. Defaults to the current word to be completed.
2725 # --sfx=<suffix>: A suffix to be appended to each fully completed
2726 # configuration variable name (but not to sections or
2727 # subsections) instead of the default space.
2728 __git_complete_config_variable_name ()
2729 {
2730 local cur_="$cur" sfx
2731
2732 while test $# != 0; do
2733 case "$1" in
2734 --cur=*) cur_="${1##--cur=}" ;;
2735 --sfx=*) sfx="${1##--sfx=}" ;;
2736 *) return 1 ;;
2737 esac
2738 shift
2739 done
2740
2741 case "$cur_" in
2742 branch.*.*)
2743 local pfx="${cur_%.*}."
2744 cur_="${cur_##*.}"
2745 __gitcomp "remote pushRemote merge mergeOptions rebase" "$pfx" "$cur_" "$sfx"
2746 return
2747 ;;
2748 branch.*)
2749 local pfx="${cur_%.*}."
2750 cur_="${cur_#*.}"
2751 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
2752 __gitcomp_nl_append $'autoSetupMerge\nautoSetupRebase\n' "$pfx" "$cur_" "${sfx- }"
2753 return
2754 ;;
2755 guitool.*.*)
2756 local pfx="${cur_%.*}."
2757 cur_="${cur_##*.}"
2758 __gitcomp "
2759 argPrompt cmd confirm needsFile noConsole noRescan
2760 prompt revPrompt revUnmerged title
2761 " "$pfx" "$cur_" "$sfx"
2762 return
2763 ;;
2764 difftool.*.*)
2765 local pfx="${cur_%.*}."
2766 cur_="${cur_##*.}"
2767 __gitcomp "cmd path" "$pfx" "$cur_" "$sfx"
2768 return
2769 ;;
2770 man.*.*)
2771 local pfx="${cur_%.*}."
2772 cur_="${cur_##*.}"
2773 __gitcomp "cmd path" "$pfx" "$cur_" "$sfx"
2774 return
2775 ;;
2776 mergetool.*.*)
2777 local pfx="${cur_%.*}."
2778 cur_="${cur_##*.}"
2779 __gitcomp "cmd path trustExitCode" "$pfx" "$cur_" "$sfx"
2780 return
2781 ;;
2782 pager.*)
2783 local pfx="${cur_%.*}."
2784 cur_="${cur_#*.}"
2785 __git_compute_all_commands
2786 __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_" "${sfx- }"
2787 return
2788 ;;
2789 remote.*.*)
2790 local pfx="${cur_%.*}."
2791 cur_="${cur_##*.}"
2792 __gitcomp "
2793 url proxy fetch push mirror skipDefaultUpdate
2794 receivepack uploadpack tagOpt pushurl
2795 " "$pfx" "$cur_" "$sfx"
2796 return
2797 ;;
2798 remote.*)
2799 local pfx="${cur_%.*}."
2800 cur_="${cur_#*.}"
2801 __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
2802 __gitcomp_nl_append "pushDefault" "$pfx" "$cur_" "${sfx- }"
2803 return
2804 ;;
2805 url.*.*)
2806 local pfx="${cur_%.*}."
2807 cur_="${cur_##*.}"
2808 __gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_" "$sfx"
2809 return
2810 ;;
2811 *.*)
2812 __git_compute_config_vars
2813 __gitcomp "$__git_config_vars" "" "$cur_" "$sfx"
2814 ;;
2815 *)
2816 __git_compute_config_sections
2817 __gitcomp "$__git_config_sections" "" "$cur_" "."
2818 ;;
2819 esac
2820 }
2821
2822 # Completes '='-separated configuration sections/variable names and values
2823 # for 'git -c section.name=value'.
2824 #
2825 # Usage: __git_complete_config_variable_name_and_value [<option>]...
2826 # --cur=<word>: The current configuration section/variable name/value to be
2827 # completed. Defaults to the current word to be completed.
2828 __git_complete_config_variable_name_and_value ()
2829 {
2830 local cur_="$cur"
2831
2832 while test $# != 0; do
2833 case "$1" in
2834 --cur=*) cur_="${1##--cur=}" ;;
2835 *) return 1 ;;
2836 esac
2837 shift
2838 done
2839
2840 case "$cur_" in
2841 *=*)
2842 __git_complete_config_variable_value \
2843 --varname="${cur_%%=*}" --cur="${cur_#*=}"
2844 ;;
2845 *)
2846 __git_complete_config_variable_name --cur="$cur_" --sfx='='
2847 ;;
2848 esac
2849 }
2850
2851 _git_config ()
2852 {
2853 case "$prev" in
2854 --get|--get-all|--unset|--unset-all)
2855 __gitcomp_nl "$(__git_config_get_set_variables)"
2856 return
2857 ;;
2858 *.*)
2859 __git_complete_config_variable_value
2860 return
2861 ;;
2862 esac
2863 case "$cur" in
2864 --*)
2865 __gitcomp_builtin config
2866 ;;
2867 *)
2868 __git_complete_config_variable_name
2869 ;;
2870 esac
2871 }
2872
2873 _git_remote ()
2874 {
2875 local subcommands="
2876 add rename remove set-head set-branches
2877 get-url set-url show prune update
2878 "
2879 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2880 if [ -z "$subcommand" ]; then
2881 case "$cur" in
2882 --*)
2883 __gitcomp_builtin remote
2884 ;;
2885 *)
2886 __gitcomp "$subcommands"
2887 ;;
2888 esac
2889 return
2890 fi
2891
2892 case "$subcommand,$cur" in
2893 add,--*)
2894 __gitcomp_builtin remote_add
2895 ;;
2896 add,*)
2897 ;;
2898 set-head,--*)
2899 __gitcomp_builtin remote_set-head
2900 ;;
2901 set-branches,--*)
2902 __gitcomp_builtin remote_set-branches
2903 ;;
2904 set-head,*|set-branches,*)
2905 __git_complete_remote_or_refspec
2906 ;;
2907 update,--*)
2908 __gitcomp_builtin remote_update
2909 ;;
2910 update,*)
2911 __gitcomp "$(__git_remotes) $(__git_get_config_variables "remotes")"
2912 ;;
2913 set-url,--*)
2914 __gitcomp_builtin remote_set-url
2915 ;;
2916 get-url,--*)
2917 __gitcomp_builtin remote_get-url
2918 ;;
2919 prune,--*)
2920 __gitcomp_builtin remote_prune
2921 ;;
2922 *)
2923 __gitcomp_nl "$(__git_remotes)"
2924 ;;
2925 esac
2926 }
2927
2928 _git_replace ()
2929 {
2930 case "$cur" in
2931 --format=*)
2932 __gitcomp "short medium long" "" "${cur##--format=}"
2933 return
2934 ;;
2935 --*)
2936 __gitcomp_builtin replace
2937 return
2938 ;;
2939 esac
2940 __git_complete_refs
2941 }
2942
2943 _git_rerere ()
2944 {
2945 local subcommands="clear forget diff remaining status gc"
2946 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2947 if test -z "$subcommand"
2948 then
2949 __gitcomp "$subcommands"
2950 return
2951 fi
2952 }
2953
2954 _git_reset ()
2955 {
2956 __git_has_doubledash && return
2957
2958 case "$cur" in
2959 --*)
2960 __gitcomp_builtin reset
2961 return
2962 ;;
2963 esac
2964 __git_complete_refs
2965 }
2966
2967 _git_restore ()
2968 {
2969 __git_find_repo_path
2970 case "$prev" in
2971 -s)
2972 __git_complete_refs
2973 return
2974 ;;
2975 esac
2976
2977 case "$cur" in
2978 --conflict=*)
2979 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
2980 ;;
2981 --source=*)
2982 __git_complete_refs --cur="${cur##--source=}"
2983 ;;
2984 --*)
2985 __gitcomp_builtin restore
2986 ;;
2987 *)
2988 if __git_pseudoref_exists HEAD; then
2989 __git_complete_index_file "--modified"
2990 fi
2991 esac
2992 }
2993
2994 __git_revert_inprogress_options=$__git_sequencer_inprogress_options
2995
2996 _git_revert ()
2997 {
2998 __git_find_repo_path
2999 if __git_pseudoref_exists REVERT_HEAD; then
3000 __gitcomp "$__git_revert_inprogress_options"
3001 return
3002 fi
3003 __git_complete_strategy && return
3004 case "$cur" in
3005 --*)
3006 __gitcomp_builtin revert "" \
3007 "$__git_revert_inprogress_options"
3008 return
3009 ;;
3010 esac
3011 __git_complete_refs
3012 }
3013
3014 _git_rm ()
3015 {
3016 case "$cur" in
3017 --*)
3018 __gitcomp_builtin rm
3019 return
3020 ;;
3021 esac
3022
3023 __git_complete_index_file "--cached"
3024 }
3025
3026 _git_shortlog ()
3027 {
3028 __git_has_doubledash && return
3029
3030 case "$cur" in
3031 --*)
3032 __gitcomp "
3033 $__git_log_common_options
3034 $__git_log_shortlog_options
3035 --numbered --summary --email
3036 "
3037 return
3038 ;;
3039 esac
3040 __git_complete_revlist
3041 }
3042
3043 _git_show ()
3044 {
3045 __git_has_doubledash && return
3046
3047 case "$cur" in
3048 --pretty=*|--format=*)
3049 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
3050 " "" "${cur#*=}"
3051 return
3052 ;;
3053 --diff-algorithm=*)
3054 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
3055 return
3056 ;;
3057 --submodule=*)
3058 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
3059 return
3060 ;;
3061 --color-moved=*)
3062 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
3063 return
3064 ;;
3065 --color-moved-ws=*)
3066 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
3067 return
3068 ;;
3069 --ws-error-highlight=*)
3070 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
3071 return
3072 ;;
3073 --diff-merges=*)
3074 __gitcomp "$__git_diff_merges_opts" "" "${cur##--diff-merges=}"
3075 return
3076 ;;
3077 --*)
3078 __gitcomp "--pretty= --format= --abbrev-commit --no-abbrev-commit
3079 --oneline --show-signature
3080 --expand-tabs --expand-tabs= --no-expand-tabs
3081 $__git_log_show_options
3082 $__git_diff_common_options
3083 "
3084 return
3085 ;;
3086 esac
3087 __git_complete_revlist_file
3088 }
3089
3090 _git_show_branch ()
3091 {
3092 case "$cur" in
3093 --*)
3094 __gitcomp_builtin show-branch
3095 return
3096 ;;
3097 esac
3098 __git_complete_revlist
3099 }
3100
3101 __gitcomp_directories ()
3102 {
3103 local _tmp_dir _tmp_completions _found=0
3104
3105 # Get the directory of the current token; this differs from dirname
3106 # in that it keeps up to the final trailing slash. If no slash found
3107 # that's fine too.
3108 [[ "$cur" =~ .*/ ]]
3109 _tmp_dir=$BASH_REMATCH
3110
3111 # Find possible directory completions, adding trailing '/' characters,
3112 # de-quoting, and handling unusual characters.
3113 while IFS= read -r -d $'\0' c ; do
3114 # If there are directory completions, find ones that start
3115 # with "$cur", the current token, and put those in COMPREPLY
3116 if [[ $c == "$cur"* ]]; then
3117 COMPREPLY+=("$c/")
3118 _found=1
3119 fi
3120 done < <(git ls-tree -z -d --name-only HEAD $_tmp_dir)
3121
3122 if [[ $_found == 0 ]] && [[ "$cur" =~ /$ ]]; then
3123 # No possible further completions any deeper, so assume we're at
3124 # a leaf directory and just consider it complete
3125 __gitcomp_direct_append "$cur "
3126 fi
3127 }
3128
3129 _git_sparse_checkout ()
3130 {
3131 local subcommands="list init set disable add reapply"
3132 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3133 if [ -z "$subcommand" ]; then
3134 __gitcomp "$subcommands"
3135 return
3136 fi
3137
3138 case "$subcommand,$cur" in
3139 *,--*)
3140 __gitcomp_builtin sparse-checkout_$subcommand "" "--"
3141 ;;
3142 set,*|add,*)
3143 if [ "$(__git config core.sparseCheckoutCone)" == "true" ] ||
3144 [ -n "$(__git_find_on_cmdline --cone)" ]; then
3145 __gitcomp_directories
3146 fi
3147 esac
3148 }
3149
3150 _git_stash ()
3151 {
3152 local subcommands='push list show apply clear drop pop create branch'
3153 local subcommand="$(__git_find_on_cmdline "$subcommands save")"
3154
3155 if [ -z "$subcommand" ]; then
3156 case "$((cword - __git_cmd_idx)),$cur" in
3157 *,--*)
3158 __gitcomp_builtin stash_push
3159 ;;
3160 1,sa*)
3161 __gitcomp "save"
3162 ;;
3163 1,*)
3164 __gitcomp "$subcommands"
3165 ;;
3166 esac
3167 return
3168 fi
3169
3170 case "$subcommand,$cur" in
3171 list,--*)
3172 # NEEDSWORK: can we somehow unify this with the options in _git_log() and _git_show()
3173 __gitcomp_builtin stash_list "$__git_log_common_options $__git_diff_common_options"
3174 ;;
3175 show,--*)
3176 __gitcomp_builtin stash_show "$__git_diff_common_options"
3177 ;;
3178 *,--*)
3179 __gitcomp_builtin "stash_$subcommand"
3180 ;;
3181 branch,*)
3182 if [ $cword -eq $((__git_cmd_idx+2)) ]; then
3183 __git_complete_refs
3184 else
3185 __gitcomp_nl "$(__git stash list \
3186 | sed -n -e 's/:.*//p')"
3187 fi
3188 ;;
3189 show,*|apply,*|drop,*|pop,*)
3190 __gitcomp_nl "$(__git stash list \
3191 | sed -n -e 's/:.*//p')"
3192 ;;
3193 esac
3194 }
3195
3196 _git_submodule ()
3197 {
3198 __git_has_doubledash && return
3199
3200 local subcommands="add status init deinit update set-branch set-url summary foreach sync absorbgitdirs"
3201 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3202 if [ -z "$subcommand" ]; then
3203 case "$cur" in
3204 --*)
3205 __gitcomp "--quiet"
3206 ;;
3207 *)
3208 __gitcomp "$subcommands"
3209 ;;
3210 esac
3211 return
3212 fi
3213
3214 case "$subcommand,$cur" in
3215 add,--*)
3216 __gitcomp "--branch --force --name --reference --depth"
3217 ;;
3218 status,--*)
3219 __gitcomp "--cached --recursive"
3220 ;;
3221 deinit,--*)
3222 __gitcomp "--force --all"
3223 ;;
3224 update,--*)
3225 __gitcomp "
3226 --init --remote --no-fetch
3227 --recommend-shallow --no-recommend-shallow
3228 --force --rebase --merge --reference --depth --recursive --jobs
3229 "
3230 ;;
3231 set-branch,--*)
3232 __gitcomp "--default --branch"
3233 ;;
3234 summary,--*)
3235 __gitcomp "--cached --files --summary-limit"
3236 ;;
3237 foreach,--*|sync,--*)
3238 __gitcomp "--recursive"
3239 ;;
3240 *)
3241 ;;
3242 esac
3243 }
3244
3245 _git_svn ()
3246 {
3247 local subcommands="
3248 init fetch clone rebase dcommit log find-rev
3249 set-tree commit-diff info create-ignore propget
3250 proplist show-ignore show-externals branch tag blame
3251 migrate mkdirs reset gc
3252 "
3253 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3254 if [ -z "$subcommand" ]; then
3255 __gitcomp "$subcommands"
3256 else
3257 local remote_opts="--username= --config-dir= --no-auth-cache"
3258 local fc_opts="
3259 --follow-parent --authors-file= --repack=
3260 --no-metadata --use-svm-props --use-svnsync-props
3261 --log-window-size= --no-checkout --quiet
3262 --repack-flags --use-log-author --localtime
3263 --add-author-from
3264 --recursive
3265 --ignore-paths= --include-paths= $remote_opts
3266 "
3267 local init_opts="
3268 --template= --shared= --trunk= --tags=
3269 --branches= --stdlayout --minimize-url
3270 --no-metadata --use-svm-props --use-svnsync-props
3271 --rewrite-root= --prefix= $remote_opts
3272 "
3273 local cmt_opts="
3274 --edit --rmdir --find-copies-harder --copy-similarity=
3275 "
3276
3277 case "$subcommand,$cur" in
3278 fetch,--*)
3279 __gitcomp "--revision= --fetch-all $fc_opts"
3280 ;;
3281 clone,--*)
3282 __gitcomp "--revision= $fc_opts $init_opts"
3283 ;;
3284 init,--*)
3285 __gitcomp "$init_opts"
3286 ;;
3287 dcommit,--*)
3288 __gitcomp "
3289 --merge --strategy= --verbose --dry-run
3290 --fetch-all --no-rebase --commit-url
3291 --revision --interactive $cmt_opts $fc_opts
3292 "
3293 ;;
3294 set-tree,--*)
3295 __gitcomp "--stdin $cmt_opts $fc_opts"
3296 ;;
3297 create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
3298 show-externals,--*|mkdirs,--*)
3299 __gitcomp "--revision="
3300 ;;
3301 log,--*)
3302 __gitcomp "
3303 --limit= --revision= --verbose --incremental
3304 --oneline --show-commit --non-recursive
3305 --authors-file= --color
3306 "
3307 ;;
3308 rebase,--*)
3309 __gitcomp "
3310 --merge --verbose --strategy= --local
3311 --fetch-all --dry-run $fc_opts
3312 "
3313 ;;
3314 commit-diff,--*)
3315 __gitcomp "--message= --file= --revision= $cmt_opts"
3316 ;;
3317 info,--*)
3318 __gitcomp "--url"
3319 ;;
3320 branch,--*)
3321 __gitcomp "--dry-run --message --tag"
3322 ;;
3323 tag,--*)
3324 __gitcomp "--dry-run --message"
3325 ;;
3326 blame,--*)
3327 __gitcomp "--git-format"
3328 ;;
3329 migrate,--*)
3330 __gitcomp "
3331 --config-dir= --ignore-paths= --minimize
3332 --no-auth-cache --username=
3333 "
3334 ;;
3335 reset,--*)
3336 __gitcomp "--revision= --parent"
3337 ;;
3338 *)
3339 ;;
3340 esac
3341 fi
3342 }
3343
3344 _git_tag ()
3345 {
3346 local i c="$__git_cmd_idx" f=0
3347 while [ $c -lt $cword ]; do
3348 i="${words[c]}"
3349 case "$i" in
3350 -d|--delete|-v|--verify)
3351 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3352 return
3353 ;;
3354 -f)
3355 f=1
3356 ;;
3357 esac
3358 ((c++))
3359 done
3360
3361 case "$prev" in
3362 -m|-F)
3363 ;;
3364 -*|tag)
3365 if [ $f = 1 ]; then
3366 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3367 fi
3368 ;;
3369 *)
3370 __git_complete_refs
3371 ;;
3372 esac
3373
3374 case "$cur" in
3375 --*)
3376 __gitcomp_builtin tag
3377 ;;
3378 esac
3379 }
3380
3381 _git_whatchanged ()
3382 {
3383 _git_log
3384 }
3385
3386 __git_complete_worktree_paths ()
3387 {
3388 local IFS=$'\n'
3389 # Generate completion reply from worktree list skipping the first
3390 # entry: it's the path of the main worktree, which can't be moved,
3391 # removed, locked, etc.
3392 __gitcomp_nl "$(git worktree list --porcelain |
3393 sed -n -e '2,$ s/^worktree //p')"
3394 }
3395
3396 _git_worktree ()
3397 {
3398 local subcommands="add list lock move prune remove unlock"
3399 local subcommand subcommand_idx
3400
3401 subcommand="$(__git_find_on_cmdline --show-idx "$subcommands")"
3402 subcommand_idx="${subcommand% *}"
3403 subcommand="${subcommand#* }"
3404
3405 case "$subcommand,$cur" in
3406 ,*)
3407 __gitcomp "$subcommands"
3408 ;;
3409 *,--*)
3410 __gitcomp_builtin worktree_$subcommand
3411 ;;
3412 add,*) # usage: git worktree add [<options>] <path> [<commit-ish>]
3413 # Here we are not completing an --option, it's either the
3414 # path or a ref.
3415 case "$prev" in
3416 -b|-B) # Complete refs for branch to be created/reseted.
3417 __git_complete_refs
3418 ;;
3419 -*) # The previous word is an -o|--option without an
3420 # unstuck argument: have to complete the path for
3421 # the new worktree, so don't list anything, but let
3422 # Bash fall back to filename completion.
3423 ;;
3424 *) # The previous word is not an --option, so it must
3425 # be either the 'add' subcommand, the unstuck
3426 # argument of an option (e.g. branch for -b|-B), or
3427 # the path for the new worktree.
3428 if [ $cword -eq $((subcommand_idx+1)) ]; then
3429 # Right after the 'add' subcommand: have to
3430 # complete the path, so fall back to Bash
3431 # filename completion.
3432 :
3433 else
3434 case "${words[cword-2]}" in
3435 -b|-B) # After '-b <branch>': have to
3436 # complete the path, so fall back
3437 # to Bash filename completion.
3438 ;;
3439 *) # After the path: have to complete
3440 # the ref to be checked out.
3441 __git_complete_refs
3442 ;;
3443 esac
3444 fi
3445 ;;
3446 esac
3447 ;;
3448 lock,*|remove,*|unlock,*)
3449 __git_complete_worktree_paths
3450 ;;
3451 move,*)
3452 if [ $cword -eq $((subcommand_idx+1)) ]; then
3453 # The first parameter must be an existing working
3454 # tree to be moved.
3455 __git_complete_worktree_paths
3456 else
3457 # The second parameter is the destination: it could
3458 # be any path, so don't list anything, but let Bash
3459 # fall back to filename completion.
3460 :
3461 fi
3462 ;;
3463 esac
3464 }
3465
3466 __git_complete_common () {
3467 local command="$1"
3468
3469 case "$cur" in
3470 --*)
3471 __gitcomp_builtin "$command"
3472 ;;
3473 esac
3474 }
3475
3476 __git_cmds_with_parseopt_helper=
3477 __git_support_parseopt_helper () {
3478 test -n "$__git_cmds_with_parseopt_helper" ||
3479 __git_cmds_with_parseopt_helper="$(__git --list-cmds=parseopt)"
3480
3481 case " $__git_cmds_with_parseopt_helper " in
3482 *" $1 "*)
3483 return 0
3484 ;;
3485 *)
3486 return 1
3487 ;;
3488 esac
3489 }
3490
3491 __git_have_func () {
3492 declare -f -- "$1" >/dev/null 2>&1
3493 }
3494
3495 __git_complete_command () {
3496 local command="$1"
3497 local completion_func="_git_${command//-/_}"
3498 if ! __git_have_func $completion_func &&
3499 __git_have_func _completion_loader
3500 then
3501 _completion_loader "git-$command"
3502 fi
3503 if __git_have_func $completion_func
3504 then
3505 $completion_func
3506 return 0
3507 elif __git_support_parseopt_helper "$command"
3508 then
3509 __git_complete_common "$command"
3510 return 0
3511 else
3512 return 1
3513 fi
3514 }
3515
3516 __git_main ()
3517 {
3518 local i c=1 command __git_dir __git_repo_path
3519 local __git_C_args C_args_count=0
3520 local __git_cmd_idx
3521
3522 while [ $c -lt $cword ]; do
3523 i="${words[c]}"
3524 case "$i" in
3525 --git-dir=*)
3526 __git_dir="${i#--git-dir=}"
3527 ;;
3528 --git-dir)
3529 ((c++))
3530 __git_dir="${words[c]}"
3531 ;;
3532 --bare)
3533 __git_dir="."
3534 ;;
3535 --help)
3536 command="help"
3537 break
3538 ;;
3539 -c|--work-tree|--namespace)
3540 ((c++))
3541 ;;
3542 -C)
3543 __git_C_args[C_args_count++]=-C
3544 ((c++))
3545 __git_C_args[C_args_count++]="${words[c]}"
3546 ;;
3547 -*)
3548 ;;
3549 *)
3550 command="$i"
3551 __git_cmd_idx="$c"
3552 break
3553 ;;
3554 esac
3555 ((c++))
3556 done
3557
3558 if [ -z "${command-}" ]; then
3559 case "$prev" in
3560 --git-dir|-C|--work-tree)
3561 # these need a path argument, let's fall back to
3562 # Bash filename completion
3563 return
3564 ;;
3565 -c)
3566 __git_complete_config_variable_name_and_value
3567 return
3568 ;;
3569 --namespace)
3570 # we don't support completing these options' arguments
3571 return
3572 ;;
3573 esac
3574 case "$cur" in
3575 --*)
3576 __gitcomp "
3577 --paginate
3578 --no-pager
3579 --git-dir=
3580 --bare
3581 --version
3582 --exec-path
3583 --exec-path=
3584 --html-path
3585 --man-path
3586 --info-path
3587 --work-tree=
3588 --namespace=
3589 --no-replace-objects
3590 --help
3591 "
3592 ;;
3593 *)
3594 if test -n "${GIT_TESTING_PORCELAIN_COMMAND_LIST-}"
3595 then
3596 __gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
3597 else
3598 local list_cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config
3599
3600 if test "${GIT_COMPLETION_SHOW_ALL_COMMANDS-}" = "1"
3601 then
3602 list_cmds=builtins,$list_cmds
3603 fi
3604 __gitcomp "$(__git --list-cmds=$list_cmds)"
3605 fi
3606 ;;
3607 esac
3608 return
3609 fi
3610
3611 __git_complete_command "$command" && return
3612
3613 local expansion=$(__git_aliased_command "$command")
3614 if [ -n "$expansion" ]; then
3615 words[1]=$expansion
3616 __git_complete_command "$expansion"
3617 fi
3618 }
3619
3620 __gitk_main ()
3621 {
3622 __git_has_doubledash && return
3623
3624 local __git_repo_path
3625 __git_find_repo_path
3626
3627 local merge=""
3628 if __git_pseudoref_exists MERGE_HEAD; then
3629 merge="--merge"
3630 fi
3631 case "$cur" in
3632 --*)
3633 __gitcomp "
3634 $__git_log_common_options
3635 $__git_log_gitk_options
3636 $merge
3637 "
3638 return
3639 ;;
3640 esac
3641 __git_complete_revlist
3642 }
3643
3644 if [[ -n ${ZSH_VERSION-} && -z ${GIT_SOURCING_ZSH_COMPLETION-} ]]; then
3645 echo "ERROR: this script is obsolete, please see git-completion.zsh" 1>&2
3646 return
3647 fi
3648
3649 __git_func_wrap ()
3650 {
3651 local cur words cword prev
3652 local __git_cmd_idx=0
3653 _get_comp_words_by_ref -n =: cur words cword prev
3654 $1
3655 }
3656
3657 ___git_complete ()
3658 {
3659 local wrapper="__git_wrap${2}"
3660 eval "$wrapper () { __git_func_wrap $2 ; }"
3661 complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
3662 || complete -o default -o nospace -F $wrapper $1
3663 }
3664
3665 # Setup the completion for git commands
3666 # 1: command or alias
3667 # 2: function to call (e.g. `git`, `gitk`, `git_fetch`)
3668 __git_complete ()
3669 {
3670 local func
3671
3672 if __git_have_func $2; then
3673 func=$2
3674 elif __git_have_func __$2_main; then
3675 func=__$2_main
3676 elif __git_have_func _$2; then
3677 func=_$2
3678 else
3679 echo "ERROR: could not find function '$2'" 1>&2
3680 return 1
3681 fi
3682 ___git_complete $1 $func
3683 }
3684
3685 ___git_complete git __git_main
3686 ___git_complete gitk __gitk_main
3687
3688 # The following are necessary only for Cygwin, and only are needed
3689 # when the user has tab-completed the executable name and consequently
3690 # included the '.exe' suffix.
3691 #
3692 if [ "$OSTYPE" = cygwin ]; then
3693 ___git_complete git.exe __git_main
3694 fi