#!/bin/zsh
set -euo pipefail

REPO="/Users/openclaw/.openclaw/workspace"
LOG="$REPO/tmp/git-checkpoint.log"
mkdir -p "$REPO/tmp"

{
  echo "[$(date '+%Y-%m-%d %H:%M:%S %Z')] START checkpoint"
  cd "$REPO"

  git rev-parse --is-inside-work-tree >/dev/null
  git add -A

  if ! git diff --cached --quiet; then
    TS="$(date '+%Y-%m-%d %H:%M:%S %Z')"
    git commit -m "checkpoint: $TS" >/dev/null
    NEW_HASH="$(git rev-parse --short HEAD)"
    echo "[$(date '+%Y-%m-%d %H:%M:%S %Z')] COMMIT created $NEW_HASH"
  else
    CUR_HASH="$(git rev-parse --short HEAD 2>/dev/null || echo 'no-commits')"
    echo "[$(date '+%Y-%m-%d %H:%M:%S %Z')] NO_CHANGES head=$CUR_HASH"
  fi

  echo "[$(date '+%Y-%m-%d %H:%M:%S %Z')] OK checkpoint"
} >> "$LOG" 2>&1
