2024-06-17 05:31:54 -07:00
|
|
|
|
using System;
|
|
|
|
|
using System.Text.RegularExpressions;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
namespace SourceGit.Models
|
|
|
|
|
{
|
|
|
|
|
public partial class Remote
|
|
|
|
|
{
|
2024-10-18 03:02:40 -07:00
|
|
|
|
[GeneratedRegex(@"^http[s]?://([\w\-]+@)?[\w\.\-]+(\:[0-9]+)?/[\w\-/~%]+/[\w\-\.%]+(\.git)?$")]
|
2024-04-06 03:11:49 -07:00
|
|
|
|
private static partial Regex REG_HTTPS();
|
2024-10-18 03:02:40 -07:00
|
|
|
|
[GeneratedRegex(@"^[\w\-]+@[\w\.\-]+(\:[0-9]+)?:[\w\-/~%]+/[\w\-\.%]+(\.git)?$")]
|
2024-04-06 03:11:49 -07:00
|
|
|
|
private static partial Regex REG_SSH1();
|
2024-06-18 19:38:30 -07:00
|
|
|
|
[GeneratedRegex(@"^ssh://([\w\-]+@)?[\w\.\-]+(\:[0-9]+)?/[\w\-/~]+/[\w\-\.]+(\.git)?$")]
|
2024-04-06 03:11:49 -07:00
|
|
|
|
private static partial Regex REG_SSH2();
|
2024-03-17 18:37:06 -07:00
|
|
|
|
|
2024-06-18 19:38:30 -07:00
|
|
|
|
[GeneratedRegex(@"^git@([\w\.\-]+):([\w\-/~]+/[\w\-\.]+)\.git$")]
|
2024-06-17 05:31:54 -07:00
|
|
|
|
private static partial Regex REG_TO_VISIT_URL_CAPTURE();
|
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
|
private static readonly Regex[] URL_FORMATS = [
|
2024-04-06 03:11:49 -07:00
|
|
|
|
REG_HTTPS(),
|
|
|
|
|
REG_SSH1(),
|
|
|
|
|
REG_SSH2(),
|
2024-02-05 23:08:37 -08:00
|
|
|
|
];
|
|
|
|
|
|
2021-04-29 05:05:55 -07:00
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
public string URL { get; set; }
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public static bool IsSSH(string url)
|
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (string.IsNullOrWhiteSpace(url))
|
|
|
|
|
return false;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
for (int i = 1; i < URL_FORMATS.Length; i++)
|
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (URL_FORMATS[i].IsMatch(url))
|
|
|
|
|
return true;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public static bool IsValidURL(string url)
|
|
|
|
|
{
|
2024-07-09 00:02:34 -07:00
|
|
|
|
if (string.IsNullOrWhiteSpace(url))
|
|
|
|
|
return false;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
foreach (var fmt in URL_FORMATS)
|
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (fmt.IsMatch(url))
|
|
|
|
|
return true;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
2024-07-09 00:02:34 -07:00
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2024-06-17 05:31:54 -07:00
|
|
|
|
|
|
|
|
|
public bool TryGetVisitURL(out string url)
|
|
|
|
|
{
|
|
|
|
|
url = null;
|
|
|
|
|
|
|
|
|
|
if (URL.StartsWith("http", StringComparison.Ordinal))
|
|
|
|
|
{
|
2024-08-14 19:39:31 -07:00
|
|
|
|
// Try to remove the user before host and `.git` extension.
|
|
|
|
|
var uri = new Uri(URL.EndsWith(".git", StringComparison.Ordinal) ? URL.Substring(0, URL.Length - 4) : URL);
|
|
|
|
|
if (uri.Port != 80 && uri.Port != 443)
|
|
|
|
|
url = $"{uri.Scheme}://{uri.Host}:{uri.Port}{uri.LocalPath}";
|
2024-06-17 05:31:54 -07:00
|
|
|
|
else
|
2024-08-14 19:39:31 -07:00
|
|
|
|
url = $"{uri.Scheme}://{uri.Host}{uri.LocalPath}";
|
2024-06-17 05:31:54 -07:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|