Skip to content

Commit 3126806

Browse files
fix(server): project identity follows gh repo set-default (pingdotgg#35005)
1 parent 09e1751 commit 3126806

2 files changed

Lines changed: 53 additions & 2 deletions

File tree

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,26 @@ it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => {
303303
}).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
304304
);
305305

306+
it.effect("uses the fork as the identity when gh repo set-default points at it", () =>
307+
Effect.gen(function* () {
308+
const fileSystem = yield* FileSystem.FileSystem;
309+
const cwd = yield* fileSystem.makeTempDirectoryScoped({
310+
prefix: "t3-repository-identity-gh-default-test-",
311+
});
312+
313+
yield* git(cwd, ["init"]);
314+
yield* git(cwd, ["remote", "add", "origin", "[email protected]:julius/t3code-fork.git"]);
315+
yield* git(cwd, ["remote", "add", "upstream", "[email protected]:T3Tools/t3code.git"]);
316+
yield* git(cwd, ["config", "remote.origin.gh-resolved", "base"]);
317+
318+
const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
319+
const identity = yield* resolver.resolve(cwd);
320+
321+
expect(identity?.canonicalKey).toBe("github.com/julius/t3code-fork");
322+
expect(identity?.locator.remoteName).toBe("origin");
323+
}).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
324+
);
325+
306326
it.effect("uses the last remote path segment as the repository name for nested groups", () =>
307327
Effect.gen(function* () {
308328
const fileSystem = yield* FileSystem.FileSystem;

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,21 @@ function parseRemoteFetchUrls(stdout: string): Map<string, string> {
7070
return remotes;
7171
}
7272

73+
// `gh repo set-default` marks the chosen remote with `remote.<name>.gh-resolved base`.
74+
function parseGhDefaultRemoteName(stdout: string): string | null {
75+
const marked = stdout
76+
.split("\n")
77+
.map((line) => /^remote\.(.+)\.gh-resolved\s+base$/.exec(line.trim())?.[1])
78+
.filter((name): name is string => name !== undefined);
79+
return marked.length === 1 ? marked[0]! : null;
80+
}
81+
7382
function pickPrimaryRemote(
7483
remotes: ReadonlyMap<string, string>,
84+
ghDefaultRemoteName: string | null,
7585
): { readonly remoteName: string; readonly remoteUrl: string } | null {
76-
for (const preferredRemoteName of ["upstream", "origin"] as const) {
86+
for (const preferredRemoteName of [ghDefaultRemoteName, "upstream", "origin"]) {
87+
if (preferredRemoteName === null) continue;
7788
const remoteUrl = remotes.get(preferredRemoteName);
7889
if (remoteUrl) {
7990
return { remoteName: preferredRemoteName, remoteUrl };
@@ -239,8 +250,28 @@ const resolveRepositoryIdentityFromCacheKey = Effect.fn(
239250
return null;
240251
}
241252

253+
const ghResolvedResult = yield* processRunner
254+
.run({
255+
command: "git",
256+
args: [
257+
"-C",
258+
cacheKey.rootPath,
259+
...(cacheKey.isBare ? ["--git-dir=."] : []),
260+
"config",
261+
"--get-regexp",
262+
"^remote\\..*\\.gh-resolved$",
263+
],
264+
env: explicitBareRepositoryEnv(process.env, yield* HostProcess.Platform),
265+
timeoutBehavior: "timedOutResult",
266+
})
267+
.pipe(Effect.option);
268+
const ghDefaultRemoteName =
269+
ghResolvedResult._tag === "Some" && ghResolvedResult.value.code === 0
270+
? parseGhDefaultRemoteName(ghResolvedResult.value.stdout)
271+
: null;
272+
242273
const remotes = parseRemoteFetchUrls(remoteResult.value.stdout);
243-
const remote = pickPrimaryRemote(remotes);
274+
const remote = pickPrimaryRemote(remotes, ghDefaultRemoteName);
244275
return remote
245276
? buildRepositoryIdentity({
246277
...remote,

0 commit comments

Comments
 (0)