.git-completion.bash 71 KB

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