Skip to content

Commit 8666de0

Browse files
committed
Reset branches more correctly
1 parent d0a5465 commit 8666de0

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

src/commands/fix/agent-fix.mts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import { getActualTree } from './get-actual-tree.mts'
2323
import {
2424
getSocketBranchName,
2525
getSocketCommitMessage,
26+
gitCheckoutBranch,
2627
gitCreateAndPushBranch,
28+
gitDeleteBranch,
2729
gitRemoteBranchExists,
2830
gitResetAndClean,
2931
gitUnstagedModifiedFiles,
@@ -395,6 +397,8 @@ export async function agentFix(
395397
if (fixEnv.isCi) {
396398
// eslint-disable-next-line no-await-in-loop
397399
await gitResetAndClean(fixEnv.baseBranch, cwd)
400+
// eslint-disable-next-line no-await-in-loop
401+
await gitCheckoutBranch(fixEnv.baseBranch, cwd)
398402
}
399403
continue infosLoop
400404
}
@@ -480,6 +484,10 @@ export async function agentFix(
480484
// eslint-disable-next-line no-await-in-loop
481485
await gitResetAndClean(fixEnv.baseBranch, cwd)
482486
// eslint-disable-next-line no-await-in-loop
487+
await gitCheckoutBranch(fixEnv.baseBranch, cwd)
488+
// eslint-disable-next-line no-await-in-loop
489+
await gitDeleteBranch(branch, cwd)
490+
// eslint-disable-next-line no-await-in-loop
483491
const maybeActualTree = await installer(pkgEnvDetails, {
484492
cwd,
485493
spinner,
@@ -551,7 +559,9 @@ export async function agentFix(
551559
if (fixEnv.isCi) {
552560
spinner?.start()
553561
// eslint-disable-next-line no-await-in-loop
554-
await gitResetAndClean(fixEnv.baseBranch, cwd)
562+
await gitResetAndClean(branch, cwd)
563+
// eslint-disable-next-line no-await-in-loop
564+
await gitCheckoutBranch(fixEnv.baseBranch, cwd)
555565
// eslint-disable-next-line no-await-in-loop
556566
const maybeActualTree = await installer(pkgEnvDetails, {
557567
cwd,

src/commands/fix/git.mts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,31 @@ export async function gitCleanFdx(cwd = process.cwd()): Promise<void> {
224224
await spawn('git', ['clean', '-fdx'], stdioIgnoreOptions)
225225
}
226226

227+
export async function gitCheckoutBranch(
228+
branch: string,
229+
cwd = process.cwd(),
230+
): Promise<boolean> {
231+
const stdioIgnoreOptions: SpawnOptions = {
232+
cwd,
233+
stdio: isDebug('stdio') ? 'inherit' : 'ignore',
234+
}
235+
try {
236+
await spawn('git', ['checkout', branch], stdioIgnoreOptions)
237+
return true
238+
} catch {}
239+
return false
240+
}
241+
227242
export async function gitCreateAndPushBranch(
228243
branch: string,
229244
commitMsg: string,
230245
filepaths: string[],
231246
options?: GitCreateAndPushBranchOptions | undefined,
232247
): Promise<boolean> {
248+
if (!filepaths.length) {
249+
debugFn('notice', `miss: no filepaths to add`)
250+
return false
251+
}
233252
const {
234253
cwd = process.cwd(),
235254
// Lazily access constants.ENV.SOCKET_CLI_GIT_USER_EMAIL.
@@ -259,9 +278,21 @@ export async function gitCreateAndPushBranch(
259278
)
260279
debugDir('inspect', { error: e })
261280
}
281+
return false
282+
}
283+
284+
export async function gitDeleteBranch(
285+
branch: string,
286+
cwd = process.cwd(),
287+
): Promise<boolean> {
288+
const stdioIgnoreOptions: SpawnOptions = {
289+
cwd,
290+
stdio: isDebug('stdio') ? 'inherit' : 'ignore',
291+
}
262292
try {
263293
// Will throw with exit code 1 if branch does not exist.
264294
await spawn('git', ['branch', '-D', branch], stdioIgnoreOptions)
295+
return true
265296
} catch {}
266297
return false
267298
}

0 commit comments

Comments
 (0)