🐛 fix(vscode): 实施启动自动丢掉残留 worktree 再新建

This commit is contained in:
2026-08-25 16:04:45 +08:00
parent 23ebdc0509
commit afdb98ad55
3 changed files with 36 additions and 32 deletions
+10 -9
View File
@@ -77,7 +77,7 @@ describe('ensureWorktree', () => {
fx = undefined
})
it('本地已有 feature 分支且无 worktree 时挂上该分支并保留 commit', async () => {
it('本地已有 feature 分支且无 worktree 时丢掉旧 commit 再新建', async () => {
fx = await setupRepo()
git(fx.repo, ['checkout', '-b', 'feature/x'])
writeFileSync(path.join(fx.repo, 'README.md'), 'hello\nextra-1\n')
@@ -101,8 +101,7 @@ describe('ensureWorktree', () => {
expect(path.resolve(actual)).toBe(path.resolve(fx.templatePath))
expect(git(actual, ['rev-parse', '--abbrev-ref', 'HEAD'])).toBe('feature/x')
expect(git(fx.repo, ['log', '--oneline', 'main..feature/x'])).toBe(ahead)
expect(git(actual, ['rev-parse', 'HEAD'])).toBe(git(fx.repo, ['rev-parse', 'feature/x']))
expect(git(fx.repo, ['log', '--oneline', 'main..feature/x'])).toBe('')
})
it('分支和 worktree 都不存在时创建新分支 worktree', async () => {
@@ -119,7 +118,7 @@ describe('ensureWorktree', () => {
expect(git(fx.repo, ['show-ref', '--verify', '--quiet', 'refs/heads/feature/fresh'])).toBe('')
})
it('该 branch 已有 live worktree 时返回已有 path,不建第二个', async () => {
it('该 branch 已有 live worktree 时删掉旧 path 在模板 path 新建', async () => {
fx = await setupRepo()
git(fx.repo, ['branch', 'feature/x'])
const existing = path.join(fx.root, 'sw-ensure', 'wt', 'already-here')
@@ -132,9 +131,9 @@ describe('ensureWorktree', () => {
branch: 'feature/x',
})
expect(path.resolve(actual)).toBe(path.resolve(existing))
expect(existsSync(fx.templatePath)).toBe(false)
expect(git(existing, ['rev-parse', '--abbrev-ref', 'HEAD'])).toBe('feature/x')
expect(path.resolve(actual)).toBe(path.resolve(fx.templatePath))
expect(existsSync(existing)).toBe(false)
expect(git(actual, ['rev-parse', '--abbrev-ref', 'HEAD'])).toBe('feature/x')
const listed = git(fx.repo, ['worktree', 'list', '--porcelain'])
const wtLines = listed.split('\n').filter(l => l.startsWith('worktree '))
expect(wtLines).toHaveLength(2)
@@ -157,7 +156,7 @@ describe('ensureWorktree', () => {
expect(existsSync(path.join(fx.templatePath, 'README.md'))).toBe(true)
})
it('无 leftover 时可重复调用,第二次返回同一 path', async () => {
it('第二次调用仍成功且在同一模板 path', async () => {
fx = await setupRepo()
const first = await ensureWorktree({
@@ -165,14 +164,16 @@ describe('ensureWorktree', () => {
worktreePath: fx.templatePath,
branch: 'feature/x',
})
writeFileSync(path.join(first, 'leftover.txt'), 'should-be-gone')
const second = await ensureWorktree({
workspaceRoot: fx.repo,
worktreePath: fx.templatePath,
branch: 'feature/x',
})
expect(path.resolve(second)).toBe(path.resolve(first))
expect(path.resolve(second)).toBe(path.resolve(fx.templatePath))
expect(git(second, ['rev-parse', '--abbrev-ref', 'HEAD'])).toBe('feature/x')
expect(existsSync(path.join(second, 'leftover.txt'))).toBe(false)
const wtLines = git(fx.repo, ['worktree', 'list', '--porcelain'])
.split('\n')
.filter(l => l.startsWith('worktree '))