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