parseGitRemote function

ParsedRepository? parseGitRemote(
  1. String input
)

Parse a git remote URL into host, owner, and name.

Implementation

ParsedRepository? parseGitRemote(String input) {
  final trimmed = input.trim();

  // SSH format: git@host:owner/repo.git
  final sshMatch = RegExp(
    r'^git@([^:]+):([^/]+)/([^/]+?)(?:\.git)?$',
  ).firstMatch(trimmed);
  if (sshMatch != null) {
    final host = sshMatch.group(1)!;
    if (!_looksLikeRealHostname(host)) return null;
    return ParsedRepository(
      host: host,
      owner: sshMatch.group(2)!,
      name: sshMatch.group(3)!,
    );
  }

  // URL format
  final urlMatch = RegExp(
    r'^(https?|ssh|git)://(?:[^@]+@)?([^/:]+(?::\d+)?)/([^/]+)/([^/]+?)(?:\.git)?$',
  ).firstMatch(trimmed);
  if (urlMatch != null) {
    final protocol = urlMatch.group(1)!;
    final hostWithPort = urlMatch.group(2)!;
    final hostWithoutPort = hostWithPort.split(':').first;
    if (!_looksLikeRealHostname(hostWithoutPort)) return null;
    final host = (protocol == 'https' || protocol == 'http')
        ? hostWithPort
        : hostWithoutPort;
    return ParsedRepository(
      host: host,
      owner: urlMatch.group(3)!,
      name: urlMatch.group(4)!,
    );
  }

  return null;
}