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.

3516 lines
76 KiB

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