@@ -52,29 +52,31 @@ jobs:
5252 uses : actions/checkout@v4
5353 with :
5454 fetch-depth : 0
55- - name : Create merge commit
55+ - name : Check if there are commits to merge
5656 id : create_merge_commit
5757 run : |
5858 # Without this, we can't perform git operations in GitHub actions.
5959 git config --global --add safe.directory "$(realpath .)"
6060 git config --local user.name 'swift-ci'
6161 git config --local user.email 'swift-ci@users.noreply.github.com'
6262
63- git checkout ${{ inputs.base_branch }}
64- git merge ${{ inputs.head_branch }}
65-
66- if [[ "$(git rev-parse HEAD)" = "$(git rev-parse ${{ inputs.head_branch }})" ]]; then
67- echo "has_merged_commits=true" >> "$GITHUB_OUTPUT"
68- else
69- echo "has_merged_commits=false" >> "$GITHUB_OUTPUT"
63+ if [[ "$(git rev-list --left-only --count origin/${{ inputs.head_branch }}...origin/${{ inputs.base_branch }})" == 0 ]]; then
64+ echo "Nothing to merge"
65+ echo "has_commits_to_merge=false" >> "$GITHUB_OUTPUT"
66+ exit
7067 fi
68+
69+ echo "has_commits_to_merge=true" >> "$GITHUB_OUTPUT"
7170 - name : Push branch and create PR
7271 id : push_branch
73- if : ${{ steps.create_merge_commit.outputs.has_merged_commits == 'true' }}
72+ if : ${{ steps.create_merge_commit.outputs.has_commits_to_merge == 'true' }}
7473 env :
7574 GH_TOKEN : ${{ github.token }}
7675 run : |
76+ # Create a branch for the PR instead of opening a PR that merges head_branch directly so that we have a fixed
77+ # target in the PR and don't modify the PR as new commits are put on the head branch.
7778 PR_BRANCH="automerge/merge-main-$(date +%Y-%m-%d_%H-%M)"
79+ git checkout ${{ inputs.head_branch }}
7880 git checkout -b "$PR_BRANCH"
7981 git push --set-upstream origin "$PR_BRANCH"
8082
0 commit comments