fromInternalString static method
Creates a GitRef from an internal string representation
Implementation
static GitRef fromInternalString(String internalString) {
if (!internalString.startsWith('GIT:')) {
throw ArgumentError('Invalid internal git ref string: $internalString');
}
final withoutPrefix = internalString.substring(4); // Remove 'GIT:'
// Find the last colon to separate ref from URI+path
final lastColonIndex = withoutPrefix.lastIndexOf(':');
if (lastColonIndex == -1) {
throw ArgumentError('Invalid internal git ref string: $internalString');
}
final uriWithPath = withoutPrefix.substring(0, lastColonIndex);
final refPart = withoutPrefix.substring(lastColonIndex + 1);
final actualRef = refPart == 'HEAD' ? null : refPart;
// Parse URI and path using regex
String uri;
String? path;
if (uriWithPath.startsWith('https://')) {
final httpsPattern =
RegExp(r'^(https://[^/]+/[^/]+/[^/]+(?:\.git)?)((?:/.+)?)$');
final match = httpsPattern.firstMatch(uriWithPath);
if (match != null) {
uri = match.group(1)!;
final pathPart = match.group(2);
path = pathPart?.isNotEmpty == true && pathPart!.startsWith('/')
? pathPart.substring(1)
: pathPart?.isNotEmpty == true
? pathPart
: null;
} else {
uri = uriWithPath;
path = null;
}
} else if (uriWithPath.contains('@')) {
final sshPattern =
RegExp(r'^([^@]+@[^:]+:[^/]+/[^/]+(?:\.git)?)((?:/.+)?)$');
final match = sshPattern.firstMatch(uriWithPath);
if (match != null) {
uri = match.group(1)!;
final pathPart = match.group(2);
path = pathPart?.isNotEmpty == true && pathPart!.startsWith('/')
? pathPart.substring(1)
: pathPart?.isNotEmpty == true
? pathPart
: null;
} else {
uri = uriWithPath;
path = null;
}
} else {
uri = uriWithPath;
path = null;
}
return GitRef._(uri: uri, path: path, ref: actualRef);
}