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