forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemote.cs
More file actions
143 lines (115 loc) · 4.52 KB
/
Remote.cs
File metadata and controls
143 lines (115 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;
namespace SourceGit.Models
{
public partial class Remote
{
[GeneratedRegex(@"^https?://[^/]+/.+[^/\.]$")]
private static partial Regex REG_HTTPS();
[GeneratedRegex(@"^git://[^/]+/.+[^/\.]$")]
private static partial Regex REG_GIT();
[GeneratedRegex(@"^[\w\-]+@[\w\.\-]+(\:[0-9]+)?:([a-zA-z0-9~%][\w\-\./~%]*)?[a-zA-Z0-9](\.git)?$")]
private static partial Regex REG_SSH1();
[GeneratedRegex(@"^ssh://([\w\-]+@)?[\w\.\-]+(\:[0-9]+)?/([a-zA-z0-9~%][\w\-\./~%]*)?[a-zA-Z0-9](\.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_GIT(),
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;
if (REG_SSH1().IsMatch(url))
return true;
return REG_SSH2().IsMatch(url);
}
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 url.StartsWith("file://", StringComparison.Ordinal) ||
url.StartsWith("./", StringComparison.Ordinal) ||
url.StartsWith("../", StringComparison.Ordinal) ||
Directory.Exists(url);
}
public bool TryGetVisitURL(out string url)
{
url = null;
if (URL.StartsWith("http", StringComparison.Ordinal))
{
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}";
else
url = $"{uri.Scheme}://{uri.Host}{uri.LocalPath}";
return true;
}
var match = REG_TO_VISIT_URL_CAPTURE().Match(URL);
if (match.Success)
{
var host = match.Groups[1].Value;
var supportHTTPS = HTTPSValidator.IsSupported(host);
var scheme = supportHTTPS ? "https" : "http";
url = $"{scheme}://{host}/{match.Groups[2].Value}";
return true;
}
return false;
}
public bool TryGetCreatePullRequestURL(out string url, string mergeBranch)
{
url = null;
if (!TryGetVisitURL(out var baseURL))
return false;
var uri = new Uri(baseURL);
var host = uri.Host;
var route = uri.AbsolutePath.TrimStart('/');
var encodedBranch = HttpUtility.UrlEncode(mergeBranch);
if (host.Contains("github.com", StringComparison.Ordinal))
{
url = $"{baseURL}/compare/{encodedBranch}?expand=1";
return true;
}
if (host.Contains("gitlab", StringComparison.Ordinal))
{
url = $"{baseURL}/-/merge_requests/new?merge_request%5Bsource_branch%5D={encodedBranch}";
return true;
}
if (host.Equals("gitee.com", StringComparison.Ordinal))
{
url = $"{baseURL}/pulls/new?source={encodedBranch}";
return true;
}
if (host.Equals("bitbucket.org", StringComparison.Ordinal))
{
url = $"{baseURL}/pull-requests/new?source={encodedBranch}";
return true;
}
if (host.Equals("gitea.org", StringComparison.Ordinal))
{
url = $"{baseURL}/compare/{encodedBranch}";
return true;
}
if (host.Contains("azure.com", StringComparison.Ordinal) ||
host.Contains("visualstudio.com", StringComparison.Ordinal))
{
url = $"{baseURL}/pullrequestcreate?sourceRef={encodedBranch}";
return true;
}
return false;
}
}
}