Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions pkg/commands/git_commands/main_branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,19 @@ func (self *MainBranches) Get() []string {

if self.existingMainBranches == nil || !slices.Equal(self.previousMainBranches, configuredMainBranches) {
self.existingMainBranches = self.determineMainBranches(configuredMainBranches)
self.previousMainBranches = configuredMainBranches
self.previousMainBranches = slices.Clone(configuredMainBranches)
}

return self.existingMainBranches
}

func (self *MainBranches) Invalidate() {
self.mutex.Lock()
defer self.mutex.Unlock()

self.existingMainBranches = nil
}

// Return the merge base of the given refName with the closest main branch.
func (self *MainBranches) GetMergeBase(refName string) string {
mainBranches := self.Get()
Expand All @@ -66,8 +73,8 @@ func (self *MainBranches) GetMergeBase(refName string) string {
// error is because one of the main branches has been deleted since the last
// call to determineMainBranches, or because the refName has no common
// history with any of the main branches. Since the former should happen
// very rarely, users must quit and restart lazygit to fix it; the latter is
// also not very common, but can totally happen and is not an error.
// very rarely and will be fixed on the next branch/commit refresh, we treat
// both cases as no merge base for now.

output, _, _ := self.cmd.New(
NewGitCmd("merge-base").Arg(refName).Arg(mainBranches...).
Expand Down
13 changes: 12 additions & 1 deletion pkg/gui/controllers/helpers/refresh_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func (self *RefreshHelper) Refresh(options types.RefreshOptions) {
// refreshing; the risk is one potential extra refresh, but capturing the
// snapshot at the end would risk missing one, which is worse.
self.updateRefsSnapshotIfRelevant(scopeSet)
self.invalidateMainBranchesIfRelevant(scopeSet)

wg := sync.WaitGroup{}
refresh := func(name string, f func()) {
Expand Down Expand Up @@ -306,7 +307,7 @@ func (self *RefreshHelper) RefsSnapshotChangedSince(snapshot string) bool {
// top of Refresh has already added these whenever REFLOG or BISECT_INFO are
// in scope, and whenever a nil scope was passed.
func (self *RefreshHelper) updateRefsSnapshotIfRelevant(scopeSet *set.Set[types.RefreshableView]) {
if !scopeSet.Includes(types.COMMITS) && !scopeSet.Includes(types.BRANCHES) {
if !refreshesRefs(scopeSet) {
return
}

Expand All @@ -318,6 +319,16 @@ func (self *RefreshHelper) updateRefsSnapshotIfRelevant(scopeSet *set.Set[types.
self.SetRefsSnapshot(snapshot)
}

func (self *RefreshHelper) invalidateMainBranchesIfRelevant(scopeSet *set.Set[types.RefreshableView]) {
if refreshesRefs(scopeSet) {
self.c.Model().MainBranches.Invalidate()
}
}

func refreshesRefs(scopeSet *set.Set[types.RefreshableView]) bool {
return scopeSet.Includes(types.COMMITS) || scopeSet.Includes(types.BRANCHES)
}

func getScopeNames(scopes []types.RefreshableView) []string {
scopeNameMap := map[types.RefreshableView]string{
types.COMMITS: "commits",
Expand Down
49 changes: 49 additions & 0 deletions pkg/integration/tests/branch/delete_after_main_branch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package branch

import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)

var DeleteAfterMainBranch = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Delete a local branch after deleting one of the configured main branches",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {},
SetupRepo: func(shell *Shell) {
shell.
EmptyCommit("first commit").
NewBranch("main").
Checkout("master").
NewBranch("dev").
Checkout("master")
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
t.Views().Branches().
Focus().
NavigateToLine(Contains("main")).
Press(keys.Universal.Remove).
Tap(func() {
t.ExpectPopup().
Menu().
Title(Equals("Delete branch 'main'?")).
Select(Contains("Delete local branch")).
Confirm()
}).
Lines(
Contains("master"),
Contains("dev").IsSelected(),
).
Press(keys.Universal.Remove).
Tap(func() {
t.ExpectPopup().
Menu().
Title(Equals("Delete branch 'dev'?")).
Select(Contains("Delete local branch")).
Confirm()
}).
Lines(
Contains("master").IsSelected(),
)
},
})
1 change: 1 addition & 0 deletions pkg/integration/tests/test_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var tests = []*components.IntegrationTest{
branch.CheckoutPreviousBranch,
branch.CreateTag,
branch.Delete,
branch.DeleteAfterMainBranch,
branch.DeleteMultiple,
branch.DeleteRemoteBranchWhenTagWithSameNameExists,
branch.DeleteRemoteBranchWithCredentialPrompt,
Expand Down
Loading