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