Merge pull request #26 from d-kimuson/fix/git-diff-view-subdirectory

This commit is contained in:
きむそん
2025-10-24 17:57:19 +09:00
committed by GitHub
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),