Files
skillshare/ui/src/lib/parseRemoteURL.ts
T
Willie d842d7a871 feat(ui): add Repository Info Card to Git Sync page
Replace the minimal Repo Status Card with a full Repository Info Card
showing remote URL with platform detection (GitHub/GitLab/Bitbucket),
branch tracking info, HEAD commit hash and message, and status badges.

Backend: export GetRemoteURL, add GetHeadMessage and GetTrackingBranch
to internal/git, extend /api/git/status with 4 new fields.

Frontend: new parseRemoteURL utility parses SSH/HTTPS remote URLs and
detects platform by exact hostname match. RepoInfoCard displays parsed
owner/repo with platform icon, clickable link, copy button, and
graceful fallbacks for missing remote or unparseable URLs.
2026-03-14 04:01:10 +08:00

79 lines
1.8 KiB
TypeScript

export type Platform = 'github' | 'gitlab' | 'bitbucket' | 'other';
export interface ParsedRemoteURL {
owner: string;
repo: string;
ownerRepo: string;
hostname: string;
platform: Platform;
webURL: string | null;
}
const PLATFORM_MAP: Record<string, Platform> = {
'github.com': 'github',
'gitlab.com': 'gitlab',
'bitbucket.org': 'bitbucket',
};
/**
* Parse a git remote URL into structured components.
* Supports SSH (git@host:owner/repo.git, ssh://git@host/owner/repo.git)
* and HTTPS (https://host/owner/repo.git) formats.
*/
export function parseRemoteURL(url: string | undefined | null): ParsedRemoteURL | null {
if (!url) return null;
let hostname = '';
let path = '';
// ssh://git@host/owner/repo.git
const sshSchemeMatch = url.match(/^ssh:\/\/[^@]+@([^/]+)\/(.+)$/);
if (sshSchemeMatch) {
hostname = sshSchemeMatch[1];
path = sshSchemeMatch[2];
}
// git@host:owner/repo.git
if (!hostname) {
const sshMatch = url.match(/^[^@]+@([^:]+):(.+)$/);
if (sshMatch) {
hostname = sshMatch[1];
path = sshMatch[2];
}
}
// https://host/owner/repo.git
if (!hostname) {
try {
const parsed = new URL(url);
hostname = parsed.hostname;
path = parsed.pathname.replace(/^\//, '');
} catch {
return null;
}
}
if (!hostname || !path) return null;
// Strip .git suffix
path = path.replace(/\.git$/, '');
// Extract owner/repo (first two path segments)
const segments = path.split('/').filter(Boolean);
if (segments.length < 2) return null;
const owner = segments[0];
const repo = segments[1];
const platform = PLATFORM_MAP[hostname] ?? 'other';
const webURL = platform !== 'other' ? `https://${hostname}/${owner}/${repo}` : null;
return {
owner,
repo,
ownerRepo: `${owner}/${repo}`,
hostname,
platform,
webURL,
};
}