dotfiles/.update.sh

55 lines
1.8 KiB
Bash
Raw Normal View History

#!/bin/bash
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
THIS_FILE=$(basename "${BASH_SOURCE}")
source "$THIS_DIR/tools/common.sh"
DFS_UPDATED_RET=${DFS_UPDATED_RET:-0}
DFS_UPDATE_CHANNEL=${DFS_UPDATE_CHANNEL:-"main"}
2023-01-21 15:28:55 +08:00
# send beacon online
apost_beacon "sys.online"
2023-03-01 01:28:53 +08:00
# update dns
2023-03-01 16:37:42 +08:00
if [[ "$DFS_DDNS_ENABLE" == "1" ]]; then
2023-03-01 01:28:53 +08:00
fmt_info "updating dns ..."
"$THIS_DIR/tools/frigg-client.sh" ddns || (fmt_error "failed to update dns" && apost_beacon "dfs.ddns.fail")
2023-03-01 01:28:53 +08:00
fi
# fetch origin
cd $DOTFILES
git fetch --all --prune
if [[ -n "$(git status -s)" ]]; then
fmt_error "directory not clean"
apost_beacon "dfs.dirty"
exit
fi
# get the specified commit id
case $DFS_UPDATE_CHANNEL in
2023-03-14 20:07:34 +08:00
"main" ) DFS_COMMIT=$(curl $DFS_CURL_OPTIONS -fsSL https://api.beardic.cn/get-var/dfs-commit-id) ;;
"dev" ) DFS_COMMIT=$(git rev-parse origin/dev 2> /dev/null) || DFS_COMMIT=$(git rev-parse origin/main) ;;
"latest" ) DFS_COMMIT=$(git for-each-ref --sort=-committerdate refs/heads refs/remotes --format='%(objectname)' | head -n 1) ;;
* ) fmt_fatal "invalid update channel: $DFS_UPDATE_CHANNEL" ;;
esac
if [[ ${#DFS_COMMIT} != 40 ]]; then
fmt_error "invalid commit id"
apost_beacon "dfs.invalid-commit"
apost_log "ERROR" "$THIS_FILE" "invalid commit id: ${DFS_COMMIT}"
exit
fi
# update
if [[ "$(git rev-parse HEAD)" == "$DFS_COMMIT" ]]; then
fmt_info "nothing to do"
else
fmt_info "checking out to commit $DFS_COMMIT ..."
if [[ -z "$DFS_DEV" || "$DFS_DEV" == "0" ]]; then
post_beacon "dfs.updated"
git -c advice.detachedHead=false checkout $DFS_COMMIT
cp ./.update.sh ./update.sh && chmod +x ./update.sh && exit $DFS_UPDATED_RET
else
fmt_warning "won't really checkout in dev mode"
fi
fi