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

worktreesDirectory.ts

44 lines1.7 KB

1import type * as Path from "effect/Path";
2​
3import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion";
4​
5/**
6 * Directory new worktrees are created under: the `worktreesDirectory`
7 * setting, or `defaultDir` (`<T3 home>/worktrees`) when it is empty. Null when
8 * the setting is not an absolute path on this machine, such as `D:\worktrees`
9 * configured for a Windows server and synced to a Linux one, or when it is a
10 * filesystem root, which would make every path on that drive look managed.
11 */
12export function resolveWorktreesDirectory(
13 setting: string,
14 defaultDir: string,
15 path: Path.Path,
16): string | null {
17 if (setting === "") return defaultDir;
18 const expanded = expandHomePath(setting);
19 if (!path.isAbsolute(expanded)) return null;
20 const resolved = path.resolve(expanded);
21 return isFilesystemRoot(resolved, path) ? null : resolved;
23​
24/** Callers re-check after resolving symlinks: a link can point at a root. */
25export function isFilesystemRoot(directory: string, path: Path.Path): boolean {
26 return path.dirname(directory) === directory;
28​
29/** Every directory that holds T3-managed worktrees on this machine. */
30export function managedWorktreesDirectories(
31 settings: {
32 readonly worktreesDirectory: string;
33 readonly previousWorktreesDirectories: ReadonlyArray<string>;
34 },
35 defaultDir: string,
36 path: Path.Path,
37): ReadonlyArray<string> {
38 const directories = new Set([defaultDir]);
39 for (const setting of [settings.worktreesDirectory, ...settings.previousWorktreesDirectories]) {
40 const directory = resolveWorktreesDirectory(setting, defaultDir, path);
41 if (directory !== null) directories.add(directory);
42 }
43 return [...directories];