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