#!/usr/bin/env bash set -Eeuo pipefail VAULT_PATH="${VAULT_PATH:-/home/kang/apps/content-forge/content-forge}" LOG_FILE="${LOG_FILE:-/tmp/vault-sync.log}" log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" >> "$LOG_FILE" } # Verify vault directory exists if [[ ! -d "$VAULT_PATH" ]]; then log "ERROR: Vault directory does not exist: $VAULT_PATH" exit 1 fi cd "$VAULT_PATH" || { log "ERROR: Failed to cd into vault: $VAULT_PATH" exit 1 } # Skip if no changes if git diff --quiet && git diff --cached --quiet && [ -z "$(git ls-files --others --exclude-standard)" ]; then log "No changes to sync" exit 0 fi # Get current branch dynamically BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "main") # Stage all changes if ! git add -A; then log "ERROR: git add failed" exit 1 fi # Commit with timestamp if ! git commit -m "vault: auto-sync $(date '+%Y-%m-%d %H:%M')"; then log "ERROR: git commit failed" exit 1 fi # Push with error handling if ! git push origin "$BRANCH"; then log "ERROR: git push failed (branch: $BRANCH)" exit 1 fi log "Sync successful: pushed to $BRANCH"