mirror of
https://github.com/sourcegit-scm/sourcegit.git
synced 2024-11-01 13:13:21 -07:00
79 lines
2.2 KiB
C#
79 lines
2.2 KiB
C#
using System;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace SourceGit.Models
|
|
{
|
|
public partial class Remote
|
|
{
|
|
[GeneratedRegex(@"^http[s]?://([\w\-]+@)?[\w\.\-]+(\:[0-9]+)?/[\w\-/~]+/[\w\-\.]+(\.git)?$")]
|
|
private static partial Regex REG_HTTPS();
|
|
[GeneratedRegex(@"^[\w\-]+@[\w\.\-]+(\:[0-9]+)?:[\w\-/~]+/[\w\-\.]+(\.git)?$")]
|
|
private static partial Regex REG_SSH1();
|
|
[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(),
|
|
REG_SSH2(),
|
|
];
|
|
|
|
public string Name { get; set; }
|
|
public string URL { get; set; }
|
|
|
|
public static bool IsSSH(string url)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(url))
|
|
return false;
|
|
|
|
for (int i = 1; i < URL_FORMATS.Length; i++)
|
|
{
|
|
if (URL_FORMATS[i].IsMatch(url))
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public static bool IsValidURL(string url)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(url))
|
|
return false;
|
|
|
|
foreach (var fmt in URL_FORMATS)
|
|
{
|
|
if (fmt.IsMatch(url))
|
|
return true;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|