🐛 fix(tsl): preserve manual files during playbook sync

Limit tsl-playbook publishing to generated bundle paths so existing branch-local files are not removed.

Add tests for preserving non-generated files and for creating a clean orphan target branch.
This commit is contained in:
csh
2026-07-07 17:10:24 +08:00
parent c0bf0358e0
commit 38ccc67238
2 changed files with 184 additions and 6 deletions
+20 -6
View File
@@ -4,6 +4,9 @@ set -euo pipefail
# Build the minimal TSL playbook bundle and publish its *expanded* contents to
# the tsl-playbook branch. The branch tree mirrors the bundle root directly
# (AGENTS.md, docs/, skills/) — there is no wrapping tsl-playbook/ directory.
#
# Only the paths produced by the bundle are managed. Any other file that lives
# on the branch (e.g. a hand-written README.md) is preserved across syncs.
REPO_DIR="${REPO_DIR:-$(pwd)}"
TARGET_BRANCH="${TARGET_BRANCH:-tsl-playbook}"
@@ -30,23 +33,34 @@ trap cleanup EXIT
bundle="$build_dir/tsl-playbook"
python3 "$BUILD_SCRIPT" --output "$bundle"
# Check out (or create) the target branch. It is a content-only branch that
# shares no history with main, so an orphan branch keeps it clean.
# These are the only paths this workflow owns on the branch. Everything else is
# left alone, including hand-written files such as README.md.
generated_paths=(AGENTS.md docs skills)
for path in "${generated_paths[@]}"; do
if [ ! -e "$bundle/$path" ]; then
echo "ERROR: bundle is missing expected path: $path" >&2
exit 1
fi
done
# Check out (or create) the target branch.
if git show-ref --verify --quiet "refs/remotes/origin/$TARGET_BRANCH"; then
git fetch origin "$TARGET_BRANCH"
git checkout -B "$TARGET_BRANCH" "origin/$TARGET_BRANCH"
else
# A brand-new orphan branch inherits main's index. Clear the index without
# deleting source files from the working tree; only generated_paths are staged.
git checkout --orphan "$TARGET_BRANCH"
git rm -rf --cached --quiet . >/dev/null 2>&1 || true
fi
# Replace the entire tracked tree with the bundle contents. `git rm` clears
# tracked files; then copy the expanded bundle to the repo root.
git rm -rf --quiet . >/dev/null 2>&1 || true
# Remove only generated paths before copying the freshly built bundle.
rm -rf "${generated_paths[@]}"
# Copy bundle contents (including dotfiles) to the repo root.
cp -R "$bundle"/. "$REPO_DIR"/
git add -A
git add -A "${generated_paths[@]}"
if git diff --cached --quiet; then
echo "No tsl-playbook changes to publish."