[O] Install and update script (#4)

This commit is contained in:
2026-05-03 13:20:22 -04:00
committed by GitHub
parent 8334026e89
commit d39f910fae
2 changed files with 54 additions and 19 deletions
+2 -8
View File
@@ -7,8 +7,8 @@ if [ -d "zshrc" ]; then
exit -1
fi
# Clone repo
git clone "https://github.com/hykilpikonna/zshrc"
# Clone repo without downloading old history.
git clone --depth 1 --recurse-submodules --shallow-submodules "https://github.com/hykilpikonna/zshrc"
# Addline function: add a line to a file if the line doesn't already exist
addline() {
@@ -19,12 +19,6 @@ addline() {
addline .zshrc 'SCR="$HOME/zshrc/scripts"'
addline .zshrc '. $SCR/zshrc.sh'
# Initialize submodules
pushd zshrc
git submodule init
git submodule update
popd
# Return to the previous directory
popd
+48 -7
View File
@@ -1,22 +1,63 @@
cd $SCR
cd "$SCR" || return
prefix="&7[&3zshrc&7]"
remote_ref="${ZSHRC_UPDATE_REF:-origin/master}"
_zshrc_stash_created=0
_zshrc_stash_if_needed() {
if ! git diff --quiet --ignore-submodules -- \
|| ! git diff --cached --quiet --ignore-submodules -- \
|| [[ -n "$(git ls-files --others --exclude-standard)" ]]; then
if git stash push -u -m "zshrc auto-update before applying $remote_ref" >/dev/null; then
_zshrc_stash_created=1
fi
fi
}
_zshrc_restore_stash() {
if [[ "$_zshrc_stash_created" == "1" ]]; then
git stash pop >/dev/null || color "$prefix &cUpdated, but saved local changes need manual conflict resolution."
fi
}
# Check for updates
git fetch origin --quiet
reslog=$(git log HEAD..origin/master --oneline)
if [[ "${reslog}" != "" ]] ; then
if git fetch origin --quiet && git rev-parse --verify --quiet "$remote_ref" >/dev/null; then
# Handle rewritten or force-pushed history. This keeps auto-update working
# after repository cleanup that removes old large objects.
if ! git merge-base --is-ancestor HEAD "$remote_ref" 2>/dev/null; then
color "$prefix &cRepo history changed. Resetting local zshrc to $remote_ref..."
_zshrc_stash_if_needed
if git reset --hard "$remote_ref" && git submodule update --init --recursive --depth 1; then
_zshrc_restore_stash
. "$SCR/zshrc.sh"
color "$prefix &aUpdated after history rewrite!"
else
color "$prefix &cUpdate failed!"
fi
else
reslog=$(git log HEAD.."$remote_ref" --oneline)
if [[ "${reslog}" != "" ]] ; then
# Has updates
color "$prefix &cYour zshrc is outdated. Automatically updating..."
# Try to pull
if git stash & git pull & git stash pop; then
. $SCR/zshrc.sh
# Try to fast-forward without invoking git pull's merge/rebase behavior.
_zshrc_stash_if_needed
if git merge --ff-only "$remote_ref" && git submodule update --init --recursive --depth 1; then
_zshrc_restore_stash
. "$SCR/zshrc.sh"
color "$prefix &aUpdated!"
else
color "$prefix &cUpdate failed!"
fi
fi
fi
elif [[ -n "$ZSHRC_UPDATE_VERBOSE" ]]; then
color "$prefix &cUpdate check failed!"
fi
unset -f _zshrc_stash_if_needed _zshrc_restore_stash
unset _zshrc_stash_created remote_ref reslog
cd - &> /dev/null