Skip to content

Commit 7153305

Browse files
committed
Add confirmation for hard reset when there are uncommitted changes
1 parent f872912 commit 7153305

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

pkg/gui/controllers/helpers/refs_helper.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,15 @@ func (self *RefsHelper) CreateGitResetMenu(name string, ref string) error {
248248
style.FgRed.Sprintf("reset --%s %s", row.strength, name),
249249
},
250250
OnPress: func() error {
251-
self.c.LogAction("Reset")
252-
return self.ResetToRef(ref, row.strength, []string{})
251+
return self.c.ConfirmIf(row.strength == "hard" && IsWorkingTreeDirty(self.c.Model().Files),
252+
types.ConfirmOpts{
253+
Title: self.c.Tr.Actions.HardReset,
254+
Prompt: self.c.Tr.ResetHardConfirmation,
255+
HandleConfirm: func() error {
256+
self.c.LogAction("Reset")
257+
return self.ResetToRef(ref, row.strength, []string{})
258+
},
259+
})
253260
},
254261
Key: row.key,
255262
Tooltip: row.tooltip,

pkg/gui/controllers/workspace_reset_controller.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/jesseduffield/gocui"
12+
"github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
1213
"github.com/jesseduffield/lazygit/pkg/gui/style"
1314
"github.com/jesseduffield/lazygit/pkg/gui/types"
1415
)
@@ -150,15 +151,22 @@ func (self *FilesController) createResetMenu() error {
150151
red.Sprint("git reset --hard HEAD"),
151152
},
152153
OnPress: func() error {
153-
self.c.LogAction(self.c.Tr.Actions.HardReset)
154-
if err := self.c.Git().WorkingTree.ResetHard("HEAD"); err != nil {
155-
return err
156-
}
154+
return self.c.ConfirmIf(helpers.IsWorkingTreeDirty(self.c.Model().Files),
155+
types.ConfirmOpts{
156+
Title: self.c.Tr.Actions.HardReset,
157+
Prompt: self.c.Tr.ResetHardConfirmation,
158+
HandleConfirm: func() error {
159+
self.c.LogAction(self.c.Tr.Actions.HardReset)
160+
if err := self.c.Git().WorkingTree.ResetHard("HEAD"); err != nil {
161+
return err
162+
}
157163

158-
self.c.Refresh(
159-
types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}},
160-
)
161-
return nil
164+
self.c.Refresh(
165+
types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}},
166+
)
167+
return nil
168+
},
169+
})
162170
},
163171
Key: 'h',
164172
},

pkg/i18n/english.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ type TranslationSet struct {
479479
ResetSoftTooltip string
480480
ResetMixedTooltip string
481481
ResetHardTooltip string
482+
ResetHardConfirmation string
482483
PressEnterToReturn string
483484
ViewStashOptions string
484485
ViewStashOptionsTooltip string
@@ -1502,6 +1503,7 @@ func EnglishTranslationSet() *TranslationSet {
15021503
ResetSoftTooltip: "Reset HEAD to the chosen commit, and keep the changes between the current and chosen commit as staged changes.",
15031504
ResetMixedTooltip: "Reset HEAD to the chosen commit, and keep the changes between the current and chosen commit as unstaged changes.",
15041505
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.",
1506+
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.",
15051507
ViewResetOptions: `Reset`,
15061508
FileResetOptionsTooltip: "View reset options for working tree (e.g. nuking the working tree).",
15071509
FixupTooltip: "Meld the selected commit into the commit below it. Similar to squash, but the selected commit's message will be discarded.",

pkg/integration/tests/branch/reset_to_upstream.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ var ResetToUpstream = NewIntegrationTest(NewIntegrationTestArgs{
9797
Title(Equals("Reset to origin/hard-branch")).
9898
Select(Contains("Hard reset")).
9999
Confirm()
100+
101+
t.ExpectPopup().Confirmation().
102+
Title(Equals("Hard reset")).
103+
Content(Contains("Are you sure you want to do a hard reset?")).
104+
Confirm()
100105
})
101106
t.Views().Commits().Lines(Contains("hard commit"))
102107
t.Views().Files().IsEmpty()

0 commit comments

Comments
 (0)