readonly record struct CommitLink(

This commit is contained in:
M-L-Ml 2025-04-21 20:50:23 +02:00
parent 46ae4064f9
commit f381c1cfdb
3 changed files with 78 additions and 84 deletions

View file

@ -8,75 +8,68 @@ namespace SourceGit.Models
/// <summary>
/// Represents a commit link for a remote repository.
/// </summary>
public class CommitLink
public readonly record struct CommitLink(string Name, string URLPrefix)
{
public string Name { get; set; }
public string URLPrefix { get; set; }
public CommitLink(string name, string prefix)
}
public readonly record struct ProviderInfo(
string Name,
string HostPrefix,
Func<string, string> ExtractRepo,
Func<string, string> BuildCommitUrlPrefix)
{
public bool IsMatch(string url) => url.StartsWith(HostPrefix, StringComparison.Ordinal);
}
public static class CommitLinkDetails // Changed from private to internal to fix CS1527
{
static readonly ProviderInfo[] Providers = new[]
{
Name = name;
URLPrefix = prefix;
}
public readonly record struct ProviderInfo(
string Name,
string HostPrefix,
Func<string, string> ExtractRepo,
Func<string, string> BuildCommitUrlPrefix)
{
public bool IsMatch(string url) => url.StartsWith(HostPrefix, StringComparison.Ordinal);
}
private static readonly ProviderInfo[] Providers = new[]
{
new ProviderInfo(
"Github",
"https://github.com/",
url => url.EndsWith(".git") ? url[19..^4] : url[19..],
baseUrl => $"{baseUrl}/commit/"
),
new ProviderInfo(
"GitLab",
"https://gitlab.",
url => {
var trimmed = url.EndsWith(".git") ? url[15..^4] : url[15..];
int idx = trimmed.IndexOf('/') + 1;
return trimmed[idx..];
},
baseUrl => $"{baseUrl}/-/commit/"
),
new ProviderInfo(
"Gitee",
"https://gitee.com/",
url => url.EndsWith(".git") ? url[18..^4] : url[18..],
baseUrl => $"{baseUrl}/commit/"
),
new ProviderInfo(
"BitBucket",
"https://bitbucket.org/",
url => url.EndsWith(".git") ? url[22..^4] : url[22..],
baseUrl => $"{baseUrl}/commits/"
),
new ProviderInfo(
"Codeberg",
"https://codeberg.org/",
url => url.EndsWith(".git") ? url[21..^4] : url[21..],
baseUrl => $"{baseUrl}/commit/"
),
new ProviderInfo(
"Gitea",
"https://gitea.org/",
url => url.EndsWith(".git") ? url[18..^4] : url[18..],
baseUrl => $"{baseUrl}/commit/"
),
new ProviderInfo(
"sourcehut",
"https://git.sr.ht/",
url => url.EndsWith(".git") ? url[18..^4] : url[18..],
baseUrl => $"{baseUrl}/commit/"
)
};
new ProviderInfo(
"Github",
"https://github.com/",
url => url.EndsWith(".git") ? url[19..^4] : url[19..],
baseUrl => $"{baseUrl}/commit/"
),
new ProviderInfo(
"GitLab",
"https://gitlab.",
url => {
var trimmed = url.EndsWith(".git") ? url[15..^4] : url[15..];
int idx = trimmed.IndexOf('/') + 1;
return trimmed[idx..];
},
baseUrl => $"{baseUrl}/-/commit/"
),
new ProviderInfo(
"Gitee",
"https://gitee.com/",
url => url.EndsWith(".git") ? url[18..^4] : url[18..],
baseUrl => $"{baseUrl}/commit/"
),
new ProviderInfo(
"BitBucket",
"https://bitbucket.org/",
url => url.EndsWith(".git") ? url[22..^4] : url[22..],
baseUrl => $"{baseUrl}/commits/"
),
new ProviderInfo(
"Codeberg",
"https://codeberg.org/",
url => url.EndsWith(".git") ? url[21..^4] : url[21..],
baseUrl => $"{baseUrl}/commit/"
),
new ProviderInfo(
"Gitea",
"https://gitea.org/",
url => url.EndsWith(".git") ? url[18..^4] : url[18..],
baseUrl => $"{baseUrl}/commit/"
),
new ProviderInfo(
"sourcehut",
"https://git.sr.ht/",
url => url.EndsWith(".git") ? url[18..^4] : url[18..],
baseUrl => $"{baseUrl}/commit/"
)
};
/// <summary>
/// Attempts to create a CommitLink for a given remote by matching a provider.
@ -97,7 +90,7 @@ namespace SourceGit.Models
/// </summary>
public static List<CommitLink> Get(List<Remote> remotes)
{
return remotes.Select(remote =>
return remotes.Select(static remote =>
{
var rr = TryCreateCommitLink(remote);
@ -111,7 +104,8 @@ namespace SourceGit.Models
}
#endif
return rr;
}).Where(cl => cl != null).ToList();
}).Select(cl => cl.Value) // Convert nullable CommitLink to non-nullable CommitLink
.Where(cl => cl != null).ToList();
}
#if DEBUG
@ -144,7 +138,7 @@ namespace SourceGit.Models
return outs.FirstOrDefault();
}
static CommitLink()
static CommitLinkDetails()
{
//Unit tests , TODO: make normal UnitTests, delete this code.

View file

@ -134,7 +134,7 @@ namespace SourceGit.ViewModels
public CommitDetail(Repository repo)
{
_repo = repo;
WebLinks = Models.CommitLink.Get(repo.Remotes);
WebLinks = Models.CommitLinkDetails.Get(repo.Remotes);
}
public void Cleanup()

View file

@ -37,7 +37,7 @@ namespace SourceGit.Views
get => GetValue(SupportsContainsInProperty);
set => SetValue(SupportsContainsInProperty, value);
}
//TODO: Maybe some observable container instead List? Add a comment for explanation.
public static readonly StyledProperty<List<Models.CommitLink>> WebLinksProperty =
AvaloniaProperty.Register<CommitBaseInfo, List<Models.CommitLink>>(nameof(WebLinks));