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 __git_has_doubledash && return
1958
1959 case "$cur" in
1960 --diff-algorithm=*)
1961 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1962 return
1963 ;;
1964 --submodule=*)
1965 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1966 return
1967 ;;
1968 --color-moved=*)
1969 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
1970 return
1971 ;;
1972 --color-moved-ws=*)
1973 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
1974 return
1975 ;;
1976 --ws-error-highlight=*)
1977 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
1978 return
1979 ;;
1980 --*)
1981 __gitcomp "$__git_diff_difftool_options"
1982 return
1983 ;;
1984 esac
1985 __git_complete_revlist_file
1986 }
1987
1988 __git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff
1989 tkdiff vimdiff nvimdiff gvimdiff xxdiff araxis p4merge
1990 bc codecompare smerge
1991 "
1992
1993 _git_difftool ()
1994 {
1995 __git_has_doubledash && return
1996
1997 case "$cur" in
1998 --tool=*)
1999 __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
2000 return
2001 ;;
2002 --*)
2003 __gitcomp_builtin difftool "$__git_diff_difftool_options"
2004 return
2005 ;;
2006 esac
2007 __git_complete_revlist_file
2008 }
2009
2010 __git_fetch_recurse_submodules="yes on-demand no"
2011
2012 _git_fetch ()
2013 {
2014 case "$cur" in
2015 --recurse-submodules=*)
2016 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
2017 return
2018 ;;
2019 --filter=*)
2020 __gitcomp "blob:none blob:limit= sparse:oid=" "" "${cur##--filter=}"
2021 return
2022 ;;
2023 --*)
2024 __gitcomp_builtin fetch
2025 return
2026 ;;
2027 esac
2028 __git_complete_remote_or_refspec
2029 }
2030
2031 __git_format_patch_extra_options="
2032 --full-index --not --all --no-prefix --src-prefix=
2033 --dst-prefix= --notes
2034 "
2035
2036 _git_format_patch ()
2037 {
2038 case "$cur" in
2039 --thread=*)
2040 __gitcomp "
2041 deep shallow
2042 " "" "${cur##--thread=}"
2043 return
2044 ;;
2045 --base=*|--interdiff=*|--range-diff=*)
2046 __git_complete_refs --cur="${cur#--*=}"
2047 return
2048 ;;
2049 --*)
2050 __gitcomp_builtin format-patch "$__git_format_patch_extra_options"
2051 return
2052 ;;
2053 esac
2054 __git_complete_revlist
2055 }
2056
2057 _git_fsck ()
2058 {
2059 case "$cur" in
2060 --*)
2061 __gitcomp_builtin fsck
2062 return
2063 ;;
2064 esac
2065 }
2066
2067 _git_gitk ()
2068 {
2069 __gitk_main
2070 }
2071
2072 # Lists matching symbol names from a tag (as in ctags) file.
2073 # 1: List symbol names matching this word.
2074 # 2: The tag file to list symbol names from.
2075 # 3: A prefix to be added to each listed symbol name (optional).
2076 # 4: A suffix to be appended to each listed symbol name (optional).
2077 __git_match_ctag () {
2078 awk -v pfx="${3-}" -v sfx="${4-}" "
2079 /^${1//\//\\/}/ { print pfx \$1 sfx }
2080 " "$2"
2081 }
2082
2083 # Complete symbol names from a tag file.
2084 # Usage: __git_complete_symbol [<option>]...
2085 # --tags=<file>: The tag file to list symbol names from instead of the
2086 # default "tags".
2087 # --pfx=<prefix>: A prefix to be added to each symbol name.
2088 # --cur=<word>: The current symbol name to be completed. Defaults to
2089 # the current word to be completed.
2090 # --sfx=<suffix>: A suffix to be appended to each symbol name instead
2091 # of the default space.
2092 __git_complete_symbol () {
2093 local tags=tags pfx="" cur_="${cur-}" sfx=" "
2094
2095 while test $# != 0; do
2096 case "$1" in
2097 --tags=*) tags="${1##--tags=}" ;;
2098 --pfx=*) pfx="${1##--pfx=}" ;;
2099 --cur=*) cur_="${1##--cur=}" ;;
2100 --sfx=*) sfx="${1##--sfx=}" ;;
2101 *) return 1 ;;
2102 esac
2103 shift
2104 done
2105
2106 if test -r "$tags"; then
2107 __gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
2108 fi
2109 }
2110
2111 _git_grep ()
2112 {
2113 __git_has_doubledash && return
2114
2115 case "$cur" in
2116 --*)
2117 __gitcomp_builtin grep
2118 return
2119 ;;
2120 esac
2121
2122 case "$cword,$prev" in
2123 $((__git_cmd_idx+1)),*|*,-*)
2124 __git_complete_symbol && return
2125 ;;
2126 esac
2127
2128 __git_complete_refs
2129 }
2130
2131 _git_help ()
2132 {
2133 case "$cur" in
2134 --*)
2135 __gitcomp_builtin help
2136 return
2137 ;;
2138 esac
2139 if test -n "${GIT_TESTING_ALL_COMMAND_LIST-}"
2140 then
2141 __gitcomp "$GIT_TESTING_ALL_COMMAND_LIST $(__git --list-cmds=alias,list-guide) gitk"
2142 else
2143 __gitcomp "$(__git --list-cmds=main,nohelpers,alias,list-guide) gitk"
2144 fi
2145 }
2146
2147 _git_init ()
2148 {
2149 case "$cur" in
2150 --shared=*)
2151 __gitcomp "
2152 false true umask group all world everybody
2153 " "" "${cur##--shared=}"
2154 return
2155 ;;
2156 --*)
2157 __gitcomp_builtin init
2158 return
2159 ;;
2160 esac
2161 }
2162
2163 _git_ls_files ()
2164 {
2165 case "$cur" in
2166 --*)
2167 __gitcomp_builtin ls-files
2168 return
2169 ;;
2170 esac
2171
2172 # XXX ignore options like --modified and always suggest all cached
2173 # files.
2174 __git_complete_index_file "--cached"
2175 }
2176
2177 _git_ls_remote ()
2178 {
2179 case "$cur" in
2180 --*)
2181 __gitcomp_builtin ls-remote
2182 return
2183 ;;
2184 esac
2185 __gitcomp_nl "$(__git_remotes)"
2186 }
2187
2188 _git_ls_tree ()
2189 {
2190 case "$cur" in
2191 --*)
2192 __gitcomp_builtin ls-tree
2193 return
2194 ;;
2195 esac
2196
2197 __git_complete_file
2198 }
2199
2200 # Options that go well for log, shortlog and gitk
2201 __git_log_common_options="
2202 --not --all
2203 --branches --tags --remotes
2204 --first-parent --merges --no-merges
2205 --max-count= --max-count-oldest=
2206 --max-age= --since= --after=
2207 --min-age= --until= --before=
2208 --min-parents= --max-parents=
2209 --no-min-parents --no-max-parents
2210 --alternate-refs --ancestry-path
2211 --author-date-order --basic-regexp
2212 --bisect --boundary --exclude-first-parent-only
2213 --exclude-hidden --extended-regexp
2214 --fixed-strings --grep-reflog
2215 --ignore-missing --left-only --perl-regexp
2216 --reflog --regexp-ignore-case --remove-empty
2217 --right-only --show-linear-break
2218 --show-notes-by-default --show-pulls
2219 --since-as-filter --single-worktree
2220 "
2221 # Options that go well for log and gitk (not shortlog)
2222 __git_log_gitk_options="
2223 --dense --sparse --full-history
2224 --simplify-merges --simplify-by-decoration
2225 --left-right --notes --no-notes
2226 "
2227 # Options that go well for log and shortlog (not gitk)
2228 __git_log_shortlog_options="
2229 --author= --grep= --exclude=
2230 --all-match --invert-grep
2231 "
2232 # Options accepted by log and show
2233 __git_log_show_options="
2234 --diff-merges --diff-merges= --no-diff-merges --dd --remerge-diff
2235 --encoding=
2236 "
2237
2238 __git_diff_merges_opts="off none on first-parent 1 separate m combined c dense-combined cc remerge r"
2239
2240 __git_log_pretty_formats="oneline short medium full fuller reference email raw format: tformat: mboxrd"
2241 __git_log_date_formats="relative iso8601 iso8601-strict rfc2822 short local default human raw unix auto: format:"
2242
2243 # Complete porcelain (i.e. not git-rev-list) options and at least some
2244 # option arguments accepted by git-log. Note that this same set of options
2245 # are also accepted by some other git commands besides git-log.
2246 __git_complete_log_opts ()
2247 {
2248 COMPREPLY=()
2249
2250 local merge=""
2251 if __git_pseudoref_exists MERGE_HEAD; then
2252 merge="--merge"
2253 fi
2254 case "$prev,$cur" in
2255 -L,:*:*)
2256 return # fall back to Bash filename completion
2257 ;;
2258 -L,:*)
2259 __git_complete_symbol --cur="${cur#:}" --sfx=":"
2260 return
2261 ;;
2262 -G,*|-S,*)
2263 __git_complete_symbol
2264 return
2265 ;;
2266 esac
2267 case "$cur" in
2268 --pretty=*|--format=*)
2269 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2270 " "" "${cur#*=}"
2271 return
2272 ;;
2273 --date=*)
2274 __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
2275 return
2276 ;;
2277 --decorate=*)
2278 __gitcomp "full short no" "" "${cur##--decorate=}"
2279 return
2280 ;;
2281 --diff-algorithm=*)
2282 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
2283 return
2284 ;;
2285 --submodule=*)
2286 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
2287 return
2288 ;;
2289 --ws-error-highlight=*)
2290 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
2291 return
2292 ;;
2293 --no-walk=*)
2294 __gitcomp "sorted unsorted" "" "${cur##--no-walk=}"
2295 return
2296 ;;
2297 --diff-merges=*)
2298 __gitcomp "$__git_diff_merges_opts" "" "${cur##--diff-merges=}"
2299 return
2300 ;;
2301 --*)
2302 __gitcomp "
2303 $__git_log_common_options
2304 $__git_log_shortlog_options
2305 $__git_log_gitk_options
2306 $__git_log_show_options
2307 --committer=
2308 --root --topo-order --date-order --reverse
2309 --follow --full-diff
2310 --abbrev-commit --no-abbrev-commit --abbrev=
2311 --relative-date --date=
2312 --pretty= --format= --oneline
2313 --show-signature
2314 --cherry-mark
2315 --cherry-pick
2316 --graph
2317 --decorate --decorate= --no-decorate
2318 --walk-reflogs
2319 --no-walk --no-walk= --do-walk
2320 --parents --children
2321 --expand-tabs --expand-tabs= --no-expand-tabs
2322 --clear-decorations --decorate-refs=
2323 --decorate-refs-exclude=
2324 $merge
2325 $__git_diff_common_options
2326 "
2327 return
2328 ;;
2329 -L:*:*)
2330 return # fall back to Bash filename completion
2331 ;;
2332 -L:*)
2333 __git_complete_symbol --cur="${cur#-L:}" --sfx=":"
2334 return
2335 ;;
2336 -G*)
2337 __git_complete_symbol --pfx="-G" --cur="${cur#-G}"
2338 return
2339 ;;
2340 -S*)
2341 __git_complete_symbol --pfx="-S" --cur="${cur#-S}"
2342 return
2343 ;;
2344 esac
2345 }
2346
2347 _git_log ()
2348 {
2349 __git_has_doubledash && return
2350 __git_find_repo_path
2351
2352 __git_complete_log_opts
2353 [ ${#COMPREPLY[@]} -eq 0 ] || return
2354
2355 __git_complete_revlist
2356 }
2357
2358 _git_merge ()
2359 {
2360 __git_complete_strategy && return
2361
2362 case "$cur" in
2363 --*)
2364 __gitcomp_builtin merge
2365 return
2366 esac
2367 __git_complete_refs
2368 }
2369
2370 _git_mergetool ()
2371 {
2372 case "$cur" in
2373 --tool=*)
2374 __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
2375 return
2376 ;;
2377 --*)
2378 __gitcomp "--tool= --tool-help --prompt --no-prompt --gui --no-gui"
2379 return
2380 ;;
2381 esac
2382 }
2383
2384 _git_merge_base ()
2385 {
2386 case "$cur" in
2387 --*)
2388 __gitcomp_builtin merge-base
2389 return
2390 ;;
2391 esac
2392 __git_complete_refs
2393 }
2394
2395 _git_mv ()
2396 {
2397 case "$cur" in
2398 --*)
2399 __gitcomp_builtin mv
2400 return
2401 ;;
2402 esac
2403
2404 if [ $(__git_count_arguments "mv") -gt 0 ]; then
2405 # We need to show both cached and untracked files (including
2406 # empty directories) since this may not be the last argument.
2407 __git_complete_index_file "--cached --others --directory"
2408 else
2409 __git_complete_index_file "--cached"
2410 fi
2411 }
2412
2413 _git_notes ()
2414 {
2415 local subcommands='add append copy edit get-ref list merge prune remove show'
2416 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2417
2418 case "$subcommand,$cur" in
2419 ,--*)
2420 __gitcomp_builtin notes
2421 ;;
2422 ,*)
2423 case "$prev" in
2424 --ref)
2425 __git_complete_refs
2426 ;;
2427 *)
2428 __gitcomp "$subcommands --ref"
2429 ;;
2430 esac
2431 ;;
2432 *,--reuse-message=*|*,--reedit-message=*)
2433 __git_complete_refs --cur="${cur#*=}"
2434 ;;
2435 *,--*)
2436 __gitcomp_builtin notes_$subcommand
2437 ;;
2438 prune,*|get-ref,*)
2439 # this command does not take a ref, do not complete it
2440 ;;
2441 *)
2442 case "$prev" in
2443 -m|-F)
2444 ;;
2445 *)
2446 __git_complete_refs
2447 ;;
2448 esac
2449 ;;
2450 esac
2451 }
2452
2453 _git_pull ()
2454 {
2455 __git_complete_strategy && return
2456
2457 case "$cur" in
2458 --recurse-submodules=*)
2459 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
2460 return
2461 ;;
2462 --*)
2463 __gitcomp_builtin pull
2464
2465 return
2466 ;;
2467 esac
2468 __git_complete_remote_or_refspec
2469 }
2470
2471 __git_push_recurse_submodules="check on-demand only"
2472
2473 __git_complete_force_with_lease ()
2474 {
2475 local cur_=$1
2476
2477 case "$cur_" in
2478 --*=)
2479 ;;
2480 *:*)
2481 __git_complete_refs --cur="${cur_#*:}"
2482 ;;
2483 *)
2484 __git_complete_refs --cur="$cur_"
2485 ;;
2486 esac
2487 }
2488
2489 _git_push ()
2490 {
2491 case "$prev" in
2492 --repo)
2493 __gitcomp_nl "$(__git_remotes)"
2494 return
2495 ;;
2496 --recurse-submodules)
2497 __gitcomp "$__git_push_recurse_submodules"
2498 return
2499 ;;
2500 esac
2501 case "$cur" in
2502 --repo=*)
2503 __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
2504 return
2505 ;;
2506 --recurse-submodules=*)
2507 __gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
2508 return
2509 ;;
2510 --force-with-lease=*)
2511 __git_complete_force_with_lease "${cur##--force-with-lease=}"
2512 return
2513 ;;
2514 --*)
2515 __gitcomp_builtin push
2516 return
2517 ;;
2518 esac
2519 __git_complete_remote_or_refspec
2520 }
2521
2522 _git_range_diff ()
2523 {
2524 case "$cur" in
2525 --*)
2526 __gitcomp "
2527 --creation-factor= --no-dual-color
2528 $__git_diff_common_options
2529 "
2530 return
2531 ;;
2532 esac
2533 __git_complete_revlist
2534 }
2535
2536 __git_rebase_inprogress_options="--continue --skip --abort --quit --show-current-patch"
2537 __git_rebase_interactive_inprogress_options="$__git_rebase_inprogress_options --edit-todo"
2538
2539 _git_rebase ()
2540 {
2541 __git_find_repo_path
2542 if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
2543 __gitcomp "$__git_rebase_interactive_inprogress_options"
2544 return
2545 elif [ -d "$__git_repo_path"/rebase-apply ] || \
2546 [ -d "$__git_repo_path"/rebase-merge ]; then
2547 __gitcomp "$__git_rebase_inprogress_options"
2548 return
2549 fi
2550 __git_complete_strategy && return
2551 case "$cur" in
2552 --whitespace=*)
2553 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
2554 return
2555 ;;
2556 --onto=*)
2557 __git_complete_refs --cur="${cur##--onto=}"
2558 return
2559 ;;
2560 --*)
2561 __gitcomp_builtin rebase "" \
2562 "$__git_rebase_interactive_inprogress_options"
2563
2564 return
2565 esac
2566 __git_complete_refs
2567 }
2568
2569 _git_reflog ()
2570 {
2571 local subcommands subcommand
2572
2573 __git_resolve_builtins "reflog"
2574
2575 subcommands="$___git_resolved_builtins"
2576 subcommand="$(__git_find_subcommand "$subcommands" "show")"
2577
2578 case "$subcommand,$cur" in
2579 show,--*)
2580 __gitcomp "
2581 $__git_log_common_options
2582 "
2583 return
2584 ;;
2585 $subcommand,--*)
2586 __gitcomp_builtin "reflog_$subcommand"
2587 return
2588 ;;
2589 esac
2590
2591 __git_complete_refs
2592
2593 if [ $((cword - __git_cmd_idx)) -eq 1 ]; then
2594 __gitcompappend "$subcommands" "" "$cur" " "
2595 fi
2596 }
2597
2598 __git_send_email_confirm_options="always never auto cc compose"
2599 __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
2600
2601 _git_send_email ()
2602 {
2603 case "$prev" in
2604 --to|--cc|--bcc|--from)
2605 __gitcomp "$(__git send-email --dump-aliases)"
2606 return
2607 ;;
2608 esac
2609
2610 case "$cur" in
2611 --confirm=*)
2612 __gitcomp "
2613 $__git_send_email_confirm_options
2614 " "" "${cur##--confirm=}"
2615 return
2616 ;;
2617 --suppress-cc=*)
2618 __gitcomp "
2619 $__git_send_email_suppresscc_options
2620 " "" "${cur##--suppress-cc=}"
2621
2622 return
2623 ;;
2624 --smtp-encryption=*)
2625 __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
2626 return
2627 ;;
2628 --thread=*)
2629 __gitcomp "
2630 deep shallow
2631 " "" "${cur##--thread=}"
2632 return
2633 ;;
2634 --to=*|--cc=*|--bcc=*|--from=*)
2635 __gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
2636 return
2637 ;;
2638 --*)
2639 __gitcomp_builtin send-email "$__git_format_patch_extra_options"
2640 return
2641 ;;
2642 esac
2643 __git_complete_revlist
2644 }
2645
2646 _git_stage ()
2647 {
2648 _git_add
2649 }
2650
2651 _git_status ()
2652 {
2653 local complete_opt
2654 local untracked_state
2655
2656 case "$cur" in
2657 --ignore-submodules=*)
2658 __gitcomp "none untracked dirty all" "" "${cur##--ignore-submodules=}"
2659 return
2660 ;;
2661 --untracked-files=*)
2662 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
2663 return
2664 ;;
2665 --column=*)
2666 __gitcomp "
2667 always never auto column row plain dense nodense
2668 " "" "${cur##--column=}"
2669 return
2670 ;;
2671 --*)
2672 __gitcomp_builtin status
2673 return
2674 ;;
2675 esac
2676
2677 untracked_state="$(__git_get_option_value "-u" "--untracked-files=" \
2678 "$__git_untracked_file_modes" "status.showUntrackedFiles")"
2679
2680 case "$untracked_state" in
2681 no)
2682 # --ignored option does not matter
2683 complete_opt=
2684 ;;
2685 all|normal|*)
2686 complete_opt="--cached --directory --no-empty-directory --others"
2687
2688 if [ -n "$(__git_find_on_cmdline "--ignored")" ]; then
2689 complete_opt="$complete_opt --ignored --exclude=*"
2690 fi
2691 ;;
2692 esac
2693
2694 __git_complete_index_file "$complete_opt"
2695 }
2696
2697 _git_switch ()
2698 {
2699 local dwim_opt="$(__git_checkout_default_dwim_mode)"
2700
2701 case "$prev" in
2702 -c|-C|--orphan)
2703 # Complete local branches (and DWIM branch
2704 # remote branch names) for an option argument
2705 # specifying a new branch name. This is for
2706 # convenience, assuming new branches are
2707 # possibly based on pre-existing branch names.
2708 __git_complete_refs $dwim_opt --mode="heads"
2709 return
2710 ;;
2711 *)
2712 ;;
2713 esac
2714
2715 case "$cur" in
2716 --conflict=*)
2717 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
2718 ;;
2719 --*)
2720 __gitcomp_builtin switch
2721 ;;
2722 *)
2723 # Unlike in git checkout, git switch --orphan does not take
2724 # a start point. Thus we really have nothing to complete after
2725 # the branch name.
2726 if [ -n "$(__git_find_on_cmdline "--orphan")" ]; then
2727 return
2728 fi
2729
2730 # At this point, we've already handled special completion for
2731 # -c/-C, and --orphan. There are 3 main things left to
2732 # complete:
2733 # 1) a start-point for -c/-C or -d/--detach
2734 # 2) a remote head, for --track
2735 # 3) a branch name, possibly including DWIM remote branches
2736
2737 if [ -n "$(__git_find_on_cmdline "-c -C -d --detach")" ]; then
2738 __git_complete_refs --mode="refs"
2739 elif [ -n "$(__git_find_on_cmdline "-t --track")" ]; then
2740 __git_complete_refs --mode="remote-heads"
2741 else
2742 __git_complete_refs $dwim_opt --mode="heads"
2743 fi
2744 ;;
2745 esac
2746 }
2747
2748 __git_config_get_set_variables ()
2749 {
2750 local prevword word config_file= c=$cword
2751 while [ $c -gt "$__git_cmd_idx" ]; do
2752 word="${words[c]}"
2753 case "$word" in
2754 --system|--global|--local|--file=*)
2755 config_file="$word"
2756 break
2757 ;;
2758 -f|--file)
2759 config_file="$word $prevword"
2760 break
2761 ;;
2762 esac
2763 prevword=$word
2764 c=$((--c))
2765 done
2766
2767 __git config $config_file --name-only --list
2768 }
2769
2770 __git_config_vars=
2771 __git_compute_config_vars ()
2772 {
2773 test -n "$__git_config_vars" ||
2774 __git_config_vars="$(git help --config-for-completion)"
2775 }
2776
2777 __git_config_vars_all=
2778 __git_compute_config_vars_all ()
2779 {
2780 test -n "$__git_config_vars_all" ||
2781 __git_config_vars_all="$(git --no-pager help --config)"
2782 }
2783
2784 __git_indirect()
2785 {
2786 eval printf '%s' "\"\$$1\""
2787 }
2788
2789 __git_compute_first_level_config_vars_for_section ()
2790 {
2791 local section="$1"
2792 __git_compute_config_vars
2793 local this_section="__git_first_level_config_vars_for_section_${section}"
2794 test -n "$(__git_indirect "${this_section}")" ||
2795 printf -v "__git_first_level_config_vars_for_section_${section}" %s \
2796 "$(echo "$__git_config_vars" | awk -F. "/^${section}\.[a-z]/ { print \$2 }")"
2797 }
2798
2799 __git_compute_second_level_config_vars_for_section ()
2800 {
2801 local section="$1"
2802 __git_compute_config_vars_all
2803 local this_section="__git_second_level_config_vars_for_section_${section}"
2804 test -n "$(__git_indirect "${this_section}")" ||
2805 printf -v "__git_second_level_config_vars_for_section_${section}" %s \
2806 "$(echo "$__git_config_vars_all" | awk -F. "/^${section}\.</ { print \$3 }")"
2807 }
2808
2809 __git_config_sections=
2810 __git_compute_config_sections ()
2811 {
2812 test -n "$__git_config_sections" ||
2813 __git_config_sections="$(git help --config-sections-for-completion)"
2814 }
2815
2816 # Completes possible values of various configuration variables.
2817 #
2818 # Usage: __git_complete_config_variable_value [<option>]...
2819 # --varname=<word>: The name of the configuration variable whose value is
2820 # to be completed. Defaults to the previous word on the
2821 # command line.
2822 # --cur=<word>: The current value to be completed. Defaults to the current
2823 # word to be completed.
2824 __git_complete_config_variable_value ()
2825 {
2826 local varname="$prev" cur_="$cur"
2827
2828 while test $# != 0; do
2829 case "$1" in
2830 --varname=*) varname="${1##--varname=}" ;;
2831 --cur=*) cur_="${1##--cur=}" ;;
2832 *) return 1 ;;
2833 esac
2834 shift
2835 done
2836
2837 if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
2838 varname="${varname,,}"
2839 else
2840 varname="$(echo "$varname" |tr A-Z a-z)"
2841 fi
2842
2843 case "$varname" in
2844 branch.*.remote|branch.*.pushremote)
2845 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2846 return
2847 ;;
2848 branch.*.merge)
2849 __git_complete_refs --cur="$cur_"
2850 return
2851 ;;
2852 branch.*.rebase)
2853 __gitcomp "false true merges interactive" "" "$cur_"
2854 return
2855 ;;
2856 remote.pushdefault)
2857 __gitcomp_nl "$(__git_remotes)" "" "$cur_"
2858 return
2859 ;;
2860 remote.*.fetch)
2861 local remote="${varname#remote.}"
2862 remote="${remote%.fetch}"
2863 if [ -z "$cur_" ]; then
2864 __gitcomp_nl "refs/heads/" "" "" ""
2865 return
2866 fi
2867 __gitcomp_nl "$(__git_refs_remotes "$remote")" "" "$cur_"
2868 return
2869 ;;
2870 remote.*.push)
2871 local remote="${varname#remote.}"
2872 remote="${remote%.push}"
2873 __gitcomp_nl "$(__git for-each-ref \
2874 --format='%(refname):%(refname)' refs/heads)" "" "$cur_"
2875 return
2876 ;;
2877 pull.twohead|pull.octopus)
2878 __git_compute_merge_strategies
2879 __gitcomp "$__git_merge_strategies" "" "$cur_"
2880 return
2881 ;;
2882 color.pager)
2883 __gitcomp "false true" "" "$cur_"
2884 return
2885 ;;
2886 color.*.*)
2887 __gitcomp "
2888 normal black red green yellow blue magenta cyan white
2889 bold dim ul blink reverse
2890 " "" "$cur_"
2891 return
2892 ;;
2893 color.*)
2894 __gitcomp "false true always never auto" "" "$cur_"
2895 return
2896 ;;
2897 diff.submodule)
2898 __gitcomp "$__git_diff_submodule_formats" "" "$cur_"
2899 return
2900 ;;
2901 help.format)
2902 __gitcomp "man info web html" "" "$cur_"
2903 return
2904 ;;
2905 log.date)
2906 __gitcomp "$__git_log_date_formats" "" "$cur_"
2907 return
2908 ;;
2909 sendemail.aliasfiletype)
2910 __gitcomp "mutt mailrc pine elm gnus" "" "$cur_"
2911 return
2912 ;;
2913 sendemail.confirm)
2914 __gitcomp "$__git_send_email_confirm_options" "" "$cur_"
2915 return
2916 ;;
2917 sendemail.suppresscc)
2918 __gitcomp "$__git_send_email_suppresscc_options" "" "$cur_"
2919 return
2920 ;;
2921 sendemail.transferencoding)
2922 __gitcomp "7bit 8bit quoted-printable base64" "" "$cur_"
2923 return
2924 ;;
2925 *.*)
2926 return
2927 ;;
2928 esac
2929 }
2930
2931 # Completes configuration sections, subsections, variable names.
2932 #
2933 # Usage: __git_complete_config_variable_name [<option>]...
2934 # --cur=<word>: The current configuration section/variable name to be
2935 # completed. Defaults to the current word to be completed.
2936 # --sfx=<suffix>: A suffix to be appended to each fully completed
2937 # configuration variable name (but not to sections or
2938 # subsections) instead of the default space.
2939 __git_complete_config_variable_name ()
2940 {
2941 local cur_="$cur" sfx
2942
2943 while test $# != 0; do
2944 case "$1" in
2945 --cur=*) cur_="${1##--cur=}" ;;
2946 --sfx=*) sfx="${1##--sfx=}" ;;
2947 *) return 1 ;;
2948 esac
2949 shift
2950 done
2951
2952 case "$cur_" in
2953 branch.*.*|guitool.*.*|difftool.*.*|man.*.*|mergetool.*.*|remote.*.*|submodule.*.*|url.*.*)
2954 local pfx="${cur_%.*}."
2955 cur_="${cur_##*.}"
2956 local section="${pfx%.*.}"
2957 __git_compute_second_level_config_vars_for_section "${section}"
2958 local this_section="__git_second_level_config_vars_for_section_${section}"
2959 __gitcomp "$(__git_indirect "${this_section}")" "$pfx" "$cur_" "$sfx"
2960 return
2961 ;;
2962 branch.*)
2963 local pfx="${cur_%.*}."
2964 cur_="${cur_#*.}"
2965 local section="${pfx%.}"
2966 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
2967 __git_compute_first_level_config_vars_for_section "${section}"
2968 local this_section="__git_first_level_config_vars_for_section_${section}"
2969 __gitcomp_nl_append "$(__git_indirect "${this_section}")" "$pfx" "$cur_" "${sfx:- }"
2970 return
2971 ;;
2972 pager.*)
2973 local pfx="${cur_%.*}."
2974 cur_="${cur_#*.}"
2975 __git_compute_all_commands
2976 __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_" "${sfx:- }"
2977 return
2978 ;;
2979 remote.*)
2980 local pfx="${cur_%.*}."
2981 cur_="${cur_#*.}"
2982 local section="${pfx%.}"
2983 __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
2984 __git_compute_first_level_config_vars_for_section "${section}"
2985 local this_section="__git_first_level_config_vars_for_section_${section}"
2986 __gitcomp_nl_append "$(__git_indirect "${this_section}")" "$pfx" "$cur_" "${sfx:- }"
2987 return
2988 ;;
2989 submodule.*)
2990 local pfx="${cur_%.*}."
2991 cur_="${cur_#*.}"
2992 local section="${pfx%.}"
2993 __gitcomp_nl "$(__git config -f "$(__git rev-parse --show-toplevel)/.gitmodules" --get-regexp 'submodule.*.path' | awk -F. '{print $2}')" "$pfx" "$cur_" "."
2994 __git_compute_first_level_config_vars_for_section "${section}"
2995 local this_section="__git_first_level_config_vars_for_section_${section}"
2996 __gitcomp_nl_append "$(__git_indirect "${this_section}")" "$pfx" "$cur_" "${sfx:- }"
2997 return
2998 ;;
2999 *.*)
3000 __git_compute_config_vars
3001 __gitcomp "$__git_config_vars" "" "$cur_" "$sfx"
3002 ;;
3003 *)
3004 __git_compute_config_sections
3005 __gitcomp "$__git_config_sections" "" "$cur_" "."
3006 ;;
3007 esac
3008 }
3009
3010 # Completes '='-separated configuration sections/variable names and values
3011 # for 'git -c section.name=value'.
3012 #
3013 # Usage: __git_complete_config_variable_name_and_value [<option>]...
3014 # --cur=<word>: The current configuration section/variable name/value to be
3015 # completed. Defaults to the current word to be completed.
3016 __git_complete_config_variable_name_and_value ()
3017 {
3018 local cur_="$cur"
3019
3020 while test $# != 0; do
3021 case "$1" in
3022 --cur=*) cur_="${1##--cur=}" ;;
3023 *) return 1 ;;
3024 esac
3025 shift
3026 done
3027
3028 case "$cur_" in
3029 *=*)
3030 __git_complete_config_variable_value \
3031 --varname="${cur_%%=*}" --cur="${cur_#*=}"
3032 ;;
3033 *)
3034 __git_complete_config_variable_name --cur="$cur_" --sfx='='
3035 ;;
3036 esac
3037 }
3038
3039 _git_config ()
3040 {
3041 local subcommands subcommand
3042
3043 __git_resolve_builtins "config"
3044
3045 subcommands="$___git_resolved_builtins"
3046 subcommand="$(__git_find_subcommand "$subcommands")"
3047
3048 if [ -z "$subcommand" ]
3049 then
3050 __gitcomp "$subcommands"
3051 return
3052 fi
3053
3054 case "$cur" in
3055 --*)
3056 __gitcomp_builtin "config_$subcommand"
3057 return
3058 ;;
3059 esac
3060
3061 case "$subcommand" in
3062 get)
3063 __gitcomp_nl "$(__git_config_get_set_variables)"
3064 ;;
3065 set)
3066 case "$prev" in
3067 *.*)
3068 __git_complete_config_variable_value
3069 ;;
3070 *)
3071 __git_complete_config_variable_name
3072 ;;
3073 esac
3074 ;;
3075 unset)
3076 __gitcomp_nl "$(__git_config_get_set_variables)"
3077 ;;
3078 esac
3079 }
3080
3081 _git_remote ()
3082 {
3083 local subcommands="
3084 add rename remove set-head set-branches
3085 get-url set-url show prune update
3086 "
3087 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3088 if [ -z "$subcommand" ]; then
3089 case "$cur" in
3090 --*)
3091 __gitcomp_builtin remote
3092 ;;
3093 *)
3094 __gitcomp "$subcommands"
3095 ;;
3096 esac
3097 return
3098 fi
3099
3100 case "$subcommand,$cur" in
3101 add,--*)
3102 __gitcomp_builtin remote_add
3103 ;;
3104 add,*)
3105 ;;
3106 set-head,--*)
3107 __gitcomp_builtin remote_set-head
3108 ;;
3109 set-branches,--*)
3110 __gitcomp_builtin remote_set-branches
3111 ;;
3112 set-head,*|set-branches,*)
3113 __git_complete_remote_or_refspec
3114 ;;
3115 update,--*)
3116 __gitcomp_builtin remote_update
3117 ;;
3118 update,*)
3119 __gitcomp "$(__git_remotes) $(__git_get_config_variables "remotes")"
3120 ;;
3121 set-url,--*)
3122 __gitcomp_builtin remote_set-url
3123 ;;
3124 get-url,--*)
3125 __gitcomp_builtin remote_get-url
3126 ;;
3127 prune,--*)
3128 __gitcomp_builtin remote_prune
3129 ;;
3130 *)
3131 __gitcomp_nl "$(__git_remotes)"
3132 ;;
3133 esac
3134 }
3135
3136 _git_replace ()
3137 {
3138 case "$cur" in
3139 --format=*)
3140 __gitcomp "short medium long" "" "${cur##--format=}"
3141 return
3142 ;;
3143 --*)
3144 __gitcomp_builtin replace
3145 return
3146 ;;
3147 esac
3148 __git_complete_refs
3149 }
3150
3151 _git_rerere ()
3152 {
3153 local subcommands="clear forget diff remaining status gc"
3154 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3155 if test -z "$subcommand"
3156 then
3157 __gitcomp "$subcommands"
3158 return
3159 fi
3160 }
3161
3162 _git_reset ()
3163 {
3164 __git_has_doubledash && return
3165
3166 case "$cur" in
3167 --*)
3168 __gitcomp_builtin reset
3169 return
3170 ;;
3171 esac
3172 __git_complete_refs
3173 }
3174
3175 _git_restore ()
3176 {
3177 case "$prev" in
3178 -s)
3179 __git_complete_refs
3180 return
3181 ;;
3182 esac
3183
3184 case "$cur" in
3185 --conflict=*)
3186 __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
3187 ;;
3188 --source=*)
3189 __git_complete_refs --cur="${cur##--source=}"
3190 ;;
3191 --*)
3192 __gitcomp_builtin restore
3193 ;;
3194 *)
3195 if __git_pseudoref_exists HEAD; then
3196 __git_complete_index_file "--modified"
3197 fi
3198 esac
3199 }
3200
3201 __git_revert_inprogress_options=$__git_sequencer_inprogress_options
3202
3203 _git_revert ()
3204 {
3205 if __git_pseudoref_exists REVERT_HEAD; then
3206 __gitcomp "$__git_revert_inprogress_options"
3207 return
3208 fi
3209 __git_complete_strategy && return
3210 case "$cur" in
3211 --*)
3212 __gitcomp_builtin revert "" \
3213 "$__git_revert_inprogress_options"
3214 return
3215 ;;
3216 esac
3217 __git_complete_refs
3218 }
3219
3220 _git_rm ()
3221 {
3222 case "$cur" in
3223 --*)
3224 __gitcomp_builtin rm
3225 return
3226 ;;
3227 esac
3228
3229 __git_complete_index_file "--cached"
3230 }
3231
3232 _git_shortlog ()
3233 {
3234 __git_has_doubledash && return
3235
3236 case "$cur" in
3237 --*)
3238 __gitcomp "
3239 $__git_log_common_options
3240 $__git_log_shortlog_options
3241 --committer --numbered --summary --email
3242 "
3243 return
3244 ;;
3245 esac
3246 __git_complete_revlist
3247 }
3248
3249 _git_show ()
3250 {
3251 __git_has_doubledash && return
3252
3253 case "$cur" in
3254 --pretty=*|--format=*)
3255 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
3256 " "" "${cur#*=}"
3257 return
3258 ;;
3259 --diff-algorithm=*)
3260 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
3261 return
3262 ;;
3263 --submodule=*)
3264 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
3265 return
3266 ;;
3267 --color-moved=*)
3268 __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
3269 return
3270 ;;
3271 --color-moved-ws=*)
3272 __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
3273 return
3274 ;;
3275 --ws-error-highlight=*)
3276 __gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
3277 return
3278 ;;
3279 --diff-merges=*)
3280 __gitcomp "$__git_diff_merges_opts" "" "${cur##--diff-merges=}"
3281 return
3282 ;;
3283 --*)
3284 __gitcomp "--pretty= --format= --abbrev-commit --no-abbrev-commit
3285 --oneline --show-signature
3286 --expand-tabs --expand-tabs= --no-expand-tabs
3287 $__git_log_show_options
3288 $__git_diff_common_options
3289 "
3290 return
3291 ;;
3292 esac
3293 __git_complete_revlist_file
3294 }
3295
3296 _git_show_branch ()
3297 {
3298 case "$cur" in
3299 --*)
3300 __gitcomp_builtin show-branch
3301 return
3302 ;;
3303 esac
3304 __git_complete_revlist
3305 }
3306
3307 __gitcomp_directories ()
3308 {
3309 local _tmp_dir _tmp_completions _found=0
3310
3311 # Get the directory of the current token; this differs from dirname
3312 # in that it keeps up to the final trailing slash. If no slash found
3313 # that's fine too.
3314 [[ "$cur" =~ .*/ ]]
3315 _tmp_dir=$BASH_REMATCH
3316
3317 # Find possible directory completions, adding trailing '/' characters,
3318 # de-quoting, and handling unusual characters.
3319 while IFS= read -r -d $'\0' c ; do
3320 # If there are directory completions, find ones that start
3321 # with "$cur", the current token, and put those in COMPREPLY
3322 if [[ $c == "$cur"* ]]; then
3323 COMPREPLY+=("$c/")
3324 _found=1
3325 fi
3326 done < <(__git ls-tree -z -d --name-only HEAD $_tmp_dir)
3327
3328 if [[ $_found == 0 ]] && [[ "$cur" =~ /$ ]]; then
3329 # No possible further completions any deeper, so assume we're at
3330 # a leaf directory and just consider it complete
3331 __gitcomp_direct_append "$cur "
3332 elif [[ $_found == 0 ]]; then
3333 # No possible completions found. Avoid falling back to
3334 # bash's default file and directory completion, because all
3335 # valid completions have already been searched and the
3336 # fallbacks can do nothing but mislead. In fact, they can
3337 # mislead in three different ways:
3338 # 1) Fallback file completion makes no sense when asking
3339 # for directory completions, as this function does.
3340 # 2) Fallback directory completion is bad because
3341 # e.g. "/pro" is invalid and should NOT complete to
3342 # "/proc".
3343 # 3) Fallback file/directory completion only completes
3344 # on paths that exist in the current working tree,
3345 # i.e. which are *already* part of their
3346 # sparse-checkout. Thus, normal file and directory
3347 # completion is always useless for "git
3348 # sparse-checkout add" and is also problematic for
3349 # "git sparse-checkout set" unless using it to
3350 # strictly narrow the checkout.
3351 COMPREPLY=( "" )
3352 fi
3353 }
3354
3355 # In non-cone mode, the arguments to {set,add} are supposed to be
3356 # patterns, relative to the toplevel directory. These can be any kind
3357 # of general pattern, like 'subdir/*.c' and we can't complete on all
3358 # of those. However, if the user presses Tab to get tab completion, we
3359 # presume that they are trying to provide a pattern that names a specific
3360 # path.
3361 __gitcomp_slash_leading_paths ()
3362 {
3363 local dequoted_word pfx="" cur_ toplevel
3364
3365 # Since we are dealing with a sparse-checkout, subdirectories may not
3366 # exist in the local working copy. Therefore, we want to run all
3367 # ls-files commands relative to the repository toplevel.
3368 toplevel="$(git rev-parse --show-toplevel)/"
3369
3370 __git_dequote "$cur"
3371
3372 # If the paths provided by the user already start with '/', then
3373 # they are considered relative to the toplevel of the repository
3374 # already. If they do not start with /, then we need to adjust
3375 # them to start with the appropriate prefix.
3376 case "$cur" in
3377 /*)
3378 cur="${cur:1}"
3379 ;;
3380 *)
3381 pfx="$(__git rev-parse --show-prefix)"
3382 esac
3383
3384 # Since sparse-index is limited to cone-mode, in non-cone-mode the
3385 # list of valid paths is precisely the cached files in the index.
3386 #
3387 # NEEDSWORK:
3388 # 1) We probably need to take care of cases where ls-files
3389 # responds with special quoting.
3390 # 2) We probably need to take care of cases where ${cur} has
3391 # some kind of special quoting.
3392 # 3) On top of any quoting from 1 & 2, we have to provide an extra
3393 # level of quoting for any paths that contain a '*', '?', '\',
3394 # '[', ']', or leading '#' or '!' since those will be
3395 # interpreted by sparse-checkout as something other than a
3396 # literal path character.
3397 # Since there are two types of quoting here, this might get really
3398 # complex. For now, just punt on all of this...
3399 completions="$(__git -C "${toplevel}" -c core.quotePath=false \
3400 ls-files --cached -- "${pfx}${cur}*" \
3401 | sed -e s%^%/% -e 's%$% %')"
3402 # Note, above, though that we needed all of the completions to be
3403 # prefixed with a '/', and we want to add a space so that bash
3404 # completion will actually complete an entry and let us move on to
3405 # the next one.
3406
3407 # Return what we've found.
3408 if test -n "$completions"; then
3409 # We found some completions; return them
3410 local IFS=$'\n'
3411 COMPREPLY=($completions)
3412 else
3413 # Do NOT fall back to bash-style all-local-files-and-dirs
3414 # when we find no match. Such options are worse than
3415 # useless:
3416 # 1. "git sparse-checkout add" needs paths that are NOT
3417 # currently in the working copy. "git
3418 # sparse-checkout set" does as well, except in the
3419 # special cases when users are only trying to narrow
3420 # their sparse checkout to a subset of what they
3421 # already have.
3422 #
3423 # 2. A path like '.config' is ambiguous as to whether
3424 # the user wants all '.config' files throughout the
3425 # tree, or just the one under the current directory.
3426 # It would result in a warning from the
3427 # sparse-checkout command due to this. As such, all
3428 # completions of paths should be prefixed with a
3429 # '/'.
3430 #
3431 # 3. We don't want paths prefixed with a '/' to
3432 # complete files in the system root directory, we
3433 # want it to complete on files relative to the
3434 # repository root.
3435 #
3436 # As such, make sure that NO completions are offered rather
3437 # than falling back to bash's default completions.
3438 COMPREPLY=( "" )
3439 fi
3440 }
3441
3442 _git_sparse_checkout ()
3443 {
3444 local subcommands="list init set disable add reapply"
3445 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3446 local using_cone=true
3447 if [ -z "$subcommand" ]; then
3448 __gitcomp "$subcommands"
3449 return
3450 fi
3451
3452 case "$subcommand,$cur" in
3453 *,--*)
3454 __gitcomp_builtin sparse-checkout_$subcommand "" "--"
3455 ;;
3456 set,*|add,*)
3457 if [[ "$(__git config core.sparseCheckout)" == "true" &&
3458 "$(__git config core.sparseCheckoutCone)" == "false" &&
3459 -z "$(__git_find_on_cmdline --cone)" ]]; then
3460 using_cone=false
3461 fi
3462 if [[ -n "$(__git_find_on_cmdline --no-cone)" ]]; then
3463 using_cone=false
3464 fi
3465 if [[ "$using_cone" == "true" ]]; then
3466 __gitcomp_directories
3467 else
3468 __gitcomp_slash_leading_paths
3469 fi
3470 esac
3471 }
3472
3473 _git_stash ()
3474 {
3475 local subcommands='push list show apply clear drop pop create branch import export'
3476 local subcommand="$(__git_find_on_cmdline "$subcommands save")"
3477
3478 if [ -z "$subcommand" ]; then
3479 case "$((cword - __git_cmd_idx)),$cur" in
3480 *,--*)
3481 __gitcomp_builtin stash_push
3482 ;;
3483 1,sa*)
3484 __gitcomp "save"
3485 ;;
3486 1,*)
3487 __gitcomp "$subcommands"
3488 ;;
3489 esac
3490 return
3491 fi
3492
3493 case "$subcommand,$cur" in
3494 list,--*)
3495 # NEEDSWORK: can we somehow unify this with the options in _git_log() and _git_show()
3496 __gitcomp_builtin stash_list "$__git_log_common_options $__git_diff_common_options"
3497 ;;
3498 show,--*)
3499 __gitcomp_builtin stash_show "$__git_diff_common_options"
3500 ;;
3501 export,--*)
3502 __gitcomp_builtin stash_export "--print --to-ref"
3503 ;;
3504 *,--*)
3505 __gitcomp_builtin "stash_$subcommand"
3506 ;;
3507 branch,*)
3508 if [ $cword -eq $((__git_cmd_idx+2)) ]; then
3509 __git_complete_refs
3510 else
3511 __gitcomp_nl "$(__git stash list \
3512 | sed -n -e 's/:.*//p')"
3513 fi
3514 ;;
3515 import,*)
3516 __git_complete_refs
3517 ;;
3518 show,*|apply,*|drop,*|pop,*|export,*)
3519 __gitcomp_nl "$(__git stash list \
3520 | sed -n -e 's/:.*//p')"
3521 ;;
3522 esac
3523 }
3524
3525 _git_submodule ()
3526 {
3527 __git_has_doubledash && return
3528
3529 local subcommands="add status init deinit update set-branch set-url summary foreach sync absorbgitdirs"
3530 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3531 if [ -z "$subcommand" ]; then
3532 case "$cur" in
3533 --*)
3534 __gitcomp "--quiet"
3535 ;;
3536 *)
3537 __gitcomp "$subcommands"
3538 ;;
3539 esac
3540 return
3541 fi
3542
3543 case "$subcommand,$cur" in
3544 add,--*)
3545 __gitcomp "--branch --force --name --reference --depth"
3546 ;;
3547 status,--*)
3548 __gitcomp "--cached --recursive"
3549 ;;
3550 deinit,--*)
3551 __gitcomp "--force --all"
3552 ;;
3553 update,--*)
3554 __gitcomp "
3555 --init --remote --no-fetch
3556 --recommend-shallow --no-recommend-shallow
3557 --force --rebase --merge --reference --depth --recursive --jobs
3558 "
3559 ;;
3560 set-branch,--*)
3561 __gitcomp "--default --branch"
3562 ;;
3563 summary,--*)
3564 __gitcomp "--cached --files --summary-limit"
3565 ;;
3566 foreach,--*|sync,--*)
3567 __gitcomp "--recursive"
3568 ;;
3569 *)
3570 ;;
3571 esac
3572 }
3573
3574 _git_svn ()
3575 {
3576 local subcommands="
3577 init fetch clone rebase dcommit log find-rev
3578 set-tree commit-diff info create-ignore propget
3579 proplist show-ignore show-externals branch tag blame
3580 migrate mkdirs reset gc
3581 "
3582 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3583 if [ -z "$subcommand" ]; then
3584 __gitcomp "$subcommands"
3585 else
3586 local remote_opts="--username= --config-dir= --no-auth-cache"
3587 local fc_opts="
3588 --follow-parent --authors-file= --repack=
3589 --no-metadata --use-svm-props --use-svnsync-props
3590 --log-window-size= --no-checkout --quiet
3591 --repack-flags --use-log-author --localtime
3592 --add-author-from
3593 --recursive
3594 --ignore-paths= --include-paths= $remote_opts
3595 "
3596 local init_opts="
3597 --template= --shared= --trunk= --tags=
3598 --branches= --stdlayout --minimize-url
3599 --no-metadata --use-svm-props --use-svnsync-props
3600 --rewrite-root= --prefix= $remote_opts
3601 "
3602 local cmt_opts="
3603 --edit --rmdir --find-copies-harder --copy-similarity=
3604 "
3605
3606 case "$subcommand,$cur" in
3607 fetch,--*)
3608 __gitcomp "--revision= --fetch-all $fc_opts"
3609 ;;
3610 clone,--*)
3611 __gitcomp "--revision= $fc_opts $init_opts"
3612 ;;
3613 init,--*)
3614 __gitcomp "$init_opts"
3615 ;;
3616 dcommit,--*)
3617 __gitcomp "
3618 --merge --strategy= --verbose --dry-run
3619 --fetch-all --no-rebase --commit-url
3620 --revision --interactive $cmt_opts $fc_opts
3621 "
3622 ;;
3623 set-tree,--*)
3624 __gitcomp "--stdin $cmt_opts $fc_opts"
3625 ;;
3626 create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
3627 show-externals,--*|mkdirs,--*)
3628 __gitcomp "--revision="
3629 ;;
3630 log,--*)
3631 __gitcomp "
3632 --limit= --revision= --verbose --incremental
3633 --oneline --show-commit --non-recursive
3634 --authors-file= --color
3635 "
3636 ;;
3637 rebase,--*)
3638 __gitcomp "
3639 --merge --verbose --strategy= --local
3640 --fetch-all --dry-run $fc_opts
3641 "
3642 ;;
3643 commit-diff,--*)
3644 __gitcomp "--message= --file= --revision= $cmt_opts"
3645 ;;
3646 info,--*)
3647 __gitcomp "--url"
3648 ;;
3649 branch,--*)
3650 __gitcomp "--dry-run --message --tag"
3651 ;;
3652 tag,--*)
3653 __gitcomp "--dry-run --message"
3654 ;;
3655 blame,--*)
3656 __gitcomp "--git-format"
3657 ;;
3658 migrate,--*)
3659 __gitcomp "
3660 --config-dir= --ignore-paths= --minimize
3661 --no-auth-cache --username=
3662 "
3663 ;;
3664 reset,--*)
3665 __gitcomp "--revision= --parent"
3666 ;;
3667 *)
3668 ;;
3669 esac
3670 fi
3671 }
3672
3673 _git_symbolic_ref () {
3674 case "$cur" in
3675 --*)
3676 __gitcomp_builtin symbolic-ref
3677 return
3678 ;;
3679 esac
3680
3681 __git_complete_refs
3682 }
3683
3684 _git_tag ()
3685 {
3686 local i c="$__git_cmd_idx" f=0
3687 while [ $c -lt $cword ]; do
3688 i="${words[c]}"
3689 case "$i" in
3690 -d|--delete|-v|--verify)
3691 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3692 return
3693 ;;
3694 -f)
3695 f=1
3696 ;;
3697 esac
3698 ((c++))
3699 done
3700
3701 case "$prev" in
3702 -m|-F)
3703 ;;
3704 -*|tag)
3705 if [ $f = 1 ]; then
3706 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3707 fi
3708 ;;
3709 *)
3710 __git_complete_refs
3711 ;;
3712 esac
3713
3714 case "$cur" in
3715 --*)
3716 __gitcomp_builtin tag
3717 ;;
3718 esac
3719 }
3720
3721 _git_whatchanged ()
3722 {
3723 _git_log
3724 }
3725
3726 __git_complete_worktree_paths ()
3727 {
3728 local IFS=$'\n'
3729 # Generate completion reply from worktree list skipping the first
3730 # entry: it's the path of the main worktree, which can't be moved,
3731 # removed, locked, etc.
3732 __gitcomp_nl "$(__git worktree list --porcelain |
3733 sed -n -e '2,$ s/^worktree //p')"
3734 }
3735
3736 _git_worktree ()
3737 {
3738 local subcommands="add list lock move prune remove unlock"
3739 local subcommand subcommand_idx
3740
3741 subcommand="$(__git_find_on_cmdline --show-idx "$subcommands")"
3742 subcommand_idx="${subcommand% *}"
3743 subcommand="${subcommand#* }"
3744
3745 case "$subcommand,$cur" in
3746 ,*)
3747 __gitcomp "$subcommands"
3748 ;;
3749 *,--*)
3750 __gitcomp_builtin worktree_$subcommand
3751 ;;
3752 add,*) # usage: git worktree add [<options>] <path> [<commit-ish>]
3753 # Here we are not completing an --option, it's either the
3754 # path or a ref.
3755 case "$prev" in
3756 -b|-B) # Complete refs for branch to be created/reset.
3757 __git_complete_refs
3758 ;;
3759 -*) # The previous word is an -o|--option without an
3760 # unstuck argument: have to complete the path for
3761 # the new worktree, so don't list anything, but let
3762 # Bash fall back to filename completion.
3763 ;;
3764 *) # The previous word is not an --option, so it must
3765 # be either the 'add' subcommand, the unstuck
3766 # argument of an option (e.g. branch for -b|-B), or
3767 # the path for the new worktree.
3768 if [ $cword -eq $((subcommand_idx+1)) ]; then
3769 # Right after the 'add' subcommand: have to
3770 # complete the path, so fall back to Bash
3771 # filename completion.
3772 :
3773 else
3774 case "${words[cword-2]}" in
3775 -b|-B) # After '-b <branch>': have to
3776 # complete the path, so fall back
3777 # to Bash filename completion.
3778 ;;
3779 *) # After the path: have to complete
3780 # the ref to be checked out.
3781 __git_complete_refs
3782 ;;
3783 esac
3784 fi
3785 ;;
3786 esac
3787 ;;
3788 lock,*|remove,*|unlock,*)
3789 __git_complete_worktree_paths
3790 ;;
3791 move,*)
3792 if [ $cword -eq $((subcommand_idx+1)) ]; then
3793 # The first parameter must be an existing working
3794 # tree to be moved.
3795 __git_complete_worktree_paths
3796 else
3797 # The second parameter is the destination: it could
3798 # be any path, so don't list anything, but let Bash
3799 # fall back to filename completion.
3800 :
3801 fi
3802 ;;
3803 esac
3804 }
3805
3806 __git_complete_common () {
3807 local command="$1"
3808
3809 case "$cur" in
3810 --*)
3811 __gitcomp_builtin "$command"
3812 ;;
3813 esac
3814 }
3815
3816 __git_cmds_with_parseopt_helper=
3817 __git_support_parseopt_helper () {
3818 test -n "$__git_cmds_with_parseopt_helper" ||
3819 __git_cmds_with_parseopt_helper="$(__git --list-cmds=parseopt)"
3820
3821 case " $__git_cmds_with_parseopt_helper " in
3822 *" $1 "*)
3823 return 0
3824 ;;
3825 *)
3826 return 1
3827 ;;
3828 esac
3829 }
3830
3831 __git_have_func () {
3832 declare -f -- "$1" >/dev/null 2>&1
3833 }
3834
3835 __git_complete_command () {
3836 local command="$1"
3837 local completion_func="_git_${command//-/_}"
3838 if ! __git_have_func $completion_func &&
3839 __git_have_func _completion_loader
3840 then
3841 _completion_loader "git-$command"
3842 fi
3843 if __git_have_func $completion_func
3844 then
3845 $completion_func
3846 return 0
3847 elif __git_support_parseopt_helper "$command"
3848 then
3849 __git_complete_common "$command"
3850 return 0
3851 else
3852 return 1
3853 fi
3854 }
3855
3856 __git_main ()
3857 {
3858 local i c=1 command __git_dir __git_repo_path
3859 local __git_C_args C_args_count=0
3860 local __git_cmd_idx
3861
3862 while [ $c -lt $cword ]; do
3863 i="${words[c]}"
3864 case "$i" in
3865 --git-dir=*)
3866 __git_dir="${i#--git-dir=}"
3867 ;;
3868 --git-dir)
3869 ((c++))
3870 __git_dir="${words[c]}"
3871 ;;
3872 --bare)
3873 __git_dir="."
3874 ;;
3875 --help)
3876 command="help"
3877 break
3878 ;;
3879 -c|--work-tree|--namespace)
3880 ((c++))
3881 ;;
3882 -C)
3883 __git_C_args[C_args_count++]=-C
3884 ((c++))
3885 __git_C_args[C_args_count++]="${words[c]}"
3886 ;;
3887 -*)
3888 ;;
3889 *)
3890 command="$i"
3891 __git_cmd_idx="$c"
3892 break
3893 ;;
3894 esac
3895 ((c++))
3896 done
3897
3898 if [ -z "${command-}" ]; then
3899 case "$prev" in
3900 --git-dir|-C|--work-tree)
3901 # these need a path argument, let's fall back to
3902 # Bash filename completion
3903 return
3904 ;;
3905 -c)
3906 __git_complete_config_variable_name_and_value
3907 return
3908 ;;
3909 --namespace)
3910 # we don't support completing these options' arguments
3911 return
3912 ;;
3913 esac
3914 case "$cur" in
3915 --*)
3916 __gitcomp "
3917 --paginate
3918 --no-pager
3919 --git-dir=
3920 --bare
3921 --version
3922 --exec-path
3923 --exec-path=
3924 --html-path
3925 --man-path
3926 --info-path
3927 --work-tree=
3928 --namespace=
3929 --no-replace-objects
3930 --help
3931 "
3932 ;;
3933 *)
3934 if test -n "${GIT_TESTING_PORCELAIN_COMMAND_LIST-}"
3935 then
3936 __gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
3937 else
3938 local list_cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config
3939
3940 if test "${GIT_COMPLETION_SHOW_ALL_COMMANDS-}" = "1"
3941 then
3942 list_cmds=builtins,$list_cmds
3943 fi
3944 __gitcomp "$(__git --list-cmds=$list_cmds)"
3945 fi
3946 ;;
3947 esac
3948 return
3949 fi
3950
3951 __git_complete_command "$command" && return
3952
3953 local expansion=$(__git_aliased_command "$command")
3954 if [ -n "$expansion" ]; then
3955 words[1]=$expansion
3956 __git_complete_command "$expansion"
3957 fi
3958 }
3959
3960 __gitk_main ()
3961 {
3962 __git_has_doubledash && return
3963
3964 local __git_repo_path
3965 __git_find_repo_path
3966
3967 local merge=""
3968 if __git_pseudoref_exists MERGE_HEAD; then
3969 merge="--merge"
3970 fi
3971 case "$cur" in
3972 --*)
3973 __gitcomp "
3974 $__git_log_common_options
3975 $__git_log_gitk_options
3976 $merge
3977 "
3978 return
3979 ;;
3980 esac
3981 __git_complete_revlist
3982 }
3983
3984 if [[ -n ${ZSH_VERSION-} && -z ${GIT_SOURCING_ZSH_COMPLETION-} ]]; then
3985 echo "ERROR: this script is obsolete, please see git-completion.zsh" 1>&2
3986 return
3987 fi
3988
3989 __git_func_wrap ()
3990 {
3991 local cur words cword prev
3992 local __git_cmd_idx=0
3993 _get_comp_words_by_ref -n =: cur words cword prev
3994 $1
3995 }
3996
3997 ___git_complete ()
3998 {
3999 local wrapper="__git_wrap${2}"
4000 eval "$wrapper () { __git_func_wrap $2 ; }"
4001 complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
4002 || complete -o default -o nospace -F $wrapper $1
4003 }
4004
4005 # Setup the completion for git commands
4006 # 1: command or alias
4007 # 2: function to call (e.g. `git`, `gitk`, `git_fetch`)
4008 __git_complete ()
4009 {
4010 local func
4011
4012 if __git_have_func $2; then
4013 func=$2
4014 elif __git_have_func __$2_main; then
4015 func=__$2_main
4016 elif __git_have_func _$2; then
4017 func=_$2
4018 else
4019 echo "ERROR: could not find function '$2'" 1>&2
4020 return 1
4021 fi
4022 ___git_complete $1 $func
4023 }
4024
4025 ___git_complete git __git_main
4026 ___git_complete gitk __gitk_main
4027
4028 # The following are necessary only for Cygwin, and only are needed
4029 # when the user has tab-completed the executable name and consequently
4030 # included the '.exe' suffix.
4031 #
4032 if [ "$OSTYPE" = cygwin ]; then
4033 ___git_complete git.exe __git_main
4034 fi