#!/bin/sh # Replace the local `dotfiles` repository with a fresh install # by re-running the latest `dotfiles-install` taken from the `origin` # # Refuses to run if there are uncommitted or unpushed changes, # unless called with `--force` set -eu XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}" DOTFILES_DIR="${DOTFILES_DIR:-$XDG_DATA_HOME/dotfiles}" _force=0 if [ "$#" -gt 1 ]; then echo "Usage: $(basename "$0") [--force]" >&2 exit 1 fi case "${1:-}" in "") ;; --force) _force=1 ;; *) echo "Usage: $(basename "$0") [--force]" >&2 exit 1 ;; esac if [ ! -d "$DOTFILES_DIR" ]; then echo "" >&2 echo "The dotfiles are not installed at: $DOTFILES_DIR" >&2 echo "" >&2 echo "=> See instructions at https://code.webartifex.de/alexander/dotfiles#installation" >&2 echo "" >&2 exit 1 fi _git() { git --git-dir="$DOTFILES_DIR" --work-tree="$HOME" "$@" } # Keep the machine on its current branch (e.g., "desktop") branch=$(_git symbolic-ref --short HEAD) || { echo "" >&2 echo "Detached HEAD => Re-run after 'dotfiles checkout '" >&2 echo "" >&2 exit 1 } if [ "$_force" -ne 1 ]; then if [ -n "$(_git status --porcelain)" ]; then echo "" >&2 echo "There are uncommitted changes!" >&2 echo "" >&2 _git --no-pager diff HEAD --stat >&2 echo "" >&2 echo "=> Commit and push them, or re-run with '--force'" >&2 echo "" >&2 exit 1 fi if _git rev-parse --verify --quiet "origin/$branch" >/dev/null \ && [ -n "$(_git log --oneline "origin/$branch..$branch")" ]; then echo "" >&2 echo "There are unpushed commits!" >&2 echo "" >&2 _git --no-pager log --oneline "origin/$branch..$branch" >&2 echo "" >&2 echo "=> Push them, or re-run with '--force'" >&2 echo "" >&2 exit 1 fi fi # Take the latest installer out of the local repository, # after fetching from the `origin` _git fetch --prune origin if ! _git rev-parse --verify --quiet "origin/$branch" >/dev/null; then echo "" >&2 echo "Branch '$branch' does not exist on origin!" >&2 echo "" >&2 echo "=> Push it first, or 'dotfiles checkout' one that does" >&2 echo "" >&2 exit 1 fi installer_path=$(mktemp) trap 'rm -f "$installer_path"' EXIT _git show "origin/$branch:.local/bin/dotfiles-install" > "$installer_path" DOTFILES_BRANCH="$branch" sh "$installer_path" --force