diff --git a/pkg/gui/controllers/helpers/refs_helper.go b/pkg/gui/controllers/helpers/refs_helper.go index 993ffb423..68bcbbc33 100644 --- a/pkg/gui/controllers/helpers/refs_helper.go +++ b/pkg/gui/controllers/helpers/refs_helper.go @@ -248,8 +248,15 @@ func (self *RefsHelper) CreateGitResetMenu(name string, ref string) error { style.FgRed.Sprintf("reset --%s %s", row.strength, name), }, OnPress: func() error { - self.c.LogAction("Reset") - return self.ResetToRef(ref, row.strength, []string{}) + 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") + return self.ResetToRef(ref, row.strength, []string{}) + }, + }) }, Key: row.key, Tooltip: row.tooltip, diff --git a/pkg/gui/controllers/workspace_reset_controller.go b/pkg/gui/controllers/workspace_reset_controller.go index 0c74e3ac7..9865e7796 100644 --- a/pkg/gui/controllers/workspace_reset_controller.go +++ b/pkg/gui/controllers/workspace_reset_controller.go @@ -9,6 +9,7 @@ import ( "time" "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/types" ) @@ -150,15 +151,22 @@ func (self *FilesController) createResetMenu() error { red.Sprint("git reset --hard HEAD"), }, OnPress: func() error { - self.c.LogAction(self.c.Tr.Actions.HardReset) - if err := self.c.Git().WorkingTree.ResetHard("HEAD"); err != nil { - return err - } + 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) + if err := self.c.Git().WorkingTree.ResetHard("HEAD"); err != nil { + return err + } - self.c.Refresh( - types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}}, - ) - return nil + self.c.Refresh( + types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}}, + ) + return nil + }, + }) }, Key: 'h', }, diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 2bf237193..5a725a677 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -479,6 +479,7 @@ type TranslationSet struct { ResetSoftTooltip string ResetMixedTooltip string ResetHardTooltip string + ResetHardConfirmation string PressEnterToReturn string ViewStashOptions 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.", 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.", + 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`, 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.", diff --git a/pkg/integration/tests/branch/reset_to_upstream.go b/pkg/integration/tests/branch/reset_to_upstream.go index 18f04257e..e5503f127 100644 --- a/pkg/integration/tests/branch/reset_to_upstream.go +++ b/pkg/integration/tests/branch/reset_to_upstream.go @@ -97,6 +97,11 @@ var ResetToUpstream = NewIntegrationTest(NewIntegrationTestArgs{ Title(Equals("Reset to origin/hard-branch")). Select(Contains("Hard reset")). 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().Files().IsEmpty()