From 4e0bd579dc7276c537cae19ebefac46135cf66eb Mon Sep 17 00:00:00 2001 From: neverland Date: Sun, 9 Aug 2026 16:59:55 +0800 Subject: [PATCH] fix: inspect Git worktree status --- src/index.ts | 5 ++++- test/custom-tools.test.ts | 2 +- test/git.test.ts | 19 +++++++++++++++---- test/skills.test.ts | 2 +- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index eba15c4..70a2aff 100644 --- a/src/index.ts +++ b/src/index.ts @@ -560,7 +560,10 @@ function initGit(cwd: string) { ); // Reuse the current repository instead of creating a nested one. - if (repositoryCheck.exitCode === 0) { + if ( + repositoryCheck.exitCode === 0 && + repositoryCheck.stdout.trim() === 'true' + ) { return; } diff --git a/test/custom-tools.test.ts b/test/custom-tools.test.ts index 08c0a6b..70e5e0d 100644 --- a/test/custom-tools.test.ts +++ b/test/custom-tools.test.ts @@ -29,7 +29,7 @@ const mocks = rs.hoisted(() => { // rslint-disable-next-line @typescript-eslint/no-explicit-any }) as any, // rslint-disable-next-line @typescript-eslint/no-explicit-any - xSync: rs.fn(() => ({ stdout: '', stderr: '', exitCode: 0 })) as any, + xSync: rs.fn(() => ({ stdout: 'true\n', stderr: '', exitCode: 0 })) as any, }; }); diff --git a/test/git.test.ts b/test/git.test.ts index c1decdb..e6da020 100644 --- a/test/git.test.ts +++ b/test/git.test.ts @@ -18,8 +18,8 @@ rs.mock('tinyexec', () => ({ xSync: mocks.xSync, })); -const createResult = (exitCode: number, stderr = '') => ({ - stdout: '', +const createResult = (exitCode: number, stdout = '', stderr = '') => ({ + stdout, stderr, exitCode, }); @@ -71,7 +71,7 @@ test('should initialize a Git repository by default', async () => { test('should reuse an existing Git repository', async () => { const projectDir = path.join(testDir, 'existing'); - rs.mocked(mocks.xSync).mockReturnValue(createResult(0)); + rs.mocked(mocks.xSync).mockReturnValue(createResult(0, 'true\n')); await createProject(projectDir); @@ -83,6 +83,17 @@ test('should reuse an existing Git repository', async () => { ); }); +test('should initialize Git when the current repository is bare', async () => { + const projectDir = path.join(testDir, 'bare'); + rs.mocked(mocks.xSync).mockReturnValueOnce(createResult(0, 'false\n')); + + await createProject(projectDir); + + expect(mocks.xSync).toHaveBeenNthCalledWith(2, 'git', ['init'], { + nodeOptions: { cwd: projectDir }, + }); +}); + test('should skip Git initialization when disabled', async () => { const projectDir = path.join(testDir, 'disabled'); @@ -96,7 +107,7 @@ test('should continue when Git initialization fails', async () => { rs.mocked(mocks.xSync).mockImplementation((_command, args) => args[0] === 'rev-parse' ? createResult(128) - : createResult(1, 'Git is unavailable'), + : createResult(1, '', 'Git is unavailable'), ); await expect(createProject(projectDir)).resolves.toBeUndefined(); diff --git a/test/skills.test.ts b/test/skills.test.ts index 1bf64fc..215270e 100644 --- a/test/skills.test.ts +++ b/test/skills.test.ts @@ -69,7 +69,7 @@ const mocks = rs.hoisted(() => { const xSync = rs.fn((command: string, args: string[], options: unknown) => { return { - stdout: '', + stdout: 'true\n', stderr: '', exitCode: 0, };