Skip to content

Commit e81c5e5

Browse files
authored
fix: bundle transport fails when agent commits to HEAD instead of named branch (#24317)
1 parent 9b9bc12 commit e81c5e5

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

actions/setup/js/generate_git_bundle.cjs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,23 @@ async function generateGitBundle(branchName, baseBranch, options = {}) {
283283
debugLog(`Strategy 2: Found ${commitCount} commits between GITHUB_SHA and HEAD`);
284284

285285
if (commitCount > 0) {
286-
execGitSync(["bundle", "create", bundlePath, `${githubSha}..HEAD`], { cwd });
286+
// If branchName is provided and doesn't exist locally (Strategy 1 already failed),
287+
// create a local branch pointing to HEAD so the bundle contains
288+
// refs/heads/<branchName> — required by create_pull_request.cjs when applying the bundle.
289+
let rangeEnd = "HEAD";
290+
if (branchName) {
291+
try {
292+
// Use -f (force) to overwrite any stale local branch from previous runs,
293+
// since Strategy 1 verified the named branch does not exist as a proper local ref.
294+
// Use -- so a branch name beginning with "-" is not parsed as another option.
295+
execGitSync(["branch", "-f", "--", branchName, "HEAD"], { cwd });
296+
rangeEnd = branchName;
297+
debugLog(`Strategy 2: Created local branch '${branchName}' pointing to HEAD for bundle ref`);
298+
} catch (branchErr) {
299+
debugLog(`Strategy 2: Could not create branch '${branchName}': ${getErrorMessage(branchErr)}, using HEAD`);
300+
}
301+
}
302+
execGitSync(["bundle", "create", bundlePath, `${githubSha}..${rangeEnd}`], { cwd });
287303

288304
if (fs.existsSync(bundlePath)) {
289305
const stat = fs.statSync(bundlePath);

0 commit comments

Comments
 (0)