diff --git a/install.sh b/install.sh index 941d90c..38a7fbe 100644 --- a/install.sh +++ b/install.sh @@ -5,15 +5,16 @@ # # Configurable via env: # REPO_URL git url (default: this public repo) -# REF branch/tag/commit to check out (default: main) +# REF branch/tag/commit (default: remote's HEAD branch, e.g. main/master) # DEST clone destination (default: /opt/linux-bootstrap) # Any extra args are forwarded to bootstrap.sh, e.g.: # curl -fsSL .../install.sh | bash -s -- --skip hardening # set -Eeuo pipefail export DEBIAN_FRONTEND=noninteractive + REPO_URL="${REPO_URL:-https://gitea.big-chungus.me/moritz/linux-bootstrap.git}" -REF="${REF:-main}" +REF="${REF:-}" # empty -> auto-detect remote default branch DEST="${DEST:-/opt/linux-bootstrap}" say() { printf '\e[34m[install]\e[0m %s\n' "$*"; } @@ -40,19 +41,32 @@ if ! command -v git >/dev/null 2>&1; then fi fi -# 2. clone or update -if [[ -d "$DEST/.git" ]]; then - say "updating existing clone at $DEST" - $SUDO git -C "$DEST" fetch --depth=1 origin "$REF" - $SUDO git -C "$DEST" checkout -f "$REF" - $SUDO git -C "$DEST" reset --hard "origin/$REF" 2>/dev/null || true -else - say "cloning $REPO_URL -> $DEST" - $SUDO git clone --depth=1 --branch "$REF" "$REPO_URL" "$DEST" 2>/dev/null \ - || $SUDO git clone --depth=1 "$REPO_URL" "$DEST" +# 2. resolve ref: explicit env wins; otherwise ask the remote what its default +# branch is (handles main vs master without guessing). +if [[ -z "$REF" ]]; then + REF="$(git ls-remote --symref "$REPO_URL" HEAD 2>/dev/null \ + | awk '/^ref:/ {sub("refs/heads/","",$2); print $2; exit}')" + if [[ -z "$REF" ]]; then + say "could not detect default branch, falling back to 'main'" + REF="main" + else + say "remote default branch: $REF" + fi fi -# 3. run bootstrap, forwarding any extra args +# 3. clone or update +if [[ -d "$DEST/.git" ]]; then + say "updating existing clone at $DEST ($REF)" + $SUDO git -C "$DEST" remote set-url origin "$REPO_URL" + $SUDO git -C "$DEST" fetch --depth=1 origin "$REF" + $SUDO git -C "$DEST" checkout -B "$REF" "origin/$REF" + $SUDO git -C "$DEST" reset --hard "origin/$REF" +else + say "cloning $REPO_URL ($REF) -> $DEST" + $SUDO git clone --depth=1 --branch "$REF" "$REPO_URL" "$DEST" +fi + +# 4. run bootstrap, forwarding any extra args say "running bootstrap.sh $*" $SUDO chmod +x "$DEST/bootstrap.sh" "$DEST/config/motd/01-banner.sh" exec "$DEST/bootstrap.sh" "$@"