File size: 1,289 Bytes
4e23b01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
33
34
35
36
37
38
39
40
41
42
43
export function normalizeRemote(url: string): string {
  const trimmed = url.trim();
  if (trimmed.length === 0) return '';

  if (!trimmed.includes('://')) {
    const scpLike = /^(?:[^@\s]+@)?([^:\s]+):(.+)$/.exec(trimmed);
    if (scpLike !== null) {
      return joinRemoteParts(scpLike[1], undefined, scpLike[2]);
    }
    return stripGitSuffix(trimmed);
  }

  try {
    const parsed = new URL(trimmed);
    if (parsed.hostname.length === 0) return stripGitSuffix(trimmed);
    return joinRemoteParts(parsed.hostname, parsed.port, parsed.pathname);
  } catch {
    return stripGitSuffix(trimmed);
  }
}

function joinRemoteParts(
  host: string | undefined,
  port: string | undefined,
  path: string | undefined,
): string {
  const normalizedHost = host?.trim() ?? '';
  const normalizedPort = port?.trim() ?? '';
  const normalizedPath = stripGitSuffix(path ?? '');
  const parts = [normalizedHost];
  if (normalizedPort.length > 0) parts.push(normalizedPort);
  if (normalizedPath.length > 0) parts.push(normalizedPath);
  return parts.join('/');
}

function stripGitSuffix(value: string): string {
  let normalized = value.trim().replace(/^\/+/, '').replace(/\/+$/, '');
  if (normalized.endsWith('.git')) {
    normalized = normalized.slice(0, -4);
  }
  return normalized;
}