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