1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-14 00:21:48 +03:00

Add confirmation for hard reset when there are uncommitted changes

This commit is contained in:
Stefan Haller
2025-07-06 14:24:50 +02:00
parent f872912c07
commit 7153305174
4 changed files with 32 additions and 10 deletions

View File

@ -248,9 +248,16 @@ func (self *RefsHelper) CreateGitResetMenu(name string, ref string) error {
style.FgRed.Sprintf("reset --%s %s", row.strength, name), style.FgRed.Sprintf("reset --%s %s", row.strength, name),
}, },
OnPress: func() error { OnPress: func() error {
return self.c.ConfirmIf(row.strength == "hard" && IsWorkingTreeDirty(self.c.Model().Files),
types.ConfirmOpts{
Title: self.c.Tr.Actions.HardReset,
Prompt: self.c.Tr.ResetHardConfirmation,
HandleConfirm: func() error {
self.c.LogAction("Reset") self.c.LogAction("Reset")
return self.ResetToRef(ref, row.strength, []string{}) return self.ResetToRef(ref, row.strength, []string{})
}, },
})
},
Key: row.key, Key: row.key,
Tooltip: row.tooltip, Tooltip: row.tooltip,
} }

View File

@ -9,6 +9,7 @@ import (
"time" "time"
"github.com/jesseduffield/gocui" "github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
"github.com/jesseduffield/lazygit/pkg/gui/style" "github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/gui/types"
) )
@ -150,6 +151,11 @@ func (self *FilesController) createResetMenu() error {
red.Sprint("git reset --hard HEAD"), red.Sprint("git reset --hard HEAD"),
}, },
OnPress: func() error { OnPress: func() error {
return self.c.ConfirmIf(helpers.IsWorkingTreeDirty(self.c.Model().Files),
types.ConfirmOpts{
Title: self.c.Tr.Actions.HardReset,
Prompt: self.c.Tr.ResetHardConfirmation,
HandleConfirm: func() error {
self.c.LogAction(self.c.Tr.Actions.HardReset) self.c.LogAction(self.c.Tr.Actions.HardReset)
if err := self.c.Git().WorkingTree.ResetHard("HEAD"); err != nil { if err := self.c.Git().WorkingTree.ResetHard("HEAD"); err != nil {
return err return err
@ -160,6 +166,8 @@ func (self *FilesController) createResetMenu() error {
) )
return nil return nil
}, },
})
},
Key: 'h', Key: 'h',
}, },
} }

View File

@ -479,6 +479,7 @@ type TranslationSet struct {
ResetSoftTooltip string ResetSoftTooltip string
ResetMixedTooltip string ResetMixedTooltip string
ResetHardTooltip string ResetHardTooltip string
ResetHardConfirmation string
PressEnterToReturn string PressEnterToReturn string
ViewStashOptions string ViewStashOptions string
ViewStashOptionsTooltip string ViewStashOptionsTooltip string
@ -1502,6 +1503,7 @@ func EnglishTranslationSet() *TranslationSet {
ResetSoftTooltip: "Reset HEAD to the chosen commit, and keep the changes between the current and chosen commit as staged changes.", ResetSoftTooltip: "Reset HEAD to the chosen commit, and keep the changes between the current and chosen commit as staged changes.",
ResetMixedTooltip: "Reset HEAD to the chosen commit, and keep the changes between the current and chosen commit as unstaged changes.", ResetMixedTooltip: "Reset HEAD to the chosen commit, and keep the changes between the current and chosen commit as unstaged changes.",
ResetHardTooltip: "Reset HEAD to the chosen commit, and discard all changes between the current and chosen commit, as well as all current modifications in the working tree.", ResetHardTooltip: "Reset HEAD to the chosen commit, and discard all changes between the current and chosen commit, as well as all current modifications in the working tree.",
ResetHardConfirmation: "Are you sure you want to do a hard reset? This will discard all uncommitted changes (both staged and unstaged), which is not undoable.",
ViewResetOptions: `Reset`, ViewResetOptions: `Reset`,
FileResetOptionsTooltip: "View reset options for working tree (e.g. nuking the working tree).", FileResetOptionsTooltip: "View reset options for working tree (e.g. nuking the working tree).",
FixupTooltip: "Meld the selected commit into the commit below it. Similar to squash, but the selected commit's message will be discarded.", FixupTooltip: "Meld the selected commit into the commit below it. Similar to squash, but the selected commit's message will be discarded.",

View File

@ -97,6 +97,11 @@ var ResetToUpstream = NewIntegrationTest(NewIntegrationTestArgs{
Title(Equals("Reset to origin/hard-branch")). Title(Equals("Reset to origin/hard-branch")).
Select(Contains("Hard reset")). Select(Contains("Hard reset")).
Confirm() Confirm()
t.ExpectPopup().Confirmation().
Title(Equals("Hard reset")).
Content(Contains("Are you sure you want to do a hard reset?")).
Confirm()
}) })
t.Views().Commits().Lines(Contains("hard commit")) t.Views().Commits().Lines(Contains("hard commit"))
t.Views().Files().IsEmpty() t.Views().Files().IsEmpty()