From b786bb2b8e2447e2a1f3616daab2bfe8f0b23c3f Mon Sep 17 00:00:00 2001 From: Jonathan Miller Date: Sat, 30 May 2026 09:49:39 -0500 Subject: [PATCH] fix(cli): checkout branch before commit to fix detached HEAD push During PR merge, CI runs on a detached HEAD. The commit+push to the branch would fail silently. Now explicitly fetches and checks out the target branch before committing release changes. Authored-by: Moko Consulting Co-Authored-By: Claude Opus 4.6 (1M context) --- cli/release_publish.php | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/cli/release_publish.php b/cli/release_publish.php index 845c406..07eb56c 100644 --- a/cli/release_publish.php +++ b/cli/release_publish.php @@ -166,18 +166,33 @@ if (!$dryRun) { // -- Step 2c: Commit version changes before building -- $root = realpath($path) ?: $path; if (!$dryRun) { + // Configure git + @shell_exec("cd " . escapeshellarg($root) . " && git config --local user.email \"gitea-actions[bot]@mokoconsulting.tech\""); + @shell_exec("cd " . escapeshellarg($root) . " && git config --local user.name \"gitea-actions[bot]\""); + if (!empty($repoUrl)) { + @shell_exec("cd " . escapeshellarg($root) . " && git remote set-url origin " . escapeshellarg($repoUrl)); + } + + // Ensure we're on the actual branch (not detached HEAD from PR merge) + @shell_exec("cd " . escapeshellarg($root) . " && git fetch origin " . escapeshellarg($branch) . " 2>/dev/null"); + @shell_exec("cd " . escapeshellarg($root) . " && git checkout -B " . escapeshellarg($branch) . " FETCH_HEAD 2>/dev/null"); + + // Re-apply version changes on the checked-out branch + passthru("{$php} {$cli}/version_set_platform.php --path " . escapeshellarg($path) + . " --version " . escapeshellarg($baseVersion) + . " --branch " . escapeshellarg($branch) + . " --stability " . escapeshellarg($stability) . " 2>/dev/null"); + passthru("{$php} {$cli}/version_check.php --path " . escapeshellarg($path) . " --fix 2>/dev/null"); + passthru("{$php} {$cli}/badge_update.php --path " . escapeshellarg($path) . " --version " . escapeshellarg($baseVersion) . " 2>/dev/null"); + $diffCheck = trim((string) @shell_exec("cd " . escapeshellarg($root) . " && git diff --quiet && git diff --cached --quiet 2>&1 && echo clean || echo dirty")); if ($diffCheck === 'dirty') { - @shell_exec("cd " . escapeshellarg($root) . " && git config --local user.email \"gitea-actions[bot]@mokoconsulting.tech\""); - @shell_exec("cd " . escapeshellarg($root) . " && git config --local user.name \"gitea-actions[bot]\""); - if (!empty($repoUrl)) { - @shell_exec("cd " . escapeshellarg($root) . " && git remote set-url origin " . escapeshellarg($repoUrl)); - } @shell_exec("cd " . escapeshellarg($root) . " && git add -A"); @shell_exec("cd " . escapeshellarg($root) . " && git commit -m " . escapeshellarg("chore(release): build {$releaseVersion} [skip ci]") . " --author=\"gitea-actions[bot] \""); - @shell_exec("cd " . escapeshellarg($root) . " && git push origin HEAD:refs/heads/" . escapeshellarg($branch) . " 2>&1"); + $pushResult = @shell_exec("cd " . escapeshellarg($root) . " && git push origin " . escapeshellarg($branch) . " 2>&1"); echo " Committed release changes\n"; + echo " Push: " . trim($pushResult ?? '') . "\n"; } }