From fd8e4803633ba802e67652266ee5131841f1bf49 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Tue, 30 Jul 2024 19:38:36 +0200 Subject: [PATCH] Re-determine existing main branches if mainBranches config changed --- pkg/commands/git_commands/main_branches.go | 12 ++++++++---- pkg/utils/slice.go | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/pkg/commands/git_commands/main_branches.go b/pkg/commands/git_commands/main_branches.go index 85ac6f174..8cf08840a 100644 --- a/pkg/commands/git_commands/main_branches.go +++ b/pkg/commands/git_commands/main_branches.go @@ -18,6 +18,8 @@ type MainBranches struct { // depending on which one exists for a given bare name. existingMainBranches []string + previousMainBranches []string + cmd oscommands.ICmdObjBuilder mutex *deadlock.Mutex } @@ -40,8 +42,11 @@ func (self *MainBranches) Get() []string { self.mutex.Lock() defer self.mutex.Unlock() - if self.existingMainBranches == nil { - self.existingMainBranches = self.determineMainBranches() + configuredMainBranches := self.c.UserConfig().Git.MainBranches + + if self.existingMainBranches == nil || !utils.EqualSlices(self.previousMainBranches, configuredMainBranches) { + self.existingMainBranches = self.determineMainBranches(configuredMainBranches) + self.previousMainBranches = configuredMainBranches } return self.existingMainBranches @@ -71,11 +76,10 @@ func (self *MainBranches) GetMergeBase(refName string) string { return ignoringWarnings(output) } -func (self *MainBranches) determineMainBranches() []string { +func (self *MainBranches) determineMainBranches(configuredMainBranches []string) []string { var existingBranches []string var wg sync.WaitGroup - configuredMainBranches := self.c.UserConfig().Git.MainBranches existingBranches = make([]string, len(configuredMainBranches)) for i, branchName := range configuredMainBranches { diff --git a/pkg/utils/slice.go b/pkg/utils/slice.go index 0dc9e64e4..c84217b77 100644 --- a/pkg/utils/slice.go +++ b/pkg/utils/slice.go @@ -179,3 +179,18 @@ func Shift[T any](slice []T) (T, []T) { slice = slice[1:] return value, slice } + +// Compares two slices for equality +func EqualSlices[T comparable](slice1 []T, slice2 []T) bool { + if len(slice1) != len(slice2) { + return false + } + + for i := range slice1 { + if slice1[i] != slice2[i] { + return false + } + } + + return true +}