From 8e7bfc43e097e00edaa2961c00f798a56b167aa4 Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 15 Jul 2024 10:17:26 +0800 Subject: [PATCH] enhance: search commits by message (#256) --- src/App.JsonCodeGen.cs | 1 - src/Commands/QueryCommits.cs | 20 +++++++++++++++++++- src/ViewModels/Repository.cs | 2 +- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/App.JsonCodeGen.cs b/src/App.JsonCodeGen.cs index 82a80c46..ffc74262 100644 --- a/src/App.JsonCodeGen.cs +++ b/src/App.JsonCodeGen.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Text.Json; using System.Text.Json.Serialization; diff --git a/src/Commands/QueryCommits.cs b/src/Commands/QueryCommits.cs index a42a8d75..66a1ee28 100644 --- a/src/Commands/QueryCommits.cs +++ b/src/Commands/QueryCommits.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Text; namespace SourceGit.Commands { @@ -9,10 +10,27 @@ namespace SourceGit.Commands { WorkingDirectory = repo; Context = repo; - Args = $"log --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 " + limits; + Args = "log --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 " + limits; _findFirstMerged = needFindHead; } + public QueryCommits(string repo, int maxCount, string messageFilter) + { + var argsBuilder = new StringBuilder(); + var words = messageFilter.Split(new[] { ' ', '\t', '\r' }, StringSplitOptions.RemoveEmptyEntries); + foreach (var word in words) + { + var escaped = word.Trim().Replace("\"", "\\\"", StringComparison.Ordinal); + argsBuilder.Append($"--grep=\"{escaped}\" "); + } + argsBuilder.Append("--all-match"); + + 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 " + argsBuilder.ToString(); + _findFirstMerged = false; + } + public List Result() { var rs = ReadToEnd(); diff --git a/src/ViewModels/Repository.cs b/src/ViewModels/Repository.cs index 6e0c9dc4..36fd77d9 100644 --- a/src/ViewModels/Repository.cs +++ b/src/ViewModels/Repository.cs @@ -566,7 +566,7 @@ namespace SourceGit.ViewModels break; case 2: - visible = new Commands.QueryCommits(FullPath, $"-1000 --grep=\"{_searchCommitFilter}\"", false).Result(); + visible = new Commands.QueryCommits(FullPath, 1000, _searchCommitFilter).Result(); break; case 3: visible = new Commands.QueryCommits(FullPath, $"-1000 -- \"{_searchCommitFilter}\"", false).Result();