_borg 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  1. #compdef borg borgfs -P -value-,BORG_*,-default-
  2. # Zsh completion for Borg Backup 1.1.15.
  3. #
  4. # Recommended _borg specific settings:
  5. #
  6. # zstyle -e ':completion:*:*:borg-*:argument-rest:*' tag-order \
  7. # '[[ $words[CURRENT] == -* ]] && reply=( "! archives archive-files" "-" )'
  8. # zstyle ':completion:*:*:(borg|-value-,BORG_)*' sort false
  9. # zstyle ':completion:*:*:borg-config:argument-2:keys' list-grouped false
  10. # zstyle ':completion:*:*:borg-*:*' gain-privileges true
  11. # zstyle ':completion:*' fake-parameters 'BORG_REPO:scalar'
  12. #
  13. # Custom styles:
  14. #
  15. # archive-description-format
  16. # Default: `{archive:<36} {time} [{id}]`.
  17. # archive-sort
  18. # In which order archive names should be listed.
  19. # Possible values are: `inverse`, `timestamp`, `name`, `id`.
  20. # file-description-format
  21. # Default: `{mode} {user:6} {group:6} {size:8d} {mtime} {path}{extra}`.
  22. # path-style-selector
  23. # Style selector used to select a path (when Borg would use either `pp` or `pf`).
  24. # Default: `fm`.
  25. # repository-suffix
  26. # This boolean style controls whether to add the `::` auto-removable suffix to a repository.
  27. # Default: `true`.
  28. (( $+functions[_borg_commands] )) ||
  29. _borg_commands() {
  30. local -a commands_=(
  31. 'benchmark:benchmark command'
  32. 'break-lock:break repository and cache locks'
  33. 'change-passphrase:change repository passphrase'
  34. 'check:verify repository'
  35. 'config:get and set configuration values'
  36. 'create:create backup'
  37. 'debug:debugging command (not intended for normal use)'
  38. 'delete:delete archive'
  39. 'diff:find differences in archive contents'
  40. 'export-tar:create tarball from archive'
  41. 'extract:extract archive contents'
  42. 'help:extra help'
  43. 'info:show repository or archive information'
  44. 'init:initialize empty repository'
  45. 'key:manage repository key'
  46. 'list:list archive or repository contents'
  47. 'mount:mount repository'
  48. 'prune:prune archives'
  49. 'recreate:re-create archives'
  50. 'rename:rename archive'
  51. 'serve:start repository server process'
  52. 'umount:umount repository'
  53. 'upgrade:upgrade repository format'
  54. 'with-lock:run user command with lock held'
  55. )
  56. _describe -t commands 'borg commands' commands_
  57. }
  58. (( $+functions[_borg-benchmark] )) ||
  59. _borg-benchmark() {
  60. local -a common_options
  61. __borg_setup_common_options
  62. _arguments -s -w -S : \
  63. $common_options \
  64. ':type:(crud)' \
  65. ': :_borg_repository' \
  66. ':PATH:_files'
  67. }
  68. (( $+functions[_borg-break-lock] )) ||
  69. _borg-break-lock() {
  70. local -a common_options
  71. __borg_setup_common_options
  72. _arguments -s -w -S : \
  73. $common_options \
  74. ':: :_borg_repository'
  75. }
  76. (( $+functions[_borg-change-passphrase] )) ||
  77. _borg-change-passphrase() {
  78. local -a common_options
  79. __borg_setup_common_options
  80. _arguments -s -w -S : \
  81. $common_options \
  82. ':: :_borg_repository'
  83. }
  84. (( $+functions[_borg-check] )) ||
  85. _borg-check() {
  86. local -a common_options common_archive_filters_options
  87. __borg_setup_common_options
  88. __borg_setup_common_archive_filters_options
  89. _arguments -s -w -S : \
  90. '--repository-only[only perform repository checks]' \
  91. '--archives-only[only perform archives checks]' \
  92. '(--repository-only)--verify-data[perform cryptographic archive data integrity verification]' \
  93. '--repair[attempt to repair any inconsistencies found]' \
  94. '--save-space[work slower, but using less space]' \
  95. $common_archive_filters_options \
  96. $common_options \
  97. '::REPOSITORY_OR_ARCHIVE: _borg_repository_or_archive'
  98. }
  99. (( $+functions[_borg-config] )) ||
  100. _borg-config() {
  101. local -a common_options
  102. __borg_setup_common_options
  103. _arguments -s -w -S : \
  104. '(-c --cache)'{-c,--cache}'[get and set values from the repo cache]' \
  105. '(-d --delete)'{-d,--delete}'[delete the key from the config]' \
  106. '(-l --list)'{-l,--list}'[list the configuration of the repo]' \
  107. $common_options \
  108. ': :_borg_repository' \
  109. ': : _borg_config $line[1]' \
  110. '::VALUE'
  111. }
  112. (( $+functions[_borg-create] )) ||
  113. _borg-create() {
  114. local -a common_options common_create_options
  115. __borg_setup_common_options
  116. __borg_setup_common_create_options
  117. _arguments -s -w -S : \
  118. '*'{-e,--exclude}'=[exclude paths matching PATTERN]: : _borg_style_selector_or_archive_files -f -e "$line[1]" fm "${(@)line[2,-1]}"' \
  119. '*--pattern=[experimental: include/exclude paths matching PATTERN]: : _borg_style_selector_or_archive_files -p -f -e "$line[1]" sh "${(@)line[2,-1]}"' \
  120. $common_create_options \
  121. '(-s --stats)--json[Output stats as JSON. Implies --stats.]' \
  122. '--no-cache-sync[experimental: do not synchronize the cache. Implies not using the files cache.]' \
  123. '--no-files-cache[do not load/update the file metadata cache used to detect unchanged files]' \
  124. '--stdin-name=[use NAME in archive for stdin data (default: "stdin")]:NAME' \
  125. '--stdin-user=[set user USER in archive for stdin data (default: root)]:USER:_users' \
  126. '--stdin-group=[set group GROUP in archive for stdin data (default: root)]:GROUP:_groups' \
  127. '--stdin-mode=[set mode to M in archive for stdin data (default: 0660)]: : _borg_guard_numeric_mode "M"' \
  128. '--exclude-nodump[exclude files flagged NODUMP]' \
  129. '(-x --one-file-system)'{-x,--one-file-system}'[stay in the same file system]' \
  130. '--numeric-owner[only store numeric user and group identifiers]' \
  131. '--noatime[do not store atime into archive]' \
  132. '--noctime[do not store ctime into archive]' \
  133. '--nobirthtime[do not store birthtime (creation date) into archive]' \
  134. '--nobsdflags[do not read and store bsdflags (e.g. NODUMP, IMMUTABLE) into archive]' \
  135. '--ignore-inode[ignore inode data in the file metadata cache used to detect unchanged files]' \
  136. '--files-cache=[operate files cache in MODE. default: ctime,size,inode]:MODE:(ctime,size,inode mtime,size,inode ctime,size mtime,size rechunk,ctime rechunk,mtime disabled)' \
  137. '--read-special[open and read block and char device files as well as FIFOs as if they were regular files]' \
  138. $common_options \
  139. ':ARCHIVE: _borg_repository_or_archive -a -p' \
  140. '*:PATH:_files'
  141. }
  142. (( $+functions[_borg-debug] )) ||
  143. _borg-debug() {
  144. local -a state line common_options
  145. local curcontext="$curcontext" state_descr
  146. declare -A opt_args
  147. local -i ret=1
  148. __borg_setup_common_options
  149. _arguments -s -w -C : \
  150. $common_options \
  151. ': :->command' \
  152. '*:: :->option-or-argument' && ret=0
  153. case $state in
  154. (command)
  155. local -a debug_commands=(
  156. 'info:show system infos for debugging / bug reports'
  157. 'dump-archive-items:dump archive items (metadata)'
  158. 'dump-archive:dump decoded archive metadata'
  159. 'dump-manifest:dump decoded repository metadata'
  160. 'dump-repo-objs:dump repo objects'
  161. 'search-repo-objs:search repo objects'
  162. 'get-obj:get object from repository'
  163. 'put-obj:put object to repository'
  164. 'delete-obj:delete object from repository'
  165. 'refcount-obj:show refcount for object from repository'
  166. 'convert-profile:convert Borg profile to Python profile'
  167. )
  168. _describe -t commands 'command' debug_commands && ret=0
  169. ;;
  170. (option-or-argument)
  171. curcontext="${curcontext%:*}-$line[1]:"
  172. case $line[1] in
  173. (info)
  174. _arguments -s -w -S : \
  175. $common_options && ret=0
  176. ;;
  177. (dump-archive-items)
  178. _arguments -s -w -S : \
  179. $common_options \
  180. ':ARCHIVE: _borg_repository_or_archive -a' && ret=0
  181. ;;
  182. (dump-archive)
  183. _arguments -s -w -S : \
  184. $common_options \
  185. ':ARCHIVE: _borg_repository_or_archive -a' \
  186. ':PATH:_files' && ret=0
  187. ;;
  188. (dump-manifest)
  189. _arguments -s -w -S : \
  190. $common_options \
  191. ': :_borg_repository' \
  192. ':PATH:_files' && ret=0
  193. ;;
  194. (dump-repo-objs)
  195. _arguments -s -w -S : \
  196. $common_options \
  197. ': :_borg_repository' && ret=0
  198. ;;
  199. (search-repo-objs)
  200. _arguments -s -w -S : \
  201. $common_options \
  202. ': :_borg_repository' \
  203. ':WANTED (hex or string):' && ret=0
  204. ;;
  205. (get-obj)
  206. _arguments -s -w -S : \
  207. $common_options \
  208. ': :_borg_repository' \
  209. ':ID (hex object):' \
  210. ':PATH:_files' && ret=0
  211. ;;
  212. (put-obj)
  213. _arguments -s -w -S : \
  214. $common_options \
  215. ': :_borg_repository' \
  216. '*:PATH:_files' && ret=0
  217. ;;
  218. (delete-obj)
  219. _arguments -s -w -S : \
  220. $common_options \
  221. ': :_borg_repository' \
  222. '*:ID (hex object):' && ret=0
  223. ;;
  224. (refcount-obj)
  225. _arguments -s -w -S : \
  226. $common_options \
  227. ': :_borg_repository' \
  228. '*:ID (hex object):' && ret=0
  229. ;;
  230. (convert-profile)
  231. _arguments -s -w -S : \
  232. $common_options \
  233. ':INPUT:_files' \
  234. ':OUTPUT:_files' && ret=0
  235. ;;
  236. (*)
  237. if ! _call_function ret _borg_debug_$line[1]; then
  238. _default && ret=0
  239. fi
  240. ;;
  241. esac
  242. ;;
  243. esac
  244. return ret
  245. }
  246. (( $+functions[_borg-delete] )) ||
  247. _borg-delete() {
  248. local -a common_options common_archive_filters_options common_dry_run_stats_options
  249. __borg_setup_common_options
  250. __borg_setup_common_archive_filters_options
  251. __borg_setup_common_dry_run_stats_options
  252. _arguments -s -w -S : \
  253. $common_dry_run_stats_options \
  254. '--cache-only[delete only the local cache for the given repository]' \
  255. '*--force[force deletion of corrupted archives, use "--force --force" in case "--force" does not work]' \
  256. '--save-space[work slower, but using less space]' \
  257. $common_archive_filters_options \
  258. $common_options \
  259. ':REPOSITORY_OR_ARCHIVE: _borg_repository_or_archive' \
  260. '*:ARCHIVE: _borg_archive "${line[1]%%::*}"'
  261. }
  262. (( $+functions[_borg-diff] )) ||
  263. _borg-diff() {
  264. local -a common_options common_exclude_options
  265. __borg_setup_common_options
  266. __borg_setup_common_exclude_options
  267. _arguments -s -w -S : \
  268. '--numeric-owner[only obey numeric user and group identifiers]' \
  269. '--same-chunker-params[override check of chunker parameters]' \
  270. '--sort[sort the output lines by file path]' \
  271. $common_exclude_options \
  272. $common_options \
  273. ':ARCHIVE1: _borg_repository_or_archive -a' \
  274. ':ARCHIVE2: _borg_archive "${line[1]%%::*}"' \
  275. '*: : _borg_style_selector_or_archive_files -e "$line[1]" pp'
  276. }
  277. (( $+functions[_borg-export-tar] )) ||
  278. _borg-export-tar() {
  279. local -a common_options common_exclude_extract_options
  280. __borg_setup_common_options
  281. __borg_setup_common_exclude_extract_options
  282. _arguments -s -w -S : \
  283. '--tar-filter[filter program to pipe data through]: :_cmdstring' \
  284. '--list[output verbose list of items (files, dirs, ...)]' \
  285. $common_exclude_extract_options \
  286. $common_options \
  287. ':ARCHIVE: _borg_repository_or_archive -a' \
  288. ':FILE:_files' \
  289. '*: : _borg_style_selector_or_archive_files -e "$line[1]" pp'
  290. }
  291. (( $+functions[_borg-extract] )) ||
  292. _borg-extract() {
  293. local -a common_options common_exclude_extract_options
  294. __borg_setup_common_options
  295. __borg_setup_common_exclude_extract_options
  296. _arguments -s -w -S : \
  297. '--list[output verbose list of items (files, dirs, ...)]' \
  298. '(-n --dry-run)'{-n,--dry-run}'[do not actually change any files]' \
  299. '--numeric-owner[only obey numeric user and group identifiers]' \
  300. '--nobsdflags[do not extract/set bsdflags (e.g. NODUMP, IMMUTABLE)]' \
  301. '--stdout[write all extracted data to stdout]' \
  302. '--sparse[create holes in output sparse file from all-zero chunks]' \
  303. $common_exclude_extract_options \
  304. $common_options \
  305. ':ARCHIVE: _borg_repository_or_archive -a' \
  306. '*: : _borg_style_selector_or_archive_files -e "$line[1]" pp'
  307. }
  308. (( $+functions[_borg-help] )) ||
  309. _borg-help() {
  310. local -a common_options
  311. __borg_setup_common_options
  312. _arguments -s -w -S : \
  313. '--epilog-only' \
  314. '--usage-only' \
  315. $common_options \
  316. ':: : _alternative "topics:TOPIC:(patterns placeholders compression)" ": :_borg_commands"'
  317. }
  318. (( $+functions[_borg-info] )) ||
  319. _borg-info() {
  320. local -a common_options common_archive_filters_options
  321. __borg_setup_common_options
  322. __borg_setup_common_archive_filters_options
  323. _arguments -s -w -S : \
  324. '--json[format output as JSON]' \
  325. $common_archive_filters_options \
  326. $common_options \
  327. '::REPOSITORY_OR_ARCHIVE: _borg_repository_or_archive'
  328. }
  329. (( $+functions[_borg-init] )) ||
  330. _borg-init() {
  331. local -i ret=1
  332. local -a common_options common_init_options
  333. __borg_setup_common_options
  334. __borg_setup_common_init_options
  335. # special handling for the required optional argument
  336. if (( ! ${words[(I)(-e|--encryption)(|=*)]} )); then
  337. local desc='select encryption key mode'
  338. local -a long=( "--encryption:$desc" ) short=( "-e:$desc" ) remove_chars=( -r '= \t\n\-' )
  339. _describe -t required-options 'required option' long -S '=' $remove_chars -- short $remove_chars && ret=0
  340. fi
  341. _arguments -s -w -S : \
  342. '(-e --encryption)'{-e,--encryption}'=[select encryption key mode (required)]:MODE:(none keyfile keyfile-blake2 repokey repokey-blake2 authenticated authenticated-blake2)' \
  343. $common_init_options \
  344. '--make-parent-dirs[create parent directories]' \
  345. '::REPOSITORY:_directories' && ret=0
  346. return ret
  347. }
  348. (( $+functions[_borg-key] )) ||
  349. _borg-key() {
  350. local -a state line common_options
  351. local curcontext="$curcontext" state_descr
  352. declare -A opt_args
  353. local -i ret=1
  354. __borg_setup_common_options
  355. _arguments -s -w -C : \
  356. $common_options \
  357. ': :->command' \
  358. '*:: :->option-or-argument' && ret=0
  359. case $state in
  360. (command)
  361. local -a key_commands=(
  362. 'change-passphrase:Change repository key file passphrase'
  363. 'export:Export the repository key for backup'
  364. 'import:Import the repository key from backup'
  365. 'migrate-to-repokey:Migrate passphrase -> repokey'
  366. )
  367. _describe -t commands 'command' key_commands && ret=0
  368. ;;
  369. (option-or-argument)
  370. curcontext="${curcontext%:*}-$line[1]:"
  371. case $line[1] in
  372. (change-passphrase)
  373. _arguments -s -w -S : \
  374. $common_options \
  375. ': :_borg_repository' && ret=0
  376. ;;
  377. (export)
  378. _arguments -s -w -S : \
  379. '--paper[create an export suitable for printing and later type-in]' \
  380. '--qr-html[create an html file suitable for printing and later type-in or qr scan]' \
  381. $common_options \
  382. ': :_borg_repository' \
  383. '::PATH:_files' && ret=0
  384. ;;
  385. (import)
  386. _arguments -s -w -S : \
  387. '--paper[interactively import from a backup done with --paper]' \
  388. $common_options \
  389. ': :_borg_repository' \
  390. '::PATH:_files' && ret=0
  391. ;;
  392. (migrate-to-repokey)
  393. _arguments -s -w -S : \
  394. $common_options \
  395. ':: :_borg_repository' && ret=0
  396. ;;
  397. (*)
  398. if ! _call_function ret _borg_key_$line[1]; then
  399. _default && ret=0
  400. fi
  401. ;;
  402. esac
  403. ;;
  404. esac
  405. return ret
  406. }
  407. (( $+functions[_borg-list] )) ||
  408. _borg-list() {
  409. local -a common_options common_exclude_options common_archive_filters_options
  410. __borg_setup_common_options
  411. __borg_setup_common_exclude_options
  412. __borg_setup_common_archive_filters_options
  413. _arguments -s -w -S : \
  414. '--short[only print file/directory names, nothing else]' \
  415. {--format,--list-format}'=[specify format for file listing]:FORMAT: _borg_format_keys $line[1]' \
  416. '--json[Only valid for listing repository contents. Format output as JSON.]' \
  417. '--json-lines[Only valid for listing archive contents. Format output as JSON Lines.]' \
  418. $common_archive_filters_options \
  419. $common_exclude_options \
  420. $common_options \
  421. ':REPOSITORY_OR_ARCHIVE: _borg_repository_or_archive' \
  422. '*: : _borg_style_selector_or_archive_files -e "$line[1]" pp'
  423. }
  424. (( $+functions[_borg-mount] )) ||
  425. _borg-mount() {
  426. local -a common_options common_exclude_extract_options common_archive_filters_options
  427. __borg_setup_common_options
  428. __borg_setup_common_exclude_extract_options
  429. __borg_setup_common_archive_filters_options
  430. _arguments -s -w -S : \
  431. $* \
  432. '(-f --foreground)'{-f,--foreground}'[stay in foreground, do not daemonize]' \
  433. '-o[mount options]: :_fuse_values "mount options"
  434. "versions[merged, versioned view of the files in the archives]"
  435. "allow_damaged_files[read damaged files]"
  436. "ignore_permissions[not enforce \"default_permissions\"]"' \
  437. $common_archive_filters_options \
  438. $common_exclude_extract_options \
  439. $common_options \
  440. ':REPOSITORY_OR_ARCHIVE: _borg_repository_or_archive' \
  441. ':MOUNTPOINT:_directories' \
  442. '*: : _borg_style_selector_or_archive_files "$line[1]" pp'
  443. }
  444. (( $+functions[_borg-prune] )) ||
  445. _borg-prune() {
  446. local -a common_options common_prefix_and_glob_archives_filter_options common_dry_run_stats_options
  447. __borg_setup_common_options
  448. __borg_setup_common_prefix_and_glob_archives_filter_options
  449. __borg_setup_common_dry_run_stats_options
  450. _arguments -s -w -S : \
  451. $common_dry_run_stats_options \
  452. '*--force[force pruning of corrupted archives, use "--force --force" in case "--force" does not work]' \
  453. '--list[output verbose list of archives it keeps/prunes]' \
  454. '--keep-within[keep all archives within this time interval]: : _borg_guard_unsigned_number "INTERVAL"' \
  455. '(--keep-last --keep-secondly)'{--keep-last,--keep-secondly}'[number of secondly archives to keep]: : _borg_guard_unsigned_number "N"' \
  456. '--keep-minutely[number of minutely archives to keep]: : _borg_guard_unsigned_number "N"' \
  457. '(-H --keep-hourly)'{-H,--keep-hourly}'[number of hourly archives to keep]: : _borg_guard_unsigned_number "N"' \
  458. '(-d --keep-daily)'{-d,--keep-daily}'[number of daily archives to keep]: : _borg_guard_unsigned_number "N"' \
  459. '(-w --keep-weekly)'{-w,--keep-weekly}'[number of weekly archives to keep]: : _borg_guard_unsigned_number "N"' \
  460. '(-m --keep-monthly)'{-m,--keep-monthly}'[number of monthly archives to keep]: : _borg_guard_unsigned_number "N"' \
  461. '(-y --keep-yearly)'{-y,--keep-yearly}'[number of yearly archives to keep]: : _borg_guard_unsigned_number "N"' \
  462. '--save-space[work slower, but using less space]' \
  463. $common_prefix_and_glob_archives_filter_options \
  464. $common_options \
  465. ':: :_borg_repository'
  466. }
  467. (( $+functions[_borg-recreate] )) ||
  468. _borg-recreate() {
  469. local -a common_options common_create_options
  470. __borg_setup_common_options
  471. __borg_setup_common_create_options
  472. local -a mods=(
  473. 'if-different:recompress if current compression is with a different compression algorithm (the level is not considered)'
  474. 'always:recompress even if current compression is with the same compression algorithm (use this to change the compression level)'
  475. 'never:do not recompress (use this option to explicitly prevent recompression)'
  476. )
  477. mods=( ${${(q)mods//\\/\\\\}//:/\\:} )
  478. _arguments -s -w -S : \
  479. $common_create_options \
  480. '--target=[create a new archive with the name ARCHIVE]:ARCHIVE: _borg_placeholder_or_archive "${line[1]%%\:\:*}"' \
  481. '--recompress=[recompress data chunks according to "MODE" and "--compression"]:MODE:'"(($mods))" \
  482. $common_options \
  483. ':REPOSITORY_OR_ARCHIVE: _borg_repository_or_archive' \
  484. '*: : _borg_style_selector_or_archive_files -e "$line[1]" pp'
  485. }
  486. (( $+functions[_borg-rename] )) ||
  487. _borg-rename() {
  488. local -a common_options
  489. __borg_setup_common_options
  490. _arguments -s -w -S : \
  491. $common_options \
  492. ':ARCHIVE: _borg_repository_or_archive -a' \
  493. ':NEWNAME'
  494. }
  495. (( $+functions[_borg-serve] )) ||
  496. _borg-serve() {
  497. local -a common_options common_init_options
  498. __borg_setup_common_options
  499. __borg_setup_common_init_options
  500. _arguments -s -w -S : \
  501. $common_init_options \
  502. '*--restrict-to-path=[restrict repository access to PATH]:PATH:_files' \
  503. '*--restrict-to-repository=[restrict repository access]: :_borg_repository'
  504. }
  505. (( $+functions[_borg-umount] )) ||
  506. _borg-umount() {
  507. local -a common_options
  508. __borg_setup_common_options
  509. _arguments -s -w -S : \
  510. $common_options \
  511. ':MOUNTPOINT:_umountable'
  512. }
  513. (( $+functions[_borg-upgrade] )) ||
  514. _borg-upgrade() {
  515. local -a common_options
  516. __borg_setup_common_options
  517. _arguments -s -w -S : \
  518. '(-n --dry-run)'{-n,--dry-run}'[do not change repository]' \
  519. '--inplace[rewrite repository in place, with no chance of going back to older versions of the repository]' \
  520. '--force[force upgrade]' \
  521. '--tam[enable manifest authentication (in key and cache)]' \
  522. '--disable-tam[disable manifest authentication (in key and cache)]' \
  523. $common_options \
  524. ':: :_borg_repository'
  525. }
  526. (( $+functions[_borg-with-lock] )) ||
  527. _borg-with-lock() {
  528. local -a state line common_options
  529. local curcontext="$curcontext" state_descr
  530. declare -A opt_args
  531. local -i ret=1
  532. __borg_setup_common_options
  533. _arguments -s -w -C -S : \
  534. $common_options \
  535. '(-): :_borg_repository' \
  536. '(-):COMMAND: _command_names -e' \
  537. '(-)*:ARGS:->normal' && ret=0
  538. case $state in
  539. (normal)
  540. shift 2 words
  541. (( CURRENT -= 2 ))
  542. _normal && ret=0
  543. ;;
  544. esac
  545. return ret
  546. }
  547. (( $+functions[__borg_setup_common_options] )) ||
  548. __borg_setup_common_options() {
  549. typeset -ga common_options=(
  550. '(- :)'{-h,--help}'[show this help message and exit]'
  551. '--critical[work on log level CRITICAL]'
  552. '--error[work on log level ERROR]'
  553. '--warning[work on log level WARNING (default)]'
  554. '(--info -v --verbose)'{--info,-v,--verbose}'[work on log level INFO]'
  555. '--debug[work on log level DEBUG]'
  556. '--debug-topic=[enable TOPIC debugging (can be specified multiple times)]:TOPIC'
  557. '(-p --progress)'{-p,--progress}'[show progress information]'
  558. '--log-json[Output one JSON object per log line instead of formatted text.]'
  559. '--lock-wait=[wait at most SECONDS for acquiring a repository/cache lock (default: 1)]: : _borg_guard_unsigned_number "SECONDS"'
  560. '--bypass-lock[bypass locking mechanism]'
  561. '(- :)--show-version[show/log the borg version]'
  562. '--show-rc[show/log the return code (rc)]'
  563. '--umask=[set umask to M (local and remote, default: 0077)]: : _borg_guard_numeric_mode "M"'
  564. '--remote-path=[set remote path to executable (default: "borg")]: :_cmdstring'
  565. '--remote-ratelimit=[set remote network upload rate limit in kiByte/s (default: 0=unlimited)]: : _borg_guard_unsigned_number "RATE"'
  566. '--consider-part-files[treat part files like normal files (e.g. to list/extract them)]'
  567. '--debug-profile=[write execution profile in Borg format into FILE]:FILE:_files'
  568. '--rsh=[use COMMAND instead of ssh]: :_cmdstring'
  569. )
  570. }
  571. (( $+functions[__borg_setup_common_exclude_options] )) ||
  572. __borg_setup_common_exclude_options() {
  573. typeset -ga common_exclude_options=(
  574. '*'{-e,--exclude}'=[exclude paths matching PATTERN]: : _borg_style_selector_or_archive_files "$line[1]" fm'
  575. '*--exclude-from=[read exclude patterns from EXCLUDEFILE, one per line]:EXCLUDEFILE:_files'
  576. '*--pattern=[experimental: include/exclude paths matching PATTERN]: : _borg_style_selector_or_archive_files -p "$line[1]" sh'
  577. '*--patterns-from=[experimental: read include/exclude patterns from PATTERNFILE, one per line]:PATTERNFILE:_files'
  578. )
  579. }
  580. (( $+functions[__borg_setup_common_exclude_extract_options] )) ||
  581. __borg_setup_common_exclude_extract_options() {
  582. local -a common_exclude_options
  583. __borg_setup_common_exclude_options
  584. typeset -ga common_exclude_extract_options=(
  585. $common_exclude_options
  586. '--strip-components=[Remove the specified number of leading path elements. Paths with fewer elements will be silently skipped.]: : _borg_guard_unsigned_number "NUMBER"'
  587. )
  588. }
  589. (( $+functions[__borg_setup_common_prefix_and_glob_archives_filter_options] )) ||
  590. __borg_setup_common_prefix_and_glob_archives_filter_options() {
  591. typeset -ga common_prefix_and_glob_archives_filter_options=(
  592. '(-P --prefix -a --glob-archives)'{-P,--prefix}'=[only consider archive names starting with this prefix]:PREFIX: _borg_archive -n "${line[1]%%\:\:*}"'
  593. '(-P --prefix)*'{-a,--glob-archives}'=[only consider archive names matching the glob]:GLOB: _borg_archive -n "${line[1]%%\:\:*}"'
  594. )
  595. }
  596. (( $+functions[__borg_setup_common_archive_filters_options] )) ||
  597. __borg_setup_common_archive_filters_options() {
  598. local -a common_prefix_and_glob_archives_filter_options
  599. __borg_setup_common_prefix_and_glob_archives_filter_options
  600. typeset -ga common_archive_filters_options=(
  601. $common_prefix_and_glob_archives_filter_options
  602. '--sort-by=[Comma-separated list of sorting keys, default: timestamp]:KEYS:(timestamp name id)'
  603. '(--last)--first=[consider first N archives after other filters were applied]:N: _borg_archive -n "${line[1]%%\:\:*}"'
  604. '(--first)--last=[consider last N archives after other filters were applied]:N: _borg_archive -n "${line[1]%%\:\:*}"'
  605. )
  606. }
  607. (( $+functions[__borg_setup_common_dry_run_stats_options] )) ||
  608. __borg_setup_common_dry_run_stats_options() {
  609. typeset -ga common_dry_run_stats_options=(
  610. '(-n --dry-run -s --stats)'{-n,--dry-run}'[do not change anything]'
  611. '(-n --dry-run -s --stats)'{-s,--stats}'[print statistics at end]'
  612. # NOTE: actual messages for subcommands differ in details
  613. )
  614. }
  615. (( $+functions[__borg_setup_common_create_options] )) ||
  616. __borg_setup_common_create_options() {
  617. local -a common_dry_run_stats_options common_exclude_options
  618. __borg_setup_common_dry_run_stats_options
  619. __borg_setup_common_exclude_options
  620. typeset -ga common_create_options=(
  621. $common_dry_run_stats_options
  622. '--list[output verbose list of items (files, dirs, ...)]'
  623. '--filter=[only display items with the given status characters]: :_borg_statuschars'
  624. $common_exclude_options
  625. '--exclude-caches[exclude directories that contain a CACHEDIR.TAG file]'
  626. '*--exclude-if-present=[exclude directories that are tagged by containing a filesystem object with the given NAME]:NAME:_files'
  627. '--keep-'{exclude-tags,tag-files}'[if tag objects are specified with --exclude-if-present, don'\''t omit the tag objects themselves]'
  628. '--comment=[add a comment text to the archive]:COMMENT:_borg_placeholders'
  629. '--timestamp=[manually specify the archive creation date/time]:TIMESTAMP:_borg_timestamp'
  630. '(-c --checkpoint-interval)'{-c,--checkpoint-interval}'=[write checkpoint every SECONDS seconds (default: 1800)]: : _borg_guard_unsigned_number "SECONDS"'
  631. '--chunker-params=[specify the chunker parameters]: :_borg_chunker_params'
  632. '(-C --compression)'{-C,--compression}'=[select compression algorithm]: :_borg_compression'
  633. )
  634. }
  635. (( $+functions[__borg_setup_common_init_options] )) ||
  636. __borg_setup_common_init_options() {
  637. local -a common_options
  638. __borg_setup_common_options
  639. typeset -ga common_init_options=(
  640. $common_options
  641. '--append-only[only allow appending to repository segment files]'
  642. '--storage-quota=[override storage quota of the repository]: :_borg_quota_suffixes'
  643. )
  644. }
  645. (( $+functions[_borgfs] )) ||
  646. _borgfs() {
  647. _borg-mount '(- :)'{-V,--version}'[show version number and exit]'
  648. }
  649. (( $+functions[_borg_parameters] )) ||
  650. _borg_parameters() {
  651. local name=$1
  652. shift
  653. local -i ret=1
  654. local -a expl
  655. case $name in
  656. (REPO)
  657. local BORG_REPO
  658. unset BORG_REPO
  659. _borg_repository && ret=0
  660. ;;
  661. ((|NEW_)PASSPHRASE)
  662. _message -e 'passphrase' && ret=0
  663. ;;
  664. (DISPLAY_PASSPHRASE)
  665. _message -e 'answer to the "display the passphrase for verification" question' && ret=0
  666. ;;
  667. (HOST_ID)
  668. _message -e 'unique ID' && ret=0
  669. ;;
  670. (FILES_CACHE_SUFFIX)
  671. _message -e 'suffix' && ret=0
  672. ;;
  673. (FILES_CACHE_TTL)
  674. _borg_guard_unsigned_number 'time to live (default: 20)' && ret=0
  675. ;;
  676. (MOUNT_DATA_CACHE_ENTRIES)
  677. _borg_guard_unsigned_number 'number of cached data chunks' && ret=0
  678. ;;
  679. (PASSCOMMAND|RSH|REMOTE_PATH)
  680. _cmdstring && ret=0
  681. ;;
  682. (HOSTNAME_IS_UNIQUE|SHOW_SYSINFO|(UNKNOWN_UNENCRYPTED|RELOCATED)_REPO_ACCESS_IS_OK)
  683. _description values expl 'value'
  684. compadd "$expl[@]" yes no && ret=0
  685. ;;
  686. ((CHECK|DELETE)_I_KNOW_WHAT_I_AM_DOING)
  687. _description values expl 'value'
  688. compadd "$expl[@]" YES NO && ret=0
  689. ;;
  690. (WORKAROUNDS)
  691. _wanted workarounds expl 'workaround' _sequence -n 1 compadd - basesyncfile && ret=0
  692. ;;
  693. (KEYS_DIR)
  694. _directories && ret=0
  695. ;;
  696. (*)
  697. _default && ret=0
  698. ;;
  699. esac
  700. return ret
  701. }
  702. (( $+functions[_borg_repository] )) ||
  703. _borg_repository() {
  704. local -a alts opts qopts
  705. zparseopts -E -a opts S:
  706. qopts=( ${(q)opts} )
  707. [[ -n $BORG_REPO ]] && alts+=( "default-repository: : __borg_default_repository $qopts" )
  708. alts+=( "cached-repositories:cached repositories:_borg_cached_repositories $qopts" )
  709. alts+=( 'directories: :_directories -r ":/ \t\n\-"' )
  710. alts+=( 'remote-repositories: : _borg_remote_repositories' )
  711. _alternative $alts
  712. }
  713. (( $+functions[__borg_default_repository] )) ||
  714. __borg_default_repository() {
  715. local -a opts suf
  716. zparseopts -E -a opts S:
  717. (( $opts[(I)-S] )) && suf=( -S '' )
  718. local -a default_repository=( "\:\::$BORG_REPO" )
  719. _describe -t default-repository 'default repository' default_repository "$suf[@]"
  720. }
  721. (( $+functions[_borg_cached_repositories] )) ||
  722. _borg_cached_repositories() {
  723. local -a cached_repos
  724. local sed_script='/^previous_location = / {
  725. s///
  726. # no port was given
  727. /ssh:\/\/[^/:]+:[0-9]+/! {
  728. # lstrip the `ssh://` prefix and add a colon before the first slash
  729. s!ssh://([^:/]+)/(.*)!\1:/\2!
  730. }
  731. p
  732. }'
  733. local cachedir=${BORG_CACHE_DIR:-${XDG_CACHE_HOME:-${BORG_BASE_DIR:-$HOME}/.cache}/borg}
  734. cached_repos=( ${(f)"$(_call_program -p cached-repositories sed -n -E ${(q)sed_script} \
  735. "${(q)cachedir}/*/config(#qN.om)" 2>/dev/null)"} )
  736. if [[ $compstate[quote] != (\'|\") ]]; then
  737. # hide ~BORG_REPO and other scalars
  738. local BORG_REPO
  739. unset BORG_REPO sed_script cachedir
  740. cached_repos=( "${(@D)cached_repos}" )
  741. fi
  742. compadd -Q "$@" -r ': \t\n\-' -a cached_repos
  743. }
  744. (( $+functions[_borg_remote_repositories] )) ||
  745. _borg_remote_repositories() {
  746. local -a match mbegin mend expl alts
  747. if compset -P '(#b)ssh://[^/]##@[^/]##:([0-9]##)/'; then
  748. _remote_files -/ -- ssh -p $match[1]
  749. return
  750. fi
  751. local -i have_scheme=0
  752. compset -P 'ssh://' && have_scheme=1
  753. if compset -P '*:'; then
  754. (( have_scheme )) && alts+=( 'ports: : _borg_guard_unsigned_number "port"' )
  755. alts+=( 'remote-files:remote file: _remote_files -/ -- ssh' )
  756. _alternative $alts
  757. elif compset -P 1 '(#b)(*)@'; then
  758. local user=$match[1]
  759. _wanted -C user-at hosts expl "host for $user" \
  760. _combination -s '[:@]' accounts users-hosts users="$user" hosts -S ':' -
  761. elif compset -S '@*'; then
  762. _wanted users expl "user" \
  763. _combination -s '[:@]' accounts users-hosts users -q -
  764. else
  765. alts=(
  766. 'users:user:_users -S "@"'
  767. 'hosts:host:_hosts -S ":"'
  768. )
  769. (( ! have_scheme )) && alts+=( 'prefixes:ssh:compadd -S "" ssh://' )
  770. _alternative $alts
  771. fi
  772. }
  773. # _borg_repository_or_archive [-a] [-p]
  774. #
  775. # -a archive is mandatory. The suffix `::` will be added to the repository if possible.
  776. # -p complete placeholders
  777. (( $+functions[_borg_repository_or_archive] )) ||
  778. _borg_repository_or_archive() {
  779. local -A opts
  780. zparseopts -A opts -D -E a p
  781. if compset -P 1 '*::'; then
  782. local qrepo=$IPREFIX[1,-3]
  783. local -i def_repo=0
  784. [[ -z $qrepo && -n $BORG_REPO ]] && qrepo=${(q)BORG_REPO} && def_repo=1
  785. if [[ -n $qrepo ]]; then
  786. if (( ! def_repo )); then
  787. case $compstate[quote] in
  788. (\') qrepo=${(qq)qrepo} ;;
  789. (\") qrepo=${(qqq)${(e)qrepo}} ;;
  790. # NOTE: currently `(e)` don't have any effect, but maybe one day zsh will stop to change the quoting method
  791. # of double quoted parameters
  792. esac
  793. fi
  794. if (( $+opts[-p] )); then
  795. _borg_placeholder_or_archive $qrepo
  796. else
  797. _borg_archive $qrepo
  798. fi
  799. else
  800. _message "not a borg repository: ${(Q)qrepo}"
  801. return 1
  802. fi
  803. else
  804. local -a suf
  805. if ! compset -S '::*'; then
  806. if (( $+opts[-a] )) || zstyle -T ":completion:${curcontext}:repositories" repository-suffix; then
  807. suf=( -S '::' )
  808. fi
  809. local oqrepo="$PREFIX$SUFFIX"
  810. local qrepo=$oqrepo
  811. [[ $compstate[quote] != (\'|\") ]] && qrepo=${(Q)qrepo}
  812. if __borg_is_borg_repo $qrepo; then
  813. qrepo=${oqrepo%%/}
  814. [[ -z $SUFFIX ]] && PREFIX=${PREFIX%%/} || SUFFIX=${SUFFIX%%/}
  815. compadd -S '::' -r ':/ \t\n\-' -Q -- $qrepo
  816. return
  817. fi
  818. fi
  819. _borg_repository "$suf[@]"
  820. fi
  821. }
  822. # _borg_archive [-F] [-n] [qrepo]
  823. #
  824. # -F don't apply archive filter options on the command line
  825. # -n reverse order, disable matchers and don't do menu completion/selection
  826. (( $+functions[_borg_archive] )) ||
  827. _borg_archive() {
  828. local -A opts
  829. zparseopts -A opts -D -E F n
  830. local qrepo=$1
  831. if [[ -z $qrepo ]]; then
  832. if [[ -n $BORG_REPO ]]; then
  833. qrepo=${(q)BORG_REPO}
  834. else
  835. _message 'no repository specified'
  836. return 1
  837. fi
  838. fi
  839. local -i ret=1
  840. _tags archives
  841. while _tags; do
  842. if _requested archives; then
  843. local -a expl disp archive_filters
  844. local -i reversed_order=1
  845. if (( ! $+opts[-F] )); then
  846. local -a archive_filter_options=( -P --prefix -a --glob-archives --first --last --sort-by ) tmp
  847. local k
  848. for k in $archive_filter_options; do
  849. if [[ -n $opt_args[$k] ]]; then
  850. IFS=: read -A tmp <<<$opt_args[$k]
  851. archive_filters+=( $k=${^tmp:#} )
  852. fi
  853. done
  854. fi
  855. if (( $+opts[-n] )); then
  856. __borg_skip_pattern_matching || return 1
  857. disp+=( -U )
  858. compstate[insert]=''
  859. compstate[list]='list force'
  860. reversed_order=0
  861. fi
  862. local -a asort
  863. zstyle -a ":completion:${curcontext}:archives" archive-sort asort
  864. if (( $asort[(I)inverse] )); then
  865. (( reversed_order = ! reversed_order ))
  866. fi
  867. local -a sort_by=( --sort-by=${(M)^asort:#(timestamp|name|id)} )
  868. # NOTE: in case of option repetition, the later one takes precedence
  869. if (( ! $+__borg_archives_need_update )); then
  870. comppostfuncs+=( __borg_unset_archives_need_update )
  871. typeset -gHi __borg_archives_need_update=1
  872. if (( ! $#archive_filters && ! $+opts[-n] )); then
  873. local erepo
  874. [[ -n $1 ]] && __borg_expand_path ${(Q)qrepo} erepo
  875. local -a newest_file=( $erepo/(hints|index|integrity).<1->(#qN.om[1]) )
  876. if [[ -n $newest_file ]]; then
  877. if zmodload -F zsh/stat b:zstat 2>/dev/null; then
  878. local -a stats
  879. zstat -A stats +mtime $newest_file
  880. local -i mtime=$stats[1]
  881. if [[ $__borg_prev_repo == $erepo
  882. && __borg_prev_mtime -ge mtime
  883. && $__borg_prev_order == $reversed_order
  884. && $__borg_prev_sort_by == $sort_by ]]
  885. then
  886. __borg_archives_need_update=0
  887. else
  888. typeset -gH __borg_prev_repo=$erepo
  889. typeset -gHi __borg_prev_mtime=mtime __borg_prev_order=reversed_order
  890. typeset -gHa __borg_prev_sort_by=( $sort_by )
  891. fi
  892. fi
  893. fi
  894. else
  895. unset __borg_prev_{repo,mtime,order,sort_by}
  896. comppostfuncs+=( __borg_unset_archives )
  897. fi
  898. fi
  899. if zstyle -t ":completion:${curcontext}:archives" verbose; then
  900. if (( __borg_archives_need_update || ! $+__borg_archive_names || ! $+__borg_archive_descriptions )); then
  901. __borg_archives_need_update=0
  902. typeset -gHa __borg_archive_names=() __borg_archive_descriptions=()
  903. local fmt descfmt name desc
  904. zstyle -s ":completion:${curcontext}:archives" archive-description-format descfmt ||
  905. descfmt='{archive:<36} {time} [{id}]'
  906. fmt="{barchive}{NUL}$descfmt{NUL}"
  907. _call_program -p archive-descriptions \
  908. ${(q)__borg_command:-borg} list --format=${(q)fmt} ${(q)sort_by} $archive_filters $qrepo 2>/dev/null |
  909. while IFS= read -r -d $'\0' name && IFS= read -r -d $'\0' descr; do
  910. __borg_archive_names[1,0]=( $name )
  911. __borg_archive_descriptions[1,0]=( "$descr" )
  912. done
  913. (( $pipestatus[1] )) && {
  914. _message "couldn't list repository: ${(Q)qrepo}"
  915. unset __borg_prev_{repo,mtime,order,sort_by}
  916. return 1
  917. }
  918. (( ! reversed_order )) &&
  919. __borg_archive_names=( "${(@aO)__borg_archive_names}" ) &&
  920. __borg_archive_descriptions=( "${(@aO)__borg_archive_descriptions}" )
  921. fi
  922. disp+=( -ld __borg_archive_descriptions )
  923. elif (( __borg_archives_need_update || ! $+__borg_archive_names )); then
  924. __borg_archives_need_update=0
  925. typeset -gHa __borg_archive_names=()
  926. local fmt='{barchive}{NUL}'
  927. __borg_archive_names=( ${(@0aO)"$(_call_program -p archives \
  928. ${(q)__borg_command:-borg} list --format=${(q)fmt} ${(q)sort_by} $archive_filters $qrepo 2>/dev/null)"} )
  929. (( $pipestatus[1] )) && {
  930. _message "couldn't list repository: ${(Q)qrepo}"
  931. unset __borg_prev_{repo,mtime,order,sort_by}
  932. return 1
  933. }
  934. (( ! reversed_order )) &&
  935. __borg_archive_names=( "${(@aO)__borg_archive_names}" )
  936. fi
  937. _all_labels archives expl 'ARCHIVE' compadd "$disp[@]" -a __borg_archive_names && ret=0
  938. fi
  939. (( ret )) || return 0
  940. done
  941. return 1
  942. }
  943. (( $+functions[__borg_unset_archives] )) ||
  944. __borg_unset_archives() {
  945. unset __borg_archive_names __borg_archive_descriptions
  946. }
  947. (( $+functions[__borg_unset_archives_need_update] )) ||
  948. __borg_unset_archives_need_update() {
  949. unset __borg_archives_need_update
  950. }
  951. (( $+functions[__borg_is_borg_repo] )) ||
  952. __borg_is_borg_repo() {
  953. local repo=$1
  954. __borg_expand_path $repo repo
  955. if [[ -d $repo && -d $repo/data ]]; then
  956. local -a files=( $repo/(hints|index|integrity).<1->(#qN.) )
  957. (( $#files >= 3 )) && return 0
  958. fi
  959. return 1
  960. }
  961. (( $+functions[__borg_expand_path] )) ||
  962. __borg_expand_path() {
  963. local _path=$1
  964. local -a match mbegin mend
  965. if [[ $_path == (#b)(\~[^/]#)(|/*) ]]; then
  966. local etilde
  967. etilde=$~match[1] 2>/dev/null
  968. _path="$etilde$match[2]"
  969. fi
  970. _path=${(e)_path//\\\\/\\\\\\\\}
  971. eval typeset -g ${2:-REPLY}=\$_path
  972. }
  973. (( $+functions[_borg_placeholder_or_archive] )) ||
  974. _borg_placeholder_or_archive() {
  975. local qrepo=$1
  976. shift
  977. _alternative \
  978. 'placeholders: :_borg_placeholders' \
  979. "archives: : _borg_archive ${(q)qrepo}"
  980. }
  981. (( $+functions[_borg_placeholders] )) ||
  982. _borg_placeholders() {
  983. local -a placeholders=(
  984. 'hostname:The (short) hostname of the machine.'
  985. 'fqdn:The full name of the machine.'
  986. 'reverse-fqdn:The full name of the machine in reverse domain name notation.'
  987. 'now:The current local date and time, by default in ISO-8601 format. You can also supply your own format string, e.g. {now:%Y-%m-%d_%H:%M:%S}'
  988. 'utcnow:The current UTC date and time, by default in ISO-8601 format. You can also supply your own format string, e.g. {utcnow:%Y-%m-%d_%H:%M:%S}'
  989. 'user:The user name (or UID, if no name is available) of the user running borg.'
  990. 'pid:The current process ID.'
  991. 'borgversion:The version of borg, e.g.: 1.0.8rc1'
  992. 'borgmajor:The version of borg, only the major version, e.g.: 1'
  993. 'borgminor:The version of borg, only major and minor version, e.g.: 1.0'
  994. 'borgpatch:The version of borg, only major, minor and patch version, e.g.: 1.0.8'
  995. )
  996. __borg_complete_keys _describe -t placeholders 'placeholder' placeholders '"$copts[@]"'
  997. }
  998. (( $+functions[_borg_format_keys] )) ||
  999. _borg_format_keys() {
  1000. local repo_or_arch=${(Q)1}
  1001. local -a keys=( NEWLINE NL NUL SPACE TAB CR LF )
  1002. local -a repository_keys=( archive name barchive comment bcomment id start time end hostname username )
  1003. local -a archive_keys=( type mode uid gid user group path bpath source linktarget flags size csize dsize dcsize
  1004. num_chunks unique_chunks mtime ctime atime isomtime isoctime isoatime blake2b blake2s md5 sha1 sha224 sha256 sha384
  1005. sha3_224 sha3_256 sha3_384 sha3_512 sha512 shake_128 shake_256 archiveid archivename extra health )
  1006. local akeys rkeys
  1007. akeys='archive-keys:archive keys:compadd -a archive_keys'
  1008. rkeys='repository-keys:repository keys:compadd -a repository_keys'
  1009. local -a alts=( 'keys:keys:compadd -a keys' )
  1010. if [[ $repo_or_arch == *::?* ]]; then
  1011. alts+=( $akeys )
  1012. elif [[ -n $repo_or_arch ]]; then
  1013. alts+=( $rkeys )
  1014. else
  1015. alts+=( $rkeys $akeys )
  1016. fi
  1017. __borg_complete_keys _alternative -O copts ${(q)alts}
  1018. }
  1019. (( $+functions[__borg_complete_keys] )) ||
  1020. __borg_complete_keys() {
  1021. compset -P '*[^A-Za-z]##'
  1022. compset -S '[^A-Za-z]##*'
  1023. [[ -n $ISUFFIX ]] && compstate[to_end]=''
  1024. # NOTE: `[[ -n $ISUFFIX ]]` is a workarond for a bug that causes cursor movement to the right further than it should
  1025. # NOTE: the _oldlist completer doesn't respect compstate[to_end]=''
  1026. local ipref suf
  1027. if [[ $IPREFIX[-1] != '{' ]]; then
  1028. ipref='{'
  1029. [[ $compstate[quote] != (\'|\") ]] && ipref='\{'
  1030. fi
  1031. if [[ $ISUFFIX[1] != (|\\)\} ]]; then
  1032. suf='}'
  1033. [[ $compstate[quote] != (\'|\") ]] && suf='\}'
  1034. fi
  1035. local -a copts=( -i "$ipref" -S "$suf" )
  1036. eval "$@"
  1037. }
  1038. # _borg_style_selector_or_archive_files [-e] [-p] archive default_style_selector
  1039. #
  1040. # -e apply exclusion options on the command line
  1041. # -p complete `--pattern`
  1042. # -f complete files rather than borg paths
  1043. (( $+functions[_borg_style_selector_or_archive_files] )) ||
  1044. _borg_style_selector_or_archive_files() {
  1045. local -A opts
  1046. zparseopts -A opts -D -E e p f
  1047. local arch=$1 default_style_selector=$2
  1048. shift 2
  1049. local -a match mbegin mend expl tags=( style-selectors archive-files ) ss_suf=( -S ':' -r ':' )
  1050. (( $+opts[-f] )) && tags=( style-selectors files )
  1051. local -i ret=1
  1052. if (( $+opts[-p] )); then
  1053. if ! compset -P '(#b)([RP\+\-\!])'; then
  1054. local -a pattern_rules=(
  1055. 'P:pattern style'
  1056. 'R:root path'
  1057. '+:include'
  1058. '-:exclude'
  1059. '!:exclude non-recurse'
  1060. )
  1061. _describe -t pattern-rules 'pattern rule' pattern_rules -S ''
  1062. return
  1063. else
  1064. if [[ $compstate[quote] == (\'|\") ]]; then
  1065. compset -P ' #'
  1066. else
  1067. compset -P '(\\ )#'
  1068. fi
  1069. if [[ $match[1] == 'R' ]]; then
  1070. default_style_selector='pp'
  1071. elif [[ $match[1] == 'P' ]]; then
  1072. tags=( style-selectors )
  1073. ss_suf=()
  1074. fi
  1075. fi
  1076. fi
  1077. _tags $tags
  1078. while _tags; do
  1079. if _requested style-selectors; then
  1080. _all_labels style-selectors expl 'style selector' \
  1081. __borg_style_selectors $default_style_selector "$ss_suf[@]" - && ret=0
  1082. fi
  1083. if _requested archive-files; then
  1084. _all_labels archive-files expl 'PATTERN' \
  1085. __borg_archive_files ${(k)opts} "$arch" $default_style_selector - && ret=0
  1086. fi
  1087. if _requested files; then
  1088. local -a borg_paths=( ${(Q)${(e)${~@}}} )
  1089. _all_labels files expl 'PATH' \
  1090. __borg_pattern_files ${(k)opts} borg_paths - && ret=0
  1091. fi
  1092. (( ret )) || return 0
  1093. done
  1094. return 1
  1095. }
  1096. (( $+functions[__borg_style_selectors] )) ||
  1097. __borg_style_selectors() {
  1098. local default_style_selector=$1 path_style_selector
  1099. shift
  1100. zstyle -s ":completion:${curcontext}:archive-files" path-style-selector path_style_selector ||
  1101. path_style_selector='fm'
  1102. local -a disp
  1103. local -A style_selectors
  1104. __borg_setup_style_selectors
  1105. if zstyle -T ":completion:${curcontext}:style-selectors" verbose; then
  1106. local -a style_selector_descriptions extra
  1107. local k v sep
  1108. for k v in ${(kv)style_selectors}; do
  1109. extra=()
  1110. [[ $k == $default_style_selector ]] && extra+=( 'default' )
  1111. [[ $k == $path_style_selector ]] && __borg_choose_path_or_pattern "" "$default_style_selector" &&
  1112. extra+=( 'path' )
  1113. (( $#extra )) && v+=" (${(j:, :)extra})"
  1114. style_selector_descriptions+=( "${${k//\\/\\\\}//:/\\:}:$v" )
  1115. done
  1116. zstyle -s ":completion:${curcontext}:style-selectors" list-separator sep || sep=--
  1117. zformat -a style_selector_descriptions " $sep " $style_selector_descriptions
  1118. disp=( -ld style_selector_descriptions )
  1119. fi
  1120. compadd "$disp[@]" "$@" -k style_selectors
  1121. }
  1122. (( $+functions[__borg_archive_files] )) ||
  1123. __borg_archive_files() {
  1124. local -A opts
  1125. zparseopts -A opts -D e p
  1126. local arch=$1 default_style_selector=$2
  1127. shift 2
  1128. if [[ -z $arch || $arch != *::?* ]]; then
  1129. _message 'no archive specified'
  1130. return 1
  1131. fi
  1132. local -a qargs tmp disp pref match mbegin mend archive_files descs
  1133. local -A style_selectors
  1134. local k cword fmt descfmt style_selector path_style_selector name descr
  1135. # take into account exclude options on the command line
  1136. if (( $+opts[-e] )); then
  1137. local -a exclude_options=( -e --exclude --exclude-from --pattern --pattern-from )
  1138. local -a excludes
  1139. for k in $exclude_options; do
  1140. if [[ -n $opt_args[$k] ]]; then
  1141. IFS=: read -A tmp <<<$opt_args[$k]
  1142. excludes+=( $k="${^tmp[@]}" )
  1143. fi
  1144. done
  1145. [[ -n $excludes ]] && qargs+=( "$excludes[@]" )
  1146. fi
  1147. (( $_matcher_num > 1 )) && return 1
  1148. __borg_skip_pattern_matching || return 1
  1149. cword="$PREFIX$SUFFIX"
  1150. [[ $compstate[quote] != (\'|\") ]] && cword=${(Q)cword}
  1151. [[ -z $cword ]] && return 1
  1152. if zstyle -t ":completion:${curcontext}:archive-files" verbose; then
  1153. zstyle -s ":completion:${curcontext}:archive-files" file-description-format descfmt ||
  1154. descfmt='{mode} {user:6} {group:6} {size:8d} {mtime} {path}{extra}'
  1155. fmt="{bpath}{NUL}$descfmt{NUL}"
  1156. else
  1157. fmt='{bpath}{NUL}'
  1158. fi
  1159. qargs+=( --format=${(q)fmt} )
  1160. qargs+=( $arch )
  1161. __borg_setup_style_selectors
  1162. [[ $cword == (#b)(${~${(j:|:)${(kb)style_selectors}}}):* ]] && style_selector=$match[1]
  1163. local -i path_expected=0
  1164. __borg_choose_path_or_pattern "$style_selector" $default_style_selector $cword && path_expected=1
  1165. if [[ -n $cword ]]; then
  1166. if (( path_expected )); then
  1167. [[ -n $style_selector ]] && compset -P "$style_selector:" && pref=( -P "$style_selector:" )
  1168. cword="$PREFIX$SUFFIX"
  1169. [[ $compstate[quote] != (\'|\") ]] && cword=${(Q)cword}
  1170. zstyle -s ":completion:${curcontext}:archive-files" path-style-selector path_style_selector ||
  1171. path_style_selector='fm'
  1172. cword="$path_style_selector:$cword"
  1173. else
  1174. [[ -z $style_selector ]] && cword="$default_style_selector:$cword"
  1175. fi
  1176. qargs+=( ${(q)cword} )
  1177. fi
  1178. if zstyle -t ":completion:${curcontext}:archive-files" verbose; then
  1179. _call_program -p archive-file-descriptions ${(q)__borg_command:-borg} list $qargs 2>/dev/null |
  1180. while IFS= read -r -d $'\0' name && IFS= read -r -d $'\0' descr; do
  1181. archive_files+=( $name )
  1182. descs+=( $descr )
  1183. done
  1184. (( $pipestatus[1] )) && { _message "couldn't list archive: ${(Q)arch}"; return 1 }
  1185. disp=( -ld descs )
  1186. else
  1187. archive_files=( ${(0)"$(_call_program -p archive-files ${(q)__borg_command:-borg} list $qargs 2>/dev/null)"} )
  1188. (( $pipestatus[1] )) && { _message "couldn't list archive: ${(Q)arch}"; return 1 }
  1189. fi
  1190. if (( $#archive_files )); then
  1191. if (( path_expected )); then
  1192. compstate[insert]='automenu'
  1193. else
  1194. compstate[insert]=''
  1195. compstate[list]='list force'
  1196. fi
  1197. fi
  1198. compadd "$pref[@]" -U "$disp[@]" "$@" -a archive_files
  1199. }
  1200. (( $+functions[__borg_choose_path_or_pattern] )) ||
  1201. __borg_choose_path_or_pattern() {
  1202. local ss=$1 defss=$2 cword=$3
  1203. shift 2
  1204. [[ $ss == (pp|pf) || ( -z $ss && $defss == (pp|pf) ) ]]
  1205. }
  1206. # transform borg exclude patterns into zsh ignore patterns and then complete files
  1207. (( $+functions[__borg_pattern_files] )) ||
  1208. __borg_pattern_files() {
  1209. local -A opts
  1210. zparseopts -A opts -D -E e p f
  1211. local paths_varname=$1
  1212. shift
  1213. local -a args
  1214. local -A style_selectors
  1215. __borg_setup_style_selectors
  1216. local pr_pat='[RP\+\-\!]' ss_pat="(${(j:|:)${(@kb)style_selectors}}):"
  1217. local prs_pat="$pr_pat #"
  1218. if (( $+opts[-e] )); then
  1219. local -a borg_excludes exclude_options=( -e --exclude --pattern ) tmp
  1220. local k cword
  1221. local -i i
  1222. for k in $exclude_options; do
  1223. if [[ -n $opt_args[$k] ]]; then
  1224. IFS=: read -A tmp <<<$opt_args[$k]
  1225. tmp=( ${(Q)tmp} )
  1226. # lstrip style selectors and pattern rules
  1227. [[ $+opts[-p] -gt 0 || $k == --pattern ]] && tmp=( ${tmp#$~prs_pat} )
  1228. tmp=( ${tmp#$~ss_pat} )
  1229. # don't take into account the word under the cursor
  1230. cword="$PREFIX$SUFFIX"
  1231. [[ $compstate[quote] != (\'|\") ]] && cword=${(Q)cword}
  1232. [[ $+opts[-p] -gt 0 || $k == --pattern ]] && cword=${cword#$~prs_pat}
  1233. cword=${cword#$~ss_pat}
  1234. i=$tmp[(I)$cword]
  1235. (( i )) && tmp=( "${(@)tmp[1,i-1]}" "${(@)tmp[i+1,-1]}" )
  1236. borg_excludes+=( "$tmp[@]" )
  1237. fi
  1238. done
  1239. [[ -n $borg_excludes ]] && args+=( -F borg_excludes )
  1240. fi
  1241. [[ -n ${(P)paths_varname} ]] && args+=( -W $paths_varname )
  1242. args+=( "$@" )
  1243. # lstrip style selectors and pattern rules
  1244. if (( $+opts[-p] )); then
  1245. if [[ $compstate[quote] != (\'|\") ]]; then
  1246. compset -P $pr_pat
  1247. compset -P '(\\ )#'
  1248. else
  1249. compset -P $prs_pat
  1250. fi
  1251. fi
  1252. compset -P $ss_pat
  1253. compstate[insert]=''
  1254. compstate[list]='list force'
  1255. _path_files "$args[@]"
  1256. }
  1257. (( $+functions[__borg_setup_style_selectors] )) ||
  1258. __borg_setup_style_selectors() {
  1259. typeset -gA style_selectors=(
  1260. fm 'Fnmatch'
  1261. sh 'Shell-style patterns'
  1262. re 'Regular expressions'
  1263. pp 'Path prefix'
  1264. pf 'Path full-match'
  1265. )
  1266. }
  1267. (( $+functions[__borg_skip_pattern_matching] )) ||
  1268. __borg_skip_pattern_matching() {
  1269. # unset glob_complete
  1270. [[ $compstate[pattern_match] == '*' ]] && compstate[pattern_match]=''
  1271. # skip the _match completer
  1272. [[ -n $compstate[pattern_match] ]] && return 1
  1273. return 0
  1274. }
  1275. (( $+functions[_borg_config] )) ||
  1276. _borg_config() {
  1277. local qrepo=$1
  1278. shift
  1279. if (( ! $+__borg_config_sect )); then
  1280. comppostfuncs+=( __borg_unset_config )
  1281. typeset -gH __borg_config_sect=
  1282. typeset -gHa __borg_config_keys=()
  1283. local sect line
  1284. local -a match mbegin mend
  1285. _call_program -p keys ${(q)__borg_command:-borg} config --list $qrepo 2>/dev/null | {
  1286. IFS= read -r sect
  1287. sect=${${sect#\[}%\]}
  1288. __borg_config_sect=$sect
  1289. while IFS= read -r line && [[ $line == (#b)(*)\ =\ (*) ]]; do
  1290. __borg_config_keys+=( "${${match[1]//\\/\\\\}//:/\\:}:(current: $match[2])" )
  1291. done
  1292. }
  1293. fi
  1294. local -a alts=( 'keys:key: _describe -t keys "key" __borg_config_keys' )
  1295. compset -P "${__borg_config_sect}." || alts+=( 'sections:section:compadd -S "." $__borg_config_sect' )
  1296. _alternative $alts
  1297. }
  1298. (( $+functions[__borg_unset_config] )) ||
  1299. __borg_unset_config() {
  1300. unset __borg_config_sect __borg_config_keys
  1301. }
  1302. # A simple prefix-oriented completion function for compressors. Can be improved by supporting the suffix.
  1303. (( $+functions[_borg_compression] )) ||
  1304. _borg_compression() {
  1305. local -a nolvl=(
  1306. 'none:do not compress'
  1307. 'lz4:very high speed, very low compression'
  1308. )
  1309. local -a havelvl=(
  1310. 'zstd:("zstandart")'
  1311. 'zlib:("gz") medium speed, medium compression'
  1312. 'lzma:("xz") low speed, high compression'
  1313. )
  1314. local -a auto=(
  1315. 'auto:compress compressible, otherwise "none"'
  1316. )
  1317. local -a match mbegin mend
  1318. # NOTE: Zsh's `-prefix` condition is confused by the leading parenthesis in the pattern.
  1319. # Fortunately, we simply need to show a message.
  1320. if compset -P '(#b)(|auto,)(zstd|zlib|lzma),'; then
  1321. local -i from to def
  1322. case $match[2] in
  1323. (zstd) from=1 to=22 def=3 ;;
  1324. (zlib|lzma) from=0 to=9 def=6 ;;
  1325. esac
  1326. _message -e "compression level (from $from to $to, default: $def)"
  1327. elif compset -P 'auto,'; then
  1328. _describe -t compression 'compression' nolvl -- havelvl -qS,
  1329. else
  1330. _describe -t compression 'compression' nolvl -- havelvl -qS, -- auto -S,
  1331. fi
  1332. }
  1333. (( $+functions[_borg_chunker_params] )) ||
  1334. _borg_chunker_params() {
  1335. if compset -P '*,*,*,'; then
  1336. _message -e 'HASH_WINDOW_SIZE'
  1337. elif compset -P '*,*,'; then
  1338. _message -e 'HASH_MASK_BITS (target chunk size ~= 2^HASH_MASK_BITS B)'
  1339. elif compset -P '*,'; then
  1340. _message -e 'CHUNK_MAX_EXP (maximum chunk size = 2^CHUNK_MAX_EXP B)'
  1341. else
  1342. _message -e 'CHUNK_MIN_EXP (minimum chunk size = 2^CHUNK_MIN_EXP B)'
  1343. local -a params=(
  1344. 'default:19,23,21,4095'
  1345. '19,23,21,4095:small amount of chunks (default)'
  1346. '10,23,16,4095:big amount of chunks'
  1347. )
  1348. _describe -t chunker-params 'typical chunker params' params
  1349. fi
  1350. }
  1351. (( $+functions[_borg_statuschars] )) ||
  1352. _borg_statuschars() {
  1353. _values -s '' 'STATUSCHARS' \
  1354. 'A[regular file, added]' \
  1355. 'M[regular file, modified]' \
  1356. 'U[regular file, unchanged]' \
  1357. 'E[regular file, an error happened while accessing/reading this file]' \
  1358. 'd[directory]' \
  1359. 'b[block device]' \
  1360. 'c[char device]' \
  1361. 'h[regular file, hardlink (to already seen inodes)]' \
  1362. 's[symlink]' \
  1363. 'f[fifo]' \
  1364. 'i[backup data was read from standard input (stdin)]' \
  1365. '-[dry run, item was not backed up]' \
  1366. 'x[excluded, item was not backed up]' \
  1367. '?[missing status code]'
  1368. }
  1369. (( $+functions[_borg_quota_suffixes] )) ||
  1370. _borg_quota_suffixes() {
  1371. if compset -P '[0-9]##'; then
  1372. local -a suffixes=(
  1373. 'K:10 ** 3 bytes'
  1374. 'M:10 ** 6 bytes'
  1375. 'G:10 ** 9 bytes'
  1376. 'T:10 ** 12 bytes'
  1377. 'P:10 ** 15 bytes'
  1378. )
  1379. # NOTE: tag `suffixes` is already in use (file extensions)
  1380. _describe -t multiplier 'suffix' suffixes
  1381. else
  1382. _message -e 'QUOTA'
  1383. fi
  1384. }
  1385. (( $+functions[_borg_timestamp] )) ||
  1386. _borg_timestamp() {
  1387. _alternative \
  1388. "dates:TIMESTAMP: _dates -f '%FT%T'" \
  1389. 'files:reference:_files'
  1390. }
  1391. (( $+functions[_borg_guard_unsigned_number] )) ||
  1392. _borg_guard_unsigned_number() {
  1393. local -A opts
  1394. zparseopts -K -D -A opts M+: J+: V+: 1 2 o+: n F: x+: X+:
  1395. _guard '[0-9]#' ${1:-number}
  1396. }
  1397. (( $+functions[_borg_guard_numeric_mode] )) ||
  1398. _borg_guard_numeric_mode() {
  1399. local -A opts
  1400. zparseopts -K -D -A opts M+: J+: V+: 1 2 o+: n F: x+: X+:
  1401. _guard '[0-7](#c0,4)' ${1:-mode}
  1402. }
  1403. _borg() {
  1404. local -a match mbegin mend line state
  1405. local curcontext="$curcontext" state_descr
  1406. typeset -A opt_args
  1407. local -i ret=1
  1408. if [[ $service == 'borg' ]]; then
  1409. local __borg_command=$words[1]
  1410. local -a common_options
  1411. __borg_setup_common_options
  1412. _arguments -s -w -C : \
  1413. '(- :)'{-V,--version}'[show version number and exit]' \
  1414. $common_options \
  1415. '(-): :->command' \
  1416. '(-)*:: :->option-or-argument' && return
  1417. case $state in
  1418. (command)
  1419. _borg_commands && ret=0
  1420. ;;
  1421. (option-or-argument)
  1422. curcontext="${curcontext%:*:*}:borg-$words[1]:"
  1423. if ! _call_function ret _borg-$words[1]; then
  1424. _default && ret=0
  1425. fi
  1426. ;;
  1427. esac
  1428. elif [[ $service == (#b)-value-,BORG_(*),-default- ]]; then
  1429. _borg_parameters $match[1] && ret=0
  1430. elif ! _call_function ret _$service; then
  1431. _default && ret=0
  1432. fi
  1433. return ret
  1434. }
  1435. _borg "$@"