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