#!/usr/bin/env bash set -euo pipefail REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" MAIN_BRANCH="${MAIN_BRANCH:-main}" DATA_BRANCH="${DATA_BRANCH:-data}" WORKTREE_DIR="${DATA_WORKTREE_DIR:-$REPO_ROOT/.worktrees/data}" NODE_BIN="${NODE_BIN:-}" if [[ -z "$NODE_BIN" ]]; then NODE_BIN="$(command -v node || true)" fi if [[ -z "$NODE_BIN" ]]; then echo "ERROR: node not found in PATH" >&2 exit 1 fi cd "$REPO_ROOT" git rev-parse --is-inside-work-tree >/dev/null # Ensure data branch exists. if ! git show-ref --verify --quiet "refs/heads/$DATA_BRANCH"; then echo "ERROR: data branch not found: $DATA_BRANCH" >&2 exit 1 fi # Create/repair worktree for data branch. git worktree prune >/dev/null 2>&1 || true # If the dir exists but isn't a valid worktree checkout, remove it properly. if [[ -e "$WORKTREE_DIR" && ! -e "$WORKTREE_DIR/.git" ]]; then rm -rf "$WORKTREE_DIR" fi # If the worktree directory is missing, add it (force is safe after prune). if [[ ! -e "$WORKTREE_DIR/.git" ]]; then mkdir -p "$(dirname "$WORKTREE_DIR")" git worktree add -f -q "$WORKTREE_DIR" "$DATA_BRANCH" fi cd "$WORKTREE_DIR" # Keep data branch up-to-date with main (merge only when main moved). if git show-ref --verify --quiet "refs/heads/$MAIN_BRANCH"; then if ! git merge -q --no-edit "$MAIN_BRANCH"; then echo "ERROR: failed to merge $MAIN_BRANCH into $DATA_BRANCH" >&2 exit 1 fi fi # Run tracker (writes data/db + a plain report file in reports/) "$NODE_BIN" bin/tracker.js # Build viz artifacts on the data branch "$NODE_BIN" tools/build_viz_index.js "$NODE_BIN" tools/build_viz_commits.js "$NODE_BIN" tools/build_viz_recent.js # Stage only data/report/viz outputs git add -A data/db reports viz/data if git diff --cached --quiet; then echo "No data/report/viz changes to commit." >&2 exit 0 fi # Commit message: include the latest report as the commit body. ts="$(date -u +'%Y-%m-%dT%H:%M:%SZ')" REPORT_FILE="" if compgen -G "reports/*.txt" > /dev/null; then REPORT_FILE="$(ls -1t reports/*.txt | head -n 1 || true)" fi MSG_FILE="$(mktemp)" { echo "run: ${ts}" echo if [[ -n "$REPORT_FILE" && -f "$REPORT_FILE" ]]; then cat "$REPORT_FILE" else echo "(no report file found in reports/*.txt)" fi } > "$MSG_FILE" git commit -F "$MSG_FILE" -q rm -f "$MSG_FILE" git push -q