Skip to content

Commit 75c667f

Browse files
committed
refactor: move searching commit code from Repository to SearchCommitContext and keep inputs remained while repo is opened (#1884)
Signed-off-by: leo <longshuang@msn.cn>
1 parent 642f576 commit 75c667f

File tree

5 files changed

+283
-252
lines changed

5 files changed

+283
-252
lines changed

src/ViewModels/Histories.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,14 @@ public void Select(IList commits)
157157
{
158158
if (commits.Count == 0)
159159
{
160-
_repo.SelectedSearchedCommit = null;
160+
_repo.SearchCommitContext.Selected = null;
161161
DetailContext = null;
162162
}
163163
else if (commits.Count == 1)
164164
{
165165
var commit = (commits[0] as Models.Commit)!;
166-
if (_repo.SelectedSearchedCommit == null || _repo.SelectedSearchedCommit.SHA != commit.SHA)
167-
_repo.SelectedSearchedCommit = _repo.SearchedCommits.Find(x => x.SHA == commit.SHA);
166+
if (_repo.SearchCommitContext.Selected == null || _repo.SearchCommitContext.Selected.SHA != commit.SHA)
167+
_repo.SearchCommitContext.Selected = _repo.SearchCommitContext.Results?.Find(x => x.SHA == commit.SHA);
168168

169169
AutoSelectedCommit = commit;
170170
NavigationId = _navigationId + 1;
@@ -182,15 +182,15 @@ public void Select(IList commits)
182182
}
183183
else if (commits.Count == 2)
184184
{
185-
_repo.SelectedSearchedCommit = null;
185+
_repo.SearchCommitContext.Selected = null;
186186

187187
var end = commits[0] as Models.Commit;
188188
var start = commits[1] as Models.Commit;
189189
DetailContext = new RevisionCompare(_repo.FullPath, start, end);
190190
}
191191
else
192192
{
193-
_repo.SelectedSearchedCommit = null;
193+
_repo.SearchCommitContext.Selected = null;
194194
DetailContext = new Models.Count(commits.Count);
195195
}
196196
}
@@ -379,7 +379,7 @@ public async Task<string> GetCommitFullMessageAsync(Models.Commit commit)
379379
var head = _commits.Find(x => x.IsCurrentHead);
380380
if (head == null)
381381
{
382-
_repo.SelectedSearchedCommit = null;
382+
_repo.SearchCommitContext.Selected = null;
383383
head = await new Commands.QuerySingleCommit(_repo.FullPath, "HEAD").GetResultAsync();
384384
if (head != null)
385385
DetailContext = new RevisionCompare(_repo.FullPath, commit, head);

src/ViewModels/Repository.cs

Lines changed: 7 additions & 222 deletions
Original file line numberDiff line numberDiff line change
@@ -271,83 +271,16 @@ public bool IsSearching
271271
if (SetProperty(ref _isSearching, value))
272272
{
273273
if (value)
274-
{
275274
SelectedViewIndex = 0;
276-
CalcWorktreeFilesForSearching();
277-
}
278275
else
279-
{
280-
SearchedCommits = new List<Models.Commit>();
281-
SelectedSearchedCommit = null;
282-
SearchCommitFilter = string.Empty;
283-
MatchedFilesForSearching = null;
284-
_requestingWorktreeFiles = false;
285-
_worktreeFiles = null;
286-
}
287-
}
288-
}
289-
}
290-
291-
public bool IsSearchLoadingVisible
292-
{
293-
get => _isSearchLoadingVisible;
294-
private set => SetProperty(ref _isSearchLoadingVisible, value);
295-
}
296-
297-
public bool OnlySearchCommitsInCurrentBranch
298-
{
299-
get => _onlySearchCommitsInCurrentBranch;
300-
set
301-
{
302-
if (SetProperty(ref _onlySearchCommitsInCurrentBranch, value) && !string.IsNullOrEmpty(_searchCommitFilter))
303-
StartSearchCommits();
304-
}
305-
}
306-
307-
public int SearchCommitFilterType
308-
{
309-
get => _searchCommitFilterType;
310-
set
311-
{
312-
if (SetProperty(ref _searchCommitFilterType, value))
313-
{
314-
CalcWorktreeFilesForSearching();
315-
if (!string.IsNullOrEmpty(_searchCommitFilter))
316-
StartSearchCommits();
276+
_searchCommitContext.EndSearch();
317277
}
318278
}
319279
}
320280

