refactor: re-write commit searching (part 2)

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo 2025-03-12 11:05:19 +08:00
parent 231f3bf668
commit ee7ccc0391
No known key found for this signature in database
4 changed files with 73 additions and 74 deletions

View file

@ -8,6 +8,7 @@ namespace SourceGit.Models
{ {
public enum CommitSearchMethod public enum CommitSearchMethod
{ {
BySHA = 0,
ByAuthor, ByAuthor,
ByCommitter, ByCommitter,
ByMessage, ByMessage,

View file

@ -148,14 +148,14 @@ namespace SourceGit.ViewModels
{ {
if (commits.Count == 0) if (commits.Count == 0)
{ {
_repo.SearchResultSelectedCommit = null; _repo.SelectedSearchedCommit = null;
DetailContext = null; DetailContext = null;
} }
else if (commits.Count == 1) else if (commits.Count == 1)
{ {
var commit = (commits[0] as Models.Commit)!; var commit = (commits[0] as Models.Commit)!;
if (_repo.SearchResultSelectedCommit == null || _repo.SearchResultSelectedCommit.SHA != commit.SHA) if (_repo.SelectedSearchedCommit == null || _repo.SelectedSearchedCommit.SHA != commit.SHA)
_repo.SearchResultSelectedCommit = _repo.SearchedCommits.Find(x => x.SHA == commit.SHA); _repo.SelectedSearchedCommit = _repo.SearchedCommits.Find(x => x.SHA == commit.SHA);
AutoSelectedCommit = commit; AutoSelectedCommit = commit;
NavigationId = _navigationId + 1; NavigationId = _navigationId + 1;
@ -173,7 +173,7 @@ namespace SourceGit.ViewModels
} }
else if (commits.Count == 2) else if (commits.Count == 2)
{ {
_repo.SearchResultSelectedCommit = null; _repo.SelectedSearchedCommit = null;
var end = commits[0] as Models.Commit; var end = commits[0] as Models.Commit;
var start = commits[1] as Models.Commit; var start = commits[1] as Models.Commit;
@ -181,7 +181,7 @@ namespace SourceGit.ViewModels
} }
else else
{ {
_repo.SearchResultSelectedCommit = null; _repo.SelectedSearchedCommit = null;
DetailContext = commits.Count; DetailContext = commits.Count;
} }
} }
@ -599,7 +599,7 @@ namespace SourceGit.ViewModels
var head = _commits.Find(x => x.SHA == current.Head); var head = _commits.Find(x => x.SHA == current.Head);
if (head == null) if (head == null)
{ {
_repo.SearchResultSelectedCommit = null; _repo.SelectedSearchedCommit = null;
head = new Commands.QuerySingleCommit(_repo.FullPath, current.Head).Result(); head = new Commands.QuerySingleCommit(_repo.FullPath, current.Head).Result();
if (head != null) if (head != null)
DetailContext = new RevisionCompare(_repo.FullPath, commit, head); DetailContext = new RevisionCompare(_repo.FullPath, commit, head);

View file

@ -268,16 +268,19 @@ namespace SourceGit.ViewModels
{ {
if (SetProperty(ref _isSearching, value)) if (SetProperty(ref _isSearching, value))
{ {
SearchedCommits = new List<Models.Commit>();
SearchCommitFilter = string.Empty;
MatchedFilesForSearching = null;
_worktreeFiles = null;
if (value) if (value)
{ {
SelectedViewIndex = 0; SelectedViewIndex = 0;
CalcWorktreeFilesForSearching(); CalcWorktreeFilesForSearching();
} }
else
{
SearchedCommits = new List<Models.Commit>();
SelectedSearchedCommit = null;
SearchCommitFilter = string.Empty;
MatchedFilesForSearching = null;
_worktreeFiles = null;
}
} }
} }
} }
@ -306,7 +309,6 @@ namespace SourceGit.ViewModels
if (SetProperty(ref _searchCommitFilterType, value)) if (SetProperty(ref _searchCommitFilterType, value))
{ {
CalcWorktreeFilesForSearching(); CalcWorktreeFilesForSearching();
if (!string.IsNullOrEmpty(_searchCommitFilter)) if (!string.IsNullOrEmpty(_searchCommitFilter))
StartSearchCommits(); StartSearchCommits();
} }
@ -318,13 +320,8 @@ namespace SourceGit.ViewModels
get => _searchCommitFilter; get => _searchCommitFilter;
set set
{ {
if (SetProperty(ref _searchCommitFilter, value)) if (SetProperty(ref _searchCommitFilter, value) && IsSearchingCommitsByFilePath())
{
if (_searchCommitFilterType == 4 && value is { Length: > 2 })
CalcMatchedFilesForSearching(); CalcMatchedFilesForSearching();
else if (_matchedFilesForSearching is { })
MatchedFilesForSearching = null;
}
} }
} }
@ -340,6 +337,16 @@ namespace SourceGit.ViewModels
set => SetProperty(ref _searchedCommits, value); set => SetProperty(ref _searchedCommits, value);
} }
public Models.Commit SelectedSearchedCommit
{
get => _selectedSearchedCommit;
set
{
if (SetProperty(ref _selectedSearchedCommit, value) && value != null)
NavigateToCommit(value.SHA);
}
}
public bool IsLocalBranchGroupExpanded public bool IsLocalBranchGroupExpanded
{ {
get => _settings.IsLocalBranchesExpandedInSideBar; get => _settings.IsLocalBranchesExpandedInSideBar;
@ -410,16 +417,6 @@ namespace SourceGit.ViewModels
get => _workingCopy?.InProgressContext; get => _workingCopy?.InProgressContext;
} }
public Models.Commit SearchResultSelectedCommit
{
get => _searchResultSelectedCommit;
set
{
if (SetProperty(ref _searchResultSelectedCommit, value) && value != null)
NavigateToCommit(value.SHA);
}
}
public bool IsAutoFetching public bool IsAutoFetching
{ {
get => _isAutoFetching; get => _isAutoFetching;
@ -523,6 +520,7 @@ namespace SourceGit.ViewModels
_submodules.Clear(); _submodules.Clear();
_visibleSubmodules.Clear(); _visibleSubmodules.Clear();
_searchedCommits.Clear(); _searchedCommits.Clear();
_selectedSearchedCommit = null;
_worktreeFiles = null; _worktreeFiles = null;
_matchedFilesForSearching = null; _matchedFilesForSearching = null;
@ -707,32 +705,22 @@ namespace SourceGit.ViewModels
return; return;
IsSearchLoadingVisible = true; IsSearchLoadingVisible = true;
SearchResultSelectedCommit = null; SelectedSearchedCommit = null;
MatchedFilesForSearching = null; MatchedFilesForSearching = null;
Task.Run(() => Task.Run(() =>
{ {
var visible = new List<Models.Commit>(); var visible = null as List<Models.Commit>;
var method = (Models.CommitSearchMethod)_searchCommitFilterType;
switch (_searchCommitFilterType) if (method == Models.CommitSearchMethod.BySHA)
{ {
case 0:
var commit = new Commands.QuerySingleCommit(_fullpath, _searchCommitFilter).Result(); var commit = new Commands.QuerySingleCommit(_fullpath, _searchCommitFilter).Result();
if (commit != null) visible = commit == null ? [] : [commit];
visible.Add(commit); }
break; else
case 1: {
visible = new Commands.QueryCommits(_fullpath, _searchCommitFilter, Models.CommitSearchMethod.ByAuthor, _onlySearchCommitsInCurrentBranch).Result(); visible = new Commands.QueryCommits(_fullpath, _searchCommitFilter, method, _onlySearchCommitsInCurrentBranch).Result();
break;
case 2:
visible = new Commands.QueryCommits(_fullpath, _searchCommitFilter, Models.CommitSearchMethod.ByCommitter, _onlySearchCommitsInCurrentBranch).Result();
break;
case 3:
visible = new Commands.QueryCommits(_fullpath, _searchCommitFilter, Models.CommitSearchMethod.ByMessage, _onlySearchCommitsInCurrentBranch).Result();
break;
case 4:
visible = new Commands.QueryCommits(_fullpath, _searchCommitFilter, Models.CommitSearchMethod.ByFile, _onlySearchCommitsInCurrentBranch).Result();
break;
} }
Dispatcher.UIThread.Invoke(() => Dispatcher.UIThread.Invoke(() =>
@ -1636,7 +1624,7 @@ namespace SourceGit.ViewModels
compareWithWorktree.Icon = App.CreateMenuIcon("Icons.Compare"); compareWithWorktree.Icon = App.CreateMenuIcon("Icons.Compare");
compareWithWorktree.Click += (_, _) => compareWithWorktree.Click += (_, _) =>
{ {
SearchResultSelectedCommit = null; SelectedSearchedCommit = null;
if (_histories != null) if (_histories != null)
{ {
@ -1918,7 +1906,7 @@ namespace SourceGit.ViewModels
compareWithWorktree.Icon = App.CreateMenuIcon("Icons.Compare"); compareWithWorktree.Icon = App.CreateMenuIcon("Icons.Compare");
compareWithWorktree.Click += (_, _) => compareWithWorktree.Click += (_, _) =>
{ {
SearchResultSelectedCommit = null; SelectedSearchedCommit = null;
if (_histories != null) if (_histories != null)
{ {
@ -2371,35 +2359,45 @@ namespace SourceGit.ViewModels
menu.Items.Add(new MenuItem() { Header = "-" }); menu.Items.Add(new MenuItem() { Header = "-" });
} }
private bool IsSearchingCommitsByFilePath()
{
return _isSearching && _searchCommitFilterType == (int)Models.CommitSearchMethod.ByFile;
}
private void CalcWorktreeFilesForSearching() private void CalcWorktreeFilesForSearching()
{ {
_worktreeFiles = null; if (!IsSearchingCommitsByFilePath())
if (_searchCommitFilterType == 4)
{ {
_worktreeFiles = null;
MatchedFilesForSearching = null;
GC.Collect();
return;
}
Task.Run(() => Task.Run(() =>
{ {
var files = new Commands.QueryRevisionFileNames(_fullpath, "HEAD").Result(); var files = new Commands.QueryRevisionFileNames(_fullpath, "HEAD").Result();
Dispatcher.UIThread.Invoke(() => Dispatcher.UIThread.Invoke(() =>
{ {
if (_searchCommitFilterType != 4) if (!IsSearchingCommitsByFilePath())
return; return;
_worktreeFiles = new List<string>(); _worktreeFiles = new List<string>();
foreach (var f in files) foreach (var f in files)
_worktreeFiles.Add(f); _worktreeFiles.Add(f);
if (_searchCommitFilter is { Length: > 2 })
CalcMatchedFilesForSearching(); CalcMatchedFilesForSearching();
}); });
}); });
} }
}
private void CalcMatchedFilesForSearching() private void CalcMatchedFilesForSearching()
{ {
if (_worktreeFiles == null || _worktreeFiles.Count == 0) if (_worktreeFiles == null || _worktreeFiles.Count == 0 || _searchCommitFilter.Length < 3)
{
MatchedFilesForSearching = null;
return; return;
}
var matched = new List<string>(); var matched = new List<string>();
foreach (var file in _worktreeFiles) foreach (var file in _worktreeFiles)
@ -2461,11 +2459,11 @@ namespace SourceGit.ViewModels
private bool _isSearching = false; private bool _isSearching = false;
private bool _isSearchLoadingVisible = false; private bool _isSearchLoadingVisible = false;
private int _searchCommitFilterType = 3; private int _searchCommitFilterType = (int)Models.CommitSearchMethod.ByMessage;
private bool _onlySearchCommitsInCurrentBranch = false; private bool _onlySearchCommitsInCurrentBranch = false;
private string _searchCommitFilter = string.Empty; private string _searchCommitFilter = string.Empty;
private List<Models.Commit> _searchedCommits = new List<Models.Commit>(); private List<Models.Commit> _searchedCommits = new List<Models.Commit>();
private Models.Commit _searchResultSelectedCommit = null; private Models.Commit _selectedSearchedCommit = null;
private List<string> _worktreeFiles = null; private List<string> _worktreeFiles = null;
private List<string> _matchedFilesForSearching = null; private List<string> _matchedFilesForSearching = null;

View file

@ -504,7 +504,7 @@
Margin="0,8,0,0" Margin="0,8,0,0"
ItemsSource="{Binding SearchedCommits}" ItemsSource="{Binding SearchedCommits}"
SelectionMode="Single" SelectionMode="Single"
SelectedItem="{Binding SearchResultSelectedCommit, Mode=TwoWay}" SelectedItem="{Binding SelectedSearchedCommit, Mode=TwoWay}"
BorderThickness="1" BorderThickness="1"
BorderBrush="{DynamicResource Brush.Border2}" BorderBrush="{DynamicResource Brush.Border2}"
Background="{DynamicResource Brush.Contents}" Background="{DynamicResource Brush.Contents}"