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