|
| 1 | +#!/bin/bash |
| 2 | +# check-upstream-sync.sh |
| 3 | +# Checks if main branch is behind upstream/main and reports the delta. |
| 4 | +# Run daily via cron to stay aware of upstream changes. |
| 5 | +# |
| 6 | +# Uses a marker file (.last-upstream-sync) to track the last synced upstream commit SHA. |
| 7 | +# After a merge, run: ./scripts/check-upstream-sync.sh --mark |
| 8 | +# to update the marker to the current upstream/main HEAD. |
| 9 | +# |
| 10 | +# Usage: ./scripts/check-upstream-sync.sh [--quiet|--mark] |
| 11 | +# --quiet: Only output if there are new commits (for cron notifications) |
| 12 | +# --mark: Record current upstream/main HEAD as the last-synced point |
| 13 | + |
| 14 | +set -uo pipefail |
| 15 | +trap "" PIPE |
| 16 | + |
| 17 | +REPO_DIR="/home/zach/development/claude_workspace/software-agent-sdk" |
| 18 | +UPSTREAM_REMOTE="upstream" |
| 19 | +UPSTREAM_BRANCH="main" |
| 20 | +LOCAL_BRANCH="main" |
| 21 | +MARKER_FILE="${REPO_DIR}/.last-upstream-sync" |
| 22 | +ARG="${1:-}" |
| 23 | + |
| 24 | +cd "$REPO_DIR" |
| 25 | + |
| 26 | +# Ensure upstream remote exists |
| 27 | +if ! git remote | grep -q "^${UPSTREAM_REMOTE}$"; then |
| 28 | + echo "Adding upstream remote..." |
| 29 | + git remote add "$UPSTREAM_REMOTE" "https://github.com/OpenHands/software-agent-sdk.git" |
| 30 | +fi |
| 31 | + |
| 32 | +# Fetch upstream (quietly) |
| 33 | +git fetch "$UPSTREAM_REMOTE" --quiet 2>/dev/null |
| 34 | + |
| 35 | +# Handle --mark: save current upstream HEAD as synced point |
| 36 | +if [ "$ARG" = "--mark" ]; then |
| 37 | + UPSTREAM_HEAD=$(git rev-parse "${UPSTREAM_REMOTE}/${UPSTREAM_BRANCH}") |
| 38 | + echo "$UPSTREAM_HEAD" > "$MARKER_FILE" |
| 39 | + echo "Marked $(echo "$UPSTREAM_HEAD" | head -c 9) as last synced upstream commit" |
| 40 | + exit 0 |
| 41 | +fi |
| 42 | + |
| 43 | +# Determine the base commit to compare from |
| 44 | +if [ -f "$MARKER_FILE" ]; then |
| 45 | + SYNC_BASE=$(cat "$MARKER_FILE" | tr -d '[:space:]') |
| 46 | + # Verify the commit still exists |
| 47 | + if ! git cat-file -e "$SYNC_BASE" 2>/dev/null; then |
| 48 | + echo "WARNING: Marker commit $SYNC_BASE not found, falling back to merge-base" |
| 49 | + SYNC_BASE="" |
| 50 | + fi |
| 51 | +fi |
| 52 | + |
| 53 | +if [ -z "${SYNC_BASE:-}" ]; then |
| 54 | + # Fallback: use merge-base (works for real merges, not squash merges) |
| 55 | + SYNC_BASE=$(git merge-base "${LOCAL_BRANCH}" "${UPSTREAM_REMOTE}/${UPSTREAM_BRANCH}" 2>/dev/null || echo "") |
| 56 | + if [ -z "$SYNC_BASE" ]; then |
| 57 | + echo "ERROR: Could not find merge base between $LOCAL_BRANCH and $UPSTREAM_REMOTE/$UPSTREAM_BRANCH" |
| 58 | + exit 1 |
| 59 | + fi |
| 60 | +fi |
| 61 | + |
| 62 | +BEHIND=$(git rev-list --count "${SYNC_BASE}..${UPSTREAM_REMOTE}/${UPSTREAM_BRANCH}" 2>/dev/null || echo "0") |
| 63 | + |
| 64 | +if [ "$BEHIND" = "0" ]; then |
| 65 | + if [ "$ARG" != "--quiet" ]; then |
| 66 | + echo "$(date '+%Y-%m-%d %H:%M') | main is up to date with upstream/main" |
| 67 | + fi |
| 68 | + exit 0 |
| 69 | +fi |
| 70 | + |
| 71 | +# Get commit summaries |
| 72 | +echo "==========================================" |
| 73 | +echo " Upstream Sync Report - $(date '+%Y-%m-%d %H:%M')" |
| 74 | +echo "==========================================" |
| 75 | +echo "" |
| 76 | +echo "main is $BEHIND commit(s) behind upstream/main" |
| 77 | +echo "(since $(echo "$SYNC_BASE" | head -c 9))" |
| 78 | +echo "" |
| 79 | + |
| 80 | +# Show custom (ahead) commits |
| 81 | +AHEAD=$(git rev-list --count "${UPSTREAM_REMOTE}/${UPSTREAM_BRANCH}..${LOCAL_BRANCH}" 2>/dev/null || echo "0") |
| 82 | +if [ "$AHEAD" -gt 0 ]; then |
| 83 | + echo "Custom commits (ahead of upstream): $AHEAD" |
| 84 | + git log --oneline --no-merges "${UPSTREAM_REMOTE}/${UPSTREAM_BRANCH}..${LOCAL_BRANCH}" |
| 85 | + echo "" |
| 86 | +fi |
| 87 | + |
| 88 | +echo "New upstream commits:" |
| 89 | +echo "---" |
| 90 | +git log --oneline --no-merges "${SYNC_BASE}..${UPSTREAM_REMOTE}/${UPSTREAM_BRANCH}" | head -30 |
| 91 | +if [ "$BEHIND" -gt 30 ]; then |
| 92 | + echo "... and $((BEHIND - 30)) more" |
| 93 | +fi |
| 94 | +echo "" |
| 95 | + |
| 96 | +# Categorize by area |
| 97 | +RANGE="${SYNC_BASE}..${UPSTREAM_REMOTE}/${UPSTREAM_BRANCH}" |
| 98 | +SDK_COUNT=$(git log --oneline --no-merges "$RANGE" -- 'openhands-sdk/' | wc -l) |
| 99 | +TOOLS_COUNT=$(git log --oneline --no-merges "$RANGE" -- 'openhands-tools/' | wc -l) |
| 100 | +SERVER_COUNT=$(git log --oneline --no-merges "$RANGE" -- 'openhands-agent-server/' | wc -l) |
| 101 | +WORKSPACE_COUNT=$(git log --oneline --no-merges "$RANGE" -- 'openhands-workspace/' | wc -l) |
| 102 | +CI_COUNT=$(git log --oneline --no-merges "$RANGE" -- '.github/' | wc -l) |
| 103 | +TESTS_COUNT=$(git log --oneline --no-merges "$RANGE" -- 'tests/' | wc -l) |
| 104 | + |
| 105 | +echo "Breakdown:" |
| 106 | +echo " SDK: $SDK_COUNT" |
| 107 | +echo " Tools: $TOOLS_COUNT" |
| 108 | +echo " Agent Server: $SERVER_COUNT" |
| 109 | +echo " Workspace: $WORKSPACE_COUNT" |
| 110 | +echo " CI/CD: $CI_COUNT" |
| 111 | +echo " Tests: $TESTS_COUNT" |
| 112 | +echo "" |
| 113 | + |
| 114 | +# Files changed summary |
| 115 | +echo "Files changed: $(git diff --stat "${SYNC_BASE}..${UPSTREAM_REMOTE}/${UPSTREAM_BRANCH}" | tail -1)" |
| 116 | +echo "" |
| 117 | + |
| 118 | +# Potential conflict check |
| 119 | +CUSTOM_FILES=$(git diff --name-only "${UPSTREAM_REMOTE}/${UPSTREAM_BRANCH}..${LOCAL_BRANCH}" 2>/dev/null) |
| 120 | +UPSTREAM_FILES=$(git diff --name-only "${SYNC_BASE}..${UPSTREAM_REMOTE}/${UPSTREAM_BRANCH}" 2>/dev/null) |
| 121 | +BOTH_MODIFIED=$(comm -12 <(echo "$CUSTOM_FILES" | sort) <(echo "$UPSTREAM_FILES" | sort)) |
| 122 | +CONFLICT_COUNT=$(echo "$BOTH_MODIFIED" | grep -c . || true) |
| 123 | + |
| 124 | +if [ "$CONFLICT_COUNT" -gt 0 ]; then |
| 125 | + echo "Potential conflict files ($CONFLICT_COUNT files modified on both sides):" |
| 126 | + echo "$BOTH_MODIFIED" | head -20 |
| 127 | + if [ "$CONFLICT_COUNT" -gt 20 ]; then |
| 128 | + echo "... and $((CONFLICT_COUNT - 20)) more" |
| 129 | + fi |
| 130 | + echo "" |
| 131 | +fi |
| 132 | + |
| 133 | +echo "Run the merge guide: docs/upstream-merge-guide.md" |
| 134 | +echo "==========================================" |
0 commit comments