Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

worktreesDirectory.ts

107 lines4.0 KB

1import * as HostProcess from "@t3tools/shared/HostProcess";
2import * as Effect from "effect/Effect";
3import * as FileSystem from "effect/FileSystem";
4import * as Path from "effect/Path";
5​
6import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion";
7​
8import * as ServerConfig from "./config.ts";
9import * as ServerSettings from "./serverSettings.ts";
10​
11/**
12 * Null when the setting is not an absolute path on this machine, such as `D:\worktrees`
13 * configured for a Windows server and synced to a Linux one, or when it is a filesystem root,
14 * which would make every path on that drive look managed.
15 */
16export function resolveWorktreesDirectory(
17 setting: string,
18 defaultDir: string,
19 path: Path.Path,
20 home: string,
21): string | null {
22 if (setting === "") return defaultDir;
23 const expanded = expandHomePath(setting, home);
24 if (!path.isAbsolute(expanded)) return null;
25 const resolved = path.resolve(expanded);
26 return isFilesystemRoot(resolved, path) ? null : resolved;
28​
29/** Callers re-check after resolving symlinks: a link can point at a root. */
30export function isFilesystemRoot(directory: string, path: Path.Path): boolean {
31 return path.dirname(directory) === directory;
33​
34export function managedWorktreesDirectories(
35 settings: {
36 readonly worktreesDirectory: string;
37 readonly previousWorktreesDirectories: ReadonlyArray<string>;
38 },
39 defaultDir: string,
40 path: Path.Path,
41 home: string,
42): ReadonlyArray<string> {
43 const directories = new Set([defaultDir]);
44 for (const setting of [settings.worktreesDirectory, ...settings.previousWorktreesDirectories]) {
45 const directory = resolveWorktreesDirectory(setting, defaultDir, path, home);
46 if (directory !== null) directories.add(directory);
47 }
48 return [...directories];
50​
51/**
52 * A thread's path and branch can come from a client, so a missing worktree is
53 * only recreated where the server would have put it:
54 * `<managed worktrees folder>/<repo>/<branch>`, compared by real path.
55 */
56export const makeIsManagedWorktreePath = Effect.gen(function* () {
57 const fileSystem = yield* FileSystem.FileSystem;
58 const path = yield* Path.Path;
59 const serverConfig = yield* ServerConfig.ServerConfig;
60 const serverSettings = yield* ServerSettings.ServerSettingsService;
61​
62 // Where git would create `target`: the real path of its nearest existing
63 // ancestor plus the missing rest, since git follows symlinks on the way.
64 const realLocation = (target: string): Effect.Effect<string> =>
65 fileSystem.realPath(target).pipe(
66 Effect.catch(() => {
67 const parent = path.dirname(target);
68 return parent === target
69 ? Effect.succeed(target)
70 : Effect.map(realLocation(parent), (real) => path.join(real, path.basename(target)));
71 }),
72 );
73​
74 return Effect.fn("worktreesDirectory.isManagedWorktreePath")(function* (worktreePath: string) {
75 // git resolves a relative path against the project, not the server cwd.
76 if (!path.isAbsolute(worktreePath)) return false;
77 // Server-made paths are normalized. A `..` would be applied after git
78 // follows a symlink but before realPath does, so the two disagree.
79 if (path.resolve(worktreePath) !== worktreePath) return false;
80 const settings = yield* serverSettings.getSettings.pipe(
81 Effect.orElseSucceed(() => ({
82 worktreesDirectory: "",
83 previousWorktreesDirectories: [],
84 })),
85 );
86 const roots = yield* Effect.forEach(
87 managedWorktreesDirectories(
88 settings,
89 serverConfig.worktreesDir,
90 path,
91 yield* HostProcess.HomeDirectory,
92 ),
93 realLocation,
94 );
95 const candidate = yield* realLocation(worktreePath);
96 return roots.some((root) => {
97 if (isFilesystemRoot(root, path)) return false;
98 const relative = path.relative(root, candidate);
99 const segments = relative.split(path.sep);
100 return (
101 !path.isAbsolute(relative) &&
102 segments.length === 2 &&
103 segments.every((segment) => segment !== "" && segment !== "..")
104 );
105 });
106 });
107});