]> git.ipfire.org Git - thirdparty/git.git/blame - contrib/completion/git-completion.bash
Merge branch 'br/commit-tree-parseopt'
[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 ; ... '".
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.
276b49ff 403 options=" $incl $(__git ${cmd/_/ } --git-completion-helper) "
d401f3de
NTND
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
06506149 441# path components.
7b003420
SG
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
5a59a230
NTND
856__git_merge_strategy_options="ours theirs subtree subtree= patience
857 histogram diff-algorithm= ignore-space-change ignore-all-space
858 ignore-space-at-eol renormalize no-renormalize no-renames
859 find-renames find-renames= rename-threshold="
860
1d66ec58 861__git_complete_revlist_file ()
690d8824 862{
6d54f528 863 local dequoted_word pfx ls ref cur_="$cur"
9244d69b 864 case "$cur_" in
1d66ec58
SG
865 *..?*:*)
866 return
867 ;;
690d8824 868 ?*:*)
9244d69b
SG
869 ref="${cur_%%:*}"
870 cur_="${cur_#*:}"
6d54f528
CY
871
872 __git_dequote "$cur_"
873
874 case "$dequoted_word" in
690d8824 875 ?*/*)
6d54f528
CY
876 pfx="${dequoted_word%/*}"
877 cur_="${dequoted_word##*/}"
690d8824
JH
878 ls="$ref:$pfx"
879 pfx="$pfx/"
880 ;;
881 *)
6d54f528 882 cur_="$dequoted_word"
690d8824
JH
883 ls="$ref"
884 ;;
80152b09 885 esac
db8a9ff0
SP
886
887 case "$COMP_WORDBREAKS" in
888 *:*) : great ;;
889 *) pfx="$ref:$pfx" ;;
890 esac
891
6d54f528
CY
892 __gitcomp_file "$(__git ls-tree "$ls" \
893 | sed 's/^.* //
894 s/$//')" \
895 "$pfx" "$cur_"
690d8824 896 ;;
f53352fb 897 *...*)
9244d69b
SG
898 pfx="${cur_%...*}..."
899 cur_="${cur_#*...}"
15b4a163 900 __git_complete_refs --pfx="$pfx" --cur="$cur_"
f53352fb
SP
901 ;;
902 *..*)
9244d69b
SG
903 pfx="${cur_%..*}.."
904 cur_="${cur_#*..}"
15b4a163 905 __git_complete_refs --pfx="$pfx" --cur="$cur_"
b3391775 906 ;;
f53352fb 907 *)
15b4a163 908 __git_complete_refs
f53352fb
SP
909 ;;
910 esac
911}
912
1d66ec58
SG
913__git_complete_file ()
914{
915 __git_complete_revlist_file
916}
917
918__git_complete_revlist ()
919{
920 __git_complete_revlist_file
921}
922
52d5c3b5
JS
923__git_complete_remote_or_refspec ()
924{
9244d69b 925 local cur_="$cur" cmd="${words[1]}"
0a4e1472 926 local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
f1c6ffe6 927 if [ "$cmd" = "remote" ]; then
6e8c755f 928 ((c++))
f1c6ffe6 929 fi
da48616f
PD
930 while [ $c -lt $cword ]; do
931 i="${words[c]}"
52d5c3b5 932 case "$i" in
e25e2b42 933 --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
723c1d52 934 -d|--delete) [ "$cmd" = "push" ] && lhs=0 ;;
e25e2b42
BG
935 --all)
936 case "$cmd" in
937 push) no_complete_refspec=1 ;;
938 fetch)
e25e2b42
BG
939 return
940 ;;
941 *) ;;
942 esac
943 ;;
b5619f6d 944 --multiple) no_complete_refspec=1; break ;;
52d5c3b5
JS
945 -*) ;;
946 *) remote="$i"; break ;;
947 esac
6e8c755f 948 ((c++))
52d5c3b5
JS
949 done
950 if [ -z "$remote" ]; then
a31e6262 951 __gitcomp_nl "$(__git_remotes)"
52d5c3b5
JS
952 return
953 fi
0a4e1472 954 if [ $no_complete_refspec = 1 ]; then
0a4e1472
JS
955 return
956 fi
52d5c3b5 957 [ "$remote" = "." ] && remote=
9244d69b 958 case "$cur_" in
52d5c3b5
JS
959 *:*)
960 case "$COMP_WORDBREAKS" in
961 *:*) : great ;;
9244d69b 962 *) pfx="${cur_%%:*}:" ;;
52d5c3b5 963 esac
9244d69b 964 cur_="${cur_#*:}"
52d5c3b5
JS
965 lhs=0
966 ;;
967 +*)
968 pfx="+"
9244d69b 969 cur_="${cur_#+}"
52d5c3b5
JS
970 ;;
971 esac
972 case "$cmd" in
973 fetch)
974 if [ $lhs = 1 ]; then
aa0644f7 975 __git_complete_fetch_refspecs "$remote" "$pfx" "$cur_"
52d5c3b5 976 else
15b4a163 977 __git_complete_refs --pfx="$pfx" --cur="$cur_"
52d5c3b5
JS
978 fi
979 ;;
f1c6ffe6 980 pull|remote)
52d5c3b5 981 if [ $lhs = 1 ]; then
15b4a163 982 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
52d5c3b5 983 else
15b4a163 984 __git_complete_refs --pfx="$pfx" --cur="$cur_"
52d5c3b5
JS
985 fi
986 ;;
987 push)
988 if [ $lhs = 1 ]; then
15b4a163 989 __git_complete_refs --pfx="$pfx" --cur="$cur_"
52d5c3b5 990 else
15b4a163 991 __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
52d5c3b5
JS
992 fi
993 ;;
994 esac
995}
996
3c7b480a
JS
997__git_complete_strategy ()
998{
eaa4e6ee 999 __git_compute_merge_strategies
da48616f 1000 case "$prev" in
3c7b480a 1001 -s|--strategy)
eaa4e6ee 1002 __gitcomp "$__git_merge_strategies"
3c7b480a 1003 return 0
5a59a230
NTND
1004 ;;
1005 -X)
1006 __gitcomp "$__git_merge_strategy_options"
1007 return 0
1008 ;;
3c7b480a 1009 esac
3c7b480a
JS
1010 case "$cur" in
1011 --strategy=*)
eaa4e6ee 1012 __gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
3c7b480a
JS
1013 return 0
1014 ;;
5a59a230
NTND
1015 --strategy-option=*)
1016 __gitcomp "$__git_merge_strategy_options" "" "${cur##--strategy-option=}"
1017 return 0
1018 ;;
3c7b480a
JS
1019 esac
1020 return 1
1021}
1022
eaa4e6ee
JN
1023__git_all_commands=
1024__git_compute_all_commands ()
1025{
cf0ff02a 1026 test -n "$__git_all_commands" ||
3301d36b 1027 __git_all_commands=$(git --list-cmds=main,others,alias,nohelpers)
eaa4e6ee 1028}
f2bb9f88 1029
e8f9e428
SG
1030# Lists all set config variables starting with the given section prefix,
1031# with the prefix removed.
1032__git_get_config_variables ()
c3898111 1033{
e8f9e428 1034 local section="$1" i IFS=$'\n'
e15098a3 1035 for i in $(__git config --name-only --get-regexp "^$section\..*"); do
905f2036 1036 echo "${i#$section.}"
c3898111
SG
1037 done
1038}
1039
c3898111
SG
1040__git_pretty_aliases ()
1041{
e8f9e428 1042 __git_get_config_variables "pretty"
c3898111
SG
1043}
1044
a42577d4 1045# __git_aliased_command requires 1 argument
367dce2a
DS
1046__git_aliased_command ()
1047{
e15098a3 1048 local word cmdline=$(__git config --get "alias.$1")
367dce2a 1049 for word in $cmdline; do
c63437cb 1050 case "$word" in
66729509
SG
1051 \!gitk|gitk)
1052 echo "gitk"
367dce2a 1053 return
66729509 1054 ;;
c63437cb
SG
1055 \!*) : shell command alias ;;
1056 -*) : option ;;
1057 *=*) : setting env ;;
1058 git) : git itself ;;
56f24e80
SP
1059 \(\)) : skip parens of shell function definition ;;
1060 {) : skip start of shell helper function ;;
1061 :) : skip null command ;;
1062 \'*) : skip opening quote after sh -c ;;
c63437cb
SG
1063 *)
1064 echo "$word"
367dce2a 1065 return
c63437cb 1066 esac
367dce2a
DS
1067 done
1068}
1069
918c03c2
SG
1070# __git_find_on_cmdline requires 1 argument
1071__git_find_on_cmdline ()
3ff1320d 1072{
da4902a7 1073 local word subcommand c=1
da48616f
PD
1074 while [ $c -lt $cword ]; do
1075 word="${words[c]}"
3ff1320d
SG
1076 for subcommand in $1; do
1077 if [ "$subcommand" = "$word" ]; then
1078 echo "$subcommand"
1079 return
1080 fi
1081 done
6e8c755f 1082 ((c++))
3ff1320d
SG
1083 done
1084}
1085
7c599e92
TB
1086# Echo the value of an option set on the command line or config
1087#
1088# $1: short option name
1089# $2: long option name including =
1090# $3: list of possible values
1091# $4: config string (optional)
1092#
1093# example:
1094# result="$(__git_get_option_value "-d" "--do-something=" \
1095# "yes no" "core.doSomething")"
1096#
1097# result is then either empty (no option set) or "yes" or "no"
1098#
1099# __git_get_option_value requires 3 arguments
1100__git_get_option_value ()
1101{
1102 local c short_opt long_opt val
1103 local result= values config_key word
1104
1105 short_opt="$1"
1106 long_opt="$2"
1107 values="$3"
1108 config_key="$4"
1109
1110 ((c = $cword - 1))
1111 while [ $c -ge 0 ]; do
1112 word="${words[c]}"
1113 for val in $values; do
1114 if [ "$short_opt$val" = "$word" ] ||
1115 [ "$long_opt$val" = "$word" ]; then
1116 result="$val"
1117 break 2
1118 fi
1119 done
1120 ((c--))
1121 done
1122
1123 if [ -n "$config_key" ] && [ -z "$result" ]; then
1cd23e9e 1124 result="$(__git config "$config_key")"
7c599e92
TB
1125 fi
1126
1127 echo "$result"
1128}
1129
d773c631
SG
1130__git_has_doubledash ()
1131{
da4902a7 1132 local c=1
da48616f
PD
1133 while [ $c -lt $cword ]; do
1134 if [ "--" = "${words[c]}" ]; then
d773c631
SG
1135 return 0
1136 fi
6e8c755f 1137 ((c++))
d773c631
SG
1138 done
1139 return 1
1140}
1141
fea16b47
MP
1142# Try to count non option arguments passed on the command line for the
1143# specified git command.
1144# When options are used, it is necessary to use the special -- option to
1145# tell the implementation were non option arguments begin.
1146# XXX this can not be improved, since options can appear everywhere, as
1147# an example:
1148# git mv x -n y
1149#
1150# __git_count_arguments requires 1 argument: the git command executed.
1151__git_count_arguments ()
1152{
1153 local word i c=0
1154
1155 # Skip "git" (first argument)
1156 for ((i=1; i < ${#words[@]}; i++)); do
1157 word="${words[i]}"
1158
1159 case "$word" in
1160 --)
1161 # Good; we can assume that the following are only non
1162 # option arguments.
1163 ((c = 0))
1164 ;;
1165 "$1")
1166 # Skip the specified git command and discard git
1167 # main options
1168 ((c = 0))
1169 ;;
1170 ?*)
1171 ((c++))
1172 ;;
1173 esac
1174 done
1175
1176 printf "%d" $c
1177}
1178
7950659d 1179__git_whitespacelist="nowarn warn error error-all fix"
5a59a230 1180__git_patchformat="mbox stgit stgit-series hg mboxrd"
7fb6aefd 1181__git_am_inprogress_options="--skip --continue --resolved --abort --quit --show-current-patch"
88329195
SP
1182
1183_git_am ()
1184{
fad9484f
SG
1185 __git_find_repo_path
1186 if [ -d "$__git_repo_path"/rebase-apply ]; then
be3ce6b2 1187 __gitcomp "$__git_am_inprogress_options"
88329195
SP
1188 return
1189 fi
1190 case "$cur" in
1191 --whitespace=*)
b3391775 1192 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
88329195
SP
1193 return
1194 ;;
5a59a230
NTND
1195 --patch-format=*)
1196 __gitcomp "$__git_patchformat" "" "${cur##--patch-format=}"
1197 return
1198 ;;
88329195 1199 --*)
2b1c01d2 1200 __gitcomp_builtin am "" \
be3ce6b2 1201 "$__git_am_inprogress_options"
88329195
SP
1202 return
1203 esac
88329195
SP
1204}
1205
1206_git_apply ()
1207{
88329195
SP
1208 case "$cur" in
1209 --whitespace=*)
b3391775 1210 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
88329195
SP
1211 return
1212 ;;
1213 --*)
b8e9d662 1214 __gitcomp_builtin apply
88329195
SP
1215 return
1216 esac
88329195
SP
1217}
1218
8435b548
SP
1219_git_add ()
1220{
8435b548 1221 case "$cur" in
5a59a230
NTND
1222 --chmod=*)
1223 __gitcomp "+x -x" "" "${cur##--chmod=}"
1224 return
1225 ;;
8435b548 1226 --*)
e1bea2c0 1227 __gitcomp_builtin add
8435b548
SP
1228 return
1229 esac
fea16b47 1230
bd9ab9df
CW
1231 local complete_opt="--others --modified --directory --no-empty-directory"
1232 if test -n "$(__git_find_on_cmdline "-u --update")"
1233 then
1234 complete_opt="--modified"
1235 fi
1236 __git_complete_index_file "$complete_opt"
8435b548
SP
1237}
1238
b3191ce2
LM
1239_git_archive ()
1240{
b3191ce2
LM
1241 case "$cur" in
1242 --format=*)
1243 __gitcomp "$(git archive --list)" "" "${cur##--format=}"
1244 return
1245 ;;
1246 --remote=*)
a31e6262 1247 __gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
b3191ce2
LM
1248 return
1249 ;;
1250 --*)
1251 __gitcomp "
1252 --format= --list --verbose
f483a0aa 1253 --prefix= --remote= --exec= --output
b3191ce2
LM
1254 "
1255 return
1256 ;;
1257 esac
1258 __git_complete_file
1259}
1260
b2e69f62
SP
1261_git_bisect ()
1262{
d773c631
SG
1263 __git_has_doubledash && return
1264
bf11d461 1265 local subcommands="start bad good skip reset visualize replay log run"
918c03c2 1266 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3ff1320d 1267 if [ -z "$subcommand" ]; then
fad9484f
SG
1268 __git_find_repo_path
1269 if [ -f "$__git_repo_path"/BISECT_START ]; then
128191f5
SG
1270 __gitcomp "$subcommands"
1271 else
1272 __gitcomp "replay start"
1273 fi
b2e69f62
SP
1274 return
1275 fi
1276
3ff1320d 1277 case "$subcommand" in
8205ff8e 1278 bad|good|reset|skip|start)
15b4a163 1279 __git_complete_refs
b2e69f62
SP
1280 ;;
1281 *)
b2e69f62
SP
1282 ;;
1283 esac
1284}
1285
5a59a230
NTND
1286__git_ref_fieldlist="refname objecttype objectsize objectname upstream push HEAD symref"
1287
690d8824
JH
1288_git_branch ()
1289{
da4902a7 1290 local i c=1 only_local_ref="n" has_r="n"
b9217642 1291
da48616f
PD
1292 while [ $c -lt $cword ]; do
1293 i="${words[c]}"
b9217642 1294 case "$i" in
2703c22f
VS
1295 -d|--delete|-m|--move) only_local_ref="y" ;;
1296 -r|--remotes) has_r="y" ;;
b9217642 1297 esac
6e8c755f 1298 ((c++))
b9217642
SG
1299 done
1300
da48616f 1301 case "$cur" in
ca45d0fa 1302 --set-upstream-to=*)
15b4a163 1303 __git_complete_refs --cur="${cur##--set-upstream-to=}"
ca45d0fa 1304 ;;
3b376b0c 1305 --*)
2b1c01d2 1306 __gitcomp_builtin branch
3b376b0c 1307 ;;
b9217642
SG
1308 *)
1309 if [ $only_local_ref = "y" -a $has_r = "n" ]; then
227307a6 1310 __gitcomp_direct "$(__git_heads "" "$cur" " ")"
b9217642 1311 else
15b4a163 1312 __git_complete_refs
b9217642
SG
1313 fi
1314 ;;
3b376b0c 1315 esac
690d8824
JH
1316}
1317
374a58c9
ML
1318_git_bundle ()
1319{
da48616f
PD
1320 local cmd="${words[2]}"
1321 case "$cword" in
8d8163f3 1322 2)
374a58c9
ML
1323 __gitcomp "create list-heads verify unbundle"
1324 ;;
8d8163f3 1325 3)
374a58c9
ML
1326 # looking for a file
1327 ;;
1328 *)
1329 case "$cmd" in
1330 create)
1331 __git_complete_revlist
1332 ;;
1333 esac
1334 ;;
1335 esac
1336}
1337
690d8824
JH
1338_git_checkout ()
1339{
c84bb14c
SG
1340 __git_has_doubledash && return
1341
e648f8b6
SG
1342 case "$cur" in
1343 --conflict=*)
1344 __gitcomp "diff3 merge" "" "${cur##--conflict=}"
1345 ;;
1346 --*)
2b1c01d2 1347 __gitcomp_builtin checkout
e648f8b6
SG
1348 ;;
1349 *)
34a6bbb5
KB
1350 # check if --track, --no-track, or --no-guess was specified
1351 # if so, disable DWIM mode
15b4a163 1352 local flags="--track --no-track --no-guess" track_opt="--track"
60e71bbc
JK
1353 if [ "$GIT_COMPLETION_CHECKOUT_NO_GUESS" = "1" ] ||
1354 [ -n "$(__git_find_on_cmdline "$flags")" ]; then
15b4a163 1355 track_opt=''
34a6bbb5 1356 fi
15b4a163 1357 __git_complete_refs $track_opt
e648f8b6
SG
1358 ;;
1359 esac
690d8824
JH
1360}
1361
660003e2
NTND
1362__git_cherry_pick_inprogress_options="--continue --quit --abort"
1363
1273231e
SP
1364_git_cherry_pick ()
1365{
fad9484f
SG
1366 __git_find_repo_path
1367 if [ -f "$__git_repo_path"/CHERRY_PICK_HEAD ]; then
660003e2 1368 __gitcomp "$__git_cherry_pick_inprogress_options"
7655fa7f
FC
1369 return
1370 fi
5a59a230
NTND
1371
1372 __git_complete_strategy && return
1373
1273231e
SP
1374 case "$cur" in
1375 --*)
660003e2
NTND
1376 __gitcomp_builtin cherry-pick "" \
1377 "$__git_cherry_pick_inprogress_options"
1273231e
SP
1378 ;;
1379 *)
15b4a163 1380 __git_complete_refs
1273231e
SP
1381 ;;
1382 esac
1383}
1384
4181c7e8
LM
1385_git_clean ()
1386{
4181c7e8
LM
1387 case "$cur" in
1388 --*)
26e90958 1389 __gitcomp_builtin clean
4181c7e8
LM
1390 return
1391 ;;
1392 esac
fea16b47
MP
1393
1394 # XXX should we check for -x option ?
ea95c7b8 1395 __git_complete_index_file "--others --directory"
4181c7e8
LM
1396}
1397
3eb11012
LM
1398_git_clone ()
1399{
3eb11012
LM
1400 case "$cur" in
1401 --*)
2b1c01d2 1402 __gitcomp_builtin clone
3eb11012
LM
1403 return
1404 ;;
1405 esac
3eb11012
LM
1406}
1407
21d2a9e3
TB
1408__git_untracked_file_modes="all no normal"
1409
4548e855
SP
1410_git_commit ()
1411{
fea16b47
MP
1412 case "$prev" in
1413 -c|-C)
15b4a163 1414 __git_complete_refs
fea16b47
MP
1415 return
1416 ;;
1417 esac
d773c631 1418
4548e855 1419 case "$cur" in
9a424b27 1420 --cleanup=*)
72dbb365 1421 __gitcomp "default scissors strip verbatim whitespace
9a424b27
SG
1422 " "" "${cur##--cleanup=}"
1423 return
1424 ;;
77653abd
TM
1425 --reuse-message=*|--reedit-message=*|\
1426 --fixup=*|--squash=*)
15b4a163 1427 __git_complete_refs --cur="${cur#*=}"
9a424b27
SG
1428 return
1429 ;;
1430 --untracked-files=*)
21d2a9e3 1431 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
9a424b27
SG
1432 return
1433 ;;
4548e855 1434 --*)
2b1c01d2 1435 __gitcomp_builtin commit
4548e855
SP
1436 return
1437 esac
fea16b47 1438
1cd23e9e 1439 if __git rev-parse --verify --quiet HEAD >/dev/null; then
f825972c 1440 __git_complete_index_file "--committable"
fea16b47
MP
1441 else
1442 # This is the first commit
1443 __git_complete_index_file "--cached"
1444 fi
4548e855
SP
1445}
1446
217926c0
SP
1447_git_describe ()
1448{
cbb504c9
TR
1449 case "$cur" in
1450 --*)
ddced834 1451 __gitcomp_builtin describe
cbb504c9
TR
1452 return
1453 esac
15b4a163 1454 __git_complete_refs
217926c0
SP
1455}
1456
07924d4d
MP
1457__git_diff_algorithms="myers minimal patience histogram"
1458
462f2134 1459__git_diff_submodule_formats="diff log short"
ac76fd54 1460
20bf7292 1461__git_diff_common_options="--stat --numstat --shortstat --summary
b3a4f858
JS
1462 --patch-with-stat --name-only --name-status --color
1463 --no-color --color-words --no-renames --check
f135aacb 1464 --full-index --binary --abbrev --diff-filter=
e9282f02 1465 --find-copies-harder --ignore-cr-at-eol
b3a4f858 1466 --text --ignore-space-at-eol --ignore-space-change
c2545167
TB
1467 --ignore-all-space --ignore-blank-lines --exit-code
1468 --quiet --ext-diff --no-ext-diff
aba201c6 1469 --no-prefix --src-prefix= --dst-prefix=
6d0e674a 1470 --inter-hunk-context=
216120ab 1471 --patience --histogram --minimal
e6414b46 1472 --raw --word-diff --word-diff-regex=
8fd2cfa7
SB
1473 --dirstat --dirstat= --dirstat-by-file
1474 --dirstat-by-file= --cumulative
07924d4d 1475 --diff-algorithm=
6cc4bc15 1476 --submodule --submodule= --ignore-submodules
20bf7292
TR
1477"
1478
1479_git_diff ()
1480{
1481 __git_has_doubledash && return
1482
20bf7292 1483 case "$cur" in
07924d4d
MP
1484 --diff-algorithm=*)
1485 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1486 return
1487 ;;
ac76fd54
JK
1488 --submodule=*)
1489 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1490 return
1491 ;;
20bf7292 1492 --*)
ebd15bf0 1493 __gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
861514d3 1494 --base --ours --theirs --no-index
20bf7292 1495 $__git_diff_common_options
aba201c6 1496 "
b3a4f858
JS
1497 return
1498 ;;
1499 esac
1d66ec58 1500 __git_complete_revlist_file
690d8824
JH
1501}
1502
c5f424fd 1503__git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff
f13f9b0e 1504 tkdiff vimdiff gvimdiff xxdiff araxis p4merge bc codecompare
e2dc2de9
DA
1505"
1506
1507_git_difftool ()
1508{
f7ad96cf
MH
1509 __git_has_doubledash && return
1510
e2dc2de9
DA
1511 case "$cur" in
1512 --tool=*)
1513 __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1514 return
1515 ;;
1516 --*)
6cc4bc15
NTND
1517 __gitcomp_builtin difftool "$__git_diff_common_options
1518 --base --cached --ours --theirs
1519 --pickaxe-all --pickaxe-regex
1520 --relative --staged
1521 "
e2dc2de9
DA
1522 return
1523 ;;
1524 esac
d8517cc6 1525 __git_complete_revlist_file
e2dc2de9
DA
1526}
1527
4dd5c470
SYS
1528__git_fetch_recurse_submodules="yes on-demand no"
1529
690d8824
JH
1530_git_fetch ()
1531{
0a4e1472 1532 case "$cur" in
4dd5c470
SYS
1533 --recurse-submodules=*)
1534 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1535 return
1536 ;;
5a59a230
NTND
1537 --filter=*)
1538 __gitcomp "blob:none blob:limit= sparse:oid= sparse:path=" "" "${cur##--filter=}"
1539 return
1540 ;;
0a4e1472 1541 --*)
2b1c01d2 1542 __gitcomp_builtin fetch
0a4e1472
JS
1543 return
1544 ;;
1545 esac
52d5c3b5 1546 __git_complete_remote_or_refspec
690d8824
JH
1547}
1548
13374987
NTND
1549__git_format_patch_extra_options="
1550 --full-index --not --all --no-prefix --src-prefix=
1551 --dst-prefix= --notes
2f65494d
FC
1552"
1553
f53352fb
SP
1554_git_format_patch ()
1555{
f53352fb 1556 case "$cur" in
e1d37937
SB
1557 --thread=*)
1558 __gitcomp "
1559 deep shallow
1560 " "" "${cur##--thread=}"
1561 return
1562 ;;
f53352fb 1563 --*)
13374987 1564 __gitcomp_builtin format-patch "$__git_format_patch_extra_options"
f53352fb
SP
1565 return
1566 ;;
1567 esac
1568 __git_complete_revlist
1569}
1570
4bca8636
AJ
1571_git_fsck ()
1572{
4bca8636
AJ
1573 case "$cur" in
1574 --*)
2b1c01d2 1575 __gitcomp_builtin fsck
4bca8636
AJ
1576 return
1577 ;;
1578 esac
4bca8636
AJ
1579}
1580
66729509
SG
1581_git_gitk ()
1582{
1583 _gitk
1584}
1585
7826a786
SG
1586# Lists matching symbol names from a tag (as in ctags) file.
1587# 1: List symbol names matching this word.
1588# 2: The tag file to list symbol names from.
1589# 3: A prefix to be added to each listed symbol name (optional).
1590# 4: A suffix to be appended to each listed symbol name (optional).
1591__git_match_ctag () {
1592 awk -v pfx="${3-}" -v sfx="${4-}" "
1593 /^${1//\//\\/}/ { print pfx \$1 sfx }
1594 " "$2"
29eec71f
JK
1595}
1596
2f779f91
SG
1597# Complete symbol names from a tag file.
1598# Usage: __git_complete_symbol [<option>]...
1599# --tags=<file>: The tag file to list symbol names from instead of the
1600# default "tags".
1601# --pfx=<prefix>: A prefix to be added to each symbol name.
1602# --cur=<word>: The current symbol name to be completed. Defaults to
1603# the current word to be completed.
1604# --sfx=<suffix>: A suffix to be appended to each symbol name instead
1605# of the default space.
1606__git_complete_symbol () {
1607 local tags=tags pfx="" cur_="${cur-}" sfx=" "
1608
1609 while test $# != 0; do
1610 case "$1" in
1611 --tags=*) tags="${1##--tags=}" ;;
1612 --pfx=*) pfx="${1##--pfx=}" ;;
1613 --cur=*) cur_="${1##--cur=}" ;;
1614 --sfx=*) sfx="${1##--sfx=}" ;;
1615 *) return 1 ;;
1616 esac
1617 shift
1618 done
1619
1620 if test -r "$tags"; then
1621 __gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
1622 fi
29eec71f
JK
1623}
1624
c72e0db1
LM
1625_git_grep ()
1626{
1627 __git_has_doubledash && return
1628
c72e0db1
LM
1629 case "$cur" in
1630 --*)
caf2de33 1631 __gitcomp_builtin grep
c72e0db1
LM
1632 return
1633 ;;
1634 esac
17225c49 1635
29eec71f
JK
1636 case "$cword,$prev" in
1637 2,*|*,-*)
2f779f91 1638 __git_complete_symbol && return
29eec71f
JK
1639 ;;
1640 esac
1641
15b4a163 1642 __git_complete_refs
c72e0db1
LM
1643}
1644
1eb7e2f8
LM
1645_git_help ()
1646{
1eb7e2f8
LM
1647 case "$cur" in
1648 --*)
8c13a8d7 1649 __gitcomp_builtin help
1eb7e2f8
LM
1650 return
1651 ;;
1652 esac
3301d36b
NTND
1653 if test -n "$GIT_TESTING_ALL_COMMAND_LIST"
1654 then
1655 __gitcomp "$GIT_TESTING_ALL_COMMAND_LIST $(git --list-cmds=alias,list-guide) gitk"
1656 else
1657 __gitcomp "$(git --list-cmds=main,nohelpers,alias,list-guide) gitk"
1658 fi
1eb7e2f8
LM
1659}
1660
5dad868b
LM
1661_git_init ()
1662{
5dad868b
LM
1663 case "$cur" in
1664 --shared=*)
1665 __gitcomp "
1666 false true umask group all world everybody
1667 " "" "${cur##--shared=}"
1668 return
1669 ;;
1670 --*)
b00116b7 1671 __gitcomp_builtin init
5dad868b
LM
1672 return
1673 ;;
1674 esac
5dad868b
LM
1675}
1676
b1bc1494
LM
1677_git_ls_files ()
1678{
b1bc1494
LM
1679 case "$cur" in
1680 --*)
2b1c01d2 1681 __gitcomp_builtin ls-files
b1bc1494
LM
1682 return
1683 ;;
1684 esac
fea16b47
MP
1685
1686 # XXX ignore options like --modified and always suggest all cached
1687 # files.
1688 __git_complete_index_file "--cached"
b1bc1494
LM
1689}
1690
690d8824
JH
1691_git_ls_remote ()
1692{
2c0f3a53
CW
1693 case "$cur" in
1694 --*)
cdc71c1c 1695 __gitcomp_builtin ls-remote
2c0f3a53
CW
1696 return
1697 ;;
1698 esac
a31e6262 1699 __gitcomp_nl "$(__git_remotes)"
690d8824
JH
1700}
1701
1702_git_ls_tree ()
1703{
be6d1b24
NTND
1704 case "$cur" in
1705 --*)
1706 __gitcomp_builtin ls-tree
1707 return
1708 ;;
1709 esac
1710
690d8824
JH
1711 __git_complete_file
1712}
1713
a393777e
TR
1714# Options that go well for log, shortlog and gitk
1715__git_log_common_options="
1716 --not --all
1717 --branches --tags --remotes
4fe1a619 1718 --first-parent --merges --no-merges
a393777e
TR
1719 --max-count=
1720 --max-age= --since= --after=
1721 --min-age= --until= --before=
6a6ebded
MG
1722 --min-parents= --max-parents=
1723 --no-min-parents --no-max-parents
a393777e
TR
1724"
1725# Options that go well for log and gitk (not shortlog)
1726__git_log_gitk_options="
1727 --dense --sparse --full-history
1728 --simplify-merges --simplify-by-decoration
3925b575 1729 --left-right --notes --no-notes
a393777e
TR
1730"
1731# Options that go well for log and shortlog (not gitk)
1732__git_log_shortlog_options="
1733 --author= --committer= --grep=
22dfa8a2 1734 --all-match --invert-grep
a393777e
TR
1735"
1736
5a59a230
NTND
1737__git_log_pretty_formats="oneline short medium full fuller email raw format: mboxrd"
1738__git_log_date_formats="relative iso8601 iso8601-strict rfc2822 short local default raw unix format:"
3d279863 1739
690d8824
JH
1740_git_log ()
1741{
d773c631 1742 __git_has_doubledash && return
fad9484f 1743 __git_find_repo_path
d773c631 1744
bf3c20f6 1745 local merge=""
fad9484f 1746 if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
bf3c20f6
TR
1747 merge="--merge"
1748 fi
41d2716c
SG
1749 case "$prev,$cur" in
1750 -L,:*:*)
1751 return # fall back to Bash filename completion
1752 ;;
1753 -L,:*)
1754 __git_complete_symbol --cur="${cur#:}" --sfx=":"
1755 return
1756 ;;
1757 -G,*|-S,*)
1758 __git_complete_symbol
1759 return
1760 ;;
1761 esac
6e31b866 1762 case "$cur" in
e67d71e5 1763 --pretty=*|--format=*)
c3898111 1764 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
e67d71e5 1765 " "" "${cur#*=}"
72de29c2
TL
1766 return
1767 ;;
47e98eec 1768 --date=*)
672c68cb 1769 __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
47e98eec
SP
1770 return
1771 ;;
af4e9e8c 1772 --decorate=*)
af16bdaa 1773 __gitcomp "full short no" "" "${cur##--decorate=}"
af4e9e8c
SB
1774 return
1775 ;;
ac76fd54
JK
1776 --diff-algorithm=*)
1777 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1778 return
1779 ;;
1780 --submodule=*)
1781 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1782 return
1783 ;;
6e31b866 1784 --*)
b3391775 1785 __gitcomp "
a393777e
TR
1786 $__git_log_common_options
1787 $__git_log_shortlog_options
1788 $__git_log_gitk_options
8f87fae6 1789 --root --topo-order --date-order --reverse
5d0e6343 1790 --follow --full-diff
6e31b866 1791 --abbrev-commit --abbrev=
47e98eec 1792 --relative-date --date=
72de29c2 1793 --pretty= --format= --oneline
2ca0b197 1794 --show-signature
d3bfbf91 1795 --cherry-mark
a393777e 1796 --cherry-pick
20827d99 1797 --graph
af4e9e8c 1798 --decorate --decorate=
20bf7292 1799 --walk-reflogs
a393777e 1800 --parents --children
bf3c20f6 1801 $merge
20bf7292 1802 $__git_diff_common_options
47d5a8fa 1803 --pickaxe-all --pickaxe-regex
b3391775 1804 "
6e31b866
SP
1805 return
1806 ;;
41d2716c
SG
1807 -L:*:*)
1808 return # fall back to Bash filename completion
1809 ;;
1810 -L:*)
1811 __git_complete_symbol --cur="${cur#-L:}" --sfx=":"
1812 return
1813 ;;
1814 -G*)
1815 __git_complete_symbol --pfx="-G" --cur="${cur#-G}"
1816 return
1817 ;;
1818 -S*)
1819 __git_complete_symbol --pfx="-S" --cur="${cur#-S}"
1820 return
1821 ;;
6e31b866 1822 esac
f53352fb 1823 __git_complete_revlist
690d8824
JH
1824}
1825
4ad91321
SP
1826_git_merge ()
1827{
3c7b480a
JS
1828 __git_complete_strategy && return
1829
4ad91321
SP
1830 case "$cur" in
1831 --*)
2b1c01d2 1832 __gitcomp_builtin merge
4ad91321
SP
1833 return
1834 esac
15b4a163 1835 __git_complete_refs
4ad91321
SP
1836}
1837
b4c72162
LM
1838_git_mergetool ()
1839{
b4c72162
LM
1840 case "$cur" in
1841 --tool=*)
e2dc2de9 1842 __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
b4c72162
LM
1843 return
1844 ;;
1845 --*)
57ba1812 1846 __gitcomp "--tool= --prompt --no-prompt --gui --no-gui"
b4c72162
LM
1847 return
1848 ;;
1849 esac
b4c72162
LM
1850}
1851
690d8824
JH
1852_git_merge_base ()
1853{
85453fd1
JK
1854 case "$cur" in
1855 --*)
4429d8b2 1856 __gitcomp_builtin merge-base
85453fd1
JK
1857 return
1858 ;;
1859 esac
15b4a163 1860 __git_complete_refs
690d8824
JH
1861}
1862
1127c51c
LM
1863_git_mv ()
1864{
1127c51c
LM
1865 case "$cur" in
1866 --*)
61d15cd6 1867 __gitcomp_builtin mv
1127c51c
LM
1868 return
1869 ;;
1870 esac
fea16b47
MP
1871
1872 if [ $(__git_count_arguments "mv") -gt 0 ]; then
1873 # We need to show both cached and untracked files (including
1874 # empty directories) since this may not be the last argument.
1875 __git_complete_index_file "--cached --others --directory"
1876 else
1877 __git_complete_index_file "--cached"
1878 fi
1127c51c
LM
1879}
1880
00f09d0e
SG
1881_git_notes ()
1882{
27b42d04 1883 local subcommands='add append copy edit get-ref list merge prune remove show'
2a5da755 1884 local subcommand="$(__git_find_on_cmdline "$subcommands")"
00f09d0e 1885
2a5da755
SG
1886 case "$subcommand,$cur" in
1887 ,--*)
7a60e3bb 1888 __gitcomp_builtin notes
2a5da755
SG
1889 ;;
1890 ,*)
39540681 1891 case "$prev" in
2a5da755 1892 --ref)
15b4a163 1893 __git_complete_refs
2a5da755
SG
1894 ;;
1895 *)
1896 __gitcomp "$subcommands --ref"
1897 ;;
1898 esac
1899 ;;
b25e2e64 1900 *,--reuse-message=*|*,--reedit-message=*)
15b4a163 1901 __git_complete_refs --cur="${cur#*=}"
2a5da755 1902 ;;
4ea2c974
NTND
1903 *,--*)
1904 __gitcomp_builtin notes_$subcommand
2a5da755 1905 ;;
27b42d04 1906 prune,*|get-ref,*)
4ea2c974 1907 # this command does not take a ref, do not complete it
00f09d0e
SG
1908 ;;
1909 *)
39540681 1910 case "$prev" in
2a5da755
SG
1911 -m|-F)
1912 ;;
1913 *)
15b4a163 1914 __git_complete_refs
2a5da755
SG
1915 ;;
1916 esac
00f09d0e
SG
1917 ;;
1918 esac
1919}
1920
690d8824
JH
1921_git_pull ()
1922{
0a4e1472
JS
1923 __git_complete_strategy && return
1924
0a4e1472 1925 case "$cur" in
4dd5c470
SYS
1926 --recurse-submodules=*)
1927 __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1928 return
1929 ;;
0a4e1472 1930 --*)
2b1c01d2 1931 __gitcomp_builtin pull
32e64e50 1932
0a4e1472
JS
1933 return
1934 ;;
1935 esac
52d5c3b5 1936 __git_complete_remote_or_refspec
690d8824
JH
1937}
1938
446624ce 1939__git_push_recurse_submodules="check on-demand only"
4dd5c470 1940
aaf7253f
JK
1941__git_complete_force_with_lease ()
1942{
1943 local cur_=$1
1944
1945 case "$cur_" in
1946 --*=)
1947 ;;
1948 *:*)
15b4a163 1949 __git_complete_refs --cur="${cur_#*:}"
aaf7253f
JK
1950 ;;
1951 *)
15b4a163 1952 __git_complete_refs --cur="$cur_"
aaf7253f
JK
1953 ;;
1954 esac
1955}
1956
690d8824
JH
1957_git_push ()
1958{
da48616f 1959 case "$prev" in
0a4e1472 1960 --repo)
a31e6262 1961 __gitcomp_nl "$(__git_remotes)"
0a4e1472 1962 return
3a224ff2
JK
1963 ;;
1964 --recurse-submodules)
1965 __gitcomp "$__git_push_recurse_submodules"
1966 return
1967 ;;
0a4e1472
JS
1968 esac
1969 case "$cur" in
1970 --repo=*)
a31e6262 1971 __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
0a4e1472
JS
1972 return
1973 ;;
4dd5c470
SYS
1974 --recurse-submodules=*)
1975 __gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
1976 return
1977 ;;
aaf7253f
JK
1978 --force-with-lease=*)
1979 __git_complete_force_with_lease "${cur##--force-with-lease=}"
1980 return
1981 ;;
0a4e1472 1982 --*)
f1e1bdd6 1983 __gitcomp_builtin push
0a4e1472
JS
1984 return
1985 ;;
1986 esac
52d5c3b5 1987 __git_complete_remote_or_refspec
690d8824
JH
1988}
1989
7190a67e
JS
1990_git_range_diff ()
1991{
1992 case "$cur" in
1993 --*)
1994 __gitcomp "
27526793 1995 --creation-factor= --no-dual-color
7190a67e
JS
1996 $__git_diff_common_options
1997 "
1998 return
1999 ;;
2000 esac
2001 __git_complete_revlist
2002}
2003
61d926a3
SP
2004_git_rebase ()
2005{
fad9484f
SG
2006 __git_find_repo_path
2007 if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
66335298 2008 __gitcomp "--continue --skip --abort --quit --edit-todo --show-current-patch"
09bb6520 2009 return
fad9484f
SG
2010 elif [ -d "$__git_repo_path"/rebase-apply ] || \
2011 [ -d "$__git_repo_path"/rebase-merge ]; then
66335298 2012 __gitcomp "--continue --skip --abort --quit --show-current-patch"
61d926a3
SP
2013 return
2014 fi
3c7b480a 2015 __git_complete_strategy && return
61d926a3 2016 case "$cur" in
93cf50a4
BG
2017 --whitespace=*)
2018 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
2019 return
2020 ;;
61d926a3 2021 --*)
93cf50a4
BG
2022 __gitcomp "
2023 --onto --merge --strategy --interactive
8f6aed71 2024 --rebase-merges --preserve-merges --stat --no-stat
93cf50a4
BG
2025 --committer-date-is-author-date --ignore-date
2026 --ignore-whitespace --whitespace=
fa4b5e3a
JK
2027 --autosquash --no-autosquash
2028 --fork-point --no-fork-point
2029 --autostash --no-autostash
2030 --verify --no-verify
2031 --keep-empty --root --force-rebase --no-ff
b475e442 2032 --rerere-autoupdate
fa4b5e3a 2033 --exec
93cf50a4
BG
2034 "
2035
61d926a3
SP
2036 return
2037 esac
15b4a163 2038 __git_complete_refs
61d926a3
SP
2039}
2040
057f3279
TRC
2041_git_reflog ()
2042{
2043 local subcommands="show delete expire"
2044 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2045
2046 if [ -z "$subcommand" ]; then
2047 __gitcomp "$subcommands"
2048 else
15b4a163 2049 __git_complete_refs
057f3279
TRC
2050 fi
2051}
2052
ae616de6 2053__git_send_email_confirm_options="always never auto cc compose"
cb8a9bd5 2054__git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
ae616de6 2055
25a1f374
TL
2056_git_send_email ()
2057{
dfbe5eeb
JK
2058 case "$prev" in
2059 --to|--cc|--bcc|--from)
e15098a3 2060 __gitcomp "$(__git send-email --dump-aliases)"
dfbe5eeb
JK
2061 return
2062 ;;
2063 esac
2064
25a1f374 2065 case "$cur" in
ae616de6
SB
2066 --confirm=*)
2067 __gitcomp "
2068 $__git_send_email_confirm_options
2069 " "" "${cur##--confirm=}"
2070 return
2071 ;;
2072 --suppress-cc=*)
2073 __gitcomp "
2074 $__git_send_email_suppresscc_options
2075 " "" "${cur##--suppress-cc=}"
2076
2077 return
2078 ;;
2079 --smtp-encryption=*)
2080 __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
2081 return
2082 ;;
2f65494d
FC
2083 --thread=*)
2084 __gitcomp "
2085 deep shallow
2086 " "" "${cur##--thread=}"
2087 return
2088 ;;
dfbe5eeb 2089 --to=*|--cc=*|--bcc=*|--from=*)
e15098a3 2090 __gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
dfbe5eeb
JK
2091 return
2092 ;;
25a1f374 2093 --*)
13374987 2094 __gitcomp_builtin send-email "--annotate --bcc --cc --cc-cmd --chain-reply-to
ae616de6
SB
2095 --compose --confirm= --dry-run --envelope-sender
2096 --from --identity
25a1f374 2097 --in-reply-to --no-chain-reply-to --no-signed-off-by-cc
d11c943c 2098 --no-suppress-from --no-thread --quiet --reply-to
25a1f374 2099 --signed-off-by-cc --smtp-pass --smtp-server
ae616de6
SB
2100 --smtp-server-port --smtp-encryption= --smtp-user
2101 --subject --suppress-cc= --suppress-from --thread --to
2f65494d 2102 --validate --no-validate
13374987 2103 $__git_format_patch_extra_options"
25a1f374
TL
2104 return
2105 ;;
2106 esac
2f65494d 2107 __git_complete_revlist
25a1f374
TL
2108}
2109
424cce83
SG
2110_git_stage ()
2111{
2112 _git_add
2113}
2114
634d2344
TB
2115_git_status ()
2116{
2117 local complete_opt
2118 local untracked_state
2119
2120 case "$cur" in
2121 --ignore-submodules=*)
2122 __gitcomp "none untracked dirty all" "" "${cur##--ignore-submodules=}"
2123 return
2124 ;;
2125 --untracked-files=*)
2126 __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
2127 return
2128 ;;
2129 --column=*)
2130 __gitcomp "
2131 always never auto column row plain dense nodense
2132 " "" "${cur##--column=}"
2133 return
2134 ;;
2135 --*)
2b1c01d2 2136 __gitcomp_builtin status
634d2344
TB
2137 return
2138 ;;
2139 esac
2140
2141 untracked_state="$(__git_get_option_value "-u" "--untracked-files=" \
2142 "$__git_untracked_file_modes" "status.showUntrackedFiles")"
2143
2144 case "$untracked_state" in
2145 no)
2146 # --ignored option does not matter
2147 complete_opt=
2148 ;;
2149 all|normal|*)
2150 complete_opt="--cached --directory --no-empty-directory --others"
2151
2152 if [ -n "$(__git_find_on_cmdline "--ignored")" ]; then
2153 complete_opt="$complete_opt --ignored --exclude=*"
2154 fi
2155 ;;
2156 esac
2157
2158 __git_complete_index_file "$complete_opt"
2159}
2160
00652369
SB
2161__git_config_get_set_variables ()
2162{
da48616f 2163 local prevword word config_file= c=$cword
00652369 2164 while [ $c -gt 1 ]; do
da48616f 2165 word="${words[c]}"
00652369 2166 case "$word" in
66c0786c 2167 --system|--global|--local|--file=*)
00652369
SB
2168 config_file="$word"
2169 break
2170 ;;
2171 -f|--file)
2172 config_file="$word $prevword"
2173 break
2174 ;;
2175 esac
2176 prevword=$word
2177 c=$((--c))
2178 done
2179
e15098a3 2180 __git config $config_file --name-only --list
00652369
SB
2181}
2182
e17ca926
NTND
2183__git_config_vars=
2184__git_compute_config_vars ()
2185{
2186 test -n "$__git_config_vars" ||
2187 __git_config_vars="$(git help --config-for-completion | sort | uniq)"
2188}
2189
e0d10e1c 2190_git_config ()
5de40f59 2191{
bea21259
NTND
2192 local varname
2193
2194 if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
2195 varname="${prev,,}"
2196 else
2197 varname="$(echo "$prev" |tr A-Z a-z)"
2198 fi
2199
2200 case "$varname" in
72f75077 2201 branch.*.remote|branch.*.pushremote)
a31e6262 2202 __gitcomp_nl "$(__git_remotes)"
5de40f59
SP
2203 return
2204 ;;
2205 branch.*.merge)
15b4a163 2206 __git_complete_refs
5de40f59
SP
2207 return
2208 ;;
a05490ed 2209 branch.*.rebase)
1131ec98 2210 __gitcomp "false true merges preserve interactive"
a05490ed
RR
2211 return
2212 ;;
7e6a0cc4
RR
2213 remote.pushdefault)
2214 __gitcomp_nl "$(__git_remotes)"
2215 return
2216 ;;
5de40f59 2217 remote.*.fetch)
da48616f 2218 local remote="${prev#remote.}"
5de40f59 2219 remote="${remote%.fetch}"
d51a8ecd 2220 if [ -z "$cur" ]; then
73704451 2221 __gitcomp_nl "refs/heads/" "" "" ""
d51a8ecd
SG
2222 return
2223 fi
a31e6262 2224 __gitcomp_nl "$(__git_refs_remotes "$remote")"
5de40f59
SP
2225 return
2226 ;;
2227 remote.*.push)
da48616f 2228 local remote="${prev#remote.}"
5de40f59 2229 remote="${remote%.push}"
3ba04201 2230 __gitcomp_nl "$(__git for-each-ref \
1cd23e9e 2231 --format='%(refname):%(refname)' refs/heads)"
78d4d6a2
SP
2232 return
2233 ;;
2234 pull.twohead|pull.octopus)
eaa4e6ee
JN
2235 __git_compute_merge_strategies
2236 __gitcomp "$__git_merge_strategies"
78d4d6a2
SP
2237 return
2238 ;;
6123d719
MH
2239 color.branch|color.diff|color.interactive|\
2240 color.showbranch|color.status|color.ui)
78d4d6a2
SP
2241 __gitcomp "always never auto"
2242 return
2243 ;;
901d615c
MK
2244 color.pager)
2245 __gitcomp "false true"
2246 return
2247 ;;
78d4d6a2
SP
2248 color.*.*)
2249 __gitcomp "
98171a07 2250 normal black red green yellow blue magenta cyan white
78d4d6a2
SP
2251 bold dim ul blink reverse
2252 "
5de40f59
SP
2253 return
2254 ;;
2651baae 2255 diff.submodule)
5a59a230 2256 __gitcomp "$__git_diff_submodule_formats"
2651baae
RR
2257 return
2258 ;;
9b82d63b
SB
2259 help.format)
2260 __gitcomp "man info web html"
2261 return
2262 ;;
672c68cb
SB
2263 log.date)
2264 __gitcomp "$__git_log_date_formats"
2265 return
2266 ;;
92c4a7a1 2267 sendemail.aliasfiletype)
ae616de6
SB
2268 __gitcomp "mutt mailrc pine elm gnus"
2269 return
2270 ;;
2271 sendemail.confirm)
2272 __gitcomp "$__git_send_email_confirm_options"
2273 return
2274 ;;
2275 sendemail.suppresscc)
2276 __gitcomp "$__git_send_email_suppresscc_options"
2277 return
2278 ;;
8d814084
PB
2279 sendemail.transferencoding)
2280 __gitcomp "7bit 8bit quoted-printable base64"
2281 return
2282 ;;
00652369 2283 --get|--get-all|--unset|--unset-all)
a31e6262 2284 __gitcomp_nl "$(__git_config_get_set_variables)"
00652369
SB
2285 return
2286 ;;
5de40f59 2287 *.*)
5de40f59
SP
2288 return
2289 ;;
2290 esac
2291 case "$cur" in
2292 --*)
5983ba0d 2293 __gitcomp_builtin config
5de40f59
SP
2294 return
2295 ;;
2296 branch.*.*)
9244d69b 2297 local pfx="${cur%.*}." cur_="${cur##*.}"
f45db831 2298 __gitcomp "remote pushRemote merge mergeOptions rebase" "$pfx" "$cur_"
5de40f59
SP
2299 return
2300 ;;
2301 branch.*)
9244d69b 2302 local pfx="${cur%.*}." cur_="${cur#*.}"
227307a6 2303 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
f45db831 2304 __gitcomp_nl_append $'autoSetupMerge\nautoSetupRebase\n' "$pfx" "$cur_"
5de40f59
SP
2305 return
2306 ;;
0aa62fd0 2307 guitool.*.*)
9244d69b 2308 local pfx="${cur%.*}." cur_="${cur##*.}"
0aa62fd0 2309 __gitcomp "
f45db831
NTND
2310 argPrompt cmd confirm needsFile noConsole noRescan
2311 prompt revPrompt revUnmerged title
9244d69b 2312 " "$pfx" "$cur_"
0aa62fd0
SB
2313 return
2314 ;;
2315 difftool.*.*)
9244d69b
SG
2316 local pfx="${cur%.*}." cur_="${cur##*.}"
2317 __gitcomp "cmd path" "$pfx" "$cur_"
0aa62fd0
SB
2318 return
2319 ;;
2320 man.*.*)
9244d69b
SG
2321 local pfx="${cur%.*}." cur_="${cur##*.}"
2322 __gitcomp "cmd path" "$pfx" "$cur_"
0aa62fd0
SB
2323 return
2324 ;;
2325 mergetool.*.*)
9244d69b
SG
2326 local pfx="${cur%.*}." cur_="${cur##*.}"
2327 __gitcomp "cmd path trustExitCode" "$pfx" "$cur_"
0aa62fd0
SB
2328 return
2329 ;;
2330 pager.*)
9244d69b 2331 local pfx="${cur%.*}." cur_="${cur#*.}"
eaa4e6ee 2332 __git_compute_all_commands
a31e6262 2333 __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_"
0aa62fd0
SB
2334 return
2335 ;;
5de40f59 2336 remote.*.*)
9244d69b 2337 local pfx="${cur%.*}." cur_="${cur##*.}"
12977705 2338 __gitcomp "
98171a07 2339 url proxy fetch push mirror skipDefaultUpdate
f45db831 2340 receivepack uploadpack tagOpt pushurl
9244d69b 2341 " "$pfx" "$cur_"
5de40f59
SP
2342 return
2343 ;;
2344 remote.*)
9244d69b 2345 local pfx="${cur%.*}." cur_="${cur#*.}"
a31e6262 2346 __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
f45db831 2347 __gitcomp_nl_append "pushDefault" "$pfx" "$cur_"
5de40f59
SP
2348 return
2349 ;;
0aa62fd0 2350 url.*.*)
9244d69b
SG
2351 local pfx="${cur%.*}." cur_="${cur##*.}"
2352 __gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_"
0aa62fd0
SB
2353 return
2354 ;;
f22f6826
NTND
2355 *.*)
2356 __git_compute_config_vars
2357 __gitcomp "$__git_config_vars"
2358 ;;
2359 *)
2360 __git_compute_config_vars
2361 __gitcomp "$(echo "$__git_config_vars" | sed 's/\.[^ ]*/./g')"
5de40f59 2362 esac
5de40f59
SP
2363}
2364
88293c67
SP
2365_git_remote ()
2366{
cac84960
CW
2367 local subcommands="
2368 add rename remove set-head set-branches
2369 get-url set-url show prune update
2370 "
918c03c2 2371 local subcommand="$(__git_find_on_cmdline "$subcommands")"
3ff1320d 2372 if [ -z "$subcommand" ]; then
cac84960
CW
2373 case "$cur" in
2374 --*)
ab6a11c5 2375 __gitcomp_builtin remote
cac84960
CW
2376 ;;
2377 *)
2378 __gitcomp "$subcommands"
2379 ;;
2380 esac
88293c67
SP
2381 return
2382 fi
2383
cac84960
CW
2384 case "$subcommand,$cur" in
2385 add,--*)
2b1c01d2 2386 __gitcomp_builtin remote_add
88293c67 2387 ;;
cac84960
CW
2388 add,*)
2389 ;;
2390 set-head,--*)
ab6a11c5 2391 __gitcomp_builtin remote_set-head
cac84960
CW
2392 ;;
2393 set-branches,--*)
ab6a11c5 2394 __gitcomp_builtin remote_set-branches
88293c67 2395 ;;
cac84960 2396 set-head,*|set-branches,*)
f1c6ffe6
PJ
2397 __git_complete_remote_or_refspec
2398 ;;
cac84960 2399 update,--*)
ab6a11c5 2400 __gitcomp_builtin remote_update
cac84960
CW
2401 ;;
2402 update,*)
9cd4382a 2403 __gitcomp "$(__git_remotes) $(__git_get_config_variables "remotes")"
fb72759b 2404 ;;
cac84960 2405 set-url,--*)
ab6a11c5 2406 __gitcomp_builtin remote_set-url
cac84960
CW
2407 ;;
2408 get-url,--*)
ab6a11c5 2409 __gitcomp_builtin remote_get-url
cac84960
CW
2410 ;;
2411 prune,--*)
ab6a11c5 2412 __gitcomp_builtin remote_prune
cac84960 2413 ;;
88293c67 2414 *)
cac84960 2415 __gitcomp_nl "$(__git_remotes)"
88293c67
SP
2416 ;;
2417 esac
2418}
2419
e1c1a067
BG
2420_git_replace ()
2421{
188fba11 2422 case "$cur" in
5a59a230
NTND
2423 --format=*)
2424 __gitcomp "short medium long" "" "${cur##--format=}"
2425 return
2426 ;;
188fba11 2427 --*)
1b354755 2428 __gitcomp_builtin replace
188fba11
CW
2429 return
2430 ;;
2431 esac
15b4a163 2432 __git_complete_refs
e1c1a067
BG
2433}
2434
e24a256b
CW
2435_git_rerere ()
2436{
2437 local subcommands="clear forget diff remaining status gc"
2438 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2439 if test -z "$subcommand"
2440 then
2441 __gitcomp "$subcommands"
2442 return
2443 fi
2444}
2445
67e78c3b
SP
2446_git_reset ()
2447{
d773c631
SG
2448 __git_has_doubledash && return
2449
b3391775
SP
2450 case "$cur" in
2451 --*)
39073104 2452 __gitcomp_builtin reset
b3391775
SP
2453 return
2454 ;;
2455 esac
15b4a163 2456 __git_complete_refs
67e78c3b
SP
2457}
2458
e5f98518
NTND
2459__git_revert_inprogress_options="--continue --quit --abort"
2460
a6c2be24
LM
2461_git_revert ()
2462{
fad9484f
SG
2463 __git_find_repo_path
2464 if [ -f "$__git_repo_path"/REVERT_HEAD ]; then
e5f98518 2465 __gitcomp "$__git_revert_inprogress_options"
956352b6
TB
2466 return
2467 fi
5a59a230 2468 __git_complete_strategy && return
a6c2be24
LM
2469 case "$cur" in
2470 --*)
2b1c01d2 2471 __gitcomp_builtin revert "" \
e5f98518 2472 "$__git_revert_inprogress_options"
a6c2be24
LM
2473 return
2474 ;;
2475 esac
15b4a163 2476 __git_complete_refs
a6c2be24
LM
2477}
2478
08c701d4
LM
2479_git_rm ()
2480{
08c701d4
LM
2481 case "$cur" in
2482 --*)
44c9a6d2 2483 __gitcomp_builtin rm
08c701d4
LM
2484 return
2485 ;;
2486 esac
fea16b47
MP
2487
2488 __git_complete_index_file "--cached"
08c701d4
LM
2489}
2490
1fd6bec9
SP
2491_git_shortlog ()
2492{
d773c631
SG
2493 __git_has_doubledash && return
2494
1fd6bec9
SP
2495 case "$cur" in
2496 --*)
2497 __gitcomp "
a393777e
TR
2498 $__git_log_common_options
2499 $__git_log_shortlog_options
f483a0aa 2500 --numbered --summary --email
1fd6bec9
SP
2501 "
2502 return
2503 ;;
2504 esac
2505 __git_complete_revlist
2506}
2507
90131924
SP
2508_git_show ()
2509{
41d8cf7d
MH
2510 __git_has_doubledash && return
2511
90131924 2512 case "$cur" in
e67d71e5 2513 --pretty=*|--format=*)
c3898111 2514 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
e67d71e5 2515 " "" "${cur#*=}"
72de29c2
TL
2516 return
2517 ;;
07924d4d
MP
2518 --diff-algorithm=*)
2519 __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
2520 return
2521 ;;
ac76fd54
JK
2522 --submodule=*)
2523 __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
2524 return
2525 ;;
90131924 2526 --*)
076c3237 2527 __gitcomp "--pretty= --format= --abbrev-commit --oneline
2ca0b197 2528 --show-signature
20bf7292
TR
2529 $__git_diff_common_options
2530 "
90131924
SP
2531 return
2532 ;;
2533 esac
5269f7f8 2534 __git_complete_revlist_file
90131924
SP
2535}
2536
2ca880fe
TR
2537_git_show_branch ()
2538{
2ca880fe
TR
2539 case "$cur" in
2540 --*)
2b1c01d2 2541 __gitcomp_builtin show-branch
2ca880fe
TR
2542 return
2543 ;;
2544 esac
2545 __git_complete_revlist
2546}
2547
7fd53fce
JH
2548_git_stash ()
2549{
d7d4ca87 2550 local save_opts='--all --keep-index --no-keep-index --quiet --patch --include-untracked'
0eb5a4f9
TG
2551 local subcommands='push list show apply clear drop pop create branch'
2552 local subcommand="$(__git_find_on_cmdline "$subcommands save")"
df70b190
TG
2553 if [ -n "$(__git_find_on_cmdline "-p")" ]; then
2554 subcommand="push"
2555 fi
7bedebca 2556 if [ -z "$subcommand" ]; then
59d5eeee
SG
2557 case "$cur" in
2558 --*)
2559 __gitcomp "$save_opts"
2560 ;;
0eb5a4f9
TG
2561 sa*)
2562 if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
2563 __gitcomp "save"
2564 fi
2565 ;;
59d5eeee
SG
2566 *)
2567 if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
2568 __gitcomp "$subcommands"
59d5eeee
SG
2569 fi
2570 ;;
2571 esac
7bedebca 2572 else
7bedebca 2573 case "$subcommand,$cur" in
3851e448
TG
2574 push,--*)
2575 __gitcomp "$save_opts --message"
2576 ;;
7bedebca 2577 save,--*)
59d5eeee 2578 __gitcomp "$save_opts"
7bedebca 2579 ;;
8513c54b 2580 apply,--*|pop,--*)
59d5eeee 2581 __gitcomp "--index --quiet"
95d43780 2582 ;;
d7d4ca87
PW
2583 drop,--*)
2584 __gitcomp "--quiet"
95d43780 2585 ;;
705f5f12
SF
2586 list,--*)
2587 __gitcomp "--name-status --oneline --patch-with-stat"
2588 ;;
d7d4ca87
PW
2589 show,--*|branch,--*)
2590 ;;
2591 branch,*)
59305aee 2592 if [ $cword -eq 3 ]; then
15b4a163 2593 __git_complete_refs
d7d4ca87 2594 else
1cd23e9e 2595 __gitcomp_nl "$(__git stash list \
d7d4ca87
PW
2596 | sed -n -e 's/:.*//p')"
2597 fi
2598 ;;
2599 show,*|apply,*|drop,*|pop,*)
1cd23e9e 2600 __gitcomp_nl "$(__git stash list \
95d43780
LM
2601 | sed -n -e 's/:.*//p')"
2602 ;;
7bedebca 2603 *)
7bedebca
SG
2604 ;;
2605 esac
3ff1320d 2606 fi
7fd53fce
JH
2607}
2608
be86f7a0
SP
2609_git_submodule ()
2610{
d773c631
SG
2611 __git_has_doubledash && return
2612
db7750cf 2613 local subcommands="add status init deinit update summary foreach sync absorbgitdirs"
65d5a1e0
CW
2614 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2615 if [ -z "$subcommand" ]; then
be86f7a0
SP
2616 case "$cur" in
2617 --*)
65d5a1e0 2618 __gitcomp "--quiet"
be86f7a0
SP
2619 ;;
2620 *)
3ff1320d 2621 __gitcomp "$subcommands"
be86f7a0
SP
2622 ;;
2623 esac
2624 return
2625 fi
65d5a1e0
CW
2626
2627 case "$subcommand,$cur" in
2628 add,--*)
2629 __gitcomp "--branch --force --name --reference --depth"
2630 ;;
2631 status,--*)
2632 __gitcomp "--cached --recursive"
2633 ;;
2634 deinit,--*)
2635 __gitcomp "--force --all"
2636 ;;
2637 update,--*)
2638 __gitcomp "
2639 --init --remote --no-fetch
2640 --recommend-shallow --no-recommend-shallow
2641 --force --rebase --merge --reference --depth --recursive --jobs
2642 "
2643 ;;
2644 summary,--*)
2645 __gitcomp "--cached --files --summary-limit"
2646 ;;
2647 foreach,--*|sync,--*)
2648 __gitcomp "--recursive"
2649 ;;
2650 *)
2651 ;;
2652 esac
be86f7a0
SP
2653}
2654
47f6ee28
SG
2655_git_svn ()
2656{
2657 local subcommands="
2658 init fetch clone rebase dcommit log find-rev
2659 set-tree commit-diff info create-ignore propget
4a5856cb 2660 proplist show-ignore show-externals branch tag blame
c18d5d82 2661 migrate mkdirs reset gc
47f6ee28 2662 "
918c03c2 2663 local subcommand="$(__git_find_on_cmdline "$subcommands")"
47f6ee28
SG
2664 if [ -z "$subcommand" ]; then
2665 __gitcomp "$subcommands"
2666 else
2667 local remote_opts="--username= --config-dir= --no-auth-cache"
2668 local fc_opts="
2669 --follow-parent --authors-file= --repack=
2670 --no-metadata --use-svm-props --use-svnsync-props
2671 --log-window-size= --no-checkout --quiet
4a5856cb 2672 --repack-flags --use-log-author --localtime
2cbad176 2673 --add-author-from
a7b10230 2674 --ignore-paths= --include-paths= $remote_opts
47f6ee28
SG
2675 "
2676 local init_opts="
2677 --template= --shared= --trunk= --tags=
2678 --branches= --stdlayout --minimize-url
2679 --no-metadata --use-svm-props --use-svnsync-props
2cbad176 2680 --rewrite-root= --prefix= $remote_opts
47f6ee28
SG
2681 "
2682 local cmt_opts="
2683 --edit --rmdir --find-copies-harder --copy-similarity=
2684 "
2685
47f6ee28
SG
2686 case "$subcommand,$cur" in
2687 fetch,--*)
2688 __gitcomp "--revision= --fetch-all $fc_opts"
2689 ;;
2690 clone,--*)
2691 __gitcomp "--revision= $fc_opts $init_opts"
2692 ;;
2693 init,--*)
2694 __gitcomp "$init_opts"
2695 ;;
2696 dcommit,--*)
2697 __gitcomp "
2698 --merge --strategy= --verbose --dry-run
4a5856cb 2699 --fetch-all --no-rebase --commit-url
7b151f49 2700 --revision --interactive $cmt_opts $fc_opts
47f6ee28
SG
2701 "
2702 ;;
2703 set-tree,--*)
2704 __gitcomp "--stdin $cmt_opts $fc_opts"
2705 ;;
2706 create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
c18d5d82 2707 show-externals,--*|mkdirs,--*)
47f6ee28
SG
2708 __gitcomp "--revision="
2709 ;;
2710 log,--*)
2711 __gitcomp "
2712 --limit= --revision= --verbose --incremental
2713 --oneline --show-commit --non-recursive
4a5856cb 2714 --authors-file= --color
47f6ee28
SG
2715 "
2716 ;;
2717 rebase,--*)
2718 __gitcomp "
2719 --merge --verbose --strategy= --local
4a5856cb 2720 --fetch-all --dry-run $fc_opts
47f6ee28
SG
2721 "
2722 ;;
2723 commit-diff,--*)
2724 __gitcomp "--message= --file= --revision= $cmt_opts"
2725 ;;
2726 info,--*)
2727 __gitcomp "--url"
2728 ;;
4a5856cb
SG
2729 branch,--*)
2730 __gitcomp "--dry-run --message --tag"
2731 ;;
2732 tag,--*)
2733 __gitcomp "--dry-run --message"
2734 ;;
2735 blame,--*)
2736 __gitcomp "--git-format"
2737 ;;
2738 migrate,--*)
2739 __gitcomp "
2740 --config-dir= --ignore-paths= --minimize
2741 --no-auth-cache --username=
2742 "
2743 ;;
c18d5d82
RZ
2744 reset,--*)
2745 __gitcomp "--revision= --parent"
2746 ;;
47f6ee28 2747 *)
47f6ee28
SG
2748 ;;
2749 esac
2750 fi
2751}
2752
88e21dc7
SP
2753_git_tag ()
2754{
2755 local i c=1 f=0
da48616f
PD
2756 while [ $c -lt $cword ]; do
2757 i="${words[c]}"
88e21dc7 2758 case "$i" in
1775e990 2759 -d|--delete|-v|--verify)
227307a6 2760 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
88e21dc7
SP
2761 return
2762 ;;
2763 -f)
2764 f=1
2765 ;;
2766 esac
6e8c755f 2767 ((c++))
88e21dc7
SP
2768 done
2769
da48616f 2770 case "$prev" in
88e21dc7 2771 -m|-F)
88e21dc7 2772 ;;
8d8163f3 2773 -*|tag)
88e21dc7 2774 if [ $f = 1 ]; then
227307a6 2775 __gitcomp_direct "$(__git_tags "" "$cur" " ")"
88e21dc7
SP
2776 fi
2777 ;;
2778 *)
15b4a163 2779 __git_complete_refs
88e21dc7
SP
2780 ;;
2781 esac
85ed2f32
RT
2782
2783 case "$cur" in
2784 --*)
80eb5197 2785 __gitcomp_builtin tag
85ed2f32
RT
2786 ;;
2787 esac
88e21dc7
SP
2788}
2789
424cce83
SG
2790_git_whatchanged ()
2791{
2792 _git_log
2793}
2794
b462c024
NTND
2795_git_worktree ()
2796{
cc73385c 2797 local subcommands="add list lock move prune remove unlock"
b462c024
NTND
2798 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2799 if [ -z "$subcommand" ]; then
2800 __gitcomp "$subcommands"
2801 else
2802 case "$subcommand,$cur" in
2803 add,--*)
fc3d4e0c 2804 __gitcomp_builtin worktree_add
b462c024
NTND
2805 ;;
2806 list,--*)
fc3d4e0c 2807 __gitcomp_builtin worktree_list
b462c024 2808 ;;
58142c09 2809 lock,--*)
fc3d4e0c 2810 __gitcomp_builtin worktree_lock
58142c09 2811 ;;
b462c024 2812 prune,--*)
fc3d4e0c 2813 __gitcomp_builtin worktree_prune
b462c024 2814 ;;
cc73385c
NTND
2815 remove,--*)
2816 __gitcomp "--force"
2817 ;;
b462c024
NTND
2818 *)
2819 ;;
2820 esac
2821 fi
2822}
2823
9f642a71
NTND
2824__git_complete_common () {
2825 local command="$1"
2826
2827 case "$cur" in
2828 --*)
2829 __gitcomp_builtin "$command"
2830 ;;
2831 esac
2832}
2833
2834__git_cmds_with_parseopt_helper=
2835__git_support_parseopt_helper () {
2836 test -n "$__git_cmds_with_parseopt_helper" ||
0089521c 2837 __git_cmds_with_parseopt_helper="$(__git --list-cmds=parseopt)"
9f642a71
NTND
2838
2839 case " $__git_cmds_with_parseopt_helper " in
2840 *" $1 "*)
2841 return 0
2842 ;;
2843 *)
2844 return 1
2845 ;;
2846 esac
2847}
2848
48e1c69a
NTND
2849__git_complete_command () {
2850 local command="$1"
2851 local completion_func="_git_${command//-/_}"
fb3a0cab
JH
2852 if ! declare -f $completion_func >/dev/null 2>/dev/null &&
2853 declare -f _completion_loader >/dev/null 2>/dev/null
2854 then
2855 _completion_loader "git-$command"
2856 fi
2857 if declare -f $completion_func >/dev/null 2>/dev/null
2858 then
48e1c69a
NTND
2859 $completion_func
2860 return 0
fb3a0cab
JH
2861 elif __git_support_parseopt_helper "$command"
2862 then
9f642a71
NTND
2863 __git_complete_common "$command"
2864 return 0
48e1c69a
NTND
2865 else
2866 return 1
2867 fi
2868}
2869
93b291e0 2870__git_main ()
690d8824 2871{
beb6ee71 2872 local i c=1 command __git_dir __git_repo_path
80ac0744 2873 local __git_C_args C_args_count=0
873537fa 2874
da48616f
PD
2875 while [ $c -lt $cword ]; do
2876 i="${words[c]}"
873537fa
SP
2877 case "$i" in
2878 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
776009d1 2879 --git-dir) ((c++)) ; __git_dir="${words[c]}" ;;
873537fa 2880 --bare) __git_dir="." ;;
1eb7e2f8 2881 --help) command="help"; break ;;
776009d1 2882 -c|--work-tree|--namespace) ((c++)) ;;
80ac0744
SG
2883 -C) __git_C_args[C_args_count++]=-C
2884 ((c++))
2885 __git_C_args[C_args_count++]="${words[c]}"
2886 ;;
911d5da6 2887 -*) ;;
873537fa
SP
2888 *) command="$i"; break ;;
2889 esac
6e8c755f 2890 ((c++))
873537fa
SP
2891 done
2892
1d17b22e 2893 if [ -z "$command" ]; then
7b329b9d
SG
2894 case "$prev" in
2895 --git-dir|-C|--work-tree)
2896 # these need a path argument, let's fall back to
2897 # Bash filename completion
2898 return
2899 ;;
2900 -c|--namespace)
2901 # we don't support completing these options' arguments
2902 return
2903 ;;
2904 esac
da48616f 2905 case "$cur" in
47e98eec 2906 --*) __gitcomp "
ce5a2c95 2907 --paginate
47e98eec
SP
2908 --no-pager
2909 --git-dir=
2910 --bare
2911 --version
2912 --exec-path
3ffcd086 2913 --exec-path=
89a56bfb 2914 --html-path
66fb37d0 2915 --man-path
69ef3c02 2916 --info-path
ce5a2c95 2917 --work-tree=
a1bea2c1 2918 --namespace=
69ef3c02 2919 --no-replace-objects
ce5a2c95 2920 --help
47e98eec
SP
2921 "
2922 ;;
3301d36b
NTND
2923 *)
2924 if test -n "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
2925 then
2926 __gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
2927 else
6532f374 2928 __gitcomp "$(git --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config)"
3301d36b
NTND
2929 fi
2930 ;;
72e5e989
SP
2931 esac
2932 return
873537fa 2933 fi
367dce2a 2934
48e1c69a 2935 __git_complete_command "$command" && return
8024ea60 2936
873537fa 2937 local expansion=$(__git_aliased_command "$command")
8024ea60 2938 if [ -n "$expansion" ]; then
880111c1 2939 words[1]=$expansion
48e1c69a 2940 __git_complete_command "$expansion"
8024ea60 2941 fi
690d8824
JH
2942}
2943
93b291e0 2944__gitk_main ()
690d8824 2945{
d773c631
SG
2946 __git_has_doubledash && return
2947
beb6ee71 2948 local __git_repo_path
fad9484f
SG
2949 __git_find_repo_path
2950
07ba53f7 2951 local merge=""
fad9484f 2952 if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
07ba53f7
RQ
2953 merge="--merge"
2954 fi
b3391775
SP
2955 case "$cur" in
2956 --*)
a393777e
TR
2957 __gitcomp "
2958 $__git_log_common_options
2959 $__git_log_gitk_options
2960 $merge
2961 "
b3391775
SP
2962 return
2963 ;;
2964 esac
ec804891 2965 __git_complete_revlist
690d8824
JH
2966}
2967
61d48c66
SG
2968if [[ -n ${ZSH_VERSION-} ]] &&
2969 # Don't define these functions when sourced from 'git-completion.zsh',
2970 # it has its own implementations.
2971 [[ -z ${GIT_SOURCING_ZSH_COMPLETION-} ]]; then
d8b45314 2972 echo "WARNING: this script is deprecated, please see git-completion.zsh" 1>&2
6b179adf 2973
9cd67bd2
FC
2974 autoload -U +X compinit && compinit
2975
d8b45314
FC
2976 __gitcomp ()
2977 {
2978 emulate -L zsh
6b179adf 2979
d8b45314
FC
2980 local cur_="${3-$cur}"
2981
2982 case "$cur_" in
2983 --*=)
2984 ;;
2985 *)
2986 local c IFS=$' \t\n'
2987 local -a array
2988 for c in ${=1}; do
2989 c="$c${4-}"
2990 case $c in
2991 --*=*|*.) ;;
2992 *) c="$c " ;;
2993 esac
5d5812f4 2994 array[${#array[@]}+1]="$c"
d8b45314
FC
2995 done
2996 compset -P '*[=:]'
2997 compadd -Q -S '' -p "${2-}" -a -- array && _ret=0
2998 ;;
2999 esac
3000 }
3001
fef56eb0
SG
3002 __gitcomp_direct ()
3003 {
3004 emulate -L zsh
3005
3006 local IFS=$'\n'
3007 compset -P '*[=:]'
3008 compadd -Q -- ${=1} && _ret=0
3009 }
3010
d8b45314
FC
3011 __gitcomp_nl ()
3012 {
3013 emulate -L zsh
3014
3015 local IFS=$'\n'
3016 compset -P '*[=:]'
3017 compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
3018 }
3019
7b003420
SG
3020 __gitcomp_file_direct ()
3021 {
3022 emulate -L zsh
3023
3024 local IFS=$'\n'
3025 compset -P '*[=:]'
7a478b36 3026 compadd -f -- ${=1} && _ret=0
7b003420
SG
3027 }
3028
fea16b47
MP
3029 __gitcomp_file ()
3030 {
3031 emulate -L zsh
3032
3033 local IFS=$'\n'
3034 compset -P '*[=:]'
7a478b36 3035 compadd -p "${2-}" -f -- ${=1} && _ret=0
fea16b47
MP
3036 }
3037
d8b45314
FC
3038 _git ()
3039 {
2bcf694b
FC
3040 local _ret=1 cur cword prev
3041 cur=${words[CURRENT]}
3042 prev=${words[CURRENT-1]}
3043 let cword=CURRENT-1
3044 emulate ksh -c __${service}_main
734b2f05 3045 let _ret && _default && _ret=0
d8b45314
FC
3046 return _ret
3047 }
3048
3049 compdef _git git gitk
3050 return
3051fi
3052
3053__git_func_wrap ()
3054{
6b179adf
FC
3055 local cur words cword prev
3056 _get_comp_words_by_ref -n =: cur words cword prev
3057 $1
3058}
3059
3060# Setup completion for certain functions defined above by setting common
3061# variables and workarounds.
3062# This is NOT a public function; use at your own risk.
3063__git_complete ()
3064{
3065 local wrapper="__git_wrap${2}"
3066 eval "$wrapper () { __git_func_wrap $2 ; }"
3067 complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
3068 || complete -o default -o nospace -F $wrapper $1
3069}
3070
b0a4b2d2
FC
3071# wrapper for backwards compatibility
3072_git ()
3073{
93b291e0 3074 __git_wrap__git_main
b0a4b2d2
FC
3075}
3076
3077# wrapper for backwards compatibility
3078_gitk ()
3079{
93b291e0 3080 __git_wrap__gitk_main
b0a4b2d2
FC
3081}
3082
93b291e0
SG
3083__git_complete git __git_main
3084__git_complete gitk __gitk_main
690d8824
JH
3085
3086# The following are necessary only for Cygwin, and only are needed
3087# when the user has tab-completed the executable name and consequently
3088# included the '.exe' suffix.
3089#
76c3eb51 3090if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
93b291e0 3091__git_complete git.exe __git_main
76c3eb51 3092fi