git-completion.bash 80 KB

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