Skip to content

Commit acf5f3b

Browse files
fix(server): onboarding offers worktrees of a bare repository (pingdotgg#16688)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 0c39430 commit acf5f3b

4 files changed

Lines changed: 107 additions & 8 deletions

File tree

‎apps/server/src/project/AgentSessionScanner.test.ts‎

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -978,9 +978,12 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {
978978
path.join(repo, ".git", "config"),
979979
'[core]\n\tbare = false\n[remote "origin"]\n\turl = [email protected]:pingdotgg/t3code.git\n\tfetch = +refs/heads/*:refs/remotes/origin/*\n',
980980
);
981+
const worktreeGitDir = path.join(repo, ".git", "worktrees", "wt");
982+
yield* fileSystem.makeDirectory(worktreeGitDir, { recursive: true });
983+
yield* fileSystem.writeFileString(path.join(worktreeGitDir, "commondir"), "../..\n");
981984
yield* fileSystem.writeFileString(
982985
path.join(worktree, ".git"),
983-
`gitdir: ${path.join(repo, ".git", "worktrees", "wt")}\n`,
986+
`gitdir: ${worktreeGitDir}\n`,
984987
);
985988
yield* fileSystem.makeDirectory(path.join(noRemote, ".git"));
986989
yield* fileSystem.writeFileString(path.join(noRemote, ".git", "config"), "[core]\n");
@@ -1022,6 +1025,75 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {
10221025
}),
10231026
);
10241027

