From b9dc5a81643548c66d925b88b7dde2d91f1c053d Mon Sep 17 00:00:00 2001 From: leo Date: Tue, 29 Apr 2025 18:08:35 +0800 Subject: [PATCH] feature: parse url in commit message (#1133) Signed-off-by: leo --- src/ViewModels/CommitDetail.cs | 36 +++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/ViewModels/CommitDetail.cs b/src/ViewModels/CommitDetail.cs index 6581d7bb..fdaa29f1 100644 --- a/src/ViewModels/CommitDetail.cs +++ b/src/ViewModels/CommitDetail.cs @@ -642,10 +642,37 @@ namespace SourceGit.ViewModels rule.Matches(inlines, message); } - var matches = REG_SHA_FORMAT().Matches(message); - for (int i = 0; i < matches.Count; i++) + var urlMatches = REG_URL_FORMAT().Matches(message); + for (int i = 0; i < urlMatches.Count; i++) { - var match = matches[i]; + var match = urlMatches[i]; + if (!match.Success) + continue; + + var start = match.Index; + var len = match.Length; + var intersect = false; + foreach (var link in inlines) + { + if (link.Intersect(start, len)) + { + intersect = true; + break; + } + } + + if (intersect) + continue; + + var url = message.Substring(start, len); + if (Uri.IsWellFormedUriString(url, UriKind.Absolute)) + inlines.Add(new Models.InlineElement(Models.InlineElementType.Link, start, len, url)); + } + + var shaMatches = REG_SHA_FORMAT().Matches(message); + for (int i = 0; i < shaMatches.Count; i++) + { + var match = shaMatches[i]; if (!match.Success) continue; @@ -840,6 +867,9 @@ namespace SourceGit.ViewModels RevisionFileSearchSuggestion = suggestion; } + [GeneratedRegex(@"\b(https?://|ftp://)[\w\d\._/\-~%@()+:?&=#!]*[\w\d/]")] + private static partial Regex REG_URL_FORMAT(); + [GeneratedRegex(@"\b([0-9a-fA-F]{6,40})\b")] private static partial Regex REG_SHA_FORMAT();