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