From 91f02ae1bdb234b307c900eeaecd4c8a82992c73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joshua=20T=C3=B6pfer?= Date: Fri, 11 Oct 2024 08:15:27 +0200 Subject: [PATCH] fix #451 --- CHANGELOG.md | 3 +++ mob.go | 24 ++++++++++++++++++++++-- mob_test.go | 23 ++++++++++++++++++++++- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f00e097..22fe3b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 5.3.3 +- Fix: `mob start` now functions correctly on WIP branches when the base branch is not checked out, applicable to branch names that do not contain a `-` character. + # 5.3.2 - Fix: Removed wrong warning about diverging wip branch when joining a new session diff --git a/mob.go b/mob.go index 51941de..893ce37 100644 --- a/mob.go +++ b/mob.go @@ -21,7 +21,7 @@ import ( ) const ( - versionNumber = "5.3.2" + versionNumber = "5.3.3" minimumGitVersion = "2.13.0" ) @@ -122,6 +122,19 @@ func (branch Branch) hasRemoteBranch(configuration config.Configuration) bool { return false } +func (branch Branch) hasLocalBranch(localBranches []string) bool { + say.Debug("Local Branches: " + strings.Join(localBranches, "\n")) + say.Debug("Local Branch: " + branch.Name) + + for i := 0; i < len(localBranches); i++ { + if localBranches[i] == branch.Name { + return true + } + } + + return false +} + func (branch Branch) IsWipBranch(configuration config.Configuration) bool { if branch.Name == "mob-session" { return true @@ -536,7 +549,9 @@ func start(configuration config.Configuration) error { } git("fetch", configuration.RemoteName, "--prune") - currentBaseBranch, currentWipBranch := determineBranches(gitCurrentBranch(), gitBranches(), configuration) + currentBranch := gitCurrentBranch() + localBranches := gitBranches() + currentBaseBranch, currentWipBranch := determineBranches(currentBranch, localBranches, configuration) if !currentWipBranch.hasRemoteBranch(configuration) && configuration.StartJoin { say.Error("Remote wip branch " + currentWipBranch.remote(configuration).String() + " is missing") @@ -551,6 +566,11 @@ func start(configuration config.Configuration) error { createRemoteBranch(configuration, currentBaseBranch) + if !currentBaseBranch.hasLocalBranch(localBranches) { + silentgit("checkout", "-b", currentBaseBranch.Name) + silentgit("checkout", currentBranch.Name) + } + if 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()) diff --git a/mob_test.go b/mob_test.go index a4cba54..ad9f832 100644 --- a/mob_test.go +++ b/mob_test.go @@ -1884,6 +1884,27 @@ func TestMobClean(t *testing.T) { assertNoMobSessionBranches(t, configuration, "mob-session") } +func TestMobStartOnWipBranchWithoutCheckedOutBaseBranchWithoutHyphens(t *testing.T) { + output, configuration := setup(t) + + setWorkingDir(tempDir + "/alice") + git("switch", "-C", "basebranchwithouthyphen") + configuration.StartCreate = true + start(configuration) + assertOnBranch(t, "mob/basebranchwithouthyphen") + createFile(t, "file1.txt", "abc") + next(configuration) + assertOnBranch(t, "basebranchwithouthyphen") + + setWorkingDir(tempDir + "/bob") + git("switch", "-C", "mob/basebranchwithouthyphen") + configuration.StartCreate = false + + assertNoError(t, start(configuration)) + assertOnBranch(t, "mob/basebranchwithouthyphen") + assertOutputContains(t, output, "joining existing session from origin/mob/basebranchwithouthyphen") +} + func TestGitVersionParse(t *testing.T) { // Check real examples equals(t, GitVersion{2, 34, 1}, parseGitVersion("git version 2.34.1")) @@ -2093,7 +2114,7 @@ func setWorkingDir(dir string) { func assertNoError(t *testing.T, err error) { if err != nil { - failWithFailure(t, err, nil) + failWithFailure(t, nil, err) } }