Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/utils/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ describe("getDiff warning on failure", () => {
try {
const result = await getDiff("/nonexistent/path/thinktank-test");
assert.equal(result, "");
assert.equal(warnings.length, 1);
assert.ok(warnings[0].includes("getDiff failed"));
assert.ok(warnings[0].includes("/nonexistent/path/thinktank-test"));
assert.ok(warnings.length >= 1);
assert.ok(warnings[0]!.includes("getDiff"));
assert.ok(warnings[0]!.includes("thinktank-test"));
} finally {
console.warn = originalWarn;
}
Expand All @@ -81,9 +81,9 @@ describe("getDiffStats warning on failure", () => {
try {
const result = await getDiffStats("/nonexistent/path/thinktank-test");
assert.deepEqual(result, { filesChanged: [], linesAdded: 0, linesRemoved: 0 });
assert.equal(warnings.length, 1);
assert.ok(warnings[0].includes("getDiffStats failed"));
assert.ok(warnings[0].includes("/nonexistent/path/thinktank-test"));
assert.ok(warnings.length >= 1);
assert.ok(warnings[0]!.includes("getDiffStats"));
assert.ok(warnings[0]!.includes("thinktank-test"));
} finally {
console.warn = originalWarn;
}
Expand Down
37 changes: 17 additions & 20 deletions src/utils/git.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { execFile } from "node:child_process";
import { randomUUID } from "node:crypto";
import { mkdtemp, rm } from "node:fs/promises";
import { access, mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { dirname, join, resolve } from "node:path";
import { promisify } from "node:util";

const exec = promisify(execFile);
Expand Down Expand Up @@ -75,21 +75,19 @@ export async function removeWorktree(worktreePath: string): Promise<void> {
}

export async function getDiff(worktreePath: string): Promise<string> {
const absPath = resolve(worktreePath);
try {
// Include both staged and unstaged changes relative to HEAD
// Exclude node_modules symlink (created by createWorktree for tool access)
await exec("git", ["add", "-A"], { cwd: worktreePath });
// Unstage node_modules symlink if it got picked up (created by createWorktree)
await exec("git", ["reset", "HEAD", "--", "node_modules"], { cwd: worktreePath }).catch(
() => {},
);
const { stdout } = await exec("git", ["diff", "--cached", "HEAD"], {
cwd: worktreePath,
});
// Verify worktree is still a git repo before running git commands
await access(join(absPath, ".git"));
await exec("git", ["rev-parse", "--git-dir"], { cwd: absPath });

await exec("git", ["add", "-A"], { cwd: absPath });
await exec("git", ["reset", "HEAD", "--", "node_modules"], { cwd: absPath }).catch(() => {});
const { stdout } = await exec("git", ["diff", "--cached", "HEAD"], { cwd: absPath });
return stdout;
} catch (err) {
console.warn(
`[thinktank] getDiff failed for worktree ${worktreePath}: ${err instanceof Error ? err.message : String(err)}`,
`[thinktank] getDiff failed for ${absPath}: ${err instanceof Error ? err.message : String(err)}`,
);
return "";
}
Expand All @@ -98,14 +96,13 @@ export async function getDiff(worktreePath: string): Promise<string> {
export async function getDiffStats(
worktreePath: string,
): Promise<{ filesChanged: string[]; linesAdded: number; linesRemoved: number }> {
const absPath = resolve(worktreePath);
try {
await exec("git", ["add", "-A"], { cwd: worktreePath });
// Unstage node_modules symlink if it got picked up (created by createWorktree)
await exec("git", ["reset", "HEAD", "--", "node_modules"], { cwd: worktreePath }).catch(
() => {},
);
await access(join(absPath, ".git"));
await exec("git", ["add", "-A"], { cwd: absPath });
await exec("git", ["reset", "HEAD", "--", "node_modules"], { cwd: absPath }).catch(() => {});
const { stdout } = await exec("git", ["diff", "--cached", "--stat", "HEAD"], {
cwd: worktreePath,
cwd: absPath,
});

const filesChanged: string[] = [];
Expand All @@ -126,7 +123,7 @@ export async function getDiffStats(
return { filesChanged, linesAdded, linesRemoved };
} catch (err) {
console.warn(
`[thinktank] getDiffStats failed for worktree ${worktreePath}: ${err instanceof Error ? err.message : String(err)}`,
`[thinktank] getDiffStats failed for ${absPath}: ${err instanceof Error ? err.message : String(err)}`,
);
return { filesChanged: [], linesAdded: 0, linesRemoved: 0 };
}
Expand Down
Loading