From 927a1cab24bcd8c803bf5e952aff49e5a07af0d6 Mon Sep 17 00:00:00 2001 From: Alexander Bogomolets Date: Sun, 28 Apr 2024 16:43:53 +0300 Subject: [PATCH] Add checkout local changes handling This commit adds the local changes handling behavior for branch checkout. One of three can be selected: stash and reapply after checkout, discard changes or leave them as is (previous behaviour) --- src/Resources/Locales/en_US.axaml | 4 ++ src/Resources/Locales/zh_CN.axaml | 4 ++ src/ViewModels/Checkout.cs | 73 ++++++++++++++++++++++++++++++- src/ViewModels/Histories.cs | 6 ++- src/ViewModels/Repository.cs | 5 ++- src/Views/Checkout.axaml | 12 +++++ src/Views/Repository.axaml.cs | 2 +- 7 files changed, 101 insertions(+), 5 deletions(-) diff --git a/src/Resources/Locales/en_US.axaml b/src/Resources/Locales/en_US.axaml index 0d7a3072..dfe51cd4 100644 --- a/src/Resources/Locales/en_US.axaml +++ b/src/Resources/Locales/en_US.axaml @@ -53,6 +53,10 @@ Show as Tree Checkout Branch Target : + Local Changes : + Stash & Reapply + Discard + Leave Cherry-Pick This Commit Commit : Commit all changes diff --git a/src/Resources/Locales/zh_CN.axaml b/src/Resources/Locales/zh_CN.axaml index 1b817c3b..217dbf7f 100644 --- a/src/Resources/Locales/zh_CN.axaml +++ b/src/Resources/Locales/zh_CN.axaml @@ -53,6 +53,10 @@ 树形模式 检出(checkout)分支 目标分支 : + 未提交更改 : + 贮藏(stash)并自动恢复 + 忽略 + 保持原样 挑选(cherry-pick)此提交 提交ID : 提交变化 diff --git a/src/ViewModels/Checkout.cs b/src/ViewModels/Checkout.cs index adfc70db..327253cf 100644 --- a/src/ViewModels/Checkout.cs +++ b/src/ViewModels/Checkout.cs @@ -9,27 +9,98 @@ namespace SourceGit.ViewModels get; private set; } + + public bool HasLocalChanges + { + get => _repo.WorkingCopyChangesCount > 0; + } + + public bool LeaveLocalChanges + { + get => _leaveLocalChanges; + set => SetProperty(ref _leaveLocalChanges, value); + } + + public bool DiscardLocalChanges + { + get => _discardLocalChanges; + set => SetProperty(ref _discardLocalChanges, value); + } + + public bool StashLocalChanges + { + get => _stashLocalChanges; + set => SetProperty(ref _stashLocalChanges, value); + } public Checkout(Repository repo, string branch) { _repo = repo; Branch = branch; View = new Views.Checkout() { DataContext = this }; + + StashLocalChanges = true; } public override Task Sure() { _repo.SetWatcherEnabled(false); ProgressDescription = $"Checkout '{Branch}' ..."; + var hasLocalChanges = HasLocalChanges; return Task.Run(() => { - var succ = new Commands.Checkout(_repo.FullPath).Branch(Branch, SetProgressDescription); + var succ = false; + if (hasLocalChanges) + { + if (DiscardLocalChanges) + { + SetProgressDescription("Discard local changes..."); + Commands.Discard.All(_repo.FullPath); + } + + if (StashLocalChanges) + { + SetProgressDescription("Stash local changes..."); + succ = new Commands.Add(_repo.FullPath).Exec(); + succ = new Commands.Stash(_repo.FullPath).Push("CHECKOUT_AUTO_STASH"); + } + } + + SetProgressDescription("Checkout branch ..."); + succ = new Commands.Checkout(_repo.FullPath).Branch(Branch, SetProgressDescription); + + if(hasLocalChanges && StashLocalChanges) + { + SetProgressDescription("Re-apply local changes..."); + succ = new Commands.Stash(_repo.FullPath).Apply("stash@{0}"); + if (succ) + { + succ = new Commands.Stash(_repo.FullPath).Drop("stash@{0}"); + } + } + CallUIThread(() => _repo.SetWatcherEnabled(true)); return succ; }); } + + public static void ShowPopup(Repository repo, string branch) + { + var checkout = new Checkout(repo, branch); + if (repo.WorkingCopyChangesCount > 0) + { + PopupHost.ShowPopup(checkout); + } + else + { + PopupHost.ShowAndStartPopup(checkout); + } + } private readonly Repository _repo; + private bool _leaveLocalChanges; + private bool _discardLocalChanges; + private bool _stashLocalChanges; } } diff --git a/src/ViewModels/Histories.cs b/src/ViewModels/Histories.cs index a4102543..4fc1e1b9 100644 --- a/src/ViewModels/Histories.cs +++ b/src/ViewModels/Histories.cs @@ -448,7 +448,9 @@ namespace SourceGit.ViewModels checkout.Click += (o, e) => { if (PopupHost.CanCreatePopup()) - PopupHost.ShowAndStartPopup(new Checkout(_repo, branch.Name)); + { + Checkout.ShowPopup(_repo, branch.Name); + } e.Handled = true; }; submenu.Items.Add(checkout); @@ -527,7 +529,7 @@ namespace SourceGit.ViewModels if (b.IsCurrent) return; if (PopupHost.CanCreatePopup()) - PopupHost.ShowAndStartPopup(new Checkout(_repo, b.Name)); + Checkout.ShowPopup(_repo, b.Name); return; } } diff --git a/src/ViewModels/Repository.cs b/src/ViewModels/Repository.cs index db35f4a9..fe99b0ec 100644 --- a/src/ViewModels/Repository.cs +++ b/src/ViewModels/Repository.cs @@ -843,7 +843,10 @@ namespace SourceGit.ViewModels checkout.Click += (o, e) => { if (PopupHost.CanCreatePopup()) - PopupHost.ShowAndStartPopup(new Checkout(this, branch.Name)); + { + Checkout.ShowPopup(this, branch.Name); + } + e.Handled = true; }; menu.Items.Add(checkout); diff --git a/src/Views/Checkout.axaml b/src/Views/Checkout.axaml index 083190f8..50bb3b8c 100644 --- a/src/Views/Checkout.axaml +++ b/src/Views/Checkout.axaml @@ -15,5 +15,17 @@ + + + + + + diff --git a/src/Views/Repository.axaml.cs b/src/Views/Repository.axaml.cs index 43d9ee11..0d010cc0 100644 --- a/src/Views/Repository.axaml.cs +++ b/src/Views/Repository.axaml.cs @@ -298,7 +298,7 @@ namespace SourceGit.Views if (branch.IsCurrent) return; - ViewModels.PopupHost.ShowAndStartPopup(new ViewModels.Checkout(repo, branch.Name)); + ViewModels.Checkout.ShowPopup(repo, branch.Name); e.Handled = true; } }