#!/bin/sh # Commits and syncs the vault. The "Sync notes" button (the vault's Sync.md) # touches TRIGGER_FILE for an immediate sync; only its mtime matters, and it # must never be deleted here or SilverBullet re-uploads it in a loop. set -u SPACE=/space INTERVAL="${SYNC_INTERVAL:-300}" TRIGGER_FILE="$SPACE/${SYNC_TRIGGER_FILE:-sync-request.md}" POLL=2 log() { echo "[git-sync] $(date -Iseconds) $*"; } git config --global --add safe.directory "$SPACE" if [ ! -d "$SPACE/.git" ]; then log "ERROR: $SPACE is not a git repository." log "Clone the notes repo to the host path OBSIDIAN_DIR points at, then restart." # Idle rather than crash-loop, so the logs stay readable. while true; do sleep 3600; done fi cd "$SPACE" || exit 1 trigger_mtime() { [ -f "$TRIGGER_FILE" ] && stat -c %Y "$TRIGGER_FILE" 2>/dev/null || echo "" } sync_once() { git add -A if ! git diff --cached --quiet; then if git commit -q -m "notes: $(date -Iseconds)"; then log "committed local changes" else log "WARN: commit failed" fi fi if git remote get-url origin >/dev/null 2>&1; then git pull --rebase --autostash -q || log "WARN: pull failed (conflict, auth or network)" git push -q || log "WARN: push failed (conflict, auth or network)" fi } last_trigger=$(trigger_mtime) log "watching $SPACE every ${INTERVAL}s (on-demand via ${TRIGGER_FILE#$SPACE/})" while true; do sync_once # Sleep in short slices so an on-demand request is picked up quickly. waited=0 while [ "$waited" -lt "$INTERVAL" ]; do sleep "$POLL" waited=$((waited + POLL)) now=$(trigger_mtime) if [ -n "$now" ] && [ "$now" != "$last_trigger" ]; then last_trigger="$now" log "sync requested from the editor" break fi done done