Skip to content

Commit 4a608b2

Browse files
committed
snapshot: fix cwd for git commands in subdirs
When opencode is launched from a git repository subdirectory, the snapshot service passes file paths relative to the worktree root but sets cwd to the launch directory. Git cannot resolve the paths and fails with "did not match any files". Use the worktree root as cwd so paths resolve correctly regardless of which subdirectory opencode is started from. Fix #27688 Close #27737
1 parent 418c0ea commit 4a608b2

2 files changed

Lines changed: 155 additions & 32 deletions

File tree

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

Lines changed: 62 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | AppProce
8585
const args = (cmd: string[]) => ["--git-dir", state.gitdir, "--work-tree", state.worktree, ...cmd]
8686

8787
const feed = (list: string[]) => list.join("\0") + "\0"
88+
const feedSpec = (list: string[]) => feed(list.map((item) => `:(top,literal)${item}`))
89+
90+
const scope = path.relative(state.worktree, state.directory).replaceAll("\\", "/")
91+
const spec = scope ? `:(top,literal)${scope}` : "."
8892

8993
const git = Effect.fnUntraced(
9094
function* (cmd: string[], opts?: { cwd?: string; env?: Record<string, string>; stdin?: string }) {
@@ -122,7 +126,7 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | AppProce
122126
"-z",
123127
],
124128
{
125-
cwd: state.directory,
129+
cwd: state.worktree,
126130
stdin: feed(files),
127131
},
128132
)
@@ -138,8 +142,8 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | AppProce
138142
...args(["rm", "--cached", "-f", "--ignore-unmatch", "--pathspec-from-file=-", "--pathspec-file-nul"]),
139143
],
140144
{
141-
cwd: state.directory,
142-
stdin: feed(files),
145+
cwd: state.worktree,
146+
stdin: feedSpec(files),
143147
},
144148
)
145149
})
@@ -149,8 +153,8 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | AppProce
149153
const result = yield* git(
150154
[...cfg, ...args(["add", "--all", "--sparse", "--pathspec-from-file=-", "--pathspec-file-nul"])],
151155
{
152-
cwd: state.directory,
153-
stdin: feed(files),
156+
cwd: state.worktree,
157+
stdin: feedSpec(files),
154158
},
155159
)
156160
if (result.code === 0) return
@@ -197,11 +201,11 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | AppProce
197201
yield* sync()
198202
const [diff, other] = yield* Effect.all(
199203
[
200-
git([...quote, ...args(["diff-files", "--name-only", "-z", "--", "."])], {
201-
cwd: state.directory,
204+
git([...quote, ...args(["diff-files", "--name-only", "-z", "--", spec])], {
205+
cwd: state.worktree,
202206
}),
203-
git([...quote, ...args(["ls-files", "--others", "--exclude-standard", "-z", "--", "."])], {
204-
cwd: state.directory,
207+
git([...quote, ...args(["ls-files", "--others", "--exclude-standard", "-z", "--", spec])], {
208+
cwd: state.worktree,
205209
}),
206210
],
207211
{ concurrency: 2 },
@@ -239,7 +243,7 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | AppProce
239243
(yield* Effect.all(
240244
allow.map((item) =>
241245
fs
242-
.stat(path.join(state.directory, item))
246+
.stat(path.join(state.worktree, item))
243247
.pipe(Effect.catch(() => Effect.void))
244248
.pipe(
245249
Effect.map((stat) => {
@@ -306,9 +310,9 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | AppProce
306310
Effect.gen(function* () {
307311
yield* add()
308312
const result = yield* git(
309-
[...quote, ...args(["diff", "--cached", "--no-ext-diff", "--name-only", hash, "--", "."])],
313+
[...quote, ...args(["diff", "--cached", "--no-ext-diff", "--name-only", hash, "--", spec])],
310314
{
311-
cwd: state.directory,
315+
cwd: state.worktree,
312316
},
313317
)
314318
if (result.code !== 0) {
@@ -338,24 +342,47 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | AppProce
338342
return yield* locked(
339343
Effect.gen(function* () {
340344
log.info("restore", { commit: snapshot })
341-
const result = yield* git([...core, ...args(["read-tree", snapshot])], { cwd: state.worktree })
342-
if (result.code === 0) {
343-
const checkout = yield* git([...core, ...args(["checkout-index", "-a", "-f"])], {
345+
const listed = yield* git([...quote, ...args(["ls-tree", "-r", "-z", "--name-only", snapshot, "--", spec])], {
346+
cwd: state.worktree,
347+
})
348+
if (listed.code !== 0) {
349+
log.error("failed to list snapshot files", {
350+
snapshot,
351+
exitCode: listed.code,
352+
stderr: listed.stderr,
353+
})
354+
return
355+
}
356+
const files = listed.text.split("\0").filter(Boolean)
357+
if (!files.length) return
358+
359+
const index = path.join(state.gitdir, "restore.index")
360+
yield* remove(index)
361+
yield* Effect.gen(function* () {
362+
const result = yield* git([...core, ...args(["read-tree", `--index-output=${index}`, snapshot])], {
344363
cwd: state.worktree,
345364
})
346-
if (checkout.code === 0) return
365+
if (result.code === 0) {
366+
const checkout = yield* git([...core, ...args(["checkout-index", "-f", "--stdin", "-z"])], {
367+
cwd: state.worktree,
368+
env: { GIT_INDEX_FILE: index },
369+
stdin: feed(files),
370+
})
371+
if (checkout.code === 0) return
372+
log.error("failed to restore snapshot", {
373+
snapshot,
374+
exitCode: checkout.code,
375+
stderr: checkout.stderr,
376+
})
377+
return
378+
}
379+
347380
log.error("failed to restore snapshot", {
348381
snapshot,
349-
exitCode: checkout.code,
350-
stderr: checkout.stderr,
382+
exitCode: result.code,
383+
stderr: result.stderr,
351384
})
352-
return
353-
}
354-
log.error("failed to restore snapshot", {
355-
snapshot,
356-
exitCode: result.code,
357-
stderr: result.stderr,
358-
})
385+
}).pipe(Effect.ensuring(remove(index)))
359386
}),
360387
)
361388
})
@@ -479,9 +506,12 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | AppProce
479506
return yield* locked(
480507
Effect.gen(function* () {
481508
yield* add()
482-
const result = yield* git([...quote, ...args(["diff", "--cached", "--no-ext-diff", hash, "--", "."])], {
483-
cwd: state.worktree,
484-
})
509+
const result = yield* git(
510+
[...quote, ...args(["diff", "--cached", "--no-ext-diff", hash, "--", spec])],
511+
{
512+
cwd: state.worktree,
513+
},
514+
)
485515
if (result.code !== 0) {
486516
log.warn("failed to get diff", {
487517
hash,
@@ -637,8 +667,8 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | AppProce
637667
const status = new Map<string, "added" | "deleted" | "modified">()
638668

639669
const statuses = yield* git(
640-
[...quote, ...args(["diff", "--no-ext-diff", "--name-status", "--no-renames", from, to, "--", "."])],
641-
{ cwd: state.directory },
670+
[...quote, ...args(["diff", "--no-ext-diff", "--name-status", "--no-renames", from, to, "--", spec])],
671+
{ cwd: state.worktree },
642672
)
643673

644674
for (const line of statuses.text.trim().split("\n")) {
@@ -649,9 +679,9 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | AppProce
649679
}
650680

651681
const numstat = yield* git(
652-
[...quote, ...args(["diff", "--no-ext-diff", "--no-renames", "--numstat", from, to, "--", "."])],
682+
[...quote, ...args(["diff", "--no-ext-diff", "--no-renames", "--numstat", from, to, "--", spec])],
653683
{
654-
cwd: state.directory,
684+
cwd: state.worktree,
655685
},
656686
)
657687

‎packages/opencode/test/snapshot/snapshot.test.ts‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,99 @@ it.instance(
476476
{ git: true },
477477
)
478478

479+
it.live(
480+
"subdirectory instances stage snapshot files relative to the worktree root",
481+
Effect.gen(function* () {
482+
const dir = yield* scopedGitTmpdir()
483+
const subdir = `${dir}/src`
484+
yield* mkdirp(subdir)
485+
yield* write(`${subdir}/tracked.txt`, "tracked content")
486+
yield* exec(dir, ["git", "add", "."])
487+
yield* exec(dir, ["git", "commit", "-m", "add subdir"])
488+
yield* Effect.gen(function* () {
489+
const snapshot = yield* Snapshot.Service
490+
const before = yield* snapshot.track()
491+
expect(before).toBeTruthy()
492+
yield* write(`${subdir}/date.txt`, "subdirectory content")
493+
const patch = yield* snapshot.patch(before!)
494+
expect(patch.files).toContain(fwd(subdir, "date.txt"))
495+
}).pipe(provideInstance(subdir))
496+
}),
497+
)
498+
499+
it.live(
500+
"subdirectory instances keep gitignored snapshot files out of patches",
501+
Effect.gen(function* () {
502+
const dir = yield* scopedGitTmpdir()
503+
const subdir = `${dir}/src`
504+
yield* mkdirp(subdir)
505+
yield* Effect.gen(function* () {
506+
const snapshot = yield* Snapshot.Service
507+
yield* write(`${subdir}/later-ignored.txt`, "initial content")
508+
const before = yield* snapshot.track()
509+
expect(before).toBeTruthy()
510+
yield* write(`${subdir}/later-ignored.txt`, "modified content")
511+
yield* write(`${subdir}/.gitignore`, "later-ignored.txt\n")
512+
yield* write(`${subdir}/still-tracked.txt`, "new tracked file")
513+
const patch = yield* snapshot.patch(before!)
514+
expect(patch.files).not.toContain(fwd(subdir, "later-ignored.txt"))
515+
expect(patch.files).toContain(fwd(subdir, ".gitignore"))
516+
expect(patch.files).toContain(fwd(subdir, "still-tracked.txt"))
517+
}).pipe(provideInstance(subdir))
518+
}),
519+
)
520+
521+
it.live(
522+
"subdirectory restore does not overwrite files outside the subdirectory",
523+
Effect.gen(function* () {
524+
const dir = yield* scopedGitTmpdir()
525+
const subdir = `${dir}/src`
526+
yield* write(`${dir}/root.txt`, "original root")
527+
yield* write(`${subdir}/file.txt`, "original src")
528+
yield* exec(dir, ["git", "add", "."])
529+
yield* exec(dir, ["git", "commit", "-m", "init"])
530+
yield* Effect.gen(function* () {
531+
const snapshot = yield* Snapshot.Service
532+
yield* write(`${dir}/root.txt`, "root snapshot")
533+
expect(yield* snapshot.track()).toBeTruthy()
534+
}).pipe(provideInstance(dir))
535+
yield* Effect.gen(function* () {
536+
const snapshot = yield* Snapshot.Service
537+
yield* write(`${subdir}/file.txt`, "src snapshot")
538+
const before = yield* snapshot.track()
539+
expect(before).toBeTruthy()
540+
yield* write(`${dir}/root.txt`, "root current")
541+
yield* write(`${subdir}/file.txt`, "src current")
542+
yield* snapshot.restore(before!)
543+
expect(yield* readText(`${dir}/root.txt`)).toBe("root current")
544+
expect(yield* readText(`${subdir}/file.txt`)).toBe("src snapshot")
545+
}).pipe(provideInstance(subdir))
546+
}),
547+
)
548+
549+
it.live(
550+
"subdirectory scope is treated as a literal git pathspec",
551+
Effect.gen(function* () {
552+
const dir = yield* scopedGitTmpdir()
553+
const subdir = `${dir}/src*`
554+
const sibling = `${dir}/srca`
555+
yield* write(`${subdir}/file.txt`, "literal original")
556+
yield* write(`${sibling}/file.txt`, "sibling original")
557+
yield* exec(dir, ["git", "add", "."])
558+
yield* exec(dir, ["git", "commit", "-m", "init"])
559+
yield* Effect.gen(function* () {
560+
const snapshot = yield* Snapshot.Service
561+
const before = yield* snapshot.track()
562+
expect(before).toBeTruthy()
563+
yield* write(`${subdir}/file.txt`, "literal modified")
564+
yield* write(`${sibling}/file.txt`, "sibling modified")
565+
const patch = yield* snapshot.patch(before!)
566+
expect(patch.files).toContain(fwd(subdir, "file.txt"))
567+
expect(patch.files).not.toContain(fwd(sibling, "file.txt"))
568+
}).pipe(provideInstance(subdir))
569+
}),
570+
)
571+
479572
it.instance(
480573
"gitignore updated between track calls filters from diff",
481574
withTrackedSnapshot(({ tmp, snapshot, before }) =>

0 commit comments

Comments
 (0)