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