fix(core): handle checkIsRepo failure in GitService.initialize (#15574)

This commit is contained in:
Maple!
2025-12-27 02:37:57 +08:00
committed by GitHub
parent acecd80afa
commit 21388a0a40
2 changed files with 35 additions and 1 deletions
@@ -60,6 +60,15 @@ vi.mock('os', async (importOriginal) => {
}; };
}); });
const hoistedMockDebugLogger = vi.hoisted(() => ({
debug: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
}));
vi.mock('../utils/debugLogger.js', () => ({
debugLogger: hoistedMockDebugLogger,
}));
describe('GitService', () => { describe('GitService', () => {
let testRootDir: string; let testRootDir: string;
let projectRoot: string; let projectRoot: string;
@@ -248,6 +257,21 @@ describe('GitService', () => {
await service.setupShadowGitRepository(); await service.setupShadowGitRepository();
expect(hoistedMockCommit).not.toHaveBeenCalled(); expect(hoistedMockCommit).not.toHaveBeenCalled();
}); });
it('should handle checkIsRepo failure gracefully and initialize repo', async () => {
// Simulate checkIsRepo failing (e.g., on certain Git versions like macOS 2.39.5)
hoistedMockCheckIsRepo.mockRejectedValue(
new Error('git rev-parse --is-inside-work-tree failed'),
);
const service = new GitService(projectRoot, storage);
await service.setupShadowGitRepository();
// Should proceed to initialize the repo since checkIsRepo failed
expect(hoistedMockInit).toHaveBeenCalled();
// Should log the error using debugLogger
expect(hoistedMockDebugLogger.debug).toHaveBeenCalledWith(
expect.stringContaining('checkIsRepo failed'),
);
});
}); });
describe('createFileSnapshot', () => { describe('createFileSnapshot', () => {
+11 -1
View File
@@ -11,6 +11,7 @@ import { spawnAsync } from '../utils/shell-utils.js';
import type { SimpleGit } from 'simple-git'; import type { SimpleGit } from 'simple-git';
import { simpleGit, CheckRepoActions } from 'simple-git'; import { simpleGit, CheckRepoActions } from 'simple-git';
import type { Storage } from '../config/storage.js'; import type { Storage } from '../config/storage.js';
import { debugLogger } from '../utils/debugLogger.js';
export class GitService { export class GitService {
private projectRoot: string; private projectRoot: string;
@@ -67,7 +68,16 @@ export class GitService {
await fs.writeFile(gitConfigPath, gitConfigContent); await fs.writeFile(gitConfigPath, gitConfigContent);
const repo = simpleGit(repoDir); const repo = simpleGit(repoDir);
const isRepoDefined = await repo.checkIsRepo(CheckRepoActions.IS_REPO_ROOT); let isRepoDefined = false;
try {
isRepoDefined = await repo.checkIsRepo(CheckRepoActions.IS_REPO_ROOT);
} catch (error) {
// If checkIsRepo fails (e.g., on certain Git versions like macOS 2.39.5),
// log the error and assume repo is not defined, then proceed with initialization
debugLogger.debug(
`checkIsRepo failed, will initialize repository: ${error instanceof Error ? error.message : String(error)}`,
);
}
if (!isRepoDefined) { if (!isRepoDefined) {
await repo.init(false, { await repo.init(false, {