Skip to content

Commit c045a96

Browse files
committed
fix: file changes visibility in worktree subdirs (anomalyco#4752)
- Use Instance.worktree as cwd for all git operations in Snapshot - Calculate relative pathspec using fs.realpath to handle symlinks - Apply fix to track(), patch(), diff(), and diffFull() - Ensures git operations are scoped to opened directory - Fixes file changes not showing in sidebar for worktree subdirs
1 parent 6134d75 commit c045a96

2 files changed

Lines changed: 26 additions & 18 deletions

File tree

‎packages/opencode/src/session/summary.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export namespace SessionSummary {
149149
},
150150
],
151151
headers: small.info.headers,
152-
}).catch(() => {})
152+
}).catch(() => { })
153153
if (result) summary = result.text
154154
}
155155
userMsg.summary.body = summary

‎packages/opencode/src/snapshot/index.ts‎

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ export namespace Snapshot {
2828
await $`git --git-dir ${git} config core.autocrlf false`.quiet().nothrow()
2929
log.info("initialized")
3030
}
31-
await $`git --git-dir ${git} --work-tree ${Instance.worktree} add .`.quiet().cwd(Instance.directory).nothrow()
31+
const realDir = await fs.realpath(Instance.directory)
32+
const relative = path.relative(Instance.worktree, realDir) || "."
33+
await $`git --git-dir ${git} --work-tree ${Instance.worktree} add ${relative}`.quiet().cwd(Instance.worktree).nothrow()
3234
const hash = await $`git --git-dir ${git} --work-tree ${Instance.worktree} write-tree`
3335
.quiet()
34-
.cwd(Instance.directory)
36+
.cwd(Instance.worktree)
3537
.nothrow()
3638
.text()
37-
log.info("tracking", { hash, cwd: Instance.directory, git })
39+
log.info("tracking", { hash, cwd: Instance.directory, git, realDir, relative, worktree: Instance.worktree })
3840
return hash.trim()
3941
}
4042

@@ -46,11 +48,13 @@ export namespace Snapshot {
4648

4749
export async function patch(hash: string): Promise<Patch> {
4850
const git = gitdir()
49-
await $`git --git-dir ${git} --work-tree ${Instance.worktree} add .`.quiet().cwd(Instance.directory).nothrow()
51+
const realDir = await fs.realpath(Instance.directory)
52+
const relative = path.relative(Instance.worktree, realDir) || "."
53+
await $`git --git-dir ${git} --work-tree ${Instance.worktree} add ${relative}`.quiet().cwd(Instance.worktree).nothrow()
5054
const result =
51-
await $`git -c core.autocrlf=false --git-dir ${git} --work-tree ${Instance.worktree} diff --no-ext-diff --name-only ${hash} -- .`
55+
await $`git -c core.autocrlf=false --git-dir ${git} --work-tree ${Instance.worktree} diff --no-ext-diff --name-only ${hash} -- ${relative}`
5256
.quiet()
53-
.cwd(Instance.directory)
57+
.cwd(Instance.worktree)
5458
.nothrow()
5559

5660
// If git diff fails, return empty patch
@@ -114,7 +118,7 @@ export namespace Snapshot {
114118
})
115119
} else {
116120
log.info("file did not exist in snapshot, deleting", { file })
117-
await fs.unlink(file).catch(() => {})
121+
await fs.unlink(file).catch(() => { })
118122
}
119123
}
120124
files.add(file)
@@ -124,9 +128,11 @@ export namespace Snapshot {
124128

125129
export async function diff(hash: string) {
126130
const git = gitdir()
127-
await $`git --git-dir ${git} --work-tree ${Instance.worktree} add .`.quiet().cwd(Instance.directory).nothrow()
131+
const realDir = await fs.realpath(Instance.directory)
132+
const relative = path.relative(Instance.worktree, realDir) || "."
133+
await $`git --git-dir ${git} --work-tree ${Instance.worktree} add ${relative}`.quiet().cwd(Instance.worktree).nothrow()
128134
const result =
129-
await $`git -c core.autocrlf=false --git-dir ${git} --work-tree ${Instance.worktree} diff --no-ext-diff ${hash} -- .`
135+
await $`git -c core.autocrlf=false --git-dir ${git} --work-tree ${Instance.worktree} diff --no-ext-diff ${hash} -- ${relative}`
130136
.quiet()
131137
.cwd(Instance.worktree)
132138
.nothrow()
@@ -159,9 +165,11 @@ export namespace Snapshot {
159165
export async function diffFull(from: string, to: string): Promise<FileDiff[]> {
160166
const git = gitdir()
161167
const result: FileDiff[] = []
162-
for await (const line of $`git -c core.autocrlf=false --git-dir ${git} --work-tree ${Instance.worktree} diff --no-ext-diff --no-renames --numstat ${from} ${to} -- .`
168+
const realDir = await fs.realpath(Instance.directory)
169+
const relative = path.relative(Instance.worktree, realDir) || "."
170+
for await (const line of $`git -c core.autocrlf=false --git-dir ${git} --work-tree ${Instance.worktree} diff --no-ext-diff --no-renames --numstat ${from} ${to} -- ${relative}`
163171
.quiet()
164-
.cwd(Instance.directory)
172+
.cwd(Instance.worktree)
165173
.nothrow()
166174
.lines()) {
167175
if (!line) continue
@@ -170,15 +178,15 @@ export namespace Snapshot {
170178
const before = isBinaryFile
171179
? ""
172180
: await $`git -c core.autocrlf=false --git-dir ${git} --work-tree ${Instance.worktree} show ${from}:${file}`
173-
.quiet()
174-
.nothrow()
175-
.text()
181+
.quiet()
182+
.nothrow()
183+
.text()
176184
const after = isBinaryFile
177185
? ""
178186
: await $`git -c core.autocrlf=false --git-dir ${git} --work-tree ${Instance.worktree} show ${to}:${file}`
179-
.quiet()
180-
.nothrow()
181-
.text()
187+
.quiet()
188+
.nothrow()
189+
.text()
182190
result.push({
183191
file,
184192
before,

0 commit comments

Comments
 (0)