From 0f11030eb1a3b048653e46bb7bdb7aca34067396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Tu=E1=BA=A5n=20Anh?= Date: Tue, 10 Nov 2026 21:48:47 +0700 Subject: [PATCH 1/4] fix(git): canonicalize paths so symlinked project paths list changes --- lib/git-changes.test.mjs | 34 ++++++++++++++++++++++++++++++++++ lib/git-changes.ts | 13 ++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/lib/git-changes.test.mjs b/lib/git-changes.test.mjs index c5c4dc024..9776f858c 100644 --- a/lib/git-changes.test.mjs +++ b/lib/git-changes.test.mjs @@ -134,3 +134,37 @@ test("git status marks attribute files and -diff files get no synthesized patch" assert.equal(note.supported, true); assert.match(note.patch, /^\+hi$/m); }); + +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"]); +}); diff --git a/lib/git-changes.ts b/lib/git-changes.ts index 757b12db5..736f88439 100644 --- a/lib/git-changes.ts +++ b/lib/git-changes.ts @@ -44,8 +44,19 @@ 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. + try { + return fs.realpathSync(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)); } From f9a3fb71c1da746574971ef58e5ff5ee9542c86f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Tu=E1=BA=A5n=20Anh?= Date: Thu, 12 Nov 2026 04:46:27 +0700 Subject: [PATCH 2/4] fix(git): use native realpath and keep tracked symlink entries in the selection --- lib/git-changes.test.mjs | 37 +++++++++++++++++++++++++++++++++++++ lib/git-changes.ts | 24 ++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/lib/git-changes.test.mjs b/lib/git-changes.test.mjs index 9776f858c..4c073de46 100644 --- a/lib/git-changes.test.mjs +++ b/lib/git-changes.test.mjs @@ -168,3 +168,40 @@ test("git status lists changes when the project path is a symlink (#322)", async 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"]); +}); diff --git a/lib/git-changes.ts b/lib/git-changes.ts index 736f88439..3664d4c2c 100644 --- a/lib/git-changes.ts +++ b/lib/git-changes.ts @@ -48,8 +48,12 @@ 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(p); + return fs.realpathSync.native(p); } catch { return path.resolve(p); } @@ -60,6 +64,16 @@ function isWithinPath(parent: string, target: string): boolean { 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)); +} + function toGitPath(filePath: string): string { return filePath.split(path.sep).join("/"); } @@ -99,8 +113,14 @@ 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. + const canonicalCwd = canonicalizePath(cwd); const entries = (await readStatusEntries(repositoryRoot)) - .filter((entry) => isWithinPath(cwd, path.resolve(repositoryRoot, entry.path))); + .filter((entry) => isWithinCanonicalParent(canonicalCwd, path.resolve(repositoryRoot, entry.path))); const reasons = await readCollapseReasons(repositoryRoot, entries.map((entry) => entry.path)); const files = entries.map((entry): GitFileStatus => ({ filePath: path.resolve(repositoryRoot, entry.path), From 3fadacd438d052b21bb69b04f3a81e4c539e4ff8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Tu=E1=BA=A5n=20Anh?= Date: Fri, 13 Nov 2026 00:05:26 +0700 Subject: [PATCH 3/4] fix(git): resolve merge conflict with main and keep symlinked-cwd listing working --- lib/git-changes.test.mjs | 33 +++++++++++++++++++++++++++++++++ lib/git-changes.ts | 19 ++++++++++++++----- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/lib/git-changes.test.mjs b/lib/git-changes.test.mjs index 4c073de46..5df4284d1 100644 --- a/lib/git-changes.test.mjs +++ b/lib/git-changes.test.mjs @@ -135,6 +135,39 @@ test("git status marks attribute files and -diff files get no synthesized patch" assert.match(note.patch, /^\+hi$/m); }); +test("git status reports a bare repository as no worktree and ignores an inherited GIT_DIR", 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"); + const saved = { GIT_DIR: process.env.GIT_DIR, GIT_CONFIG_GLOBAL: process.env.GIT_CONFIG_GLOBAL, GIT_CONFIG_NOSYSTEM: process.env.GIT_CONFIG_NOSYSTEM }; + const root = fs.realpathSync.native(fs.mkdtempSync(path.join(os.tmpdir(), "git-ambient-dir-"))); + t.after(() => { + for (const [key, value] of Object.entries(saved)) { + if (value === undefined) delete process.env[key]; + else process.env[key] = value; + } + fs.rmSync(root, { recursive: true, force: true }); + }); + process.env.GIT_CONFIG_GLOBAL = "/dev/null"; + process.env.GIT_CONFIG_NOSYSTEM = "1"; + const bare = path.join(root, "bare.git"); + const repo = path.join(root, "repo"); + execFileSync("git", ["init", "-q", "--bare", bare]); + execFileSync("git", ["init", "-q", repo]); + fs.writeFileSync(path.join(repo, "note.txt"), "hi\n"); + + const inBare = await getGitStatus(bare); + assert.equal(inBare.isGitRepository, false, "a bare repository has no worktree to report"); + + // A daemon started from a Git hook can inherit GIT_DIR; the status must still be the cwd's own repository. + process.env.GIT_DIR = bare; + const status = await getGitStatus(repo); + 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"); diff --git a/lib/git-changes.ts b/lib/git-changes.ts index 3664d4c2c..81e8b3e27 100644 --- a/lib/git-changes.ts +++ b/lib/git-changes.ts @@ -3,7 +3,7 @@ import fs from "fs"; import path from "path"; import { promisify } from "util"; import { TEXT_PREVIEW_MAX_BYTES } from "./file-types"; -import { createChildProcessEnvironment } from "./project-command-env"; +import { createGitEnvironment } from "./project-command-env"; import type { GitCollapseReason, GitFileDiffResponse, @@ -26,7 +26,7 @@ async function git(cwd: string, args: string[], maxBuffer = GIT_STATUS_MAX_BUFFE const pending = execFileAsync("git", ["-C", cwd, ...args], { timeout: GIT_TIMEOUT_MS, maxBuffer, - env: createChildProcessEnvironment({ LC_ALL: "C" }), + env: createGitEnvironment({ LC_ALL: "C" }), }); if (input !== undefined) { // git may exit before reading stdin (EPIPE); that failure surfaces through `pending`. @@ -36,9 +36,12 @@ async function git(cwd: string, args: string[], maxBuffer = GIT_STATUS_MAX_BUFFE return (await pending).stdout; } +/** The worktree toplevel in `cwd`'s own spelling: Git's --show-toplevel can differ in separators, 8.3 names and symlinks. + * Null outside a worktree; a bare repository has a Git directory but no toplevel. */ async function findRepositoryRoot(cwd: string): Promise { try { - return (await git(cwd, ["rev-parse", "--show-toplevel"])).trim() || null; + const [insideWorkTree, cdup = ""] = (await git(cwd, ["rev-parse", "--is-inside-work-tree", "--show-cdup"])).split("\n"); + return insideWorkTree === "true" ? path.resolve(cwd, cdup.trim()) : null; } catch { return null; } @@ -118,12 +121,18 @@ export async function getGitStatus(cwd: string): Promise { // 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) => isWithinCanonicalParent(canonicalCwd, 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, From c0c5bd218cbb326a3a0fb5aef69522f6c3f081ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Tu=E1=BA=A5n=20Anh?= Date: Sat, 14 Nov 2026 08:09:39 +0700 Subject: [PATCH 4/4] fix(git): return canonical repository root so diff works for symlinked projects --- lib/git-changes.test.mjs | 49 ++++++++++++++++++++++++++++++++++++++++ lib/git-changes.ts | 16 +++++++++++-- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/lib/git-changes.test.mjs b/lib/git-changes.test.mjs index 5df4284d1..9359cdde5 100644 --- a/lib/git-changes.test.mjs +++ b/lib/git-changes.test.mjs @@ -238,3 +238,52 @@ test("git status keeps a tracked symlink inside the selection even when its targ 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 81e8b3e27..4aacbbea1 100644 --- a/lib/git-changes.ts +++ b/lib/git-changes.ts @@ -139,7 +139,11 @@ export async function getGitStatus(cwd: string): Promise { 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 { @@ -200,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 };