Skip to content

Commit 2057dc1

Browse files
authored
Log hashes of local branches when deleting them (#5441)
This makes it easier to recover them if they were deleted accidentally. We only do this for local branches for now, we don't bother for remote branches; it would be trickier there, because the branch on the server might not actually point to the same hash as our local remote tracking branch does. Fixes #2468.
2 parents 4c2c0ce + e4309f1 commit 2057dc1

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

pkg/gui/controllers/helpers/branches_helper.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func (self *BranchesHelper) ConfirmLocalDelete(branches []*models.Branch) error
4343
doDelete := func() error {
4444
return self.c.WithWaitingStatus(self.c.Tr.DeletingStatus, func(_ gocui.Task) error {
4545
self.c.LogAction(self.c.Tr.Actions.DeleteLocalBranch)
46+
self.logBranchHashes(branches)
4647
branchNames := lo.Map(branches, func(branch *models.Branch, _ int) string { return branch.Name })
4748
if err := self.c.Git().Branch.LocalDelete(branchNames, true); err != nil {
4849
return err
@@ -178,6 +179,7 @@ func (self *BranchesHelper) ConfirmLocalAndRemoteDelete(branches []*models.Branc
178179
}
179180

180181
self.c.LogAction(self.c.Tr.Actions.DeleteLocalBranch)
182+
self.logBranchHashes(branches)
181183
branchNames := lo.Map(branches, func(branch *models.Branch, _ int) string { return branch.Name })
182184
if err := self.c.Git().Branch.LocalDelete(branchNames, true); err != nil {
183185
return err
@@ -257,6 +259,20 @@ func (self *BranchesHelper) allBranchesMerged(branches []*models.Branch) (bool,
257259
return allBranchesMerged, nil
258260
}
259261

262+
func (self *BranchesHelper) logBranchHashes(branches []*models.Branch) {
263+
for _, branch := range branches {
264+
msg := utils.ResolvePlaceholderString(
265+
self.c.Tr.Log.DeletingBranch,
266+
map[string]string{
267+
"branchName": branch.Name,
268+
"hash": branch.CommitHash,
269+
},
270+
)
271+
272+
self.c.LogCommand(msg, false)
273+
}
274+
}
275+
260276
func (self *BranchesHelper) deleteRemoteBranches(remoteBranches []*models.RemoteBranch, task gocui.Task) error {
261277
remotes := lo.GroupBy(remoteBranches, func(branch *models.RemoteBranch) string { return branch.RemoteName })
262278
for remote, branches := range remotes {

pkg/gui/controllers/stash_controller.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package controllers
22

33
import (
4+
"fmt"
5+
46
"github.com/jesseduffield/lazygit/pkg/commands/models"
57
"github.com/jesseduffield/lazygit/pkg/gui/context"
68
"github.com/jesseduffield/lazygit/pkg/gui/style"
@@ -129,7 +131,7 @@ func (self *StashController) handleStashApply(stashEntry *models.StashEntry) err
129131
func (self *StashController) handleStashPop(stashEntry *models.StashEntry) error {
130132
pop := func() error {
131133
self.c.LogAction(self.c.Tr.Actions.PopStash)
132-
self.c.LogCommand("Popping stash "+stashEntry.Hash, false)
134+
self.c.LogCommand(fmt.Sprintf(self.c.Tr.Log.PoppingStash, stashEntry.Hash), false)
133135
err := self.c.Git().Stash.Pop(stashEntry.Index)
134136
self.postStashRefresh()
135137
if err != nil {
@@ -163,7 +165,7 @@ func (self *StashController) handleStashDrop(stashEntries []*models.StashEntry)
163165
HandleConfirm: func() error {
164166
self.c.LogAction(self.c.Tr.Actions.DropStash)
165167
for i := len(stashEntries) - 1; i >= 0; i-- {
166-
self.c.LogCommand("Dropping stash "+stashEntries[i].Hash, false)
168+
self.c.LogCommand(fmt.Sprintf(self.c.Tr.Log.DroppingStash, stashEntries[i].Hash), false)
167169
err := self.c.Git().Stash.Drop(stashEntries[i].Index)
168170
self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.STASH}})
169171
if err != nil {

pkg/i18n/english.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,9 @@ type Log struct {
963963
CreateFileWithContent string
964964
AppendingLineToFile string
965965
EditRebaseFromBaseCommit string
966+
DroppingStash string
967+
PoppingStash string
968+
DeletingBranch string
966969
}
967970

968971
type Actions struct {
@@ -2196,7 +2199,10 @@ func EnglishTranslationSet() *TranslationSet {
21962199
Remove: "Removing '{{.filename}}'",
21972200
CreateFileWithContent: "Creating file '{{.path}}'",
21982201
AppendingLineToFile: "Appending '{{.line}}' to file '{{.filename}}'",
2199-
EditRebaseFromBaseCommit: "Beginning interactive rebase from '{{.baseCommit}}' onto '{{.targetBranchName}}",
2202+
EditRebaseFromBaseCommit: "Beginning interactive rebase from '{{.baseCommit}}' onto '{{.targetBranchName}}'",
2203+
DroppingStash: "Dropping stash %s",
2204+
PoppingStash: "Popping stash %s",
2205+
DeletingBranch: "Deleting branch '{{.branchName}}' (was {{.hash}})",
22002206
},
22012207
BreakingChangesTitle: "Breaking Changes",
22022208
BreakingChangesMessage: `You are updating to a new version of lazygit which contains breaking changes. Please review the notes below and update your configuration if necessary.

0 commit comments

Comments
 (0)