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