186 lines
5.8 KiB
TypeScript
186 lines
5.8 KiB
TypeScript
const ALLOWED_PROTOCOLS = new Set(["https:", "http:", "ssh:", "git:"]);
|
|
const SCP_PATTERN = /^([A-Za-z0-9._-]+@)?([A-Za-z0-9][A-Za-z0-9.-]*):([^\s]+)$/;
|
|
const CONTROL_OR_SPACE = /[\s\0-\x1f\x7f]/u;
|
|
|
|
export type ParseGitUrlResult =
|
|
| {
|
|
ok: true;
|
|
url: string;
|
|
host: string;
|
|
repoPath: string;
|
|
repoName: string;
|
|
}
|
|
| {
|
|
ok: false;
|
|
error: string;
|
|
};
|
|
|
|
export type ValidationResult =
|
|
| {
|
|
ok: true;
|
|
value: string;
|
|
}
|
|
| {
|
|
ok: false;
|
|
error: string;
|
|
};
|
|
|
|
/** Validate a single directory name used for a cloned repository folder. */
|
|
export function validateDirectoryName(name: string): ValidationResult {
|
|
const trimmed = name.trim();
|
|
if (!trimmed) {
|
|
return { ok: false, error: "Folder name cannot be empty" };
|
|
}
|
|
if (trimmed === "." || trimmed === "..") {
|
|
return { ok: false, error: "Folder name cannot be '.' or '..'" };
|
|
}
|
|
if (trimmed.startsWith("-")) {
|
|
return { ok: false, error: "Folder name cannot start with '-'" };
|
|
}
|
|
if (/[/\\\0-\x1f\x7f]/u.test(trimmed)) {
|
|
return { ok: false, error: "Folder name contains invalid characters" };
|
|
}
|
|
return { ok: true, value: trimmed };
|
|
}
|
|
|
|
/** Validate an optional branch or tag ref passed to `git clone --branch`. */
|
|
export function validateBranchName(branch: string): ValidationResult {
|
|
const trimmed = branch.trim();
|
|
if (!trimmed) {
|
|
return { ok: false, error: "Branch name cannot be empty" };
|
|
}
|
|
if (trimmed.startsWith("-")) {
|
|
return { ok: false, error: "Branch name cannot start with '-'" };
|
|
}
|
|
if (CONTROL_OR_SPACE.test(trimmed)) {
|
|
return { ok: false, error: "Branch name cannot contain whitespace or control characters" };
|
|
}
|
|
if (
|
|
trimmed.includes("..") ||
|
|
trimmed.includes("@{") ||
|
|
trimmed.includes("//") ||
|
|
/[\\~^:?*[]/.test(trimmed) ||
|
|
trimmed.startsWith("/") ||
|
|
trimmed.endsWith("/") ||
|
|
trimmed.endsWith(".") ||
|
|
trimmed.endsWith(".lock")
|
|
) {
|
|
return { ok: false, error: "Branch name contains invalid Git ref characters" };
|
|
}
|
|
return { ok: true, value: trimmed };
|
|
}
|
|
|
|
function derivePathAndName(rawPath: string): { repoPath: string; repoName: string } | { error: string } {
|
|
const segments = rawPath.split("/").filter(Boolean);
|
|
if (segments.length === 0) {
|
|
return { error: "Git URL must include a repository path" };
|
|
}
|
|
const lastRaw = segments[segments.length - 1] ?? "";
|
|
const repoName = lastRaw.replace(/\.git$/iu, "");
|
|
const nameCheck = validateDirectoryName(repoName);
|
|
if (!nameCheck.ok) {
|
|
return { error: `Invalid repository name in URL: ${nameCheck.error}` };
|
|
}
|
|
const normalizedSegments = [...segments.slice(0, -1), nameCheck.value];
|
|
return {
|
|
repoPath: normalizedSegments.join("/"),
|
|
repoName: nameCheck.value,
|
|
};
|
|
}
|
|
|
|
/** Parse and validate an external Git clone URL (`https://`, `http://`, `ssh://`, `git://`, or `user@host:path`). */
|
|
export function parseGitUrl(raw: string): ParseGitUrlResult {
|
|
const trimmed = raw.trim();
|
|
if (!trimmed) {
|
|
return { ok: false, error: "Enter a Git repository URL" };
|
|
}
|
|
if (CONTROL_OR_SPACE.test(trimmed)) {
|
|
return { ok: false, error: "Git URL cannot contain whitespace or control characters" };
|
|
}
|
|
if (trimmed.startsWith("-")) {
|
|
return { ok: false, error: "Git URL cannot start with '-'" };
|
|
}
|
|
if (/^ext::/iu.test(trimmed)) {
|
|
return { ok: false, error: "ext:: Git URLs are not supported" };
|
|
}
|
|
|
|
if (trimmed.includes("://")) {
|
|
let parsed: URL;
|
|
try {
|
|
parsed = new URL(trimmed);
|
|
} catch {
|
|
return { ok: false, error: "Invalid Git URL" };
|
|
}
|
|
const protocol = parsed.protocol.toLowerCase();
|
|
if (!ALLOWED_PROTOCOLS.has(protocol)) {
|
|
return {
|
|
ok: false,
|
|
error: `Unsupported Git URL protocol '${protocol.replace(/:$/u, "")}'. Use https://, http://, ssh://, git://, or user@host:path.`,
|
|
};
|
|
}
|
|
const host = parsed.hostname.toLowerCase();
|
|
if (!host || host.startsWith("-")) {
|
|
return { ok: false, error: "Git URL must include a valid host" };
|
|
}
|
|
let decodedPath: string;
|
|
try {
|
|
decodedPath = decodeURIComponent(parsed.pathname);
|
|
} catch {
|
|
return { ok: false, error: "Git URL contains invalid percent-encoding" };
|
|
}
|
|
const derived = derivePathAndName(decodedPath);
|
|
if ("error" in derived) {
|
|
return { ok: false, error: derived.error };
|
|
}
|
|
return {
|
|
ok: true,
|
|
url: trimmed,
|
|
host,
|
|
repoPath: derived.repoPath,
|
|
repoName: derived.repoName,
|
|
};
|
|
}
|
|
|
|
const scpMatch = trimmed.match(SCP_PATTERN);
|
|
if (scpMatch) {
|
|
const host = (scpMatch[2] ?? "").toLowerCase();
|
|
const rawRepoPath = scpMatch[3] ?? "";
|
|
if (!host || host.startsWith("-") || rawRepoPath.startsWith("//")) {
|
|
return { ok: false, error: "Invalid SSH Git URL" };
|
|
}
|
|
const derived = derivePathAndName(rawRepoPath);
|
|
if ("error" in derived) {
|
|
return { ok: false, error: derived.error };
|
|
}
|
|
return {
|
|
ok: true,
|
|
url: trimmed,
|
|
host,
|
|
repoPath: derived.repoPath,
|
|
repoName: derived.repoName,
|
|
};
|
|
}
|
|
|
|
return {
|
|
ok: false,
|
|
error: "Enter a full Git URL (https://, http://, ssh://, git://, or user@host:owner/repo.git)",
|
|
};
|
|
}
|
|
|
|
/** Normalize a Git remote URL across HTTPS/HTTP/SSH/SCP forms for origin comparison. */
|
|
export function normalizeRemoteUrl(raw: string): string {
|
|
const parsed = parseGitUrl(raw);
|
|
if (parsed.ok) {
|
|
return `${parsed.host}/${parsed.repoPath}`.toLowerCase();
|
|
}
|
|
return raw.trim().replace(/\/+$/u, "").replace(/\.git$/iu, "").toLowerCase();
|
|
}
|
|
|
|
/** Preview the full destination checkout path for a parent directory and folder name. */
|
|
export function destinationPreview(parent: string, folderName: string): string {
|
|
const trimmed = parent.trim().replace(/[\\/]+$/u, "");
|
|
if (!trimmed) return folderName;
|
|
const separator = trimmed.includes("\\") && !trimmed.includes("/") ? "\\" : "/";
|
|
return `${trimmed}${separator}${folderName}`;
|
|
}
|