-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGitHubUrl.cs
More file actions
59 lines (34 loc) · 1.69 KB
/
GitHubUrl.cs
File metadata and controls
59 lines (34 loc) · 1.69 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
using System;
using System.Text.RegularExpressions;
namespace Gsemac.Net.GitHub {
public class GitHubUrl :
IGitHubUrl {
// Public members
public string Owner { get; }
public string Path { get; }
public string RepositoryName { get; }
public string Tree { get; }
public static GitHubUrl Parse(string url) {
return new GitHubUrl(url);
}
// Private members
private GitHubUrl(string url) {
// Read the owner and repository name from the URL.
Match ownerAndRepositoryNameMatch = Regex.Match(url, $@"^(?:{Regex.Escape(Properties.GitHub.RootUrl)}|{Regex.Escape(Properties.GitHub.RawRootUrl)})(?<owner>[^\/]+)\/(?<name>[^\/]+)");
if (!ownerAndRepositoryNameMatch.Success)
throw new FormatException("The URL is not in the correct format.");
Owner = Uri.UnescapeDataString(ownerAndRepositoryNameMatch.Groups["owner"].Value);
RepositoryName = Uri.UnescapeDataString(ownerAndRepositoryNameMatch.Groups["name"].Value);
// Read the branch name or commit hash.
Match treeNameMatch = Regex.Match(url, $@"\/(?:tree|blob)\/(?<tree>[^\/]+)|{Regex.Escape(Properties.GitHub.RawRootUrl)}[^\/]+\/[^\/]+\/([^\/]+)");
if (treeNameMatch.Success) {
string tree = treeNameMatch.Groups["tree"].Value;
Tree = tree;
// Read the remaining path.
Match pathMatch = Regex.Match(url, $@"{tree}(?<path>\/.+?)$");
if (pathMatch.Success)
Path = Uri.UnescapeDataString(pathMatch.Groups["path"].Value);
}
}
}
}