From c90abd0ca2a12bd313c03a983ce969bdf4ae77fc Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 28 Aug 2024 16:56:43 +0800 Subject: [PATCH] refactor: commit search * no longer depends on the commits displayed in histories view * limit the number of commits returned by all search methods to a maximum of 1000 --- src/Commands/QueryCommits.cs | 15 ++++++++++----- src/Models/Commit.cs | 7 +++++++ src/ViewModels/Repository.cs | 24 +++++++----------------- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/Commands/QueryCommits.cs b/src/Commands/QueryCommits.cs index ef80566e..8a48eff3 100644 --- a/src/Commands/QueryCommits.cs +++ b/src/Commands/QueryCommits.cs @@ -14,17 +14,22 @@ namespace SourceGit.Commands _findFirstMerged = needFindHead; } - public QueryCommits(string repo, int maxCount, string messageFilter, bool isFile) + public QueryCommits(string repo, string filter, Models.CommitSearchMethod method) { string search; - if (isFile) + + if (method == Models.CommitSearchMethod.ByUser) { - search = $"-- \"{messageFilter}\""; + search = $"-i --author=\"{filter}\" --committer=\"{filter}\""; + } + else if (method == Models.CommitSearchMethod.ByFile) + { + search = $"-- \"{filter}\""; } else { var argsBuilder = new StringBuilder(); - var words = messageFilter.Split(new[] { ' ', '\t', '\r' }, StringSplitOptions.RemoveEmptyEntries); + var words = filter.Split(new[] { ' ', '\t', '\r' }, StringSplitOptions.RemoveEmptyEntries); foreach (var word in words) { var escaped = word.Trim().Replace("\"", "\\\"", StringComparison.Ordinal); @@ -36,7 +41,7 @@ namespace SourceGit.Commands WorkingDirectory = repo; Context = repo; - Args = $"log -{maxCount} --date-order --no-show-signature --decorate=full --pretty=format:%H%n%P%n%D%n%aN±%aE%n%at%n%cN±%cE%n%ct%n%s --branches --remotes " + search; + Args = $"log -1000 --date-order --no-show-signature --decorate=full --pretty=format:%H%n%P%n%D%n%aN±%aE%n%at%n%cN±%cE%n%ct%n%s --branches --remotes " + search; _findFirstMerged = false; } diff --git a/src/Models/Commit.cs b/src/Models/Commit.cs index 6a199478..9549e4a4 100644 --- a/src/Models/Commit.cs +++ b/src/Models/Commit.cs @@ -6,6 +6,13 @@ using Avalonia.Media; namespace SourceGit.Models { + public enum CommitSearchMethod + { + ByUser, + ByMessage, + ByFile, + } + public class Commit { public static double OpacityForNotMerged diff --git a/src/ViewModels/Repository.cs b/src/ViewModels/Repository.cs index c63243ee..4d822a40 100644 --- a/src/ViewModels/Repository.cs +++ b/src/ViewModels/Repository.cs @@ -548,6 +548,7 @@ namespace SourceGit.ViewModels return; IsSearchLoadingVisible = true; + SearchResultSelectedCommit = null; IsSearchCommitSuggestionOpen = false; SearchCommitFilterSuggestion.Clear(); @@ -558,29 +559,18 @@ namespace SourceGit.ViewModels switch (_searchCommitFilterType) { case 0: - foreach (var c in _histories.Commits) - { - if (c.SHA.Contains(_searchCommitFilter, StringComparison.OrdinalIgnoreCase)) - visible.Add(c); - } - + var commit = new Commands.QuerySingleCommit(_fullpath, _searchCommitFilter).Result(); + if (commit != null) + visible.Add(commit); break; case 1: - foreach (var c in _histories.Commits) - { - if (c.Author.Name.Contains(_searchCommitFilter, StringComparison.OrdinalIgnoreCase) - || c.Committer.Name.Contains(_searchCommitFilter, StringComparison.OrdinalIgnoreCase) - || c.Author.Email.Contains(_searchCommitFilter, StringComparison.OrdinalIgnoreCase) - || c.Committer.Email.Contains(_searchCommitFilter, StringComparison.OrdinalIgnoreCase)) - visible.Add(c); - } - + visible = new Commands.QueryCommits(_fullpath, _searchCommitFilter, Models.CommitSearchMethod.ByUser).Result(); break; case 2: - visible = new Commands.QueryCommits(_fullpath, 1000, _searchCommitFilter, false).Result(); + visible = new Commands.QueryCommits(_fullpath, _searchCommitFilter, Models.CommitSearchMethod.ByMessage).Result(); break; case 3: - visible = new Commands.QueryCommits(_fullpath, 1000, _searchCommitFilter, true).Result(); + visible = new Commands.QueryCommits(_fullpath, _searchCommitFilter, Models.CommitSearchMethod.ByFile).Result(); break; }