321-
public string SearchCommitFilter
281+
public SearchCommitContext SearchCommitContext
322282
{
323-
get => _searchCommitFilter;
324-
set
325-
{
326-
if (SetProperty(ref _searchCommitFilter, value) && IsSearchingCommitsByFilePath())
327-
CalcMatchedFilesForSearching();
328-
}
329-
}
330-
331-
public List<string> MatchedFilesForSearching
332-
{
333-
get => _matchedFilesForSearching;
334-
private set => SetProperty(ref _matchedFilesForSearching, value);
335-
}
336-
337-
public List<Models.Commit> SearchedCommits
338-
{
339-
get => _searchedCommits;
340-
set => SetProperty(ref _searchedCommits, value);
341-
}
342-
343-
public Models.Commit SelectedSearchedCommit
344-
{
345-
get => _selectedSearchedCommit;
346-
set
347-
{
348-
if (SetProperty(ref _selectedSearchedCommit, value) && value != null)
349-
NavigateToCommit(value.SHA);
350-
}
283+
get => _searchCommitContext;
351284
}
352285

353286
public bool IsLocalBranchGroupExpanded
@@ -562,6 +495,7 @@ public void Open()
562495
_histories = new Histories(this);
563496
_workingCopy = new WorkingCopy(this) { CommitMessage = _settings.LastCommitMessage };
564497
_stashesPage = new StashesPage(this);
498+
_searchCommitContext = new SearchCommitContext(this);
565499

566500
if (Preferences.Instance.ShowLocalChangesByDefault)
567501
{
@@ -633,6 +567,7 @@ public void Close()
633567
_histories.Dispose();
634568
_workingCopy.Dispose();
635569
_stashesPage.Dispose();
570+
_searchCommitContext.Dispose();
636571

637572
_watcher = null;
638573
_histories = null;
@@ -650,12 +585,6 @@ public void Close()
650585
_visibleTags = null;
651586
_submodules.Clear();
652587
_visibleSubmodules = null;
653-
_searchedCommits.Clear();
654-
_selectedSearchedCommit = null;
655-
656-
_requestingWorktreeFiles = false;
657-
_worktreeFiles = null;
658-
_matchedFilesForSearching = null;
659588
}
660589

661590
public bool CanCreatePopup()
@@ -904,83 +833,6 @@ public void ClearFilter()
904833
Filter = string.Empty;
905834
}
906835

