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