From 61b7d11977b96c9aceb6a595daffee81bfd7e5ef Mon Sep 17 00:00:00 2001 From: Dict Xiong Date: Fri, 29 Jul 2022 00:52:21 +0800 Subject: [PATCH] use update.sh to update safely --- .update.sh | 25 +++++++++++++++++++++++++ .zshrc2 | 2 +- install.sh | 11 ++++++++++- 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 .update.sh diff --git a/.update.sh b/.update.sh new file mode 100644 index 0000000..3439a8c --- /dev/null +++ b/.update.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +export DOTFILES=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd ) +# get the specified commit id +dfs_commit=$(curl -fsSL https://api.beardic.cn/get-dfs-commit) +if [[ ${#dfs_commit} != 40 ]]; then + echo "Error: invalid commit id." + exit +fi +# fetch origin +cd $DOTFILES +git fetch +if [[ -n "$(git status -s)" ]]; then + echo "Error: directory not clean." + exit +fi +# update +if [[ "$(git rev-parse HEAD)" == "$dfs_commit" ]]; then + echo "Nothing to do." +else + echo "Checking out to commit $dfs_commit ..." + git checkout $dfs_commit + cp ./.update.sh ./update.sh + chmod +x ./update.sh +fi diff --git a/.zshrc2 b/.zshrc2 index 7932159..eca7bae 100644 --- a/.zshrc2 +++ b/.zshrc2 @@ -80,7 +80,7 @@ fi dfs() { case $1 in - update ) (cd "$DOTFILES" && git pull) ;; + update ) $DOTFILES/update.sh ;; force-update ) (cd "$DOTFILES" && git fetch --all && git reset --hard origin/main && git pull) ;; reset ) antigen reset 1> /dev/null diff --git a/install.sh b/install.sh index dccd4fe..c8c682f 100755 --- a/install.sh +++ b/install.sh @@ -96,7 +96,7 @@ if [[ ! $dotfile_path == ${home_slashes}* ]]; then fi dotfile_home_path=${dotfile_path/${home_slashes}/\~} dotfile_relative_path=${dotfile_path#${home_slashes}\/} -crontab_job="0 * * * * cd ${dotfile_path} && env git pull" +crontab_job="0 * * * * ${dotfile_path}/update.sh" ask_for_yN() { @@ -209,7 +209,16 @@ install_vim_vundle(){ fi } +install_update(){ + fmt_note "installing update.sh ..." + cp "${dotfile_path}/.update.sh" "${dotfile_path}/update.sh" + chmod +x "${dotfile_path}/update.sh" + fmt_note "running update.sh ..." + ${dotfile_path}/update.sh +} + install(){ + install_update install_crontab insert_if_not_exist "${HOME}/.zshrc" "source ${dotfile_home_path}/.zshrc2" insert_if_not_exist "${HOME}/.tmux.conf" "source-file ${dotfile_home_path}/.tmux.conf2"