@@ -151,14 +151,36 @@ jobs:
151151 git config user.name "github-actions[bot]"
152152 git config user.email "github-actions[bot]@users.noreply.github.com"
153153
154+ # First, check whether regenerated content actually differs from what
155+ # is already on disk (relative to the workflow's HEAD).
154156 git add eng/tools/emitter/gen/
155157 if git diff --cached --quiet; then
156158 echo "No changes to commit"
157159 echo "has_changes=false" >> $GITHUB_OUTPUT
158160 exit 0
159161 fi
160162
161- git checkout -B "$BRANCH"
163+ # Branch off origin/main (NOT the current HEAD) so the auto branch
164+ # never contains unrelated commits from the branch that triggered
165+ # the workflow. In particular this avoids carrying changes to
166+ # .github/workflows/*, which GITHUB_TOKEN is not allowed to push
167+ # (missing `workflows` permission).
168+ git fetch --no-tags --depth=1 origin main
169+ git checkout -B "$BRANCH" origin/main
170+
171+ # Re-apply just the regenerated gen/ tree on top of origin/main.
172+ # HEAD@{1} is the workflow's original HEAD before the checkout above.
173+ git checkout HEAD@{1} -- eng/tools/emitter/gen
174+ git add eng/tools/emitter/gen/
175+
176+ # If origin/main already matches the regenerated output, there is
177+ # nothing to push.
178+ if git diff --cached --quiet; then
179+ echo "No changes vs origin/main"
180+ echo "has_changes=false" >> $GITHUB_OUTPUT
181+ exit 0
182+ fi
183+
162184 git commit -m "[typespec-python] Regenerate tests from ${SOURCE_LABEL}"
163185 git push origin "$BRANCH" --force
164186 echo "has_changes=true" >> $GITHUB_OUTPUT
0 commit comments