_borg 54 KB

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