Skip to content

Commit ee7ccc0

Browse files
committed
refactor: re-write commit searching (part 2)
Signed-off-by: leo <[email protected]>
1 parent 231f3bf commit ee7ccc0

File tree

4 files changed

+69
-70
lines changed

4 files changed

+69
-70
lines changed

src/Models/Commit.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace SourceGit.Models
88
{
99
public enum CommitSearchMethod
1010
{
11+
BySHA = 0,
1112
ByAuthor,
1213
ByCommitter,
1314
ByMessage,

src/ViewModels/Histories.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,14 @@ public void Select(IList commits)
148148
{
149149
if (commits.Count == 0)
150150
{
151-
_repo.SearchResultSelectedCommit = null;
151+
_repo.SelectedSearchedCommit = null;
152152
DetailContext = null;
153153
}
154154
else if (commits.Count == 1)
155155
{
156156
var commit = (commits[0] as Models.Commit)!;
157-
if (_repo.SearchResultSelectedCommit == null || _repo.SearchResultSelectedCommit.SHA != commit.SHA)
158-
_repo.SearchResultSelectedCommit = _repo.SearchedCommits.Find(x => x.SHA == commit.SHA);
157+
if (_repo.SelectedSearchedCommit == null || _repo.SelectedSearchedCommit.SHA != commit.SHA)
158+
_repo.SelectedSearchedCommit = _repo.SearchedCommits.Find(x => x.SHA == commit.SHA);
159159

160160
AutoSelectedCommit = commit;
161161
NavigationId = _navigationId + 1;
@@ -173,15 +173,15 @@ public void Select(IList commits)
173173
}
174174
else if (commits.Count == 2)
175175
{
176-
_repo.SearchResultSelectedCommit = null;
176+
_repo.SelectedSearchedCommit = null;
177177

178178
var end = commits[0] as Models.Commit;
179179
var start = commits[1] as Models.Commit;
180180
DetailContext = new RevisionCompare(_repo.FullPath, start, end);
181181
}
182182
else
183183
{
184-
_repo.SearchResultSelectedCommit = null;
184+
_repo.SelectedSearchedCommit = null;
185185
DetailContext = commits.Count;
186186
}
187187
}
@@ -599,7 +599,7 @@ public ContextMenu MakeContextMenu(ListBox list)
599599
var head = _commits.Find(x => x.SHA == current.Head);
600600
if (head == null)
601601
{
602-
_repo.SearchResultSelectedCommit = null;
602+
_repo.SelectedSearchedCommit = null;
603603
head = new Commands.QuerySingleCommit(_repo.FullPath, current.Head).Result();
604604
if (head != null)
605605
DetailContext = new RevisionCompare(_repo.FullPath, commit, head);

src/ViewModels/Repository.cs

Lines changed: 61 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -268,16 +268,19 @@ public bool IsSearching
268268
{
269269
if (SetProperty(ref _isSearching, value))
270270
{
271-
SearchedCommits = new List<Models.Commit>();
272-
SearchCommitFilter = string.Empty;
273-
MatchedFilesForSearching = null;
274-
_worktreeFiles = null;
275-
276271
if (value)
277272
{
278273
SelectedViewIndex = 0;
279274
CalcWorktreeFilesForSearching();
280275
}
276+
else
277+
{
278+
SearchedCommits = new List<Models.Commit>();
279+
SelectedSearchedCommit = null;
280+
SearchCommitFilter = string.Empty;
281+
MatchedFilesForSearching = null;
282+
_worktreeFiles = null;
283+
}
281284
}
282285
}
283286
}
@@ -306,7 +309,6 @@ public int SearchCommitFilterType
306309
if (SetProperty(ref _searchCommitFilterType, value))
307310
{
308311
CalcWorktreeFilesForSearching();
309-
310312
if (!string.IsNullOrEmpty(_searchCommitFilter))
311313
StartSearchCommits();
312314
}
@@ -318,13 +320,8 @@ public string SearchCommitFilter
318320
get => _searchCommitFilter;
319321
set
320322
{
321-
if (SetProperty(ref _searchCommitFilter, value))
322-
{
323-
if (_searchCommitFilterType == 4 && value is { Length: > 2 })
324-
CalcMatchedFilesForSearching();
325-
else if (_matchedFilesForSearching is { })
326-
MatchedFilesForSearching = null;
327-
}
323+
if (SetProperty(ref _searchCommitFilter, value) && IsSearchingCommitsByFilePath())
324+
CalcMatchedFilesForSearching();
328325
}
329326
}
330327

@@ -340,6 +337,16 @@ public List<Models.Commit> SearchedCommits
340337
set => SetProperty(ref _searchedCommits, value);
341338
}
342339

