using System; using System.Collections.Generic; namespace SourceGit.Models { /// /// Represents a commit link for a remote repository. /// public class CommitLink { public string Name { get; set; } public string URLPrefix { get; set; } public CommitLink(string name, string prefix) { Name = name; URLPrefix = prefix; } private static readonly (string Host, string Display, string CommitPath, int NameStart)[] Providers = new[] { ("https://github.com/", "Github", "/commit/", 19), ("https://gitlab.", "GitLab", "/-/commit/", 15), ("https://gitee.com/", "Gitee", "/commit/", 18), ("https://bitbucket.org/", "BitBucket", "/commits/", 22), ("https://codeberg.org/", "Codeberg", "/commit/", 21), ("https://gitea.org/", "Gitea", "/commit/", 18), ("https://git.sr.ht/", "sourcehut", "/commit/", 18) }; public static List Get(List remotes) { var outs = new List(); foreach (var remote in remotes) { if (remote.TryGetVisitURL(out var url)) { var trimmedUrl = url.EndsWith(".git") ? url[..^4] : url; foreach (var provider in Providers) { if (url.StartsWith(provider.Host, StringComparison.Ordinal)) { string repoName; if (provider.Host == "https://gitlab.") { // GitLab: find the first '/' after host int idx = trimmedUrl[provider.NameStart..].IndexOf('/') + provider.NameStart + 1; repoName = trimmedUrl[idx..]; } else { repoName = trimmedUrl[provider.NameStart..]; } outs.Add(new CommitLink($"{provider.Display} ({repoName})", $"{url}{provider.CommitPath}")); break; } } } } return outs; } } }