diff --git a/fastinstall.sh b/fastinstall.sh index da998ed..00b35ba 100644 --- a/fastinstall.sh +++ b/fastinstall.sh @@ -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 diff --git a/scripts/includes/init/update.sh b/scripts/includes/init/update.sh index 10ad689..ddd97be 100644 --- a/scripts/includes/init/update.sh +++ b/scripts/includes/init/update.sh @@ -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 - # Has updates - color "$prefix &cYour zshrc is outdated. Automatically updating..." + # 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..." - # Try to pull - if git stash & git pull & git stash pop; then - . $SCR/zshrc.sh - color "$prefix &aUpdated!" + _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 - color "$prefix &cUpdate failed!" + reslog=$(git log HEAD.."$remote_ref" --oneline) + if [[ "${reslog}" != "" ]] ; then + + # Has updates + color "$prefix &cYour zshrc is outdated. Automatically updating..." + + # 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