Skip to content

Instantly share code, notes, and snippets.

@ntpeters
Last active January 2, 2020 09:12
Dotfile Status Script
#!/usr/bin/env/bash
# This is a simple script to check the current local/remote status of your
# dotfiles, and prompt to update them if needed.
# This script uses updot (https://github.com/ntpeters/updot) for syncing
# dotfiles, but could easily be updated to call anything else.
#
# The core portion of this script is taken from a general solution for checking
# the status of local/remote git repos posted by Neil Mayhew on StackOverflow:
# http://stackoverflow.com/a/3278427/1428743
# Checks the status of local and remote dotfiles repos to determine if they
# need to be synced up.
function check_dotfiles_status() {
# Retrieve hashes from git
local LOCAL=$(git rev-parse @)
local REMOTE=$(git rev-parse @{u})
local BASE=$(git merge-base @ @{u})
# Flag to determine if an update may need to be done
local update=true
# Determine status of dotfiles
if [ -z $LOCAL ] || [ -z $REMOTE ] || [ -z $BASE ]; then
echo "ERROR: Failed to get status of dotfiles!"
update=false
elif [ $LOCAL = $REMOTE ]; then
echo "Dotfiles up-to-date."
update=false
elif [ $LOCAL = $BASE ]; then
echo "Remote dotfiles have changed."
elif [ $REMOTE = $BASE ]; then
echo "Local dotfiles have changed."
else
echo "WARNING: Local and remote dotfiles have diverged. You may experience merge conflicts!"
fi
# See if the user wants to sync dotfiles
if $update; then
read -p "Sync dotfiles now? [y/n] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo
# Ensure updot is in our path
if ! updot_loc="$(type -p "updot")" || [ -z "$updot_loc"]; then
updot
else
echo "ERROR: updot not found! Aborting sync!"
fi
fi
fi
}
# Ensure a path to the local dotfiles repo has been provided
if [ -z ${LOCAL_DOTFILES_REPOSITORY+x} ]; then
echo "ERROR: Must set the 'LOCAL_DOTFILES_REPOSITORY' variable with path to your dotfiles directory!"
else
# Ensure path to dotfiles repo has expanded tilde for home dir
# Not sure how portable this method is...
local_dotfiles_repo=$(eval "echo $LOCAL_DOTFILES_REPOSITORY")
# Check if dotfiles need to be updated
( cd ${local_dotfiles_repo} && check_dotfiles_status )
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment