normalizeGitRemoteUrl function
Normalise a git remote URL to a canonical form for hashing. Converts SSH and HTTPS URLs to: host/owner/repo (lowercase, no .git).
Implementation
String? normalizeGitRemoteUrl(String url) {
final trimmed = url.trim();
if (trimmed.isEmpty) return null;
// SSH format: git@host:owner/repo.git
final sshMatch = RegExp(r'^git@([^:]+):(.+?)(?:\.git)?$').firstMatch(trimmed);
if (sshMatch != null) {
return '${sshMatch.group(1)}/${sshMatch.group(2)}'.toLowerCase();
}
// HTTPS/SSH URL format
final urlMatch = RegExp(
r'^(?:https?|ssh)://(?:[^@]+@)?([^/]+)/(.+?)(?:\.git)?$',
).firstMatch(trimmed);
if (urlMatch != null) {
final host = urlMatch.group(1)!;
final path = urlMatch.group(2)!;
if (_isLocalHost(host) && path.startsWith('git/')) {
final proxyPath = path.substring(4);
final segments = proxyPath.split('/');
if (segments.length >= 3 && segments[0].contains('.')) {
return proxyPath.toLowerCase();
}
return 'github.com/$proxyPath'.toLowerCase();
}
return '$host/$path'.toLowerCase();
}
return null;
}