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