Skip to content

Commit 9a34554

Browse files
fix(server): project identity follows gh repo set-default
AI-assisted; verified with the tests and tools listed above.
1 parent c9e29b7 commit 9a34554

1 file changed

Lines changed: 33 additions & 2 deletions

File tree

‎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)