From 2c2f91c92d42beda4c872eb839b60ab9f02b6230 Mon Sep 17 00:00:00 2001 From: Shrikant Sharat Date: Fri, 21 Sep 2012 18:23:15 +0530 Subject: [PATCH] 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. --- antigen.zsh | 40 +++++++++++++++++++++++++++++++++++++--- tests/snapshots.t | 20 ++++++++++++++++++-- 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/antigen.zsh b/antigen.zsh index 87af785..fc4149b 100644 --- a/antigen.zsh +++ b/antigen.zsh @@ -358,9 +358,9 @@ antigen-snapshot () { # local snapshot_content="$(-antigen-echo-record | sed 's/ .*$//' | sort -u | while read url; do - dir="$(-antigen-get-clone-dir "$url")" - version_hash="$(cd "$dir" && git rev-parse HEAD)" - echo "$version_hash $dir" + local dir="$(-antigen-get-clone-dir "$url")" + local version_hash="$(cd "$dir" && git rev-parse HEAD)" + echo "$version_hash $url" done)" { @@ -386,6 +386,40 @@ antigen-snapshot () { } +antigen-restore () { + + if [[ $# == 0 ]]; then + echo 'Please provide a snapshot file to restore from.' >&2 + return 1 + fi + + local snapshot_file="$1" + + # TODO: Before doing anything with the snapshot file, verify its checksum. + # If it fails, notify this to the user and confirm if restore should + # proceed. + + echo -n "Restoring from $snapshot_file..." + + sed -n '1!p' "$snapshot_file" | + while read line; do + + local version_hash="${line%% *}" + local url="${line##* }" + local clone_dir="$(-antigen-get-clone-dir "$url")" + + if [[ ! -d $clone_dir ]]; then + git clone "$url" "$clone_dir" > /dev/null + fi + + (cd "$clone_dir" && git checkout $version_hash) 2> /dev/null + + done + + echo ' done.' + echo 'Please open a new shell to get the restored changes.' +} + antigen-help () { cat <