From 3275dd07d226c6c6dc4c6a75b1f83d53108fdcbb Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 7 Apr 2025 16:03:49 +0800 Subject: [PATCH] enhance: auto stash and re-apply local changes before squashing (#1141) Signed-off-by: leo --- src/ViewModels/Histories.cs | 12 ------------ src/ViewModels/Squash.cs | 21 ++++++++++++++++++++- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/ViewModels/Histories.cs b/src/ViewModels/Histories.cs index 01114dd3..b0bbff3c 100644 --- a/src/ViewModels/Histories.cs +++ b/src/ViewModels/Histories.cs @@ -419,12 +419,6 @@ namespace SourceGit.ViewModels squash.Icon = App.CreateMenuIcon("Icons.SquashIntoParent"); squash.Click += (_, e) => { - if (_repo.LocalChangesCount > 0) - { - App.RaiseException(_repo.FullPath, "You have local changes. Please run stash or discard first."); - return; - } - if (_repo.CanCreatePopup()) _repo.ShowPopup(new Squash(_repo, commit, commit.SHA)); @@ -458,12 +452,6 @@ namespace SourceGit.ViewModels squash.IsEnabled = commit.Parents.Count == 1; squash.Click += (_, e) => { - if (_repo.LocalChangesCount > 0) - { - App.RaiseException(_repo.FullPath, "You have local changes. Please run stash or discard first."); - return; - } - if (commit.Parents.Count == 1) { var parent = _commits.Find(x => x.SHA == commit.Parents[0]); diff --git a/src/ViewModels/Squash.cs b/src/ViewModels/Squash.cs index 198dbe9a..eceb8339 100644 --- a/src/ViewModels/Squash.cs +++ b/src/ViewModels/Squash.cs @@ -33,9 +33,28 @@ namespace SourceGit.ViewModels return Task.Run(() => { - var succ = new Commands.Reset(_repo.FullPath, Target.SHA, "--soft").Exec(); + var autoStashed = false; + var succ = false; + + if (_repo.LocalChangesCount > 0) + { + succ = new Commands.Stash(_repo.FullPath).Push("SQUASH_AUTO_STASH"); + if (!succ) + { + CallUIThread(() => _repo.SetWatcherEnabled(true)); + return false; + } + + autoStashed = true; + } + + succ = new Commands.Reset(_repo.FullPath, Target.SHA, "--soft").Exec(); if (succ) succ = new Commands.Commit(_repo.FullPath, _message, true, _repo.Settings.EnableSignOffForCommit).Run(); + + if (succ && autoStashed) + new Commands.Stash(_repo.FullPath).Pop("stash@{0}"); + CallUIThread(() => _repo.SetWatcherEnabled(true)); return succ; });