git-completion.bash: add completion for stash list
[git/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 ; ... '".
60e71bbc 31#
d23bb387
NTND
32# Compatible with bash 3.2.57.
33#
60e71bbc
JK
34# You can set the following environment variables to influence the behavior of
35# the completion routines:
36#
37# GIT_COMPLETION_CHECKOUT_NO_GUESS
38#
39# When set to "1", do not include "DWIM" suggestions in git-checkout
40# completion (e.g., completing "foo" when "origin/foo" exists).
690d8824 41
db8a9ff0
SP
42case "$COMP_WORDBREAKS" in
43*:*) : great ;;
44*) COMP_WORDBREAKS="$COMP_WORDBREAKS:"
45esac
46
beb6ee71
SG
47# Discovers the path to the git repository taking any '--git-dir=<path>' and
48# '-C <path>' options into account and stores it in the $__git_repo_path
49# variable.
50__git_find_repo_path ()
51{
fad9484f
SG
52 if [ -n "$__git_repo_path" ]; then
53 # we already know where it is
54 return
55 fi
56
beb6ee71
SG
57 if [ -n "${__git_C_args-}" ]; then
58 __git_repo_path="$(git "${__git_C_args[@]}" \
59 ${__git_dir:+--git-dir="$__git_dir"} \
60 rev-parse --absolute-git-dir 2>/dev/null)"
61 elif [ -n "${__git_dir-}" ]; then
62 test -d "$__git_dir" &&
63 __git_repo_path="$__git_dir"
64 elif [ -n "${GIT_DIR-}" ]; then
65 test -d "${GIT_DIR-}" &&
66 __git_repo_path="$GIT_DIR"
67 elif [ -d .git ]; then
68 __git_repo_path=.git
69 else
70 __git_repo_path="$(git rev-parse --git-dir 2>/dev/null)"
71 fi
72}
73
fad9484f 74# Deprecated: use __git_find_repo_path() and $__git_repo_path instead
a42577d4
TP
75# __gitdir accepts 0 or 1 arguments (i.e., location)
76# returns location of .git repo
873537fa
SP
77__gitdir ()
78{
25a31f81 79 if [ -z "${1-}" ]; then
beb6ee71
SG
80 __git_find_repo_path || return 1
81 echo "$__git_repo_path"
67ffa114
SP
82 elif [ -d "$1/.git" ]; then
83 echo "$1/.git"
84 else
85 echo "$1"
86 fi
873537fa
SP
87}
88
1cd23e9e
SG
89# Runs git with all the options given as argument, respecting any
90# '--git-dir=<path>' and '-C <path>' options present on the command line
91__git ()
92{
93 git ${__git_C_args:+"${__git_C_args[@]}"} \
e15098a3 94 ${__git_dir:+--git-dir="$__git_dir"} "$@" 2>/dev/null
1cd23e9e
SG
95}
96
f12785a3
SG
97# Removes backslash escaping, single quotes and double quotes from a word,
98# stores the result in the variable $dequoted_word.
99# 1: The word to dequote.
100__git_dequote ()
101{
102 local rest="$1" len ch
103
104 dequoted_word=""
105
106 while test -n "$rest"; do
107 len=${#dequoted_word}
108 dequoted_word="$dequoted_word${rest%%[\\\'\"]*}"
109 rest="${rest:$((${#dequoted_word}-$len))}"
110
111 case "${rest:0:1}" in
112 \\)
113 ch="${rest:1:1}"
114 case "$ch" in
115 $'\n')
116 ;;
117 *)
118 dequoted_word="$dequoted_word$ch"
119 ;;
120 esac
121 rest="${rest:2}"
122 ;;
123 \')
124 rest="${rest:1}"
125 len=${#dequoted_word}
126 dequoted_word="$dequoted_word${rest%%\'*}"
127 rest="${rest:$((${#dequoted_word}-$len+1))}"
128 ;;
129 \")
130 rest="${rest:1}"
131 while test -n "$rest" ; do
132 len=${#dequoted_word}
133 dequoted_word="$dequoted_word${rest%%[\\\"]*}"
134 rest="${rest:$((${#dequoted_word}-$len))}"
135 case "${rest:0:1}" in
136 \\)
137 ch="${rest:1:1}"
138 case "$ch" in
139 \"|\\|\$|\`)
140 dequoted_word="$dequoted_word$ch"
141 ;;
142 $'\n')
143 ;;
144 *)
145 dequoted_word="$dequoted_word\\$ch"
146 ;;
147 esac
148 rest="${rest:2}"
149 ;;
150 \")
151 rest="${rest:1}"
152 break
153 ;;
154 esac
155 done
156 ;;
157 esac
158 done
159}
160
dbda3b10
JN
161# The following function is based on code from:
162#
163# bash_completion - programmable completion functions for bash 3.2+
164#
165# Copyright © 2006-2008, Ian Macdonald <ian@caliban.org>
166# © 2009-2010, Bash Completion Maintainers
167# <bash-completion-devel@lists.alioth.debian.org>
168#
169# This program is free software; you can redistribute it and/or modify
170# it under the terms of the GNU General Public License as published by
171# the Free Software Foundation; either version 2, or (at your option)
172# any later version.
173#
174# This program is distributed in the hope that it will be useful,
175# but WITHOUT ANY WARRANTY; without even the implied warranty of
176# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
177# GNU General Public License for more details.
178#
179# You should have received a copy of the GNU General Public License
48425792 180# along with this program; if not, see <http://www.gnu.org/licenses/>.
dbda3b10
JN
181#
182# The latest version of this software can be obtained here:
183#
184# http://bash-completion.alioth.debian.org/
185#
186# RELEASE: 2.x
187
188# This function can be used to access a tokenized list of words
189# on the command line:
190#
191# __git_reassemble_comp_words_by_ref '=:'
192# if test "${words_[cword_-1]}" = -w
193# then
194# ...
195# fi
196#
197# The argument should be a collection of characters from the list of
198# word completion separators (COMP_WORDBREAKS) to treat as ordinary
199# characters.
200#
201# This is roughly equivalent to going back in time and setting
202# COMP_WORDBREAKS to exclude those characters. The intent is to
203# make option types like --date=<type> and <rev>:<path> easy to
204# recognize by treating each shell word as a single token.
205#
206# It is best not to set COMP_WORDBREAKS directly because the value is
207# shared with other completion scripts. By the time the completion
208# function gets called, COMP_WORDS has already been populated so local
209# changes to COMP_WORDBREAKS have no effect.
210#
211# Output: words_, cword_, cur_.
212
213__git_reassemble_comp_words_by_ref()
214{
215 local exclude i j first
216 # Which word separators to exclude?
217 exclude="${1//[^$COMP_WORDBREAKS]}"
218 cword_=$COMP_CWORD
219 if [ -z "$exclude" ]; then
220 words_=("${COMP_WORDS[@]}")
221 return
222 fi
223 # List of word completion separators has shrunk;
224 # re-assemble words to complete.
225 for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
226 # Append each nonempty word consisting of just
227 # word separator characters to the current word.
228 first=t
229 while
230 [ $i -gt 0 ] &&
231 [ -n "${COMP_WORDS[$i]}" ] &&
232 # word consists of excluded word separators
233 [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
234 do
235 # Attach to the previous token,
236 # unless the previous token is the command name.
237 if [ $j -ge 2 ] && [ -n "$first" ]; then
238 ((j--))
239 fi
240 first=
241 words_[$j]=${words_[j]}${COMP_WORDS[i]}
242 if [ $i = $COMP_CWORD ]; then
243 cword_=$j
244 fi
245 if (($i < ${#COMP_WORDS[@]} - 1)); then
246 ((i++))
247 else
248 # Done.
249 return
250 fi
251 done
252 words_[$j]=${words_[j]}${COMP_WORDS[i]}
253 if [ $i = $COMP_CWORD ]; then
254 cword_=$j
255 fi
256 done
257}
258
da48616f
PD
259if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
260_get_comp_words_by_ref ()
261{
dbda3b10
JN
262 local exclude cur_ words_ cword_
263 if [ "$1" = "-n" ]; then
264 exclude=$2
265 shift 2
266 fi
267 __git_reassemble_comp_words_by_ref "$exclude"
268 cur_=${words_[cword_]}
da48616f
PD
269 while [ $# -gt 0 ]; do
270 case "$1" in
271 cur)
dbda3b10 272 cur=$cur_
da48616f
PD
273 ;;
274 prev)
dbda3b10 275 prev=${words_[$cword_-1]}
da48616f
PD
276 ;;
277 words)
dbda3b10 278 words=("${words_[@]}")
da48616f
PD
279 ;;
280 cword)
dbda3b10 281 cword=$cword_
da48616f
PD
282 ;;
283 esac
284 shift
285 done
286}
287fi
288
fef56eb0
SG
289# Fills the COMPREPLY array with prefiltered words without any additional
290# processing.
291# Callers must take care of providing only words that match the current word
292# to be completed and adding any prefix and/or suffix (trailing space!), if
293# necessary.
294# 1: List of newline-separated matching completion words, complete with
295# prefix and suffix.
296__gitcomp_direct ()
297{
298 local IFS=$'\n'
299
300 COMPREPLY=($1)
301}
302
f33c2c0f 303__gitcompappend ()
1ce23aad 304{
852ff1c3 305 local x i=${#COMPREPLY[@]}
7d13e0a3
FC
306 for x in $1; do
307 if [[ "$x" == "$3"* ]]; then
308 COMPREPLY[i++]="$2$x$4"
309 fi
310 done
1ce23aad
FC
311}
312
f33c2c0f
RR
313__gitcompadd ()
314{
315 COMPREPLY=()
316 __gitcompappend "$@"
317}
318
7d13e0a3
FC
319# Generates completion reply, appending a space to possible completion words,
320# if necessary.
f674bb80
SG
321# It accepts 1 to 4 arguments:
322# 1: List of possible completion words.
323# 2: A prefix to be added to each possible completion word (optional).
324# 3: Generate possible completion matches for this word (optional).
325# 4: A suffix to be appended to each possible completion word (optional).
72e5e989
SP
326__gitcomp ()
327{
583e4d57 328 local cur_="${3-$cur}"
9244d69b 329
9244d69b 330 case "$cur_" in
5447aac7 331 --*=)
5447aac7 332 ;;
b221b5ab
NTND
333 --no-*)
334 local c i=0 IFS=$' \t\n'
335 for c in $1; do
336 if [[ $c == "--" ]]; then
337 continue
338 fi
339 c="$c${4-}"
340 if [[ $c == "$cur_"* ]]; then
341 case $c in
342 --*=*|*.) ;;
343 *) c="$c " ;;
344 esac
345 COMPREPLY[i++]="${2-}$c"
346 fi
347 done
348 ;;
5447aac7 349 *)
b4cfbc96
FC
350 local c i=0 IFS=$' \t\n'
351 for c in $1; do
b221b5ab
NTND
352 if [[ $c == "--" ]]; then
353 c="--no-...${4-}"
354 if [[ $c == "$cur_"* ]]; then
355 COMPREPLY[i++]="${2-}$c "
356 fi
357 break
358 fi
b4cfbc96 359 c="$c${4-}"
b4cfbc96 360 if [[ $c == "$cur_"* ]]; then
ddc996d7
FC
361 case $c in
362 --*=*|*.) ;;
363 *) c="$c " ;;
364 esac
b4cfbc96
FC
365 COMPREPLY[i++]="${2-}$c"
366 fi
367 done
5447aac7
SG
368 ;;
369 esac
72e5e989
SP
370}
371
8b0eaa41
SG
372# Clear the variables caching builtins' options when (re-)sourcing
373# the completion script.
94408dc7
SG
374if [[ -n ${ZSH_VERSION-} ]]; then
375 unset $(set |sed -ne 's/^\(__gitcomp_builtin_[a-zA-Z0-9_][a-zA-Z0-9_]*\)=.*/\1/p') 2>/dev/null
376else
377 unset $(compgen -v __gitcomp_builtin_)
378fi
8b0eaa41 379
d401f3de
NTND
380# This function is equivalent to
381#
382# __gitcomp "$(git xxx --git-completion-helper) ..."
383#
384# except that the output is cached. Accept 1-3 arguments:
385# 1: the git command to execute, this is also the cache key
386# 2: extra options to be added on top (e.g. negative forms)
387# 3: options to be excluded
388__gitcomp_builtin ()
389{
390 # spaces must be replaced with underscore for multi-word
391 # commands, e.g. "git remote add" becomes remote_add.
392 local cmd="$1"
393 local incl="$2"
394 local excl="$3"
395
396 local var=__gitcomp_builtin_"${cmd/-/_}"
397 local options
398 eval "options=\$$var"
399
400 if [ -z "$options" ]; then
401 # leading and trailing spaces are significant to make
402 # option removal work correctly.
403 options=" $(__git ${cmd/_/ } --git-completion-helper) $incl "
404 for i in $excl; do
405 options="${options/ $i / }"
406 done
407 eval "$var=\"$options\""
408 fi
409
410 __gitcomp "$options"
411}
412
f33c2c0f
RR
413# Variation of __gitcomp_nl () that appends to the existing list of
414# completion candidates, COMPREPLY.
415__gitcomp_nl_append ()
416{
417 local IFS=$'\n'
418 __gitcompappend "$1" "${2-}" "${3-$cur}" "${4- }"
419}
420
7d13e0a3
FC
421# Generates completion reply from newline-separated possible completion words
422# by appending a space to all of them.
a31e6262
SG
423# It accepts 1 to 4 arguments:
424# 1: List of possible completion words, separated by a single newline.
425# 2: A prefix to be added to each possible completion word (optional).
426# 3: Generate possible completion matches for this word (optional).
427# 4: A suffix to be appended to each possible completion word instead of
428# the default space (optional). If specified but empty, nothing is
429# appended.
430__gitcomp_nl ()
431{
f33c2c0f
RR
432 COMPREPLY=()
433 __gitcomp_nl_append "$@"
a31e6262
SG
434}
435
7b003420
SG
436# Fills the COMPREPLY array with prefiltered paths without any additional
437# processing.
438# Callers must take care of providing only paths that match the current path
439# to be completed and adding any prefix path components, if necessary.
440# 1: List of newline-separated matching paths, complete with all prefix
441# path componens.
442__gitcomp_file_direct ()
443{
444 local IFS=$'\n'
445
446 COMPREPLY=($1)
447
448 # use a hack to enable file mode in bash < 4
449 compopt -o filenames +o nospace 2>/dev/null ||
8b4c2e0b
SG
450 compgen -f /non-existing-dir/ >/dev/null ||
451 true
7b003420
SG
452}
453
fea16b47
MP
454# Generates completion reply with compgen from newline-separated possible
455# completion filenames.
456# It accepts 1 to 3 arguments:
457# 1: List of possible completion filenames, separated by a single newline.
458# 2: A directory prefix to be added to each possible completion filename
459# (optional).
460# 3: Generate possible completion matches for this word (optional).
461__gitcomp_file ()
462{
463 local IFS=$'\n'
464
465 # XXX does not work when the directory prefix contains a tilde,
466 # since tilde expansion is not applied.
467 # This means that COMPREPLY will be empty and Bash default
468 # completion will be used.
0afe8e9e 469 __gitcompadd "$1" "${2-}" "${3-$cur}" ""
fea16b47 470
3ffa4df4 471 # use a hack to enable file mode in bash < 4
fbe45118 472 compopt -o filenames +o nospace 2>/dev/null ||
8b4c2e0b
SG
473 compgen -f /non-existing-dir/ >/dev/null ||
474 true
fea16b47
MP
475}
476
f825972c
FC
477# Execute 'git ls-files', unless the --committable option is specified, in
478# which case it runs 'git diff-index' to find out the files that can be
479# committed. It return paths relative to the directory specified in the first
480# argument, and using the options specified in the second argument.
fea16b47
MP
481__git_ls_files_helper ()
482{
fca416a4 483 if [ "$2" == "--committable" ]; then
3dfe23ba 484 __git -C "$1" -c core.quotePath=false diff-index \
a364e984 485 --name-only --relative HEAD -- "${3//\\/\\\\}*"
fca416a4
JH
486 else
487 # NOTE: $2 is not quoted in order to support multiple options
3dfe23ba 488 __git -C "$1" -c core.quotePath=false ls-files \
a364e984 489 --exclude-standard $2 -- "${3//\\/\\\\}*"
e15098a3 490 fi
35ba83cc 491}
fea16b47
MP
492
493
fea16b47
MP
494# __git_index_files accepts 1 or 2 arguments:
495# 1: Options to pass to ls-files (required).
fea16b47
MP
496# 2: A directory path (optional).
497# If provided, only files within the specified directory are listed.
498# Sub directories are never recursed. Path must have a trailing
499# slash.
a364e984 500# 3: List only paths matching this path component (optional).
fea16b47
MP
501__git_index_files ()
502{
105c0eff 503 local root="$2" match="$3"
fea16b47 504
a364e984 505 __git_ls_files_helper "$root" "$1" "$match" |
7b003420 506 awk -F / -v pfx="${2//\\/\\\\}" '{
c1bc0a0e
SG
507 paths[$1] = 1
508 }
509 END {
193757f8
SG
510 for (p in paths) {
511 if (substr(p, 1, 1) != "\"") {
512 # No special characters, easy!
7b003420 513 print pfx p
193757f8
SG
514 continue
515 }
516
517 # The path is quoted.
518 p = dequote(p)
519 if (p == "")
520 continue
521
522 # Even when a directory name itself does not contain
523 # any special characters, it will still be quoted if
524 # any of its (stripped) trailing path components do.
525 # Because of this we may have seen the same direcory
526 # both quoted and unquoted.
527 if (p in paths)
528 # We have seen the same directory unquoted,
529 # skip it.
530 continue
531 else
7b003420 532 print pfx p
193757f8
SG
533 }
534 }
535 function dequote(p, bs_idx, out, esc, esc_idx, dec) {
536 # Skip opening double quote.
537 p = substr(p, 2)
538
539 # Interpret backslash escape sequences.
540 while ((bs_idx = index(p, "\\")) != 0) {
541 out = out substr(p, 1, bs_idx - 1)
542 esc = substr(p, bs_idx + 1, 1)
543 p = substr(p, bs_idx + 2)
544
545 if ((esc_idx = index("abtvfr\"\\", esc)) != 0) {
546 # C-style one-character escape sequence.
547 out = out substr("\a\b\t\v\f\r\"\\",
548 esc_idx, 1)
549 } else if (esc == "n") {
550 # Uh-oh, a newline character.
551 # We cant reliably put a pathname
552 # containing a newline into COMPREPLY,
553 # and the newline would create a mess.
554 # Skip this path.
555 return ""
556 } else {
557 # Must be a \nnn octal value, then.
558 dec = esc * 64 + \
559 substr(p, 1, 1) * 8 + \
560 substr(p, 2, 1)
561 out = out sprintf("%c", dec)
562 p = substr(p, 3)
563 }
564 }
565 # Drop closing double quote, if there is one.
566 # (There isnt any if this is a directory, as it was
567 # already stripped with the trailing path components.)
568 if (substr(p, length(p), 1) == "\"")
569 out = out substr(p, 1, length(p) - 1)
570 else
571 out = out p
572
573 return out
c1bc0a0e 574 }'
fea16b47
MP
575}
576
722e31c7
SG
577# __git_complete_index_file requires 1 argument:
578# 1: the options to pass to ls-file
579#
580# The exception is --committable, which finds the files appropriate commit.
581__git_complete_index_file ()
582{
f12785a3 583 local dequoted_word pfx="" cur_
722e31c7 584
f12785a3
SG
585 __git_dequote "$cur"
586
587 case "$dequoted_word" in
722e31c7 588 ?*/*)
f12785a3
SG
589 pfx="${dequoted_word%/*}/"
590 cur_="${dequoted_word##*/}"
722e31c7 591 ;;
f12785a3
SG
592 *)
593 cur_="$dequoted_word"
722e31c7
SG
594 esac
595
7b003420 596 __gitcomp_file_direct "$(__git_index_files "$1" "$pfx" "$cur_")"
fea16b47
MP
597}
598
227307a6
SG
599# Lists branches from the local repository.
600# 1: A prefix to be added to each listed branch (optional).
601# 2: List only branches matching this word (optional; list all branches if
602# unset or empty).
603# 3: A suffix to be appended to each listed branch (optional).
5de40f59
SP
604__git_heads ()
605{
227307a6
SG
606 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
607
608 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
609 "refs/heads/$cur_*" "refs/heads/$cur_*/**"
5de40f59
SP
610}
611
227307a6
SG
612# Lists tags from the local repository.
613# Accepts the same positional parameters as __git_heads() above.
88e21dc7
SP
614__git_tags ()
615{
227307a6
SG
616 local pfx="${1-}" cur_="${2-}" sfx="${3-}"
617
618 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
619 "refs/tags/$cur_*" "refs/tags/$cur_*/**"
88e21dc7
SP
620}
621
be6fbdb5
SG
622# Lists refs from the local (by default) or from a remote repository.
623# It accepts 0, 1 or 2 arguments:
624# 1: The remote to list refs from (optional; ignored, if set but empty).
91b7ea81 625# Can be the name of a configured remote, a path, or a URL.
be6fbdb5
SG
626# 2: In addition to local refs, list unique branches from refs/remotes/ for
627# 'git checkout's tracking DWIMery (optional; ignored, if set but empty).
fef56eb0 628# 3: A prefix to be added to each listed ref (optional).
e896369b
SG
629# 4: List only refs matching this word (optional; list all refs if unset or
630# empty).
fef56eb0
SG
631# 5: A suffix to be appended to each listed ref (optional; ignored, if set
632# but empty).
15b4a163
SG
633#
634# Use __git_complete_refs() instead.
690d8824
JH
635__git_refs ()
636{
fad9484f 637 local i hash dir track="${2-}"
5c12f642 638 local list_refs_from=path remote="${1-}"
fef56eb0
SG
639 local format refs
640 local pfx="${3-}" cur_="${4-$cur}" sfx="${5-}"
e896369b 641 local match="${4-}"
fef56eb0 642 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
5c12f642 643
fad9484f
SG
644 __git_find_repo_path
645 dir="$__git_repo_path"
646
62a1b732
SG
647 if [ -z "$remote" ]; then
648 if [ -z "$dir" ]; then
649 return
650 fi
651 else
69a77596
SG
652 if __git_is_configured_remote "$remote"; then
653 # configured remote takes precedence over a
654 # local directory with the same name
655 list_refs_from=remote
656 elif [ -d "$remote/.git" ]; then
5c12f642
SG
657 dir="$remote/.git"
658 elif [ -d "$remote" ]; then
659 dir="$remote"
660 else
69a77596 661 list_refs_from=url
5c12f642
SG
662 fi
663 fi
664
62a1b732 665 if [ "$list_refs_from" = path ]; then
aed38813 666 if [[ "$cur_" == ^* ]]; then
fef56eb0
SG
667 pfx="$pfx^"
668 fer_pfx="$fer_pfx^"
aed38813 669 cur_=${cur_#^}
e896369b 670 match=${match#^}
aed38813 671 fi
2ea328a1 672 case "$cur_" in
608efb87
SG
673 refs|refs/*)
674 format="refname"
e896369b 675 refs=("$match*" "$match*/**")
34a6bbb5 676 track=""
608efb87
SG
677 ;;
678 *)
fbd7a232 679 for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD REBASE_HEAD; do
e896369b
SG
680 case "$i" in
681 $match*)
682 if [ -e "$dir/$i" ]; then
fef56eb0 683 echo "$pfx$i$sfx"
e896369b
SG
684 fi
685 ;;
686 esac
d23e7570 687 done
b2b68114 688 format="refname:strip=2"
e896369b
SG
689 refs=("refs/tags/$match*" "refs/tags/$match*/**"
690 "refs/heads/$match*" "refs/heads/$match*/**"
691 "refs/remotes/$match*" "refs/remotes/$match*/**")
608efb87
SG
692 ;;
693 esac
fef56eb0 694 __git_dir="$dir" __git for-each-ref --format="$fer_pfx%($format)$sfx" \
e896369b 695 "${refs[@]}"
34a6bbb5
KB
696 if [ -n "$track" ]; then
697 # employ the heuristic used by git checkout
698 # Try to find a remote branch that matches the completion word
699 # but only output if the branch name is unique
fef56eb0 700 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
400a7553 701 --sort="refname:strip=3" \
824388d5 702 "refs/remotes/*/$match*" "refs/remotes/*/$match*/**" | \
400a7553 703 uniq -u
34a6bbb5 704 fi
35e65ecc 705 return
690d8824 706 fi
2ea328a1 707 case "$cur_" in
fb772cca 708 refs|refs/*)
e896369b 709 __git ls-remote "$remote" "$match*" | \
6f2dd720 710 while read -r hash i; do
fb772cca
SG
711 case "$i" in
712 *^{}) ;;
fef56eb0 713 *) echo "$pfx$i$sfx" ;;
fb772cca
SG
714 esac
715 done
716 ;;
717 *)
91b7ea81 718 if [ "$list_refs_from" = remote ]; then
e896369b 719 case "HEAD" in
fef56eb0 720 $match*) echo "${pfx}HEAD$sfx" ;;
e896369b 721 esac
fef56eb0 722 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
e896369b 723 "refs/remotes/$remote/$match*" \
e8cb0234 724 "refs/remotes/$remote/$match*/**"
91b7ea81 725 else
e896369b
SG
726 local query_symref
727 case "HEAD" in
728 $match*) query_symref="HEAD" ;;
729 esac
730 __git ls-remote "$remote" $query_symref \
731 "refs/tags/$match*" "refs/heads/$match*" \
732 "refs/remotes/$match*" |
91b7ea81
SG
733 while read -r hash i; do
734 case "$i" in
735 *^{}) ;;
fef56eb0
SG
736 refs/*) echo "$pfx${i#refs/*/}$sfx" ;;
737 *) echo "$pfx$i$sfx" ;; # symbolic refs
91b7ea81
SG
738 esac
739 done
740 fi
fb772cca
SG
741 ;;
742 esac
690d8824
JH
743}
744
15b4a163
SG
745# Completes refs, short and long, local and remote, symbolic and pseudo.
746#
747# Usage: __git_complete_refs [<option>]...
748# --remote=<remote>: The remote to list refs from, can be the name of a
749# configured remote, a path, or a URL.
750# --track: List unique remote branches for 'git checkout's tracking DWIMery.
751# --pfx=<prefix>: A prefix to be added to each ref.
752# --cur=<word>: The current ref to be completed. Defaults to the current
753# word to be completed.
754# --sfx=<suffix>: A suffix to be appended to each ref instead of the default
755# space.
756__git_complete_refs ()
757{
758 local remote track pfx cur_="$cur" sfx=" "
759
760 while test $# != 0; do
761 case "$1" in
762 --remote=*) remote="${1##--remote=}" ;;
763 --track) track="yes" ;;
764 --pfx=*) pfx="${1##--pfx=}" ;;
765 --cur=*) cur_="${1##--cur=}" ;;
766 --sfx=*) sfx="${1##--sfx=}" ;;
767 *) return 1 ;;
768 esac
769 shift
770 done
771
fef56eb0 772 __gitcomp_direct "$(__git_refs "$remote" "$track" "$pfx" "$cur_" "$sfx")"
15b4a163
SG
773}
774
a42577d4 775# __git_refs2 requires 1 argument (to pass to __git_refs)
aa0644f7 776# Deprecated: use __git_complete_fetch_refspecs() instead.
690d8824
JH
777__git_refs2 ()
778{
67ffa114
SP
779 local i
780 for i in $(__git_refs "$1"); do
781 echo "$i:$i"
690d8824
JH
782 done
783}
784
aa0644f7
SG
785# Completes refspecs for fetching from a remote repository.
786# 1: The remote repository.
787# 2: A prefix to be added to each listed refspec (optional).
788# 3: The ref to be completed as a refspec instead of the current word to be
789# completed (optional)
790# 4: A suffix to be appended to each listed refspec instead of the default
791# space (optional).
792__git_complete_fetch_refspecs ()
793{
794 local i remote="$1" pfx="${2-}" cur_="${3-$cur}" sfx="${4- }"
795
745d655d 796 __gitcomp_direct "$(
aa0644f7 797 for i in $(__git_refs "$remote" "" "" "$cur_") ; do
745d655d 798 echo "$pfx$i:$i$sfx"
aa0644f7 799 done
745d655d 800 )"
aa0644f7
SG
801}
802
a42577d4 803# __git_refs_remotes requires 1 argument (to pass to ls-remote)
5de40f59
SP
804__git_refs_remotes ()
805{
48058f5d 806 local i hash
e15098a3 807 __git ls-remote "$1" 'refs/heads/*' | \
6f2dd720 808 while read -r hash i; do
48058f5d 809 echo "$i:refs/remotes/$1/${i#refs/heads/}"
5de40f59
SP
810 done
811}
812
690d8824
JH
813__git_remotes ()
814{
fad9484f
SG
815 __git_find_repo_path
816 test -d "$__git_repo_path/remotes" && ls -1 "$__git_repo_path/remotes"
1cd23e9e 817 __git remote
690d8824
JH
818}
819
69a77596
SG
820# Returns true if $1 matches the name of a configured remote, false otherwise.
821__git_is_configured_remote ()
822{
823 local remote
824 for remote in $(__git_remotes); do
825 if [ "$remote" = "$1" ]; then
826 return 0
827 fi
828 done
829 return 1
690d8824
JH
830}
831
eaa4e6ee 832__git_list_merge_strategies ()
4ad91321 833{
7cc763aa 834 LANG=C LC_ALL=C git merge -s help 2>&1 |
25b3d4d6
JH
835 sed -n -e '/[Aa]vailable strategies are: /,/^$/{
836 s/\.$//
837 s/.*://
838 s/^[ ]*//
839 s/[ ]*$//
4ad91321 840 p
25b3d4d6 841 }'
4ad91321 842}
eaa4e6ee
JN
843
844__git_merge_strategies=
845# 'git merge -s help' (and thus detection of the merge strategy
846# list) fails, unfortunately, if run outside of any git working
847# tree. __git_merge_strategies is set to the empty string in
848# that case, and the detection will be repeated the next time it
849# is needed.
850__git_compute_merge_strategies ()
851{
cf0ff02a
FC
852 test -n "$__git_merge_strategies" ||
853 __git_merge_strategies=$(__git_list_merge_strategies)
eaa4e6ee 854}
4ad91321 855
1d66ec58 856__git_complete_revlist_file ()
690d8824 857{
da4902a7 858 local pfx ls ref cur_="$cur"
9244d69b 859 case "$cur_" in
1d66ec58
SG
860 *..?*:*)
861 return
862 ;;
690d8824 863 ?*:*)
9244d69b
SG
864 ref="${cur_%%:*}"
865 cur_="${cur_#*:}"
866 case "$cur_" in
690d8824 867 ?*/*)
9244d69b
SG
868 pfx="${cur_%/*}"
869 cur_="${cur_##*/}"
690d8824
JH
870 ls="$ref:$pfx"
871 pfx="$pfx/"
872 ;;
873 *)
874 ls="$ref"
875 ;;
80152b09 876 esac
db8a9ff0
SP
877
878 case "$COMP_WORDBREAKS" in
879 *:*) : great ;;
880 *) pfx="$ref:$pfx" ;;
881 esac
882
e15098a3 883 __gitcomp_nl "$(__git ls-tree "$ls" \
778306e4
SP
884 | sed '/^100... blob /{
885 s,^.* ,,
886 s,$, ,
887 }
888 /^120000 blob /{
889 s,^.* ,,
890 s,$, ,
891 }
690d8824
JH
892 /^040000 tree /{
893 s,^.* ,,
894 s,$,/,
895 }
896 s/^.* //')" \
41e6229d 897 "$pfx" "$cur_" ""
690d8824 898 ;;
f53352fb 899 *...*)
9244d69b
SG
900 pfx="${cur_%...*}..."
901 cur_="${cur_#*...}"
15b4a163 902 __git_complete_refs --pfx="$pfx" --cur="$cur_"
f53352fb
SP
903 ;;
904 *..*)
9244d69b
SG
905 pfx="${cur_%..*}.."
906 cur_="${cur_#*..}"
15b4a163 907 __git_complete_refs --pfx="$pfx" --cur="$cur_"
b3391775 908 ;;
f53352fb 909 *)
15b4a163 910 __git_complete_refs
f53352fb
SP
911 ;;
912 esac
913}
914
1d66ec58
SG
915__git_complete_file ()
916{
917 __git_complete_revlist_file
918}
919
920__git_complete_revlist ()
921{
922 __git_complete_revlist_file
923}
924
52d5c3b5
JS
925__git_complete_remote_or_refspec ()
926{
9244d69b 927 local cur_="$cur" cmd="${words[1]}"
0a4e1472 928 local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
f1c6ffe6 929 if [ "$cmd" = "remote" ]; then
6e8c755f 930 ((c++))
f1c6ffe6 931 fi
da48616f
PD
932 while [ $c -lt $cword ]; do
933 i="${words[c]}"
52d5c3b5 934 case "$i" in
e25e2b42 935 --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
723c1d52 936 -d|--delete) [ "$cmd" = "push" ] && lhs=0 ;;
e25e2b42
BG
937 --all)
938 case "$cmd" in
939 push) no_complete_refspec=1 ;;
940 fetch)
e25e2b42
BG
941 return
942 ;;
943 *) ;;
944 esac
945 ;;
52d5c3b5
JS
946 -*) ;;
947 *) remote="$i"; break ;;
948 esac
6e8c755f 949 ((c++))
52d5c3b5
JS
950 done
951 if [ -z "$remote" ]; then
a31e6262 952 __gitcomp_nl "$(__git_remotes)"
52d5c3b5
JS
953 return
954 fi
0a4e1472 955 if [ $no_complete_refspec = 1 ]; then
0a4e1472
JS
956 return
957 fi
52d5c3b5 958 [ "$remote" = "." ] && remote=
9244d69b 959 case "$cur_" in
52d5c3b5
JS
960 *:*)
961 case "$COMP_WORDBREAKS" in
962 *:*) : great ;;
9244d69b 963 *) pfx="${cur_%%:*}:" ;;
52d5c3b5 964 esac
9244d69b 965 cur_="${cur_#*:}"
52d5c3b5
JS
966 lhs=0
967 ;;
968 +*)
969 pfx="+"
9244d69b 970 cur_="${cur_#+}"
52d5c3b5
JS
971 ;;
972 esac
973 case "$cmd" in
974 fetch)
975 if [ $lhs = 1 ]; then
aa0644f7 976 __git_complete_fetch_refspecs "$remote" "$pfx" "$cur_"
52d5c3b5 977 else
15b4a163 978 __git_complete_refs --pfx="$pfx" --cur="$cur_"
52d5c3b5
JS
979 fi
980 ;;
f1c6ffe6 981 pull|remote)
52d5c3b5 982 if [ $lhs = 1 ]; then
15b4a163 983 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
52d5c3b5 984 else
15b4a163 985 __git_complete_refs --pfx="$pfx" --cur="$cur_"
52d5c3b5
JS
986 fi
987 ;;
988 push)
989 if [ $lhs = 1 ]; then
15b4a163 990 __git_complete_refs --pfx="$pfx" --cur="$cur_"
52d5c3b5 991 else
15b4a163 992 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
52d5c3b5
JS
993 fi
994 ;;
995 esac
996}
997
3c7b480a
JS
998__git_complete_strategy ()
999{
eaa4e6ee 1000 __git_compute_merge_strategies
da48616f 1001 case "$prev" in
3c7b480a 1002 -s|--strategy)
eaa4e6ee 1003 __gitcomp "$__git_merge_strategies"
3c7b480a
JS
1004 return 0
1005 esac
3c7b480a
JS
1006 case "$cur" in
1007 --strategy=*)
eaa4e6ee 1008 __gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
3c7b480a
JS
1009 return 0
1010 ;;
1011 esac
1012 return 1
1013}
1014
eaa4e6ee
JN
1015__git_all_commands=
1016__git_compute_all_commands ()
1017{
cf0ff02a 1018 test -n "$__git_all_commands" ||
3301d36b 1019 __git_all_commands=$(git --list-cmds=main,others,alias,nohelpers)
eaa4e6ee 1020}
f2bb9f88 1021
e8f9e428
SG
1022# Lists all set config variables starting with the given section prefix,
1023# with the prefix removed.
1024__git_get_config_variables ()
c3898111 1025{
e8f9e428 1026 local section="$1" i IFS=$'\n'
e15098a3 1027 for i in $(__git config --name-only --get-regexp "^$section\..*"); do
905f2036 1028 echo "${i#$section.}"
c3898111
SG
1029 done
1030}
1031
c3898111
SG
1032__git_pretty_aliases ()
1033{
e8f9e428 1034 __git_get_config_variables "pretty"
c3898111
SG
1035}
1036
a42577d4 1037# __git_aliased_command requires 1 argument
367dce2a
DS
1038__git_aliased_command ()
1039{
e15098a3 1040 local word cmdline=$(__git config --get "alias.$1")
367dce2a 1041 for word in $cmdline; do
c63437cb 1042 case "$word" in
66729509
SG
1043 \!gitk|gitk)
1044 echo "gitk"
367dce2a 1045 return
66729509 1046 ;;
c63437cb
SG
1047 \!*) : shell command alias ;;
1048 -*) : option ;;
1049 *=*) : setting env ;;
1050 git) : git itself ;;
56f24e80
SP
1051 \(\)) : skip parens of shell function definition ;;
1052 {) : skip start of shell helper function ;;
1053 :) : skip null command ;;
1054 \'*) : skip opening quote after sh -c ;;
c63437cb
SG
1055 *)
1056 echo "$word"
367dce2a 1057 return
c63437cb 1058 esac
367dce2a
DS
1059 done
1060}
1061
918c03c2
SG
1062# __git_find_on_cmdline requires 1 argument
1063__git_find_on_cmdline ()
3ff1320d 1064{
da4902a7 1065 local word subcommand c=1
da48616f
PD
1066 while [ $c -lt $cword ]; do
1067 word="${words[c]}"
3ff1320d
SG
1068 for subcommand in $1; do
1069 if [ "$subcommand" = "$word" ]; then
1070 echo "$subcommand"
1071 return
1072 fi
1073 done
6e8c755f 1074 ((c++))
3ff1320d
SG
1075 done
1076}
1077
7c599e92
TB
1078# Echo the value of an option set on the command line or config
1079#
1080# $1: short option name
1081# $2: long option name including =
1082# $3: list of possible values
1083# $4: config string (optional)
1084#
1085# example:
1086# result="$(__git_get_option_value "-d" "--do-something=" \
1087# "yes no" "core.doSomething")"
1088#
1089# result is then either empty (no option set) or "yes" or "no"
1090#
1091# __git_get_option_value requires 3 arguments
1092__git_get_option_value ()
1093{
1094 local c short_opt long_opt val
1095 local result= values config_key word
1096
1097 short_opt="$1"
1098 long_opt="$2"
1099 values="$3"
1100 config_key="$4"
1101
1102 ((c = $cword - 1))
1103 while [ $c -ge 0 ]; do
1104 word="${words[c]}"
1105 for val in $values; do
1106 if [ "$short_opt$val" = "$word" ] ||
1107 [ "$long_opt$val" = "$word" ]; then
1108 result="$val"
1109 break 2
1110 fi
1111 done
1112 ((c--))
1113 done
1114
1115 if [ -n "$config_key" ] && [ -z "$result" ]; then
1cd23e9e 1116 result="$(__git config "$config_key")"
7c599e92
TB
1117 fi
1118
1119 echo "$result"
1120}
1121
d773c631
SG
1122__git_has_doubledash ()
1123{
da4902a7 1124 local c=1
da48616f
PD
1125 while [ $c -lt $cword ]; do
1126 if [ "--" = "${words[c]}" ]; then
d773c631
SG
1127 return 0
1128 fi
6e8c755f 1129 ((c++))
d773c631
SG
1130 done
1131 return 1
1132}
1133
fea16b47
MP
1134# Try to count non option arguments passed on the command line for the
1135# specified git command.
1136# When options are used, it is necessary to use the special -- option to
1137# tell the implementation were non option arguments begin.
1138# XXX this can not be improved, since options can appear everywhere, as
1139# an example:
1140# git mv x -n y
1141#
1142# __git_count_arguments requires 1 argument: the git command executed.
1143__git_count_arguments ()
1144{
1145 local word i c=0
1146
1147 # Skip "git" (first argument)
1148 for ((i=1; i < ${#words[@]}; i++)); do
1149 word="${words[i]}"
1150
1151 case "$word" in
1152 --)
1153 # Good; we can assume that the following are only non
1154 # option arguments.
1155 ((c = 0))
1156 ;;
1157 "$1")
1158 # Skip the specified git command and discard git
1159 # main options
1160 ((c = 0))
1161 ;;
1162 ?*)
1163 ((c++))
1164 ;;
1165 esac
1166 done
1167
1168 printf "%d" $c
1169}
1170
7950659d 1171__git_whitespacelist="nowarn warn error error-all fix"
7fb6aefd 1172__git_am_inprogress_options="--skip --continue --resolved --abort --quit --show-current-patch"
88329195
SP
1173
1174_git_am ()
1175{
fad9484f
SG
1176 __git_find_repo_path
1177 if [ -d "$__git_repo_path"/rebase-apply ]; then
be3ce6b2 1178 __gitcomp "$__git_am_inprogress_options"
88329195
SP
1179 return
1180 fi
1181 case "$cur" in
1182 --whitespace=*)
b3391775 1183 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
88329195
SP
1184 return
1185 ;;
1186 --*)
2b1c01d2 1187 __gitcomp_builtin am "" \
be3ce6b2 1188 "$__git_am_inprogress_options"
88329195
SP
1189 return
1190 esac
88329195
SP
1191}
1192
1193_git_apply ()
1194{
88329195
SP
1195 case "$cur" in
1196 --whitespace=*)
b3391775 1197 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
88329195
SP
1198 return
1199 ;;
1200 --*)
b8e9d662 1201 __gitcomp_builtin apply
88329195
SP
1202 return
1203 esac
88329195
SP
1204}
1205
8435b548
SP
1206_git_add ()
1207{
8435b548
SP
1208 case "$cur" in
1209 --*)
e1bea2c0 1210 __gitcomp_builtin add
8435b548
SP
1211 return
1212 esac
fea16b47 1213
bd9ab9df
CW
1214 local complete_opt="--others --modified --directory --no-empty-directory"
1215 if test -n "$(__git_find_on_cmdline "-u --update")"
1216 then
1217 complete_opt="--modified"
1218 fi
1219 __git_complete_index_file "$complete_opt"
8435b548
SP
1220}
1221
b3191ce2
LM
1222_git_archive ()
1223{
b3191ce2
LM
1224 case "$cur" in
1225 --format=*)
1226 __gitcomp "$(git archive --list)" "" "${cur##--format=}"
1227 return
1228 ;;
1229 --remote=*)
a31e6262 1230 __gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
b3191ce2
LM
1231 return
1232 ;;
1233 --*)
1234 __gitcomp "
1235 --format= --list --verbose
f483a0aa 1236 --prefix= --remote= --exec= --output
b3191ce2
LM
1237 "
1238 return
1239 ;;
1240 esac
1241 __git_complete_file
1242}
1243
b2e69f62
SP
1244_git_bisect ()
1245{
d773c631
SG
1246 __git_has_doubledash && return
1247
bf11d461 1248 local subcommands="start bad good skip reset visualize replay log run"
918c03c2 1249 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3ff1320d 1250 if [ -z "$subcommand" ]; then
fad9484f
SG
1251 __git_find_repo_path
1252 if [ -f "$__git_repo_path"/BISECT_START ]; then
128191f5
SG
1253 __gitcomp "$subcommands"
1254 else
1255 __gitcomp "replay start"
1256 fi
b2e69f62
SP
1257 return
1258 fi
1259
3ff1320d 1260 case "$subcommand" in
8205ff8e 1261 bad|good|reset|skip|start)
15b4a163 1262 __git_complete_refs
b2e69f62
SP
1263 ;;
1264 *)
b2e69f62
SP
1265 ;;
1266 esac
1267}
1268
690d8824
JH
1269_git_branch ()
1270{
da4902a7 1271 local i c=1 only_local_ref="n" has_r="n"
b9217642 1272
da48616f
PD
1273 while [ $c -lt $cword ]; do
1274 i="${words[c]}"
b9217642 1275 case "$i" in
2703c22f
VS
1276 -d|--delete|-m|--move) only_local_ref="y" ;;
1277 -r|--remotes) has_r="y" ;;
b9217642 1278 esac
6e8c755f 1279 ((c++))
b9217642
SG
1280 done
1281
da48616f 1282 case "$cur" in
ca45d0fa 1283 --set-upstream-to=*)
15b4a163 1284 __git_complete_refs --cur="${cur##--set-upstream-to=}"
ca45d0fa 1285 ;;
3b376b0c 1286 --*)
2b1c01d2 1287 __gitcomp_builtin branch
3b376b0c 1288 ;;
b9217642
SG
1289 *)
1290 if [ $only_local_ref = "y" -a $has_r = "n" ]; then
227307a6 1291 __gitcomp_direct "$(__git_heads "" "$cur" " ")"
b9217642 1292 else
15b4a163 1293 __git_complete_refs
b9217642
SG
1294 fi
1295 ;;
3b376b0c 1296 esac
690d8824
JH
1297}
1298
374a58c9
ML
1299_git_bundle ()
1300{
da48616f
PD
1301 local cmd="${words[2]}"
1302 case "$cword" in
8d8163f3 1303 2)
374a58c9
ML
1304 __gitcomp "create list-heads verify unbundle"
1305 ;;
8d8163f3 1306 3)
374a58c9
ML
1307 # looking for a file
1308 ;;
1309 *)
1310 case "$cmd" in
1311 create)
1312 __git_complete_revlist
1313 ;;
1314 esac
1315 ;;
1316 esac
1317}
1318
690d8824
JH
1319_git_checkout ()
1320{
c84bb14c
SG
1321 __git_has_doubledash && return
1322
e648f8b6
SG
1323 case "$cur" in
1324 --conflict=*)
1325 __gitcomp "diff3 merge" "" "${cur##--conflict=}"
1326 ;;
1327 --*)
2b1c01d2 1328 __gitcomp_builtin checkout
e648f8b6
SG
1329 ;;
1330 *)
34a6bbb5
KB
1331 # check if --track, --no-track, or --no-guess was specified
1332 # if so, disable DWIM mode
15b4a163 1333 local flags="--track --no-track --no-guess" track_opt="--track"
60e71bbc
JK
1334 if [ "$GIT_COMPLETION_CHECKOUT_NO_GUESS" = "1" ] ||
1335 [ -n "$(__git_find_on_cmdline "$flags")" ]; then
15b4a163 1336 track_opt=''
34a6bbb5 1337 fi
15b4a163 1338 __git_complete_refs $track_opt
e648f8b6
SG
1339 ;;
1340 esac
690d8824
JH
1341}
1342
d8a9fea5
SP
1343_git_cherry ()
1344{
c55c4a5b
NTND
1345 case "$cur" in
1346 --*)
1347 __gitcomp_builtin cherry
1348 return
1349 esac
1350
15b4a163 1351 __git_complete_refs
d8a9fea5
SP
1352}
1353
660003e2
NTND
1354__git_cherry_pick_inprogress_options="--continue --quit --abort"
1355
1273231e
SP
1356_git_cherry_pick ()
1357{
fad9484f
SG
1358 __git_find_repo_path
1359 if [ -f "$__git_repo_path"/CHERRY_PICK_HEAD ]; then
660003e2 1360 __gitcomp "$__git_cherry_pick_inprogress_options"
7655fa7f
FC
1361 return
1362 fi
1273231e
SP
1363 case "$cur" in
1364 --*)
660003e2
NTND
1365 __gitcomp_builtin cherry-pick "" \
1366 "$__git_cherry_pick_inprogress_options"
1273231e
SP
1367 ;;
1368 *)
15b4a163 1369 __git_complete_refs
1273231e
SP
1370 ;;
1371 esac
1372}
1373
4181c7e8
LM
1374_git_clean ()
1375{
4181c7e8
LM
1376 case "$cur" in
1377 --*)
26e90958 1378 __gitcomp_builtin clean
4181c7e8
LM
1379 return
1380 ;;
1381 esac
fea16b47
MP
1382
1383 # XXX should we check for -x option ?
ea95c7b8 1384 __git_complete_index_file "--others --directory"
4181c7e8
LM
1385}
1386
3eb11012
LM
1387_git_clone ()
1388{
3eb11012
LM
1389 case "$cur" in
1390 --*)
2b1c01d2 1391 __gitcomp_builtin clone
3eb11012
LM
1392 return
1393 ;;
1394 esac
3eb11012
LM
1395}
1396
21d2a9e3
TB
1397__git_untracked_file_modes="all no normal"
1398
4548e855
SP
1399_git_commit ()
1400{
fea16b47
MP
1401 case "$prev" in
1402 -c|-C)
15b4a163 1403 __git_complete_refs
fea16b47
MP
1404 return
1405 ;;
1406 esac
d773c631 1407
4548e855 1408 case "$cur" in
9a424b27 1409 --cleanup=*)
72dbb365 1410 __gitcomp "default scissors strip verbatim whitespace
9a424b27
SG
1411 " "" "${cur##--cleanup=}"
1412 return
1413 ;;
77653abd
TM
1414 --reuse-message=*|--reedit-message=*|\
1415 --fixup=*|--squash=*)
15b4a163 1416 __git_complete_refs --cur="${cur#*=}"
9a424b27
SG
1417 return
1418 ;;
1419 --untracked-files=*)
21d2a9e3 1420 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
9a424b27
SG
1421 return
1422 ;;
4548e855 1423 --*)
2b1c01d2 1424 __gitcomp_builtin commit
4548e855
SP
1425 return
1426 esac
fea16b47 1427
1cd23e9e 1428 if __git rev-parse --verify --quiet HEAD >/dev/null; then
f825972c 1429 __git_complete_index_file "--committable"
fea16b47
MP
1430 else
1431 # This is the first commit
1432 __git_complete_index_file "--cached"
1433 fi
4548e855
SP
1434}
1435
217926c0
SP
1436_git_describe ()
1437{
cbb504c9
TR
1438 case "$cur" in
1439 --*)
ddced834 1440 __gitcomp_builtin describe
cbb504c9
TR
1441 return
1442 esac
15b4a163 1443 __git_complete_refs
217926c0
SP
1444}
1445
07924d4d
MP
1446__git_diff_algorithms="myers minimal patience histogram"
1447
462f2134 1448__git_diff_submodule_formats="diff log short"
ac76fd54 1449
20bf7292 1450__git_diff_common_options="--stat --numstat --shortstat --summary
b3a4f858
JS
1451 --patch-with-stat --name-only --name-status --color
1452 --no-color --color-words --no-renames --check
f135aacb 1453 --full-index --binary --abbrev --diff-filter=
e9282f02 1454 --find-copies-harder --ignore-cr-at-eol
b3a4f858 1455 --text --ignore-space-at-eol --ignore-space-change
c2545167
TB
1456 --ignore-all-space --ignore-blank-lines --exit-code
1457 --quiet --ext-diff --no-ext-diff
aba201c6 1458 --no-prefix --src-prefix= --dst-prefix=
6d0e674a 1459 --inter-hunk-context=
216120ab 1460 --patience --histogram --minimal
e6414b46 1461 --raw --word-diff --word-diff-regex=
8fd2cfa7
SB
1462 --dirstat --dirstat= --dirstat-by-file
1463 --dirstat-by-file= --cumulative
07924d4d 1464 --diff-algorithm=
6cc4bc15 1465 --submodule --submodule= --ignore-submodules
20bf7292
TR
1466"
1467
1468_git_diff ()
1469{
1470 __git_has_doubledash && return
1471
20bf7292 1472 case "$cur" in
07924d4d
MP
1473 --diff-algorithm=*)
1474 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1475 return
1476 ;;
ac76fd54
JK
1477 --submodule=*)
1478 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1479 return
1480 ;;
20bf7292 1481 --*)
ebd15bf0 1482 __gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
861514d3 1483 --base --ours --theirs --no-index
20bf7292 1484 $__git_diff_common_options
aba201c6 1485 "
b3a4f858
JS
1486 return
1487 ;;
1488 esac
1d66ec58 1489 __git_complete_revlist_file
690d8824
JH
1490}
1491
c5f424fd 1492__git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff
f13f9b0e 1493 tkdiff vimdiff gvimdiff xxdiff araxis p4merge bc codecompare
e2dc2de9
DA
1494"
1495
1496_git_difftool ()
1497{
f7ad96cf
MH
1498 __git_has_doubledash && return
1499
e2dc2de9
DA
1500 case "$cur" in
1501 --tool=*)
1502 __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1503 return
1504 ;;
1505 --*)
6cc4bc15
NTND
1506 __gitcomp_builtin difftool "$__git_diff_common_options
1507 --base --cached --ours --theirs
1508 --pickaxe-all --pickaxe-regex
1509 --relative --staged
1510 "
e2dc2de9
DA
1511 return
1512 ;;
1513 esac
d8517cc6 1514 __git_complete_revlist_file
e2dc2de9
DA
1515}
1516
4dd5c470
SYS
1517__git_fetch_recurse_submodules="yes on-demand no"
1518
690d8824
JH
1519_git_fetch ()
1520{
0a4e1472 1521 case "$cur" in
4dd5c470
SYS
1522 --recurse-submodules=*)
1523 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1524 return
1525 ;;
0a4e1472 1526 --*)
2b1c01d2 1527 __gitcomp_builtin fetch
0a4e1472
JS
1528 return
1529 ;;
1530 esac
52d5c3b5 1531 __git_complete_remote_or_refspec
690d8824
JH
1532}
1533
2f65494d 1534__git_format_patch_options="
ea573521 1535 --stdout --attach --no-attach --thread --thread= --no-thread
2f65494d
FC
1536 --numbered --start-number --numbered-files --keep-subject --signoff
1537 --signature --no-signature --in-reply-to= --cc= --full-index --binary
1538 --not --all --cover-letter --no-prefix --src-prefix= --dst-prefix=
1539 --inline --suffix= --ignore-if-in-upstream --subject-prefix=
ea573521 1540 --output-directory --reroll-count --to= --quiet --notes
2f65494d
FC
1541"
1542
f53352fb
SP
1543_git_format_patch ()
1544{
f53352fb 1545 case "$cur" in
e1d37937
SB
1546 --thread=*)
1547 __gitcomp "
1548 deep shallow
1549 " "" "${cur##--thread=}"
1550 return
1551 ;;
f53352fb 1552 --*)
2f65494d 1553 __gitcomp "$__git_format_patch_options"
f53352fb
SP
1554 return
1555 ;;
1556 esac
1557 __git_complete_revlist
1558}
1559
4bca8636
AJ
1560_git_fsck ()
1561{
4bca8636
AJ
1562 case "$cur" in
1563 --*)
2b1c01d2 1564 __gitcomp_builtin fsck
4bca8636
AJ
1565 return
1566 ;;
1567 esac
4bca8636
AJ
1568}
1569
66729509
SG
1570_git_gitk ()
1571{
1572 _gitk
1573}
1574
7826a786
SG
1575# Lists matching symbol names from a tag (as in ctags) file.
1576# 1: List symbol names matching this word.
1577# 2: The tag file to list symbol names from.
1578# 3: A prefix to be added to each listed symbol name (optional).
1579# 4: A suffix to be appended to each listed symbol name (optional).
1580__git_match_ctag () {
1581 awk -v pfx="${3-}" -v sfx="${4-}" "
1582 /^${1//\//\\/}/ { print pfx \$1 sfx }
1583 " "$2"
29eec71f
JK
1584}
1585
2f779f91
SG
1586# Complete symbol names from a tag file.
1587# Usage: __git_complete_symbol [<option>]...
1588# --tags=<file>: The tag file to list symbol names from instead of the
1589# default "tags".
1590# --pfx=<prefix>: A prefix to be added to each symbol name.
1591# --cur=<word>: The current symbol name to be completed. Defaults to
1592# the current word to be completed.
1593# --sfx=<suffix>: A suffix to be appended to each symbol name instead
1594# of the default space.
1595__git_complete_symbol () {
1596 local tags=tags pfx="" cur_="${cur-}" sfx=" "
1597
1598 while test $# != 0; do
1599 case "$1" in
1600 --tags=*) tags="${1##--tags=}" ;;
1601 --pfx=*) pfx="${1##--pfx=}" ;;
1602 --cur=*) cur_="${1##--cur=}" ;;
1603 --sfx=*) sfx="${1##--sfx=}" ;;
1604 *) return 1 ;;
1605 esac
1606 shift
1607 done
1608
1609 if test -r "$tags"; then
1610 __gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
1611 fi
29eec71f
JK
1612}
1613
c72e0db1
LM
1614_git_grep ()
1615{
1616 __git_has_doubledash && return
1617
c72e0db1
LM
1618 case "$cur" in
1619 --*)
caf2de33 1620 __gitcomp_builtin grep
c72e0db1
LM
1621 return
1622 ;;
1623 esac
17225c49 1624
29eec71f
JK
1625 case "$cword,$prev" in
1626 2,*|*,-*)
2f779f91 1627 __git_complete_symbol && return
29eec71f
JK
1628 ;;
1629 esac
1630
15b4a163 1631 __git_complete_refs
c72e0db1
LM
1632}
1633
1eb7e2f8
LM
1634_git_help ()
1635{
1eb7e2f8
LM
1636 case "$cur" in
1637 --*)
8c13a8d7 1638 __gitcomp_builtin help
1eb7e2f8
LM
1639 return
1640 ;;
1641 esac
3301d36b
NTND
1642 if test -n "$GIT_TESTING_ALL_COMMAND_LIST"
1643 then
1644 __gitcomp "$GIT_TESTING_ALL_COMMAND_LIST $(git --list-cmds=alias,list-guide) gitk"
1645 else
1646 __gitcomp "$(git --list-cmds=main,nohelpers,alias,list-guide) gitk"
1647 fi
1eb7e2f8
LM
1648}
1649
5dad868b
LM
1650_git_init ()
1651{
5dad868b
LM
1652 case "$cur" in
1653 --shared=*)
1654 __gitcomp "
1655 false true umask group all world everybody
1656 " "" "${cur##--shared=}"
1657 return
1658 ;;
1659 --*)
b00116b7 1660 __gitcomp_builtin init
5dad868b
LM
1661 return
1662 ;;
1663 esac
5dad868b
LM
1664}
1665
b1bc1494
LM
1666_git_ls_files ()
1667{
b1bc1494
LM
1668 case "$cur" in
1669 --*)
2b1c01d2 1670 __gitcomp_builtin ls-files
b1bc1494
LM
1671 return
1672 ;;
1673 esac
fea16b47
MP
1674
1675 # XXX ignore options like --modified and always suggest all cached
1676 # files.
1677 __git_complete_index_file "--cached"
b1bc1494
LM
1678}
1679
690d8824
JH
1680_git_ls_remote ()
1681{
2c0f3a53
CW
1682 case "$cur" in
1683 --*)
cdc71c1c 1684 __gitcomp_builtin ls-remote
2c0f3a53
CW
1685 return
1686 ;;
1687 esac
a31e6262 1688 __gitcomp_nl "$(__git_remotes)"
690d8824
JH
1689}
1690
1691_git_ls_tree ()
1692{
be6d1b24
NTND
1693 case "$cur" in
1694 --*)
1695 __gitcomp_builtin ls-tree
1696 return
1697 ;;
1698 esac
1699
690d8824
JH
1700 __git_complete_file
1701}
1702
a393777e
TR
1703# Options that go well for log, shortlog and gitk
1704__git_log_common_options="
1705 --not --all
1706 --branches --tags --remotes
4fe1a619 1707 --first-parent --merges --no-merges
a393777e
TR
1708 --max-count=
1709 --max-age= --since= --after=
1710 --min-age= --until= --before=
6a6ebded
MG
1711 --min-parents= --max-parents=
1712 --no-min-parents --no-max-parents
a393777e
TR
1713"
1714# Options that go well for log and gitk (not shortlog)
1715__git_log_gitk_options="
1716 --dense --sparse --full-history
1717 --simplify-merges --simplify-by-decoration
3925b575 1718 --left-right --notes --no-notes
a393777e
TR
1719"
1720# Options that go well for log and shortlog (not gitk)
1721__git_log_shortlog_options="
1722 --author= --committer= --grep=
22dfa8a2 1723 --all-match --invert-grep
a393777e
TR
1724"
1725
3d279863 1726__git_log_pretty_formats="oneline short medium full fuller email raw format:"
672c68cb 1727__git_log_date_formats="relative iso8601 rfc2822 short local default raw"
3d279863 1728
690d8824
JH
1729_git_log ()
1730{
d773c631 1731 __git_has_doubledash && return
fad9484f 1732 __git_find_repo_path
d773c631 1733
bf3c20f6 1734 local merge=""
fad9484f 1735 if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
bf3c20f6
TR
1736 merge="--merge"
1737 fi
41d2716c
SG
1738 case "$prev,$cur" in
1739 -L,:*:*)
1740 return # fall back to Bash filename completion
1741 ;;
1742 -L,:*)
1743 __git_complete_symbol --cur="${cur#:}" --sfx=":"
1744 return
1745 ;;
1746 -G,*|-S,*)
1747 __git_complete_symbol
1748 return
1749 ;;
1750 esac
6e31b866 1751 case "$cur" in
e67d71e5 1752 --pretty=*|--format=*)
c3898111 1753 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
e67d71e5 1754 " "" "${cur#*=}"
72de29c2
TL
1755 return
1756 ;;
47e98eec 1757 --date=*)
672c68cb 1758 __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
47e98eec
SP
1759 return
1760 ;;
af4e9e8c 1761 --decorate=*)
af16bdaa 1762 __gitcomp "full short no" "" "${cur##--decorate=}"
af4e9e8c
SB
1763 return
1764 ;;
ac76fd54
JK
1765 --diff-algorithm=*)
1766 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1767 return
1768 ;;
1769 --submodule=*)
1770 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1771 return
1772 ;;
6e31b866 1773 --*)
b3391775 1774 __gitcomp "
a393777e
TR
1775 $__git_log_common_options
1776 $__git_log_shortlog_options
1777 $__git_log_gitk_options
8f87fae6 1778 --root --topo-order --date-order --reverse
5d0e6343 1779 --follow --full-diff
6e31b866 1780 --abbrev-commit --abbrev=
47e98eec 1781 --relative-date --date=
72de29c2 1782 --pretty= --format= --oneline
2ca0b197 1783 --show-signature
d3bfbf91 1784 --cherry-mark
a393777e 1785 --cherry-pick
20827d99 1786 --graph
af4e9e8c 1787 --decorate --decorate=
20bf7292 1788 --walk-reflogs
a393777e 1789 --parents --children
bf3c20f6 1790 $merge
20bf7292 1791 $__git_diff_common_options
47d5a8fa 1792 --pickaxe-all --pickaxe-regex
b3391775 1793 "
6e31b866
SP
1794 return
1795 ;;
41d2716c
SG
1796 -L:*:*)
1797 return # fall back to Bash filename completion
1798 ;;
1799 -L:*)
1800 __git_complete_symbol --cur="${cur#-L:}" --sfx=":"
1801 return
1802 ;;
1803 -G*)
1804 __git_complete_symbol --pfx="-G" --cur="${cur#-G}"
1805 return
1806 ;;
1807 -S*)
1808 __git_complete_symbol --pfx="-S" --cur="${cur#-S}"
1809 return
1810 ;;
6e31b866 1811 esac
f53352fb 1812 __git_complete_revlist
690d8824
JH
1813}
1814
4ad91321
SP
1815_git_merge ()
1816{
3c7b480a
JS
1817 __git_complete_strategy && return
1818
4ad91321
SP
1819 case "$cur" in
1820 --*)
2b1c01d2 1821 __gitcomp_builtin merge
4ad91321
SP
1822 return
1823 esac
15b4a163 1824 __git_complete_refs
4ad91321
SP
1825}
1826
b4c72162
LM
1827_git_mergetool ()
1828{
b4c72162
LM
1829 case "$cur" in
1830 --tool=*)
e2dc2de9 1831 __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
b4c72162
LM
1832 return
1833 ;;
1834 --*)
f483a0aa 1835 __gitcomp "--tool= --prompt --no-prompt"
b4c72162
LM
1836 return
1837 ;;
1838 esac
b4c72162
LM
1839}
1840
690d8824
JH
1841_git_merge_base ()
1842{
85453fd1
JK
1843 case "$cur" in
1844 --*)
4429d8b2 1845 __gitcomp_builtin merge-base
85453fd1
JK
1846 return
1847 ;;
1848 esac
15b4a163 1849 __git_complete_refs
690d8824
JH
1850}
1851
1127c51c
LM
1852_git_mv ()
1853{
1127c51c
LM
1854 case "$cur" in
1855 --*)
61d15cd6 1856 __gitcomp_builtin mv
1127c51c
LM
1857 return
1858 ;;
1859 esac
fea16b47
MP
1860
1861 if [ $(__git_count_arguments "mv") -gt 0 ]; then
1862 # We need to show both cached and untracked files (including
1863 # empty directories) since this may not be the last argument.
1864 __git_complete_index_file "--cached --others --directory"
1865 else
1866 __git_complete_index_file "--cached"
1867 fi
1127c51c
LM
1868}
1869
00f09d0e
SG
1870_git_notes ()
1871{
27b42d04 1872 local subcommands='add append copy edit get-ref list merge prune remove show'
2a5da755 1873 local subcommand="$(__git_find_on_cmdline "$subcommands")"
00f09d0e 1874
2a5da755
SG
1875 case "$subcommand,$cur" in
1876 ,--*)
7a60e3bb 1877 __gitcomp_builtin notes
2a5da755
SG
1878 ;;
1879 ,*)
39540681 1880 case "$prev" in
2a5da755 1881 --ref)
15b4a163 1882 __git_complete_refs
2a5da755
SG
1883 ;;
1884 *)
1885 __gitcomp "$subcommands --ref"
1886 ;;
1887 esac
1888 ;;
b25e2e64 1889 *,--reuse-message=*|*,--reedit-message=*)
15b4a163 1890 __git_complete_refs --cur="${cur#*=}"
2a5da755 1891 ;;
4ea2c974
NTND
1892 *,--*)
1893 __gitcomp_builtin notes_$subcommand
2a5da755 1894 ;;
27b42d04 1895 prune,*|get-ref,*)
4ea2c974 1896 # this command does not take a ref, do not complete it
00f09d0e
SG
1897 ;;
1898 *)
39540681 1899 case "$prev" in
2a5da755
SG
1900 -m|-F)
1901 ;;
1902 *)
15b4a163 1903 __git_complete_refs
2a5da755
SG
1904 ;;
1905 esac
00f09d0e
SG
1906 ;;
1907 esac
1908}
1909
690d8824
JH
1910_git_pull ()
1911{
0a4e1472
JS
1912 __git_complete_strategy && return
1913
0a4e1472 1914 case "$cur" in
4dd5c470
SYS
1915 --recurse-submodules=*)
1916 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1917 return
1918 ;;
0a4e1472 1919 --*)
2b1c01d2 1920 __gitcomp_builtin pull
32e64e50 1921
0a4e1472
JS
1922 return
1923 ;;
1924 esac
52d5c3b5 1925 __git_complete_remote_or_refspec
690d8824
JH
1926}
1927
446624ce 1928__git_push_recurse_submodules="check on-demand only"
4dd5c470 1929
aaf7253f
JK
1930__git_complete_force_with_lease ()
1931{
1932 local cur_=$1
1933
1934 case "$cur_" in
1935 --*=)
1936 ;;
1937 *:*)
15b4a163 1938 __git_complete_refs --cur="${cur_#*:}"
aaf7253f
JK
1939 ;;
1940 *)
15b4a163 1941 __git_complete_refs --cur="$cur_"
aaf7253f
JK
1942 ;;
1943 esac
1944}
1945
690d8824
JH
1946_git_push ()
1947{
da48616f 1948 case "$prev" in
0a4e1472 1949 --repo)
a31e6262 1950 __gitcomp_nl "$(__git_remotes)"
0a4e1472 1951 return
3a224ff2
JK
1952 ;;
1953 --recurse-submodules)
1954 __gitcomp "$__git_push_recurse_submodules"
1955 return
1956 ;;
0a4e1472
JS
1957 esac
1958 case "$cur" in
1959 --repo=*)
a31e6262 1960 __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
0a4e1472
JS
1961 return
1962 ;;
4dd5c470
SYS
1963 --recurse-submodules=*)
1964 __gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
1965 return
1966 ;;
aaf7253f
JK
1967 --force-with-lease=*)
1968 __git_complete_force_with_lease "${cur##--force-with-lease=}"
1969 return
1970 ;;
0a4e1472 1971 --*)
f1e1bdd6 1972 __gitcomp_builtin push
0a4e1472
JS
1973 return
1974 ;;
1975 esac
52d5c3b5 1976 __git_complete_remote_or_refspec
690d8824
JH
1977}
1978
7190a67e
JS
1979_git_range_diff ()
1980{
1981 case "$cur" in
1982 --*)
1983 __gitcomp "
27526793 1984 --creation-factor= --no-dual-color
7190a67e
JS
1985 $__git_diff_common_options
1986 "
1987 return
1988 ;;
1989 esac
1990 __git_complete_revlist
1991}
1992
61d926a3
SP
1993_git_rebase ()
1994{
fad9484f
SG
1995 __git_find_repo_path
1996 if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
66335298 1997 __gitcomp "--continue --skip --abort --quit --edit-todo --show-current-patch"
09bb6520 1998 return
fad9484f
SG
1999 elif [ -d "$__git_repo_path"/rebase-apply ] || \
2000 [ -d "$__git_repo_path"/rebase-merge ]; then
66335298 2001 __gitcomp "--continue --skip --abort --quit --show-current-patch"
61d926a3
SP
2002 return
2003 fi
3c7b480a 2004 __git_complete_strategy && return
61d926a3 2005 case "$cur" in
93cf50a4
BG
2006 --whitespace=*)
2007 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
2008 return
2009 ;;
61d926a3 2010 --*)
93cf50a4
BG
2011 __gitcomp "
2012 --onto --merge --strategy --interactive
8f6aed71 2013 --rebase-merges --preserve-merges --stat --no-stat
93cf50a4
BG
2014 --committer-date-is-author-date --ignore-date
2015 --ignore-whitespace --whitespace=
fa4b5e3a
JK
2016 --autosquash --no-autosquash
2017 --fork-point --no-fork-point
2018 --autostash --no-autostash
2019 --verify --no-verify
2020 --keep-empty --root --force-rebase --no-ff
b475e442 2021 --rerere-autoupdate
fa4b5e3a 2022 --exec
93cf50a4
BG
2023 "
2024
61d926a3
SP
2025 return
2026 esac
15b4a163 2027 __git_complete_refs
61d926a3
SP
2028}
2029
057f3279
TRC
2030_git_reflog ()
2031{
2032 local subcommands="show delete expire"
2033 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2034
2035 if [ -z "$subcommand" ]; then
2036 __gitcomp "$subcommands"
2037 else
15b4a163 2038 __git_complete_refs
057f3279
TRC
2039 fi
2040}
2041
ae616de6 2042__git_send_email_confirm_options="always never auto cc compose"
cb8a9bd5 2043__git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
ae616de6 2044
25a1f374
TL
2045_git_send_email ()
2046{
dfbe5eeb
JK
2047 case "$prev" in
2048 --to|--cc|--bcc|--from)
e15098a3 2049 __gitcomp "$(__git send-email --dump-aliases)"
dfbe5eeb
JK
2050 return
2051 ;;
2052 esac
2053
25a1f374 2054 case "$cur" in
ae616de6
SB
2055 --confirm=*)
2056 __gitcomp "
2057 $__git_send_email_confirm_options
2058 " "" "${cur##--confirm=}"
2059 return
2060 ;;
2061 --suppress-cc=*)
2062 __gitcomp "
2063 $__git_send_email_suppresscc_options
2064 " "" "${cur##--suppress-cc=}"
2065
2066 return
2067 ;;
2068 --smtp-encryption=*)
2069 __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
2070 return
2071 ;;
2f65494d
FC
2072 --thread=*)
2073 __gitcomp "
2074 deep shallow
2075 " "" "${cur##--thread=}"
2076 return
2077 ;;
dfbe5eeb 2078 --to=*|--cc=*|--bcc=*|--from=*)
e15098a3 2079 __gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
dfbe5eeb
JK
2080 return
2081 ;;
25a1f374 2082 --*)
77813151 2083 __gitcomp "--annotate --bcc --cc --cc-cmd --chain-reply-to
ae616de6
SB
2084 --compose --confirm= --dry-run --envelope-sender
2085 --from --identity
25a1f374 2086 --in-reply-to --no-chain-reply-to --no-signed-off-by-cc
d11c943c 2087 --no-suppress-from --no-thread --quiet --reply-to
25a1f374 2088 --signed-off-by-cc --smtp-pass --smtp-server
ae616de6
SB
2089 --smtp-server-port --smtp-encryption= --smtp-user
2090 --subject --suppress-cc= --suppress-from --thread --to
2f65494d
FC
2091 --validate --no-validate
2092 $__git_format_patch_options"
25a1f374
TL
2093 return
2094 ;;
2095 esac
2f65494d 2096 __git_complete_revlist
25a1f374
TL
2097}
2098
424cce83
SG
2099_git_stage ()
2100{
2101 _git_add
2102}
2103
634d2344
TB
2104_git_status ()
2105{
2106 local complete_opt
2107 local untracked_state
2108
2109 case "$cur" in
2110 --ignore-submodules=*)
2111 __gitcomp "none untracked dirty all" "" "${cur##--ignore-submodules=}"
2112 return
2113 ;;
2114 --untracked-files=*)
2115 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
2116 return
2117 ;;
2118 --column=*)
2119 __gitcomp "
2120 always never auto column row plain dense nodense
2121 " "" "${cur##--column=}"
2122 return
2123 ;;
2124 --*)
2b1c01d2 2125 __gitcomp_builtin status
634d2344
TB
2126 return
2127 ;;
2128 esac
2129
2130 untracked_state="$(__git_get_option_value "-u" "--untracked-files=" \
2131 "$__git_untracked_file_modes" "status.showUntrackedFiles")"
2132
2133 case "$untracked_state" in
2134 no)
2135 # --ignored option does not matter
2136 complete_opt=
2137 ;;
2138 all|normal|*)
2139 complete_opt="--cached --directory --no-empty-directory --others"
2140
2141 if [ -n "$(__git_find_on_cmdline "--ignored")" ]; then
2142 complete_opt="$complete_opt --ignored --exclude=*"
2143 fi
2144 ;;
2145 esac
2146
2147 __git_complete_index_file "$complete_opt"
2148}
2149
00652369
SB
2150__git_config_get_set_variables ()
2151{
da48616f 2152 local prevword word config_file= c=$cword
00652369 2153 while [ $c -gt 1 ]; do
da48616f 2154 word="${words[c]}"
00652369 2155 case "$word" in
66c0786c 2156 --system|--global|--local|--file=*)
00652369
SB
2157 config_file="$word"
2158 break
2159 ;;
2160 -f|--file)
2161 config_file="$word $prevword"
2162 break
2163 ;;
2164 esac
2165 prevword=$word
2166 c=$((--c))
2167 done
2168
e15098a3 2169 __git config $config_file --name-only --list
00652369
SB
2170}
2171
e17ca926
NTND
2172__git_config_vars=
2173__git_compute_config_vars ()
2174{
2175 test -n "$__git_config_vars" ||
2176 __git_config_vars="$(git help --config-for-completion | sort | uniq)"
2177}
2178
e0d10e1c 2179_git_config ()
5de40f59 2180{
bea21259
NTND
2181 local varname
2182
2183 if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
2184 varname="${prev,,}"
2185 else
2186 varname="$(echo "$prev" |tr A-Z a-z)"
2187 fi
2188
2189 case "$varname" in
72f75077 2190 branch.*.remote|branch.*.pushremote)
a31e6262 2191 __gitcomp_nl "$(__git_remotes)"
5de40f59
SP
2192 return
2193 ;;
2194 branch.*.merge)
15b4a163 2195 __git_complete_refs
5de40f59
SP
2196 return
2197 ;;
a05490ed 2198 branch.*.rebase)
1131ec98 2199 __gitcomp "false true merges preserve interactive"
a05490ed
RR
2200 return
2201 ;;
7e6a0cc4
RR
2202 remote.pushdefault)
2203 __gitcomp_nl "$(__git_remotes)"
2204 return
2205 ;;
5de40f59 2206 remote.*.fetch)
da48616f 2207 local remote="${prev#remote.}"
5de40f59 2208 remote="${remote%.fetch}"
d51a8ecd 2209 if [ -z "$cur" ]; then
73704451 2210 __gitcomp_nl "refs/heads/" "" "" ""
d51a8ecd
SG
2211 return
2212 fi
a31e6262 2213 __gitcomp_nl "$(__git_refs_remotes "$remote")"
5de40f59
SP
2214 return
2215 ;;
2216 remote.*.push)
da48616f 2217 local remote="${prev#remote.}"
5de40f59 2218 remote="${remote%.push}"
3ba04201 2219 __gitcomp_nl "$(__git for-each-ref \
1cd23e9e 2220 --format='%(refname):%(refname)' refs/heads)"
78d4d6a2
SP
2221 return
2222 ;;
2223 pull.twohead|pull.octopus)
eaa4e6ee
JN
2224 __git_compute_merge_strategies
2225 __gitcomp "$__git_merge_strategies"
78d4d6a2
SP
2226 return
2227 ;;
6123d719
MH
2228 color.branch|color.diff|color.interactive|\
2229 color.showbranch|color.status|color.ui)
78d4d6a2
SP
2230 __gitcomp "always never auto"
2231 return
2232 ;;
901d615c
MK
2233 color.pager)
2234 __gitcomp "false true"
2235 return
2236 ;;
78d4d6a2
SP
2237 color.*.*)
2238 __gitcomp "
98171a07 2239 normal black red green yellow blue magenta cyan white
78d4d6a2
SP
2240 bold dim ul blink reverse
2241 "
5de40f59
SP
2242 return
2243 ;;
2651baae
RR
2244 diff.submodule)
2245 __gitcomp "log short"
2246 return
2247 ;;
9b82d63b
SB
2248 help.format)
2249 __gitcomp "man info web html"
2250 return
2251 ;;
672c68cb
SB
2252 log.date)
2253 __gitcomp "$__git_log_date_formats"
2254 return
2255 ;;
92c4a7a1 2256 sendemail.aliasfiletype)
ae616de6
SB
2257 __gitcomp "mutt mailrc pine elm gnus"
2258 return
2259 ;;
2260 sendemail.confirm)
2261 __gitcomp "$__git_send_email_confirm_options"
2262 return
2263 ;;
2264 sendemail.suppresscc)
2265 __gitcomp "$__git_send_email_suppresscc_options"
2266 return
2267 ;;
8d814084
PB
2268 sendemail.transferencoding)
2269 __gitcomp "7bit 8bit quoted-printable base64"
2270 return
2271 ;;
00652369 2272 --get|--get-all|--unset|--unset-all)
a31e6262 2273 __gitcomp_nl "$(__git_config_get_set_variables)"
00652369
SB
2274 return
2275 ;;
5de40f59 2276 *.*)
5de40f59
SP
2277 return
2278 ;;
2279 esac
2280 case "$cur" in
2281 --*)
5983ba0d 2282 __gitcomp_builtin config
5de40f59
SP
2283 return
2284 ;;
2285 branch.*.*)
9244d69b 2286 local pfx="${cur%.*}." cur_="${cur##*.}"
f45db831 2287 __gitcomp "remote pushRemote merge mergeOptions rebase" "$pfx" "$cur_"
5de40f59
SP
2288 return
2289 ;;
2290 branch.*)
9244d69b 2291 local pfx="${cur%.*}." cur_="${cur#*.}"
227307a6 2292 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
f45db831 2293 __gitcomp_nl_append $'autoSetupMerge\nautoSetupRebase\n' "$pfx" "$cur_"
5de40f59
SP
2294 return
2295 ;;
0aa62fd0 2296 guitool.*.*)
9244d69b 2297 local pfx="${cur%.*}." cur_="${cur##*.}"
0aa62fd0 2298 __gitcomp "
f45db831
NTND
2299 argPrompt cmd confirm needsFile noConsole noRescan
2300 prompt revPrompt revUnmerged title
9244d69b 2301 " "$pfx" "$cur_"
0aa62fd0
SB
2302 return
2303 ;;
2304 difftool.*.*)
9244d69b
SG
2305 local pfx="${cur%.*}." cur_="${cur##*.}"
2306 __gitcomp "cmd path" "$pfx" "$cur_"
0aa62fd0
SB
2307 return
2308 ;;
2309 man.*.*)
9244d69b
SG
2310 local pfx="${cur%.*}." cur_="${cur##*.}"
2311 __gitcomp "cmd path" "$pfx" "$cur_"
0aa62fd0
SB
2312 return
2313 ;;
2314 mergetool.*.*)
9244d69b
SG
2315 local pfx="${cur%.*}." cur_="${cur##*.}"
2316 __gitcomp "cmd path trustExitCode" "$pfx" "$cur_"
0aa62fd0
SB
2317 return
2318 ;;
2319 pager.*)
9244d69b 2320 local pfx="${cur%.*}." cur_="${cur#*.}"
eaa4e6ee 2321 __git_compute_all_commands
a31e6262 2322 __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_"
0aa62fd0
SB
2323 return
2324 ;;
5de40f59 2325 remote.*.*)
9244d69b 2326 local pfx="${cur%.*}." cur_="${cur##*.}"
12977705 2327 __gitcomp "
98171a07 2328 url proxy fetch push mirror skipDefaultUpdate
f45db831 2329 receivepack uploadpack tagOpt pushurl
9244d69b 2330 " "$pfx" "$cur_"
5de40f59
SP
2331 return
2332 ;;
2333 remote.*)
9244d69b 2334 local pfx="${cur%.*}." cur_="${cur#*.}"
a31e6262 2335 __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
f45db831 2336 __gitcomp_nl_append "pushDefault" "$pfx" "$cur_"
5de40f59
SP
2337 return
2338 ;;
0aa62fd0 2339 url.*.*)
9244d69b
SG
2340 local pfx="${cur%.*}." cur_="${cur##*.}"
2341 __gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_"
0aa62fd0
SB
2342 return
2343 ;;
f22f6826
NTND
2344 *.*)
2345 __git_compute_config_vars
2346 __gitcomp "$__git_config_vars"
2347 ;;
2348 *)
2349 __git_compute_config_vars
2350 __gitcomp "$(echo "$__git_config_vars" | sed 's/\.[^ ]*/./g')"
5de40f59 2351 esac
5de40f59
SP
2352}
2353
88293c67
SP
2354_git_remote ()
2355{
cac84960
CW
2356 local subcommands="
2357 add rename remove set-head set-branches
2358 get-url set-url show prune update
2359 "
918c03c2 2360 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3ff1320d 2361 if [ -z "$subcommand" ]; then
cac84960
CW
2362 case "$cur" in
2363 --*)
ab6a11c5 2364 __gitcomp_builtin remote
cac84960
CW
2365 ;;
2366 *)
2367 __gitcomp "$subcommands"
2368 ;;
2369 esac
88293c67
SP
2370 return
2371 fi
2372
cac84960
CW
2373 case "$subcommand,$cur" in
2374 add,--*)
2b1c01d2 2375 __gitcomp_builtin remote_add
88293c67 2376 ;;
cac84960
CW
2377 add,*)
2378 ;;
2379 set-head,--*)
ab6a11c5 2380 __gitcomp_builtin remote_set-head
cac84960
CW
2381 ;;
2382 set-branches,--*)
ab6a11c5 2383 __gitcomp_builtin remote_set-branches
88293c67 2384 ;;
cac84960 2385 set-head,*|set-branches,*)
f1c6ffe6
PJ
2386 __git_complete_remote_or_refspec
2387 ;;
cac84960 2388 update,--*)
ab6a11c5 2389 __gitcomp_builtin remote_update
cac84960
CW
2390 ;;
2391 update,*)
9cd4382a 2392 __gitcomp "$(__git_remotes) $(__git_get_config_variables "remotes")"
fb72759b 2393 ;;
cac84960 2394 set-url,--*)
ab6a11c5 2395 __gitcomp_builtin remote_set-url
cac84960
CW
2396 ;;
2397 get-url,--*)
ab6a11c5 2398 __gitcomp_builtin remote_get-url
cac84960
CW
2399 ;;
2400 prune,--*)
ab6a11c5 2401 __gitcomp_builtin remote_prune
cac84960 2402 ;;
88293c67 2403 *)
cac84960 2404 __gitcomp_nl "$(__git_remotes)"
88293c67
SP
2405 ;;
2406 esac
2407}
2408
e1c1a067
BG
2409_git_replace ()
2410{
188fba11
CW
2411 case "$cur" in
2412 --*)
1b354755 2413 __gitcomp_builtin replace
188fba11
CW
2414 return
2415 ;;
2416 esac
15b4a163 2417 __git_complete_refs
e1c1a067
BG
2418}
2419
e24a256b
CW
2420_git_rerere ()
2421{
2422 local subcommands="clear forget diff remaining status gc"
2423 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2424 if test -z "$subcommand"
2425 then
2426 __gitcomp "$subcommands"
2427 return
2428 fi
2429}
2430
67e78c3b
SP
2431_git_reset ()
2432{
d773c631
SG
2433 __git_has_doubledash && return
2434
b3391775
SP
2435 case "$cur" in
2436 --*)
39073104 2437 __gitcomp_builtin reset
b3391775
SP
2438 return
2439 ;;
2440 esac
15b4a163 2441 __git_complete_refs
67e78c3b
SP
2442}
2443
e5f98518
NTND
2444__git_revert_inprogress_options="--continue --quit --abort"
2445
a6c2be24
LM
2446_git_revert ()
2447{
fad9484f
SG
2448 __git_find_repo_path
2449 if [ -f "$__git_repo_path"/REVERT_HEAD ]; then
e5f98518 2450 __gitcomp "$__git_revert_inprogress_options"
956352b6
TB
2451 return
2452 fi
a6c2be24
LM
2453 case "$cur" in
2454 --*)
2b1c01d2 2455 __gitcomp_builtin revert "" \
e5f98518 2456 "$__git_revert_inprogress_options"
a6c2be24
LM
2457 return
2458 ;;
2459 esac
15b4a163 2460 __git_complete_refs
a6c2be24
LM
2461}
2462
08c701d4
LM
2463_git_rm ()
2464{
08c701d4
LM
2465 case "$cur" in
2466 --*)
44c9a6d2 2467 __gitcomp_builtin rm
08c701d4
LM
2468 return
2469 ;;
2470 esac
fea16b47
MP
2471
2472 __git_complete_index_file "--cached"
08c701d4
LM
2473}
2474
1fd6bec9
SP
2475_git_shortlog ()
2476{
d773c631
SG
2477 __git_has_doubledash && return
2478
1fd6bec9
SP
2479 case "$cur" in
2480 --*)
2481 __gitcomp "
a393777e
TR
2482 $__git_log_common_options
2483 $__git_log_shortlog_options
f483a0aa 2484 --numbered --summary --email
1fd6bec9
SP
2485 "
2486 return
2487 ;;
2488 esac
2489 __git_complete_revlist
2490}
2491
90131924
SP
2492_git_show ()
2493{
41d8cf7d
MH
2494 __git_has_doubledash && return
2495
90131924 2496 case "$cur" in
e67d71e5 2497 --pretty=*|--format=*)
c3898111 2498 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
e67d71e5 2499 " "" "${cur#*=}"
72de29c2
TL
2500 return
2501 ;;
07924d4d
MP
2502 --diff-algorithm=*)
2503 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
2504 return
2505 ;;
ac76fd54
JK
2506 --submodule=*)
2507 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
2508 return
2509 ;;
90131924 2510 --*)
076c3237 2511 __gitcomp "--pretty= --format= --abbrev-commit --oneline
2ca0b197 2512 --show-signature
20bf7292
TR
2513 $__git_diff_common_options
2514 "
90131924
SP
2515 return
2516 ;;
2517 esac
5269f7f8 2518 __git_complete_revlist_file
90131924
SP
2519}
2520
2ca880fe
TR
2521_git_show_branch ()
2522{
2ca880fe
TR
2523 case "$cur" in
2524 --*)
2b1c01d2 2525 __gitcomp_builtin show-branch
2ca880fe
TR
2526 return
2527 ;;
2528 esac
2529 __git_complete_revlist
2530}
2531
7fd53fce
JH
2532_git_stash ()
2533{
d7d4ca87 2534 local save_opts='--all --keep-index --no-keep-index --quiet --patch --include-untracked'
0eb5a4f9
TG
2535 local subcommands='push list show apply clear drop pop create branch'
2536 local subcommand="$(__git_find_on_cmdline "$subcommands save")"
df70b190
TG
2537 if [ -n "$(__git_find_on_cmdline "-p")" ]; then
2538 subcommand="push"
2539 fi
7bedebca 2540 if [ -z "$subcommand" ]; then
59d5eeee
SG
2541 case "$cur" in
2542 --*)
2543 __gitcomp "$save_opts"
2544 ;;
0eb5a4f9
TG
2545 sa*)
2546 if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
2547 __gitcomp "save"
2548 fi
2549 ;;
59d5eeee
SG
2550 *)
2551 if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
2552 __gitcomp "$subcommands"
59d5eeee
SG
2553 fi
2554 ;;
2555 esac
7bedebca 2556 else
7bedebca 2557 case "$subcommand,$cur" in
3851e448
TG
2558 push,--*)
2559 __gitcomp "$save_opts --message"
2560 ;;
7bedebca 2561 save,--*)
59d5eeee 2562 __gitcomp "$save_opts"
7bedebca 2563 ;;
8513c54b 2564 apply,--*|pop,--*)
59d5eeee 2565 __gitcomp "--index --quiet"
95d43780 2566 ;;
d7d4ca87
PW
2567 drop,--*)
2568 __gitcomp "--quiet"
95d43780 2569 ;;
705f5f12
SF
2570 list,--*)
2571 __gitcomp "--name-status --oneline --patch-with-stat"
2572 ;;
d7d4ca87
PW
2573 show,--*|branch,--*)
2574 ;;
2575 branch,*)
59305aee 2576 if [ $cword -eq 3 ]; then
15b4a163 2577 __git_complete_refs
d7d4ca87 2578 else
1cd23e9e 2579 __gitcomp_nl "$(__git stash list \
d7d4ca87
PW
2580 | sed -n -e 's/:.*//p')"
2581 fi
2582 ;;
2583 show,*|apply,*|drop,*|pop,*)
1cd23e9e 2584 __gitcomp_nl "$(__git stash list \
95d43780
LM
2585 | sed -n -e 's/:.*//p')"
2586 ;;
7bedebca 2587 *)
7bedebca
SG
2588 ;;
2589 esac
3ff1320d 2590 fi
7fd53fce
JH
2591}
2592
be86f7a0
SP
2593_git_submodule ()
2594{
d773c631
SG
2595 __git_has_doubledash && return
2596
cf419828 2597 local subcommands="add status init deinit update summary foreach sync"
65d5a1e0
CW
2598 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2599 if [ -z "$subcommand" ]; then
be86f7a0
SP
2600 case "$cur" in
2601 --*)
65d5a1e0 2602 __gitcomp "--quiet"
be86f7a0
SP
2603 ;;
2604 *)
3ff1320d 2605 __gitcomp "$subcommands"
be86f7a0
SP
2606 ;;
2607 esac
2608 return
2609 fi
65d5a1e0
CW
2610
2611 case "$subcommand,$cur" in
2612 add,--*)
2613 __gitcomp "--branch --force --name --reference --depth"
2614 ;;
2615 status,--*)
2616 __gitcomp "--cached --recursive"
2617 ;;
2618 deinit,--*)
2619 __gitcomp "--force --all"
2620 ;;
2621 update,--*)
2622 __gitcomp "
2623 --init --remote --no-fetch
2624 --recommend-shallow --no-recommend-shallow
2625 --force --rebase --merge --reference --depth --recursive --jobs
2626 "
2627 ;;
2628 summary,--*)
2629 __gitcomp "--cached --files --summary-limit"
2630 ;;
2631 foreach,--*|sync,--*)
2632 __gitcomp "--recursive"
2633 ;;
2634 *)
2635 ;;
2636 esac
be86f7a0
SP
2637}
2638
47f6ee28
SG
2639_git_svn ()
2640{
2641 local subcommands="
2642 init fetch clone rebase dcommit log find-rev
2643 set-tree commit-diff info create-ignore propget
4a5856cb 2644 proplist show-ignore show-externals branch tag blame
c18d5d82 2645 migrate mkdirs reset gc
47f6ee28 2646 "
918c03c2 2647 local subcommand="$(__git_find_on_cmdline "$subcommands")"
47f6ee28
SG
2648 if [ -z "$subcommand" ]; then
2649 __gitcomp "$subcommands"
2650 else
2651 local remote_opts="--username= --config-dir= --no-auth-cache"
2652 local fc_opts="
2653 --follow-parent --authors-file= --repack=
2654 --no-metadata --use-svm-props --use-svnsync-props
2655 --log-window-size= --no-checkout --quiet
4a5856cb 2656 --repack-flags --use-log-author --localtime
2cbad176 2657 --add-author-from
a7b10230 2658 --ignore-paths= --include-paths= $remote_opts
47f6ee28
SG
2659 "
2660 local init_opts="
2661 --template= --shared= --trunk= --tags=
2662 --branches= --stdlayout --minimize-url
2663 --no-metadata --use-svm-props --use-svnsync-props
2cbad176 2664 --rewrite-root= --prefix= $remote_opts
47f6ee28
SG
2665 "
2666 local cmt_opts="
2667 --edit --rmdir --find-copies-harder --copy-similarity=
2668 "
2669
47f6ee28
SG
2670 case "$subcommand,$cur" in
2671 fetch,--*)
2672 __gitcomp "--revision= --fetch-all $fc_opts"
2673 ;;
2674 clone,--*)
2675 __gitcomp "--revision= $fc_opts $init_opts"
2676 ;;
2677 init,--*)
2678 __gitcomp "$init_opts"
2679 ;;
2680 dcommit,--*)
2681 __gitcomp "
2682 --merge --strategy= --verbose --dry-run
4a5856cb 2683 --fetch-all --no-rebase --commit-url
7b151f49 2684 --revision --interactive $cmt_opts $fc_opts
47f6ee28
SG
2685 "
2686 ;;
2687 set-tree,--*)
2688 __gitcomp "--stdin $cmt_opts $fc_opts"
2689 ;;
2690 create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
c18d5d82 2691 show-externals,--*|mkdirs,--*)
47f6ee28
SG
2692 __gitcomp "--revision="
2693 ;;
2694 log,--*)
2695 __gitcomp "
2696 --limit= --revision= --verbose --incremental
2697 --oneline --show-commit --non-recursive
4a5856cb 2698 --authors-file= --color
47f6ee28
SG
2699 "
2700 ;;
2701 rebase,--*)
2702 __gitcomp "
2703 --merge --verbose --strategy= --local
4a5856cb 2704 --fetch-all --dry-run $fc_opts
47f6ee28
SG
2705 "
2706 ;;
2707 commit-diff,--*)
2708 __gitcomp "--message= --file= --revision= $cmt_opts"
2709 ;;
2710 info,--*)
2711 __gitcomp "--url"
2712 ;;
4a5856cb
SG
2713 branch,--*)
2714 __gitcomp "--dry-run --message --tag"
2715 ;;
2716 tag,--*)
2717 __gitcomp "--dry-run --message"
2718 ;;
2719 blame,--*)
2720 __gitcomp "--git-format"
2721 ;;
2722 migrate,--*)
2723 __gitcomp "
2724 --config-dir= --ignore-paths= --minimize
2725 --no-auth-cache --username=
2726 "
2727 ;;
c18d5d82
RZ
2728 reset,--*)
2729 __gitcomp "--revision= --parent"
2730 ;;
47f6ee28 2731 *)
47f6ee28
SG
2732 ;;
2733 esac
2734 fi
2735}
2736
88e21dc7
SP
2737_git_tag ()
2738{
2739 local i c=1 f=0
da48616f
PD
2740 while [ $c -lt $cword ]; do
2741 i="${words[c]}"
88e21dc7 2742 case "$i" in
1775e990 2743 -d|--delete|-v|--verify)
227307a6 2744 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
88e21dc7
SP
2745 return
2746 ;;
2747 -f)
2748 f=1
2749 ;;
2750 esac
6e8c755f 2751 ((c++))
88e21dc7
SP
2752 done
2753
da48616f 2754 case "$prev" in
88e21dc7 2755 -m|-F)
88e21dc7 2756 ;;
8d8163f3 2757 -*|tag)
88e21dc7 2758 if [ $f = 1 ]; then
227307a6 2759 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
88e21dc7
SP
2760 fi
2761 ;;
2762 *)
15b4a163 2763 __git_complete_refs
88e21dc7
SP
2764 ;;
2765 esac
85ed2f32
RT
2766
2767 case "$cur" in
2768 --*)
80eb5197 2769 __gitcomp_builtin tag
85ed2f32
RT
2770 ;;
2771 esac
88e21dc7
SP
2772}
2773
424cce83
SG
2774_git_whatchanged ()
2775{
2776 _git_log
2777}
2778
b462c024
NTND
2779_git_worktree ()
2780{
cc73385c 2781 local subcommands="add list lock move prune remove unlock"
b462c024
NTND
2782 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2783 if [ -z "$subcommand" ]; then
2784 __gitcomp "$subcommands"
2785 else
2786 case "$subcommand,$cur" in
2787 add,--*)
fc3d4e0c 2788 __gitcomp_builtin worktree_add
b462c024
NTND
2789 ;;
2790 list,--*)
fc3d4e0c 2791 __gitcomp_builtin worktree_list
b462c024 2792 ;;
58142c09 2793 lock,--*)
fc3d4e0c 2794 __gitcomp_builtin worktree_lock
58142c09 2795 ;;
b462c024 2796 prune,--*)
fc3d4e0c 2797 __gitcomp_builtin worktree_prune
b462c024 2798 ;;
cc73385c
NTND
2799 remove,--*)
2800 __gitcomp "--force"
2801 ;;
b462c024
NTND
2802 *)
2803 ;;
2804 esac
2805 fi
2806}
2807
9f642a71
NTND
2808__git_complete_common () {
2809 local command="$1"
2810
2811 case "$cur" in
2812 --*)
2813 __gitcomp_builtin "$command"
2814 ;;
2815 esac
2816}
2817
2818__git_cmds_with_parseopt_helper=
2819__git_support_parseopt_helper () {
2820 test -n "$__git_cmds_with_parseopt_helper" ||
0089521c 2821 __git_cmds_with_parseopt_helper="$(__git --list-cmds=parseopt)"
9f642a71
NTND
2822
2823 case " $__git_cmds_with_parseopt_helper " in
2824 *" $1 "*)
2825 return 0
2826 ;;
2827 *)
2828 return 1
2829 ;;
2830 esac
2831}
2832
48e1c69a
NTND
2833__git_complete_command () {
2834 local command="$1"
2835 local completion_func="_git_${command//-/_}"
fb3a0cab
JH
2836 if ! declare -f $completion_func >/dev/null 2>/dev/null &&
2837 declare -f _completion_loader >/dev/null 2>/dev/null
2838 then
2839 _completion_loader "git-$command"
2840 fi
2841 if declare -f $completion_func >/dev/null 2>/dev/null
2842 then
48e1c69a
NTND
2843 $completion_func
2844 return 0
fb3a0cab
JH
2845 elif __git_support_parseopt_helper "$command"
2846 then
9f642a71
NTND
2847 __git_complete_common "$command"
2848 return 0
48e1c69a
NTND
2849 else
2850 return 1
2851 fi
2852}
2853
93b291e0 2854__git_main ()
690d8824 2855{
beb6ee71 2856 local i c=1 command __git_dir __git_repo_path
80ac0744 2857 local __git_C_args C_args_count=0
873537fa 2858
da48616f
PD
2859 while [ $c -lt $cword ]; do
2860 i="${words[c]}"
873537fa
SP
2861 case "$i" in
2862 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
776009d1 2863 --git-dir) ((c++)) ; __git_dir="${words[c]}" ;;
873537fa 2864 --bare) __git_dir="." ;;
1eb7e2f8 2865 --help) command="help"; break ;;
776009d1 2866 -c|--work-tree|--namespace) ((c++)) ;;
80ac0744
SG
2867 -C) __git_C_args[C_args_count++]=-C
2868 ((c++))
2869 __git_C_args[C_args_count++]="${words[c]}"
2870 ;;
911d5da6 2871 -*) ;;
873537fa
SP
2872 *) command="$i"; break ;;
2873 esac
6e8c755f 2874 ((c++))
873537fa
SP
2875 done
2876
1d17b22e 2877 if [ -z "$command" ]; then
7b329b9d
SG
2878 case "$prev" in
2879 --git-dir|-C|--work-tree)
2880 # these need a path argument, let's fall back to
2881 # Bash filename completion
2882 return
2883 ;;
2884 -c|--namespace)
2885 # we don't support completing these options' arguments
2886 return
2887 ;;
2888 esac
da48616f 2889 case "$cur" in
47e98eec 2890 --*) __gitcomp "
ce5a2c95 2891 --paginate
47e98eec
SP
2892 --no-pager
2893 --git-dir=
2894 --bare
2895 --version
2896 --exec-path
3ffcd086 2897 --exec-path=
89a56bfb 2898 --html-path
66fb37d0 2899 --man-path
69ef3c02 2900 --info-path
ce5a2c95 2901 --work-tree=
a1bea2c1 2902 --namespace=
69ef3c02 2903 --no-replace-objects
ce5a2c95 2904 --help
47e98eec
SP
2905 "
2906 ;;
3301d36b
NTND
2907 *)
2908 if test -n "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
2909 then
2910 __gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
2911 else
6532f374 2912 __gitcomp "$(git --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config)"
3301d36b
NTND
2913 fi
2914 ;;
72e5e989
SP
2915 esac
2916 return
873537fa 2917 fi
367dce2a 2918
48e1c69a 2919 __git_complete_command "$command" && return
8024ea60 2920
873537fa 2921 local expansion=$(__git_aliased_command "$command")
8024ea60 2922 if [ -n "$expansion" ]; then
880111c1 2923 words[1]=$expansion
48e1c69a 2924 __git_complete_command "$expansion"
8024ea60 2925 fi
690d8824
JH
2926}
2927
93b291e0 2928__gitk_main ()
690d8824 2929{
d773c631
SG
2930 __git_has_doubledash && return
2931
beb6ee71 2932 local __git_repo_path
fad9484f
SG
2933 __git_find_repo_path
2934
07ba53f7 2935 local merge=""
fad9484f 2936 if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
07ba53f7
RQ
2937 merge="--merge"
2938 fi
b3391775
SP
2939 case "$cur" in
2940 --*)
a393777e
TR
2941 __gitcomp "
2942 $__git_log_common_options
2943 $__git_log_gitk_options
2944 $merge
2945 "
b3391775
SP
2946 return
2947 ;;
2948 esac
ec804891 2949 __git_complete_revlist
690d8824
JH
2950}
2951
61d48c66
SG
2952if [[ -n ${ZSH_VERSION-} ]] &&
2953 # Don't define these functions when sourced from 'git-completion.zsh',
2954 # it has its own implementations.
2955 [[ -z ${GIT_SOURCING_ZSH_COMPLETION-} ]]; then
d8b45314 2956 echo "WARNING: this script is deprecated, please see git-completion.zsh" 1>&2
6b179adf 2957
9cd67bd2
FC
2958 autoload -U +X compinit && compinit
2959
d8b45314
FC
2960 __gitcomp ()
2961 {
2962 emulate -L zsh
6b179adf 2963
d8b45314
FC
2964 local cur_="${3-$cur}"
2965
2966 case "$cur_" in
2967 --*=)
2968 ;;
2969 *)
2970 local c IFS=$' \t\n'
2971 local -a array
2972 for c in ${=1}; do
2973 c="$c${4-}"
2974 case $c in
2975 --*=*|*.) ;;
2976 *) c="$c " ;;
2977 esac
5d5812f4 2978 array[${#array[@]}+1]="$c"
d8b45314
FC
2979 done
2980 compset -P '*[=:]'
2981 compadd -Q -S '' -p "${2-}" -a -- array && _ret=0
2982 ;;
2983 esac
2984 }
2985
fef56eb0
SG
2986 __gitcomp_direct ()
2987 {
2988 emulate -L zsh
2989
2990 local IFS=$'\n'
2991 compset -P '*[=:]'
2992 compadd -Q -- ${=1} && _ret=0
2993 }
2994
d8b45314
FC
2995 __gitcomp_nl ()
2996 {
2997 emulate -L zsh
2998
2999 local IFS=$'\n'
3000 compset -P '*[=:]'
3001 compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
3002 }
3003
7b003420
SG
3004 __gitcomp_file_direct ()
3005 {
3006 emulate -L zsh
3007
3008 local IFS=$'\n'
3009 compset -P '*[=:]'
3010 compadd -Q -f -- ${=1} && _ret=0
3011 }
3012
fea16b47
MP
3013 __gitcomp_file ()
3014 {
3015 emulate -L zsh
3016
3017 local IFS=$'\n'
3018 compset -P '*[=:]'
3019 compadd -Q -p "${2-}" -f -- ${=1} && _ret=0
3020 }
3021
d8b45314
FC
3022 _git ()
3023 {
2bcf694b
FC
3024 local _ret=1 cur cword prev
3025 cur=${words[CURRENT]}
3026 prev=${words[CURRENT-1]}
3027 let cword=CURRENT-1
3028 emulate ksh -c __${service}_main
734b2f05 3029 let _ret && _default && _ret=0
d8b45314
FC
3030 return _ret
3031 }
3032
3033 compdef _git git gitk
3034 return
3035fi
3036
3037__git_func_wrap ()
3038{
6b179adf
FC
3039 local cur words cword prev
3040 _get_comp_words_by_ref -n =: cur words cword prev
3041 $1
3042}
3043
3044# Setup completion for certain functions defined above by setting common
3045# variables and workarounds.
3046# This is NOT a public function; use at your own risk.
3047__git_complete ()
3048{
3049 local wrapper="__git_wrap${2}"
3050 eval "$wrapper () { __git_func_wrap $2 ; }"
3051 complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
3052 || complete -o default -o nospace -F $wrapper $1
3053}
3054
b0a4b2d2
FC
3055# wrapper for backwards compatibility
3056_git ()
3057{
93b291e0 3058 __git_wrap__git_main
b0a4b2d2
FC
3059}
3060
3061# wrapper for backwards compatibility
3062_gitk ()
3063{
93b291e0 3064 __git_wrap__gitk_main
b0a4b2d2
FC
3065}
3066
93b291e0
SG
3067__git_complete git __git_main
3068__git_complete gitk __gitk_main
690d8824
JH
3069
3070# The following are necessary only for Cygwin, and only are needed
3071# when the user has tab-completed the executable name and consequently
3072# included the '.exe' suffix.
3073#
76c3eb51 3074if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
93b291e0 3075__git_complete git.exe __git_main
76c3eb51 3076fi