Commit 2c2f91c92d42beda4c872eb839b60ab9f02b6230

Authored by Shrikant Sharat
1 parent d36c6f93d8

Added `antigen-restore` command.

This command is used to restore from a snapshot file provided as an argument.
The snapshot file can be generated by the `antigen-snapshot` command.

Showing 2 changed files with 55 additions and 5 deletions Side-by-side Diff

... ... @@ -358,9 +358,9 @@ antigen-snapshot () {
358 358 # <version-hash> <repo-url>
359 359 local snapshot_content="$(-antigen-echo-record | sed 's/ .*$//' | sort -u |
360 360 while read url; do
361   - dir="$(-antigen-get-clone-dir "$url")"
362   - version_hash="$(cd "$dir" && git rev-parse HEAD)"
363   - echo "$version_hash $dir"
  361 + local dir="$(-antigen-get-clone-dir "$url")"
  362 + local version_hash="$(cd "$dir" && git rev-parse HEAD)"
  363 + echo "$version_hash $url"
364 364 done)"
365 365  
366 366 {
... ... @@ -386,6 +386,40 @@ antigen-snapshot () {
386 386  
387 387 }
388 388  
  389 +antigen-restore () {
  390 +
  391 + if [[ $# == 0 ]]; then
  392 + echo 'Please provide a snapshot file to restore from.' >&2
  393 + return 1
  394 + fi
  395 +
  396 + local snapshot_file="$1"
  397 +
  398 + # TODO: Before doing anything with the snapshot file, verify its checksum.
  399 + # If it fails, notify this to the user and confirm if restore should
  400 + # proceed.
  401 +
  402 + echo -n "Restoring from $snapshot_file..."
  403 +
  404 + sed -n '1!p' "$snapshot_file" |
  405 + while read line; do
  406 +
  407 + local version_hash="${line%% *}"
  408 + local url="${line##* }"
  409 + local clone_dir="$(-antigen-get-clone-dir "$url")"
  410 +
  411 + if [[ ! -d $clone_dir ]]; then
  412 + git clone "$url" "$clone_dir" > /dev/null
  413 + fi
  414 +
  415 + (cd "$clone_dir" && git checkout $version_hash) 2> /dev/null
  416 +
  417 + done
  418 +
  419 + echo ' done.'
  420 + echo 'Please open a new shell to get the restored changes.'
  421 +}
  422 +
389 423 antigen-help () {
390 424 cat <<EOF
391 425 Antigen is a plugin management system for zsh. It makes it easy to grab awesome
... ... @@ -18,5 +18,21 @@ See the contents of the snapshot file.
18 18  
19 19 $ cat snapshot-file
20 20 version='1'; created_on='*'; checksum='*'; (glob)
21   - .{40} .*-test-plugin (re)
22   - .{40} .*-test-plugin2 (re)
  21 + .{40} .*/test-plugin (re)
  22 + .{40} .*/test-plugin2 (re)
  23 +
  24 +Reset the antigen's bundle record and run cleanup.
  25 +
  26 + $ unset _ANTIGEN_BUNDLE_RECORD
  27 + $ antigen-cleanup --force | grep '^Deleting' | wc -l
  28 + 2
  29 +
  30 +Restore from the snapshot.
  31 +
  32 + $ ls dot-antigen/repos | wc -l
  33 + 0
  34 + $ antigen-restore snapshot-file
  35 + Restoring from snapshot-file... done.
  36 + Please open a new shell to get the restored changes.
  37 + $ ls dot-antigen/repos | wc -l
  38 + 2