From d7471f0ff58febfa835cb095947228c3de14842b Mon Sep 17 00:00:00 2001 From: JC-Chung <52159296+JC-Chung@users.noreply.github.com> Date: Mon, 29 Jun 2026 15:42:43 +0800 Subject: [PATCH] fix: escape BRE special characters when selecting author from suggestions --- src/App.Extensions.cs | 12 ++++++++++++ src/ViewModels/SearchCommitContext.cs | 17 +++++++++++++++-- src/Views/Repository.axaml.cs | 4 ++-- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/App.Extensions.cs b/src/App.Extensions.cs index 7c2d0195a..e6d8add25 100644 --- a/src/App.Extensions.cs +++ b/src/App.Extensions.cs @@ -18,6 +18,18 @@ public static string Escaped(this string value) return value.Replace("\"", "\\\"", StringComparison.Ordinal); } + public static string EscapeForBRE(this string value) + { + return value + .Replace("\\", "\\\\", StringComparison.Ordinal) + .Replace(".", "\\.", StringComparison.Ordinal) + .Replace("[", "\\[", StringComparison.Ordinal) + .Replace("*", "\\*", StringComparison.Ordinal) + .Replace("^", "\\^", StringComparison.Ordinal) + .Replace("$", "\\$", StringComparison.Ordinal) + .Replace("{", "\\{", StringComparison.Ordinal); + } + public static string FormatFontNames(string input) { if (string.IsNullOrEmpty(input)) diff --git a/src/ViewModels/SearchCommitContext.cs b/src/ViewModels/SearchCommitContext.cs index e67046a9c..eec02d7fa 100644 --- a/src/ViewModels/SearchCommitContext.cs +++ b/src/ViewModels/SearchCommitContext.cs @@ -27,6 +27,7 @@ public string Filter get => _filter; set { + _isLiteralFilter = false; if (SetProperty(ref _filter, value)) UpdateSuggestions(); } @@ -82,6 +83,14 @@ public void ClearFilter() Results = null; } + public void SetLiteralFilter(string filter) + { + _isLiteralFilter = true; + _filter = filter; + OnPropertyChanged(nameof(Filter)); + UpdateSuggestions(); + } + public void ClearSuggestions() { Suggestions = null; @@ -109,6 +118,9 @@ public void StartSearch() var result = new List(); var method = (Models.CommitSearchMethod)_method; var repoPath = _repo.FullPath; + var filter = (_isLiteralFilter && method == Models.CommitSearchMethod.ByAuthor) + ? _filter.EscapeForBRE() + : _filter; if (method == Models.CommitSearchMethod.BySHA) { @@ -131,7 +143,7 @@ public void StartSearch() } else if (_onlySearchCurrentBranch) { - result = await new Commands.QueryCommits(repoPath, _filter, method, true) + result = await new Commands.QueryCommits(repoPath, filter, method, true) .GetResultAsync() .ConfigureAwait(false); @@ -140,7 +152,7 @@ public void StartSearch() } else { - result = await new Commands.QueryCommits(repoPath, _filter, method, false) + result = await new Commands.QueryCommits(repoPath, filter, method, false) .GetResultAsync() .ConfigureAwait(false); @@ -290,6 +302,7 @@ private void UpdateSuggestions() private int _method = (int)Models.CommitSearchMethod.ByMessage; private string _filter = string.Empty; private bool _onlySearchCurrentBranch = false; + private bool _isLiteralFilter = false; private bool _isQuerying = false; private List _results = null; private Models.Commit _selected = null; diff --git a/src/Views/Repository.axaml.cs b/src/Views/Repository.axaml.cs index 97e544481..1fdbe3ddc 100644 --- a/src/Views/Repository.axaml.cs +++ b/src/Views/Repository.axaml.cs @@ -328,7 +328,7 @@ private void OnSearchSuggestionBoxKeyDown(object _, KeyEventArgs e) else if (selected is Models.User user) { var apply = user.ToString(); - repo.SearchCommitContext.Filter = apply; + repo.SearchCommitContext.SetLiteralFilter(apply); TxtSearchCommitsBox.CaretIndex = apply.Length; } @@ -351,7 +351,7 @@ private void OnSearchSuggestionTapped(object sender, TappedEventArgs e) else if (ctx is Models.User user) { var apply = user.ToString(); - repo.SearchCommitContext.Filter = apply; + repo.SearchCommitContext.SetLiteralFilter(apply); TxtSearchCommitsBox.CaretIndex = apply.Length; }