Skip to content

Commit

Permalink
unified hasLocalBranch function
Browse files Browse the repository at this point in the history
  • Loading branch information
hollesse committed Oct 17, 2024
1 parent a36976f commit 5061e61
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 32 deletions.
26 changes: 6 additions & 20 deletions mob.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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) {
Expand All @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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 + ") !")
}

Expand Down Expand Up @@ -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")
}
Expand Down
26 changes: 14 additions & 12 deletions mob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 5061e61

Please sign in to comment.