_borg 53 KB

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