This commit is contained in:
cruldra committed 2026-07-31 06:52:09 +08:00
1 parent 532f9e09b5
commit 0d078e848c
6 files changed
+208 -50

No files matched your search

+87
View File
@@ -0,0 +1,87 @@
import { execFileSync } from 'node:child_process'
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import * as os from 'node:os'
import * as path from 'node:path'
import { afterEach, describe, expect, it } from 'vitest'
import { deleteLocalBranch, resolveFeatureBranch } from './branchSync'
const tempDirs: string[] = []
afterEach(() => {
while (tempDirs.length > 0) {
const dir = tempDirs.pop()
if (dir)
rmSync(dir, { recursive: true, force: true })
}
})
function tempDir(): string {
const dir = mkdtempSync(path.join(os.tmpdir(), 'branch-sync-'))
tempDirs.push(dir)
return dir
}
function git(cwd: string, args: string[]): void {
execFileSync('git', ['-C', cwd, ...args], { stdio: 'ignore' })
}
function seedCommit(root: string): void {
writeFileSync(path.join(root, 'f'), 'a\n')
git(root, ['add', 'f'])
git(root, ['commit', '-m', 'i'])
}
function initRepoWithWorktree(branch: string): { root: string, worktree: string } {
const root = tempDir()
git(root, ['init', '-b', 'main'])
git(root, ['config', 'user.email', 't@t.com'])
git(root, ['config', 'user.name', 't'])
seedCommit(root)
const worktree = path.join(root, 'wt')
git(root, ['worktree', 'add', worktree, '-b', branch])
return { root, worktree }
}
describe('resolveFeatureBranch', () => {
it('优先用 state.branch', () => {
expect(resolveFeatureBranch({ stateBranch: 'feature/abc', prHeadRef: 'feature/other' }))
.toBe('feature/abc')
})
it('state 缺 branch 时回退 PR head.ref', () => {
expect(resolveFeatureBranch({ stateBranch: '', prHeadRef: 'feature/from-pr' }))
.toBe('feature/from-pr')
})
it('不回退到 main/master', () => {
expect(resolveFeatureBranch({ stateBranch: undefined, prHeadRef: 'main' })).toBe('')
expect(resolveFeatureBranch({ stateBranch: ' ', prHeadRef: 'master' })).toBe('')
})
})
describe('deleteLocalBranch', () => {
it('强删 worktree 目录后仍能删掉本地 feature 分支', async () => {
const branch = 'feature/leftover'
const { root, worktree } = initRepoWithWorktree(branch)
// 复现完成列路径:rm -rf 目录(可能留下 prunable 注册)
rmSync(worktree, { recursive: true, force: true })
const result = await deleteLocalBranch(root, branch)
expect(result.ok).toBe(true)
const heads = execFileSync('git', ['-C', root, 'branch', '--list', branch], { encoding: 'utf8' }).trim()
expect(heads).toBe('')
})
it('分支不存在时视为已清理', async () => {
const root = tempDir()
git(root, ['init', '-b', 'main'])
git(root, ['config', 'user.email', 't@t.com'])
git(root, ['config', 'user.name', 't'])
seedCommit(root)
const result = await deleteLocalBranch(root, 'feature/missing')
expect(result.ok).toBe(true)
})
})
+38 -2
View File
@@ -62,18 +62,54 @@ export async function gitFetch(workspaceRoot: string): Promise<{ ok: boolean, st
return { ok: r.ok, stderr: r.stderr, stdout: r.stdout }
}
/**
* 解析要清理的 feature 分支名。
* state.branch 优先;缺省时回退 PR head.ref。永不回退到 main/master。
*/
export function resolveFeatureBranch(opts: {
stateBranch?: string | null
prHeadRef?: string | null
}): string {
const fromState = (opts.stateBranch ?? '').trim()
if (fromState)
return fromState
const fromPr = (opts.prHeadRef ?? '').trim()
if (!fromPr || fromPr === 'main' || fromPr === 'master')
return ''
return fromPr
}
/**
* 删除本地分支。分支不存在时视为已清理,避免重复完成流程产生噪音。
*
* 完成列路径会先 `rm -rf` worktree 再 prune;若 prune 未生效,git 仍认为
* 分支被 worktree 占用,`branch -D` 会失败。这里在失败时再 prune 一次并重试。
* 同时清掉 `refs/remotes/origin/<branch>`,避免 UI 里仍显示远程残留。
*/
export async function deleteLocalBranch(workspaceRoot: string, branch: string): Promise<{ ok: boolean, stdout: string, stderr: string }> {
const exists = await runGit(workspaceRoot, ['show-ref', '--verify', '--quiet', `refs/heads/${branch}`])
if (!exists.ok)
if (!exists.ok) {
await pruneRemoteTrackingRef(workspaceRoot, branch)
return { ok: true, stdout: '', stderr: '' }
}
const r = await runGit(workspaceRoot, ['branch', '-D', branch])
let r = await runGit(workspaceRoot, ['branch', '-D', branch])
if (!r.ok) {
await runGit(workspaceRoot, ['worktree', 'prune'])
r = await runGit(workspaceRoot, ['branch', '-D', branch])
}
await pruneRemoteTrackingRef(workspaceRoot, branch)
return { ok: r.ok, stdout: r.stdout, stderr: r.stderr }
}
async function pruneRemoteTrackingRef(workspaceRoot: string, branch: string): Promise<void> {
const ref = `refs/remotes/origin/${branch}`
const exists = await runGit(workspaceRoot, ['show-ref', '--verify', '--quiet', ref])
if (!exists.ok)
return
await runGit(workspaceRoot, ['update-ref', '-d', ref])
}
export interface CheckBranchSyncOpts {
workspaceRoot: string
/** Already non-empty (caller falls back to devBranch when autoBuild is ''). */