From 73800831aafb0d8bc1bfae98ee9023e0b61de811 Mon Sep 17 00:00:00 2001 From: apiooo <27990398+apiooo@users.noreply.github.com> Date: Sat, 23 Aug 2025 16:43:48 +0800 Subject: [PATCH 1/7] ci: Add workflow to sync with upstream repository --- .github/workflows/sync-upstream.yml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 .github/workflows/sync-upstream.yml diff --git a/.github/workflows/sync-upstream.yml b/.github/workflows/sync-upstream.yml new file mode 100644 index 0000000000..d147537f5a --- /dev/null +++ b/.github/workflows/sync-upstream.yml @@ -0,0 +1,18 @@ +name: Sync Upstream to mainup + +on: + schedule: + - cron: '0 3 * * *' # Run daily at 3 AM + workflow_dispatch: + +jobs: + sync: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Sync upstream branch + run: | + git remote add upstream https://github.com/NobyDa/Script.git + git fetch upstream + git push origin --force upstream/main:refs/heads/mainup \ No newline at end of file From c8873064accca10ae5f6367b0d02ef30aa1823db Mon Sep 17 00:00:00 2001 From: apiooo <27990398+apiooo@users.noreply.github.com> Date: Sat, 23 Aug 2025 16:50:10 +0800 Subject: [PATCH 2/7] fix: Update sync workflow to auto-detect default branch --- .github/workflows/sync-upstream.yml | 52 +++++++++++++++++++---------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/.github/workflows/sync-upstream.yml b/.github/workflows/sync-upstream.yml index d147537f5a..3cc56b223e 100644 --- a/.github/workflows/sync-upstream.yml +++ b/.github/workflows/sync-upstream.yml @@ -1,18 +1,34 @@ -name: Sync Upstream to mainup - -on: - schedule: - - cron: '0 3 * * *' # Run daily at 3 AM - workflow_dispatch: - -jobs: - sync: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - - name: Sync upstream branch - run: | - git remote add upstream https://github.com/NobyDa/Script.git - git fetch upstream - git push origin --force upstream/main:refs/heads/mainup \ No newline at end of file +name: Sync Upstream to mainup + +on: + schedule: + - cron: '0 3 * * *' # Run daily at 3 AM + workflow_dispatch: + +jobs: + sync: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Sync upstream branch + run: | + git remote add upstream https://github.com/NobyDa/Script.git + git fetch upstream + + # Detect the default branch of upstream + DEFAULT_BRANCH=$(git symbolic-ref refs/remotes/upstream/HEAD | sed 's@^refs/remotes/upstream/@@') + if [ -z "$DEFAULT_BRANCH" ]; then + # Fallback: try common branch names + if git show-ref --verify --quiet refs/remotes/upstream/main; then + DEFAULT_BRANCH="main" + elif git show-ref --verify --quiet refs/remotes/upstream/master; then + DEFAULT_BRANCH="master" + else + echo "Could not determine default branch" + exit 1 + fi + fi + + echo "Default branch detected: $DEFAULT_BRANCH" + git push origin --force upstream/$DEFAULT_BRANCH:refs/heads/mainup \ No newline at end of file From 4b55933955107780e666b937492b8fa587bbb761 Mon Sep 17 00:00:00 2001 From: apiooo <27990398+apiooo@users.noreply.github.com> Date: Sat, 23 Aug 2025 19:50:27 +0800 Subject: [PATCH 3/7] cleanup: Remove old sync workflow for upgrade --- .github/workflows/sync-upstream.yml | 34 ----------------------------- 1 file changed, 34 deletions(-) delete mode 100644 .github/workflows/sync-upstream.yml diff --git a/.github/workflows/sync-upstream.yml b/.github/workflows/sync-upstream.yml deleted file mode 100644 index 3cc56b223e..0000000000 --- a/.github/workflows/sync-upstream.yml +++ /dev/null @@ -1,34 +0,0 @@ -name: Sync Upstream to mainup - -on: - schedule: - - cron: '0 3 * * *' # Run daily at 3 AM - workflow_dispatch: - -jobs: - sync: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - - name: Sync upstream branch - run: | - git remote add upstream https://github.com/NobyDa/Script.git - git fetch upstream - - # Detect the default branch of upstream - DEFAULT_BRANCH=$(git symbolic-ref refs/remotes/upstream/HEAD | sed 's@^refs/remotes/upstream/@@') - if [ -z "$DEFAULT_BRANCH" ]; then - # Fallback: try common branch names - if git show-ref --verify --quiet refs/remotes/upstream/main; then - DEFAULT_BRANCH="main" - elif git show-ref --verify --quiet refs/remotes/upstream/master; then - DEFAULT_BRANCH="master" - else - echo "Could not determine default branch" - exit 1 - fi - fi - - echo "Default branch detected: $DEFAULT_BRANCH" - git push origin --force upstream/$DEFAULT_BRANCH:refs/heads/mainup \ No newline at end of file From f99b2bc70de3c6308cebfa8ce44afb4e028257d0 Mon Sep 17 00:00:00 2001 From: apiooo <27990398+apiooo@users.noreply.github.com> Date: Sat, 23 Aug 2025 20:42:10 +0800 Subject: [PATCH 4/7] fix: Deploy intelligent smart sync with branch detection --- .github/workflows/sync-upstream.yml | 110 ++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 .github/workflows/sync-upstream.yml diff --git a/.github/workflows/sync-upstream.yml b/.github/workflows/sync-upstream.yml new file mode 100644 index 0000000000..f6578c2a91 --- /dev/null +++ b/.github/workflows/sync-upstream.yml @@ -0,0 +1,110 @@ +name: Smart Sync Upstream + +on: + schedule: + - cron: '0 3 * * *' # Daily at 3 AM + workflow_dispatch: + +jobs: + smart_sync: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + token: ${{ secrets.GITHUB_TOKEN }} + fetch-depth: 0 + + - name: Smart upstream sync + run: | + set -e + + # Configure git + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" + + # Add upstream remote + UPSTREAM_URL="https://github.com/521xueweihan/GitHub520.git" + git remote add upstream $UPSTREAM_URL || git remote set-url upstream $UPSTREAM_URL + git fetch upstream --prune + + echo "=== Checking upstream branches for recent activity ===" + + # Get all upstream branches sorted by last commit date + BRANCHES=$(git for-each-ref --sort=-committerdate --format='%(refname:short) %(committerdate:iso8601)' refs/remotes/upstream/) + + echo "Available upstream branches:" + echo "$BRANCHES" + + # Find the most recently updated branch (filter out invalid refs and remove upstream/ prefix) + MOST_RECENT_REF=$(echo "$BRANCHES" | grep "upstream/" | head -1 | cut -d' ' -f1) + MOST_RECENT_BRANCH=$(echo "$MOST_RECENT_REF" | sed 's/^upstream\///') + MOST_RECENT_DATE=$(echo "$BRANCHES" | grep "upstream/" | head -1 | cut -d' ' -f2-) + + echo "Most recent upstream branch: $MOST_RECENT_BRANCH (updated: $MOST_RECENT_DATE)" + + # Get current mainup branch head (if exists) + CURRENT_MAINUP_SHA="" + if git show-ref --verify --quiet refs/heads/mainup; then + CURRENT_MAINUP_SHA=$(git rev-parse mainup) + echo "Current mainup SHA: $CURRENT_MAINUP_SHA" + else + echo "mainup branch does not exist yet" + fi + + # Get upstream branch SHA + UPSTREAM_SHA=$(git rev-parse upstream/$MOST_RECENT_BRANCH) + echo "Upstream $MOST_RECENT_BRANCH SHA: $UPSTREAM_SHA" + + # Check if sync is needed + if [ "$CURRENT_MAINUP_SHA" = "$UPSTREAM_SHA" ]; then + echo "SUCCESS: No sync needed - mainup is already up to date with upstream/$MOST_RECENT_BRANCH" + echo "SYNC_NEEDED=false" >> $GITHUB_OUTPUT + exit 0 + fi + + # Check if there are new commits in the last 7 days + RECENT_COMMITS=$(git rev-list --count --since="7 days ago" upstream/$MOST_RECENT_BRANCH) + echo "Recent commits (last 7 days): $RECENT_COMMITS" + + if [ "$RECENT_COMMITS" -eq 0 ] && [ -n "$CURRENT_MAINUP_SHA" ]; then + echo "鈩癸笍 No recent activity in upstream/$MOST_RECENT_BRANCH, checking if mainup is reasonable" + + # Check if mainup is behind by more than 10 commits + if git merge-base --is-ancestor $CURRENT_MAINUP_SHA upstream/$MOST_RECENT_BRANCH; then + BEHIND_COUNT=$(git rev-list --count $CURRENT_MAINUP_SHA..upstream/$MOST_RECENT_BRANCH) + if [ "$BEHIND_COUNT" -lt 10 ]; then + echo "SUCCESS: Skip sync - mainup is only $BEHIND_COUNT commits behind and no recent upstream activity" + echo "SYNC_NEEDED=false" >> $GITHUB_OUTPUT + exit 0 + fi + fi + fi + + echo "SYNC: Sync needed - updating mainup to upstream/$MOST_RECENT_BRANCH" + echo "SYNC_NEEDED=true" >> $GITHUB_OUTPUT + echo "SYNCED_BRANCH=$MOST_RECENT_BRANCH" >> $GITHUB_OUTPUT + + # Create or update mainup branch + git checkout -B mainup + git reset --hard upstream/$MOST_RECENT_BRANCH + + # Push the updated branch + git push origin mainup --force + + echo "SUCCESS: Successfully synced mainup with upstream/$MOST_RECENT_BRANCH" + + # Show sync summary + echo "=== Sync Summary ===" + echo "Synced from: upstream/$MOST_RECENT_BRANCH" + echo "New HEAD: $(git rev-parse HEAD)" + echo "Commits synced: $(git rev-list --count $CURRENT_MAINUP_SHA..HEAD 2>/dev/null || echo 'N/A (new branch)')" + + - name: Update sync status + if: always() + run: | + if [ "${{ steps.smart_sync.outputs.SYNC_NEEDED }}" = "true" ]; then + echo "STATUS: Sync completed for branch: ${{ steps.smart_sync.outputs.SYNCED_BRANCH }}" + else + echo "STATUS: Sync skipped - no updates needed" + fi \ No newline at end of file From 9e523e3ce6f1ffbdc69f5943f50c5bbe5a563a75 Mon Sep 17 00:00:00 2001 From: apiooo <27990398+apiooo@users.noreply.github.com> Date: Sat, 23 Aug 2025 21:06:14 +0800 Subject: [PATCH 5/7] fix: Use dynamic upstream detection and add GH_TOKEN env --- .github/workflows/sync-upstream.yml | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/.github/workflows/sync-upstream.yml b/.github/workflows/sync-upstream.yml index f6578c2a91..c217ba4e48 100644 --- a/.github/workflows/sync-upstream.yml +++ b/.github/workflows/sync-upstream.yml @@ -16,6 +16,8 @@ jobs: fetch-depth: 0 - name: Smart upstream sync + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | set -e @@ -23,8 +25,18 @@ jobs: git config user.name "github-actions[bot]" git config user.email "github-actions[bot]@users.noreply.github.com" + # Get upstream repo dynamically from GitHub API + UPSTREAM_REPO=$(gh repo view --json parent --jq '.parent.owner.login + "/" + .parent.name') + + if [ -z "$UPSTREAM_REPO" ] || [ "$UPSTREAM_REPO" = "null/null" ]; then + echo "ERROR: This repository is not a fork or upstream info not found" + exit 1 + fi + + echo "Detected upstream repository: $UPSTREAM_REPO" + # Add upstream remote - UPSTREAM_URL="https://github.com/521xueweihan/GitHub520.git" + UPSTREAM_URL="https://github.com/$UPSTREAM_REPO.git" git remote add upstream $UPSTREAM_URL || git remote set-url upstream $UPSTREAM_URL git fetch upstream --prune From 9e09588d50aa7f2a863c1593ede5e823e17430f6 Mon Sep 17 00:00:00 2001 From: apiooo <27990398+apiooo@users.noreply.github.com> Date: Sat, 23 Aug 2025 21:47:56 +0800 Subject: [PATCH 6/7] fix: Use dynamic upstream detection and add GH_TOKEN env --- .github/workflows/sync-upstream.yml | 84 +++++++++++++++++++++-------- 1 file changed, 63 insertions(+), 21 deletions(-) diff --git a/.github/workflows/sync-upstream.yml b/.github/workflows/sync-upstream.yml index c217ba4e48..fb120b382a 100644 --- a/.github/workflows/sync-upstream.yml +++ b/.github/workflows/sync-upstream.yml @@ -42,18 +42,60 @@ jobs: echo "=== Checking upstream branches for recent activity ===" - # Get all upstream branches sorted by last commit date - BRANCHES=$(git for-each-ref --sort=-committerdate --format='%(refname:short) %(committerdate:iso8601)' refs/remotes/upstream/) + # Get upstream default branch (main or master) + DEFAULT_BRANCH="" + if git show-ref --verify --quiet refs/remotes/upstream/main; then + DEFAULT_BRANCH="main" + elif git show-ref --verify --quiet refs/remotes/upstream/master; then + DEFAULT_BRANCH="master" + fi + + echo "Detected default branch: $DEFAULT_BRANCH" + + # Check if default branch has commits in the last 6 months + SELECTED_BRANCH="" + SELECTION_REASON="" - echo "Available upstream branches:" - echo "$BRANCHES" + if [ -n "$DEFAULT_BRANCH" ]; then + RECENT_DEFAULT_COMMITS=$(git rev-list --count --since="6 months ago" upstream/$DEFAULT_BRANCH) + echo "Default branch ($DEFAULT_BRANCH) commits in last 6 months: $RECENT_DEFAULT_COMMITS" + + if [ "$RECENT_DEFAULT_COMMITS" -gt 0 ]; then + SELECTED_BRANCH="$DEFAULT_BRANCH" + SELECTION_REASON="Default branch has recent activity" + echo "鉁?Using default branch $DEFAULT_BRANCH (has $RECENT_DEFAULT_COMMITS commits in last 6 months)" + else + echo "脳 Default branch $DEFAULT_BRANCH has no commits in last 6 months, checking other branches..." + fi + else + echo "脳 No standard default branch (main/master) found, checking all branches..." + fi - # Find the most recently updated branch (filter out invalid refs and remove upstream/ prefix) - MOST_RECENT_REF=$(echo "$BRANCHES" | grep "upstream/" | head -1 | cut -d' ' -f1) - MOST_RECENT_BRANCH=$(echo "$MOST_RECENT_REF" | sed 's/^upstream\///') - MOST_RECENT_DATE=$(echo "$BRANCHES" | grep "upstream/" | head -1 | cut -d' ' -f2-) + # If default branch is not active, find the most recently updated branch + if [ -z "$SELECTED_BRANCH" ]; then + echo "Scanning all branches for most recent activity..." + + # Get all upstream branches sorted by last commit date + BRANCHES=$(git for-each-ref --sort=-committerdate --format='%(refname:short) %(committerdate:iso8601)' refs/remotes/upstream/) + + echo "Available upstream branches:" + echo "$BRANCHES" + + # Find the most recently updated branch + MOST_RECENT_REF=$(echo "$BRANCHES" | grep "upstream/" | head -1 | cut -d' ' -f1) + MOST_RECENT_BRANCH=$(echo "$MOST_RECENT_REF" | sed 's/^upstream\///') + MOST_RECENT_DATE=$(echo "$BRANCHES" | grep "upstream/" | head -1 | cut -d' ' -f2-) + + SELECTED_BRANCH="$MOST_RECENT_BRANCH" + SELECTION_REASON="Most recently updated branch" + echo "鉁?Using most active branch: $MOST_RECENT_BRANCH (updated: $MOST_RECENT_DATE)" + fi - echo "Most recent upstream branch: $MOST_RECENT_BRANCH (updated: $MOST_RECENT_DATE)" + echo "" + echo "=== BRANCH SELECTION SUMMARY ===" + echo "Selected branch: $SELECTED_BRANCH" + echo "Selection reason: $SELECTION_REASON" + echo "=================================" # Get current mainup branch head (if exists) CURRENT_MAINUP_SHA="" @@ -65,26 +107,26 @@ jobs: fi # Get upstream branch SHA - UPSTREAM_SHA=$(git rev-parse upstream/$MOST_RECENT_BRANCH) - echo "Upstream $MOST_RECENT_BRANCH SHA: $UPSTREAM_SHA" + UPSTREAM_SHA=$(git rev-parse upstream/$SELECTED_BRANCH) + echo "Upstream $SELECTED_BRANCH SHA: $UPSTREAM_SHA" # Check if sync is needed if [ "$CURRENT_MAINUP_SHA" = "$UPSTREAM_SHA" ]; then - echo "SUCCESS: No sync needed - mainup is already up to date with upstream/$MOST_RECENT_BRANCH" + echo "SUCCESS: No sync needed - mainup is already up to date with upstream/$SELECTED_BRANCH" echo "SYNC_NEEDED=false" >> $GITHUB_OUTPUT exit 0 fi # Check if there are new commits in the last 7 days - RECENT_COMMITS=$(git rev-list --count --since="7 days ago" upstream/$MOST_RECENT_BRANCH) + RECENT_COMMITS=$(git rev-list --count --since="7 days ago" upstream/$SELECTED_BRANCH) echo "Recent commits (last 7 days): $RECENT_COMMITS" if [ "$RECENT_COMMITS" -eq 0 ] && [ -n "$CURRENT_MAINUP_SHA" ]; then - echo "鈩癸笍 No recent activity in upstream/$MOST_RECENT_BRANCH, checking if mainup is reasonable" + echo "鈩癸笍 No recent activity in upstream/$SELECTED_BRANCH, checking if mainup is reasonable" # Check if mainup is behind by more than 10 commits - if git merge-base --is-ancestor $CURRENT_MAINUP_SHA upstream/$MOST_RECENT_BRANCH; then - BEHIND_COUNT=$(git rev-list --count $CURRENT_MAINUP_SHA..upstream/$MOST_RECENT_BRANCH) + if git merge-base --is-ancestor $CURRENT_MAINUP_SHA upstream/$SELECTED_BRANCH; then + BEHIND_COUNT=$(git rev-list --count $CURRENT_MAINUP_SHA..upstream/$SELECTED_BRANCH) if [ "$BEHIND_COUNT" -lt 10 ]; then echo "SUCCESS: Skip sync - mainup is only $BEHIND_COUNT commits behind and no recent upstream activity" echo "SYNC_NEEDED=false" >> $GITHUB_OUTPUT @@ -93,22 +135,22 @@ jobs: fi fi - echo "SYNC: Sync needed - updating mainup to upstream/$MOST_RECENT_BRANCH" + echo "SYNC: Sync needed - updating mainup to upstream/$SELECTED_BRANCH" echo "SYNC_NEEDED=true" >> $GITHUB_OUTPUT - echo "SYNCED_BRANCH=$MOST_RECENT_BRANCH" >> $GITHUB_OUTPUT + echo "SYNCED_BRANCH=$SELECTED_BRANCH" >> $GITHUB_OUTPUT # Create or update mainup branch git checkout -B mainup - git reset --hard upstream/$MOST_RECENT_BRANCH + git reset --hard upstream/$SELECTED_BRANCH # Push the updated branch git push origin mainup --force - echo "SUCCESS: Successfully synced mainup with upstream/$MOST_RECENT_BRANCH" + echo "SUCCESS: Successfully synced mainup with upstream/$SELECTED_BRANCH" # Show sync summary echo "=== Sync Summary ===" - echo "Synced from: upstream/$MOST_RECENT_BRANCH" + echo "Synced from: upstream/$SELECTED_BRANCH" echo "New HEAD: $(git rev-parse HEAD)" echo "Commits synced: $(git rev-list --count $CURRENT_MAINUP_SHA..HEAD 2>/dev/null || echo 'N/A (new branch)')" From 14633be26ffc5340a2f4a710315212ec01d5c391 Mon Sep 17 00:00:00 2001 From: apiooo <27990398+apiooo@users.noreply.github.com> Date: Thu, 23 Oct 2025 11:50:33 +0800 Subject: [PATCH 7/7] Remove sync-upstream.yml workflow --- .github/workflows/sync-upstream.yml | 164 ---------------------------- 1 file changed, 164 deletions(-) delete mode 100644 .github/workflows/sync-upstream.yml diff --git a/.github/workflows/sync-upstream.yml b/.github/workflows/sync-upstream.yml deleted file mode 100644 index fb120b382a..0000000000 --- a/.github/workflows/sync-upstream.yml +++ /dev/null @@ -1,164 +0,0 @@ -name: Smart Sync Upstream - -on: - schedule: - - cron: '0 3 * * *' # Daily at 3 AM - workflow_dispatch: - -jobs: - smart_sync: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v4 - with: - token: ${{ secrets.GITHUB_TOKEN }} - fetch-depth: 0 - - - name: Smart upstream sync - env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: | - set -e - - # Configure git - git config user.name "github-actions[bot]" - git config user.email "github-actions[bot]@users.noreply.github.com" - - # Get upstream repo dynamically from GitHub API - UPSTREAM_REPO=$(gh repo view --json parent --jq '.parent.owner.login + "/" + .parent.name') - - if [ -z "$UPSTREAM_REPO" ] || [ "$UPSTREAM_REPO" = "null/null" ]; then - echo "ERROR: This repository is not a fork or upstream info not found" - exit 1 - fi - - echo "Detected upstream repository: $UPSTREAM_REPO" - - # Add upstream remote - UPSTREAM_URL="https://github.com/$UPSTREAM_REPO.git" - git remote add upstream $UPSTREAM_URL || git remote set-url upstream $UPSTREAM_URL - git fetch upstream --prune - - echo "=== Checking upstream branches for recent activity ===" - - # Get upstream default branch (main or master) - DEFAULT_BRANCH="" - if git show-ref --verify --quiet refs/remotes/upstream/main; then - DEFAULT_BRANCH="main" - elif git show-ref --verify --quiet refs/remotes/upstream/master; then - DEFAULT_BRANCH="master" - fi - - echo "Detected default branch: $DEFAULT_BRANCH" - - # Check if default branch has commits in the last 6 months - SELECTED_BRANCH="" - SELECTION_REASON="" - - if [ -n "$DEFAULT_BRANCH" ]; then - RECENT_DEFAULT_COMMITS=$(git rev-list --count --since="6 months ago" upstream/$DEFAULT_BRANCH) - echo "Default branch ($DEFAULT_BRANCH) commits in last 6 months: $RECENT_DEFAULT_COMMITS" - - if [ "$RECENT_DEFAULT_COMMITS" -gt 0 ]; then - SELECTED_BRANCH="$DEFAULT_BRANCH" - SELECTION_REASON="Default branch has recent activity" - echo "鉁?Using default branch $DEFAULT_BRANCH (has $RECENT_DEFAULT_COMMITS commits in last 6 months)" - else - echo "脳 Default branch $DEFAULT_BRANCH has no commits in last 6 months, checking other branches..." - fi - else - echo "脳 No standard default branch (main/master) found, checking all branches..." - fi - - # If default branch is not active, find the most recently updated branch - if [ -z "$SELECTED_BRANCH" ]; then - echo "Scanning all branches for most recent activity..." - - # Get all upstream branches sorted by last commit date - BRANCHES=$(git for-each-ref --sort=-committerdate --format='%(refname:short) %(committerdate:iso8601)' refs/remotes/upstream/) - - echo "Available upstream branches:" - echo "$BRANCHES" - - # Find the most recently updated branch - MOST_RECENT_REF=$(echo "$BRANCHES" | grep "upstream/" | head -1 | cut -d' ' -f1) - MOST_RECENT_BRANCH=$(echo "$MOST_RECENT_REF" | sed 's/^upstream\///') - MOST_RECENT_DATE=$(echo "$BRANCHES" | grep "upstream/" | head -1 | cut -d' ' -f2-) - - SELECTED_BRANCH="$MOST_RECENT_BRANCH" - SELECTION_REASON="Most recently updated branch" - echo "鉁?Using most active branch: $MOST_RECENT_BRANCH (updated: $MOST_RECENT_DATE)" - fi - - echo "" - echo "=== BRANCH SELECTION SUMMARY ===" - echo "Selected branch: $SELECTED_BRANCH" - echo "Selection reason: $SELECTION_REASON" - echo "=================================" - - # Get current mainup branch head (if exists) - CURRENT_MAINUP_SHA="" - if git show-ref --verify --quiet refs/heads/mainup; then - CURRENT_MAINUP_SHA=$(git rev-parse mainup) - echo "Current mainup SHA: $CURRENT_MAINUP_SHA" - else - echo "mainup branch does not exist yet" - fi - - # Get upstream branch SHA - UPSTREAM_SHA=$(git rev-parse upstream/$SELECTED_BRANCH) - echo "Upstream $SELECTED_BRANCH SHA: $UPSTREAM_SHA" - - # Check if sync is needed - if [ "$CURRENT_MAINUP_SHA" = "$UPSTREAM_SHA" ]; then - echo "SUCCESS: No sync needed - mainup is already up to date with upstream/$SELECTED_BRANCH" - echo "SYNC_NEEDED=false" >> $GITHUB_OUTPUT - exit 0 - fi - - # Check if there are new commits in the last 7 days - RECENT_COMMITS=$(git rev-list --count --since="7 days ago" upstream/$SELECTED_BRANCH) - echo "Recent commits (last 7 days): $RECENT_COMMITS" - - if [ "$RECENT_COMMITS" -eq 0 ] && [ -n "$CURRENT_MAINUP_SHA" ]; then - echo "鈩癸笍 No recent activity in upstream/$SELECTED_BRANCH, checking if mainup is reasonable" - - # Check if mainup is behind by more than 10 commits - if git merge-base --is-ancestor $CURRENT_MAINUP_SHA upstream/$SELECTED_BRANCH; then - BEHIND_COUNT=$(git rev-list --count $CURRENT_MAINUP_SHA..upstream/$SELECTED_BRANCH) - if [ "$BEHIND_COUNT" -lt 10 ]; then - echo "SUCCESS: Skip sync - mainup is only $BEHIND_COUNT commits behind and no recent upstream activity" - echo "SYNC_NEEDED=false" >> $GITHUB_OUTPUT - exit 0 - fi - fi - fi - - echo "SYNC: Sync needed - updating mainup to upstream/$SELECTED_BRANCH" - echo "SYNC_NEEDED=true" >> $GITHUB_OUTPUT - echo "SYNCED_BRANCH=$SELECTED_BRANCH" >> $GITHUB_OUTPUT - - # Create or update mainup branch - git checkout -B mainup - git reset --hard upstream/$SELECTED_BRANCH - - # Push the updated branch - git push origin mainup --force - - echo "SUCCESS: Successfully synced mainup with upstream/$SELECTED_BRANCH" - - # Show sync summary - echo "=== Sync Summary ===" - echo "Synced from: upstream/$SELECTED_BRANCH" - echo "New HEAD: $(git rev-parse HEAD)" - echo "Commits synced: $(git rev-list --count $CURRENT_MAINUP_SHA..HEAD 2>/dev/null || echo 'N/A (new branch)')" - - - name: Update sync status - if: always() - run: | - if [ "${{ steps.smart_sync.outputs.SYNC_NEEDED }}" = "true" ]; then - echo "STATUS: Sync completed for branch: ${{ steps.smart_sync.outputs.SYNCED_BRANCH }}" - else - echo "STATUS: Sync skipped - no updates needed" - fi \ No newline at end of file