diff --git a/home-manager/services/dotfiles-updater/update.sh b/home-manager/services/dotfiles-updater/update.sh index d2d4705c4..38042f8e7 100755 --- a/home-manager/services/dotfiles-updater/update.sh +++ b/home-manager/services/dotfiles-updater/update.sh @@ -10,8 +10,24 @@ if [ "$(git rev-parse --abbrev-ref HEAD)" != "main" ]; then exit 0 fi -# Fetch and pull latest changes +# Store current commit hash before fetching +CURRENT_COMMIT=$(git rev-parse HEAD) + +# Fetch latest changes git fetch origin main + +# Get the latest remote commit +REMOTE_COMMIT=$(git rev-parse origin/main) + +# Check if there are any changes +if [ "$CURRENT_COMMIT" = "$REMOTE_COMMIT" ]; then + echo "No changes detected (current: ${CURRENT_COMMIT:0:8}). Skipping build and switch." + exit 0 +fi + +echo "Changes detected: ${CURRENT_COMMIT:0:8} -> ${REMOTE_COMMIT:0:8}" + +# Reset to latest git reset --hard origin/main # Run the install script diff --git a/spec/dotfiles_updater_spec.sh b/spec/dotfiles_updater_spec.sh index 27353836c..2f3d6eddc 100644 --- a/spec/dotfiles_updater_spec.sh +++ b/spec/dotfiles_updater_spec.sh @@ -50,6 +50,36 @@ The output should include 'git reset --hard origin/main' End End +Describe 'change detection' +It 'stores current commit before fetching' +When run bash -c "grep 'CURRENT_COMMIT=\$(git rev-parse HEAD)' '$SCRIPT'" +# shellcheck disable=SC2016 +The output should include 'CURRENT_COMMIT=$(git rev-parse HEAD)' +End + +It 'gets remote commit after fetching' +When run bash -c "grep 'REMOTE_COMMIT=\$(git rev-parse origin/main)' '$SCRIPT'" +# shellcheck disable=SC2016 +The output should include 'REMOTE_COMMIT=$(git rev-parse origin/main)' +End + +It 'compares commits to detect changes' +When run bash -c "grep 'CURRENT_COMMIT.*=.*REMOTE_COMMIT' '$SCRIPT'" +The output should include 'CURRENT_COMMIT' +The output should include 'REMOTE_COMMIT' +End + +It 'skips build when no changes detected' +When run bash -c "grep 'No changes detected' '$SCRIPT'" +The output should include 'No changes detected' +End + +It 'logs when changes are detected' +When run bash -c "grep 'Changes detected' '$SCRIPT'" +The output should include 'Changes detected' +End +End + Describe 'installation' It 'runs install.sh after update' When run bash -c "grep './install.sh' '$SCRIPT'"