Skip to content
This repository was archived by the owner on Oct 21, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion apps/server/src/project/RepositoryIdentityResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,16 @@ it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => {
expect(calls).toEqual([
["-C", "/repo/packages/web", "rev-parse", "--show-toplevel"],
["-C", "/repo", "remote", "-v"],
["-C", "/repo", "config", "--get-regexp", "^remote\\..*\\.gh-resolved$"],
]);

const refreshed = yield* resolver.resolve("/repo/packages/web", { refresh: true });
expect(refreshed?.rootPath).toBe("/repo/packages/web");
expect(yield* resolver.resolve("/repo/packages/web")).toEqual(refreshed);
expect(calls.slice(2)).toEqual([
expect(calls.slice(3)).toEqual([
["-C", "/repo/packages/web", "rev-parse", "--show-toplevel"],
["-C", "/repo/packages/web", "remote", "-v"],
["-C", "/repo/packages/web", "config", "--get-regexp", "^remote\\..*\\.gh-resolved$"],
]);
remoteUrl = "git@ssh.forge.test:team/repo.git";
const forgejo = yield* resolver.resolve(rootPath, { refresh: true });
Expand Down Expand Up @@ -170,6 +172,7 @@ it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => {
["-C", "/repo/packages/web", "rev-parse", "--show-toplevel"],
["-C", "/repo/packages/web", "rev-parse", "--show-toplevel"],
["-C", "/repo", "remote", "-v"],
["-C", "/repo", "config", "--get-regexp", "^remote\\..*\\.gh-resolved$"],
]);
}).pipe(Effect.provide(Layer.merge(TestClock.layer(), resolverLayer)));
});
Expand Down Expand Up @@ -288,6 +291,52 @@ it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => {
}).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
);

it.effect("prefers the remote gh marked as the default repository", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const cwd = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-repository-identity-gh-default-test-",
});

yield* git(cwd, ["init"]);
yield* git(cwd, ["remote", "add", "origin", "git@github.com:julius/t3code.git"]);
yield* git(cwd, ["remote", "add", "upstream", "git@github.com:T3Tools/t3code.git"]);

const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
expect((yield* resolver.resolve(cwd))?.locator.remoteName).toBe("upstream");

yield* git(cwd, ["config", "remote.origin.gh-resolved", "base"]);
const identity = yield* resolver.resolve(cwd, { refresh: true });

expect(identity?.locator.remoteName).toBe("origin");
expect(identity?.canonicalKey).toBe("github.com/julius/t3code");
}).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
);

it.effect("resolves a gh default parent repository that has no remote", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const cwd = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-repository-identity-gh-parent-test-",
});

yield* git(cwd, ["init"]);
yield* git(cwd, ["remote", "add", "origin", "git@github.com:julius/t3code.git"]);
yield* git(cwd, ["config", "remote.origin.gh-resolved", "T3Tools/t3code"]);

const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
const identity = yield* resolver.resolve(cwd);

expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
expect(identity?.owner).toBe("t3tools");
expect(identity?.locator).toEqual({
source: "git-remote",
remoteName: "origin",
remoteUrl: "git@github.com:T3Tools/t3code.git",
});
}).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
);

it.effect("uses the last remote path segment as the repository name for nested groups", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
Expand Down
53 changes: 45 additions & 8 deletions apps/server/src/project/RepositoryIdentityResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,41 @@ function parseRemoteFetchUrls(stdout: string): Map<string, string> {
return remotes;
}

interface GhDefaultRemote {
readonly remoteName: string;
readonly resolution: string;
}

// `gh repo set-default` marks one remote with `remote.<name>.gh-resolved`: `base`
// for that remote's own repo, or `OWNER/REPO` for a parent repo with no remote.
function parseGhDefaultRemote(stdout: string): GhDefaultRemote | null {
for (const line of stdout.split("\n")) {
const match = /^remote\.(.+)\.gh-resolved\s+(\S+)$/.exec(line.trim());
const [, remoteName, resolution] = match ?? [];
if (remoteName && resolution) return { remoteName, resolution };
}
return null;
}

function withRepositoryPath(remoteUrl: string, repository: string): string | null {
if (!/^[^/\s]+\/[^/\s]+$/.test(repository)) return null;
const match = /^(.*[/:])[^/:]+\/[^/]+?(\.git)?\/?$/.exec(remoteUrl);
return match ? `${match[1]}${repository}${match[2] ?? ""}` : null;
}

function pickPrimaryRemote(
remotes: ReadonlyMap<string, string>,
ghDefault: GhDefaultRemote | null,
): { readonly remoteName: string; readonly remoteUrl: string } | null {
const ghRemoteUrl = ghDefault ? remotes.get(ghDefault.remoteName) : undefined;
if (ghDefault && ghRemoteUrl) {
const remoteUrl =
ghDefault.resolution === "base"
? ghRemoteUrl
: withRepositoryPath(ghRemoteUrl, ghDefault.resolution);
if (remoteUrl) return { remoteName: ghDefault.remoteName, remoteUrl };
}

for (const preferredRemoteName of ["upstream", "origin"] as const) {
const remoteUrl = remotes.get(preferredRemoteName);
if (remoteUrl) {
Expand Down Expand Up @@ -125,18 +157,23 @@ const resolveRepositoryIdentityFromCacheKey = Effect.fn(
cacheKey: string,
): Effect.fn.Return<RepositoryIdentity | null, never, ProcessRunner.ProcessRunner> {
const processRunner = yield* ProcessRunner.ProcessRunner;
const remoteResult = yield* processRunner
.run({
command: "git",
args: ["-C", cacheKey, "remote", "-v"],
timeoutBehavior: "timedOutResult",
})
.pipe(Effect.option);
const git = (args: ReadonlyArray<string>) =>
processRunner
.run({ command: "git", args: ["-C", cacheKey, ...args], timeoutBehavior: "timedOutResult" })
.pipe(Effect.option);
const [remoteResult, ghDefaultResult] = yield* Effect.all(
[git(["remote", "-v"]), git(["config", "--get-regexp", "^remote\\..*\\.gh-resolved$"])],
{ concurrency: "unbounded" },
);
if (remoteResult._tag === "None" || remoteResult.value.code !== 0) {
return null;
}

const remote = pickPrimaryRemote(parseRemoteFetchUrls(remoteResult.value.stdout));
const ghDefault =
ghDefaultResult._tag === "Some" && ghDefaultResult.value.code === 0
? parseGhDefaultRemote(ghDefaultResult.value.stdout)
: null;
const remote = pickPrimaryRemote(parseRemoteFetchUrls(remoteResult.value.stdout), ghDefault);
return remote ? buildRepositoryIdentity({ ...remote, rootPath: cacheKey }) : null;
});

Expand Down
Loading