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