@@ -17,6 +17,10 @@ permissions:
1717 contents : write
1818 pull-requests : write
1919
20+ concurrency :
21+ group : upstream-sync
22+ cancel-in-progress : true
23+
2024jobs :
2125 sync :
2226 runs-on : ubuntu-latest
@@ -49,12 +53,18 @@ jobs:
4953 - name : Determine if main is already in sync
5054 id : diff
5155 run : |
56+ set -euo pipefail
5257 git fetch origin ${BASE_BRANCH} --quiet
5358 UP_SHA=$(git rev-parse upstream/${BASE_BRANCH})
5459 OR_SHA=$(git rev-parse origin/${BASE_BRANCH} || true)
5560 echo "upstream/main: $UP_SHA"
5661 echo "origin/main : $OR_SHA"
57- if [ "$UP_SHA" = "$OR_SHA" ]; then
62+ # Content-based sync check (robust to squash/rebase): compare trees
63+ UP_TREE=$(git rev-parse upstream/${BASE_BRANCH}^{tree})
64+ OR_TREE=$(git rev-parse origin/${BASE_BRANCH}^{tree} || true)
65+ echo "upstream tree: $UP_TREE"
66+ echo "origin tree: $OR_TREE"
67+ if [ "$UP_TREE" = "$OR_TREE" ]; then
5868 echo "in_sync=true" >> $GITHUB_OUTPUT
5969 else
6070 echo "in_sync=false" >> $GITHUB_OUTPUT
6373 - name : Update sync branch to upstream/main
6474 if : steps.diff.outputs.in_sync == 'false'
6575 run : |
76+ set -euo pipefail
77+ git fetch origin --quiet || true
6678 # Create or switch to the evergreen sync branch
6779 if git show-ref --verify --quiet refs/remotes/origin/${SYNC_BRANCH}; then
6880 # Sync branch exists on remote - check if we have it locally
@@ -87,30 +99,26 @@ jobs:
8799 run : |
88100 # Fetch latest state to ensure we have current refs
89101 git fetch origin ${BASE_BRANCH} ${SYNC_BRANCH} --quiet
90-
91- # Check if branches are actually different (tree-wise)
92- MAIN_TREE=$(git rev-parse origin/${BASE_BRANCH}^{tree})
93- SYNC_TREE=$(git rev-parse origin/${SYNC_BRANCH}^{tree})
94-
95- echo "main tree: $MAIN_TREE"
96- echo "sync tree: $SYNC_TREE"
97-
98- if [ "$MAIN_TREE" = "$SYNC_TREE" ]; then
99- echo "has_changes=false" >> $GITHUB_OUTPUT
100- echo "Branches have identical content (same tree), no PR needed"
101- else
102+
103+ # After resetting sync to upstream, decide if PR is needed based on patch-unique commits
104+ UPSTREAM_PATCH_AHEAD=$(git rev-list --left-only --cherry-pick --count upstream/${BASE_BRANCH}...origin/${BASE_BRANCH} || echo 0)
105+ echo "upstream patch-ahead of main by: $UPSTREAM_PATCH_AHEAD commits"
106+
107+ if [ "$UPSTREAM_PATCH_AHEAD" -gt 0 ]; then
102108 echo "has_changes=true" >> $GITHUB_OUTPUT
103- echo "Branches have different content, PR is needed"
104-
105- # Show what's different (for debugging)
106- echo "Files changed between branches:"
107- git diff --name-only origin/${BASE_BRANCH} origin/${SYNC_BRANCH} | head -10
109+ echo "PR is needed; upstream has patch-unique commits not in main"
110+ echo "Representative missing upstream commits:"
111+ git log --left-right --cherry-pick --oneline upstream/${BASE_BRANCH}...origin/${BASE_BRANCH} | sed -n 's/^</missing /p' | head -10 || true
112+ else
113+ echo "has_changes=false" >> $GITHUB_OUTPUT
114+ echo "All upstream changes already present in main (possibly via squash/rebase); no PR needed"
108115 fi
109116
110117 - name : Create or update PR to main
111118 if : steps.diff.outputs.in_sync == 'false' && steps.branch_diff.outputs.has_changes == 'true'
112119 uses : actions/github-script@v7
113120 with :
121+ github-token : ${{ secrets.GITHUB_TOKEN }}
114122 script : |
115123 const {owner, repo} = context.repo;
116124 const headRef = `${owner}:${process.env.SYNC_BRANCH}`;
@@ -171,12 +179,12 @@ jobs:
171179 git log upstream/${BASE_BRANCH} --oneline -5 || true
172180 echo ""
173181 echo "Hotfix branches (preserved, untouched by this workflow):"
174- git for-each-ref --format='%(refname:short)' refs/ heads/ hotfix/ || echo " No hotfix/* branches found"
182+ git ls-remote --heads origin ' hotfix/*' | awk '{print $2}' || echo " No hotfix/* branches found"
175183 echo "=========================="
176184
177185 - name : Notify on failure
178186 if : failure()
179187 run : |
180188 echo "::error::🚨 Mirror sync failed! Manual intervention required."
181189 echo "::error::Repository: ${{ github.repository }}"
182- echo "::error::Run: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
190+ echo "::error::Run: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
0 commit comments