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
This commit is contained in:
leo 2024-08-28 16:56:43 +08:00
parent 48e1de809b
commit c90abd0ca2
No known key found for this signature in database
3 changed files with 24 additions and 22 deletions

View file

@ -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;
}

View file

@ -6,6 +6,13 @@ using Avalonia.Media;
namespace SourceGit.Models
{
public enum CommitSearchMethod
{
ByUser,
ByMessage,
ByFile,
}
public class Commit
{
public static double OpacityForNotMerged

View file

@ -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;
}