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