-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepo-paths.ts
More file actions
32 lines (28 loc) · 928 Bytes
/
repo-paths.ts
File metadata and controls
32 lines (28 loc) · 928 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { realpathSync } from "node:fs";
import { isAbsolute, relative, resolve } from "node:path";
function realPathOrSelf(p: string): string {
try {
return realpathSync(p);
} catch {
return p;
}
}
export function isStrictlyUnderGitTop(absPath: string, gitTop: string): boolean {
const absR = realPathOrSelf(resolve(absPath));
const topR = realPathOrSelf(resolve(gitTop));
const rel = relative(topR, absR);
if (rel === "") return true;
return !rel.startsWith("..") && !isAbsolute(rel);
}
export function resolvePathForRepo(p: string, gitTop: string): string {
const t = p.trim();
return isAbsolute(t) ? resolve(t) : resolve(gitTop, t);
}
/** Resolved path must lie inside git toplevel (relative or absolute user input). */
export function assertRelativePathUnderTop(
_relPath: string,
absResolved: string,
gitTop: string,
): boolean {
return isStrictlyUnderGitTop(absResolved, gitTop);
}