From b04c94ccc1332dcb189f5eeb1ebc122e6a25ea41 Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 10 Feb 2022 14:27:46 +0800 Subject: [PATCH] feature: add toolbar button to run `git gc` and `git lfs prune` --- src/Commands/GC.cs | 21 ++++++++++++++++++ src/Commands/LFS.cs | 20 +++++++++++++++++ src/Resources/Icons.xaml | 3 ++- src/Resources/Locales/en_US.xaml | 1 + src/Resources/Locales/zh_CN.xaml | 1 + src/Views/Popups/Cleanup.xaml | 12 ++++++++++ src/Views/Popups/Cleanup.xaml.cs | 34 +++++++++++++++++++++++++++++ src/Views/Widgets/Dashboard.xaml | 7 ++++++ src/Views/Widgets/Dashboard.xaml.cs | 5 +++++ 9 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 src/Commands/GC.cs create mode 100644 src/Views/Popups/Cleanup.xaml create mode 100644 src/Views/Popups/Cleanup.xaml.cs diff --git a/src/Commands/GC.cs b/src/Commands/GC.cs new file mode 100644 index 00000000..ceadb0b7 --- /dev/null +++ b/src/Commands/GC.cs @@ -0,0 +1,21 @@ +using System; + +namespace SourceGit.Commands { + /// + /// GC + /// + public class GC : Command { + private Action handler; + + public GC(string repo, Action onProgress) { + Cwd = repo; + Args = "gc"; + TraitErrorAsOutput = true; + handler = onProgress; + } + + public override void OnReadline(string line) { + handler?.Invoke(line); + } + } +} diff --git a/src/Commands/LFS.cs b/src/Commands/LFS.cs index 1faf258b..87d3e378 100644 --- a/src/Commands/LFS.cs +++ b/src/Commands/LFS.cs @@ -1,3 +1,4 @@ +using System; using System.IO; namespace SourceGit.Commands { @@ -7,6 +8,21 @@ namespace SourceGit.Commands { public class LFS { private string repo; + private class PruneCmd : Command { + private Action handler; + + public PruneCmd(string repo, Action onProgress) { + Cwd = repo; + Args = "lfs prune"; + TraitErrorAsOutput = true; + handler = onProgress; + } + + public override void OnReadline(string line) { + handler?.Invoke(line); + } + } + public LFS(string repo) { this.repo = repo; } @@ -27,5 +43,9 @@ namespace SourceGit.Commands { var rs = cmd.ReadToEnd(); return rs.Output.Contains("filter\0lfs"); } + + public void Prune(Action onProgress) { + new PruneCmd(repo, onProgress).Exec(); + } } } diff --git a/src/Resources/Icons.xaml b/src/Resources/Icons.xaml index 47faa94b..3ef4f5b4 100644 --- a/src/Resources/Icons.xaml +++ b/src/Resources/Icons.xaml @@ -21,7 +21,8 @@ M352 64h320L960 352v320L672 960h-320L64 672v-320L352 64zm161 363L344 256 260 341 429 512l-169 171L344 768 513 597 682 768l85-85L598 512l169-171L682 256 513 427z M899 870l-53-306H864c14 0 26-12 26-26V346c0-14-12-26-26-26H618V138c0-14-12-26-26-26H432c-14 0-26 12-26 26v182H160c-14 0-26 12-26 26v192c0 14 12 26 26 26h18l-53 306c0 2 0 3 0 4c0 14 12 26 26 26h723c2 0 3 0 4 0c14-2 24-16 21-30zM204 390h272V182h72v208h272v104H204V390zm468 440V674c0-4-4-8-8-8h-48c-4 0-8 4-8 8v156H416V674c0-4-4-8-8-8h-48c-4 0-8 4-8 8v156H203l45-260H776l45 260H672z M512 64C265 64 64 265 64 512s201 448 448 448s448-201 448-448S759 64 512 64zm238 642-46 46L512 558 318 750l-46-46L467 512 274 318l46-46L512 467l194-194 46 46L558 512l193 194z - + M797 829a49 49 0 1049 49 49 49 0 00-49-49zm147-114A49 49 0 10992 764a49 49 0 00-49-49zM928 861a49 49 0 1049 49A49 49 0 00928 861zm-5-586L992 205 851 64l-71 71a67 67 0 00-94 0l235 235a67 67 0 000-94zm-853 128a32 32 0 00-32 50 1291 1291 0 0075 112L288 552c20 0 25 21 8 37l-93 86a1282 1282 0 00120 114l100-32c19-6 28 15 14 34l-40 55c26 19 53 36 82 53a89 89 0 00115-20 1391 1391 0 00256-485l-188-188s-306 224-595 198z + M0 33h1024v160H0zM0 432h1024v160H0zM0 831h1024v160H0z M1024 610v-224H640v48H256V224h128V0H0v224h128v752h512v48h384V800H640v48H256V562h384v48z M30 271l241 0 0-241-241 0 0 241zM392 271l241 0 0-241-241 0 0 241zM753 30l0 241 241 0 0-241-241 0zM30 632l241 0 0-241-241 0 0 241zM392 632l241 0 0-241-241 0 0 241zM753 632l241 0 0-241-241 0 0 241zM30 994l241 0 0-241-241 0 0 241zM392 994l241 0 0-241-241 0 0 241zM753 994l241 0 0-241-241 0 0 241z diff --git a/src/Resources/Locales/en_US.xaml b/src/Resources/Locales/en_US.xaml index 62686a51..5110880e 100644 --- a/src/Resources/Locales/en_US.xaml +++ b/src/Resources/Locales/en_US.xaml @@ -122,6 +122,7 @@ Refresh Search Commit Statistics + Cleanup(GC & Prune) Configure this repository WORKSPACE LOCAL BRANCHES diff --git a/src/Resources/Locales/zh_CN.xaml b/src/Resources/Locales/zh_CN.xaml index 60521cd5..25d86c6c 100644 --- a/src/Resources/Locales/zh_CN.xaml +++ b/src/Resources/Locales/zh_CN.xaml @@ -121,6 +121,7 @@ 重新加载 查找提交 提交统计 + 清理本仓库(GC) 配置本仓库 工作区 本地分支 diff --git a/src/Views/Popups/Cleanup.xaml b/src/Views/Popups/Cleanup.xaml new file mode 100644 index 00000000..ede090f8 --- /dev/null +++ b/src/Views/Popups/Cleanup.xaml @@ -0,0 +1,12 @@ + + + + + diff --git a/src/Views/Popups/Cleanup.xaml.cs b/src/Views/Popups/Cleanup.xaml.cs new file mode 100644 index 00000000..52a8a154 --- /dev/null +++ b/src/Views/Popups/Cleanup.xaml.cs @@ -0,0 +1,34 @@ +using System.Threading.Tasks; + +namespace SourceGit.Views.Popups { + /// + /// 清理仓库 + /// + public partial class Cleanup : Controls.PopupWidget { + private string repo; + + public Cleanup(string repo) { + this.repo = repo; + InitializeComponent(); + } + + public override string GetTitle() { + return App.Text("Dashboard.Clean"); + } + + public override Task Start() { + UpdateProgress(GetTitle()); + + return Task.Run(() => { + Models.Watcher.SetEnabled(repo, false); + new Commands.GC(repo, UpdateProgress).Exec(); + + var lfs = new Commands.LFS(repo); + if (lfs.IsEnabled()) lfs.Prune(UpdateProgress); + + Models.Watcher.SetEnabled(repo, true); + return true; + }); + } + } +} diff --git a/src/Views/Widgets/Dashboard.xaml b/src/Views/Widgets/Dashboard.xaml index 85984014..9a8a8c5d 100644 --- a/src/Views/Widgets/Dashboard.xaml +++ b/src/Views/Widgets/Dashboard.xaml @@ -122,6 +122,13 @@ ToolTip="{DynamicResource Text.Dashboard.Refresh}" Click="TriggerRefresh"/> + +