refactor: rewrite commit web link

* add support for Bitbucket
This commit is contained in:
leo 2024-08-15 10:39:31 +08:00
parent 48de509c63
commit bf6ac4d20d
No known key found for this signature in database
4 changed files with 13 additions and 9 deletions

View file

@ -3,6 +3,6 @@
public class CommitLink
{
public string Name { get; set; } = null;
public string URLTemplate { get; set; } = null;
public string URLPrefix { get; set; } = null;
}
}

View file

@ -58,10 +58,12 @@ namespace SourceGit.Models
if (URL.StartsWith("http", StringComparison.Ordinal))
{
if (URL.EndsWith(".git"))
url = URL.Substring(0, URL.Length - 4);
// Try to remove the user before host and `.git` extension.
var uri = new Uri(URL.EndsWith(".git", StringComparison.Ordinal) ? URL.Substring(0, URL.Length - 4) : URL);
if (uri.Port != 80 && uri.Port != 443)
url = $"{uri.Scheme}://{uri.Host}:{uri.Port}{uri.LocalPath}";
else
url = URL;
url = $"{uri.Scheme}://{uri.Host}{uri.LocalPath}";
return true;
}

View file

@ -109,11 +109,13 @@ namespace SourceGit.ViewModels
if (remote.TryGetVisitURL(out var url))
{
if (url.StartsWith("https://github.com/", StringComparison.Ordinal))
WebLinks.Add(new Models.CommitLink() { Name = "Github", URLTemplate = $"{url}/commit/SOURCEGIT_COMMIT_HASH_CODE" });
WebLinks.Add(new Models.CommitLink() { Name = "Github", URLPrefix = $"{url}/commit/" });
else if (url.StartsWith("https://gitlab.com/", StringComparison.Ordinal))
WebLinks.Add(new Models.CommitLink() { Name = "GitLab", URLTemplate = $"{url}/-/commit/SOURCEGIT_COMMIT_HASH_CODE" });
WebLinks.Add(new Models.CommitLink() { Name = "GitLab", URLPrefix = $"{url}/-/commit/" });
else if (url.StartsWith("https://gitee.com/", StringComparison.Ordinal))
WebLinks.Add(new Models.CommitLink() { Name = "Gitee", URLTemplate = $"{url}/commit/SOURCEGIT_COMMIT_HASH_CODE" });
WebLinks.Add(new Models.CommitLink() { Name = "Gitee", URLPrefix = $"{url}/commit/" });
else if (url.StartsWith("https://bitbucket.org/", StringComparison.Ordinal))
WebLinks.Add(new Models.CommitLink() { Name = "Bithbucket", URLPrefix = $"{url}/commits/" });
}
}
}

View file

@ -51,7 +51,7 @@ namespace SourceGit.Views
foreach (var link in links)
{
var url = link.URLTemplate.Replace("SOURCEGIT_COMMIT_HASH_CODE", detail.Commit.SHA);
var url = $"{link.URLPrefix}{detail.Commit.SHA}";
var item = new MenuItem() { Header = link.Name };
item.Click += (_, ev) =>
{
@ -66,7 +66,7 @@ namespace SourceGit.Views
}
else if (links.Count == 1)
{
var url = links[0].URLTemplate.Replace("SOURCEGIT_COMMIT_HASH_CODE", detail.Commit.SHA);
var url = $"{links[0].URLPrefix}{detail.Commit.SHA}";
Native.OS.OpenBrowser(url);
}
}