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