340+
public Models.Commit SelectedSearchedCommit
341+
{
342+
get => _selectedSearchedCommit;
343+
set
344+
{
345+
if (SetProperty(ref _selectedSearchedCommit, value) && value != null)
346+
NavigateToCommit(value.SHA);
347+
}
348+
}
349+
343350
public bool IsLocalBranchGroupExpanded
344351
{
345352
get => _settings.IsLocalBranchesExpandedInSideBar;
@@ -410,16 +417,6 @@ public InProgressContext InProgressContext
410417
get => _workingCopy?.InProgressContext;
411418
}
412419

413-
public Models.Commit SearchResultSelectedCommit
414-
{
415-
get => _searchResultSelectedCommit;
416-
set
417-
{
418-
if (SetProperty(ref _searchResultSelectedCommit, value) && value != null)
419-
NavigateToCommit(value.SHA);
420-
}
421-
}
422-
423420
public bool IsAutoFetching
424421
{
425422
get => _isAutoFetching;
@@ -523,6 +520,7 @@ public void Close()
523520
_submodules.Clear();
524521
_visibleSubmodules.Clear();
525522
_searchedCommits.Clear();
523+
_selectedSearchedCommit = null;
526524

527525
_worktreeFiles = null;
528526
_matchedFilesForSearching = null;
@@ -707,32 +705,22 @@ public void StartSearchCommits()
707705
return;
708706

709707
IsSearchLoadingVisible = true;
710-
SearchResultSelectedCommit = null;
708+
SelectedSearchedCommit = null;
711709
MatchedFilesForSearching = null;
712710

713711
Task.Run(() =>
714712
{
715-
var visible = new List<Models.Commit>();
713+
var visible = null as List<Models.Commit>;
714+
var method = (Models.CommitSearchMethod)_searchCommitFilterType;
716715

717-
switch (_searchCommitFilterType)
716+
if (method == Models.CommitSearchMethod.BySHA)
718717
{
719-
case 0:
720-
var commit = new Commands.QuerySingleCommit(_fullpath, _searchCommitFilter).Result();
721-
if (commit != null)
722-
visible.Add(commit);
723-
break;
724-
case 1:
725-
visible = new Commands.QueryCommits(_fullpath, _searchCommitFilter, Models.CommitSearchMethod.ByAuthor, _onlySearchCommitsInCurrentBranch).Result();
726-
break;
727-
case 2:
728-
visible = new Commands.QueryCommits(_fullpath, _searchCommitFilter, Models.CommitSearchMethod.ByCommitter, _onlySearchCommitsInCurrentBranch).Result();
729-
break;
730-
case 3:
731-
visible = new Commands.QueryCommits(_fullpath, _searchCommitFilter, Models.CommitSearchMethod.ByMessage, _onlySearchCommitsInCurrentBranch).Result();
732-
break;
733-
case 4:
734-
visible = new Commands.QueryCommits(_fullpath, _searchCommitFilter, Models.CommitSearchMethod.ByFile, _onlySearchCommitsInCurrentBranch).Result();
735-
break;
718+
var commit = new Commands.QuerySingleCommit(_fullpath, _searchCommitFilter).Result();
719+
visible = commit == null ? [] : [commit];
720+
}
721+
else
722+
{
723+
visible = new Commands.QueryCommits(_fullpath, _searchCommitFilter, method, _onlySearchCommitsInCurrentBranch).Result();
736724
}
737725

738726
Dispatcher.UIThread.Invoke(() =>
@@ -1636,7 +1624,7 @@ public ContextMenu CreateContextMenuForLocalBranch(Models.Branch branch)
16361624
compareWithWorktree.Icon = App.CreateMenuIcon("Icons.Compare");
16371625
compareWithWorktree.Click += (_, _) =>
16381626
{
1639-
SearchResultSelectedCommit = null;
1627+
SelectedSearchedCommit = null;
16401628

16411629
if (_histories != null)
16421630
{
@@ -1918,7 +1906,7 @@ public ContextMenu CreateContextMenuForRemoteBranch(Models.Branch branch)
19181906
compareWithWorktree.Icon = App.CreateMenuIcon("Icons.Compare");
19191907
compareWithWorktree.Click += (_, _) =>
19201908
{
1921-
SearchResultSelectedCommit = null;
1909+
SelectedSearchedCommit = null;
19221910

19231911
if (_histories != null)
19241912
{
@@ -2371,35 +2359,45 @@ private void TryToAddCustomActionsToBranchContextMenu(ContextMenu menu, Models.B
23712359
menu.Items.Add(new MenuItem() { Header = "-" });
23722360
}
23732361

2362+
private bool IsSearchingCommitsByFilePath()
2363+
{
2364+
return _isSearching && _searchCommitFilterType == (int)Models.CommitSearchMethod.ByFile;
2365+
}
2366+
23742367
private void CalcWorktreeFilesForSearching()
23752368
{
2376-
_worktreeFiles = null;
2369+
if (!IsSearchingCommitsByFilePath())
2370+
{
2371+
_worktreeFiles = null;
2372+
MatchedFilesForSearching = null;
2373+
GC.Collect();
2374+
return;
2375+
}
23772376

2378-
if (_searchCommitFilterType == 4)
2377+
Task.Run(() =>
23792378
{
2380-
Task.Run(() =>
2379+
var files = new Commands.QueryRevisionFileNames(_fullpath, "HEAD").Result();
2380+
Dispatcher.UIThread.Invoke(() =>
23812381
{
2382-
var files = new Commands.QueryRevisionFileNames(_fullpath, "HEAD").Result();
2383-
Dispatcher.UIThread.Invoke(() =>
2384-
{
2385-
if (_searchCommitFilterType != 4)
2386-
return;
2382+
if (!IsSearchingCommitsByFilePath())
2383+
return;
23872384

2388-
_worktreeFiles = new List<string>();
2389-
foreach (var f in files)
2390-
_worktreeFiles.Add(f);
2385+
_worktreeFiles = new List<string>();
2386+
foreach (var f in files)
2387+
_worktreeFiles.Add(f);
23912388

2392-
if (_searchCommitFilter is { Length: > 2 })
2393-
CalcMatchedFilesForSearching();
2394-
});
2389+
CalcMatchedFilesForSearching();
23952390
});
2396-
}
2391+
});
23972392
}
23982393

23992394
private void CalcMatchedFilesForSearching()
24002395
{
2401-
if (_worktreeFiles == null || _worktreeFiles.Count == 0)
2396+
if (_worktreeFiles == null || _worktreeFiles.Count == 0 || _searchCommitFilter.Length < 3)
2397+
{
2398+
MatchedFilesForSearching = null;
24022399
return;
2400+
}
24032401

24042402
var matched = new List<string>();
24052403
foreach (var file in _worktreeFiles)
@@ -2461,11 +2459,11 @@ private void AutoFetchImpl(object sender)
24612459

24622460
private bool _isSearching = false;
24632461
private bool _isSearchLoadingVisible = false;
2464-
private int _searchCommitFilterType = 3;
2462+
private int _searchCommitFilterType = (int)Models.CommitSearchMethod.ByMessage;
24652463
private bool _onlySearchCommitsInCurrentBranch = false;
24662464
private string _searchCommitFilter = string.Empty;
24672465
private List<Models.Commit> _searchedCommits = new List<Models.Commit>();
2468-
private Models.Commit _searchResultSelectedCommit = null;
2466+
private Models.Commit _selectedSearchedCommit = null;
24692467
private List<string> _worktreeFiles = null;
24702468
private List<string> _matchedFilesForSearching = null;
24712469

src/Views/Repository.axaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@
504504
Margin="0,8,0,0"
505505
ItemsSource="{Binding SearchedCommits}"
506506
SelectionMode="Single"
507-
SelectedItem="{Binding SearchResultSelectedCommit, Mode=TwoWay}"
507+
SelectedItem="{Binding SelectedSearchedCommit, Mode=TwoWay}"
508508
BorderThickness="1"
509509
BorderBrush="{DynamicResource Brush.Border2}"
510510
Background="{DynamicResource Brush.Contents}"

0 commit comments

Comments
 (0)