1028+
it.effect(
1029+
"offers bare-repository worktrees separately and imports only their own sessions",
1030+
() =>
1031+
Effect.gen(function* () {
1032+
const path = yield* Path.Path;
1033+
const fileSystem = yield* FileSystem.FileSystem;
1034+
const nowMs = Date.parse("2026-08-24T12:00:00.000Z");
1035+
yield* TestClock.setTime(nowMs);
1036+
const claudeHomePath = yield* makeTempDir("t3code-claude-home-");
1037+
const codexHomePath = yield* makeTempDir("t3code-codex-home-");
1038+
const configBaseDir = yield* makeTempDir("t3code-scanner-base-");
1039+
const bare = yield* makeTempDir("t3code-workspace-bare-");
1040+
const bareGitDir = path.join(bare, ".git");
1041+
yield* fileSystem.makeDirectory(bareGitDir);
1042+
yield* fileSystem.writeFileString(
1043+
path.join(bareGitDir, "config"),
1044+
'[core] bare = "true" ; bare repository\n[remote "origin"]\nurl = [email protected]:pingdotgg/t3code.git\n',
1045+
);
1046+
const dev = path.join(bare, "dev");
1047+
const prod = path.join(bare, "prod");
1048+
const managed = path.join(configBaseDir, "worktrees", "t3code", "sandbox");
1049+
for (const [index, cwd] of [bare, dev, prod, managed].entries()) {
1050+
if (cwd !== bare) {
1051+
const gitDir = path.join(bareGitDir, "worktrees", path.basename(cwd));
1052+
yield* fileSystem.makeDirectory(gitDir, { recursive: true });
1053+
yield* fileSystem.writeFileString(path.join(gitDir, "commondir"), "../..\n");
1054+
yield* fileSystem.makeDirectory(cwd, { recursive: true });
1055+
yield* fileSystem.writeFileString(path.join(cwd, ".git"), `gitdir: ${gitDir}\n`);
1056+
}
1057+
yield* writeTranscript({
1058+
filePath: path.join(
1059+
codexHomePath,
1060+
"sessions",
1061+
"2026",
1062+
"08",
1063+
"24",
1064+
`rollout-${index}.jsonl`,
1065+
),
1066+
contents: [
1067+
encodeTranscriptRecord({
1068+
type: "session_meta",
1069+
payload: { id: `session-${index}`, cwd },
1070+
}),
1071+
encodeTranscriptRecord({
1072+
type: "event_msg",
1073+
payload: { type: "user_message", message: `Work in ${cwd}` },
1074+
}),
1075+
].join("\n"),
1076+
mtimeMs: nowMs - index * 1_000,
1077+
});
1078+
}
1079+
1080+
const input = { claudeHomePath, codexHomePath, configBaseDir };
1081+
const result = yield* runScan(input);
1082+
const git = { remoteKey: "github.com/pingdotgg/t3code", repository: "pingdotgg/t3code" };
1083+
expect(
1084+
result.candidates.map((candidate) => ({ path: candidate.path, git: candidate.git })),
1085+
).toEqual([
1086+
{ path: bare, git },
1087+
{ path: dev, git },
1088+
{ path: prod, git },
1089+
]);
1090+
const threads = yield* runRecentThreads({ ...input, workspaceRoot: dev });
1091+
expect(threads.map((thread) => thread.providerSessionId)).toEqual(["session-1"]);
1092+
const managedThreads = yield* runRecentThreads({ ...input, workspaceRoot: managed });
1093+
expect(managedThreads).toEqual([]);
1094+
}),
1095+
);
1096+
10251097
it.effect("excludes sandboxes under the configured worktrees dir without .t3 in the path", () =>
10261098
Effect.gen(function* () {
10271099
const path = yield* Path.Path;

‎apps/server/src/project/AgentSessionScanner.ts‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import * as Stream from "effect/Stream";
2626

2727
import {
2828
normalizeGitRemoteUrl,
29+
isBareGitConfig,
2930
parseGitHubRepositoryNameWithOwnerFromRemoteUrl,
3031
parseOriginUrlFromGitConfig,
3132
} from "@t3tools/shared/git";
@@ -687,11 +688,6 @@ export const make = Effect.gen(function* () {
687688
return `path:${normalizeProjectPathForComparison(realPath)}`;
688689
});
689690

690-
/**
691-
* When it points into a `worktrees/` directory the checkout is a linked worktree, which
692-
* onboarding skips because its history belongs to the main checkout. Submodules use the same
693-
* pointer shape but live under `modules/`, and are offered like any other repository.
694-
*/
695691
const readGitIdentity = Effect.fn("AgentSessionScanner.readGitIdentity")(function* (
696692
directory: string,
697693
): Effect.fn.Return<
@@ -710,11 +706,21 @@ export const make = Effect.gen(function* () {
710706
const target = /^gitdir:\s*(.+)$/m.exec(pointer)?.[1]?.trim();
711707
if (target === undefined || target.length === 0) return { _tag: "NotGit" } as const;
712708
gitDir = path.resolve(directory, target);
713-
if (/[\\/]worktrees[\\/][^\\/]+[\\/]?$/.test(gitDir)) return { _tag: "Worktree" } as const;
709+
}
710+
const linkedWorktree =
711+
gitStats.value.type !== "Directory" && /[\\/]worktrees[\\/][^\\/]+[\\/]?$/.test(gitDir);
712+
if (linkedWorktree) {
713+
const commonDir = yield* fileSystem
714+
.readFileString(path.join(gitDir, "commondir"))
715+
.pipe(Effect.orElseSucceed(() => ""));
716+
if (commonDir.trim().length === 0) return { _tag: "Worktree" } as const;
717+
gitDir = path.resolve(gitDir, commonDir.trim());
714718
}
715719
const configText = yield* fileSystem
716720
.readFileString(path.join(gitDir, "config"))
717721
.pipe(Effect.orElseSucceed(() => ""));
722+
// Bare repositories have no main checkout, so offer their linked worktrees independently.
723+
if (linkedWorktree && !isBareGitConfig(configText)) return { _tag: "Worktree" } as const;
718724
const originUrl = parseOriginUrlFromGitConfig(configText);
719725
return {
720726
_tag: "Repository",

‎docs/user/welcome-wizard.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ one group. Directories that are not git repositories sit under "Other folders".
6666

6767
The default selection includes git repositories active within the last 30 days
6868
with at least three conversations. Use the checkboxes, or "Select all" and
69-
"Select none", to change the selection. Linked git worktrees, Codex scratch
69+
"Select none", to change the selection. Linked worktrees of bare repositories
70+
can be offered as separate projects. Other linked git worktrees, Codex scratch
7071
directories under `Documents/Codex`, and anything under `Downloads` are not
7172
offered.
7273

‎packages/shared/src/git.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,26 @@ function parseGitConfigValue(raw: string): string {
196196
return out.trim();
197197
}
198198

199+
export function isBareGitConfig(configText: string): boolean {
200+
let inCore = false;
201+
let bare = false;
202+
for (const rawLine of configText.split(/\r?\n/)) {
203+
let line = rawLine.trim();
204+
// Git accepts the first setting on the section header's own line.
205+
const header = /^\[\s*([^\]]*?)\s*\](.*)$/.exec(line);
206+
if (header !== null) {
207+
inCore = header[1]!.toLowerCase() === "core";
208+
line = header[2]!.trim();
209+
}
210+
if (!inCore) continue;
211+
const match = /^bare\s*(?:=(.*)|[#;].*)?$/i.exec(line);
212+
if (match === null) continue;
213+
const value = match[1] === undefined ? "true" : parseGitConfigValue(match[1]).toLowerCase();
214+
bare = value === "true" || value === "yes" || value === "on" || value === "1";
215+
}
216+
return bare;
217+
}
218+
199219
export function parseOriginUrlFromGitConfig(configText: string): string | null {
200220
let section: string | null = null;
201221
let originUrl: string | null = null;

0 commit comments

Comments
 (0)