The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 17:15 UTC
fix(server): repository identity honors the gh default remote (#21574)
Julius Marminge committed
Nov 12, 2026 at 23:41 UTC
56e58a9866528e11119da38e450676947fae6b40
2 files changed
+94
-39
apps/server/src/project/RepositoryIdentityResolver.test.ts
+40
@@ -344,6 +344,46 @@ it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => {
344
}).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
345
);
346
347
+ it.effect("follows an OWNER/REPO mark only to a remote that fetches that repository", () =>
348
+ Effect.gen(function* () {
349
+ const fileSystem = yield* FileSystem.FileSystem;
350
+ const cwd = yield* fileSystem.makeTempDirectoryScoped({
351
+ prefix: "t3-repository-identity-gh-parent-test-",
352
+ });
353
+ const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
354
+ const resolveFresh = () => resolver.resolve(cwd, { refresh: true });
355
+
356
+ yield* git(cwd, ["init"]);
357
+ yield* git(cwd, ["remote", "add", "fork", "https://github.com/julius/t3code-fork.git"]);
358
+ yield* git(cwd, ["remote", "add", "main", "https://github.com/T3Tools/t3code.git"]);
359
+ yield* git(cwd, ["remote", "add", "upstream", "https://github.com/other/t3code.git"]);
360
+ yield* git(cwd, ["config", "remote.fork.gh-resolved", "t3tools/t3code"]);
361
+ const parent = yield* resolveFresh();
362
+ expect(parent?.canonicalKey).toBe("github.com/t3tools/t3code");
363
+ expect(parent?.locator).toEqual({
364
+ source: "git-remote",
365
+ remoteName: "main",
366
+ remoteUrl: "https://github.com/T3Tools/t3code.git",
367
+ });
368
+
369
+ // No remote fetches the marked repository, and several marks leave gh's choice unknown.
370
+ yield* git(cwd, ["config", "remote.fork.gh-resolved", "someone/else"]);
371
+ expect((yield* resolveFresh())?.locator.remoteName).toBe("upstream");
372
+ yield* git(cwd, ["config", "remote.fork.gh-resolved", "base"]);
373
+ yield* git(cwd, ["config", "remote.main.gh-resolved", "base"]);
374
+ expect((yield* resolveFresh())?.locator.remoteName).toBe("upstream");
375
+
376
+ // The same path on another HTTPS port is a different server.
377
+ yield* git(cwd, ["config", "--unset", "remote.fork.gh-resolved"]);
378
+ yield* git(cwd, ["config", "--unset", "remote.main.gh-resolved"]);
379
+ yield* git(cwd, ["remote", "add", "backup", "https://ghe.example.com:9443/acme/web.git"]);
380
+ yield* git(cwd, ["remote", "add", "ghe-fork", "https://ghe.example.com:8443/me/web.git"]);
381
+ yield* git(cwd, ["remote", "add", "ghe-parent", "https://ghe.example.com:8443/acme/web.git"]);
382
+ yield* git(cwd, ["config", "remote.ghe-fork.gh-resolved", "acme/web"]);
383
+ expect((yield* resolveFresh())?.locator.remoteName).toBe("ghe-parent");
384
+ }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
385
+ );
386
+
387
it.effect("reads the project's gh default when the server inherited GIT_DIR", () =>
388
Effect.gen(function* () {
389
const fileSystem = yield* FileSystem.FileSystem;
apps/server/src/project/RepositoryIdentityResolver.ts
+54
-39
@@ -1,5 +1,7 @@
1
import type { RepositoryIdentity, SourceControlProviderError } from "@t3tools/contracts";
2
+import { parseGitRemoteHost } from "@t3tools/source-control-core/shared/definition";
3
import { repositoryPathFromRemoteUrl } from "@t3tools/source-control-core/server/SourceControlProvider";
4
+import { normalizeGitRemoteUrl } from "@t3tools/shared/git";
5
import * as Cache from "effect/Cache";
6
import * as Context from "effect/Context";
7
import * as Data from "effect/Data";
@@ -69,21 +71,44 @@ function parseRemoteFetchUrls(stdout: string): Map<string, string> {
71
return remotes;
72
}
73
72
-// `gh repo set-default` marks the chosen remote with `remote.<name>.gh-resolved base`.
73
-function parseGhDefaultRemoteName(stdout: string): string | null {
74
- const marked = stdout
75
- .split("\n")
76
- .map((line) => /^remote\.(.+)\.gh-resolved\s+base$/.exec(line.trim())?.[1])
77
- .filter((name): name is string => name !== undefined);
78
- return marked.length === 1 ? marked[0]! : null;
74
+// `gh repo set-default` marks one remote with `base`, or with OWNER/REPO when it picked a
75
+// parent repository. gh reads PRs from that repository, so the identity follows the mark when
76
+// a remote fetches it. The locator must name a real remote: review checkouts fetch through it.
77
+function pickMarkedRemote(
78
+ remotes: ReadonlyMap<string, string>,
79
+ resolved: string,
80
+): { readonly remoteName: string; readonly remoteUrl: string } | null {
81
+ const marks = resolved.split("\n").flatMap((line) => {
82
+ const match = /^remote\.(.+)\.gh-resolved\s+(\S+)$/u.exec(line.trim());
83
+ const remoteUrl = match?.[1] === undefined ? undefined : remotes.get(match[1]);
84
+ return match?.[1] && match[2] && remoteUrl
85
+ ? [{ remoteName: match[1], remoteUrl, value: match[2] }]
86
+ : [];
87
+ });
88
+ // gh's choice is ambiguous with several marks; GitHubSourceControlProvider ignores them too.
89
+ const [mark, ...others] = marks;
90
+ if (!mark || others.length > 0) return null;
91
+ if (mark.value === "base") return { remoteName: mark.remoteName, remoteUrl: mark.remoteUrl };
92
+ // Compare hosts with HTTP ports kept: one hostname can serve separate forges on two ports.
93
+ const host = parseGitRemoteHost(mark.remoteUrl)?.host;
94
+ const markedPath = mark.value.toLowerCase();
95
+ const [remoteName, remoteUrl] =
96
+ [...remotes.entries()].find(
97
+ ([, url]) =>
98
+ parseGitRemoteHost(url)?.host === host &&
99
+ normalizeGitRemoteUrl(url).split("/").slice(1).join("/") === markedPath,
100
+ ) ?? [];
101
+ return remoteName && remoteUrl ? { remoteName, remoteUrl } : null;
102
}
103
104
function pickPrimaryRemote(
105
remotes: ReadonlyMap<string, string>,
83
- ghDefaultRemoteName: string | null,
106
+ resolved: string,
107
): { readonly remoteName: string; readonly remoteUrl: string } | null {
85
- for (const preferredRemoteName of [ghDefaultRemoteName, "upstream", "origin"]) {
86
- if (preferredRemoteName === null) continue;
108
+ const marked = pickMarkedRemote(remotes, resolved);
109
+ if (marked) return marked;
110
+
111
+ for (const preferredRemoteName of ["upstream", "origin"] as const) {
112
const remoteUrl = remotes.get(preferredRemoteName);
113
if (remoteUrl) {
114
return { remoteName: preferredRemoteName, remoteUrl };
@@ -242,40 +267,30 @@ const resolveRepositoryIdentityFromCacheKey = Effect.fn(
267
): Effect.fn.Return<RepositoryIdentity | null, never, ProcessRunner.ProcessRunner> {
268
const processRunner = yield* ProcessRunner.ProcessRunner;
269
const env = gitEnv(yield* HostProcess.Platform);
245
- const remoteResult = yield* processRunner
246
- .run({
247
- command: "git",
248
- args: ["-C", cacheKey.rootPath, ...(cacheKey.isBare ? ["--git-dir=."] : []), "remote", "-v"],
249
- env,
250
- timeoutBehavior: "timedOutResult",
251
- })
252
- .pipe(Effect.option);
270
+ const runGit = (args: ReadonlyArray<string>) =>
271
+ processRunner
272
+ .run({
273
+ command: "git",
274
+ args: ["-C", cacheKey.rootPath, ...(cacheKey.isBare ? ["--git-dir=."] : []), ...args],
275
+ env,
276
+ timeoutBehavior: "timedOutResult",
277
+ })
278
+ .pipe(Effect.option);
279
+ const [remoteResult, resolvedResult] = yield* Effect.all(
280
+ [runGit(["remote", "-v"]), runGit(["config", "--get-regexp", "^remote\\..*\\.gh-resolved$"])],
281
+ { concurrency: "unbounded" },
282
+ );
283
if (remoteResult._tag === "None" || remoteResult.value.code !== 0) {
284
return null;
285
}
286
257
- const ghResolvedResult = yield* processRunner
258
- .run({
259
- command: "git",
260
- args: [
261
- "-C",
262
- cacheKey.rootPath,
263
- ...(cacheKey.isBare ? ["--git-dir=."] : []),
264
- "config",
265
- "--get-regexp",
266
- "^remote\\..*\\.gh-resolved$",
267
- ],
268
- env,
269
- timeoutBehavior: "timedOutResult",
270
- })
271
- .pipe(Effect.option);
272
- const ghDefaultRemoteName =
273
- ghResolvedResult._tag === "Some" && ghResolvedResult.value.code === 0
274
- ? parseGhDefaultRemoteName(ghResolvedResult.value.stdout)
275
- : null;
276
-
287
const remotes = parseRemoteFetchUrls(remoteResult.value.stdout);
278
- const remote = pickPrimaryRemote(remotes, ghDefaultRemoteName);
288
+ // Exits 1 when no remote has a mark.
289
+ const resolved =
290
+ resolvedResult._tag === "Some" && resolvedResult.value.code === 0
291
+ ? resolvedResult.value.stdout
292
+ : "";
293
+ const remote = pickPrimaryRemote(remotes, resolved);
294
return remote
295
? buildRepositoryIdentity({
296
...remote,