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