Skip to content

Commit ac52ce6

Browse files
authored
Merge pull request #459 from remotemobprogramming/451-fix
fix #451
2 parents e8d2a56 + 5061e61 commit ac52ce6

File tree

4 files changed

+80
-43
lines changed

4 files changed

+80
-43
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 5.3.3
2+
- 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.
3+
14
# 5.3.2
25
- Fix: Removed wrong warning about diverging wip branch when joining a new session
36

mob.go

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
)
2222

2323
const (
24-
versionNumber = "5.3.2"
24+
versionNumber = "5.3.3"
2525
minimumGitVersion = "2.13.0"
2626
)
2727

@@ -122,6 +122,20 @@ func (branch Branch) hasRemoteBranch(configuration config.Configuration) bool {
122122
return false
123123
}
124124

125+
func (branch Branch) hasLocalBranch() bool {
126+
localBranches := gitBranches()
127+
say.Debug("Local Branches: " + strings.Join(localBranches, "\n"))
128+
say.Debug("Local Branch: " + branch.Name)
129+
130+
for i := 0; i < len(localBranches); i++ {
131+
if localBranches[i] == branch.Name {
132+
return true
133+
}
134+
}
135+
136+
return false
137+
}
138+
125139
func (branch Branch) IsWipBranch(configuration config.Configuration) bool {
126140
if branch.Name == "mob-session" {
127141
return true
@@ -516,7 +530,7 @@ func deleteRemoteWipBranch(configuration config.Configuration) {
516530
currentBaseBranch, currentWipBranch := determineBranches(gitCurrentBranch(), gitBranches(), configuration)
517531

518532
git("checkout", currentBaseBranch.String())
519-
if hasLocalBranch(currentWipBranch.String()) {
533+
if currentWipBranch.hasLocalBranch() {
520534
git("branch", "--delete", "--force", currentWipBranch.String())
521535
}
522536
if currentWipBranch.hasRemoteBranch(configuration) {
@@ -536,7 +550,8 @@ func start(configuration config.Configuration) error {
536550
}
537551

538552
git("fetch", configuration.RemoteName, "--prune")
539-
currentBaseBranch, currentWipBranch := determineBranches(gitCurrentBranch(), gitBranches(), configuration)
553+
currentBranch := gitCurrentBranch()
554+
currentBaseBranch, currentWipBranch := determineBranches(currentBranch, gitBranches(), configuration)
540555

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

552567
createRemoteBranch(configuration, currentBaseBranch)
553568

554-
if currentBaseBranch.hasUnpushedCommits(configuration) {
569+
if currentBaseBranch.hasLocalBranch() && currentBaseBranch.hasUnpushedCommits(configuration) {
555570
say.Error("cannot start; unpushed changes on base branch must be pushed upstream")
556571
say.Fix("to fix this, push those commits and try again", "git push "+configuration.RemoteName+" "+currentBaseBranch.String())
557572
return errors.New("cannot start; unpushed changes on base branch must be pushed upstream")
@@ -590,7 +605,7 @@ func start(configuration config.Configuration) error {
590605
}
591606

592607
say.Info("you are on wip branch '" + currentWipBranch.String() + "' (base branch '" + currentBaseBranch.String() + "')")
593-
sayLastCommitsList(currentBaseBranch.String(), currentWipBranch.String())
608+
sayLastCommitsList(currentBaseBranch, currentWipBranch, configuration)
594609

595610
openLastModifiedFileIfPresent(configuration)
596611

@@ -751,7 +766,7 @@ func startJoinMobSession(configuration config.Configuration) {
751766
baseBranch, currentWipBranch := determineBranches(gitCurrentBranch(), gitBranches(), configuration)
752767

753768
say.Info("joining existing session from " + currentWipBranch.remote(configuration).String())
754-
if hasLocalBranch(currentWipBranch.Name) && doBranchesDiverge(baseBranch.remote(configuration).Name, currentWipBranch.Name) {
769+
if currentWipBranch.hasLocalBranch() && doBranchesDiverge(baseBranch.remote(configuration).Name, currentWipBranch.Name) {
755770
say.Warning("Careful, your wip branch (" + currentWipBranch.Name + ") diverges from your main branch (" + baseBranch.remote(configuration).Name + ") !")
756771
}
757772

@@ -1001,12 +1016,16 @@ func squashOrCommit(configuration config.Configuration) string {
10011016
}
10021017
}
10031018

1004-
func sayLastCommitsList(currentBaseBranch string, currentWipBranch string) {
1005-
commitsBaseWipBranch := currentBaseBranch + ".." + currentWipBranch
1006-
log := silentgit("--no-pager", "log", commitsBaseWipBranch, "--pretty=format:%h %cr <%an>", "--abbrev-commit")
1019+
func sayLastCommitsList(currentBaseBranch Branch, currentWipBranch Branch, configuration config.Configuration) {
1020+
commitsBaseWipBranch := currentBaseBranch.String() + ".." + currentWipBranch.String()
1021+
log, err := silentgitignorefailure("--no-pager", "log", commitsBaseWipBranch, "--pretty=format:%h %cr <%an>", "--abbrev-commit")
1022+
if err != nil {
1023+
commitsBaseWipBranch = currentBaseBranch.remote(configuration).String() + ".." + currentWipBranch.String()
1024+
log = silentgit("--no-pager", "log", commitsBaseWipBranch, "--pretty=format:%h %cr <%an>", "--abbrev-commit")
1025+
}
10071026
lines := strings.Split(log, "\n")
10081027
if len(lines) > 5 {
1009-
say.Info("wip branch '" + currentWipBranch + "' contains " + strconv.Itoa(len(lines)) + " commits. The last 5 were:")
1028+
say.Info("wip branch '" + currentWipBranch.String() + "' contains " + strconv.Itoa(len(lines)) + " commits. The last 5 were:")
10101029
lines = lines[:5]
10111030
}
10121031
ReverseSlice(lines)
@@ -1038,20 +1057,6 @@ func isMobProgramming(configuration config.Configuration) bool {
10381057
return currentWipBranch == currentBranch
10391058
}
10401059

1041-
func hasLocalBranch(localBranch string) bool {
1042-
localBranches := gitBranches()
1043-
say.Debug("Local Branches: " + strings.Join(localBranches, "\n"))
1044-
say.Debug("Local Branch: " + localBranch)
1045-
1046-
for i := 0; i < len(localBranches); i++ {
1047-
if localBranches[i] == localBranch {
1048-
return true
1049-
}
1050-
}
1051-
1052-
return false
1053-
}
1054-
10551060
func gitBranches() []string {
10561061
return strings.Split(silentgit("branch", "--format=%(refname:short)"), "\n")
10571062
}
@@ -1074,7 +1079,8 @@ func doBranchesDiverge(ancestor string, successor string) bool {
10741079
}
10751080

10761081
func gitUserName() string {
1077-
return silentgitignorefailure("config", "--get", "user.name")
1082+
output, _ := silentgitignorefailure("config", "--get", "user.name")
1083+
return output
10781084
}
10791085

10801086
func gitUserEmail() string {
@@ -1130,13 +1136,13 @@ func silentgit(args ...string) string {
11301136
return strings.TrimSpace(output)
11311137
}
11321138

1133-
func silentgitignorefailure(args ...string) string {
1139+
func silentgitignorefailure(args ...string) (string, error) {
11341140
_, output, err := runCommandSilent("git", args...)
11351141

11361142
if err != nil {
1137-
return ""
1143+
return "", err
11381144
}
1139-
return strings.TrimSpace(output)
1145+
return strings.TrimSpace(output), nil
11401146
}
11411147

11421148
func deleteEmptyStrings(s []string) []string {
@@ -1205,7 +1211,8 @@ func gitIgnoreFailure(args ...string) error {
12051211
}
12061212

12071213
func gitCommitHash() string {
1208-
return silentgitignorefailure("rev-parse", "HEAD")
1214+
output, _ := silentgitignorefailure("rev-parse", "HEAD")
1215+
return output
12091216
}
12101217

12111218
func gitVersion() string {

mob_test.go

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,6 +1884,31 @@ func TestMobClean(t *testing.T) {
18841884
assertNoMobSessionBranches(t, configuration, "mob-session")
18851885
}
18861886

1887+
func TestMobStartOnWipBranchWithoutCheckedOutBaseBranchWithoutHyphens(t *testing.T) {
1888+
output, configuration := setup(t)
1889+
1890+
setWorkingDir(tempDir + "/alice")
1891+
git("checkout", "-b", "basebranchwithouthyphen")
1892+
configuration.StartCreate = true
1893+
start(configuration)
1894+
assertOnBranch(t, "mob/basebranchwithouthyphen")
1895+
createFile(t, "file1.txt", "abc")
1896+
next(configuration)
1897+
assertOnBranch(t, "basebranchwithouthyphen")
1898+
1899+
setWorkingDir(tempDir + "/bob")
1900+
git("checkout", "-b", "mob/basebranchwithouthyphen")
1901+
configuration.StartCreate = false
1902+
1903+
assertNoError(t, start(configuration))
1904+
assertOnBranch(t, "mob/basebranchwithouthyphen")
1905+
assertOutputContains(t, output, "joining existing session from origin/mob/basebranchwithouthyphen")
1906+
1907+
createFile(t, "file2.txt", "abc")
1908+
done(configuration)
1909+
assertOnBranch(t, "basebranchwithouthyphen")
1910+
}
1911+
18871912
func TestGitVersionParse(t *testing.T) {
18881913
// Check real examples
18891914
equals(t, GitVersion{2, 34, 1}, parseGitVersion("git version 2.34.1"))
@@ -2093,7 +2118,7 @@ func setWorkingDir(dir string) {
20932118

20942119
func assertNoError(t *testing.T, err error) {
20952120
if err != nil {
2096-
failWithFailure(t, err, nil)
2121+
failWithFailure(t, nil, err)
20972122
}
20982123

20992124
}
@@ -2230,33 +2255,35 @@ func assertOutputNotContains(t *testing.T, output *string, notContains string) {
22302255
}
22312256
}
22322257

2233-
func assertMobSessionBranches(t *testing.T, configuration config.Configuration, branch string) {
2234-
if !newBranch(branch).hasRemoteBranch(configuration) {
2235-
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")
22362262
}
2237-
if !hasLocalBranch(branch) {
2238-
failWithFailure(t, branch, "none")
2263+
if !branch.hasLocalBranch() {
2264+
failWithFailure(t, branchName, "none")
22392265
}
22402266
}
22412267

22422268
func assertLocalBranch(t *testing.T, branch string) {
2243-
if !hasLocalBranch(branch) {
2269+
if !newBranch(branch).hasLocalBranch() {
22442270
failWithFailure(t, branch, "none")
22452271
}
22462272
}
22472273

22482274
func assertNoLocalBranch(t *testing.T, branch string) {
2249-
if hasLocalBranch(branch) {
2275+
if newBranch(branch).hasLocalBranch() {
22502276
failWithFailure(t, branch, "none")
22512277
}
22522278
}
22532279

2254-
func assertNoMobSessionBranches(t *testing.T, configuration config.Configuration, branch string) {
2255-
if newBranch(branch).hasRemoteBranch(configuration) {
2256-
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)
22572284
}
2258-
if hasLocalBranch(branch) {
2259-
failWithFailure(t, "none", branch)
2285+
if branch.hasLocalBranch() {
2286+
failWithFailure(t, "none", branchName)
22602287
}
22612288
}
22622289

status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func status(configuration config.Configuration) {
1010
currentBaseBranch, currentWipBranch := determineBranches(gitCurrentBranch(), gitBranches(), configuration)
1111
say.Info("you are on wip branch " + currentWipBranch.String() + " (base branch " + currentBaseBranch.String() + ")")
1212

13-
sayLastCommitsList(currentBaseBranch.String(), currentWipBranch.String())
13+
sayLastCommitsList(currentBaseBranch, currentWipBranch, configuration)
1414
} else {
1515
currentBaseBranch, _ := determineBranches(gitCurrentBranch(), gitBranches(), configuration)
1616
say.Info("you are on base branch '" + currentBaseBranch.String() + "'")

0 commit comments

Comments
 (0)