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