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