diff --git a/src/Models/Commit.cs b/src/Models/Commit.cs index 5c48b0c0..72ce54bd 100644 --- a/src/Models/Commit.cs +++ b/src/Models/Commit.cs @@ -8,6 +8,7 @@ namespace SourceGit.Models { public enum CommitSearchMethod { + BySHA = 0, ByAuthor, ByCommitter, ByMessage, diff --git a/src/ViewModels/Histories.cs b/src/ViewModels/Histories.cs index 707e8c43..0e67915c 100644 --- a/src/ViewModels/Histories.cs +++ b/src/ViewModels/Histories.cs @@ -148,14 +148,14 @@ namespace SourceGit.ViewModels { if (commits.Count == 0) { - _repo.SearchResultSelectedCommit = null; + _repo.SelectedSearchedCommit = null; DetailContext = null; } else if (commits.Count == 1) { var commit = (commits[0] as Models.Commit)!; - if (_repo.SearchResultSelectedCommit == null || _repo.SearchResultSelectedCommit.SHA != commit.SHA) - _repo.SearchResultSelectedCommit = _repo.SearchedCommits.Find(x => x.SHA == commit.SHA); + if (_repo.SelectedSearchedCommit == null || _repo.SelectedSearchedCommit.SHA != commit.SHA) + _repo.SelectedSearchedCommit = _repo.SearchedCommits.Find(x => x.SHA == commit.SHA); AutoSelectedCommit = commit; NavigationId = _navigationId + 1; @@ -173,7 +173,7 @@ namespace SourceGit.ViewModels } else if (commits.Count == 2) { - _repo.SearchResultSelectedCommit = null; + _repo.SelectedSearchedCommit = null; var end = commits[0] as Models.Commit; var start = commits[1] as Models.Commit; @@ -181,7 +181,7 @@ namespace SourceGit.ViewModels } else { - _repo.SearchResultSelectedCommit = null; + _repo.SelectedSearchedCommit = null; DetailContext = commits.Count; } } @@ -599,7 +599,7 @@ namespace SourceGit.ViewModels var head = _commits.Find(x => x.SHA == current.Head); if (head == null) { - _repo.SearchResultSelectedCommit = null; + _repo.SelectedSearchedCommit = null; head = new Commands.QuerySingleCommit(_repo.FullPath, current.Head).Result(); if (head != null) DetailContext = new RevisionCompare(_repo.FullPath, commit, head); diff --git a/src/ViewModels/Repository.cs b/src/ViewModels/Repository.cs index 59ab6a1b..c7557356 100644 --- a/src/ViewModels/Repository.cs +++ b/src/ViewModels/Repository.cs @@ -268,16 +268,19 @@ namespace SourceGit.ViewModels { if (SetProperty(ref _isSearching, value)) { - SearchedCommits = new List(); - SearchCommitFilter = string.Empty; - MatchedFilesForSearching = null; - _worktreeFiles = null; - if (value) { SelectedViewIndex = 0; CalcWorktreeFilesForSearching(); } + else + { + SearchedCommits = new List(); + SelectedSearchedCommit = null; + SearchCommitFilter = string.Empty; + MatchedFilesForSearching = null; + _worktreeFiles = null; + } } } } @@ -306,7 +309,6 @@ namespace SourceGit.ViewModels if (SetProperty(ref _searchCommitFilterType, value)) { CalcWorktreeFilesForSearching(); - if (!string.IsNullOrEmpty(_searchCommitFilter)) StartSearchCommits(); } @@ -318,13 +320,8 @@ namespace SourceGit.ViewModels get => _searchCommitFilter; set { - if (SetProperty(ref _searchCommitFilter, value)) - { - if (_searchCommitFilterType == 4 && value is { Length: > 2 }) - CalcMatchedFilesForSearching(); - else if (_matchedFilesForSearching is { }) - MatchedFilesForSearching = null; - } + if (SetProperty(ref _searchCommitFilter, value) && IsSearchingCommitsByFilePath()) + CalcMatchedFilesForSearching(); } } @@ -340,6 +337,16 @@ namespace SourceGit.ViewModels 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 { get => _settings.IsLocalBranchesExpandedInSideBar; @@ -410,16 +417,6 @@ namespace SourceGit.ViewModels get => _workingCopy?.InProgressContext; } - public Models.Commit SearchResultSelectedCommit - { - get => _searchResultSelectedCommit; - set - { - if (SetProperty(ref _searchResultSelectedCommit, value) && value != null) - NavigateToCommit(value.SHA); - } - } - public bool IsAutoFetching { get => _isAutoFetching; @@ -523,6 +520,7 @@ namespace SourceGit.ViewModels _submodules.Clear(); _visibleSubmodules.Clear(); _searchedCommits.Clear(); + _selectedSearchedCommit = null; _worktreeFiles = null; _matchedFilesForSearching = null; @@ -707,32 +705,22 @@ namespace SourceGit.ViewModels return; IsSearchLoadingVisible = true; - SearchResultSelectedCommit = null; + SelectedSearchedCommit = null; MatchedFilesForSearching = null; Task.Run(() => { - var visible = new List(); + var visible = null as List; + var method = (Models.CommitSearchMethod)_searchCommitFilterType; - switch (_searchCommitFilterType) + if (method == Models.CommitSearchMethod.BySHA) { - case 0: - var commit = new Commands.QuerySingleCommit(_fullpath, _searchCommitFilter).Result(); - if (commit != null) - visible.Add(commit); - break; - case 1: - visible = new Commands.QueryCommits(_fullpath, _searchCommitFilter, Models.CommitSearchMethod.ByAuthor, _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; + var commit = new Commands.QuerySingleCommit(_fullpath, _searchCommitFilter).Result(); + visible = commit == null ? [] : [commit]; + } + else + { + visible = new Commands.QueryCommits(_fullpath, _searchCommitFilter, method, _onlySearchCommitsInCurrentBranch).Result(); } Dispatcher.UIThread.Invoke(() => @@ -1636,7 +1624,7 @@ namespace SourceGit.ViewModels compareWithWorktree.Icon = App.CreateMenuIcon("Icons.Compare"); compareWithWorktree.Click += (_, _) => { - SearchResultSelectedCommit = null; + SelectedSearchedCommit = null; if (_histories != null) { @@ -1918,7 +1906,7 @@ namespace SourceGit.ViewModels compareWithWorktree.Icon = App.CreateMenuIcon("Icons.Compare"); compareWithWorktree.Click += (_, _) => { - SearchResultSelectedCommit = null; + SelectedSearchedCommit = null; if (_histories != null) { @@ -2371,35 +2359,45 @@ namespace SourceGit.ViewModels menu.Items.Add(new MenuItem() { Header = "-" }); } + private bool IsSearchingCommitsByFilePath() + { + return _isSearching && _searchCommitFilterType == (int)Models.CommitSearchMethod.ByFile; + } + private void CalcWorktreeFilesForSearching() { - _worktreeFiles = null; - - if (_searchCommitFilterType == 4) + if (!IsSearchingCommitsByFilePath()) { - Task.Run(() => - { - var files = new Commands.QueryRevisionFileNames(_fullpath, "HEAD").Result(); - Dispatcher.UIThread.Invoke(() => - { - if (_searchCommitFilterType != 4) - return; - - _worktreeFiles = new List(); - foreach (var f in files) - _worktreeFiles.Add(f); - - if (_searchCommitFilter is { Length: > 2 }) - CalcMatchedFilesForSearching(); - }); - }); + _worktreeFiles = null; + MatchedFilesForSearching = null; + GC.Collect(); + return; } + + Task.Run(() => + { + var files = new Commands.QueryRevisionFileNames(_fullpath, "HEAD").Result(); + Dispatcher.UIThread.Invoke(() => + { + if (!IsSearchingCommitsByFilePath()) + return; + + _worktreeFiles = new List(); + foreach (var f in files) + _worktreeFiles.Add(f); + + CalcMatchedFilesForSearching(); + }); + }); } private void CalcMatchedFilesForSearching() { - if (_worktreeFiles == null || _worktreeFiles.Count == 0) + if (_worktreeFiles == null || _worktreeFiles.Count == 0 || _searchCommitFilter.Length < 3) + { + MatchedFilesForSearching = null; return; + } var matched = new List(); foreach (var file in _worktreeFiles) @@ -2461,11 +2459,11 @@ namespace SourceGit.ViewModels private bool _isSearching = false; private bool _isSearchLoadingVisible = false; - private int _searchCommitFilterType = 3; + private int _searchCommitFilterType = (int)Models.CommitSearchMethod.ByMessage; private bool _onlySearchCommitsInCurrentBranch = false; private string _searchCommitFilter = string.Empty; private List _searchedCommits = new List(); - private Models.Commit _searchResultSelectedCommit = null; + private Models.Commit _selectedSearchedCommit = null; private List _worktreeFiles = null; private List _matchedFilesForSearching = null; diff --git a/src/Views/Repository.axaml b/src/Views/Repository.axaml index 9caac015..1d94e9b2 100644 --- a/src/Views/Repository.axaml +++ b/src/Views/Repository.axaml @@ -504,7 +504,7 @@ Margin="0,8,0,0" ItemsSource="{Binding SearchedCommits}" SelectionMode="Single" - SelectedItem="{Binding SearchResultSelectedCommit, Mode=TwoWay}" + SelectedItem="{Binding SelectedSearchedCommit, Mode=TwoWay}" BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}" Background="{DynamicResource Brush.Contents}"