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