Skip to content

Commit 5061e61

Browse files
committed
unified hasLocalBranch function
1 parent a36976f commit 5061e61

File tree

2 files changed

+20
-32
lines changed

2 files changed

+20
-32
lines changed

mob.go

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ func (branch Branch) hasRemoteBranch(configuration config.Configuration) bool {
122122
return false
123123
}
124124

125-
func (branch Branch) hasLocalBranch(localBranches []string) bool {
125+
func (branch Branch) hasLocalBranch() bool {
126+
localBranches := gitBranches()
126127
say.Debug("Local Branches: " + strings.Join(localBranches, "\n"))
127128
say.Debug("Local Branch: " + branch.Name)
128129

@@ -529,7 +530,7 @@ func deleteRemoteWipBranch(configuration config.Configuration) {
529530
currentBaseBranch, currentWipBranch := determineBranches(gitCurrentBranch(), gitBranches(), configuration)
530531

531532
git("checkout", currentBaseBranch.String())
532-
if hasLocalBranch(currentWipBranch.String()) {
533+
if currentWipBranch.hasLocalBranch() {
533534
git("branch", "--delete", "--force", currentWipBranch.String())
534535
}
535536
if currentWipBranch.hasRemoteBranch(configuration) {
@@ -550,8 +551,7 @@ func start(configuration config.Configuration) error {
550551

551552
git("fetch", configuration.RemoteName, "--prune")
552553
currentBranch := gitCurrentBranch()
553-
localBranches := gitBranches()
554-
currentBaseBranch, currentWipBranch := determineBranches(currentBranch, localBranches, configuration)
554+
currentBaseBranch, currentWipBranch := determineBranches(currentBranch, gitBranches(), configuration)
555555

556556
if !currentWipBranch.hasRemoteBranch(configuration) && configuration.StartJoin {
557557
say.Error("Remote wip branch " + currentWipBranch.remote(configuration).String() + " is missing")
@@ -566,7 +566,7 @@ func start(configuration config.Configuration) error {
566566

567567
createRemoteBranch(configuration, currentBaseBranch)
568568

569-
if currentBaseBranch.hasLocalBranch(localBranches) && currentBaseBranch.hasUnpushedCommits(configuration) {
569+
if currentBaseBranch.hasLocalBranch() && currentBaseBranch.hasUnpushedCommits(configuration) {
570570
say.Error("cannot start; unpushed changes on base branch must be pushed upstream")
571571
say.Fix("to fix this, push those commits and try again", "git push "+configuration.RemoteName+" "+currentBaseBranch.String())
572572
return errors.New("cannot start; unpushed changes on base branch must be pushed upstream")
@@ -766,7 +766,7 @@ func startJoinMobSession(configuration config.Configuration) {
766766
baseBranch, currentWipBranch := determineBranches(gitCurrentBranch(), gitBranches(), configuration)
767767

768768
say.Info("joining existing session from " + currentWipBranch.remote(configuration).String())
769-
if hasLocalBranch(currentWipBranch.Name) && doBranchesDiverge(baseBranch.remote(configuration).Name, currentWipBranch.Name) {
769+
if currentWipBranch.hasLocalBranch() && doBranchesDiverge(baseBranch.remote(configuration).Name, currentWipBranch.Name) {
770770
say.Warning("Careful, your wip branch (" + currentWipBranch.Name + ") diverges from your main branch (" + baseBranch.remote(configuration).Name + ") !")
771771
}
772772

@@ -1057,20 +1057,6 @@ func isMobProgramming(configuration config.Configuration) bool {
10571057
return currentWipBranch == currentBranch
10581058
}
10591059

1060-
func hasLocalBranch(localBranch string) bool {
1061-
localBranches := gitBranches()
1062-
say.Debug("Local Branches: " + strings.Join(localBranches, "\n"))
1063-
say.Debug("Local Branch: " + localBranch)
1064-
1065-
for i := 0; i < len(localBranches); i++ {
1066-
if localBranches[i] == localBranch {
1067-
return true
1068-
}
1069-
}
1070-
1071-
return false
1072-
}
1073-
10741060
func gitBranches() []string {
10751061
return strings.Split(silentgit("branch", "--format=%(refname:short)"), "\n")
10761062
}

mob_test.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2255,33 +2255,35 @@ func assertOutputNotContains(t *testing.T, output *string, notContains string) {
22552255
}
22562256
}
22572257

2258-
func assertMobSessionBranches(t *testing.T, configuration config.Configuration, branch string) {
2259-
if !newBranch(branch).hasRemoteBranch(configuration) {
2260-
failWithFailure(t, newBranch(branch).remote(configuration).Name, "none")
2258+
func assertMobSessionBranches(t *testing.T, configuration config.Configuration, branchName string) {
2259+
branch := newBranch(branchName)
2260+
if !branch.hasRemoteBranch(configuration) {
2261+
failWithFailure(t, branch.remote(configuration).Name, "none")
22612262
}
2262-
if !hasLocalBranch(branch) {
2263-
failWithFailure(t, branch, "none")
2263+
if !branch.hasLocalBranch() {
2264+
failWithFailure(t, branchName, "none")
22642265
}
22652266
}
22662267

22672268
func assertLocalBranch(t *testing.T, branch string) {
2268-
if !hasLocalBranch(branch) {
2269+
if !newBranch(branch).hasLocalBranch() {
22692270
failWithFailure(t, branch, "none")
22702271
}
22712272
}
22722273

22732274
func assertNoLocalBranch(t *testing.T, branch string) {
2274-
if hasLocalBranch(branch) {
2275+
if newBranch(branch).hasLocalBranch() {
22752276
failWithFailure(t, branch, "none")
22762277
}
22772278
}
22782279

2279-
func assertNoMobSessionBranches(t *testing.T, configuration config.Configuration, branch string) {
2280-
if newBranch(branch).hasRemoteBranch(configuration) {
2281-
failWithFailure(t, "none", newBranch(branch).remote(configuration).Name)
2280+
func assertNoMobSessionBranches(t *testing.T, configuration config.Configuration, branchName string) {
2281+
branch := newBranch(branchName)
2282+
if branch.hasRemoteBranch(configuration) {
2283+
failWithFailure(t, "none", branch.remote(configuration).Name)
22822284
}
2283-
if hasLocalBranch(branch) {
2284-
failWithFailure(t, "none", branch)
2285+
if branch.hasLocalBranch() {
2286+
failWithFailure(t, "none", branchName)
22852287
}
22862288
}
22872289

0 commit comments

Comments
 (0)