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