From 5061e61509ce08f4f0f73849a939ba5496519c27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joshua=20T=C3=B6pfer?= Date: Thu, 17 Oct 2024 17:03:52 +0200 Subject: [PATCH] unified hasLocalBranch function --- mob.go | 26 ++++++-------------------- mob_test.go | 26 ++++++++++++++------------ 2 files changed, 20 insertions(+), 32 deletions(-) diff --git a/mob.go b/mob.go index f02bce7..0e07ef2 100644 --- a/mob.go +++ b/mob.go @@ -122,7 +122,8 @@ func (branch Branch) hasRemoteBranch(configuration config.Configuration) bool { return false } -func (branch Branch) hasLocalBranch(localBranches []string) bool { +func (branch Branch) hasLocalBranch() bool { + localBranches := gitBranches() say.Debug("Local Branches: " + strings.Join(localBranches, "\n")) say.Debug("Local Branch: " + branch.Name) @@ -529,7 +530,7 @@ func deleteRemoteWipBranch(configuration config.Configuration) { currentBaseBranch, currentWipBranch := determineBranches(gitCurrentBranch(), gitBranches(), configuration) git("checkout", currentBaseBranch.String()) - if hasLocalBranch(currentWipBranch.String()) { + if currentWipBranch.hasLocalBranch() { git("branch", "--delete", "--force", currentWipBranch.String()) } if currentWipBranch.hasRemoteBranch(configuration) { @@ -550,8 +551,7 @@ func start(configuration config.Configuration) error { git("fetch", configuration.RemoteName, "--prune") currentBranch := gitCurrentBranch() - localBranches := gitBranches() - currentBaseBranch, currentWipBranch := determineBranches(currentBranch, localBranches, configuration) + currentBaseBranch, currentWipBranch := determineBranches(currentBranch, gitBranches(), configuration) if !currentWipBranch.hasRemoteBranch(configuration) && configuration.StartJoin { say.Error("Remote wip branch " + currentWipBranch.remote(configuration).String() + " is missing") @@ -566,7 +566,7 @@ func start(configuration config.Configuration) error { createRemoteBranch(configuration, currentBaseBranch) - if currentBaseBranch.hasLocalBranch(localBranches) && currentBaseBranch.hasUnpushedCommits(configuration) { + if currentBaseBranch.hasLocalBranch() && currentBaseBranch.hasUnpushedCommits(configuration) { say.Error("cannot start; unpushed changes on base branch must be pushed upstream") say.Fix("to fix this, push those commits and try again", "git push "+configuration.RemoteName+" "+currentBaseBranch.String()) return errors.New("cannot start; unpushed changes on base branch must be pushed upstream") @@ -766,7 +766,7 @@ func startJoinMobSession(configuration config.Configuration) { baseBranch, currentWipBranch := determineBranches(gitCurrentBranch(), gitBranches(), configuration) say.Info("joining existing session from " + currentWipBranch.remote(configuration).String()) - if hasLocalBranch(currentWipBranch.Name) && doBranchesDiverge(baseBranch.remote(configuration).Name, currentWipBranch.Name) { + if currentWipBranch.hasLocalBranch() && doBranchesDiverge(baseBranch.remote(configuration).Name, currentWipBranch.Name) { say.Warning("Careful, your wip branch (" + currentWipBranch.Name + ") diverges from your main branch (" + baseBranch.remote(configuration).Name + ") !") } @@ -1057,20 +1057,6 @@ func isMobProgramming(configuration config.Configuration) bool { return currentWipBranch == currentBranch } -func hasLocalBranch(localBranch string) bool { - localBranches := gitBranches() - say.Debug("Local Branches: " + strings.Join(localBranches, "\n")) - say.Debug("Local Branch: " + localBranch) - - for i := 0; i < len(localBranches); i++ { - if localBranches[i] == localBranch { - return true - } - } - - return false -} - func gitBranches() []string { return strings.Split(silentgit("branch", "--format=%(refname:short)"), "\n") } diff --git a/mob_test.go b/mob_test.go index 4ac1e86..af95d31 100644 --- a/mob_test.go +++ b/mob_test.go @@ -2255,33 +2255,35 @@ func assertOutputNotContains(t *testing.T, output *string, notContains string) { } } -func assertMobSessionBranches(t *testing.T, configuration config.Configuration, branch string) { - if !newBranch(branch).hasRemoteBranch(configuration) { - failWithFailure(t, newBranch(branch).remote(configuration).Name, "none") +func assertMobSessionBranches(t *testing.T, configuration config.Configuration, branchName string) { + branch := newBranch(branchName) + if !branch.hasRemoteBranch(configuration) { + failWithFailure(t, branch.remote(configuration).Name, "none") } - if !hasLocalBranch(branch) { - failWithFailure(t, branch, "none") + if !branch.hasLocalBranch() { + failWithFailure(t, branchName, "none") } } func assertLocalBranch(t *testing.T, branch string) { - if !hasLocalBranch(branch) { + if !newBranch(branch).hasLocalBranch() { failWithFailure(t, branch, "none") } } func assertNoLocalBranch(t *testing.T, branch string) { - if hasLocalBranch(branch) { + if newBranch(branch).hasLocalBranch() { failWithFailure(t, branch, "none") } } -func assertNoMobSessionBranches(t *testing.T, configuration config.Configuration, branch string) { - if newBranch(branch).hasRemoteBranch(configuration) { - failWithFailure(t, "none", newBranch(branch).remote(configuration).Name) +func assertNoMobSessionBranches(t *testing.T, configuration config.Configuration, branchName string) { + branch := newBranch(branchName) + if branch.hasRemoteBranch(configuration) { + failWithFailure(t, "none", branch.remote(configuration).Name) } - if hasLocalBranch(branch) { - failWithFailure(t, "none", branch) + if branch.hasLocalBranch() { + failWithFailure(t, "none", branchName) } }