From 4e57cd50cd53c773f5d1261eed88482381a45bee Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 2 Oct 2024 21:17:44 +0800 Subject: [PATCH] feature: add an option to clean up ignored files when discard all changes in repo (#531) --- src/Commands/Clean.cs | 6 +-- src/Commands/Discard.cs | 4 +- src/Resources/Locales/en_US.axaml | 1 + src/Resources/Locales/zh_CN.axaml | 1 + src/Resources/Locales/zh_TW.axaml | 1 + src/ViewModels/Checkout.cs | 3 +- src/ViewModels/CheckoutCommit.cs | 3 +- src/ViewModels/CreateBranch.cs | 3 +- src/ViewModels/Discard.cs | 40 ++++++++++++--- src/ViewModels/Pull.cs | 2 +- src/ViewModels/Repository.cs | 1 - src/Views/Discard.axaml | 85 +++++++++++++++++++++---------- 12 files changed, 104 insertions(+), 46 deletions(-) diff --git a/src/Commands/Clean.cs b/src/Commands/Clean.cs index 900a7f93..a10e5873 100644 --- a/src/Commands/Clean.cs +++ b/src/Commands/Clean.cs @@ -5,16 +5,16 @@ namespace SourceGit.Commands { public class Clean : Command { - public Clean(string repo) + public Clean(string repo, bool includeIgnored) { WorkingDirectory = repo; Context = repo; - Args = "clean -qfd"; + Args = includeIgnored ? "clean -qfdx" : "clean -qfd"; } public Clean(string repo, List files) { - StringBuilder builder = new StringBuilder(); + var builder = new StringBuilder(); builder.Append("clean -qfd --"); foreach (var f in files) { diff --git a/src/Commands/Discard.cs b/src/Commands/Discard.cs index 63fcaa8e..a279bb84 100644 --- a/src/Commands/Discard.cs +++ b/src/Commands/Discard.cs @@ -5,10 +5,10 @@ namespace SourceGit.Commands { public static class Discard { - public static void All(string repo) + public static void All(string repo, bool includeIgnored) { new Restore(repo).Exec(); - new Clean(repo).Exec(); + new Clean(repo, includeIgnored).Exec(); } public static void Changes(string repo, List changes) diff --git a/src/Resources/Locales/en_US.axaml b/src/Resources/Locales/en_US.axaml index 8f6fac9f..b4f52b55 100644 --- a/src/Resources/Locales/en_US.axaml +++ b/src/Resources/Locales/en_US.axaml @@ -224,6 +224,7 @@ Discard Changes All local changes in working copy. Changes: + Include ignored files Total {0} changes will be discard You can't undo this action!!! Bookmark: diff --git a/src/Resources/Locales/zh_CN.axaml b/src/Resources/Locales/zh_CN.axaml index bb39f0f3..57712fa5 100644 --- a/src/Resources/Locales/zh_CN.axaml +++ b/src/Resources/Locales/zh_CN.axaml @@ -227,6 +227,7 @@ 放弃更改确认 所有本地址未提交的修改。 变更 : + 包括所有已忽略的文件 总计{0}项选中更改 本操作不支持回退,请确认后继续!!! 书签 : diff --git a/src/Resources/Locales/zh_TW.axaml b/src/Resources/Locales/zh_TW.axaml index ad8f7746..7abfb36a 100644 --- a/src/Resources/Locales/zh_TW.axaml +++ b/src/Resources/Locales/zh_TW.axaml @@ -227,6 +227,7 @@ 捨棄變更 所有本機未提交的變更。 變更: + 包括所有已忽略的檔案 將捨棄總計 {0} 項已選取的變更 您無法復原此操作,請確認後再繼續! 書籤: diff --git a/src/ViewModels/Checkout.cs b/src/ViewModels/Checkout.cs index 5661e2ed..26858b4f 100644 --- a/src/ViewModels/Checkout.cs +++ b/src/ViewModels/Checkout.cs @@ -7,7 +7,6 @@ namespace SourceGit.ViewModels public string Branch { get; - private set; } public Models.DealWithLocalChanges PreAction @@ -49,7 +48,7 @@ namespace SourceGit.ViewModels else if (PreAction == Models.DealWithLocalChanges.Discard) { SetProgressDescription("Discard local changes ..."); - Commands.Discard.All(_repo.FullPath); + Commands.Discard.All(_repo.FullPath, false); } } diff --git a/src/ViewModels/CheckoutCommit.cs b/src/ViewModels/CheckoutCommit.cs index 54cdbccd..ddc0a0c6 100644 --- a/src/ViewModels/CheckoutCommit.cs +++ b/src/ViewModels/CheckoutCommit.cs @@ -7,7 +7,6 @@ namespace SourceGit.ViewModels public Models.Commit Commit { get; - private set; } public bool AutoStash @@ -49,7 +48,7 @@ namespace SourceGit.ViewModels else { SetProgressDescription("Discard local changes ..."); - Commands.Discard.All(_repo.FullPath); + Commands.Discard.All(_repo.FullPath, false); } } diff --git a/src/ViewModels/CreateBranch.cs b/src/ViewModels/CreateBranch.cs index 2c925c03..a22a6871 100644 --- a/src/ViewModels/CreateBranch.cs +++ b/src/ViewModels/CreateBranch.cs @@ -17,7 +17,6 @@ namespace SourceGit.ViewModels public object BasedOn { get; - private set; } public Models.DealWithLocalChanges PreAction @@ -105,7 +104,7 @@ namespace SourceGit.ViewModels else if (PreAction == Models.DealWithLocalChanges.Discard) { SetProgressDescription("Discard local changes..."); - Commands.Discard.All(_repo.FullPath); + Commands.Discard.All(_repo.FullPath, false); } } diff --git a/src/ViewModels/Discard.cs b/src/ViewModels/Discard.cs index 916c3b86..e6653d02 100644 --- a/src/ViewModels/Discard.cs +++ b/src/ViewModels/Discard.cs @@ -3,19 +3,45 @@ using System.Threading.Tasks; namespace SourceGit.ViewModels { + public class DiscardAllMode + { + public bool IncludeIgnored + { + get; + set; + } = false; + } + + public class DiscardSingleFile + { + public string Path + { + get; + set; + } = string.Empty; + } + + public class DiscardMultipleFiles + { + public int Count + { + get; + set; + } = 0; + } + public class Discard : Popup { public object Mode { get; - private set; } public Discard(Repository repo) { _repo = repo; - Mode = new Models.Null(); + Mode = new DiscardAllMode(); View = new Views.Discard { DataContext = this }; } @@ -25,11 +51,11 @@ namespace SourceGit.ViewModels _changes = changes; if (_changes == null) - Mode = new Models.Null(); + Mode = new DiscardAllMode(); else if (_changes.Count == 1) - Mode = _changes[0].Path; + Mode = new DiscardSingleFile() { Path = _changes[0].Path }; else - Mode = _changes.Count; + Mode = new DiscardMultipleFiles() { Count = _changes.Count }; View = new Views.Discard() { DataContext = this }; } @@ -41,8 +67,8 @@ namespace SourceGit.ViewModels return Task.Run(() => { - if (_changes == null) - Commands.Discard.All(_repo.FullPath); + if (Mode is DiscardAllMode all) + Commands.Discard.All(_repo.FullPath, all.IncludeIgnored); else Commands.Discard.Changes(_repo.FullPath, _changes); diff --git a/src/ViewModels/Pull.cs b/src/ViewModels/Pull.cs index 03a72753..b37e8817 100644 --- a/src/ViewModels/Pull.cs +++ b/src/ViewModels/Pull.cs @@ -139,7 +139,7 @@ namespace SourceGit.ViewModels else if (PreAction == Models.DealWithLocalChanges.Discard) { SetProgressDescription("Discard local changes ..."); - Commands.Discard.All(_repo.FullPath); + Commands.Discard.All(_repo.FullPath, false); } } diff --git a/src/ViewModels/Repository.cs b/src/ViewModels/Repository.cs index 0667c546..414a135b 100644 --- a/src/ViewModels/Repository.cs +++ b/src/ViewModels/Repository.cs @@ -1271,7 +1271,6 @@ namespace SourceGit.ViewModels var discard = new MenuItem(); discard.Header = App.Text("BranchCM.DiscardAll"); discard.Icon = App.CreateMenuIcon("Icons.Undo"); - discard.IsEnabled = _localChangesCount > 0; discard.Click += (_, e) => { if (PopupHost.CanCreatePopup()) diff --git a/src/Views/Discard.axaml b/src/Views/Discard.axaml index e67600d7..52668213 100644 --- a/src/Views/Discard.axaml +++ b/src/Views/Discard.axaml @@ -2,7 +2,6 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" - xmlns:m="using:SourceGit.Models" xmlns:vm="using:SourceGit.ViewModels" xmlns:c="using:SourceGit.Converters" mc:Ignorable="d" d:DesignWidth="500" d:DesignHeight="450" @@ -12,35 +11,69 @@ - - - - - - - - - + + + + + + + + + + + + + - - - - + + + + + + + - + - - - - + + + + + + + + + - + - - - - - - + + + + + +