feature: supports visit remote url in browser

This commit is contained in:
leo 2024-06-17 20:31:54 +08:00
parent 3afb134037
commit ad2fc68c6b
No known key found for this signature in database
GPG key ID: B528468E49CD0E58
6 changed files with 58 additions and 9 deletions

View file

@ -1,4 +1,5 @@
using System.Text.RegularExpressions;
using System;
using System.Text.RegularExpressions;
namespace SourceGit.Models
{
@ -11,6 +12,9 @@ namespace SourceGit.Models
[GeneratedRegex(@"^ssh://([\w\-]+@)?[\w\.\-]+(\:[0-9]+)?/[\w\-/]+/[\w\-\.]+(\.git)?$")]
private static partial Regex REG_SSH2();
[GeneratedRegex(@"^git@([\w\.\-]+):([\w\-/]+/[\w\-\.]+)\.git$")]
private static partial Regex REG_TO_VISIT_URL_CAPTURE();
private static readonly Regex[] URL_FORMATS = [
REG_HTTPS(),
REG_SSH1(),
@ -43,5 +47,29 @@ namespace SourceGit.Models
}
return false;
}
public bool TryGetVisitURL(out string url)
{
url = null;
if (URL.StartsWith("http", StringComparison.Ordinal))
{
if (URL.EndsWith(".git"))
url = URL.Substring(0, URL.Length - 4);
else
url = URL;
return true;
}
var match = REG_TO_VISIT_URL_CAPTURE().Match(URL);
if (match.Success)
{
url = $"https://{match.Groups[1].Value}/{match.Groups[2].Value}";
return true;
}
return false;
}
}
}