diff --git a/lib/git-changes.test.mjs b/lib/git-changes.test.mjs index 12b29f9e4..9359cdde5 100644 --- a/lib/git-changes.test.mjs +++ b/lib/git-changes.test.mjs @@ -167,3 +167,123 @@ test("git status reports a bare repository as no worktree and ignores an inherit assert.equal(status.isGitRepository, true); assert.deepEqual(status.files.map((f) => path.relative(repo, f.filePath)), ["note.txt"]); }); + +test("git status lists changes when the project path is a symlink (#322)", async (t) => { + const { getGitStatus } = await import("./git-changes.ts"); + const { execFileSync } = await import("node:child_process"); + const fs = await import("node:fs"); + const os = await import("node:os"); + const path = await import("node:path"); + // getGitStatus inherits process.env; isolate global/system attributes. + const saved = { GIT_CONFIG_GLOBAL: process.env.GIT_CONFIG_GLOBAL, GIT_CONFIG_NOSYSTEM: process.env.GIT_CONFIG_NOSYSTEM }; + process.env.GIT_CONFIG_GLOBAL = "/dev/null"; + process.env.GIT_CONFIG_NOSYSTEM = "1"; + const realRoot = fs.realpathSync.native(fs.mkdtempSync(path.join(os.tmpdir(), "git-symlink-real-"))); + const linkParent = fs.realpathSync.native(fs.mkdtempSync(path.join(os.tmpdir(), "git-symlink-link-"))); + const linkPath = path.join(linkParent, "example"); + t.after(() => { + for (const [key, value] of Object.entries(saved)) { + if (value === undefined) delete process.env[key]; + else process.env[key] = value; + } + fs.rmSync(realRoot, { recursive: true, force: true }); + fs.rmSync(linkParent, { recursive: true, force: true }); + }); + const git = (...args) => execFileSync("git", ["-C", realRoot, "-c", "user.name=t", "-c", "user.email=t@t", ...args]); + git("init", "-q"); + fs.writeFileSync(path.join(realRoot, "tracked.txt"), "v1\n"); + git("add", "-A"); + git("commit", "-qm", "init"); + fs.writeFileSync(path.join(realRoot, "tracked.txt"), "v2\n"); + fs.symlinkSync(realRoot, linkPath, "dir"); + + const status = await getGitStatus(linkPath); + assert.equal(status.isGitRepository, true); + assert.deepEqual(status.files.map((f) => path.relative(realRoot, f.filePath)), ["tracked.txt"]); +}); + +test("git status keeps a tracked symlink inside the selection even when its target points outside it", async (t) => { + const { getGitStatus } = await import("./git-changes.ts"); + const { execFileSync } = await import("node:child_process"); + const fs = await import("node:fs"); + const os = await import("node:os"); + const path = await import("node:path"); + // getGitStatus inherits process.env; isolate global/system attributes. + const saved = { GIT_CONFIG_GLOBAL: process.env.GIT_CONFIG_GLOBAL, GIT_CONFIG_NOSYSTEM: process.env.GIT_CONFIG_NOSYSTEM }; + process.env.GIT_CONFIG_GLOBAL = "/dev/null"; + process.env.GIT_CONFIG_NOSYSTEM = "1"; + const realRoot = fs.realpathSync.native(fs.mkdtempSync(path.join(os.tmpdir(), "git-symlink-entry-"))); + const subDir = path.join(realRoot, "sub"); + t.after(() => { + for (const [key, value] of Object.entries(saved)) { + if (value === undefined) delete process.env[key]; + else process.env[key] = value; + } + fs.rmSync(realRoot, { recursive: true, force: true }); + }); + const git = (...args) => execFileSync("git", ["-C", realRoot, "-c", "user.name=t", "-c", "user.email=t@t", ...args]); + git("init", "-q"); + fs.writeFileSync(path.join(realRoot, "outside.txt"), "outside\n"); + fs.writeFileSync(path.join(realRoot, "other.txt"), "other\n"); + fs.mkdirSync(subDir); + fs.symlinkSync("../outside.txt", path.join(subDir, "link.txt")); + git("add", "-A"); + git("commit", "-qm", "init"); + // Re-point the symlink so git status reports it modified; its location + // stays inside sub/ while its target now points outside it. + fs.unlinkSync(path.join(subDir, "link.txt")); + fs.symlinkSync("../other.txt", path.join(subDir, "link.txt")); + + const status = await getGitStatus(subDir); + assert.equal(status.isGitRepository, true); + assert.deepEqual(status.files.map((f) => path.relative(realRoot, f.filePath)), ["sub/link.txt"]); +}); + +test("git diff works when the project path is a symlink (#322 follow-up)", async (t) => { + const { getGitFileDiff, getGitStatus } = await import("./git-changes.ts"); + const { execFileSync } = await import("node:child_process"); + const fs = await import("node:fs"); + const os = await import("node:os"); + const path = await import("node:path"); + // getGitStatus/getGitFileDiff inherit process.env; isolate global/system attributes. + const saved = { GIT_CONFIG_GLOBAL: process.env.GIT_CONFIG_GLOBAL, GIT_CONFIG_NOSYSTEM: process.env.GIT_CONFIG_NOSYSTEM }; + process.env.GIT_CONFIG_GLOBAL = "/dev/null"; + process.env.GIT_CONFIG_NOSYSTEM = "1"; + const realRoot = fs.realpathSync.native(fs.mkdtempSync(path.join(os.tmpdir(), "git-symlink-diff-"))); + const linkParent = fs.realpathSync.native(fs.mkdtempSync(path.join(os.tmpdir(), "git-symlink-diff-link-"))); + const linkPath = path.join(linkParent, "example"); + t.after(() => { + for (const [key, value] of Object.entries(saved)) { + if (value === undefined) delete process.env[key]; + else process.env[key] = value; + } + fs.rmSync(realRoot, { recursive: true, force: true }); + fs.rmSync(linkParent, { recursive: true, force: true }); + }); + const git = (...args) => execFileSync("git", ["-C", realRoot, "-c", "user.name=t", "-c", "user.email=t@t", ...args]); + git("init", "-q"); + fs.writeFileSync(path.join(realRoot, "tracked.txt"), "v1\n"); + git("add", "-A"); + git("commit", "-qm", "init"); + fs.writeFileSync(path.join(realRoot, "tracked.txt"), "v2\n"); + fs.symlinkSync(realRoot, linkPath, "dir"); + + const status = await getGitStatus(linkPath); + assert.equal(status.isGitRepository, true); + // The response must be self-consistent: repositoryRoot in the same + // spelling as the canonical filePath, or callers doing + // path.relative(repositoryRoot, file.filePath) get garbage. + assert.equal(status.repositoryRoot, realRoot); + assert.deepEqual(status.files.map((f) => path.relative(realRoot, f.filePath)), ["tracked.txt"]); + + // The diff view is fed the canonical filePath from the status above. + const diff = await getGitFileDiff(linkPath, status.files[0].filePath); + assert.equal(diff.supported, true); + assert.match(diff.patch, /^-v1$/m); + assert.match(diff.patch, /^\+v2$/m); + + // …and the same file addressed through the symlinked spelling. + const lexical = await getGitFileDiff(linkPath, path.join(linkPath, "tracked.txt")); + assert.equal(lexical.supported, true); + assert.match(lexical.patch, /^\+v2$/m); +}); diff --git a/lib/git-changes.ts b/lib/git-changes.ts index 54a24f78d..4aacbbea1 100644 --- a/lib/git-changes.ts +++ b/lib/git-changes.ts @@ -47,8 +47,33 @@ async function findRepositoryRoot(cwd: string): Promise { } } +function canonicalizePath(p: string): string { + // Resolve symlinks so a project opened through a symlinked path compares + // equal to the canonical repository root git reports (#322). Missing paths + // (e.g. deleted files in a status listing) keep the lexical resolution. + // .native returns the on-disk casing on case-insensitive filesystems + // (macOS APFS), matching sameOnDiskEntry in lib/omp/agents-service.ts; + // the plain realpathSync preserves input casing and would let two + // spellings of the same directory compare unequal. + try { + return fs.realpathSync.native(p); + } catch { + return path.resolve(p); + } +} + function isWithinPath(parent: string, target: string): boolean { - const relative = path.relative(path.resolve(parent), path.resolve(target)); + const relative = path.relative(canonicalizePath(parent), canonicalizePath(target)); + return relative === "" || (!relative.startsWith(`..${path.sep}`) && relative !== ".." && !path.isAbsolute(relative)); +} + +// Lexical containment against an already-canonical parent: the target is a +// git status entry, whose location is repo-relative, so following the entry +// itself would drop a tracked symlink that points outside the selected +// directory. The canonical parent is what fixes the symlinked-cwd case +// (#322); the entry's own target is never resolved here. +function isWithinCanonicalParent(canonicalParent: string, target: string): boolean { + const relative = path.relative(canonicalParent, path.resolve(target)); return relative === "" || (!relative.startsWith(`..${path.sep}`) && relative !== ".." && !path.isAbsolute(relative)); } @@ -91,18 +116,34 @@ export async function getGitStatus(cwd: string): Promise { return { isGitRepository: false, repositoryRoot: null, files: [] }; } + // The cwd is canonicalized once: it is constant across the filter, and + // entries are compared by location only (isWithinCanonicalParent) so a + // tracked symlink inside the selection is kept even when its target + // points outside it. getGitFileDiff below keeps the fully canonical + // check, since git reads the file content through the link there. + // Entries are repo-relative, so resolve them against the canonical root: + // findRepositoryRoot keeps cwd's own spelling since #450, which for a + // symlinked cwd is the link spelling — resolving entries against it + // would put them lexically outside the canonical cwd and drop them + // (#322). Only the root is canonicalized, never an entry itself. + const canonicalCwd = canonicalizePath(cwd); + const canonicalRoot = canonicalizePath(repositoryRoot); const entries = (await readStatusEntries(repositoryRoot)) - .filter((entry) => isWithinPath(cwd, path.resolve(repositoryRoot, entry.path))); + .filter((entry) => isWithinCanonicalParent(canonicalCwd, path.resolve(canonicalRoot, entry.path))); const reasons = await readCollapseReasons(repositoryRoot, entries.map((entry) => entry.path)); const files = entries.map((entry): GitFileStatus => ({ - filePath: path.resolve(repositoryRoot, entry.path), + filePath: path.resolve(canonicalRoot, entry.path), ...classifyGitStatus(entry), indexStatus: entry.indexStatus, worktreeStatus: entry.worktreeStatus, collapseReason: reasons.get(entry.path), })); - return { isGitRepository: true, repositoryRoot, files }; + // The response is self-consistent: repositoryRoot is canonical, matching + // the canonical filePath entries, so callers can combine them with + // path.relative. findRepositoryRoot's lexical spelling is only used as + // git's -C cwd above, never returned here. + return { isGitRepository: true, repositoryRoot: canonicalRoot, files }; } function hasNullByte(content: Buffer): boolean { @@ -163,7 +204,15 @@ export async function getGitFileDiff(cwd: string, filePath: string): Promise TEXT_PREVIEW_MAX_BYTES) return { supported: false }; - const relativePath = toGitPath(path.relative(repositoryRoot, resolvedFilePath)); + // Derive the git-relative path from the canonical root and the + // canonicalized file directory, keeping the leaf name: a caller may pass + // the file through the symlinked cwd spelling, while a tracked symlink's + // own name must survive — only its directory is resolved, never the + // entry itself (resolving the entry would follow the link out of the + // repository and drop it, the #322 case in reverse). + const canonicalRoot = canonicalizePath(repositoryRoot); + const canonicalDir = canonicalizePath(path.dirname(resolvedFilePath)); + const relativePath = toGitPath(path.relative(canonicalRoot, path.join(canonicalDir, path.basename(resolvedFilePath)))); const entries = await readStatusEntries(repositoryRoot); const entry = entries.find((candidate) => candidate.path === relativePath); if (!entry) return { supported: false };