From 6b1cab7011846e404b0d1499f1bfc8dd49006294 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Wed, 2 Jul 2025 17:10:00 +0200 Subject: [PATCH] Auto-stash if necessary when reverting commits --- .../controllers/local_commits_controller.go | 18 ++++++++++++++++++ pkg/i18n/english.go | 2 ++ 2 files changed, 20 insertions(+) diff --git a/pkg/gui/controllers/local_commits_controller.go b/pkg/gui/controllers/local_commits_controller.go index 42059796c..2886c8cd0 100644 --- a/pkg/gui/controllers/local_commits_controller.go +++ b/pkg/gui/controllers/local_commits_controller.go @@ -873,12 +873,30 @@ func (self *LocalCommitsController) revert(commits []*models.Commit, start, end HandleConfirm: func() error { self.c.LogAction(self.c.Tr.Actions.RevertCommit) return self.c.WithWaitingStatusSync(self.c.Tr.RevertingStatus, func() error { + mustStash := helpers.IsWorkingTreeDirty(self.c.Model().Files) + + if mustStash { + if err := self.c.Git().Stash.Push(self.c.Tr.AutoStashForReverting); err != nil { + return err + } + } + result := self.c.Git().Commit.Revert(hashes, isMerge) if err := self.c.Helpers().MergeAndRebase.CheckMergeOrRebaseWithRefreshOptions(result, types.RefreshOptions{Mode: types.SYNC}); err != nil { return err } self.context().MoveSelection(len(commits)) self.context().FocusLine() + + if mustStash { + if err := self.c.Git().Stash.Pop(0); err != nil { + return err + } + self.c.Refresh(types.RefreshOptions{ + Scope: []types.RefreshableView{types.STASH, types.FILES}, + }) + } + return nil }) }, diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 85c075f7b..ee4014747 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -452,6 +452,7 @@ type TranslationSet struct { AutoStashForNewBranch string AutoStashForMovingPatchToIndex string AutoStashForCherryPicking string + AutoStashForReverting string Discard string DiscardChangesTitle string DiscardFileChangesTooltip string @@ -1549,6 +1550,7 @@ func EnglishTranslationSet() *TranslationSet { AutoStashForNewBranch: "Auto-stashing changes for creating new branch %s", AutoStashForMovingPatchToIndex: "Auto-stashing changes for moving custom patch to index from %s", AutoStashForCherryPicking: "Auto-stashing changes for cherry-picking commits", + AutoStashForReverting: "Auto-stashing changes for reverting commits", Discard: "Discard", DiscardFileChangesTooltip: "View options for discarding changes to the selected file.", DiscardChangesTitle: "Discard changes",