_borg 54 KB

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