|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Auto-update openclaw submodule from fork |
| 3 | +# Runs silently on session start, logs to ~/.claude-code-pp/logs/autoupdate.log |
| 4 | + |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +CLAUDE_CODE_PP_DIR="${CLAUDE_CODE_PP_DIR:-$HOME/.claude-code-pp}" |
| 8 | +LOG_DIR="$CLAUDE_CODE_PP_DIR/logs" |
| 9 | +LOG_FILE="$LOG_DIR/autoupdate.log" |
| 10 | +REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 11 | +OPENCLAW_DIR="$REPO_DIR/openclaw" |
| 12 | + |
| 13 | +# Ensure log directory exists |
| 14 | +mkdir -p "$LOG_DIR" |
| 15 | + |
| 16 | +log() { |
| 17 | + echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" >> "$LOG_FILE" |
| 18 | +} |
| 19 | + |
| 20 | +# Check if we should skip (e.g., no network, recently updated) |
| 21 | +LAST_CHECK_FILE="$CLAUDE_CODE_PP_DIR/.last-openclaw-update" |
| 22 | +MIN_CHECK_INTERVAL=3600 # 1 hour between checks |
| 23 | + |
| 24 | +if [[ -f "$LAST_CHECK_FILE" ]]; then |
| 25 | + last_check=$(cat "$LAST_CHECK_FILE") |
| 26 | + now=$(date +%s) |
| 27 | + if (( now - last_check < MIN_CHECK_INTERVAL )); then |
| 28 | + exit 0 # Skip, checked recently |
| 29 | + fi |
| 30 | +fi |
| 31 | + |
| 32 | +# Record this check |
| 33 | +date +%s > "$LAST_CHECK_FILE" |
| 34 | + |
| 35 | +log "Starting auto-update check" |
| 36 | + |
| 37 | +# Check if openclaw directory exists and is a git repo |
| 38 | +if [[ ! -d "$OPENCLAW_DIR/.git" ]] && [[ ! -f "$OPENCLAW_DIR/.git" ]]; then |
| 39 | + log "openclaw directory not found or not a git repo" |
| 40 | + exit 0 |
| 41 | +fi |
| 42 | + |
| 43 | +cd "$OPENCLAW_DIR" |
| 44 | + |
| 45 | +# Fetch latest from origin (fork) |
| 46 | +if ! git fetch origin main --quiet 2>/dev/null; then |
| 47 | + log "Failed to fetch from origin (network issue?)" |
| 48 | + exit 0 |
| 49 | +fi |
| 50 | + |
| 51 | +# Check if we're behind |
| 52 | +LOCAL=$(git rev-parse HEAD 2>/dev/null || echo "unknown") |
| 53 | +REMOTE=$(git rev-parse origin/main 2>/dev/null || echo "unknown") |
| 54 | + |
| 55 | +if [[ "$LOCAL" == "$REMOTE" ]]; then |
| 56 | + log "Already up to date ($LOCAL)" |
| 57 | + exit 0 |
| 58 | +fi |
| 59 | + |
| 60 | +BEHIND=$(git rev-list --count HEAD..origin/main 2>/dev/null || echo "0") |
| 61 | +if [[ "$BEHIND" == "0" ]]; then |
| 62 | + log "Already up to date" |
| 63 | + exit 0 |
| 64 | +fi |
| 65 | + |
| 66 | +log "Found $BEHIND new commits, updating..." |
| 67 | + |
| 68 | +# Check for local changes |
| 69 | +if ! git diff --quiet 2>/dev/null || ! git diff --cached --quiet 2>/dev/null; then |
| 70 | + log "Local changes detected, skipping auto-update" |
| 71 | + exit 0 |
| 72 | +fi |
| 73 | + |
| 74 | +# Pull changes |
| 75 | +if git pull --ff-only origin main --quiet 2>/dev/null; then |
| 76 | + NEW_HEAD=$(git rev-parse --short HEAD) |
| 77 | + log "Updated to $NEW_HEAD ($BEHIND commits)" |
| 78 | + |
| 79 | + # Update parent repo submodule reference |
| 80 | + cd "$REPO_DIR" |
| 81 | + if git diff --quiet openclaw 2>/dev/null; then |
| 82 | + log "Submodule reference unchanged" |
| 83 | + else |
| 84 | + log "Submodule reference updated locally (commit manually if desired)" |
| 85 | + fi |
| 86 | +else |
| 87 | + log "Fast-forward failed, manual intervention required" |
| 88 | +fi |
0 commit comments