Repository: remotemobprogramming/mob
Pull request #292 (2022-07-03 21:41:06)

Warns about diverging wip branch


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index cae11f6..8aeb29e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,7 @@
 - Add a more specific error message if `git` is not installed.
 - Fix: `mob done --squash-wip` now successfully auto-merges auto-mergeable diverging changes.
 - Print the help output whenever any kind of help argument (`help`, `--help`, `-h`) is present in the command, e.g. `mob s 10 -h`.
+- Adds a warning to `mob start` in case the wip branch diverges from the main branch.
 - Various fixes in suggestion of next typist:
   - Show list of last committers only if there are any.
   - Skip suggestions if there has only been a single person so far.
diff --git a/mob.go b/mob.go
index 250ac9a..89e654d 100644
--- a/mob.go
+++ b/mob.go
@@ -1274,9 +1274,13 @@ func getWipBranchesForBaseBranch(currentBaseBranch Branch, configuration Configu
 }
 
 func startJoinMobSession(configuration Configuration) {
-	_, currentWipBranch := determineBranches(gitCurrentBranch(), gitBranches(), configuration)
+	baseBranch, currentWipBranch := determineBranches(gitCurrentBranch(), gitBranches(), configuration)
 
 	sayInfo("joining existing session from " + currentWipBranch.remote(configuration).String())
+	if doBranchesDiverge(baseBranch.remote(configuration).Name, currentWipBranch.Name) {
+		sayWarning("Careful, your wip branch (" + currentWipBranch.Name + ") diverges from your main branch (" + baseBranch.remote(configuration).Name + ") !")
+	}
+
 	git("checkout", "-B", currentWipBranch.Name, currentWipBranch.remote(configuration).Name)
 	git("branch", "--set-upstream-to="+currentWipBranch.remote(configuration).Name, currentWipBranch.Name)
 }
@@ -1591,6 +1595,14 @@ func gitCurrentBranch() Branch {
 	return newBranch(silentgit("rev-parse", "--abbrev-ref", "HEAD"))
 }
 
+func doBranchesDiverge(ancestor string, successor string) bool {
+	_, _, err := runCommandSilent("git", "merge-base", "--is-ancestor", ancestor, successor)
+	if err == nil {
+		return false
+	}
+	return true
+}
+
 func gitUserName() string {
 	return silentgitignorefailure("config", "--get", "user.name")
 }


Test patch:
diff --git a/mob_test.go b/mob_test.go
index 25a5d45..f0d3e0d 100644
--- a/mob_test.go
+++ b/mob_test.go
@@ -519,7 +519,7 @@ func TestStartNextStartWithBranch(t *testing.T) {
 	assertOnBranch(t, "mob/master-green")
 }
 
-func TestStartFromDivergingBranches(t *testing.T) {
+func TestStartWarnsAboutPreexistingWipBranches(t *testing.T) {
 	output, configuration := setup(t)
 	checkoutBranchAndCreateRemoteBranch("feature-something")
 	checkoutBranchAndCreateRemoteBranch("feature-something-2")
@@ -536,18 +536,20 @@ func TestStartFromDivergingBranches(t *testing.T) {
 	assertOutputContains(t, output, "mob/feature-something-2")
 }
 
-func TestStartFromDivergingBranches_noWarning(t *testing.T) {
+func TestStartWarnsOnDivergingWipBranch(t *testing.T) {
 	output, configuration := setup(t)
-	checkoutBranchAndCreateRemoteBranch("mob/feature-something")
-	checkoutBranchAndCreateRemoteBranch("feature-something")
-	checkoutBranchAndCreateRemoteBranch("mob/feature-something-2")
-	checkoutBranchAndCreateRemoteBranch("feature-something-2")
 
-	assertOnBranch(t, "feature-something-2")
 	start(configuration)
-	assertOnBranch(t, "mob/feature-something-2")
+	createFileAndCommitIt(t, "example.txt", "asdf", "asdf")
+	next(configuration)
 
-	assertOutputNotContains(t, output, "qualified mob branches detected")
+	git("checkout", "master")
+	createFileAndCommitIt(t, "example.txt", "other", "other")
+	git("push")
+
+	start(configuration)
+
+	assertOutputContains(t, output, "Careful, your wip branch (mob-session) diverges from your main branch (origin/master) !")
 }
 
 func TestStartNextOnFeatureWithBranch(t *testing.T) {
@@ -1536,6 +1538,29 @@ func TestSetMobDoneSquashEmptyStringValue(t *testing.T) {
 	equals(t, Squash, configuration.DoneSquash)
 }
 
+func TestBranchesDoNotDiverge(t *testing.T) {
+	setup(t)
+	createFileAndCommitIt(t, "example.txt", "asdf", "asdf")
+	git("checkout", "-b", "diverges")
+
+	diverge := doBranchesDiverge("master", "diverges")
+
+	equals(t, false, diverge)
+}
+
+func TestBranchesDoDiverge(t *testing.T) {
+	setup(t)
+	createFileAndCommitIt(t, "example.txt", "asdf", "asdf")
+	git("checkout", "-b", "diverges")
+	createFileAndCommitIt(t, "example.txt", "other", "asdf")
+	git("checkout", "master")
+	createFileAndCommitIt(t, "diverging-commit.txt", "asdf", "diverging")
+
+	diverge := doBranchesDiverge("master", "diverges")
+
+	equals(t, true, diverge)
+}
+
 func TestPrintDeprecatedDoneSquashMessageWhenValueIsTrue(t *testing.T) {
 	output, _ := setup(t)