Skip to content

Commit 1e25529

Browse files
authored
fix: escape BRE special characters when selecting author from suggestions (#2494)
1 parent 01f55f7 commit 1e25529

3 files changed

Lines changed: 29 additions & 4 deletions

File tree

src/App.Extensions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ public static string Escaped(this string value)
1818
return value.Replace("\"", "\\\"", StringComparison.Ordinal);
1919
}
2020

21+
public static string EscapeForBRE(this string value)
22+
{
23+
return value
24+
.Replace("\\", "\\\\", StringComparison.Ordinal)
25+
.Replace(".", "\\.", StringComparison.Ordinal)
26+
.Replace("[", "\\[", StringComparison.Ordinal)
27+
.Replace("*", "\\*", StringComparison.Ordinal)
28+
.Replace("^", "\\^", StringComparison.Ordinal)
29+
.Replace("$", "\\$", StringComparison.Ordinal)
30+
.Replace("{", "\\{", StringComparison.Ordinal);
31+
}
32+
2133
public static string FormatFontNames(string input)
2234
{
2335
if (string.IsNullOrEmpty(input))

src/ViewModels/SearchCommitContext.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public string Filter
2727
get => _filter;
2828
set
2929
{
30+
_isLiteralFilter = false;
3031
if (SetProperty(ref _filter, value))
3132
UpdateSuggestions();
3233
}
@@ -82,6 +83,14 @@ public void ClearFilter()
8283
Results = null;
8384
}
8485

86+
public void SetLiteralFilter(string filter)
87+
{
88+
_isLiteralFilter = true;
89+
_filter = filter;
90+
OnPropertyChanged(nameof(Filter));
91+
UpdateSuggestions();
92+
}
93+
8594
public void ClearSuggestions()
8695
{
8796
Suggestions = null;
@@ -109,6 +118,9 @@ public void StartSearch()
109118
var result = new List<Models.Commit>();
110119
var method = (Models.CommitSearchMethod)_method;
111120
var repoPath = _repo.FullPath;
121+
var filter = (_isLiteralFilter && method == Models.CommitSearchMethod.ByAuthor)
122+
? _filter.EscapeForBRE()
123+
: _filter;
112124

113125
if (method == Models.CommitSearchMethod.BySHA)
114126
{
@@ -131,7 +143,7 @@ public void StartSearch()
131143
}
132144
else if (_onlySearchCurrentBranch)
133145
{
134-
result = await new Commands.QueryCommits(repoPath, _filter, method, true)
146+
result = await new Commands.QueryCommits(repoPath, filter, method, true)
135147
.GetResultAsync()
136148
.ConfigureAwait(false);
137149

@@ -140,7 +152,7 @@ public void StartSearch()
140152
}
141153
else
142154
{
143-
result = await new Commands.QueryCommits(repoPath, _filter, method, false)
155+
result = await new Commands.QueryCommits(repoPath, filter, method, false)
144156
.GetResultAsync()
145157
.ConfigureAwait(false);
146158

@@ -290,6 +302,7 @@ private void UpdateSuggestions()
290302
private int _method = (int)Models.CommitSearchMethod.ByMessage;
291303
private string _filter = string.Empty;
292304
private bool _onlySearchCurrentBranch = false;
305+
private bool _isLiteralFilter = false;
293306
private bool _isQuerying = false;
294307
private List<Models.Commit> _results = null;
295308
private Models.Commit _selected = null;

src/Views/Repository.axaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ private void OnSearchSuggestionBoxKeyDown(object _, KeyEventArgs e)
328328
else if (selected is Models.User user)
329329
{
330330
var apply = user.ToString();
331-
repo.SearchCommitContext.Filter = apply;
331+
repo.SearchCommitContext.SetLiteralFilter(apply);
332332
TxtSearchCommitsBox.CaretIndex = apply.Length;
333333
}
334334

@@ -351,7 +351,7 @@ private void OnSearchSuggestionTapped(object sender, TappedEventArgs e)
351351
else if (ctx is Models.User user)
352352
{
353353
var apply = user.ToString();
354-
repo.SearchCommitContext.Filter = apply;
354+
repo.SearchCommitContext.SetLiteralFilter(apply);
355355
TxtSearchCommitsBox.CaretIndex = apply.Length;
356356
}
357357

0 commit comments

Comments
 (0)