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

worktreesDirectory.ts

43 lines1.5 KB

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