Skip to content

Commit 2437475

Browse files
authored
feature: self host GitLab URLs in CommitLink (#1635)
1 parent df48678 commit 2437475

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/Models/CommitLink.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Text.RegularExpressions;
34

45
namespace SourceGit.Models
56
{
6-
public class CommitLink
7+
public partial class CommitLink
78
{
89
public string Name { get; set; } = null;
910
public string URLPrefix { get; set; } = null;
@@ -14,6 +15,9 @@ public CommitLink(string name, string prefix)
1415
URLPrefix = prefix;
1516
}
1617

18+
[GeneratedRegex(@"^(http|https)://[^/]*gitlab[^/]*(:[0-9]+)?.*$")]
19+
private static partial Regex REG_GITLAB();
20+
1721
public static List<CommitLink> Get(List<Remote> remotes)
1822
{
1923
var outs = new List<CommitLink>();
@@ -28,7 +32,7 @@ public static List<CommitLink> Get(List<Remote> remotes)
2832

2933
if (url.StartsWith("https://github.com/", StringComparison.Ordinal))
3034
outs.Add(new($"GitHub ({trimmedUrl[19..]})", $"{url}/commit/"));
31-
else if (url.StartsWith("https://gitlab.", StringComparison.Ordinal))
35+
else if (REG_GITLAB().IsMatch(url))
3236
outs.Add(new($"GitLab ({trimmedUrl[(trimmedUrl[15..].IndexOf('/') + 16)..]})", $"{url}/-/commit/"));
3337
else if (url.StartsWith("https://gitee.com/", StringComparison.Ordinal))
3438
outs.Add(new($"Gitee ({trimmedUrl[18..]})", $"{url}/commit/"));

src/Models/Remote.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ public partial class Remote
88
{
99
[GeneratedRegex(@"^https?://[^/]+/.+[^/\.]$")]
1010
private static partial Regex REG_HTTPS();
11+
1112
[GeneratedRegex(@"^git://[^/]+/.+[^/\.]$")]
1213
private static partial Regex REG_GIT();
14+
1315
[GeneratedRegex(@"^[\w\-]+@[\w\.\-]+(\:[0-9]+)?:([a-zA-z0-9~%][\w\-\./~%]*)?[a-zA-Z0-9](\.git)?$")]
1416
private static partial Regex REG_SSH1();
17+
1518
[GeneratedRegex(@"^ssh://([\w\-]+@)?[\w\.\-]+(\:[0-9]+)?/([a-zA-z0-9~%][\w\-\./~%]*)?[a-zA-Z0-9](\.git)?$")]
1619
private static partial Regex REG_SSH2();
1720

0 commit comments

Comments
 (0)