Add base configuration
- Follow XDG standard: ~/.config and ~/.local folders - Set environment variables and define aliases within ~/.config/shell - Add installation script for easy setup and maintenance (i.e., `dotfiles-install` and `dotfiles-update`) - Add README.md with info on the installation and general notes
This commit is contained in:
commit
a89c4955c5
13 changed files with 583 additions and 0 deletions
3
.local/bin/README.md
Normal file
3
.local/bin/README.md
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# User-local Executables
|
||||
|
||||
This folder contains executable files that are on the `$PATH`.
|
||||
146
.local/bin/dotfiles-install
Executable file
146
.local/bin/dotfiles-install
Executable file
|
|
@ -0,0 +1,146 @@
|
|||
#!/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
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
# 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 ""
|
||||
116
.local/bin/dotfiles-update
Executable file
116
.local/bin/dotfiles-update
Executable file
|
|
@ -0,0 +1,116 @@
|
|||
#!/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 <branch>'" >&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
|
||||
Loading…
Reference in a new issue