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