Another copy of my dotfiles. Because I don't completely trust GitHub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3548 lines
77 KiB

  1. # bash/zsh completion support for core Git.
  2. #
  3. # Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
  4. # Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
  5. # Distributed under the GNU General Public License, version 2.0.
  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'
  13. # *) git email aliases for git-send-email
  14. # *) tree paths within 'ref:path/to/file' expressions
  15. # *) file paths within current working directory and index
  16. # *) common --long-options
  17. #
  18. # To use these routines:
  19. #
  20. # 1) Copy this file to somewhere (e.g. ~/.git-completion.bash).
  21. # 2) Add the following line to your .bashrc/.zshrc:
  22. # source ~/.git-completion.bash
  23. # 3) Consider changing your PS1 to also show the current branch,
  24. # see git-prompt.sh for details.
  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 ; ... '".
  31. #
  32. # If you have a command that is not part of git, but you would still
  33. # like completion, you can use __git_complete:
  34. #
  35. # __git_complete gl git_log
  36. #
  37. # Or if it's a main command (i.e. git or gitk):
  38. #
  39. # __git_complete gk gitk
  40. #
  41. # Compatible with bash 3.2.57.
  42. #
  43. # You can set the following environment variables to influence the behavior of
  44. # the completion routines:
  45. #
  46. # GIT_COMPLETION_CHECKOUT_NO_GUESS
  47. #
  48. # When set to "1", do not include "DWIM" suggestions in git-checkout
  49. # and git-switch completion (e.g., completing "foo" when "origin/foo"
  50. # exists).
  51. #
  52. # GIT_COMPLETION_SHOW_ALL
  53. #
  54. # When set to "1" suggest all options, including options which are
  55. # typically hidden (e.g. '--allow-empty' for 'git commit').
  56. case "$COMP_WORDBREAKS" in
  57. *:*) : great ;;
  58. *) COMP_WORDBREAKS="$COMP_WORDBREAKS:"
  59. esac
  60. # Discovers the path to the git repository taking any '--git-dir=<path>' and
  61. # '-C <path>' options into account and stores it in the $__git_repo_path
  62. # variable.
  63. __git_find_repo_path ()
  64. {
  65. if [ -n "${__git_repo_path-}" ]; then
  66. # we already know where it is
  67. return
  68. fi
  69. if [ -n "${__git_C_args-}" ]; then
  70. __git_repo_path="$(git "${__git_C_args[@]}" \
  71. ${__git_dir:+--git-dir="$__git_dir"} \
  72. rev-parse --absolute-git-dir 2>/dev/null)"
  73. elif [ -n "${__git_dir-}" ]; then
  74. test -d "$__git_dir" &&
  75. __git_repo_path="$__git_dir"
  76. elif [ -n "${GIT_DIR-}" ]; then
  77. test -d "${GIT_DIR-}" &&
  78. __git_repo_path="$GIT_DIR"
  79. elif [ -d .git ]; then
  80. __git_repo_path=.git
  81. else
  82. __git_repo_path="$(git rev-parse --git-dir 2>/dev/null)"
  83. fi
  84. }
  85. # Deprecated: use __git_find_repo_path() and $__git_repo_path instead
  86. # __gitdir accepts 0 or 1 arguments (i.e., location)
  87. # returns location of .git repo
  88. __gitdir ()
  89. {
  90. if [ -z "${1-}" ]; then
  91. __git_find_repo_path || return 1
  92. echo "$__git_repo_path"
  93. elif [ -d "$1/.git" ]; then
  94. echo "$1/.git"
  95. else
  96. echo "$1"
  97. fi
  98. }
  99. # Runs git with all the options given as argument, respecting any
  100. # '--git-dir=<path>' and '-C <path>' options present on the command line
  101. __git ()
  102. {
  103. git ${__git_C_args:+"${__git_C_args[@]}"} \
  104. ${__git_dir:+--git-dir="$__git_dir"} "$@" 2>/dev/null
  105. }
  106. # Removes backslash escaping, single quotes and double quotes from a word,
  107. # stores the result in the variable $dequoted_word.
  108. # 1: The word to dequote.
  109. __git_dequote ()
  110. {
  111. local rest="$1" len ch
  112. dequoted_word=""
  113. while test -n "$rest"; do
  114. len=${#dequoted_word}
  115. dequoted_word="$dequoted_word${rest%%[\\\'\"]*}"
  116. rest="${rest:$((${#dequoted_word}-$len))}"
  117. case "${rest:0:1}" in
  118. \\)
  119. ch="${rest:1:1}"
  120. case "$ch" in
  121. $'\n')
  122. ;;
  123. *)
  124. dequoted_word="$dequoted_word$ch"
  125. ;;
  126. esac
  127. rest="${rest:2}"
  128. ;;
  129. \')
  130. rest="${rest:1}"
  131. len=${#dequoted_word}
  132. dequoted_word="$dequoted_word${rest%%\'*}"
  133. rest="${rest:$((${#dequoted_word}-$len+1))}"
  134. ;;
  135. \")
  136. rest="${rest:1}"
  137. while test -n "$rest" ; do
  138. len=${#dequoted_word}
  139. dequoted_word="$dequoted_word${rest%%[\\\"]*}"
  140. rest="${rest:$((${#dequoted_word}-$len))}"
  141. case "${rest:0:1}" in
  142. \\)
  143. ch="${rest:1:1}"
  144. case "$ch" in
  145. \"|\\|\$|\`)
  146. dequoted_word="$dequoted_word$ch"
  147. ;;
  148. $'\n')
  149. ;;
  150. *)
  151. dequoted_word="$dequoted_word\\$ch"
  152. ;;
  153. esac
  154. rest="${rest:2}"
  155. ;;
  156. \")
  157. rest="${rest:1}"
  158. break
  159. ;;
  160. esac
  161. done
  162. ;;
  163. esac
  164. done
  165. }
  166. # The following function is based on code from:
  167. #
  168. # bash_completion - programmable completion functions for bash 3.2+
  169. #
  170. # Copyright © 2006-2008, Ian Macdonald <ian@caliban.org>
  171. # © 2009-2010, Bash Completion Maintainers
  172. # <bash-completion-devel@lists.alioth.debian.org>
  173. #
  174. # This program is free software; you can redistribute it and/or modify
  175. # it under the terms of the GNU General Public License as published by
  176. # the Free Software Foundation; either version 2, or (at your option)
  177. # any later version.
  178. #
  179. # This program is distributed in the hope that it will be useful,
  180. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  181. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  182. # GNU General Public License for more details.
  183. #
  184. # You should have received a copy of the GNU General Public License
  185. # along with this program; if not, see <http://www.gnu.org/licenses/>.
  186. #
  187. # The latest version of this software can be obtained here:
  188. #
  189. # http://bash-completion.alioth.debian.org/
  190. #
  191. # RELEASE: 2.x
  192. # This function can be used to access a tokenized list of words
  193. # on the command line:
  194. #
  195. # __git_reassemble_comp_words_by_ref '=:'
  196. # if test "${words_[cword_-1]}" = -w
  197. # then
  198. # ...
  199. # fi
  200. #
  201. # The argument should be a collection of characters from the list of
  202. # word completion separators (COMP_WORDBREAKS) to treat as ordinary
  203. # characters.
  204. #
  205. # This is roughly equivalent to going back in time and setting
  206. # COMP_WORDBREAKS to exclude those characters. The intent is to
  207. # make option types like --date=<type> and <rev>:<path> easy to
  208. # recognize by treating each shell word as a single token.
  209. #
  210. # It is best not to set COMP_WORDBREAKS directly because the value is
  211. # shared with other completion scripts. By the time the completion
  212. # function gets called, COMP_WORDS has already been populated so local
  213. # changes to COMP_WORDBREAKS have no effect.
  214. #
  215. # Output: words_, cword_, cur_.
  216. __git_reassemble_comp_words_by_ref()
  217. {
  218. local exclude i j first
  219. # Which word separators to exclude?
  220. exclude="${1//[^$COMP_WORDBREAKS]}"
  221. cword_=$COMP_CWORD
  222. if [ -z "$exclude" ]; then
  223. words_=("${COMP_WORDS[@]}")
  224. return
  225. fi
  226. # List of word completion separators has shrunk;
  227. # re-assemble words to complete.
  228. for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
  229. # Append each nonempty word consisting of just
  230. # word separator characters to the current word.
  231. first=t
  232. while
  233. [ $i -gt 0 ] &&
  234. [ -n "${COMP_WORDS[$i]}" ] &&
  235. # word consists of excluded word separators
  236. [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
  237. do
  238. # Attach to the previous token,
  239. # unless the previous token is the command name.
  240. if [ $j -ge 2 ] && [ -n "$first" ]; then
  241. ((j--))
  242. fi
  243. first=
  244. words_[$j]=${words_[j]}${COMP_WORDS[i]}
  245. if [ $i = $COMP_CWORD ]; then
  246. cword_=$j
  247. fi
  248. if (($i < ${#COMP_WORDS[@]} - 1)); then
  249. ((i++))
  250. else
  251. # Done.
  252. return
  253. fi
  254. done
  255. words_[$j]=${words_[j]}${COMP_WORDS[i]}
  256. if [ $i = $COMP_CWORD ]; then
  257. cword_=$j
  258. fi
  259. done
  260. }
  261. if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
  262. _get_comp_words_by_ref ()
  263. {
  264. local exclude cur_ words_ cword_
  265. if [ "$1" = "-n" ]; then
  266. exclude=$2
  267. shift 2
  268. fi
  269. __git_reassemble_comp_words_by_ref "$exclude"
  270. cur_=${words_[cword_]}
  271. while [ $# -gt 0 ]; do
  272. case "$1" in
  273. cur)
  274. cur=$cur_
  275. ;;
  276. prev)
  277. prev=${words_[$cword_-1]}
  278. ;;
  279. words)
  280. words=("${words_[@]}")
  281. ;;
  282. cword)
  283. cword=$cword_
  284. ;;
  285. esac
  286. shift
  287. done
  288. }
  289. fi
  290. # Fills the COMPREPLY array with prefiltered words without any additional
  291. # processing.
  292. # Callers must take care of providing only words that match the current word
  293. # to be completed and adding any prefix and/or suffix (trailing space!), if
  294. # necessary.
  295. # 1: List of newline-separated matching completion words, complete with
  296. # prefix and suffix.
  297. __gitcomp_direct ()
  298. {
  299. local IFS=$'\n'
  300. COMPREPLY=($1)
  301. }
  302. # Similar to __gitcomp_direct, but appends to COMPREPLY instead.
  303. # Callers must take care of providing only words that match the current word
  304. # to be completed and adding any prefix and/or suffix (trailing space!), if
  305. # necessary.
  306. # 1: List of newline-separated matching completion words, complete with
  307. # prefix and suffix.
  308. __gitcomp_direct_append ()
  309. {
  310. local IFS=$'\n'
  311. COMPREPLY+=($1)
  312. }
  313. __gitcompappend ()
  314. {
  315. local x i=${#COMPREPLY[@]}
  316. for x in $1; do
  317. if [[ "$x" == "$3"* ]]; then
  318. COMPREPLY[i++]="$2$x$4"
  319. fi
  320. done
  321. }
  322. __gitcompadd ()
  323. {
  324. COMPREPLY=()
  325. __gitcompappend "$@"
  326. }
  327. # Generates completion reply, appending a space to possible completion words,
  328. # if necessary.
  329. # It accepts 1 to 4 arguments:
  330. # 1: List of possible completion words.
  331. # 2: A prefix to be added to each possible completion word (optional).
  332. # 3: Generate possible completion matches for this word (optional).
  333. # 4: A suffix to be appended to each possible completion word (optional).
  334. __gitcomp ()
  335. {
  336. local cur_="${3-$cur}"
  337. case "$cur_" in
  338. --*=)
  339. ;;
  340. --no-*)
  341. local c i=0 IFS=$' \t\n'
  342. for c in $1; do
  343. if [[ $c == "--" ]]; then
  344. continue
  345. fi
  346. c="$c${4-}"
  347. if [[ $c == "$cur_"* ]]; then
  348. case $c in
  349. --*=|*.) ;;
  350. *) c="$c " ;;
  351. esac
  352. COMPREPLY[i++]="${2-}$c"
  353. fi
  354. done
  355. ;;
  356. *)
  357. local c i=0 IFS=$' \t\n'
  358. for c in $1; do
  359. if [[ $c == "--" ]]; then
  360. c="--no-...${4-}"
  361. if [[ $c == "$cur_"* ]]; then
  362. COMPREPLY[i++]="${2-}$c "
  363. fi
  364. break
  365. fi
  366. c="$c${4-}"
  367. if [[ $c == "$cur_"* ]]; then
  368. case $c in
  369. *=|*.) ;;
  370. *) c="$c " ;;
  371. esac
  372. COMPREPLY[i++]="${2-}$c"
  373. fi
  374. done
  375. ;;
  376. esac
  377. }
  378. # Clear the variables caching builtins' options when (re-)sourcing
  379. # the completion script.
  380. if [[ -n ${ZSH_VERSION-} ]]; then
  381. unset ${(M)${(k)parameters[@]}:#__gitcomp_builtin_*} 2>/dev/null
  382. else
  383. unset $(compgen -v __gitcomp_builtin_)
  384. fi
  385. # This function is equivalent to
  386. #
  387. # __gitcomp "$(git xxx --git-completion-helper) ..."
  388. #
  389. # except that the output is cached. Accept 1-3 arguments:
  390. # 1: the git command to execute, this is also the cache key
  391. # 2: extra options to be added on top (e.g. negative forms)
  392. # 3: options to be excluded
  393. __gitcomp_builtin ()
  394. {
  395. # spaces must be replaced with underscore for multi-word
  396. # commands, e.g. "git remote add" becomes remote_add.
  397. local cmd="$1"
  398. local incl="${2-}"
  399. local excl="${3-}"
  400. local var=__gitcomp_builtin_"${cmd/-/_}"
  401. local options
  402. eval "options=\${$var-}"
  403. if [ -z "$options" ]; then
  404. local completion_helper
  405. if [ "$GIT_COMPLETION_SHOW_ALL" = "1" ]; then
  406. completion_helper="--git-completion-helper-all"
  407. else
  408. completion_helper="--git-completion-helper"
  409. fi
  410. # leading and trailing spaces are significant to make
  411. # option removal work correctly.
  412. options=" $incl $(__git ${cmd/_/ } $completion_helper) " || return
  413. for i in $excl; do
  414. options="${options/ $i / }"
  415. done
  416. eval "$var=\"$options\""
  417. fi
  418. __gitcomp "$options"
  419. }
  420. # Variation of __gitcomp_nl () that appends to the existing list of
  421. # completion candidates, COMPREPLY.
  422. __gitcomp_nl_append ()
  423. {
  424. local IFS=$'\n'
  425. __gitcompappend "$1" "${2-}" "${3-$cur}" "${4- }"
  426. }
  427. # Generates completion reply from newline-separated possible completion words
  428. # by appending a space to all of them.
  429. # It accepts 1 to 4 arguments:
  430. # 1: List of possible completion words, separated by a single newline.
  431. # 2: A prefix to be added to each possible completion word (optional).
  432. # 3: Generate possible completion matches for this word (optional).
  433. # 4: A suffix to be appended to each possible completion word instead of
  434. # the default space (optional). If specified but empty, nothing is
  435. # appended.
  436. __gitcomp_nl ()
  437. {
  438. COMPREPLY=()
  439. __gitcomp_nl_append "$@"
  440. }
  441. # Fills the COMPREPLY array with prefiltered paths without any additional
  442. # processing.
  443. # Callers must take care of providing only paths that match the current path
  444. # to be completed and adding any prefix path components, if necessary.
  445. # 1: List of newline-separated matching paths, complete with all prefix
  446. # path components.
  447. __gitcomp_file_direct ()
  448. {
  449. local IFS=$'\n'
  450. COMPREPLY=($1)
  451. # use a hack to enable file mode in bash < 4
  452. compopt -o filenames +o nospace 2>/dev/null ||
  453. compgen -f /non-existing-dir/ >/dev/null ||
  454. true
  455. }
  456. # Generates completion reply with compgen from newline-separated possible
  457. # completion filenames.
  458. # It accepts 1 to 3 arguments:
  459. # 1: List of possible completion filenames, separated by a single newline.
  460. # 2: A directory prefix to be added to each possible completion filename
  461. # (optional).
  462. # 3: Generate possible completion matches for this word (optional).
  463. __gitcomp_file ()
  464. {
  465. local IFS=$'\n'
  466. # XXX does not work when the directory prefix contains a tilde,
  467. # since tilde expansion is not applied.
  468. # This means that COMPREPLY will be empty and Bash default
  469. # completion will be used.
  470. __gitcompadd "$1" "${2-}" "${3-$cur}" ""
  471. # use a hack to enable file mode in bash < 4
  472. compopt -o filenames +o nospace 2>/dev/null ||
  473. compgen -f /non-existing-dir/ >/dev/null ||
  474. true
  475. }
  476. # Execute 'git ls-files', unless the --committable option is specified, in
  477. # which case it runs 'git diff-index' to find out the files that can be
  478. # committed. It return paths relative to the directory specified in the first
  479. # argument, and using the options specified in the second argument.
  480. __git_ls_files_helper ()
  481. {
  482. if [ "$2" == "--committable" ]; then
  483. __git -C "$1" -c core.quotePath=false diff-index \
  484. --name-only --relative HEAD -- "${3//\\/\\\\}*"
  485. else
  486. # NOTE: $2 is not quoted in order to support multiple options
  487. __git -C "$1" -c core.quotePath=false ls-files \
  488. --exclude-standard $2 -- "${3//\\/\\\\}*"
  489. fi
  490. }
  491. # __git_index_files accepts 1 or 2 arguments:
  492. # 1: Options to pass to ls-files (required).
  493. # 2: A directory path (optional).
  494. # If provided, only files within the specified directory are listed.
  495. # Sub directories are never recursed. Path must have a trailing
  496. # slash.
  497. # 3: List only paths matching this path component (optional).
  498. __git_index_files ()
  499. {
  500. local root="$2" match="$3"
  501. __git_ls_files_helper "$root" "$1" "${match:-?}" |
  502. awk -F / -v pfx="${2//\\/\\\\}" '{
  503. paths[$1] = 1
  504. }
  505. END {
  506. for (p in paths) {
  507. if (substr(p, 1, 1) != "\"") {
  508. # No special characters, easy!
  509. print pfx p
  510. continue
  511. }
  512. # The path is quoted.
  513. p = dequote(p)
  514. if (p == "")
  515. continue
  516. # Even when a directory name itself does not contain
  517. # any special characters, it will still be quoted if
  518. # any of its (stripped) trailing path components do.
  519. # Because of this we may have seen the same directory
  520. # both quoted and unquoted.
  521. if (p in paths)
  522. # We have seen the same directory unquoted,
  523. # skip it.
  524. continue
  525. else
  526. print pfx p
  527. }
  528. }
  529. function dequote(p, bs_idx, out, esc, esc_idx, dec) {
  530. # Skip opening double quote.
  531. p = substr(p, 2)
  532. # Interpret backslash escape sequences.
  533. while ((bs_idx = index(p, "\\")) != 0) {
  534. out = out substr(p, 1, bs_idx - 1)
  535. esc = substr(p, bs_idx + 1, 1)
  536. p = substr(p, bs_idx + 2)
  537. if ((esc_idx = index("abtvfr\"\\", esc)) != 0) {
  538. # C-style one-character escape sequence.
  539. out = out substr("\a\b\t\v\f\r\"\\",
  540. esc_idx, 1)
  541. } else if (esc == "n") {
  542. # Uh-oh, a newline character.
  543. # We cannot reliably put a pathname
  544. # containing a newline into COMPREPLY,
  545. # and the newline would create a mess.
  546. # Skip this path.
  547. return ""
  548. } else {
  549. # Must be a \nnn octal value, then.
  550. dec = esc * 64 + \
  551. substr(p, 1, 1) * 8 + \
  552. substr(p, 2, 1)
  553. out = out sprintf("%c", dec)
  554. p = substr(p, 3)
  555. }
  556. }
  557. # Drop closing double quote, if there is one.
  558. # (There is not any if this is a directory, as it was
  559. # already stripped with the trailing path components.)
  560. if (substr(p, length(p), 1) == "\"")
  561. out = out substr(p, 1, length(p) - 1)
  562. else
  563. out = out p
  564. return out
  565. }'
  566. }
  567. # __git_complete_index_file requires 1 argument:
  568. # 1: the options to pass to ls-file
  569. #
  570. # The exception is --committable, which finds the files appropriate commit.
  571. __git_complete_index_file ()
  572. {
  573. local dequoted_word pfx="" cur_
  574. __git_dequote "$cur"
  575. case "$dequoted_word" in
  576. ?*/*)
  577. pfx="${dequoted_word%/*}/"
  578. cur_="${dequoted_word##*/}"
  579. ;;
  580. *)
  581. cur_="$dequoted_word"
  582. esac
  583. __gitcomp_file_direct "$(__git_index_files "$1" "$pfx" "$cur_")"
  584. }
  585. # Lists branches from the local repository.
  586. # 1: A prefix to be added to each listed branch (optional).
  587. # 2: List only branches matching this word (optional; list all branches if
  588. # unset or empty).
  589. # 3: A suffix to be appended to each listed branch (optional).
  590. __git_heads ()
  591. {
  592. local pfx="${1-}" cur_="${2-}" sfx="${3-}"
  593. __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
  594. "refs/heads/$cur_*" "refs/heads/$cur_*/**"
  595. }
  596. # Lists branches from remote repositories.
  597. # 1: A prefix to be added to each listed branch (optional).
  598. # 2: List only branches matching this word (optional; list all branches if
  599. # unset or empty).
  600. # 3: A suffix to be appended to each listed branch (optional).
  601. __git_remote_heads ()
  602. {
  603. local pfx="${1-}" cur_="${2-}" sfx="${3-}"
  604. __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
  605. "refs/remotes/$cur_*" "refs/remotes/$cur_*/**"
  606. }
  607. # Lists tags from the local repository.
  608. # Accepts the same positional parameters as __git_heads() above.
  609. __git_tags ()
  610. {
  611. local pfx="${1-}" cur_="${2-}" sfx="${3-}"
  612. __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
  613. "refs/tags/$cur_*" "refs/tags/$cur_*/**"
  614. }
  615. # List unique branches from refs/remotes used for 'git checkout' and 'git
  616. # switch' tracking DWIMery.
  617. # 1: A prefix to be added to each listed branch (optional)
  618. # 2: List only branches matching this word (optional; list all branches if
  619. # unset or empty).
  620. # 3: A suffix to be appended to each listed branch (optional).
  621. __git_dwim_remote_heads ()
  622. {
  623. local pfx="${1-}" cur_="${2-}" sfx="${3-}"
  624. local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
  625. # employ the heuristic used by git checkout and git switch
  626. # Try to find a remote branch that cur_es the completion word
  627. # but only output if the branch name is unique
  628. __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
  629. --sort="refname:strip=3" \
  630. "refs/remotes/*/$cur_*" "refs/remotes/*/$cur_*/**" | \
  631. uniq -u
  632. }
  633. # Lists refs from the local (by default) or from a remote repository.
  634. # It accepts 0, 1 or 2 arguments:
  635. # 1: The remote to list refs from (optional; ignored, if set but empty).
  636. # Can be the name of a configured remote, a path, or a URL.
  637. # 2: In addition to local refs, list unique branches from refs/remotes/ for
  638. # 'git checkout's tracking DWIMery (optional; ignored, if set but empty).
  639. # 3: A prefix to be added to each listed ref (optional).
  640. # 4: List only refs matching this word (optional; list all refs if unset or
  641. # empty).
  642. # 5: A suffix to be appended to each listed ref (optional; ignored, if set
  643. # but empty).
  644. #
  645. # Use __git_complete_refs() instead.
  646. __git_refs ()
  647. {
  648. local i hash dir track="${2-}"
  649. local list_refs_from=path remote="${1-}"
  650. local format refs
  651. local pfx="${3-}" cur_="${4-$cur}" sfx="${5-}"
  652. local match="${4-}"
  653. local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
  654. __git_find_repo_path
  655. dir="$__git_repo_path"
  656. if [ -z "$remote" ]; then
  657. if [ -z "$dir" ]; then
  658. return
  659. fi
  660. else
  661. if __git_is_configured_remote "$remote"; then
  662. # configured remote takes precedence over a
  663. # local directory with the same name
  664. list_refs_from=remote
  665. elif [ -d "$remote/.git" ]; then
  666. dir="$remote/.git"
  667. elif [ -d "$remote" ]; then
  668. dir="$remote"
  669. else
  670. list_refs_from=url
  671. fi
  672. fi
  673. if [ "$list_refs_from" = path ]; then
  674. if [[ "$cur_" == ^* ]]; then
  675. pfx="$pfx^"
  676. fer_pfx="$fer_pfx^"
  677. cur_=${cur_#^}
  678. match=${match#^}
  679. fi
  680. case "$cur_" in
  681. refs|refs/*)
  682. format="refname"
  683. refs=("$match*" "$match*/**")
  684. track=""
  685. ;;
  686. *)
  687. for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD REBASE_HEAD; do
  688. case "$i" in
  689. $match*)
  690. if [ -e "$dir/$i" ]; then
  691. echo "$pfx$i$sfx"
  692. fi
  693. ;;
  694. esac
  695. done
  696. format="refname:strip=2"
  697. refs=("refs/tags/$match*" "refs/tags/$match*/**"
  698. "refs/heads/$match*" "refs/heads/$match*/**"
  699. "refs/remotes/$match*" "refs/remotes/$match*/**")
  700. ;;
  701. esac
  702. __git_dir="$dir" __git for-each-ref --format="$fer_pfx%($format)$sfx" \
  703. "${refs[@]}"
  704. if [ -n "$track" ]; then
  705. __git_dwim_remote_heads "$pfx" "$match" "$sfx"
  706. fi
  707. return
  708. fi
  709. case "$cur_" in
  710. refs|refs/*)
  711. __git ls-remote "$remote" "$match*" | \
  712. while read -r hash i; do
  713. case "$i" in
  714. *^{}) ;;
  715. *) echo "$pfx$i$sfx" ;;
  716. esac
  717. done
  718. ;;
  719. *)
  720. if [ "$list_refs_from" = remote ]; then
  721. case "HEAD" in
  722. $match*) echo "${pfx}HEAD$sfx" ;;
  723. esac
  724. __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
  725. "refs/remotes/$remote/$match*" \
  726. "refs/remotes/$remote/$match*/**"
  727. else
  728. local query_symref
  729. case "HEAD" in
  730. $match*) query_symref="HEAD" ;;
  731. esac
  732. __git ls-remote "$remote" $query_symref \
  733. "refs/tags/$match*" "refs/heads/$match*" \
  734. "refs/remotes/$match*" |
  735. while read -r hash i; do
  736. case "$i" in
  737. *^{}) ;;
  738. refs/*) echo "$pfx${i#refs/*/}$sfx" ;;
  739. *) echo "$pfx$i$sfx" ;; # symbolic refs
  740. esac
  741. done
  742. fi
  743. ;;
  744. esac
  745. }
  746. # Completes refs, short and long, local and remote, symbolic and pseudo.
  747. #
  748. # Usage: __git_complete_refs [<option>]...
  749. # --remote=<remote>: The remote to list refs from, can be the name of a
  750. # configured remote, a path, or a URL.
  751. # --dwim: List unique remote branches for 'git switch's tracking DWIMery.
  752. # --pfx=<prefix>: A prefix to be added to each ref.
  753. # --cur=<word>: The current ref to be completed. Defaults to the current
  754. # word to be completed.
  755. # --sfx=<suffix>: A suffix to be appended to each ref instead of the default
  756. # space.
  757. # --mode=<mode>: What set of refs to complete, one of 'refs' (the default) to
  758. # complete all refs, 'heads' to complete only branches, or
  759. # 'remote-heads' to complete only remote branches. Note that
  760. # --remote is only compatible with --mode=refs.
  761. __git_complete_refs ()
  762. {
  763. local remote= dwim= pfx= cur_="$cur" sfx=" " mode="refs"
  764. while test $# != 0; do
  765. case "$1" in
  766. --remote=*) remote="${1##--remote=}" ;;
  767. --dwim) dwim="yes" ;;
  768. # --track is an old spelling of --dwim
  769. --track) dwim="yes" ;;
  770. --pfx=*) pfx="${1##--pfx=}" ;;
  771. --cur=*) cur_="${1##--cur=}" ;;
  772. --sfx=*) sfx="${1##--sfx=}" ;;
  773. --mode=*) mode="${1##--mode=}" ;;
  774. *) return 1 ;;
  775. esac
  776. shift
  777. done
  778. # complete references based on the specified mode
  779. case "$mode" in
  780. refs)
  781. __gitcomp_direct "$(__git_refs "$remote" "" "$pfx" "$cur_" "$sfx")" ;;
  782. heads)
  783. __gitcomp_direct "$(__git_heads "$pfx" "$cur_" "$sfx")" ;;
  784. remote-heads)
  785. __gitcomp_direct "$(__git_remote_heads "$pfx" "$cur_" "$sfx")" ;;
  786. *)
  787. return 1 ;;
  788. esac
  789. # Append DWIM remote branch names if requested
  790. if [ "$dwim" = "yes" ]; then
  791. __gitcomp_direct_append "$(__git_dwim_remote_heads "$pfx" "$cur_" "$sfx")"
  792. fi
  793. }
  794. # __git_refs2 requires 1 argument (to pass to __git_refs)
  795. # Deprecated: use __git_complete_fetch_refspecs() instead.
  796. __git_refs2 ()
  797. {
  798. local i
  799. for i in $(__git_refs "$1"); do
  800. echo "$i:$i"
  801. done
  802. }
  803. # Completes refspecs for fetching from a remote repository.
  804. # 1: The remote repository.
  805. # 2: A prefix to be added to each listed refspec (optional).
  806. # 3: The ref to be completed as a refspec instead of the current word to be
  807. # completed (optional)
  808. # 4: A suffix to be appended to each listed refspec instead of the default
  809. # space (optional).
  810. __git_complete_fetch_refspecs ()
  811. {
  812. local i remote="$1" pfx="${2-}" cur_="${3-$cur}" sfx="${4- }"
  813. __gitcomp_direct "$(
  814. for i in $(__git_refs "$remote" "" "" "$cur_") ; do
  815. echo "$pfx$i:$i$sfx"
  816. done
  817. )"
  818. }
  819. # __git_refs_remotes requires 1 argument (to pass to ls-remote)
  820. __git_refs_remotes ()
  821. {
  822. local i hash
  823. __git ls-remote "$1" 'refs/heads/*' | \
  824. while read -r hash i; do
  825. echo "$i:refs/remotes/$1/${i#refs/heads/}"
  826. done
  827. }
  828. __git_remotes ()
  829. {
  830. __git_find_repo_path
  831. test -d "$__git_repo_path/remotes" && ls -1 "$__git_repo_path/remotes"
  832. __git remote
  833. }
  834. # Returns true if $1 matches the name of a configured remote, false otherwise.
  835. __git_is_configured_remote ()
  836. {
  837. local remote
  838. for remote in $(__git_remotes); do
  839. if [ "$remote" = "$1" ]; then
  840. return 0
  841. fi
  842. done
  843. return 1
  844. }
  845. __git_list_merge_strategies ()
  846. {
  847. LANG=C LC_ALL=C git merge -s help 2>&1 |
  848. sed -n -e '/[Aa]vailable strategies are: /,/^$/{
  849. s/\.$//
  850. s/.*://
  851. s/^[ ]*//
  852. s/[ ]*$//
  853. p
  854. }'
  855. }
  856. __git_merge_strategies=
  857. # 'git merge -s help' (and thus detection of the merge strategy
  858. # list) fails, unfortunately, if run outside of any git working
  859. # tree. __git_merge_strategies is set to the empty string in
  860. # that case, and the detection will be repeated the next time it
  861. # is needed.
  862. __git_compute_merge_strategies ()
  863. {
  864. test -n "$__git_merge_strategies" ||
  865. __git_merge_strategies=$(__git_list_merge_strategies)
  866. }
  867. __git_merge_strategy_options="ours theirs subtree subtree= patience
  868. histogram diff-algorithm= ignore-space-change ignore-all-space
  869. ignore-space-at-eol renormalize no-renormalize no-renames
  870. find-renames find-renames= rename-threshold="
  871. __git_complete_revlist_file ()
  872. {
  873. local dequoted_word pfx ls ref cur_="$cur"
  874. case "$cur_" in
  875. *..?*:*)
  876. return
  877. ;;
  878. ?*:*)
  879. ref="${cur_%%:*}"
  880. cur_="${cur_#*:}"
  881. __git_dequote "$cur_"
  882. case "$dequoted_word" in
  883. ?*/*)
  884. pfx="${dequoted_word%/*}"
  885. cur_="${dequoted_word##*/}"
  886. ls="$ref:$pfx"
  887. pfx="$pfx/"
  888. ;;
  889. *)
  890. cur_="$dequoted_word"
  891. ls="$ref"
  892. ;;
  893. esac
  894. case "$COMP_WORDBREAKS" in
  895. *:*) : great ;;
  896. *) pfx="$ref:$pfx" ;;
  897. esac
  898. __gitcomp_file "$(__git ls-tree "$ls" \
  899. | sed 's/^.* //
  900. s/$//')" \
  901. "$pfx" "$cur_"
  902. ;;
  903. *...*)
  904. pfx="${cur_%...*}..."
  905. cur_="${cur_#*...}"
  906. __git_complete_refs --pfx="$pfx" --cur="$cur_"
  907. ;;
  908. *..*)
  909. pfx="${cur_%..*}.."
  910. cur_="${cur_#*..}"
  911. __git_complete_refs --pfx="$pfx" --cur="$cur_"
  912. ;;
  913. *)
  914. __git_complete_refs
  915. ;;
  916. esac
  917. }
  918. __git_complete_file ()
  919. {
  920. __git_complete_revlist_file
  921. }
  922. __git_complete_revlist ()
  923. {
  924. __git_complete_revlist_file
  925. }
  926. __git_complete_remote_or_refspec ()
  927. {
  928. local cur_="$cur" cmd="${words[1]}"
  929. local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
  930. if [ "$cmd" = "remote" ]; then
  931. ((c++))
  932. fi
  933. while [ $c -lt $cword ]; do
  934. i="${words[c]}"
  935. case "$i" in
  936. --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
  937. -d|--delete) [ "$cmd" = "push" ] && lhs=0 ;;
  938. --all)
  939. case "$cmd" in
  940. push) no_complete_refspec=1 ;;
  941. fetch)
  942. return
  943. ;;
  944. *) ;;
  945. esac
  946. ;;
  947. --multiple) no_complete_refspec=1; break ;;
  948. -*) ;;
  949. *) remote="$i"; break ;;
  950. esac
  951. ((c++))
  952. done
  953. if [ -z "$remote" ]; then
  954. __gitcomp_nl "$(__git_remotes)"
  955. return
  956. fi
  957. if [ $no_complete_refspec = 1 ]; then
  958. return
  959. fi
  960. [ "$remote" = "." ] && remote=
  961. case "$cur_" in
  962. *:*)
  963. case "$COMP_WORDBREAKS" in
  964. *:*) : great ;;
  965. *) pfx="${cur_%%:*}:" ;;
  966. esac
  967. cur_="${cur_#*:}"
  968. lhs=0
  969. ;;
  970. +*)
  971. pfx="+"
  972. cur_="${cur_#+}"
  973. ;;
  974. esac
  975. case "$cmd" in
  976. fetch)
  977. if [ $lhs = 1 ]; then
  978. __git_complete_fetch_refspecs "$remote" "$pfx" "$cur_"
  979. else
  980. __git_complete_refs --pfx="$pfx" --cur="$cur_"
  981. fi
  982. ;;
  983. pull|remote)
  984. if [ $lhs = 1 ]; then
  985. __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
  986. else
  987. __git_complete_refs --pfx="$pfx" --cur="$cur_"
  988. fi
  989. ;;
  990. push)
  991. if [ $lhs = 1 ]; then
  992. __git_complete_refs --pfx="$pfx" --cur="$cur_"
  993. else
  994. __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
  995. fi
  996. ;;
  997. esac
  998. }
  999. __git_complete_strategy ()
  1000. {
  1001. __git_compute_merge_strategies
  1002. case "$prev" in
  1003. -s|--strategy)
  1004. __gitcomp "$__git_merge_strategies"
  1005. return 0
  1006. ;;
  1007. -X)
  1008. __gitcomp "$__git_merge_strategy_options"
  1009. return 0
  1010. ;;
  1011. esac
  1012. case "$cur" in
  1013. --strategy=*)
  1014. __gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
  1015. return 0
  1016. ;;
  1017. --strategy-option=*)
  1018. __gitcomp "$__git_merge_strategy_options" "" "${cur##--strategy-option=}"
  1019. return 0
  1020. ;;
  1021. esac
  1022. return 1
  1023. }
  1024. __git_all_commands=
  1025. __git_compute_all_commands ()
  1026. {
  1027. test -n "$__git_all_commands" ||
  1028. __git_all_commands=$(__git --list-cmds=main,others,alias,nohelpers)
  1029. }
  1030. # Lists all set config variables starting with the given section prefix,
  1031. # with the prefix removed.
  1032. __git_get_config_variables ()
  1033. {
  1034. local section="$1" i IFS=$'\n'
  1035. for i in $(__git config --name-only --get-regexp "^$section\..*"); do
  1036. echo "${i#$section.}"
  1037. done
  1038. }
  1039. __git_pretty_aliases ()
  1040. {
  1041. __git_get_config_variables "pretty"
  1042. }
  1043. # __git_aliased_command requires 1 argument
  1044. __git_aliased_command ()
  1045. {
  1046. local cur=$1 last list word cmdline
  1047. while [[ -n "$cur" ]]; do
  1048. if [[ "$list" == *" $cur "* ]]; then
  1049. # loop detected
  1050. return
  1051. fi
  1052. cmdline=$(__git config --get "alias.$cur")
  1053. list=" $cur $list"
  1054. last=$cur
  1055. cur=
  1056. for word in $cmdline; do
  1057. case "$word" in
  1058. \!gitk|gitk)
  1059. cur="gitk"
  1060. break
  1061. ;;
  1062. \!*) : shell command alias ;;
  1063. -*) : option ;;
  1064. *=*) : setting env ;;
  1065. git) : git itself ;;
  1066. \(\)) : skip parens of shell function definition ;;
  1067. {) : skip start of shell helper function ;;
  1068. :) : skip null command ;;
  1069. \'*) : skip opening quote after sh -c ;;
  1070. *)
  1071. cur="$word"
  1072. break
  1073. esac
  1074. done
  1075. done
  1076. cur=$last
  1077. if [[ "$cur" != "$1" ]]; then
  1078. echo "$cur"
  1079. fi
  1080. }
  1081. # Check whether one of the given words is present on the command line,
  1082. # and print the first word found.
  1083. #
  1084. # Usage: __git_find_on_cmdline [<option>]... "<wordlist>"
  1085. # --show-idx: Optionally show the index of the found word in the $words array.
  1086. __git_find_on_cmdline ()
  1087. {
  1088. local word c=1 show_idx
  1089. while test $# -gt 1; do
  1090. case "$1" in
  1091. --show-idx) show_idx=y ;;
  1092. *) return 1 ;;
  1093. esac
  1094. shift
  1095. done
  1096. local wordlist="$1"
  1097. while [ $c -lt $cword ]; do
  1098. for word in $wordlist; do
  1099. if [ "$word" = "${words[c]}" ]; then
  1100. if [ -n "${show_idx-}" ]; then
  1101. echo "$c $word"
  1102. else
  1103. echo "$word"
  1104. fi
  1105. return
  1106. fi
  1107. done
  1108. ((c++))
  1109. done
  1110. }
  1111. # Similar to __git_find_on_cmdline, except that it loops backwards and thus
  1112. # prints the *last* word found. Useful for finding which of two options that
  1113. # supersede each other came last, such as "--guess" and "--no-guess".
  1114. #
  1115. # Usage: __git_find_last_on_cmdline [<option>]... "<wordlist>"
  1116. # --show-idx: Optionally show the index of the found word in the $words array.
  1117. __git_find_last_on_cmdline ()
  1118. {
  1119. local word c=$cword show_idx
  1120. while test $# -gt 1; do
  1121. case "$1" in
  1122. --show-idx) show_idx=y ;;
  1123. *) return 1 ;;
  1124. esac
  1125. shift
  1126. done
  1127. local wordlist="$1"
  1128. while [ $c -gt 1 ]; do
  1129. ((c--))
  1130. for word in $wordlist; do
  1131. if [ "$word" = "${words[c]}" ]; then
  1132. if [ -n "$show_idx" ]; then
  1133. echo "$c $word"
  1134. else
  1135. echo "$word"
  1136. fi
  1137. return
  1138. fi
  1139. done
  1140. done
  1141. }
  1142. # Echo the value of an option set on the command line or config
  1143. #
  1144. # $1: short option name
  1145. # $2: long option name including =
  1146. # $3: list of possible values
  1147. # $4: config string (optional)
  1148. #
  1149. # example:
  1150. # result="$(__git_get_option_value "-d" "--do-something=" \
  1151. # "yes no" "core.doSomething")"
  1152. #
  1153. # result is then either empty (no option set) or "yes" or "no"
  1154. #
  1155. # __git_get_option_value requires 3 arguments
  1156. __git_get_option_value ()
  1157. {
  1158. local c short_opt long_opt val
  1159. local result= values config_key word
  1160. short_opt="$1"
  1161. long_opt="$2"
  1162. values="$3"
  1163. config_key="$4"
  1164. ((c = $cword - 1))
  1165. while [ $c -ge 0 ]; do
  1166. word="${words[c]}"
  1167. for val in $values; do
  1168. if [ "$short_opt$val" = "$word" ] ||
  1169. [ "$long_opt$val" = "$word" ]; then
  1170. result="$val"
  1171. break 2
  1172. fi
  1173. done
  1174. ((c--))
  1175. done
  1176. if [ -n "$config_key" ] && [ -z "$result" ]; then
  1177. result="$(__git config "$config_key")"
  1178. fi
  1179. echo "$result"
  1180. }
  1181. __git_has_doubledash ()
  1182. {
  1183. local c=1
  1184. while [ $c -lt $cword ]; do
  1185. if [ "--" = "${words[c]}" ]; then
  1186. return 0
  1187. fi
  1188. ((c++))
  1189. done
  1190. return 1
  1191. }
  1192. # Try to count non option arguments passed on the command line for the
  1193. # specified git command.
  1194. # When options are used, it is necessary to use the special -- option to
  1195. # tell the implementation were non option arguments begin.
  1196. # XXX this can not be improved, since options can appear everywhere, as
  1197. # an example:
  1198. # git mv x -n y
  1199. #
  1200. # __git_count_arguments requires 1 argument: the git command executed.
  1201. __git_count_arguments ()
  1202. {
  1203. local word i c=0
  1204. # Skip "git" (first argument)
  1205. for ((i=1; i < ${#words[@]}; i++)); do
  1206. word="${words[i]}"
  1207. case "$word" in
  1208. --)
  1209. # Good; we can assume that the following are only non
  1210. # option arguments.
  1211. ((c = 0))
  1212. ;;
  1213. "$1")
  1214. # Skip the specified git command and discard git
  1215. # main options
  1216. ((c = 0))
  1217. ;;
  1218. ?*)
  1219. ((c++))
  1220. ;;
  1221. esac
  1222. done
  1223. printf "%d" $c
  1224. }
  1225. __git_whitespacelist="nowarn warn error error-all fix"
  1226. __git_patchformat="mbox stgit stgit-series hg mboxrd"
  1227. __git_showcurrentpatch="diff raw"
  1228. __git_am_inprogress_options="--skip --continue --resolved --abort --quit --show-current-patch"
  1229. _git_am ()
  1230. {
  1231. __git_find_repo_path
  1232. if [ -d "$__git_repo_path"/rebase-apply ]; then
  1233. __gitcomp "$__git_am_inprogress_options"
  1234. return
  1235. fi
  1236. case "$cur" in
  1237. --whitespace=*)
  1238. __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
  1239. return
  1240. ;;
  1241. --patch-format=*)
  1242. __gitcomp "$__git_patchformat" "" "${cur##--patch-format=}"
  1243. return
  1244. ;;
  1245. --show-current-patch=*)
  1246. __gitcomp "$__git_showcurrentpatch" "" "${cur##--show-current-patch=}"
  1247. return
  1248. ;;
  1249. --*)
  1250. __gitcomp_builtin am "" \
  1251. "$__git_am_inprogress_options"
  1252. return
  1253. esac
  1254. }
  1255. _git_apply ()
  1256. {
  1257. case "$cur" in
  1258. --whitespace=*)
  1259. __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
  1260. return
  1261. ;;
  1262. --*)
  1263. __gitcomp_builtin apply
  1264. return
  1265. esac
  1266. }
  1267. _git_add ()
  1268. {
  1269. case "$cur" in
  1270. --chmod=*)
  1271. __gitcomp "+x -x" "" "${cur##--chmod=}"
  1272. return
  1273. ;;
  1274. --*)
  1275. __gitcomp_builtin add
  1276. return
  1277. esac
  1278. local complete_opt="--others --modified --directory --no-empty-directory"
  1279. if test -n "$(__git_find_on_cmdline "-u --update")"
  1280. then
  1281. complete_opt="--modified"
  1282. fi
  1283. __git_complete_index_file "$complete_opt"
  1284. }
  1285. _git_archive ()
  1286. {
  1287. case "$cur" in
  1288. --format=*)
  1289. __gitcomp "$(git archive --list)" "" "${cur##--format=}"
  1290. return
  1291. ;;
  1292. --remote=*)
  1293. __gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
  1294. return
  1295. ;;
  1296. --*)
  1297. __gitcomp_builtin archive "--format= --list --verbose --prefix= --worktree-attributes"
  1298. return
  1299. ;;
  1300. esac
  1301. __git_complete_file
  1302. }
  1303. _git_bisect ()
  1304. {
  1305. __git_has_doubledash && return
  1306. local subcommands="start bad good skip reset visualize replay log run"
  1307. local subcommand="$(__git_find_on_cmdline "$subcommands")"
  1308. if [ -z "$subcommand" ]; then
  1309. __git_find_repo_path
  1310. if [ -f "$__git_repo_path"/BISECT_START ]; then
  1311. __gitcomp "$subcommands"
  1312. else
  1313. __gitcomp "replay start"
  1314. fi
  1315. return
  1316. fi
  1317. case "$subcommand" in
  1318. bad|good|reset|skip|start)
  1319. __git_complete_refs
  1320. ;;
  1321. *)
  1322. ;;
  1323. esac
  1324. }
  1325. __git_ref_fieldlist="refname objecttype objectsize objectname upstream push HEAD symref"
  1326. _git_branch ()
  1327. {
  1328. local i c=1 only_local_ref="n" has_r="n"
  1329. while [ $c -lt $cword ]; do
  1330. i="${words[c]}"
  1331. case "$i" in
  1332. -d|-D|--delete|-m|-M|--move|-c|-C|--copy)
  1333. only_local_ref="y" ;;
  1334. -r|--remotes)
  1335. has_r="y" ;;
  1336. esac
  1337. ((c++))
  1338. done
  1339. case "$cur" in
  1340. --set-upstream-to=*)
  1341. __git_complete_refs --cur="${cur##--set-upstream-to=}"
  1342. ;;
  1343. --*)
  1344. __gitcomp_builtin branch
  1345. ;;
  1346. *)
  1347. if [ $only_local_ref = "y" -a $has_r = "n" ]; then
  1348. __gitcomp_direct "$(__git_heads "" "$cur" " ")"
  1349. else
  1350. __git_complete_refs
  1351. fi
  1352. ;;
  1353. esac
  1354. }
  1355. _git_bundle ()
  1356. {
  1357. local cmd="${words[2]}"
  1358. case "$cword" in
  1359. 2)
  1360. __gitcomp "create list-heads verify unbundle"
  1361. ;;
  1362. 3)
  1363. # looking for a file
  1364. ;;
  1365. *)
  1366. case "$cmd" in
  1367. create)
  1368. __git_complete_revlist
  1369. ;;
  1370. esac
  1371. ;;
  1372. esac
  1373. }
  1374. # Helper function to decide whether or not we should enable DWIM logic for
  1375. # git-switch and git-checkout.
  1376. #
  1377. # To decide between the following rules in decreasing priority order:
  1378. # - the last provided of "--guess" or "--no-guess" explicitly enable or
  1379. # disable completion of DWIM logic respectively.
  1380. # - If checkout.guess is false, disable completion of DWIM logic.
  1381. # - If the --no-track option is provided, take this as a hint to disable the
  1382. # DWIM completion logic
  1383. # - If GIT_COMPLETION_CHECKOUT_NO_GUESS is set, disable the DWIM completion
  1384. # logic, as requested by the user.
  1385. # - Enable DWIM logic otherwise.
  1386. #
  1387. __git_checkout_default_dwim_mode ()
  1388. {
  1389. local last_option dwim_opt="--dwim"
  1390. if [ "${GIT_COMPLETION_CHECKOUT_NO_GUESS-}" = "1" ]; then
  1391. dwim_opt=""
  1392. fi
  1393. # --no-track disables DWIM, but with lower priority than
  1394. # --guess/--no-guess/checkout.guess
  1395. if [ -n "$(__git_find_on_cmdline "--no-track")" ]; then
  1396. dwim_opt=""
  1397. fi
  1398. # checkout.guess = false disables DWIM, but with lower priority than
  1399. # --guess/--no-guess
  1400. if [ "$(__git config --type=bool checkout.guess)" = "false" ]; then
  1401. dwim_opt=""
  1402. fi
  1403. # Find the last provided --guess or --no-guess
  1404. last_option="$(__git_find_last_on_cmdline "--guess --no-guess")"
  1405. case "$last_option" in
  1406. --guess)
  1407. dwim_opt="--dwim"
  1408. ;;
  1409. --no-guess)
  1410. dwim_opt=""
  1411. ;;
  1412. esac
  1413. echo "$dwim_opt"
  1414. }
  1415. _git_checkout ()
  1416. {
  1417. __git_has_doubledash && return
  1418. local dwim_opt="$(__git_checkout_default_dwim_mode)"
  1419. case "$prev" in
  1420. -b|-B|--orphan)
  1421. # Complete local branches (and DWIM branch
  1422. # remote branch names) for an option argument
  1423. # specifying a new branch name. This is for
  1424. # convenience, assuming new branches are
  1425. # possibly based on pre-existing branch names.
  1426. __git_complete_refs $dwim_opt --mode="heads"
  1427. return
  1428. ;;
  1429. *)
  1430. ;;
  1431. esac
  1432. case "$cur" in
  1433. --conflict=*)
  1434. __gitcomp "diff3 merge" "" "${cur##--conflict=}"
  1435. ;;
  1436. --*)
  1437. __gitcomp_builtin checkout
  1438. ;;
  1439. *)
  1440. # At this point, we've already handled special completion for
  1441. # the arguments to -b/-B, and --orphan. There are 3 main
  1442. # things left we can possibly complete:
  1443. # 1) a start-point for -b/-B, -d/--detach, or --orphan
  1444. # 2) a remote head, for --track
  1445. # 3) an arbitrary reference, possibly including DWIM names
  1446. #
  1447. if [ -n "$(__git_find_on_cmdline "-b -B -d --detach --orphan")" ]; then
  1448. __git_complete_refs --mode="refs"
  1449. elif [ -n "$(__git_find_on_cmdline "--track")" ]; then
  1450. __git_complete_refs --mode="remote-heads"
  1451. else
  1452. __git_complete_refs $dwim_opt --mode="refs"
  1453. fi
  1454. ;;
  1455. esac
  1456. }
  1457. __git_sequencer_inprogress_options="--continue --quit --abort --skip"
  1458. __git_cherry_pick_inprogress_options=$__git_sequencer_inprogress_options
  1459. _git_cherry_pick ()
  1460. {
  1461. __git_find_repo_path
  1462. if [ -f "$__git_repo_path"/CHERRY_PICK_HEAD ]; then
  1463. __gitcomp "$__git_cherry_pick_inprogress_options"
  1464. return
  1465. fi
  1466. __git_complete_strategy && return
  1467. case "$cur" in
  1468. --*)
  1469. __gitcomp_builtin cherry-pick "" \
  1470. "$__git_cherry_pick_inprogress_options"
  1471. ;;
  1472. *)
  1473. __git_complete_refs
  1474. ;;
  1475. esac
  1476. }
  1477. _git_clean ()
  1478. {
  1479. case "$cur" in
  1480. --*)
  1481. __gitcomp_builtin clean
  1482. return
  1483. ;;
  1484. esac
  1485. # XXX should we check for -x option ?
  1486. __git_complete_index_file "--others --directory"
  1487. }
  1488. _git_clone ()
  1489. {
  1490. case "$prev" in
  1491. -c|--config)
  1492. __git_complete_config_variable_name_and_value
  1493. return
  1494. ;;
  1495. esac
  1496. case "$cur" in
  1497. --config=*)
  1498. __git_complete_config_variable_name_and_value \
  1499. --cur="${cur##--config=}"
  1500. return
  1501. ;;
  1502. --*)
  1503. __gitcomp_builtin clone
  1504. return
  1505. ;;
  1506. esac
  1507. }
  1508. __git_untracked_file_modes="all no normal"
  1509. _git_commit ()
  1510. {
  1511. case "$prev" in
  1512. -c|-C)
  1513. __git_complete_refs
  1514. return
  1515. ;;
  1516. esac
  1517. case "$cur" in
  1518. --cleanup=*)
  1519. __gitcomp "default scissors strip verbatim whitespace
  1520. " "" "${cur##--cleanup=}"
  1521. return
  1522. ;;
  1523. --reuse-message=*|--reedit-message=*|\
  1524. --fixup=*|--squash=*)
  1525. __git_complete_refs --cur="${cur#*=}"
  1526. return
  1527. ;;
  1528. --untracked-files=*)
  1529. __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
  1530. return
  1531. ;;
  1532. --*)
  1533. __gitcomp_builtin commit
  1534. return
  1535. esac
  1536. if __git rev-parse --verify --quiet HEAD >/dev/null; then
  1537. __git_complete_index_file "--committable"
  1538. else
  1539. # This is the first commit
  1540. __git_complete_index_file "--cached"
  1541. fi
  1542. }
  1543. _git_describe ()
  1544. {
  1545. case "$cur" in
  1546. --*)
  1547. __gitcomp_builtin describe
  1548. return
  1549. esac
  1550. __git_complete_refs
  1551. }
  1552. __git_diff_algorithms="myers minimal patience histogram"
  1553. __git_diff_submodule_formats="diff log short"
  1554. __git_color_moved_opts="no default plain blocks zebra dimmed-zebra"
  1555. __git_color_moved_ws_opts="no ignore-space-at-eol ignore-space-change
  1556. ignore-all-space allow-indentation-change"
  1557. __git_diff_common_options="--stat --numstat --shortstat --summary
  1558. --patch-with-stat --name-only --name-status --color
  1559. --no-color --color-words --no-renames --check
  1560. --color-moved --color-moved= --no-color-moved
  1561. --color-moved-ws= --no-color-moved-ws
  1562. --full-index --binary --abbrev --diff-filter=
  1563. --find-copies-harder --ignore-cr-at-eol
  1564. --text --ignore-space-at-eol --ignore-space-change
  1565. --ignore-all-space --ignore-blank-lines --exit-code
  1566. --quiet --ext-diff --no-ext-diff
  1567. --no-prefix --src-prefix= --dst-prefix=
  1568. --inter-hunk-context=
  1569. --patience --histogram --minimal
  1570. --raw --word-diff --word-diff-regex=
  1571. --dirstat --dirstat= --dirstat-by-file
  1572. --dirstat-by-file= --cumulative
  1573. --diff-algorithm=
  1574. --submodule --submodule= --ignore-submodules
  1575. --indent-heuristic --no-indent-heuristic
  1576. --textconv --no-textconv
  1577. --patch --no-patch
  1578. "
  1579. __git_diff_difftool_options="--cached --staged --pickaxe-all --pickaxe-regex
  1580. --base --ours --theirs --no-index --relative --merge-base
  1581. $__git_diff_common_options"
  1582. _git_diff ()
  1583. {
  1584. __git_has_doubledash && return
  1585. case "$cur" in
  1586. --diff-algorithm=*)
  1587. __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
  1588. return
  1589. ;;
  1590. --submodule=*)
  1591. __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
  1592. return
  1593. ;;
  1594. --color-moved=*)
  1595. __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
  1596. return
  1597. ;;
  1598. --color-moved-ws=*)
  1599. __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
  1600. return
  1601. ;;
  1602. --*)
  1603. __gitcomp "$__git_diff_difftool_options"
  1604. return
  1605. ;;
  1606. esac
  1607. __git_complete_revlist_file
  1608. }
  1609. __git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff
  1610. tkdiff vimdiff nvimdiff gvimdiff xxdiff araxis p4merge
  1611. bc codecompare smerge
  1612. "
  1613. _git_difftool ()
  1614. {
  1615. __git_has_doubledash && return
  1616. case "$cur" in
  1617. --tool=*)
  1618. __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
  1619. return
  1620. ;;
  1621. --*)
  1622. __gitcomp_builtin difftool "$__git_diff_difftool_options"
  1623. return
  1624. ;;
  1625. esac
  1626. __git_complete_revlist_file
  1627. }
  1628. __git_fetch_recurse_submodules="yes on-demand no"
  1629. _git_fetch ()
  1630. {
  1631. case "$cur" in
  1632. --recurse-submodules=*)
  1633. __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
  1634. return
  1635. ;;
  1636. --filter=*)
  1637. __gitcomp "blob:none blob:limit= sparse:oid=" "" "${cur##--filter=}"
  1638. return
  1639. ;;
  1640. --*)
  1641. __gitcomp_builtin fetch
  1642. return
  1643. ;;
  1644. esac
  1645. __git_complete_remote_or_refspec
  1646. }
  1647. __git_format_patch_extra_options="
  1648. --full-index --not --all --no-prefix --src-prefix=
  1649. --dst-prefix= --notes
  1650. "
  1651. _git_format_patch ()
  1652. {
  1653. case "$cur" in
  1654. --thread=*)
  1655. __gitcomp "
  1656. deep shallow
  1657. " "" "${cur##--thread=}"
  1658. return
  1659. ;;
  1660. --base=*|--interdiff=*|--range-diff=*)
  1661. __git_complete_refs --cur="${cur#--*=}"
  1662. return
  1663. ;;
  1664. --*)
  1665. __gitcomp_builtin format-patch "$__git_format_patch_extra_options"
  1666. return
  1667. ;;
  1668. esac
  1669. __git_complete_revlist
  1670. }
  1671. _git_fsck ()
  1672. {
  1673. case "$cur" in
  1674. --*)
  1675. __gitcomp_builtin fsck
  1676. return
  1677. ;;
  1678. esac
  1679. }
  1680. _git_gitk ()
  1681. {
  1682. __gitk_main
  1683. }
  1684. # Lists matching symbol names from a tag (as in ctags) file.
  1685. # 1: List symbol names matching this word.
  1686. # 2: The tag file to list symbol names from.
  1687. # 3: A prefix to be added to each listed symbol name (optional).
  1688. # 4: A suffix to be appended to each listed symbol name (optional).
  1689. __git_match_ctag () {
  1690. awk -v pfx="${3-}" -v sfx="${4-}" "
  1691. /^${1//\//\\/}/ { print pfx \$1 sfx }
  1692. " "$2"
  1693. }
  1694. # Complete symbol names from a tag file.
  1695. # Usage: __git_complete_symbol [<option>]...
  1696. # --tags=<file>: The tag file to list symbol names from instead of the
  1697. # default "tags".
  1698. # --pfx=<prefix>: A prefix to be added to each symbol name.
  1699. # --cur=<word>: The current symbol name to be completed. Defaults to
  1700. # the current word to be completed.
  1701. # --sfx=<suffix>: A suffix to be appended to each symbol name instead
  1702. # of the default space.
  1703. __git_complete_symbol () {
  1704. local tags=tags pfx="" cur_="${cur-}" sfx=" "
  1705. while test $# != 0; do
  1706. case "$1" in
  1707. --tags=*) tags="${1##--tags=}" ;;
  1708. --pfx=*) pfx="${1##--pfx=}" ;;
  1709. --cur=*) cur_="${1##--cur=}" ;;
  1710. --sfx=*) sfx="${1##--sfx=}" ;;
  1711. *) return 1 ;;
  1712. esac
  1713. shift
  1714. done
  1715. if test -r "$tags"; then
  1716. __gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
  1717. fi
  1718. }
  1719. _git_grep ()
  1720. {
  1721. __git_has_doubledash && return
  1722. case "$cur" in
  1723. --*)
  1724. __gitcomp_builtin grep
  1725. return
  1726. ;;
  1727. esac
  1728. case "$cword,$prev" in
  1729. 2,*|*,-*)
  1730. __git_complete_symbol && return
  1731. ;;
  1732. esac
  1733. __git_complete_refs
  1734. }
  1735. _git_help ()
  1736. {
  1737. case "$cur" in
  1738. --*)
  1739. __gitcomp_builtin help
  1740. return
  1741. ;;
  1742. esac
  1743. if test -n "$GIT_TESTING_ALL_COMMAND_LIST"
  1744. then
  1745. __gitcomp "$GIT_TESTING_ALL_COMMAND_LIST $(__git --list-cmds=alias,list-guide) gitk"
  1746. else
  1747. __gitcomp "$(__git --list-cmds=main,nohelpers,alias,list-guide) gitk"
  1748. fi
  1749. }
  1750. _git_init ()
  1751. {
  1752. case "$cur" in
  1753. --shared=*)
  1754. __gitcomp "
  1755. false true umask group all world everybody
  1756. " "" "${cur##--shared=}"
  1757. return
  1758. ;;
  1759. --*)
  1760. __gitcomp_builtin init
  1761. return
  1762. ;;
  1763. esac
  1764. }
  1765. _git_ls_files ()
  1766. {
  1767. case "$cur" in
  1768. --*)
  1769. __gitcomp_builtin ls-files
  1770. return
  1771. ;;
  1772. esac
  1773. # XXX ignore options like --modified and always suggest all cached
  1774. # files.
  1775. __git_complete_index_file "--cached"
  1776. }
  1777. _git_ls_remote ()
  1778. {
  1779. case "$cur" in
  1780. --*)
  1781. __gitcomp_builtin ls-remote
  1782. return
  1783. ;;
  1784. esac
  1785. __gitcomp_nl "$(__git_remotes)"
  1786. }
  1787. _git_ls_tree ()
  1788. {
  1789. case "$cur" in
  1790. --*)
  1791. __gitcomp_builtin ls-tree
  1792. return
  1793. ;;
  1794. esac
  1795. __git_complete_file
  1796. }
  1797. # Options that go well for log, shortlog and gitk
  1798. __git_log_common_options="
  1799. --not --all
  1800. --branches --tags --remotes
  1801. --first-parent --merges --no-merges
  1802. --max-count=
  1803. --max-age= --since= --after=
  1804. --min-age= --until= --before=
  1805. --min-parents= --max-parents=
  1806. --no-min-parents --no-max-parents
  1807. "
  1808. # Options that go well for log and gitk (not shortlog)
  1809. __git_log_gitk_options="
  1810. --dense --sparse --full-history
  1811. --simplify-merges --simplify-by-decoration
  1812. --left-right --notes --no-notes
  1813. "
  1814. # Options that go well for log and shortlog (not gitk)
  1815. __git_log_shortlog_options="
  1816. --author= --committer= --grep=
  1817. --all-match --invert-grep
  1818. "
  1819. __git_log_pretty_formats="oneline short medium full fuller reference email raw format: tformat: mboxrd"
  1820. __git_log_date_formats="relative iso8601 iso8601-strict rfc2822 short local default raw unix format:"
  1821. _git_log ()
  1822. {
  1823. __git_has_doubledash && return
  1824. __git_find_repo_path
  1825. local merge=""
  1826. if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
  1827. merge="--merge"
  1828. fi
  1829. case "$prev,$cur" in
  1830. -L,:*:*)
  1831. return # fall back to Bash filename completion
  1832. ;;
  1833. -L,:*)
  1834. __git_complete_symbol --cur="${cur#:}" --sfx=":"
  1835. return
  1836. ;;
  1837. -G,*|-S,*)
  1838. __git_complete_symbol
  1839. return
  1840. ;;
  1841. esac
  1842. case "$cur" in
  1843. --pretty=*|--format=*)
  1844. __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
  1845. " "" "${cur#*=}"
  1846. return
  1847. ;;
  1848. --date=*)
  1849. __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
  1850. return
  1851. ;;
  1852. --decorate=*)
  1853. __gitcomp "full short no" "" "${cur##--decorate=}"
  1854. return
  1855. ;;
  1856. --diff-algorithm=*)
  1857. __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
  1858. return
  1859. ;;
  1860. --submodule=*)
  1861. __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
  1862. return
  1863. ;;
  1864. --no-walk=*)
  1865. __gitcomp "sorted unsorted" "" "${cur##--no-walk=}"
  1866. return
  1867. ;;
  1868. --*)
  1869. __gitcomp "
  1870. $__git_log_common_options
  1871. $__git_log_shortlog_options
  1872. $__git_log_gitk_options
  1873. --root --topo-order --date-order --reverse
  1874. --follow --full-diff
  1875. --abbrev-commit --no-abbrev-commit --abbrev=
  1876. --relative-date --date=
  1877. --pretty= --format= --oneline
  1878. --show-signature
  1879. --cherry-mark
  1880. --cherry-pick
  1881. --graph
  1882. --decorate --decorate= --no-decorate
  1883. --walk-reflogs
  1884. --no-walk --no-walk= --do-walk
  1885. --parents --children
  1886. --expand-tabs --expand-tabs= --no-expand-tabs
  1887. $merge
  1888. $__git_diff_common_options
  1889. --pickaxe-all --pickaxe-regex
  1890. "
  1891. return
  1892. ;;
  1893. -L:*:*)
  1894. return # fall back to Bash filename completion
  1895. ;;
  1896. -L:*)
  1897. __git_complete_symbol --cur="${cur#-L:}" --sfx=":"
  1898. return
  1899. ;;
  1900. -G*)
  1901. __git_complete_symbol --pfx="-G" --cur="${cur#-G}"
  1902. return
  1903. ;;
  1904. -S*)
  1905. __git_complete_symbol --pfx="-S" --cur="${cur#-S}"
  1906. return
  1907. ;;
  1908. esac
  1909. __git_complete_revlist
  1910. }
  1911. _git_merge ()
  1912. {
  1913. __git_complete_strategy && return
  1914. case "$cur" in
  1915. --*)
  1916. __gitcomp_builtin merge
  1917. return
  1918. esac
  1919. __git_complete_refs
  1920. }
  1921. _git_mergetool ()
  1922. {
  1923. case "$cur" in
  1924. --tool=*)
  1925. __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
  1926. return
  1927. ;;
  1928. --*)
  1929. __gitcomp "--tool= --prompt --no-prompt --gui --no-gui"
  1930. return
  1931. ;;
  1932. esac
  1933. }
  1934. _git_merge_base ()
  1935. {
  1936. case "$cur" in
  1937. --*)
  1938. __gitcomp_builtin merge-base
  1939. return
  1940. ;;
  1941. esac
  1942. __git_complete_refs
  1943. }
  1944. _git_mv ()
  1945. {
  1946. case "$cur" in
  1947. --*)
  1948. __gitcomp_builtin mv
  1949. return
  1950. ;;
  1951. esac
  1952. if [ $(__git_count_arguments "mv") -gt 0 ]; then
  1953. # We need to show both cached and untracked files (including
  1954. # empty directories) since this may not be the last argument.
  1955. __git_complete_index_file "--cached --others --directory"
  1956. else
  1957. __git_complete_index_file "--cached"
  1958. fi
  1959. }
  1960. _git_notes ()
  1961. {
  1962. local subcommands='add append copy edit get-ref list merge prune remove show'
  1963. local subcommand="$(__git_find_on_cmdline "$subcommands")"
  1964. case "$subcommand,$cur" in
  1965. ,--*)
  1966. __gitcomp_builtin notes
  1967. ;;
  1968. ,*)
  1969. case "$prev" in
  1970. --ref)
  1971. __git_complete_refs
  1972. ;;
  1973. *)
  1974. __gitcomp "$subcommands --ref"
  1975. ;;
  1976. esac
  1977. ;;
  1978. *,--reuse-message=*|*,--reedit-message=*)
  1979. __git_complete_refs --cur="${cur#*=}"
  1980. ;;
  1981. *,--*)
  1982. __gitcomp_builtin notes_$subcommand
  1983. ;;
  1984. prune,*|get-ref,*)
  1985. # this command does not take a ref, do not complete it
  1986. ;;
  1987. *)
  1988. case "$prev" in
  1989. -m|-F)
  1990. ;;
  1991. *)
  1992. __git_complete_refs
  1993. ;;
  1994. esac
  1995. ;;
  1996. esac
  1997. }
  1998. _git_pull ()
  1999. {
  2000. __git_complete_strategy && return
  2001. case "$cur" in
  2002. --recurse-submodules=*)
  2003. __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
  2004. return
  2005. ;;
  2006. --*)
  2007. __gitcomp_builtin pull
  2008. return
  2009. ;;
  2010. esac
  2011. __git_complete_remote_or_refspec
  2012. }
  2013. __git_push_recurse_submodules="check on-demand only"
  2014. __git_complete_force_with_lease ()
  2015. {
  2016. local cur_=$1
  2017. case "$cur_" in
  2018. --*=)
  2019. ;;
  2020. *:*)
  2021. __git_complete_refs --cur="${cur_#*:}"
  2022. ;;
  2023. *)
  2024. __git_complete_refs --cur="$cur_"
  2025. ;;
  2026. esac
  2027. }
  2028. _git_push ()
  2029. {
  2030. case "$prev" in
  2031. --repo)
  2032. __gitcomp_nl "$(__git_remotes)"
  2033. return
  2034. ;;
  2035. --recurse-submodules)
  2036. __gitcomp "$__git_push_recurse_submodules"
  2037. return
  2038. ;;
  2039. esac
  2040. case "$cur" in
  2041. --repo=*)
  2042. __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
  2043. return
  2044. ;;
  2045. --recurse-submodules=*)
  2046. __gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
  2047. return
  2048. ;;
  2049. --force-with-lease=*)
  2050. __git_complete_force_with_lease "${cur##--force-with-lease=}"
  2051. return
  2052. ;;
  2053. --*)
  2054. __gitcomp_builtin push
  2055. return
  2056. ;;
  2057. esac
  2058. __git_complete_remote_or_refspec
  2059. }
  2060. _git_range_diff ()
  2061. {
  2062. case "$cur" in
  2063. --*)
  2064. __gitcomp "
  2065. --creation-factor= --no-dual-color
  2066. $__git_diff_common_options
  2067. "
  2068. return
  2069. ;;
  2070. esac
  2071. __git_complete_revlist
  2072. }
  2073. __git_rebase_inprogress_options="--continue --skip --abort --quit --show-current-patch"
  2074. __git_rebase_interactive_inprogress_options="$__git_rebase_inprogress_options --edit-todo"
  2075. _git_rebase ()
  2076. {
  2077. __git_find_repo_path
  2078. if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
  2079. __gitcomp "$__git_rebase_interactive_inprogress_options"
  2080. return
  2081. elif [ -d "$__git_repo_path"/rebase-apply ] || \
  2082. [ -d "$__git_repo_path"/rebase-merge ]; then
  2083. __gitcomp "$__git_rebase_inprogress_options"
  2084. return
  2085. fi
  2086. __git_complete_strategy && return
  2087. case "$cur" in
  2088. --whitespace=*)
  2089. __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
  2090. return
  2091. ;;
  2092. --onto=*)
  2093. __git_complete_refs --cur="${cur##--onto=}"
  2094. return
  2095. ;;
  2096. --*)
  2097. __gitcomp_builtin rebase "" \
  2098. "$__git_rebase_interactive_inprogress_options"
  2099. return
  2100. esac
  2101. __git_complete_refs
  2102. }
  2103. _git_reflog ()
  2104. {
  2105. local subcommands="show delete expire"
  2106. local subcommand="$(__git_find_on_cmdline "$subcommands")"
  2107. if [ -z "$subcommand" ]; then
  2108. __gitcomp "$subcommands"
  2109. else
  2110. __git_complete_refs
  2111. fi
  2112. }
  2113. __git_send_email_confirm_options="always never auto cc compose"
  2114. __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
  2115. _git_send_email ()
  2116. {
  2117. case "$prev" in
  2118. --to|--cc|--bcc|--from)
  2119. __gitcomp "$(__git send-email --dump-aliases)"
  2120. return
  2121. ;;
  2122. esac
  2123. case "$cur" in
  2124. --confirm=*)
  2125. __gitcomp "
  2126. $__git_send_email_confirm_options
  2127. " "" "${cur##--confirm=}"
  2128. return
  2129. ;;
  2130. --suppress-cc=*)
  2131. __gitcomp "
  2132. $__git_send_email_suppresscc_options
  2133. " "" "${cur##--suppress-cc=}"
  2134. return
  2135. ;;
  2136. --smtp-encryption=*)
  2137. __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
  2138. return
  2139. ;;
  2140. --thread=*)
  2141. __gitcomp "
  2142. deep shallow
  2143. " "" "${cur##--thread=}"
  2144. return
  2145. ;;
  2146. --to=*|--cc=*|--bcc=*|--from=*)
  2147. __gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
  2148. return
  2149. ;;
  2150. --*)
  2151. __gitcomp_builtin send-email "--annotate --bcc --cc --cc-cmd --chain-reply-to
  2152. --compose --confirm= --dry-run --envelope-sender
  2153. --from --identity
  2154. --in-reply-to --no-chain-reply-to --no-signed-off-by-cc
  2155. --no-suppress-from --no-thread --quiet --reply-to
  2156. --signed-off-by-cc --smtp-pass --smtp-server
  2157. --smtp-server-port --smtp-encryption= --smtp-user
  2158. --subject --suppress-cc= --suppress-from --thread --to
  2159. --validate --no-validate
  2160. $__git_format_patch_extra_options"
  2161. return
  2162. ;;
  2163. esac
  2164. __git_complete_revlist
  2165. }
  2166. _git_stage ()
  2167. {
  2168. _git_add
  2169. }
  2170. _git_status ()
  2171. {
  2172. local complete_opt
  2173. local untracked_state
  2174. case "$cur" in
  2175. --ignore-submodules=*)
  2176. __gitcomp "none untracked dirty all" "" "${cur##--ignore-submodules=}"
  2177. return
  2178. ;;
  2179. --untracked-files=*)
  2180. __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
  2181. return
  2182. ;;
  2183. --column=*)
  2184. __gitcomp "
  2185. always never auto column row plain dense nodense
  2186. " "" "${cur##--column=}"
  2187. return
  2188. ;;
  2189. --*)
  2190. __gitcomp_builtin status
  2191. return
  2192. ;;
  2193. esac
  2194. untracked_state="$(__git_get_option_value "-u" "--untracked-files=" \
  2195. "$__git_untracked_file_modes" "status.showUntrackedFiles")"
  2196. case "$untracked_state" in
  2197. no)
  2198. # --ignored option does not matter
  2199. complete_opt=
  2200. ;;
  2201. all|normal|*)
  2202. complete_opt="--cached --directory --no-empty-directory --others"
  2203. if [ -n "$(__git_find_on_cmdline "--ignored")" ]; then
  2204. complete_opt="$complete_opt --ignored --exclude=*"
  2205. fi
  2206. ;;
  2207. esac
  2208. __git_complete_index_file "$complete_opt"
  2209. }
  2210. _git_switch ()
  2211. {
  2212. local dwim_opt="$(__git_checkout_default_dwim_mode)"
  2213. case "$prev" in
  2214. -c|-C|--orphan)
  2215. # Complete local branches (and DWIM branch
  2216. # remote branch names) for an option argument
  2217. # specifying a new branch name. This is for
  2218. # convenience, assuming new branches are
  2219. # possibly based on pre-existing branch names.
  2220. __git_complete_refs $dwim_opt --mode="heads"
  2221. return
  2222. ;;
  2223. *)
  2224. ;;
  2225. esac
  2226. case "$cur" in
  2227. --conflict=*)
  2228. __gitcomp "diff3 merge" "" "${cur##--conflict=}"
  2229. ;;
  2230. --*)
  2231. __gitcomp_builtin switch
  2232. ;;
  2233. *)
  2234. # Unlike in git checkout, git switch --orphan does not take
  2235. # a start point. Thus we really have nothing to complete after
  2236. # the branch name.
  2237. if [ -n "$(__git_find_on_cmdline "--orphan")" ]; then
  2238. return
  2239. fi
  2240. # At this point, we've already handled special completion for
  2241. # -c/-C, and --orphan. There are 3 main things left to
  2242. # complete:
  2243. # 1) a start-point for -c/-C or -d/--detach
  2244. # 2) a remote head, for --track
  2245. # 3) a branch name, possibly including DWIM remote branches
  2246. if [ -n "$(__git_find_on_cmdline "-c -C -d --detach")" ]; then
  2247. __git_complete_refs --mode="refs"
  2248. elif [ -n "$(__git_find_on_cmdline "--track")" ]; then
  2249. __git_complete_refs --mode="remote-heads"
  2250. else
  2251. __git_complete_refs $dwim_opt --mode="heads"
  2252. fi
  2253. ;;
  2254. esac
  2255. }
  2256. __git_config_get_set_variables ()
  2257. {
  2258. local prevword word config_file= c=$cword
  2259. while [ $c -gt 1 ]; do
  2260. word="${words[c]}"
  2261. case "$word" in
  2262. --system|--global|--local|--file=*)
  2263. config_file="$word"
  2264. break
  2265. ;;
  2266. -f|--file)
  2267. config_file="$word $prevword"
  2268. break
  2269. ;;
  2270. esac
  2271. prevword=$word
  2272. c=$((--c))
  2273. done
  2274. __git config $config_file --name-only --list
  2275. }
  2276. __git_config_vars=
  2277. __git_compute_config_vars ()
  2278. {
  2279. test -n "$__git_config_vars" ||
  2280. __git_config_vars="$(git help --config-for-completion | sort -u)"
  2281. }
  2282. # Completes possible values of various configuration variables.
  2283. #
  2284. # Usage: __git_complete_config_variable_value [<option>]...
  2285. # --varname=<word>: The name of the configuration variable whose value is
  2286. # to be completed. Defaults to the previous word on the
  2287. # command line.
  2288. # --cur=<word>: The current value to be completed. Defaults to the current
  2289. # word to be completed.
  2290. __git_complete_config_variable_value ()
  2291. {
  2292. local varname="$prev" cur_="$cur"
  2293. while test $# != 0; do
  2294. case "$1" in
  2295. --varname=*) varname="${1##--varname=}" ;;
  2296. --cur=*) cur_="${1##--cur=}" ;;
  2297. *) return 1 ;;
  2298. esac
  2299. shift
  2300. done
  2301. if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
  2302. varname="${varname,,}"
  2303. else
  2304. varname="$(echo "$varname" |tr A-Z a-z)"
  2305. fi
  2306. case "$varname" in
  2307. branch.*.remote|branch.*.pushremote)
  2308. __gitcomp_nl "$(__git_remotes)" "" "$cur_"
  2309. return
  2310. ;;
  2311. branch.*.merge)
  2312. __git_complete_refs --cur="$cur_"
  2313. return
  2314. ;;
  2315. branch.*.rebase)
  2316. __gitcomp "false true merges preserve interactive" "" "$cur_"
  2317. return
  2318. ;;
  2319. remote.pushdefault)
  2320. __gitcomp_nl "$(__git_remotes)" "" "$cur_"
  2321. return
  2322. ;;
  2323. remote.*.fetch)
  2324. local remote="${varname#remote.}"
  2325. remote="${remote%.fetch}"
  2326. if [ -z "$cur_" ]; then
  2327. __gitcomp_nl "refs/heads/" "" "" ""
  2328. return
  2329. fi
  2330. __gitcomp_nl "$(__git_refs_remotes "$remote")" "" "$cur_"
  2331. return
  2332. ;;
  2333. remote.*.push)
  2334. local remote="${varname#remote.}"
  2335. remote="${remote%.push}"
  2336. __gitcomp_nl "$(__git for-each-ref \
  2337. --format='%(refname):%(refname)' refs/heads)" "" "$cur_"
  2338. return
  2339. ;;
  2340. pull.twohead|pull.octopus)
  2341. __git_compute_merge_strategies
  2342. __gitcomp "$__git_merge_strategies" "" "$cur_"
  2343. return
  2344. ;;
  2345. color.pager)
  2346. __gitcomp "false true" "" "$cur_"
  2347. return
  2348. ;;
  2349. color.*.*)
  2350. __gitcomp "
  2351. normal black red green yellow blue magenta cyan white
  2352. bold dim ul blink reverse
  2353. " "" "$cur_"
  2354. return
  2355. ;;
  2356. color.*)
  2357. __gitcomp "false true always never auto" "" "$cur_"
  2358. return
  2359. ;;
  2360. diff.submodule)
  2361. __gitcomp "$__git_diff_submodule_formats" "" "$cur_"
  2362. return
  2363. ;;
  2364. help.format)
  2365. __gitcomp "man info web html" "" "$cur_"
  2366. return
  2367. ;;
  2368. log.date)
  2369. __gitcomp "$__git_log_date_formats" "" "$cur_"
  2370. return
  2371. ;;
  2372. sendemail.aliasfiletype)
  2373. __gitcomp "mutt mailrc pine elm gnus" "" "$cur_"
  2374. return
  2375. ;;
  2376. sendemail.confirm)
  2377. __gitcomp "$__git_send_email_confirm_options" "" "$cur_"
  2378. return
  2379. ;;
  2380. sendemail.suppresscc)
  2381. __gitcomp "$__git_send_email_suppresscc_options" "" "$cur_"
  2382. return
  2383. ;;
  2384. sendemail.transferencoding)
  2385. __gitcomp "7bit 8bit quoted-printable base64" "" "$cur_"
  2386. return
  2387. ;;
  2388. *.*)
  2389. return
  2390. ;;
  2391. esac
  2392. }
  2393. # Completes configuration sections, subsections, variable names.
  2394. #
  2395. # Usage: __git_complete_config_variable_name [<option>]...
  2396. # --cur=<word>: The current configuration section/variable name to be
  2397. # completed. Defaults to the current word to be completed.
  2398. # --sfx=<suffix>: A suffix to be appended to each fully completed
  2399. # configuration variable name (but not to sections or
  2400. # subsections) instead of the default space.
  2401. __git_complete_config_variable_name ()
  2402. {
  2403. local cur_="$cur" sfx
  2404. while test $# != 0; do
  2405. case "$1" in
  2406. --cur=*) cur_="${1##--cur=}" ;;
  2407. --sfx=*) sfx="${1##--sfx=}" ;;
  2408. *) return 1 ;;
  2409. esac
  2410. shift
  2411. done
  2412. case "$cur_" in
  2413. branch.*.*)
  2414. local pfx="${cur_%.*}."
  2415. cur_="${cur_##*.}"
  2416. __gitcomp "remote pushRemote merge mergeOptions rebase" "$pfx" "$cur_" "$sfx"
  2417. return
  2418. ;;
  2419. branch.*)
  2420. local pfx="${cur%.*}."
  2421. cur_="${cur#*.}"
  2422. __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
  2423. __gitcomp_nl_append $'autoSetupMerge\nautoSetupRebase\n' "$pfx" "$cur_" "$sfx"
  2424. return
  2425. ;;
  2426. guitool.*.*)
  2427. local pfx="${cur_%.*}."
  2428. cur_="${cur_##*.}"
  2429. __gitcomp "
  2430. argPrompt cmd confirm needsFile noConsole noRescan
  2431. prompt revPrompt revUnmerged title
  2432. " "$pfx" "$cur_" "$sfx"
  2433. return
  2434. ;;
  2435. difftool.*.*)
  2436. local pfx="${cur_%.*}."
  2437. cur_="${cur_##*.}"
  2438. __gitcomp "cmd path" "$pfx" "$cur_" "$sfx"
  2439. return
  2440. ;;
  2441. man.*.*)
  2442. local pfx="${cur_%.*}."
  2443. cur_="${cur_##*.}"
  2444. __gitcomp "cmd path" "$pfx" "$cur_" "$sfx"
  2445. return
  2446. ;;
  2447. mergetool.*.*)
  2448. local pfx="${cur_%.*}."
  2449. cur_="${cur_##*.}"
  2450. __gitcomp "cmd path trustExitCode" "$pfx" "$cur_" "$sfx"
  2451. return
  2452. ;;
  2453. pager.*)
  2454. local pfx="${cur_%.*}."
  2455. cur_="${cur_#*.}"
  2456. __git_compute_all_commands
  2457. __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_" "$sfx"
  2458. return
  2459. ;;
  2460. remote.*.*)
  2461. local pfx="${cur_%.*}."
  2462. cur_="${cur_##*.}"
  2463. __gitcomp "
  2464. url proxy fetch push mirror skipDefaultUpdate
  2465. receivepack uploadpack tagOpt pushurl
  2466. " "$pfx" "$cur_" "$sfx"
  2467. return
  2468. ;;
  2469. remote.*)
  2470. local pfx="${cur_%.*}."
  2471. cur_="${cur_#*.}"
  2472. __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
  2473. __gitcomp_nl_append "pushDefault" "$pfx" "$cur_" "$sfx"
  2474. return
  2475. ;;
  2476. url.*.*)
  2477. local pfx="${cur_%.*}."
  2478. cur_="${cur_##*.}"
  2479. __gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_" "$sfx"
  2480. return
  2481. ;;
  2482. *.*)
  2483. __git_compute_config_vars
  2484. __gitcomp "$__git_config_vars" "" "$cur_" "$sfx"
  2485. ;;
  2486. *)
  2487. __git_compute_config_vars
  2488. __gitcomp "$(echo "$__git_config_vars" |
  2489. awk -F . '{
  2490. sections[$1] = 1
  2491. }
  2492. END {
  2493. for (s in sections)
  2494. print s "."
  2495. }
  2496. ')" "" "$cur_"
  2497. ;;
  2498. esac
  2499. }
  2500. # Completes '='-separated configuration sections/variable names and values
  2501. # for 'git -c section.name=value'.
  2502. #
  2503. # Usage: __git_complete_config_variable_name_and_value [<option>]...
  2504. # --cur=<word>: The current configuration section/variable name/value to be
  2505. # completed. Defaults to the current word to be completed.
  2506. __git_complete_config_variable_name_and_value ()
  2507. {
  2508. local cur_="$cur"
  2509. while test $# != 0; do
  2510. case "$1" in
  2511. --cur=*) cur_="${1##--cur=}" ;;
  2512. *) return 1 ;;
  2513. esac
  2514. shift
  2515. done
  2516. case "$cur_" in
  2517. *=*)
  2518. __git_complete_config_variable_value \
  2519. --varname="${cur_%%=*}" --cur="${cur_#*=}"
  2520. ;;
  2521. *)
  2522. __git_complete_config_variable_name --cur="$cur_" --sfx='='
  2523. ;;
  2524. esac
  2525. }
  2526. _git_config ()
  2527. {
  2528. case "$prev" in
  2529. --get|--get-all|--unset|--unset-all)
  2530. __gitcomp_nl "$(__git_config_get_set_variables)"
  2531. return
  2532. ;;
  2533. *.*)
  2534. __git_complete_config_variable_value
  2535. return
  2536. ;;
  2537. esac
  2538. case "$cur" in
  2539. --*)
  2540. __gitcomp_builtin config
  2541. ;;
  2542. *)
  2543. __git_complete_config_variable_name
  2544. ;;
  2545. esac
  2546. }
  2547. _git_remote ()
  2548. {
  2549. local subcommands="
  2550. add rename remove set-head set-branches
  2551. get-url set-url show prune update
  2552. "
  2553. local subcommand="$(__git_find_on_cmdline "$subcommands")"
  2554. if [ -z "$subcommand" ]; then
  2555. case "$cur" in
  2556. --*)
  2557. __gitcomp_builtin remote
  2558. ;;
  2559. *)
  2560. __gitcomp "$subcommands"
  2561. ;;
  2562. esac
  2563. return
  2564. fi
  2565. case "$subcommand,$cur" in
  2566. add,--*)
  2567. __gitcomp_builtin remote_add
  2568. ;;
  2569. add,*)
  2570. ;;
  2571. set-head,--*)
  2572. __gitcomp_builtin remote_set-head
  2573. ;;
  2574. set-branches,--*)
  2575. __gitcomp_builtin remote_set-branches
  2576. ;;
  2577. set-head,*|set-branches,*)
  2578. __git_complete_remote_or_refspec
  2579. ;;
  2580. update,--*)
  2581. __gitcomp_builtin remote_update
  2582. ;;
  2583. update,*)
  2584. __gitcomp "$(__git_remotes) $(__git_get_config_variables "remotes")"
  2585. ;;
  2586. set-url,--*)
  2587. __gitcomp_builtin remote_set-url
  2588. ;;
  2589. get-url,--*)
  2590. __gitcomp_builtin remote_get-url
  2591. ;;
  2592. prune,--*)
  2593. __gitcomp_builtin remote_prune
  2594. ;;
  2595. *)
  2596. __gitcomp_nl "$(__git_remotes)"
  2597. ;;
  2598. esac
  2599. }
  2600. _git_replace ()
  2601. {
  2602. case "$cur" in
  2603. --format=*)
  2604. __gitcomp "short medium long" "" "${cur##--format=}"
  2605. return
  2606. ;;
  2607. --*)
  2608. __gitcomp_builtin replace
  2609. return
  2610. ;;
  2611. esac
  2612. __git_complete_refs
  2613. }
  2614. _git_rerere ()
  2615. {
  2616. local subcommands="clear forget diff remaining status gc"
  2617. local subcommand="$(__git_find_on_cmdline "$subcommands")"
  2618. if test -z "$subcommand"
  2619. then
  2620. __gitcomp "$subcommands"
  2621. return
  2622. fi
  2623. }
  2624. _git_reset ()
  2625. {
  2626. __git_has_doubledash && return
  2627. case "$cur" in
  2628. --*)
  2629. __gitcomp_builtin reset
  2630. return
  2631. ;;
  2632. esac
  2633. __git_complete_refs
  2634. }
  2635. _git_restore ()
  2636. {
  2637. case "$prev" in
  2638. -s)
  2639. __git_complete_refs
  2640. return
  2641. ;;
  2642. esac
  2643. case "$cur" in
  2644. --conflict=*)
  2645. __gitcomp "diff3 merge" "" "${cur##--conflict=}"
  2646. ;;
  2647. --source=*)
  2648. __git_complete_refs --cur="${cur##--source=}"
  2649. ;;
  2650. --*)
  2651. __gitcomp_builtin restore
  2652. ;;
  2653. esac
  2654. }
  2655. __git_revert_inprogress_options=$__git_sequencer_inprogress_options
  2656. _git_revert ()
  2657. {
  2658. __git_find_repo_path
  2659. if [ -f "$__git_repo_path"/REVERT_HEAD ]; then
  2660. __gitcomp "$__git_revert_inprogress_options"
  2661. return
  2662. fi
  2663. __git_complete_strategy && return
  2664. case "$cur" in
  2665. --*)
  2666. __gitcomp_builtin revert "" \
  2667. "$__git_revert_inprogress_options"
  2668. return
  2669. ;;
  2670. esac
  2671. __git_complete_refs
  2672. }
  2673. _git_rm ()
  2674. {
  2675. case "$cur" in
  2676. --*)
  2677. __gitcomp_builtin rm
  2678. return
  2679. ;;
  2680. esac
  2681. __git_complete_index_file "--cached"
  2682. }
  2683. _git_shortlog ()
  2684. {
  2685. __git_has_doubledash && return
  2686. case "$cur" in
  2687. --*)
  2688. __gitcomp "
  2689. $__git_log_common_options
  2690. $__git_log_shortlog_options
  2691. --numbered --summary --email
  2692. "
  2693. return
  2694. ;;
  2695. esac
  2696. __git_complete_revlist
  2697. }
  2698. _git_show ()
  2699. {
  2700. __git_has_doubledash && return
  2701. case "$cur" in
  2702. --pretty=*|--format=*)
  2703. __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
  2704. " "" "${cur#*=}"
  2705. return
  2706. ;;
  2707. --diff-algorithm=*)
  2708. __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
  2709. return
  2710. ;;
  2711. --submodule=*)
  2712. __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
  2713. return
  2714. ;;
  2715. --color-moved=*)
  2716. __gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
  2717. return
  2718. ;;
  2719. --color-moved-ws=*)
  2720. __gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
  2721. return
  2722. ;;
  2723. --*)
  2724. __gitcomp "--pretty= --format= --abbrev-commit --no-abbrev-commit
  2725. --oneline --show-signature
  2726. --expand-tabs --expand-tabs= --no-expand-tabs
  2727. $__git_diff_common_options
  2728. "
  2729. return
  2730. ;;
  2731. esac
  2732. __git_complete_revlist_file
  2733. }
  2734. _git_show_branch ()
  2735. {
  2736. case "$cur" in
  2737. --*)
  2738. __gitcomp_builtin show-branch
  2739. return
  2740. ;;
  2741. esac
  2742. __git_complete_revlist
  2743. }
  2744. _git_sparse_checkout ()
  2745. {
  2746. local subcommands="list init set disable"
  2747. local subcommand="$(__git_find_on_cmdline "$subcommands")"
  2748. if [ -z "$subcommand" ]; then
  2749. __gitcomp "$subcommands"
  2750. return
  2751. fi
  2752. case "$subcommand,$cur" in
  2753. init,--*)
  2754. __gitcomp "--cone"
  2755. ;;
  2756. set,--*)
  2757. __gitcomp "--stdin"
  2758. ;;
  2759. *)
  2760. ;;
  2761. esac
  2762. }
  2763. _git_stash ()
  2764. {
  2765. local save_opts='--all --keep-index --no-keep-index --quiet --patch --include-untracked'
  2766. local subcommands='push list show apply clear drop pop create branch'
  2767. local subcommand="$(__git_find_on_cmdline "$subcommands save")"
  2768. if [ -z "$subcommand" -a -n "$(__git_find_on_cmdline "-p")" ]; then
  2769. subcommand="push"
  2770. fi
  2771. if [ -z "$subcommand" ]; then
  2772. case "$cur" in
  2773. --*)
  2774. __gitcomp "$save_opts"
  2775. ;;
  2776. sa*)
  2777. if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
  2778. __gitcomp "save"
  2779. fi
  2780. ;;
  2781. *)
  2782. if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
  2783. __gitcomp "$subcommands"
  2784. fi
  2785. ;;
  2786. esac
  2787. else
  2788. case "$subcommand,$cur" in
  2789. push,--*)
  2790. __gitcomp "$save_opts --message"
  2791. ;;
  2792. save,--*)
  2793. __gitcomp "$save_opts"
  2794. ;;
  2795. apply,--*|pop,--*)
  2796. __gitcomp "--index --quiet"
  2797. ;;
  2798. drop,--*)
  2799. __gitcomp "--quiet"
  2800. ;;
  2801. list,--*)
  2802. __gitcomp "--name-status --oneline --patch-with-stat"
  2803. ;;
  2804. show,--*)
  2805. __gitcomp "$__git_diff_common_options"
  2806. ;;
  2807. branch,--*)
  2808. ;;
  2809. branch,*)
  2810. if [ $cword -eq 3 ]; then
  2811. __git_complete_refs
  2812. else
  2813. __gitcomp_nl "$(__git stash list \
  2814. | sed -n -e 's/:.*//p')"
  2815. fi
  2816. ;;
  2817. show,*|apply,*|drop,*|pop,*)
  2818. __gitcomp_nl "$(__git stash list \
  2819. | sed -n -e 's/:.*//p')"
  2820. ;;
  2821. *)
  2822. ;;
  2823. esac
  2824. fi
  2825. }
  2826. _git_submodule ()
  2827. {
  2828. __git_has_doubledash && return
  2829. local subcommands="add status init deinit update set-branch set-url summary foreach sync absorbgitdirs"
  2830. local subcommand="$(__git_find_on_cmdline "$subcommands")"
  2831. if [ -z "$subcommand" ]; then
  2832. case "$cur" in
  2833. --*)
  2834. __gitcomp "--quiet"
  2835. ;;
  2836. *)
  2837. __gitcomp "$subcommands"
  2838. ;;
  2839. esac
  2840. return
  2841. fi
  2842. case "$subcommand,$cur" in
  2843. add,--*)
  2844. __gitcomp "--branch --force --name --reference --depth"
  2845. ;;
  2846. status,--*)
  2847. __gitcomp "--cached --recursive"
  2848. ;;
  2849. deinit,--*)
  2850. __gitcomp "--force --all"
  2851. ;;
  2852. update,--*)
  2853. __gitcomp "
  2854. --init --remote --no-fetch
  2855. --recommend-shallow --no-recommend-shallow
  2856. --force --rebase --merge --reference --depth --recursive --jobs
  2857. "
  2858. ;;
  2859. set-branch,--*)
  2860. __gitcomp "--default --branch"
  2861. ;;
  2862. summary,--*)
  2863. __gitcomp "--cached --files --summary-limit"
  2864. ;;
  2865. foreach,--*|sync,--*)
  2866. __gitcomp "--recursive"
  2867. ;;
  2868. *)
  2869. ;;
  2870. esac
  2871. }
  2872. _git_svn ()
  2873. {
  2874. local subcommands="
  2875. init fetch clone rebase dcommit log find-rev
  2876. set-tree commit-diff info create-ignore propget
  2877. proplist show-ignore show-externals branch tag blame
  2878. migrate mkdirs reset gc
  2879. "
  2880. local subcommand="$(__git_find_on_cmdline "$subcommands")"
  2881. if [ -z "$subcommand" ]; then
  2882. __gitcomp "$subcommands"
  2883. else
  2884. local remote_opts="--username= --config-dir= --no-auth-cache"
  2885. local fc_opts="
  2886. --follow-parent --authors-file= --repack=
  2887. --no-metadata --use-svm-props --use-svnsync-props
  2888. --log-window-size= --no-checkout --quiet
  2889. --repack-flags --use-log-author --localtime
  2890. --add-author-from
  2891. --recursive
  2892. --ignore-paths= --include-paths= $remote_opts
  2893. "
  2894. local init_opts="
  2895. --template= --shared= --trunk= --tags=
  2896. --branches= --stdlayout --minimize-url
  2897. --no-metadata --use-svm-props --use-svnsync-props
  2898. --rewrite-root= --prefix= $remote_opts
  2899. "
  2900. local cmt_opts="
  2901. --edit --rmdir --find-copies-harder --copy-similarity=
  2902. "
  2903. case "$subcommand,$cur" in
  2904. fetch,--*)
  2905. __gitcomp "--revision= --fetch-all $fc_opts"
  2906. ;;
  2907. clone,--*)
  2908. __gitcomp "--revision= $fc_opts $init_opts"
  2909. ;;
  2910. init,--*)
  2911. __gitcomp "$init_opts"
  2912. ;;
  2913. dcommit,--*)
  2914. __gitcomp "
  2915. --merge --strategy= --verbose --dry-run
  2916. --fetch-all --no-rebase --commit-url
  2917. --revision --interactive $cmt_opts $fc_opts
  2918. "
  2919. ;;
  2920. set-tree,--*)
  2921. __gitcomp "--stdin $cmt_opts $fc_opts"
  2922. ;;
  2923. create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
  2924. show-externals,--*|mkdirs,--*)
  2925. __gitcomp "--revision="
  2926. ;;
  2927. log,--*)
  2928. __gitcomp "
  2929. --limit= --revision= --verbose --incremental
  2930. --oneline --show-commit --non-recursive
  2931. --authors-file= --color
  2932. "
  2933. ;;
  2934. rebase,--*)
  2935. __gitcomp "
  2936. --merge --verbose --strategy= --local
  2937. --fetch-all --dry-run $fc_opts
  2938. "
  2939. ;;
  2940. commit-diff,--*)
  2941. __gitcomp "--message= --file= --revision= $cmt_opts"
  2942. ;;
  2943. info,--*)
  2944. __gitcomp "--url"
  2945. ;;
  2946. branch,--*)
  2947. __gitcomp "--dry-run --message --tag"
  2948. ;;
  2949. tag,--*)
  2950. __gitcomp "--dry-run --message"
  2951. ;;
  2952. blame,--*)
  2953. __gitcomp "--git-format"
  2954. ;;
  2955. migrate,--*)
  2956. __gitcomp "
  2957. --config-dir= --ignore-paths= --minimize
  2958. --no-auth-cache --username=
  2959. "
  2960. ;;
  2961. reset,--*)
  2962. __gitcomp "--revision= --parent"
  2963. ;;
  2964. *)
  2965. ;;
  2966. esac
  2967. fi
  2968. }
  2969. _git_tag ()
  2970. {
  2971. local i c=1 f=0
  2972. while [ $c -lt $cword ]; do
  2973. i="${words[c]}"
  2974. case "$i" in
  2975. -d|--delete|-v|--verify)
  2976. __gitcomp_direct "$(__git_tags "" "$cur" " ")"
  2977. return
  2978. ;;
  2979. -f)
  2980. f=1
  2981. ;;
  2982. esac
  2983. ((c++))
  2984. done
  2985. case "$prev" in
  2986. -m|-F)
  2987. ;;
  2988. -*|tag)
  2989. if [ $f = 1 ]; then
  2990. __gitcomp_direct "$(__git_tags "" "$cur" " ")"
  2991. fi
  2992. ;;
  2993. *)
  2994. __git_complete_refs
  2995. ;;
  2996. esac
  2997. case "$cur" in
  2998. --*)
  2999. __gitcomp_builtin tag
  3000. ;;
  3001. esac
  3002. }
  3003. _git_whatchanged ()
  3004. {
  3005. _git_log
  3006. }
  3007. __git_complete_worktree_paths ()
  3008. {
  3009. local IFS=$'\n'
  3010. __gitcomp_nl "$(git worktree list --porcelain |
  3011. # Skip the first entry: it's the path of the main worktree,
  3012. # which can't be moved, removed, locked, etc.
  3013. sed -n -e '2,$ s/^worktree //p')"
  3014. }
  3015. _git_worktree ()
  3016. {
  3017. local subcommands="add list lock move prune remove unlock"
  3018. local subcommand subcommand_idx
  3019. subcommand="$(__git_find_on_cmdline --show-idx "$subcommands")"
  3020. subcommand_idx="${subcommand% *}"
  3021. subcommand="${subcommand#* }"
  3022. case "$subcommand,$cur" in
  3023. ,*)
  3024. __gitcomp "$subcommands"
  3025. ;;
  3026. *,--*)
  3027. __gitcomp_builtin worktree_$subcommand
  3028. ;;
  3029. add,*) # usage: git worktree add [<options>] <path> [<commit-ish>]
  3030. # Here we are not completing an --option, it's either the
  3031. # path or a ref.
  3032. case "$prev" in
  3033. -b|-B) # Complete refs for branch to be created/reseted.
  3034. __git_complete_refs
  3035. ;;
  3036. -*) # The previous word is an -o|--option without an
  3037. # unstuck argument: have to complete the path for
  3038. # the new worktree, so don't list anything, but let
  3039. # Bash fall back to filename completion.
  3040. ;;
  3041. *) # The previous word is not an --option, so it must
  3042. # be either the 'add' subcommand, the unstuck
  3043. # argument of an option (e.g. branch for -b|-B), or
  3044. # the path for the new worktree.
  3045. if [ $cword -eq $((subcommand_idx+1)) ]; then
  3046. # Right after the 'add' subcommand: have to
  3047. # complete the path, so fall back to Bash
  3048. # filename completion.
  3049. :
  3050. else
  3051. case "${words[cword-2]}" in
  3052. -b|-B) # After '-b <branch>': have to
  3053. # complete the path, so fall back
  3054. # to Bash filename completion.
  3055. ;;
  3056. *) # After the path: have to complete
  3057. # the ref to be checked out.
  3058. __git_complete_refs
  3059. ;;
  3060. esac
  3061. fi
  3062. ;;
  3063. esac
  3064. ;;
  3065. lock,*|remove,*|unlock,*)
  3066. __git_complete_worktree_paths
  3067. ;;
  3068. move,*)
  3069. if [ $cword -eq $((subcommand_idx+1)) ]; then
  3070. # The first parameter must be an existing working
  3071. # tree to be moved.
  3072. __git_complete_worktree_paths
  3073. else
  3074. # The second parameter is the destination: it could
  3075. # be any path, so don't list anything, but let Bash
  3076. # fall back to filename completion.
  3077. :
  3078. fi
  3079. ;;
  3080. esac
  3081. }
  3082. __git_complete_common () {
  3083. local command="$1"
  3084. case "$cur" in
  3085. --*)
  3086. __gitcomp_builtin "$command"
  3087. ;;
  3088. esac
  3089. }
  3090. __git_cmds_with_parseopt_helper=
  3091. __git_support_parseopt_helper () {
  3092. test -n "$__git_cmds_with_parseopt_helper" ||
  3093. __git_cmds_with_parseopt_helper="$(__git --list-cmds=parseopt)"
  3094. case " $__git_cmds_with_parseopt_helper " in
  3095. *" $1 "*)
  3096. return 0
  3097. ;;
  3098. *)
  3099. return 1
  3100. ;;
  3101. esac
  3102. }
  3103. __git_have_func () {
  3104. declare -f -- "$1" >/dev/null 2>&1
  3105. }
  3106. __git_complete_command () {
  3107. local command="$1"
  3108. local completion_func="_git_${command//-/_}"
  3109. if ! __git_have_func $completion_func &&
  3110. __git_have_func _completion_loader
  3111. then
  3112. _completion_loader "git-$command"
  3113. fi
  3114. if __git_have_func $completion_func
  3115. then
  3116. $completion_func
  3117. return 0
  3118. elif __git_support_parseopt_helper "$command"
  3119. then
  3120. __git_complete_common "$command"
  3121. return 0
  3122. else
  3123. return 1
  3124. fi
  3125. }
  3126. __git_main ()
  3127. {
  3128. local i c=1 command __git_dir __git_repo_path
  3129. local __git_C_args C_args_count=0
  3130. while [ $c -lt $cword ]; do
  3131. i="${words[c]}"
  3132. case "$i" in
  3133. --git-dir=*) __git_dir="${i#--git-dir=}" ;;
  3134. --git-dir) ((c++)) ; __git_dir="${words[c]}" ;;
  3135. --bare) __git_dir="." ;;
  3136. --help) command="help"; break ;;
  3137. -c|--work-tree|--namespace) ((c++)) ;;
  3138. -C) __git_C_args[C_args_count++]=-C
  3139. ((c++))
  3140. __git_C_args[C_args_count++]="${words[c]}"
  3141. ;;
  3142. -*) ;;
  3143. *) command="$i"; break ;;
  3144. esac
  3145. ((c++))
  3146. done
  3147. if [ -z "${command-}" ]; then
  3148. case "$prev" in
  3149. --git-dir|-C|--work-tree)
  3150. # these need a path argument, let's fall back to
  3151. # Bash filename completion
  3152. return
  3153. ;;
  3154. -c)
  3155. __git_complete_config_variable_name_and_value
  3156. return
  3157. ;;
  3158. --namespace)
  3159. # we don't support completing these options' arguments
  3160. return
  3161. ;;
  3162. esac
  3163. case "$cur" in
  3164. --*) __gitcomp "
  3165. --paginate
  3166. --no-pager
  3167. --git-dir=
  3168. --bare
  3169. --version
  3170. --exec-path
  3171. --exec-path=
  3172. --html-path
  3173. --man-path
  3174. --info-path
  3175. --work-tree=
  3176. --namespace=
  3177. --no-replace-objects
  3178. --help
  3179. "
  3180. ;;
  3181. *)
  3182. if test -n "${GIT_TESTING_PORCELAIN_COMMAND_LIST-}"
  3183. then
  3184. __gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
  3185. else
  3186. __gitcomp "$(__git --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config)"
  3187. fi
  3188. ;;
  3189. esac
  3190. return
  3191. fi
  3192. __git_complete_command "$command" && return
  3193. local expansion=$(__git_aliased_command "$command")
  3194. if [ -n "$expansion" ]; then
  3195. words[1]=$expansion
  3196. __git_complete_command "$expansion"
  3197. fi
  3198. }
  3199. __gitk_main ()
  3200. {
  3201. __git_has_doubledash && return
  3202. local __git_repo_path
  3203. __git_find_repo_path
  3204. local merge=""
  3205. if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
  3206. merge="--merge"
  3207. fi
  3208. case "$cur" in
  3209. --*)
  3210. __gitcomp "
  3211. $__git_log_common_options
  3212. $__git_log_gitk_options
  3213. $merge
  3214. "
  3215. return
  3216. ;;
  3217. esac
  3218. __git_complete_revlist
  3219. }
  3220. if [[ -n ${ZSH_VERSION-} && -z ${GIT_SOURCING_ZSH_COMPLETION-} ]]; then
  3221. echo "ERROR: this script is obsolete, please see git-completion.zsh" 1>&2
  3222. return
  3223. fi
  3224. __git_func_wrap ()
  3225. {
  3226. local cur words cword prev
  3227. _get_comp_words_by_ref -n =: cur words cword prev
  3228. $1
  3229. }
  3230. ___git_complete ()
  3231. {
  3232. local wrapper="__git_wrap${2}"
  3233. eval "$wrapper () { __git_func_wrap $2 ; }"
  3234. complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
  3235. || complete -o default -o nospace -F $wrapper $1
  3236. }
  3237. # Setup the completion for git commands
  3238. # 1: command or alias
  3239. # 2: function to call (e.g. `git`, `gitk`, `git_fetch`)
  3240. __git_complete ()
  3241. {
  3242. local func
  3243. if __git_have_func $2; then
  3244. func=$2
  3245. elif __git_have_func __$2_main; then
  3246. func=__$2_main
  3247. elif __git_have_func _$2; then
  3248. func=_$2
  3249. else
  3250. echo "ERROR: could not find function '$2'" 1>&2
  3251. return 1
  3252. fi
  3253. ___git_complete $1 $func
  3254. }
  3255. ___git_complete git __git_main
  3256. ___git_complete gitk __gitk_main
  3257. # The following are necessary only for Cygwin, and only are needed
  3258. # when the user has tab-completed the executable name and consequently
  3259. # included the '.exe' suffix.
  3260. #
  3261. if [ "$OSTYPE" = cygwin ]; then
  3262. ___git_complete git.exe __git_main
  3263. fi