Skip to content

Commit 2ff37dd

Browse files
authored
Fix freeze patch branching logic (#1590)
The code that applies freeze patches assumed that the caller would always want to return to `main` afterwards. That assumption is only valid when curation runs against `main`, not when it runs as part of a pull request, or locally when preparing a pull request on a different branch. The new logic gets back to the right position, either a branch name if there's one, or a commit ID if contex is in "detached HEAD" mode.
1 parent 1fe5ddc commit 2ff37dd

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

tools/apply-patches.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ async function applyFreezePatches(rawFolder, outputFolder) {
116116
const outputIndex = await loadJSON(path.join(outputFolder, 'index.json'));
117117
let patchApplied = false;
118118

119+
const startingRef = await getCurrentRef();
120+
119121
for (const file of patchFiles) {
120122
if (!file.endsWith('.json')) {
121123
continue;
@@ -147,7 +149,7 @@ async function applyFreezePatches(rawFolder, outputFolder) {
147149
outputIndex.results.splice(outputSpecPos, 1, crawlSpec);
148150
}
149151

150-
await execFile('git', ['checkout', 'main']);
152+
await execFile('git', ['checkout', startingRef]);
151153
patchApplied = true;
152154
}
153155

@@ -162,6 +164,24 @@ async function applyFreezePatches(rawFolder, outputFolder) {
162164
}
163165

164166

167+
/**
168+
* Retrieve a meaningful name for the current position in Git,
169+
* either a branch name if possible (that is typically possible when curation
170+
* runs locally from a branch), or a commit ID (which is typically what happens
171+
* when curation runs in "detached HEAD" mode as in GitHub jobs).
172+
*/
173+
async function getCurrentRef() {
174+
const { stdout } = await execFile('git', ['branch', '--show-current']);
175+
let currentRef = stdout.trim();
176+
if (!currentRef) {
177+
// The code runs in detached HEAD mode
178+
const { stdout: commitOut } = await execFile('git', ['rev-parse', 'HEAD']);
179+
currentRef = commitOut.trim();
180+
}
181+
return currentRef;
182+
}
183+
184+
165185
/**************************************************
166186
Export methods for use as module
167187
**************************************************/

0 commit comments

Comments
 (0)