907-
public void ClearSearchCommitFilter()
908-
{
909-
SearchCommitFilter = string.Empty;
910-
}
911-
912-
public void ClearMatchedFilesForSearching()
913-
{
914-
MatchedFilesForSearching = null;
915-
}
916-
917-
public void StartSearchCommits()
918-
{
919-
if (_histories == null)
920-
return;
921-
922-
IsSearchLoadingVisible = true;
923-
SelectedSearchedCommit = null;
924-
MatchedFilesForSearching = null;
925-
926-
Task.Run(async () =>
927-
{
928-
var visible = new List<Models.Commit>();
929-
var method = (Models.CommitSearchMethod)_searchCommitFilterType;
930-
931-
if (method == Models.CommitSearchMethod.BySHA)
932-
{
933-
var isCommitSHA = await new Commands.IsCommitSHA(FullPath, _searchCommitFilter)
934-
.GetResultAsync()
935-
.ConfigureAwait(false);
936-
937-
if (isCommitSHA)
938-
{
939-
var commit = await new Commands.QuerySingleCommit(FullPath, _searchCommitFilter)
940-
.GetResultAsync()
941-
.ConfigureAwait(false);
942-
943-
commit.IsMerged = await new Commands.IsAncestor(FullPath, commit.SHA, "HEAD")
944-
.GetResultAsync()
945-
.ConfigureAwait(false);
946-
947-
visible.Add(commit);
948-
}
949-
}
950-
else if (_onlySearchCommitsInCurrentBranch)
951-
{
952-
visible = await new Commands.QueryCommits(FullPath, _searchCommitFilter, method, true)
953-
.GetResultAsync()
954-
.ConfigureAwait(false);
955-
956-
foreach (var c in visible)
957-
c.IsMerged = true;
958-
}
959-
else
960-
{
961-
visible = await new Commands.QueryCommits(FullPath, _searchCommitFilter, method, false)
962-
.GetResultAsync()
963-
.ConfigureAwait(false);
964-
965-
if (visible.Count > 0)
966-
{
967-
var set = await new Commands.QueryCurrentBranchCommitHashes(FullPath, visible[^1].CommitterTime)
968-
.GetResultAsync()
969-
.ConfigureAwait(false);
970-
971-
foreach (var c in visible)
972-
c.IsMerged = set.Contains(c.SHA);
973-
}
974-
}
975-
976-
Dispatcher.UIThread.Post(() =>
977-
{
978-
SearchedCommits = visible;
979-
IsSearchLoadingVisible = false;
980-
});
981-
});
982-
}
983-
984836
public IDisposable LockWatcher()
985837
{
986838
return _watcher?.Lock();
@@ -1559,7 +1411,7 @@ public async Task CompareBranchWithWorktreeAsync(Models.Branch branch)
15591411
{
15601412
if (_histories != null)
15611413
{
1562-
SelectedSearchedCommit = null;
1414+
_searchCommitContext.Selected = null;
15631415

15641416
var target = await new Commands.QuerySingleCommit(FullPath, branch.Head).GetResultAsync();
15651417
_histories.AutoSelectedCommit = null;
@@ -1956,65 +1808,6 @@ private BranchTreeNode FindBranchNode(List<BranchTreeNode> nodes, string path)
19561808
return null;
19571809
}
19581810

1959-
private bool IsSearchingCommitsByFilePath()
1960-
{
1961-
return _isSearching && _searchCommitFilterType == (int)Models.CommitSearchMethod.ByPath;
1962-
}
1963-
1964-
private void CalcWorktreeFilesForSearching()
1965-
{
1966-
if (!IsSearchingCommitsByFilePath())
1967-
{
1968-
_requestingWorktreeFiles = false;
1969-
_worktreeFiles = null;
1970-
MatchedFilesForSearching = null;
1971-
GC.Collect();
1972-
return;
1973-
}
1974-
1975-
if (_requestingWorktreeFiles)
1976-
return;
1977-
1978-
_requestingWorktreeFiles = true;
1979-
1980-
Task.Run(async () =>
1981-
{
1982-
_worktreeFiles = await new Commands.QueryRevisionFileNames(FullPath, "HEAD")
1983-
.GetResultAsync()
1984-
.ConfigureAwait(false);
1985-
1986-
Dispatcher.UIThread.Post(() =>
1987-
{
1988-
if (IsSearchingCommitsByFilePath() && _requestingWorktreeFiles)
1989-
CalcMatchedFilesForSearching();
1990-
1991-
_requestingWorktreeFiles = false;
1992-
});
1993-
});
1994-
}
1995-
1996-
private void CalcMatchedFilesForSearching()
1997-
{
1998-
if (_worktreeFiles == null || _worktreeFiles.Count == 0 || _searchCommitFilter.Length < 3)
1999-
{
2000-
MatchedFilesForSearching = null;
2001-
return;
2002-
}
2003-
2004-
var matched = new List<string>();
2005-
foreach (var file in _worktreeFiles)
2006-
{
2007-
if (file.Contains(_searchCommitFilter, StringComparison.OrdinalIgnoreCase) && file.Length != _searchCommitFilter.Length)
2008-
{
2009-
matched.Add(file);
2010-
if (matched.Count > 100)
2011-
break;
2012-
}
2013-
}
2014-
2015-
MatchedFilesForSearching = matched;
2016-
}
2017-
20181811
private void FetchInBackground(object sender)
20191812
{
20201813
Dispatcher.UIThread.Invoke(async Task () =>
@@ -2081,15 +1874,7 @@ private void FetchInBackground(object sender)
20811874
private int _stashesCount = 0;
20821875

20831876
private bool _isSearching = false;
2084-
private bool _isSearchLoadingVisible = false;
2085-
private int _searchCommitFilterType = (int)Models.CommitSearchMethod.ByMessage;
2086-
private bool _onlySearchCommitsInCurrentBranch = false;
2087-
private string _searchCommitFilter = string.Empty;
2088-
private List<Models.Commit> _searchedCommits = [];
2089-
private Models.Commit _selectedSearchedCommit = null;
2090-
private bool _requestingWorktreeFiles = false;
2091-
private List<string> _worktreeFiles = null;
2092-
private List<string> _matchedFilesForSearching = null;
1877+
private SearchCommitContext _searchCommitContext = null;
20931878

20941879
private string _filter = string.Empty;
20951880
private List<Models.Remote> _remotes = [];

0 commit comments

Comments
 (0)