Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off - realpathSync.native resolves Windows 8.3 short names, which the Effect realPath does not.
2import * as NodeFS from "node:fs";
3import * as NodeServices from "@effect/platform-node/NodeServices";
4import { expect, it } from "@effect/vitest";
5import * as Effect from "effect/Effect";
6import * as FileSystem from "effect/FileSystem";
7import * as Path from "effect/Path";
8import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
9import * as HostProcess from "@t3tools/shared/HostProcess";
10import { vi } from "vite-plus/test";
11
12import * as ProcessRunner from "../processRunner.ts";
13import { sourceControlDefinitions } from "../sourceControl/sourceControlDefinitions.ts";
14import * as RepositoryIdentityResolver from "./RepositoryIdentityResolver.ts";
15
16const normalizePathSeparators = (value: string) => value.replaceAll("\\", "/");
17const normalizeResolvedPath = (value: string) => normalizePathSeparators(value);
18
19// Windows forbids CR and LF in file names.
20const lineBreakSuffixes = HostProcess.Platform.defaultValue() === "win32" ? [] : ["\r", "\n"];
21const bareLayouts = [
22 "bare clone",
23 "dot-bare",
24 ...lineBreakSuffixes.map((suffix) => `bare clone${suffix}`),
25];
26
27const git = (cwd: string, args: ReadonlyArray<string>) =>
28 Effect.gen(function* () {
29 const processRunner = yield* ProcessRunner.ProcessRunner;
30 return yield* processRunner.run({
31 command: "git",
32 args: ["-C", cwd, ...args],
33 });
34 }).pipe(Effect.provide(ProcessRunner.layer));
35
36it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => {
37 it.effect("normalizes equivalent GitHub remotes into a stable repository identity", () =>
38 Effect.gen(function* () {
39 const fileSystem = yield* FileSystem.FileSystem;
40 const path = yield* Path.Path;
41 const cwd = yield* fileSystem.makeTempDirectoryScoped({
42 prefix: "t3-repository-identity-test-",
43 });
44
45 yield* git(cwd, ["init"]);
46 yield* git(cwd, ["remote", "add", "origin", "git@github.com:T3Tools/t3code.git"]);
47
48 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
49 const identity = yield* resolver.resolve(cwd);
50 // Native realpath matches git's long paths when Windows temp dirs use 8.3 names.
51 const resolvedIdentityRoot =
52 identity?.rootPath === undefined ? "" : NodeFS.realpathSync.native(identity.rootPath);
53 const resolvedCwd = NodeFS.realpathSync.native(cwd);
54
55 expect(identity).not.toBeNull();
56 expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
57 expect(normalizeResolvedPath(resolvedIdentityRoot)).toBe(normalizeResolvedPath(resolvedCwd));
58 expect(identity?.displayName).toBe("T3Tools/t3code");
59 expect(identity?.provider).toBe("github");
60 expect(identity?.owner).toBe("T3Tools");
61 expect(identity?.name).toBe("t3code");
62 expect(yield* resolver.resolve(path.join(cwd, ".git"))).toBeNull();
63 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
64 );
65
66 it.effect("identifies the host of an SSH remote that leaves its user to ssh config", () =>
67 Effect.gen(function* () {
68 const fileSystem = yield* FileSystem.FileSystem;
69 const cwd = yield* fileSystem.makeTempDirectoryScoped({
70 prefix: "t3-repository-identity-userless-remote-test-",
71 });
72
73 yield* git(cwd, ["init"]);
74 yield* git(cwd, ["remote", "add", "origin", "github.com:T3Tools/t3code.git"]);
75
76 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
77 const identity = yield* resolver.resolve(cwd);
78
79 expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
80 expect(identity?.provider).toBe("github");
81 expect(identity?.displayName).toBe("T3Tools/t3code");
82 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
83 );
84
85 it.effect("keys a remote behind an SSH host alias by the host ssh config resolves it to", () =>
86 Effect.gen(function* () {
87 const fileSystem = yield* FileSystem.FileSystem;
88 const aliasDir = yield* fileSystem.makeTempDirectoryScoped({
89 prefix: "t3-repository-identity-ssh-alias-test-",
90 });
91 const plainDir = yield* fileSystem.makeTempDirectoryScoped({
92 prefix: "t3-repository-identity-ssh-plain-test-",
93 });
94
95 yield* git(aliasDir, ["init"]);
96 yield* git(aliasDir, ["remote", "add", "origin", "git@github.com-personal:owner/repo.git"]);
97 yield* git(plainDir, ["init"]);
98 yield* git(plainDir, ["remote", "add", "origin", "git@github.com:owner/repo.git"]);
99
100 const runner = yield* ProcessRunner.ProcessRunner;
101 // Stands in for `Host github.com-personal` / `HostName github.com` in ~/.ssh/config.
102 const resolver = yield* RepositoryIdentityResolver.make().pipe(
103 Effect.provideService(ProcessRunner.ProcessRunner, {
104 run: (input) =>
105 input.command === "ssh" && input.args.includes("-G")
106 ? Effect.succeed({
107 stdout: input.args.some((arg) => arg.includes("github.com-personal"))
108 ? "user git\nhostname github.com\nport 22\n"
109 : `hostname ${input.args.at(-1) ?? ""}\nport 22\n`,
110 stderr: "",
111 code: ChildProcessSpawner.ExitCode(0),
112 timedOut: false,
113 stdoutTruncated: false,
114 stderrTruncated: false,
115 stdoutInvalidUtf8: false,
116 stderrInvalidUtf8: false,
117 })
118 : runner.run(input),
119 }),
120 );
121
122 const aliasIdentity = yield* resolver.resolve(aliasDir);
123 const plainIdentity = yield* resolver.resolve(plainDir);
124
125 expect(plainIdentity?.canonicalKey).toBe("github.com/owner/repo");
126 expect(aliasIdentity?.canonicalKey).toBe(plainIdentity?.canonicalKey);
127 expect(aliasIdentity?.locator.remoteUrl).toBe("git@github.com-personal:owner/repo.git");
128 }).pipe(Effect.provide(ProcessRunner.layer)),
129 );
130
131 it.effect("keys a remote by its configured URL when insteadOf rewrites it", () =>
132 Effect.gen(function* () {
133 const fileSystem = yield* FileSystem.FileSystem;
134 const cwd = yield* fileSystem.makeTempDirectoryScoped({
135 prefix: "t3-repository-identity-instead-of-test-",
136 });
137
138 yield* git(cwd, ["init"]);
139 yield* git(cwd, ["remote", "add", "origin", "https://github.com/T3Tools/t3code.git"]);
140 yield* git(cwd, ["config", "url.https://proxy.example/.insteadOf", "https://github.com/"]);
141 yield* git(cwd, ["config", "remote.origin.gh-resolved", "base"]);
142 const remotes = yield* git(cwd, ["remote", "-v"]);
143 expect(remotes.stdout).toContain("https://proxy.example/T3Tools/t3code.git (fetch)");
144
145 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
146 const identity = yield* resolver.resolve(cwd);
147
148 expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
149 expect(identity?.provider).toBe("github");
150 expect(identity?.displayName).toBe("T3Tools/t3code");
151 expect(identity?.locator).toEqual({
152 source: "git-remote",
153 remoteName: "origin",
154 remoteUrl: "https://github.com/T3Tools/t3code.git",
155 });
156
157 const shorthandCwd = yield* fileSystem.makeTempDirectoryScoped({
158 prefix: "t3-repository-identity-instead-of-shorthand-test-",
159 });
160 yield* git(shorthandCwd, ["init"]);
161 yield* git(shorthandCwd, ["remote", "add", "origin", "gh:T3Tools/t3code.git"]);
162 yield* git(shorthandCwd, ["config", "url.https://github.com/.insteadOf", "gh:"]);
163
164 const shorthandIdentity = yield* resolver.resolve(shorthandCwd);
165
166 expect(shorthandIdentity?.canonicalKey).toBe("github.com/t3tools/t3code");
167 expect(shorthandIdentity?.provider).toBe("github");
168 expect(shorthandIdentity?.locator.remoteUrl).toBe("https://github.com/T3Tools/t3code.git");
169 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
170 );
171
172 it.effect("keeps a literal .git in an Azure DevOps repository name", () =>
173 Effect.gen(function* () {
174 const fileSystem = yield* FileSystem.FileSystem;
175 const cwd = yield* fileSystem.makeTempDirectoryScoped({
176 prefix: "t3-repository-identity-azure-git-suffix-test-",
177 });
178
179 yield* git(cwd, ["init"]);
180 yield* git(cwd, [
181 "remote",
182 "add",
183 "origin",
184 "https://dev.azure.com/Acme/Platform/_git/Tool.git",
185 ]);
186
187 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
188 const identity = yield* resolver.resolve(cwd);
189
190 expect(identity?.displayName).toBe("Acme/Platform/_git/Tool.git");
191 expect(identity?.name).toBe("Tool.git");
192 expect(sourceControlDefinitions.repositorySelector(identity)).toBe("Tool.git");
193 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
194 );
195
196 it.effect("resolves the fetch remote of a partial clone", () =>
197 Effect.gen(function* () {
198 const fileSystem = yield* FileSystem.FileSystem;
199 const cwd = yield* fileSystem.makeTempDirectoryScoped({
200 prefix: "t3-repository-identity-partial-clone-test-",
201 });
202
203 yield* git(cwd, ["init"]);
204 yield* git(cwd, ["remote", "add", "origin", "git@github.com:T3Tools/t3code.git"]);
205 yield* git(cwd, ["remote", "set-url", "--push", "origin", "git@github.com:T3Tools/push.git"]);
206 yield* git(cwd, ["config", "remote.origin.promisor", "true"]);
207 yield* git(cwd, ["config", "remote.origin.partialclonefilter", "blob:none"]);
208
209 const remotes = yield* git(cwd, ["remote", "-v"]);
210 expect(remotes.stdout).toContain("(fetch) [blob:none]");
211
212 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
213 const identity = yield* resolver.resolve(cwd);
214 expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
215 expect(identity?.locator.remoteUrl).toBe("git@github.com:T3Tools/t3code.git");
216 expect(identity?.locator.remoteName).toBe("origin");
217 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
218 );
219
220 it.effect("resolves a remote whose URL contains a space", () =>
221 Effect.gen(function* () {
222 const fileSystem = yield* FileSystem.FileSystem;
223 const cwd = yield* fileSystem.makeTempDirectoryScoped({
224 prefix: "t3-repository-identity-spaced-url-test-",
225 });
226
227 yield* git(cwd, ["init"]);
228 yield* git(cwd, ["remote", "add", "origin", "ssh://git@git.example.test/team repos/app.git"]);
229
230 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
231 const identity = yield* resolver.resolve(cwd);
232 expect(identity?.locator.remoteUrl).toBe("ssh://git@git.example.test/team repos/app.git");
233 expect(identity?.locator.remoteName).toBe("origin");
234 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
235 );
236
237 it.effect("keeps a login embedded in a remote URL out of the identity", () =>
238 Effect.gen(function* () {
239 const fileSystem = yield* FileSystem.FileSystem;
240 const cwd = yield* fileSystem.makeTempDirectoryScoped({
241 prefix: "t3-repository-identity-credentials-test-",
242 });
243
244 yield* git(cwd, ["init"]);
245 yield* git(cwd, [
246 "remote",
247 "add",
248 "origin",
249 "https://me:origin-secret@github.com/me/t3code.git",
250 ]);
251 yield* git(cwd, [
252 "remote",
253 "add",
254 "upstream",
255 "https://x-access-token:upstream-secret@github.com/T3Tools/t3code.git?token=query-secret",
256 ]);
257
258 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
259 const identity = yield* resolver.resolve(cwd);
260 expect(identity?.locator.remoteUrl).toBe("https://github.com/T3Tools/t3code.git");
261 expect(identity?.origin?.remoteUrl).toBe("https://github.com/me/t3code.git");
262 expect(JSON.stringify(identity)).not.toContain("secret");
263 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
264 );
265
266 it.effect.each(["repo", "repo ", ...lineBreakSuffixes.map((suffix) => `repo${suffix}`)])(
267 "returns the git top-level root path when resolving from a nested workspace in %j",
268 (rootName) =>
269 Effect.gen(function* () {
270 const fileSystem = yield* FileSystem.FileSystem;
271 const path = yield* Path.Path;
272 const tempDir = yield* fileSystem.makeTempDirectoryScoped({
273 prefix: "t3-repository-identity-nested-root-test-",
274 });
275 const repoRoot = path.join(tempDir, rootName);
276 const nestedWorkspace = path.join(repoRoot, "packages", "web");
277
278 yield* fileSystem.makeDirectory(nestedWorkspace, { recursive: true });
279 yield* git(repoRoot, ["init"]);
280 yield* git(repoRoot, ["remote", "add", "origin", "git@github.com:T3Tools/t3code.git"]);
281
282 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
283 const identity = yield* resolver.resolve(nestedWorkspace);
284 const resolvedIdentityRoot =
285 identity?.rootPath === undefined ? "" : NodeFS.realpathSync.native(identity.rootPath);
286 const resolvedRepoRoot = NodeFS.realpathSync.native(repoRoot);
287
288 expect(identity).not.toBeNull();
289 expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
290 expect(normalizeResolvedPath(resolvedIdentityRoot)).toBe(
291 normalizeResolvedPath(resolvedRepoRoot),
292 );
293 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
294 );
295
296 it.effect.each(bareLayouts)(
297 "resolves a %j to its Git directory and the same identity as a checkout",
298 (layout) =>
299 Effect.gen(function* () {
300 const fileSystem = yield* FileSystem.FileSystem;
301 const path = yield* Path.Path;
302 const checkout = yield* fileSystem.makeTempDirectoryScoped({
303 prefix: "t3-repository-identity-checkout-",
304 });
305 const parent = yield* fileSystem.makeTempDirectoryScoped({
306 prefix: "t3-repository-identity-bare-",
307 });
308 const root = path.join(parent, layout);
309 yield* fileSystem.makeDirectory(root);
310 const bareGitDir = layout === "dot-bare" ? path.join(root, ".bare") : root;
311
312 yield* git(checkout, ["init"]);
313 yield* git(checkout, ["remote", "add", "origin", "git@github.com:T3Tools/t3code.git"]);
314 yield* git(root, ["init", "--bare", bareGitDir]);
315 if (layout === "dot-bare") {
316 yield* fileSystem.writeFileString(path.join(root, ".git"), "gitdir: ./.bare\n");
317 }
318 yield* git(root, [
319 "--git-dir",
320 bareGitDir,
321 "remote",
322 "add",
323 "origin",
324 "https://github.com/T3Tools/t3code.git",
325 ]);
326
327 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
328 const checkoutIdentity = yield* resolver.resolve(checkout);
329 expect(checkoutIdentity?.canonicalKey).toBe("github.com/t3tools/t3code");
330 const resolvedBareGitDir = NodeFS.realpathSync.native(bareGitDir);
331 const projectRoots = layout === "dot-bare" ? [root, bareGitDir] : [root];
332
333 for (const projectRoot of projectRoots) {
334 const identity = yield* resolver.resolve(projectRoot);
335 const resolvedIdentityRoot =
336 identity?.rootPath === undefined ? "" : NodeFS.realpathSync.native(identity.rootPath);
337
338 expect(identity).not.toBeNull();
339 expect(identity?.canonicalKey).toBe(checkoutIdentity?.canonicalKey);
340 expect(normalizeResolvedPath(resolvedIdentityRoot)).toBe(
341 normalizeResolvedPath(resolvedBareGitDir),
342 );
343 }
344
345 yield* git(root, ["--git-dir", bareGitDir, "remote", "remove", "origin"]);
346 for (const projectRoot of projectRoots) {
347 expect(yield* resolver.resolve(projectRoot, { refresh: true })).toBeNull();
348 }
349 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
350 );
351
352 it.effect(
353 "returns null for non-git, missing and no-remote folders, probing only what can exist",
354 () =>
355 Effect.gen(function* () {
356 const fileSystem = yield* FileSystem.FileSystem;
357 const path = yield* Path.Path;
358 const nonGitDir = yield* fileSystem.makeTempDirectoryScoped({
359 prefix: "t3-repository-identity-non-git-",
360 });
361 const gitDir = yield* fileSystem.makeTempDirectoryScoped({
362 prefix: "t3-repository-identity-no-remote-",
363 });
364
365 yield* git(gitDir, ["init"]);
366
367 const runner = yield* ProcessRunner.ProcessRunner;
368 const runs: Array<ReadonlyArray<string>> = [];
369 const resolver = yield* RepositoryIdentityResolver.make().pipe(
370 Effect.provideService(ProcessRunner.ProcessRunner, {
371 run: (input) =>
372 Effect.suspend(() => {
373 runs.push(input.args);
374 return runner.run(input);
375 }),
376 }),
377 );
378 const bareProbes = () =>
379 runs.filter((args) => args.some((arg) => arg.startsWith("--git-dir=")));
380
381 expect(yield* resolver.resolve(path.join(nonGitDir, "deleted"))).toBeNull();
382 expect(runs).toEqual([]);
383
384 expect(yield* resolver.resolve(nonGitDir)).toBeNull();
385 expect(bareProbes()).toEqual([]);
386
387 expect(yield* resolver.resolve(gitDir)).toBeNull();
388 }).pipe(Effect.provide(ProcessRunner.layer)),
389 );
390
391 it.effect("ignores local-path remotes such as GitButler's gb-local", () =>
392 Effect.gen(function* () {
393 const fileSystem = yield* FileSystem.FileSystem;
394 const localOnlyDir = yield* fileSystem.makeTempDirectoryScoped({
395 prefix: "t3-repository-identity-local-remote-",
396 });
397 const mixedDir = yield* fileSystem.makeTempDirectoryScoped({
398 prefix: "t3-repository-identity-mixed-remote-",
399 });
400
401 yield* git(localOnlyDir, ["init"]);
402 yield* git(localOnlyDir, ["remote", "add", "gb-local", "."]);
403 yield* git(localOnlyDir, ["remote", "add", "backup", "/srv/git/repo.git"]);
404 yield* git(localOnlyDir, ["remote", "add", "archive", "file:///srv/git/repo.git"]);
405 yield* git(mixedDir, ["init"]);
406 yield* git(mixedDir, ["remote", "add", "gb-local", "."]);
407 yield* git(mixedDir, ["remote", "add", "mirror", "git@github.com:T3Tools/t3code.git"]);
408
409 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
410 expect(yield* resolver.resolve(localOnlyDir)).toBeNull();
411
412 const mixedIdentity = yield* resolver.resolve(mixedDir);
413 expect(mixedIdentity?.locator.remoteName).toBe("mirror");
414 expect(mixedIdentity?.canonicalKey).toBe("github.com/t3tools/t3code");
415 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
416 );
417
418 it.effect.each(["add", "replace"] as const)(
419 "refreshes the primary upstream after %s before cache expiry",
420 (change) =>
421 Effect.gen(function* () {
422 const fileSystem = yield* FileSystem.FileSystem;
423 const cwd = yield* fileSystem.makeTempDirectoryScoped({
424 prefix: "t3-repository-identity-upstream-test-",
425 });
426
427 yield* git(cwd, ["init"]);
428 yield* git(cwd, ["remote", "add", "origin", "git@github.com:julius/t3code.git"]);
429 if (change === "replace") {
430 yield* git(cwd, ["remote", "add", "upstream", "git@github.com:T3Tools/previous.git"]);
431 }
432
433 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
434 const initialIdentity = yield* resolver.resolve(cwd);
435 expect(initialIdentity?.canonicalKey).toBe(
436 change === "add" ? "github.com/julius/t3code" : "github.com/t3tools/previous",
437 );
438
439 yield* git(cwd, [
440 "remote",
441 change === "add" ? "add" : "set-url",
442 "upstream",
443 "git@github.com:T3Tools/t3code.git",
444 ]);
445 expect(yield* resolver.resolve(cwd)).toEqual(initialIdentity);
446 const identity = yield* resolver.resolve(cwd, { refresh: true });
447
448 expect(identity).not.toBeNull();
449 expect(identity?.locator.remoteName).toBe("upstream");
450 expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
451 expect(identity?.displayName).toBe("T3Tools/t3code");
452 expect(yield* resolver.resolve(cwd)).toEqual(identity);
453 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
454 );
455
456 it.effect("reports a fork's own remote as origin next to the upstream identity", () =>
457 Effect.gen(function* () {
458 const fileSystem = yield* FileSystem.FileSystem;
459 const cwd = yield* fileSystem.makeTempDirectoryScoped({
460 prefix: "t3-repository-identity-fork-test-",
461 });
462
463 yield* git(cwd, ["init"]);
464 yield* git(cwd, ["remote", "add", "origin", "git@github.com:julius/t3code-fork.git"]);
465 yield* git(cwd, ["remote", "add", "upstream", "git@github.com:T3Tools/t3code.git"]);
466
467 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
468 const identity = yield* resolver.resolve(cwd);
469
470 expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
471 expect(identity?.displayName).toBe("T3Tools/t3code");
472 expect(identity?.origin).toEqual({
473 canonicalKey: "github.com/julius/t3code-fork",
474 remoteUrl: "git@github.com:julius/t3code-fork.git",
475 displayName: "julius/t3code-fork",
476 });
477 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
478 );
479
480 it.effect("uses the fork as the identity when gh repo set-default points at it", () =>
481 Effect.gen(function* () {
482 const fileSystem = yield* FileSystem.FileSystem;
483 const cwd = yield* fileSystem.makeTempDirectoryScoped({
484 prefix: "t3-repository-identity-gh-default-test-",
485 });
486
487 yield* git(cwd, ["init"]);
488 yield* git(cwd, ["remote", "add", "origin", "git@github.com:julius/t3code-fork.git"]);
489 yield* git(cwd, ["remote", "add", "upstream", "git@github.com:T3Tools/t3code.git"]);
490 yield* git(cwd, ["config", "remote.origin.gh-resolved", "base"]);
491
492 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
493 const identity = yield* resolver.resolve(cwd);
494
495 expect(identity?.canonicalKey).toBe("github.com/julius/t3code-fork");
496 expect(identity?.locator.remoteName).toBe("origin");
497 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
498 );
499
500 it.effect("follows an OWNER/REPO mark only to a remote that fetches that repository", () =>
501 Effect.gen(function* () {
502 const fileSystem = yield* FileSystem.FileSystem;
503 const cwd = yield* fileSystem.makeTempDirectoryScoped({
504 prefix: "t3-repository-identity-gh-parent-test-",
505 });
506 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
507 const resolveFresh = () => resolver.resolve(cwd, { refresh: true });
508
509 yield* git(cwd, ["init"]);
510 yield* git(cwd, ["remote", "add", "fork", "https://github.com/julius/t3code-fork.git"]);
511 yield* git(cwd, ["remote", "add", "main", "https://github.com/T3Tools/t3code.git"]);
512 yield* git(cwd, ["remote", "add", "upstream", "https://github.com/other/t3code.git"]);
513 yield* git(cwd, ["config", "remote.fork.gh-resolved", "t3tools/t3code"]);
514 const parent = yield* resolveFresh();
515 expect(parent?.canonicalKey).toBe("github.com/t3tools/t3code");
516 expect(parent?.locator).toEqual({
517 source: "git-remote",
518 remoteName: "main",
519 remoteUrl: "https://github.com/T3Tools/t3code.git",
520 });
521
522 // No remote fetches the marked repository, and several marks leave gh's choice unknown.
523 yield* git(cwd, ["config", "remote.fork.gh-resolved", "someone/else"]);
524 expect((yield* resolveFresh())?.locator.remoteName).toBe("upstream");
525 yield* git(cwd, ["config", "remote.fork.gh-resolved", "base"]);
526 yield* git(cwd, ["config", "remote.main.gh-resolved", "base"]);
527 expect((yield* resolveFresh())?.locator.remoteName).toBe("upstream");
528
529 // The same path on another HTTPS port is a different server.
530 yield* git(cwd, ["config", "--unset", "remote.fork.gh-resolved"]);
531 yield* git(cwd, ["config", "--unset", "remote.main.gh-resolved"]);
532 yield* git(cwd, ["remote", "add", "backup", "https://ghe.example.com:9443/acme/web.git"]);
533 yield* git(cwd, ["remote", "add", "ghe-fork", "https://ghe.example.com:8443/me/web.git"]);
534 yield* git(cwd, ["remote", "add", "ghe-parent", "https://ghe.example.com:8443/acme/web.git"]);
535 yield* git(cwd, ["config", "remote.ghe-fork.gh-resolved", "acme/web"]);
536 expect((yield* resolveFresh())?.locator.remoteName).toBe("ghe-parent");
537 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
538 );
539
540 it.effect("reads the project's gh default when the server inherited GIT_DIR", () =>
541 Effect.gen(function* () {
542 const fileSystem = yield* FileSystem.FileSystem;
543 const path = yield* Path.Path;
544 const cwd = yield* fileSystem.makeTempDirectoryScoped({
545 prefix: "t3-repository-identity-gh-default-git-dir-",
546 });
547 const other = yield* fileSystem.makeTempDirectoryScoped({
548 prefix: "t3-repository-identity-inherited-git-dir-",
549 });
550
551 yield* git(cwd, ["init"]);
552 yield* git(cwd, ["remote", "add", "origin", "git@github.com:julius/t3code-fork.git"]);
553 yield* git(cwd, ["remote", "add", "upstream", "git@github.com:T3Tools/t3code.git"]);
554 yield* git(cwd, ["config", "remote.origin.gh-resolved", "base"]);
555 yield* git(other, ["init"]);
556 yield* git(other, ["config", "remote.upstream.gh-resolved", "base"]);
557
558 vi.stubEnv("GIT_DIR", path.join(other, ".git"));
559 yield* Effect.addFinalizer(() => Effect.sync(() => vi.unstubAllEnvs()));
560
561 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
562 const identity = yield* resolver.resolve(cwd);
563
564 expect(identity?.canonicalKey).toBe("github.com/julius/t3code-fork");
565 expect(identity?.locator.remoteName).toBe("origin");
566 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
567 );
568
569 it.effect("uses the last remote path segment as the repository name for nested groups", () =>
570 Effect.gen(function* () {
571 const fileSystem = yield* FileSystem.FileSystem;
572 const cwd = yield* fileSystem.makeTempDirectoryScoped({
573 prefix: "t3-repository-identity-nested-group-test-",
574 });
575
576 yield* git(cwd, ["init"]);
577 yield* git(cwd, ["remote", "add", "origin", "git@gitlab.com:T3Tools/platform/t3code.git"]);
578
579 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
580 const identity = yield* resolver.resolve(cwd);
581
582 expect(identity).not.toBeNull();
583 expect(identity?.canonicalKey).toBe("gitlab.com/t3tools/platform/t3code");
584 expect(identity?.displayName).toBe("T3Tools/platform/t3code");
585 expect(identity?.owner).toBe("T3Tools");
586 expect(identity?.name).toBe("t3code");
587 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
588 );
589});