fix: Git Diff View works in subdirectories

Remove explicit .git directory checks from executeGitCommand functions.
Git automatically searches parent directories for .git, making these
checks unnecessary and preventing subdirectory execution.

Changes:
- src/server/core/git/functions/utils.ts: Remove .git existence check
- src/server/core/git/services/GitService.ts: Remove .git existence check
- src/server/core/git/functions/getDiff.test.ts: Add subdirectory test case

Fixes #25

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
d-kimsuon
2025-10-23 03:26:31 +09:00
parent 9c296671ee
commit 7ac09bbd6a
3 changed files with 41 additions and 16 deletions

View File

@@ -367,6 +367,44 @@ index abc123..abc123 100644`;
});
describe("エッジケース", () => {
it("サブディレクトリから実行しても動作する", async () => {
const mockCwd = "/test/repo/subdirectory";
const fromRef = "base:main";
const toRef = "compare:feature";
const mockNumstatOutput = `3\t1\tsrc/file.ts`;
const mockDiffOutput = `diff --git a/src/file.ts b/src/file.ts
index abc123..def456 100644
--- a/src/file.ts
+++ b/src/file.ts
@@ -1,2 +1,3 @@
content`;
vi.mocked(utils.executeGitCommand)
.mockResolvedValueOnce({
success: true,
data: mockNumstatOutput,
})
.mockResolvedValueOnce({
success: true,
data: mockDiffOutput,
});
const result = await getDiff(mockCwd, fromRef, toRef);
expect(result.success).toBe(true);
if (result.success) {
expect(result.data.files).toHaveLength(1);
expect(result.data.files[0]?.filePath).toBe("src/file.ts");
}
// Verify that git commands are executed in the subdirectory
expect(utils.executeGitCommand).toHaveBeenCalledWith(
["diff", "--numstat", "main", "feature"],
mockCwd,
);
});
it("特殊文字を含むファイル名を処理できる", async () => {
const mockCwd = "/test/repo";
const fromRef = "base:main";

View File

@@ -15,7 +15,7 @@ export async function executeGitCommand(
cwd: string,
): Promise<GitResult<string>> {
try {
// Check if the directory exists and contains a git repository
// Check if the directory exists
if (!existsSync(cwd)) {
return {
success: false,
@@ -27,16 +27,7 @@ export async function executeGitCommand(
};
}
if (!existsSync(resolve(cwd, ".git"))) {
return {
success: false,
error: {
code: "NOT_A_REPOSITORY",
message: `Not a git repository: ${cwd}`,
command: `git ${args.join(" ")}`,
},
};
}
// Git will search parent directories for .git, so we don't need to check explicitly
const { stdout } = await execFileAsync("git", args, {
cwd,

View File

@@ -33,11 +33,7 @@ const LayerImpl = Effect.gen(function* () {
);
}
if (!(yield* fs.exists(path.resolve(absoluteCwd, ".git")))) {
return yield* Effect.fail(
new NotARepositoryError({ cwd: absoluteCwd }),
);
}
// Git will search parent directories for .git, so we don't need to check explicitly
const command = Command.make("git", ...args).pipe(
Command.workingDirectory(absoluteCwd),