#!/bin/sh # Bring a fresh personal Linux machine to a plain dotfiles clone under /srv/git, # then hand off to its install-mise.sh. Served as https://bootstrap.sammohr.dev, # so it runs without a checkout or mise. The BOOTSTRAP_* overrides exist for the # test. set -eu REMOTE=${BOOTSTRAP_REMOTE:-https://github.com/smores56/dotfiles.git} ROOT=${BOOTSTRAP_ROOT:-/srv/git} die() { printf 'bootstrap: %s\n' "$*" >&2; exit 1; } log() { printf 'bootstrap: %s\n' "$*"; } [ "$(uname -s)" = Darwin ] && die "macOS keeps its clone under ~/.local/state; run its scripts/install-mise.sh instead" case $REMOTE in https://*) path=${REMOTE#https://} ;; *) die "expected an https remote, got: $REMOTE" ;; esac path=${path%.git} old_ifs=$IFS IFS=/ # shellcheck disable=SC2086 # splitting on / is the point set -- $path IFS=$old_ifs [ $# -eq 3 ] || die "expected host/owner/repo in the remote, got: $REMOTE" host=$1 owner=$2 name=$3 for part in "$host" "$owner" "$name"; do case $part in ''|.|..|*..*|-*) die "invalid path component in the remote: $part" ;; esac done case $host in *@*|*:*) die "credentials in the remote are not supported: $REMOTE" ;; esac repo=$ROOT/$host/$owner/$name if [ ! -d "$ROOT" ]; then command -v sudo >/dev/null 2>&1 || die "sudo is required to create $ROOT" sudo install -d -o "$(id -un)" -g "$(id -gn)" "$ROOT" || die "could not create $ROOT" elif [ ! -w "$ROOT" ]; then # A machine that provisions the root owns it (on smortress it is the # `raw/git` dataset). Take the root over only when creating it from # nothing; never adopt a tree someone else owns. die "$ROOT exists but is not writable; its owner provisions it" fi # A plain clone checks the default branch out at the repository root and fills # origin/* in the same pass, so main tracks origin/main with no follow-up. if [ ! -e "$repo/.git" ]; then log "cloning $REMOTE" git clone "$REMOTE" "$repo" || die "could not clone $REMOTE" fi git -C "$repo" rev-parse --is-bare-repository 2>/dev/null | grep -qx false \ || die "$repo is not a plain clone; remove the old checkout and re-run" git -C "$repo" fetch origin || die "could not fetch origin" # A force-push on origin (a rewritten history) leaves the checkout on the old # history, and mise's `--update` only fast-forwards. This script owns the # checkout, so converge it; uncommitted edits mean someone worked here by hand, # so stop rather than throw them away. git -C "$repo" diff-index --quiet HEAD -- \ || die "$repo has uncommitted changes; commit or discard them and re-run" git -C "$repo" merge-base --is-ancestor HEAD origin/main \ || log "replacing $repo's diverged history with origin/main" git -C "$repo" reset --hard origin/main >/dev/null \ || die "could not update $repo to origin/main" [ -x "$repo/scripts/install-mise.sh" ] \ || die "$repo has no scripts/install-mise.sh" log "running the checkout's installer" exec "$repo/scripts/install-mise.sh"