Watch
1
0
Fork
You've already forked dotfiles
0
dotfiles/.local/bin/update-dotfiles

83 lines
2 KiB
Text
Raw Normal View History

2026-08-14 16:37:54 +02:00
#!/bin/sh
# Replace the local `dotfiles` repository with a fresh install
# by downloading and re-running the latest `install-dotfiles`
#
# 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}" # also set in ~/.config/shell/env
DOTFILES_DIR="$XDG_DATA_HOME/dotfiles" # also set in ~/.config/shell/env
INSTALLER_URL="https://code.webartifex.de/alexander/dotfiles/raw/branch/main/.local/bin/install-dotfiles"
force=0
[ "${1:-}" = "--force" ] && force=1
_git() {
git --git-dir="$DOTFILES_DIR" --work-tree="$HOME" "$@"
}
branch="main"
if [ -d "$DOTFILES_DIR" ]; then
# Keep the machine on its current branch (e.g., "desktop")
branch=$(_git symbolic-ref --short HEAD)
if [ "$force" -ne 1 ]; then
if [ -n "$(_git status --porcelain)" ]; then
echo ""
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 ""
exit 1
fi
if _git rev-parse --verify --quiet "origin/$branch" >/dev/null \
&& [ -n "$(_git log --oneline "origin/$branch..$branch")" ]; then
echo ""
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 ""
exit 1
fi
fi
fi
installer=$(mktemp)
trap 'rm -f "$installer"' EXIT
if command -v curl >/dev/null 2>&1; then
curl -fsSL "$INSTALLER_URL" -o "$installer"
elif command -v wget >/dev/null 2>&1; then
wget -q "$INSTALLER_URL" -O "$installer"
else
echo ""
echo "Neither curl nor wget are installed!" >&2
echo "=> Install one of them" >&2
echo ""
exit 1
fi
DOTFILES_BRANCH="$branch" FORCE_INSTALL=1 sh "$installer"