diff --git a/lib/clean-start.sh b/lib/clean-start.sh new file mode 100644 index 0000000..545035b --- /dev/null +++ b/lib/clean-start.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +# ============================================================================= +# lib/clean-start.sh — single source of truth for the pre-session reset. +# Caller must already be INSIDE the genome checkout. +# Aligns the working tree to origin/. Never force-pushes a shared branch. +# Tolerates a missing remote branch (first-setup scenario). +# NOTE: sourced library — no `set -euo pipefail` (would leak into the caller). +# ============================================================================= + +clean_start() { + local base="${INGEST_BASE:-main}" + git fetch -q origin || return 1 + git switch -q "$base" 2>/dev/null || git checkout -q -b "$base" || return 1 + if git ls-remote --exit-code --heads origin "$base" >/dev/null 2>&1; then + git reset -q --hard "origin/${base}" || return 1 + fi + git clean -q -fd || return 1 +} diff --git a/tests/clean-start.bats b/tests/clean-start.bats new file mode 100644 index 0000000..2be2326 --- /dev/null +++ b/tests/clean-start.bats @@ -0,0 +1,18 @@ +#!/usr/bin/env bats +setup() { + load 'helpers' + source "${LIB_DIR}/clean-start.sh" 2>/dev/null || source "${REPO_ROOT}/lib/clean-start.sh" +} +@test "clean_start: aligns to origin/base, reverts tracked edits, removes untracked" { + G="$(make_fixture_genome)"; cd "$G" + echo "from origin" >> wiki/index.md + git add -A && git commit -q -m "origin ahead" && git push -q + git reset --hard HEAD~1 # local BEHIND origin/main + echo "local junk" >> wiki/log.md # tracked edit, uncommitted + echo "scratch" > scratch.txt # genuinely untracked + INGEST_BASE="main" clean_start + git diff --quiet origin/main # aligned to origin + grep -q "from origin" wiki/index.md # forwarded to origin state + ! grep -q "local junk" wiki/log.md # tracked edit reverted + [ ! -f scratch.txt ] # untracked removed +} diff --git a/tests/permissions.bats b/tests/permissions.bats index ebe9888..b77c30f 100644 --- a/tests/permissions.bats +++ b/tests/permissions.bats @@ -29,7 +29,7 @@ EXECUTABLES=( # Librerie sourced: NON devono essere eseguibili. LIBRARIES=( - lib/lint.sh lib/output.sh lib/deps.sh lib/git-crypt.sh lib/scaffold.sh lib/structure.sh + lib/lint.sh lib/output.sh lib/deps.sh lib/git-crypt.sh lib/scaffold.sh lib/structure.sh lib/clean-start.sh providers/forgejo.sh providers/github.sh registry.sh globals.env )