Open History to see the last commit
1import { decodeGitCafePath } from "../shared/definition.ts";
2
3export const GITCAFE_HOSTS = ["git.cafe", "staging.git.cafe"] as const;
4export type GitCafeHost = (typeof GITCAFE_HOSTS)[number];
5
6export function gitCafeHost(host: string): GitCafeHost | null {
7 const normalized = host.trim().toLowerCase();
8 return GITCAFE_HOSTS.find((candidate) => candidate === normalized) ?? null;
9}
10
11export function isGitCafeHost(host: string): boolean {
12 return gitCafeHost(host) !== null;
13}
14
15const REPOSITORY = /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/u;
16const REMOTE_PATTERNS = [
17 /^https?:\/\/(?:[^@/]+@)?([^/:]+)(?::\d+)?\/(.+)$/iu,
18 /^ssh:\/\/(?:[^@/]+@)?([^/:]+)(?::\d+)?\/(.+)$/iu,
19 /^(?:[^@/:]+@)?([^/:]+):\/?(.+)$/u,
20];
21
22export function gitCafeRepositoryPath(path: string): string | null {
23 const repository = path
24 .trim()
25 .replace(/\/+$/u, "")
26 .replace(/\.git$/u, "");
27 return REPOSITORY.test(repository) ? repository : null;
28}
29
30export function parseGitCafeRemote(
31 url: string,
32): { readonly host: GitCafeHost; readonly repository: string } | null {
33 for (const [index, pattern] of REMOTE_PATTERNS.entries()) {
34 const match = pattern.exec(url.trim());
35 if (!match?.[1] || !match[2]) continue;
36 const host = gitCafeHost(match[1]);
37 // Only the HTTP(S) pattern is a URL path; SSH and SCP paths reach the server literally.
38 const path = index === 0 ? decodeGitCafePath(match[2]) : match[2];
39 const repository = path === null ? null : gitCafeRepositoryPath(path);
40 return host && repository ? { host, repository } : null;
41 }
42 return null;
43}