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.each(["origin", "team\u3000"])(
132 "keys remote %j by its configured URL when insteadOf rewrites it",
133 (remoteName) =>
134 Effect.gen(function* () {
135 const fileSystem = yield* FileSystem.FileSystem;
136 const cwd = yield* fileSystem.makeTempDirectoryScoped({
137 prefix: "t3-repository-identity-instead-of-test-",
138 });
139
140 yield* git(cwd, ["init"]);
141 yield* git(cwd, ["remote", "add", remoteName, "https://github.com/T3Tools/t3code.git"]);
142 yield* git(cwd, ["config", "url.https://proxy.example/.insteadOf", "https://github.com/"]);
143 yield* git(cwd, ["config", `remote.${remoteName}.gh-resolved`, "base"]);
144 const remotes = yield* git(cwd, ["remote", "-v"]);
145 expect(remotes.stdout).toContain("https://proxy.example/T3Tools/t3code.git (fetch)");
146
147 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
148 const identity = yield* resolver.resolve(cwd);
149
150 expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
151 expect(identity?.provider).toBe("github");
152 expect(identity?.displayName).toBe("T3Tools/t3code");
153 expect(identity?.locator).toEqual({
154 source: "git-remote",
155 remoteName,
156 remoteUrl: "https://github.com/T3Tools/t3code.git",
157 });
158
159 const shorthandCwd = yield* fileSystem.makeTempDirectoryScoped({
160 prefix: "t3-repository-identity-instead-of-shorthand-test-",
161 });
162 yield* git(shorthandCwd, ["init"]);
163 for (const [prefix, target, provider, canonicalKey, keepConfigured] of [
164 ["gh:", "https://github.com/", "github", "github.com/t3tools/t3code", false],
165 ["github:", "https://github.com/", "github", "github.com/t3tools/t3code", false],
166 ["https://gitlab/", "https://github.com/", "gitlab", "gitlab/t3tools/t3code", true],
167 ] as const) {
168 yield* git(shorthandCwd, ["config", `url.${target}.insteadOf`, prefix]);
169 yield* git(shorthandCwd, [
170 "remote",
171 prefix === "gh:" ? "add" : "set-url",
172 remoteName,
173 `${prefix}T3Tools/t3code.git`,
174 ]);
175 const rewritten = yield* git(shorthandCwd, ["remote", "-v"]);
176 expect(rewritten.stdout).toContain(`${target}T3Tools/t3code.git (fetch)`);
177
178 const shorthandIdentity = yield* resolver.resolve(shorthandCwd, { refresh: true });
179
180 expect(shorthandIdentity?.canonicalKey).toBe(canonicalKey);
181 expect(shorthandIdentity?.provider).toBe(provider);
182 expect(shorthandIdentity?.locator.remoteUrl).toBe(
183 `${keepConfigured ? prefix : target}T3Tools/t3code.git`,
184 );
185 }
186 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
187 );
188
189 it.effect("keeps a literal .git in an Azure DevOps repository name", () =>
190 Effect.gen(function* () {
191 const fileSystem = yield* FileSystem.FileSystem;
192 const cwd = yield* fileSystem.makeTempDirectoryScoped({
193 prefix: "t3-repository-identity-azure-git-suffix-test-",
194 });
195
196 yield* git(cwd, ["init"]);
197 yield* git(cwd, [
198 "remote",
199 "add",
200 "origin",
201 "https://dev.azure.com/Acme/Platform/_git/Tool.git",
202 ]);
203
204 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
205 const identity = yield* resolver.resolve(cwd);
206
207 expect(identity?.displayName).toBe("Acme/Platform/_git/Tool.git");
208 expect(identity?.name).toBe("Tool.git");
209 expect(sourceControlDefinitions.repositorySelector(identity)).toBe("Tool.git");
210 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
211 );
212
213 it.effect("resolves the fetch remote of a partial clone", () =>
214 Effect.gen(function* () {
215 const fileSystem = yield* FileSystem.FileSystem;
216 const cwd = yield* fileSystem.makeTempDirectoryScoped({
217 prefix: "t3-repository-identity-partial-clone-test-",
218 });
219
220 yield* git(cwd, ["init"]);
221 yield* git(cwd, ["remote", "add", "origin", "git@github.com:T3Tools/t3code.git"]);
222 yield* git(cwd, ["remote", "set-url", "--push", "origin", "git@github.com:T3Tools/push.git"]);
223 yield* git(cwd, ["config", "remote.origin.promisor", "true"]);
224 yield* git(cwd, ["config", "remote.origin.partialclonefilter", "blob:none"]);
225
226 const remotes = yield* git(cwd, ["remote", "-v"]);
227 expect(remotes.stdout).toContain("(fetch) [blob:none]");
228
229 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
230 const identity = yield* resolver.resolve(cwd);
231 expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
232 expect(identity?.locator.remoteUrl).toBe("git@github.com:T3Tools/t3code.git");
233 expect(identity?.locator.remoteName).toBe("origin");
234 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
235 );
236
237 it.effect("resolves a remote whose URL contains a space", () =>
238 Effect.gen(function* () {
239 const fileSystem = yield* FileSystem.FileSystem;
240 const cwd = yield* fileSystem.makeTempDirectoryScoped({
241 prefix: "t3-repository-identity-spaced-url-test-",
242 });
243
244 yield* git(cwd, ["init"]);
245 yield* git(cwd, ["remote", "add", "origin", "ssh://git@git.example.test/team repos/app.git"]);
246
247 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
248 const identity = yield* resolver.resolve(cwd);
249 expect(identity?.locator.remoteUrl).toBe("ssh://git@git.example.test/team repos/app.git");
250 expect(identity?.locator.remoteName).toBe("origin");
251 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
252 );
253
254 it.effect("keeps a login embedded in a remote URL out of the identity", () =>
255 Effect.gen(function* () {
256 const fileSystem = yield* FileSystem.FileSystem;
257 const cwd = yield* fileSystem.makeTempDirectoryScoped({
258 prefix: "t3-repository-identity-credentials-test-",
259 });
260
261 yield* git(cwd, ["init"]);
262 yield* git(cwd, [
263 "remote",
264 "add",
265 "origin",
266 "https://me:origin-secret@github.com/me/t3code.git",
267 ]);
268 yield* git(cwd, [
269 "remote",
270 "add",
271 "upstream",
272 "https://x-access-token:upstream-secret@github.com/T3Tools/t3code.git?token=query-secret",
273 ]);
274
275 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
276 const identity = yield* resolver.resolve(cwd);
277 expect(identity?.locator.remoteUrl).toBe("https://github.com/T3Tools/t3code.git");
278 expect(identity?.origin?.remoteUrl).toBe("https://github.com/me/t3code.git");
279 expect(JSON.stringify(identity)).not.toContain("secret");
280 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
281 );
282
283 it.effect.each(["repo", "repo ", ...lineBreakSuffixes.map((suffix) => `repo${suffix}`)])(
284 "returns the git top-level root path when resolving from a nested workspace in %j",
285 (rootName) =>
286 Effect.gen(function* () {
287 const fileSystem = yield* FileSystem.FileSystem;
288 const path = yield* Path.Path;
289 const tempDir = yield* fileSystem.makeTempDirectoryScoped({
290 prefix: "t3-repository-identity-nested-root-test-",
291 });
292 const repoRoot = path.join(tempDir, rootName);
293 const nestedWorkspace = path.join(repoRoot, "packages", "web");
294
295 yield* fileSystem.makeDirectory(nestedWorkspace, { recursive: true });
296 yield* git(repoRoot, ["init"]);
297 yield* git(repoRoot, ["remote", "add", "origin", "git@github.com:T3Tools/t3code.git"]);
298
299 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
300 const identity = yield* resolver.resolve(nestedWorkspace);
301 const resolvedIdentityRoot =
302 identity?.rootPath === undefined ? "" : NodeFS.realpathSync.native(identity.rootPath);
303 const resolvedRepoRoot = NodeFS.realpathSync.native(repoRoot);
304
305 expect(identity).not.toBeNull();
306 expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
307 expect(normalizeResolvedPath(resolvedIdentityRoot)).toBe(
308 normalizeResolvedPath(resolvedRepoRoot),
309 );
310 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
311 );
312
313 it.effect.each(bareLayouts)(
314 "resolves a %j to its Git directory and the same identity as a checkout",
315 (layout) =>
316 Effect.gen(function* () {
317 const fileSystem = yield* FileSystem.FileSystem;
318 const path = yield* Path.Path;
319 const checkout = yield* fileSystem.makeTempDirectoryScoped({
320 prefix: "t3-repository-identity-checkout-",
321 });
322 const parent = yield* fileSystem.makeTempDirectoryScoped({
323 prefix: "t3-repository-identity-bare-",
324 });
325 const root = path.join(parent, layout);
326 yield* fileSystem.makeDirectory(root);
327 const bareGitDir = layout === "dot-bare" ? path.join(root, ".bare") : root;
328
329 yield* git(checkout, ["init"]);
330 yield* git(checkout, ["remote", "add", "origin", "git@github.com:T3Tools/t3code.git"]);
331 yield* git(root, ["init", "--bare", bareGitDir]);
332 if (layout === "dot-bare") {
333 yield* fileSystem.writeFileString(path.join(root, ".git"), "gitdir: ./.bare\n");
334 }
335 yield* git(root, [
336 "--git-dir",
337 bareGitDir,
338 "remote",
339 "add",
340 "origin",
341 "https://github.com/T3Tools/t3code.git",
342 ]);
343
344 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
345 const checkoutIdentity = yield* resolver.resolve(checkout);
346 expect(checkoutIdentity?.canonicalKey).toBe("github.com/t3tools/t3code");
347 const resolvedBareGitDir = NodeFS.realpathSync.native(bareGitDir);
348 const projectRoots = layout === "dot-bare" ? [root, bareGitDir] : [root];
349
350 for (const projectRoot of projectRoots) {
351 const identity = yield* resolver.resolve(projectRoot);
352 const resolvedIdentityRoot =
353 identity?.rootPath === undefined ? "" : NodeFS.realpathSync.native(identity.rootPath);
354
355 expect(identity).not.toBeNull();
356 expect(identity?.canonicalKey).toBe(checkoutIdentity?.canonicalKey);
357 expect(normalizeResolvedPath(resolvedIdentityRoot)).toBe(
358 normalizeResolvedPath(resolvedBareGitDir),
359 );
360 }
361
362 yield* git(root, ["--git-dir", bareGitDir, "remote", "remove", "origin"]);
363 for (const projectRoot of projectRoots) {
364 expect(yield* resolver.resolve(projectRoot, { refresh: true })).toBeNull();
365 }
366 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
367 );
368
369 it.effect(
370 "returns null for non-git, missing and no-remote folders, probing only what can exist",
371 () =>
372 Effect.gen(function* () {
373 const fileSystem = yield* FileSystem.FileSystem;
374 const path = yield* Path.Path;
375 const nonGitDir = yield* fileSystem.makeTempDirectoryScoped({
376 prefix: "t3-repository-identity-non-git-",
377 });
378 const gitDir = yield* fileSystem.makeTempDirectoryScoped({
379 prefix: "t3-repository-identity-no-remote-",
380 });
381
382 yield* git(gitDir, ["init"]);
383
384 const runner = yield* ProcessRunner.ProcessRunner;
385 const runs: Array<ReadonlyArray<string>> = [];
386 const resolver = yield* RepositoryIdentityResolver.make().pipe(
387 Effect.provideService(ProcessRunner.ProcessRunner, {
388 run: (input) =>
389 Effect.suspend(() => {
390 runs.push(input.args);
391 return runner.run(input);
392 }),
393 }),
394 );
395 const bareProbes = () =>
396 runs.filter((args) => args.some((arg) => arg.startsWith("--git-dir=")));
397
398 expect(yield* resolver.resolve(path.join(nonGitDir, "deleted"))).toBeNull();
399 expect(runs).toEqual([]);
400
401 expect(yield* resolver.resolve(nonGitDir)).toBeNull();
402 expect(bareProbes()).toEqual([]);
403
404 expect(yield* resolver.resolve(gitDir)).toBeNull();
405 }).pipe(Effect.provide(ProcessRunner.layer)),
406 );
407
408 it.effect("ignores local-path remotes such as GitButler's gb-local", () =>
409 Effect.gen(function* () {
410 const fileSystem = yield* FileSystem.FileSystem;
411 const localOnlyDir = yield* fileSystem.makeTempDirectoryScoped({
412 prefix: "t3-repository-identity-local-remote-",
413 });
414 const mixedDir = yield* fileSystem.makeTempDirectoryScoped({
415 prefix: "t3-repository-identity-mixed-remote-",
416 });
417
418 yield* git(localOnlyDir, ["init"]);
419 yield* git(localOnlyDir, ["remote", "add", "gb-local", "."]);
420 yield* git(localOnlyDir, ["remote", "add", "backup", "/srv/git/repo.git"]);
421 yield* git(localOnlyDir, ["remote", "add", "archive", "file:///srv/git/repo.git"]);
422 yield* git(mixedDir, ["init"]);
423 yield* git(mixedDir, ["remote", "add", "gb-local", "."]);
424 yield* git(mixedDir, ["remote", "add", "mirror", "git@github.com:T3Tools/t3code.git"]);
425
426 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
427 expect(yield* resolver.resolve(localOnlyDir)).toBeNull();
428
429 const mixedIdentity = yield* resolver.resolve(mixedDir);
430 expect(mixedIdentity?.locator.remoteName).toBe("mirror");
431 expect(mixedIdentity?.canonicalKey).toBe("github.com/t3tools/t3code");
432 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
433 );
434
435 it.effect.each(["add", "replace"] as const)(
436 "refreshes the primary upstream after %s before cache expiry",
437 (change) =>
438 Effect.gen(function* () {
439 const fileSystem = yield* FileSystem.FileSystem;
440 const cwd = yield* fileSystem.makeTempDirectoryScoped({
441 prefix: "t3-repository-identity-upstream-test-",
442 });
443
444 yield* git(cwd, ["init"]);
445 yield* git(cwd, ["remote", "add", "origin", "git@github.com:julius/t3code.git"]);
446 if (change === "replace") {
447 yield* git(cwd, ["remote", "add", "upstream", "git@github.com:T3Tools/previous.git"]);
448 }
449
450 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
451 const initialIdentity = yield* resolver.resolve(cwd);
452 expect(initialIdentity?.canonicalKey).toBe(
453 change === "add" ? "github.com/julius/t3code" : "github.com/t3tools/previous",
454 );
455
456 yield* git(cwd, [
457 "remote",
458 change === "add" ? "add" : "set-url",
459 "upstream",
460 "git@github.com:T3Tools/t3code.git",
461 ]);
462 expect(yield* resolver.resolve(cwd)).toEqual(initialIdentity);
463 const identity = yield* resolver.resolve(cwd, { refresh: true });
464
465 expect(identity).not.toBeNull();
466 expect(identity?.locator.remoteName).toBe("upstream");
467 expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
468 expect(identity?.displayName).toBe("T3Tools/t3code");
469 expect(yield* resolver.resolve(cwd)).toEqual(identity);
470 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
471 );
472
473 it.effect("reports a fork's own remote as origin next to the upstream identity", () =>
474 Effect.gen(function* () {
475 const fileSystem = yield* FileSystem.FileSystem;
476 const cwd = yield* fileSystem.makeTempDirectoryScoped({
477 prefix: "t3-repository-identity-fork-test-",
478 });
479
480 yield* git(cwd, ["init"]);
481 yield* git(cwd, ["remote", "add", "origin", "git@github.com:julius/t3code-fork.git"]);
482 yield* git(cwd, ["remote", "add", "upstream", "git@github.com:T3Tools/t3code.git"]);
483
484 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
485 const identity = yield* resolver.resolve(cwd);
486
487 expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
488 expect(identity?.displayName).toBe("T3Tools/t3code");
489 expect(identity?.origin).toEqual({
490 canonicalKey: "github.com/julius/t3code-fork",
491 remoteUrl: "git@github.com:julius/t3code-fork.git",
492 displayName: "julius/t3code-fork",
493 });
494 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
495 );
496
497 it.effect("uses the fork as the identity when gh repo set-default points at it", () =>
498 Effect.gen(function* () {
499 const fileSystem = yield* FileSystem.FileSystem;
500 const cwd = yield* fileSystem.makeTempDirectoryScoped({
501 prefix: "t3-repository-identity-gh-default-test-",
502 });
503
504 yield* git(cwd, ["init"]);
505 yield* git(cwd, ["remote", "add", "origin", "git@github.com:julius/t3code-fork.git"]);
506 yield* git(cwd, ["remote", "add", "upstream", "git@github.com:T3Tools/t3code.git"]);
507 yield* git(cwd, ["config", "remote.origin.gh-resolved", "base"]);
508
509 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
510 const identity = yield* resolver.resolve(cwd);
511
512 expect(identity?.canonicalKey).toBe("github.com/julius/t3code-fork");
513 expect(identity?.locator.remoteName).toBe("origin");
514 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
515 );
516
517 it.effect("follows an OWNER/REPO mark only to a remote that fetches that repository", () =>
518 Effect.gen(function* () {
519 const fileSystem = yield* FileSystem.FileSystem;
520 const cwd = yield* fileSystem.makeTempDirectoryScoped({
521 prefix: "t3-repository-identity-gh-parent-test-",
522 });
523 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
524 const resolveFresh = () => resolver.resolve(cwd, { refresh: true });
525
526 yield* git(cwd, ["init"]);
527 yield* git(cwd, ["remote", "add", "fork", "https://github.com/julius/t3code-fork.git"]);
528 yield* git(cwd, ["remote", "add", "main", "https://github.com/T3Tools/t3code.git"]);
529 yield* git(cwd, ["remote", "add", "upstream", "https://github.com/other/t3code.git"]);
530 yield* git(cwd, ["config", "remote.fork.gh-resolved", "t3tools/t3code"]);
531 const parent = yield* resolveFresh();
532 expect(parent?.canonicalKey).toBe("github.com/t3tools/t3code");
533 expect(parent?.locator).toEqual({
534 source: "git-remote",
535 remoteName: "main",
536 remoteUrl: "https://github.com/T3Tools/t3code.git",
537 });
538
539 // No remote fetches the marked repository, and several marks leave gh's choice unknown.
540 yield* git(cwd, ["config", "remote.fork.gh-resolved", "someone/else"]);
541 expect((yield* resolveFresh())?.locator.remoteName).toBe("upstream");
542 yield* git(cwd, ["config", "remote.fork.gh-resolved", "base"]);
543 yield* git(cwd, ["config", "remote.main.gh-resolved", "base"]);
544 expect((yield* resolveFresh())?.locator.remoteName).toBe("upstream");
545
546 // The same path on another HTTPS port is a different server.
547 yield* git(cwd, ["config", "--unset", "remote.fork.gh-resolved"]);
548 yield* git(cwd, ["config", "--unset", "remote.main.gh-resolved"]);
549 yield* git(cwd, ["remote", "add", "backup", "https://ghe.example.com:9443/acme/web.git"]);
550 yield* git(cwd, ["remote", "add", "ghe-fork", "https://ghe.example.com:8443/me/web.git"]);
551 yield* git(cwd, ["remote", "add", "ghe-parent", "https://ghe.example.com:8443/acme/web.git"]);
552 yield* git(cwd, ["config", "remote.ghe-fork.gh-resolved", "acme/web"]);
553 expect((yield* resolveFresh())?.locator.remoteName).toBe("ghe-parent");
554 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
555 );
556
557 it.effect("reads the project's gh default when the server inherited GIT_DIR", () =>
558 Effect.gen(function* () {
559 const fileSystem = yield* FileSystem.FileSystem;
560 const path = yield* Path.Path;
561 const cwd = yield* fileSystem.makeTempDirectoryScoped({
562 prefix: "t3-repository-identity-gh-default-git-dir-",
563 });
564 const other = yield* fileSystem.makeTempDirectoryScoped({
565 prefix: "t3-repository-identity-inherited-git-dir-",
566 });
567
568 yield* git(cwd, ["init"]);
569 yield* git(cwd, ["remote", "add", "origin", "git@github.com:julius/t3code-fork.git"]);
570 yield* git(cwd, ["remote", "add", "upstream", "git@github.com:T3Tools/t3code.git"]);
571 yield* git(cwd, ["config", "remote.origin.gh-resolved", "base"]);
572 yield* git(other, ["init"]);
573 yield* git(other, ["config", "remote.upstream.gh-resolved", "base"]);
574
575 vi.stubEnv("GIT_DIR", path.join(other, ".git"));
576 yield* Effect.addFinalizer(() => Effect.sync(() => vi.unstubAllEnvs()));
577
578 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
579 const identity = yield* resolver.resolve(cwd);
580
581 expect(identity?.canonicalKey).toBe("github.com/julius/t3code-fork");
582 expect(identity?.locator.remoteName).toBe("origin");
583 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
584 );
585
586 it.effect("uses the last remote path segment as the repository name for nested groups", () =>
587 Effect.gen(function* () {
588 const fileSystem = yield* FileSystem.FileSystem;
589 const cwd = yield* fileSystem.makeTempDirectoryScoped({
590 prefix: "t3-repository-identity-nested-group-test-",
591 });
592
593 yield* git(cwd, ["init"]);
594 yield* git(cwd, ["remote", "add", "origin", "git@gitlab.com:T3Tools/platform/t3code.git"]);
595
596 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
597 const identity = yield* resolver.resolve(cwd);
598
599 expect(identity).not.toBeNull();
600 expect(identity?.canonicalKey).toBe("gitlab.com/t3tools/platform/t3code");
601 expect(identity?.displayName).toBe("T3Tools/platform/t3code");
602 expect(identity?.owner).toBe("T3Tools");
603 expect(identity?.name).toBe("t3code");
604 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
605 );
606});