Skip to content

Commit 6404fc1

Browse files
t3dotggclaude
andcommitted
Match worktrees by their gitdir tail, ignore blank --home-dir
The `.git/worktrees` segment check rejected worktrees whose common dir is not named `.git`. A worktree of a bare repo points at `<repo>.git/worktrees/<name>`, and $GIT_COMMON_DIR can be anything. Git always uses the `worktrees/<name>` tail, so match on that instead; `<git-dir>/modules/<name>` still fails it. A blank `--home-dir` also counted as a selection under `??`, skipping the worktree default and landing on the shared home — the outcome this precedence exists to prevent. Trim before choosing. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent e52d104 commit 6404fc1

4 files changed

Lines changed: 66 additions & 10 deletions

File tree

‎packages/shared/src/devHome.test.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,27 @@ import * as Effect from "effect/Effect";
88

99
import { resolveGitWorktreePath, resolveWorktreeT3Home } from "./devHome.ts";
1010

11-
const makeRepo = (kind: "worktree" | "checkout" | "bare" | "submodule" | "unreadable-git-file") =>
11+
const makeRepo = (
12+
kind:
13+
| "worktree"
14+
| "checkout"
15+
| "bare"
16+
| "submodule"
17+
| "unreadable-git-file"
18+
| "bare-repo-worktree"
19+
| "custom-common-dir-worktree",
20+
) =>
1221
Effect.acquireRelease(
1322
Effect.sync(() => {
1423
const root = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-devhome-"));
1524
if (kind === "worktree") {
1625
NodeFS.writeFileSync(NodePath.join(root, ".git"), "gitdir: /elsewhere/.git/worktrees/x\n");
26+
} else if (kind === "bare-repo-worktree") {
27+
// `git worktree add` from a bare repo: the common dir is `<name>.git`.
28+
NodeFS.writeFileSync(NodePath.join(root, ".git"), "gitdir: /srv/myrepo.git/worktrees/x\n");
29+
} else if (kind === "custom-common-dir-worktree") {
30+
// $GIT_COMMON_DIR need not be named `.git` at all.
31+
NodeFS.writeFileSync(NodePath.join(root, ".git"), "gitdir: /srv/store/worktrees/x\n");
1732
} else if (kind === "submodule") {
1833
NodeFS.writeFileSync(NodePath.join(root, ".git"), "gitdir: ../.git/modules/sub\n");
1934
} else if (kind === "unreadable-git-file") {
@@ -63,6 +78,20 @@ describe("resolveGitWorktreePath", () => {
6378
assert.equal(yield* resolveGitWorktreePath(nested), undefined);
6479
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
6580
);
81+
82+
it.effect("finds a worktree of a bare repository", () =>
83+
Effect.gen(function* () {
84+
const { root, nested } = yield* makeRepo("bare-repo-worktree");
85+
assert.equal(yield* resolveGitWorktreePath(nested), NodePath.resolve(root));
86+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
87+
);
88+
89+
it.effect("finds a worktree whose common dir is not named .git", () =>
90+
Effect.gen(function* () {
91+
const { root, nested } = yield* makeRepo("custom-common-dir-worktree");
92+
assert.equal(yield* resolveGitWorktreePath(nested), NodePath.resolve(root));
93+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
94+
);
6695
});
6796

6897
describe("resolveWorktreeT3Home", () => {

‎packages/shared/src/devHome.ts‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@ import * as Option from "effect/Option";
1414
import * as Path from "effect/Path";
1515

1616
/**
17-
* A `.git` file points at the real git directory. A linked worktree's lives
18-
* under `<repo>/.git/worktrees/<name>`; a submodule's under
19-
* `<super>/.git/modules/<name>`. Both are files, so the pointer — not the
17+
* A `.git` file points at the real git directory. A linked worktree's lives at
18+
* `<common-dir>/worktrees/<name>`; a submodule's at
19+
* `<super-git-dir>/modules/<name>`. Both are files, so the pointer — not the
2020
* file-vs-directory distinction alone — is what identifies a worktree.
21+
*
22+
* The common dir is not necessarily named `.git`: a worktree of a bare repo
23+
* points at `<repo>.git/worktrees/<name>`, and `$GIT_COMMON_DIR` can be
24+
* anything. So match on the `worktrees/<name>` tail, which git always uses,
25+
* rather than on the name of the directory containing it.
2126
*/
2227
const pointsAtLinkedWorktree = (gitFileContents: string, path: Path.Path): boolean => {
2328
const gitdir = gitFileContents
@@ -29,11 +34,15 @@ const pointsAtLinkedWorktree = (gitFileContents: string, path: Path.Path): boole
2934
if (gitdir === undefined || gitdir.length === 0) {
3035
return false;
3136
}
32-
// Normalize so `.git/worktrees` is matched as path segments rather than as a
33-
// substring of some directory that merely happens to be named that way.
34-
const segments = path.normalize(gitdir.replaceAll("\\", "/")).split(/[/\\]/);
35-
const gitIndex = segments.lastIndexOf(".git");
36-
return gitIndex !== -1 && segments[gitIndex + 1] === "worktrees";
37+
// Compare as path segments so a directory merely named `…worktrees…` cannot
38+
// match as a substring. Trailing separators normalize away first.
39+
const segments = path
40+
.normalize(gitdir.replaceAll("\\", "/"))
41+
.split(/[/\\]/)
42+
.filter((segment) => segment.length > 0);
43+
// `<common-dir>/worktrees/<name>`: `worktrees` is the penultimate segment,
44+
// and something must precede it. This excludes `<git-dir>/modules/<name>`.
45+
return segments.length >= 3 && segments.at(-2) === "worktrees";
3746
};
3847

3948
/**

‎scripts/dev-runner.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,19 @@ it.layer(NodeServices.layer)("dev-runner", (it) => {
696696
}).pipe(Effect.scoped),
697697
);
698698

699+
it.effect("treats a blank --home-dir as unset rather than as a selection", () =>
700+
Effect.gen(function* () {
701+
const path = yield* Path.Path;
702+
const root = yield* makeWorktree;
703+
const home = yield* spawnedHome({
704+
t3Home: " ",
705+
cwd: root,
706+
ambientHome: "/home/user/.t3",
707+
});
708+
assert.equal(home, path.join(path.resolve(root), ".t3"));
709+
}).pipe(Effect.scoped),
710+
);
711+
699712
it.effect("prefers the worktree .t3 over an ambient T3CODE_HOME", () =>
700713
Effect.gen(function* () {
701714
const path = yield* Path.Path;

‎scripts/dev-runner.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,13 @@ export function runDevRunnerWithInput(input: DevRunnerCliInput) {
512512
// outranks ambient T3CODE_HOME, which otherwise selects the installed
513513
// app's live userdata database. An explicit --home-dir still wins.
514514
const worktreeHome = yield* resolveWorktreeT3Home(yield* HostProcessWorkingDirectory);
515+
// Trim before choosing: `--home-dir ""` is not a selection, and treating it
516+
// as one would skip the worktree default and land on the shared home —
517+
// exactly the outcome this precedence exists to prevent.
515518
const resolvedT3Home =
516-
input.t3Home ?? worktreeHome ?? (hostEnvironment.T3CODE_HOME?.trim() || undefined);
519+
(input.t3Home?.trim() || undefined) ??
520+
worktreeHome ??
521+
(hostEnvironment.T3CODE_HOME?.trim() || undefined);
517522
const env = yield* createDevRunnerEnv({
518523
mode: input.mode,
519524
baseEnv: hostEnvironment,

0 commit comments

Comments
 (0)