dotfiles/.local/bin/dotfiles-install

152 lines
3.8 KiB
Text
Raw Normal View History

#!/bin/sh
# Installation script to make the dotfiles available in ~/
#
# `git` is the only dependency for this script to run (See: https://git-scm.com)
#
# See: https://code.webartifex.de/alexander/dotfiles#installation
set -eu
2026-08-03 02:57:48 +02:00
XDG_BIN_HOME="${XDG_BIN_HOME:-$HOME/.local/bin}"
XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
XDG_STATE_HOME="${XDG_STATE_HOME:-$HOME/.local/state}"
# Each branch's first commit sets the default to its own name
# => Overridable via the environment (e.g., done by `dotfiles-update`)
DOTFILES_BRANCH="${DOTFILES_BRANCH:-main}"
DOTFILES_DIR="${DOTFILES_DIR:-$XDG_DATA_HOME/dotfiles}"
DOTFILES_HTTPS="${DOTFILES_HTTPS:-https://code.webartifex.de/alexander/dotfiles}"
BACKUPS_DIR="$XDG_STATE_HOME/dotfiles-backups"
BACKUP_DIR="$BACKUPS_DIR/$(date +%Y%m%d-%H%M%S)"
_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
if [ "$_force" -ne 1 ]; then
echo ""
echo "The dotfiles are already installed at: $DOTFILES_DIR"
echo ""
echo "Options:"
echo " - Run the installation again with '--force'"
echo " - Update with 'dotfiles pull' and continue using them"
echo ""
exit 0
fi
echo "Backing up the existing repository to: $BACKUP_DIR"
mkdir -p "$BACKUP_DIR"
mv "$DOTFILES_DIR" "$BACKUP_DIR/dotfiles"
fi
git clone --bare "$DOTFILES_HTTPS" "$DOTFILES_DIR"
_git() {
git --git-dir="$DOTFILES_DIR" --work-tree="$HOME" "$@"
}
if ! _git show-ref --verify --quiet "refs/heads/$DOTFILES_BRANCH"; then
echo "" >&2
echo "Internal error: branch '$DOTFILES_BRANCH' missing in the repository" >&2
echo "" >&2
echo "Available branches:" >&2
_git branch --format=' %(refname:short)' >&2
echo "" >&2
exit 1
fi
_git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
_git fetch origin
# Do not checkout project documentation intended for web GUIs
#
# Note: The patterns match at every depth, like in a .gitignore,
# which excludes the per-folder README.md files as well
_git config core.sparseCheckout true
{
echo "/*"
echo "!LICENSE.txt"
echo "!README.md"
} > "$DOTFILES_DIR/info/sparse-checkout"
# Back up files in the user's $HOME folder if a checkout is about to overwrite them
#
# Note: This may also back up local-only files marked sparse in the repo,
# which is not considered a big problem because it's very unlikely
_conflicts=$(_git ls-tree -r --name-only "$DOTFILES_BRANCH" | while IFS= read -r f; do
if [ -e "$HOME/$f" ] || [ -L "$HOME/$f" ]; then echo "$f"; fi
done)
if [ -n "$_conflicts" ]; then
echo "Backing up pre-existing files to: $BACKUP_DIR"
echo "$_conflicts" | while IFS= read -r f; do
mkdir -p "$BACKUP_DIR/$(dirname "$f")"
cp -a "$HOME/$f" "$BACKUP_DIR/$f"
done
fi
# Put the dotfiles in the user's $HOME folder
_git checkout --force "$DOTFILES_BRANCH"
# Do not show files not tracked in the dotfiles repository because there are simply too many
_git config --local status.showUntrackedFiles no
2026-08-03 02:57:48 +02:00
if command -v batcat >/dev/null 2>&1 && ! command -v bat >/dev/null 2>&1; then
ln -sf "$(command -v batcat)" "$XDG_BIN_HOME/bat"
fi
# Keep only the most recent backups
_keep=10
if [ -d "$BACKUPS_DIR" ]; then
# `head -n -N` is a GNU extension; fine on Debian, fails on, e.g., BSD/BusyBox
find "$BACKUPS_DIR" -mindepth 1 -maxdepth 1 -type d | sort | head -n -"$_keep" \
| while IFS= read -r _old; do
rm -f -r "$_old"
done
fi
echo ""
echo "The dotfiles were installed successfully (branch: $DOTFILES_BRANCH)"
echo ""
echo "Reload your shell to use them:"
echo " exec \$SHELL -l"
echo ""