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