From 814637946d0b976de0e6b9b958abfb054389e015 Mon Sep 17 00:00:00 2001 From: leo Date: Tue, 20 Sep 2022 16:55:24 +0800 Subject: [PATCH] fix: remove invalid filters to fix issue #I5RZC9 --- src/Commands/Tag.cs | 5 -- src/Models/Repository.cs | 73 +++++++++++++++++++++++++++ src/Views/Popups/DeleteBranch.xaml.cs | 5 -- src/Views/Widgets/Dashboard.xaml.cs | 25 +++------ src/Views/Widgets/Histories.xaml.cs | 2 + 5 files changed, 81 insertions(+), 29 deletions(-) diff --git a/src/Commands/Tag.cs b/src/Commands/Tag.cs index e3ad4821..88942878 100644 --- a/src/Commands/Tag.cs +++ b/src/Commands/Tag.cs @@ -29,11 +29,6 @@ namespace SourceGit.Commands { Args = $"tag --delete {name}"; if (!Exec()) return false; - var repo = Models.Preference.Instance.FindRepository(Cwd); - if (repo != null && repo.Filters.Contains(name)) { - repo.Filters.Remove(name); - } - if (push) { var remotes = new Remotes(Cwd).Result(); foreach (var r in remotes) { diff --git a/src/Models/Repository.cs b/src/Models/Repository.cs index 345599ef..e7accc47 100644 --- a/src/Models/Repository.cs +++ b/src/Models/Repository.cs @@ -3,6 +3,20 @@ using System.IO; using System.Text.Json.Serialization; namespace SourceGit.Models { + /// + /// 用于更新过滤器的参数 + /// + public class FilterUpdateParam { + /// + /// 是否是添加过滤的操作,false代表删除 + /// + public bool IsAdd = false; + + /// + /// 过滤内容 + /// + public string Name = ""; + } /// /// 仓库 @@ -26,6 +40,10 @@ namespace SourceGit.Models { [JsonIgnore] public GitFlow GitFlow = new GitFlow(); #endregion + /// + /// 记录历史输入的提交信息 + /// + /// public void PushCommitMessage(string message) { if (string.IsNullOrEmpty(message)) return; @@ -42,10 +60,65 @@ namespace SourceGit.Models { CommitMessages.Insert(0, message); } + /// + /// 判断一个文件是否在GitDir中 + /// + /// + /// public bool ExistsInGitDir(string file) { if (string.IsNullOrEmpty(file)) return false; string fullpath = System.IO.Path.Combine(GitDir, file); return Directory.Exists(fullpath) || File.Exists(fullpath); } + + /// + /// 更新提交记录过滤器 + /// + /// 更新参数 + /// 是否发生了变化 + public bool UpdateFilters(FilterUpdateParam param = null) { + lock (updateFilterLock) { + bool changed = false; + + // 填写了参数就仅增删 + if (param != null) { + if (param.IsAdd) { + if (!Filters.Contains(param.Name)) { + Filters.Add(param.Name); + changed = true; + } + } else { + if (Filters.Contains(param.Name)) { + Filters.Remove(param.Name); + changed = true; + } + } + + return changed; + } + + // 未填写参数就检测,去掉无效的过滤 + if (Filters.Count > 0) { + var invalidFilters = new List(); + var tags = new Commands.Tags(Path).Result(); + foreach (var filter in Filters) { + if (filter.StartsWith("refs/")) { + if (!ExistsInGitDir(filter)) invalidFilters.Add(filter); + } else { + if (tags.FindIndex(t => t.Name == filter) < 0) invalidFilters.Add(filter); + } + } + + if (invalidFilters.Count > 0) { + foreach (var filter in invalidFilters) Filters.Remove(filter); + return true; + } + } + + return false; + } + } + + private readonly object updateFilterLock = new object(); } } diff --git a/src/Views/Popups/DeleteBranch.xaml.cs b/src/Views/Popups/DeleteBranch.xaml.cs index 9a00045a..7d1b5bcb 100644 --- a/src/Views/Popups/DeleteBranch.xaml.cs +++ b/src/Views/Popups/DeleteBranch.xaml.cs @@ -43,11 +43,6 @@ namespace SourceGit.Views.Popups { full = $"refs/remotes/{remote}/{branch}"; new Commands.Push(repo, remote, branch).Exec(); } - - var exists = Models.Preference.Instance.FindRepository(repo); - if (exists != null && exists.Filters.Contains(full)) { - exists.Filters.Remove(full); - } finishHandler?.Invoke(); Models.Watcher.SetEnabled(repo, true); diff --git a/src/Views/Widgets/Dashboard.xaml.cs b/src/Views/Widgets/Dashboard.xaml.cs index b4f3a53a..68222d6a 100644 --- a/src/Views/Widgets/Dashboard.xaml.cs +++ b/src/Views/Widgets/Dashboard.xaml.cs @@ -271,7 +271,7 @@ namespace SourceGit.Views.Widgets { Task.Run(() => { var tags = new Commands.Tags(repo.Path).Result(); - foreach (var t in tags) t.IsFiltered = repo.Filters.Contains(t.Name); + foreach (var tag in tags) tag.IsFiltered = repo.Filters.Contains(tag.Name); Dispatcher.Invoke(() => { txtTagCount.Text = $"({tags.Count})"; tagList.ItemsSource = tags; @@ -1100,30 +1100,17 @@ namespace SourceGit.Views.Widgets { var toggle = sender as ToggleButton; if (toggle == null) return; - var filter = ""; - var changed = false; - + var filter = new Models.FilterUpdateParam(); + filter.IsAdd = (toggle.IsChecked == true); if (toggle.DataContext is BranchNode) { var branch = (toggle.DataContext as BranchNode).Data as Models.Branch; if (branch == null) return; - filter = branch.FullName; + filter.Name = branch.FullName; } else if (toggle.DataContext is Models.Tag) { - filter = (toggle.DataContext as Models.Tag).Name; + filter.Name = (toggle.DataContext as Models.Tag).Name; } - if (toggle.IsChecked == true) { - if (!repo.Filters.Contains(filter)) { - repo.Filters.Add(filter); - changed = true; - } - } else { - if (repo.Filters.Contains(filter)) { - repo.Filters.Remove(filter); - changed = true; - } - } - - if (changed) (pages.Get("histories") as Histories).UpdateCommits(); + if (repo.UpdateFilters(filter)) (pages.Get("histories") as Histories).UpdateCommits(); } #endregion diff --git a/src/Views/Widgets/Histories.xaml.cs b/src/Views/Widgets/Histories.xaml.cs index 07cb7227..ac161d0f 100644 --- a/src/Views/Widgets/Histories.xaml.cs +++ b/src/Views/Widgets/Histories.xaml.cs @@ -52,6 +52,8 @@ namespace SourceGit.Views.Widgets { Task.Run(() => { var limits = "-20000 "; + + repo.UpdateFilters(); if (repo.Filters.Count > 0) { limits += string.Join(" ", repo.Filters); } else {