content-forge/scripts/vault-sync.sh

64 lines
1.6 KiB
Bash
Executable File

#!/usr/bin/env bash
set -Eeuo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
VAULT_PATH="${VAULT_PATH:-$PROJECT_DIR/content-forge}"
LOG_FILE="${LOG_FILE:-$PROJECT_DIR/logs/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
}
# Remove stale index.lock if no git process is actively using it
LOCK_FILE="$VAULT_PATH/.git/index.lock"
if [[ -f "$LOCK_FILE" ]]; then
if ! fuser "$LOCK_FILE" >/dev/null 2>&1; then
log "WARN: Removing stale index.lock (no process holding it)"
rm -f "$LOCK_FILE"
else
log "ERROR: index.lock held by active process, skipping sync"
exit 1
fi
fi
# 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"