-
Notifications
You must be signed in to change notification settings - Fork 1
/
GitUrl.cs
164 lines (148 loc) · 5.33 KB
/
GitUrl.cs
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace git_gears
{
/// <summary>
/// Class for parsing a git repo URL into its components
/// exists b/c default parser `class Uri` fails on github URLs
/// </summary>
public class GitUrl
{
static private List<Regex> UrlSchemas = new List<Regex>(){
// generic patterns _adapted_ from git-url-parse (python)
new Regex(@"^(?<protocol>https?|git|ssh|rsync)\://" + //
@"(?:(?<user>.+)@)*" + //
@"(?<hostname>[a-z0-9_.-]*)" + //
@"[:/]*" + //
@"(?<port>[\d]+){0,1}" + //
@"(?<path>\/((?<owner>[\w\-]+)\/)?" + //
@"((?<reponame>[\w\-\.]+?)(\.git|\/)?)?)$",
RegexOptions.Compiled | RegexOptions.IgnoreCase),
new Regex(@"(git\+)?" + //
@"((?<protocol>\w+)://)" + //
@"((?<user>\w+)@)?" + //
@"((?<hostname>[\w\.\-]+))" + //
@"(:(?<port>\d+))?" + //
@"(?<path>(\/(?<owner>\w+)/)?" + //
@"(\/?(?<reponame>[\w\-]+)(\.git|\/)?)?)$",
RegexOptions.Compiled | RegexOptions.IgnoreCase),
new Regex(@"^(?:(?<user>.+)@)*" + //
@"(?<hostname>[a-z0-9_.-]*)[:]*" + //
@"(?<port>[\d]+){0,1}" + //
@"(?<path>\/?(?<owner>.+)/(?<reponame>.+).git)$",
RegexOptions.Compiled | RegexOptions.IgnoreCase),
new Regex(@"((?<user>\w+)@)?" + //
@"((?<hostname>[\w\.\-]+))" + //
@"[\:\/]{1,2}" + //
@"(?<path>((?<owner>\w+)/)?" + //
@"((?<reponame>[\w\-]+)(\.git|\/)?)?)$",
RegexOptions.Compiled | RegexOptions.IgnoreCase),
// service specific patterns adapted from giturlparse
// base
new Regex(@"(?<user>.+)s@(?<hostname>.+)s:(?<reponame>.+)s.git",
RegexOptions.Compiled | RegexOptions.IgnoreCase),
new Regex(@"(http://(?<hostname>.+)s/(?<reponame>.+)s.git)", RegexOptions.Compiled | RegexOptions.IgnoreCase),
new Regex(@"(http://(?<hostname>.+)s/(?<reponame>.+)s.git)", RegexOptions.Compiled | RegexOptions.IgnoreCase),
new Regex(@"^(git://(?<hostname>.+)s/(?<reponame>.+)s.git)$", RegexOptions.Compiled | RegexOptions.IgnoreCase),
// assembla
new Regex(@"^(git@(?<hostname>.+):(?<reponame>.+).git)$", RegexOptions.Compiled | RegexOptions.IgnoreCase),
new Regex(@"^(git://(?<hostname>.+)/(?<reponame>.+).git)$", RegexOptions.Compiled | RegexOptions.IgnoreCase),
// bitbucket
new Regex(@"^(https://(?<user>.+)@(?<hostname>.+)/(?<owner>.+)/(?<reponame>.+).git)$",
RegexOptions.Compiled | RegexOptions.IgnoreCase),
new Regex(@"^(git@(?<hostname>.+):(?<owner>.+)/(?<reponame>.+).git)$",
RegexOptions.Compiled | RegexOptions.IgnoreCase),
// friendcode
new Regex(@"^(https://(?<hostname>.+)/(?<owner>.+)@user/(?<reponame>.+).git)$",
RegexOptions.Compiled | RegexOptions.IgnoreCase),
// github
new Regex(@"^(https://(?<hostname>.+)/(?<owner>.+)/(?<reponame>.+).git)$",
RegexOptions.Compiled | RegexOptions.IgnoreCase),
new Regex(@"^(git@(?<hostname>.+):(?<owner>.+)/(?<reponame>.+).git)$",
RegexOptions.Compiled | RegexOptions.IgnoreCase),
new Regex(@"^(git://(?<hostname>.+)/(?<owner>.+)/(?<reponame>.+).git)$",
RegexOptions.Compiled | RegexOptions.IgnoreCase),
// gitlab
new Regex(@"^(https://(?<hostname>.+)/(?<owner>.+)/(?<reponame>.+).git)$",
RegexOptions.Compiled | RegexOptions.IgnoreCase),
new Regex(@"^(git@(?<hostname>.+):(?<owner>.+)/(?<reponame>.+).git)$",
RegexOptions.Compiled | RegexOptions.IgnoreCase),
new Regex(@"^(git://(?<hostname>.+)/(?<owner>.+)/(?<reponame>.+).git)$",
RegexOptions.Compiled | RegexOptions.IgnoreCase),
};
public GitUrl(string url)
{
foreach (var pattern in UrlSchemas)
{
Match match = pattern.Match(url);
if (match.Success)
{
if (match.Groups.ContainsKey("protocol") && !string.IsNullOrEmpty(match.Groups["protocol"].Value))
{
Protocol = match.Groups["protocol"].Value;
}
if (match.Groups.ContainsKey("user") && !string.IsNullOrEmpty(match.Groups["user"].Value))
{
User = match.Groups["user"].Value;
}
if (match.Groups.ContainsKey("hostname") && !string.IsNullOrEmpty(match.Groups["hostname"].Value))
{
Host = match.Groups["hostname"].Value;
}
if (match.Groups.ContainsKey("port") && !string.IsNullOrEmpty(match.Groups["port"].Value))
{
Console.WriteLine($"port: {match.Groups[" port "].Value}");
Port = int.Parse(match.Groups["port"].Value);
}
if (match.Groups.ContainsKey("path") && !string.IsNullOrEmpty(match.Groups["path"].Value))
{
Path = match.Groups["path"].Value;
}
if (match.Groups.ContainsKey("owner") && !string.IsNullOrEmpty(match.Groups["owner"].Value))
{
Owner = match.Groups["owner"].Value;
}
if (match.Groups.ContainsKey("reponame") && !string.IsNullOrEmpty(match.Groups["reponame"].Value))
{
RepoName = match.Groups["reponame"].Value;
}
// iterate until the important parts are set
// i.e. break if they are set, else continue
if (!(string.IsNullOrEmpty(Host) || string.IsNullOrEmpty(Path) || string.IsNullOrEmpty(Owner) ||
string.IsNullOrEmpty(RepoName)))
break;
}
}
}
public string Protocol
{
get;
}
public string User
{
get;
}
public string Host
{
get;
}
public int Port
{
get;
}
public string Path
{
get;
}
public string Owner
{
get;
}
public string RepoName
{
get;
}
}
}