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