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