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