Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #451 #459

Merged
merged 4 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
65 changes: 36 additions & 29 deletions mob.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

const (
versionNumber = "5.3.2"
versionNumber = "5.3.3"
minimumGitVersion = "2.13.0"
)

Expand Down Expand Up @@ -122,6 +122,20 @@ func (branch Branch) hasRemoteBranch(configuration config.Configuration) bool {
return false
}

func (branch Branch) hasLocalBranch() bool {
localBranches := gitBranches()
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
Expand Down Expand Up @@ -516,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 @@ -536,7 +550,8 @@ func start(configuration config.Configuration) error {
}

git("fetch", configuration.RemoteName, "--prune")
currentBaseBranch, currentWipBranch := determineBranches(gitCurrentBranch(), gitBranches(), configuration)
currentBranch := gitCurrentBranch()
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 @@ -551,7 +566,7 @@ func start(configuration config.Configuration) error {

createRemoteBranch(configuration, currentBaseBranch)

if 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 @@ -590,7 +605,7 @@ func start(configuration config.Configuration) error {
}

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

openLastModifiedFileIfPresent(configuration)

Expand Down Expand Up @@ -751,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 @@ -1001,12 +1016,16 @@ func squashOrCommit(configuration config.Configuration) string {
}
}

func sayLastCommitsList(currentBaseBranch string, currentWipBranch string) {
commitsBaseWipBranch := currentBaseBranch + ".." + currentWipBranch
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simonharrer this is the place where we could also just use the currentRemoteBaseBranch, this would be a much easier solution.

log := silentgit("--no-pager", "log", commitsBaseWipBranch, "--pretty=format:%h %cr <%an>", "--abbrev-commit")
func sayLastCommitsList(currentBaseBranch Branch, currentWipBranch Branch, configuration config.Configuration) {
commitsBaseWipBranch := currentBaseBranch.String() + ".." + currentWipBranch.String()
log, err := silentgitignorefailure("--no-pager", "log", commitsBaseWipBranch, "--pretty=format:%h %cr <%an>", "--abbrev-commit")
if err != nil {
commitsBaseWipBranch = currentBaseBranch.remote(configuration).String() + ".." + currentWipBranch.String()
log = silentgit("--no-pager", "log", commitsBaseWipBranch, "--pretty=format:%h %cr <%an>", "--abbrev-commit")
}
lines := strings.Split(log, "\n")
if len(lines) > 5 {
say.Info("wip branch '" + currentWipBranch + "' contains " + strconv.Itoa(len(lines)) + " commits. The last 5 were:")
say.Info("wip branch '" + currentWipBranch.String() + "' contains " + strconv.Itoa(len(lines)) + " commits. The last 5 were:")
lines = lines[:5]
}
ReverseSlice(lines)
Expand Down Expand Up @@ -1038,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 All @@ -1074,7 +1079,8 @@ func doBranchesDiverge(ancestor string, successor string) bool {
}

func gitUserName() string {
return silentgitignorefailure("config", "--get", "user.name")
output, _ := silentgitignorefailure("config", "--get", "user.name")
return output
}

func gitUserEmail() string {
Expand Down Expand Up @@ -1130,13 +1136,13 @@ func silentgit(args ...string) string {
return strings.TrimSpace(output)
}

func silentgitignorefailure(args ...string) string {
func silentgitignorefailure(args ...string) (string, error) {
_, output, err := runCommandSilent("git", args...)

if err != nil {
return ""
return "", err
}
return strings.TrimSpace(output)
return strings.TrimSpace(output), nil
}

func deleteEmptyStrings(s []string) []string {
Expand Down Expand Up @@ -1205,7 +1211,8 @@ func gitIgnoreFailure(args ...string) error {
}

func gitCommitHash() string {
return silentgitignorefailure("rev-parse", "HEAD")
output, _ := silentgitignorefailure("rev-parse", "HEAD")
return output
}

func gitVersion() string {
Expand Down
53 changes: 40 additions & 13 deletions mob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1884,6 +1884,31 @@ func TestMobClean(t *testing.T) {
assertNoMobSessionBranches(t, configuration, "mob-session")
}

func TestMobStartOnWipBranchWithoutCheckedOutBaseBranchWithoutHyphens(t *testing.T) {
output, configuration := setup(t)

setWorkingDir(tempDir + "/alice")
git("checkout", "-b", "basebranchwithouthyphen")
configuration.StartCreate = true
start(configuration)
assertOnBranch(t, "mob/basebranchwithouthyphen")
createFile(t, "file1.txt", "abc")
next(configuration)
assertOnBranch(t, "basebranchwithouthyphen")

setWorkingDir(tempDir + "/bob")
git("checkout", "-b", "mob/basebranchwithouthyphen")
configuration.StartCreate = false

assertNoError(t, start(configuration))
assertOnBranch(t, "mob/basebranchwithouthyphen")
assertOutputContains(t, output, "joining existing session from origin/mob/basebranchwithouthyphen")

createFile(t, "file2.txt", "abc")
done(configuration)
assertOnBranch(t, "basebranchwithouthyphen")
}

func TestGitVersionParse(t *testing.T) {
// Check real examples
equals(t, GitVersion{2, 34, 1}, parseGitVersion("git version 2.34.1"))
Expand Down Expand Up @@ -2093,7 +2118,7 @@ func setWorkingDir(dir string) {

func assertNoError(t *testing.T, err error) {
if err != nil {
failWithFailure(t, err, nil)
failWithFailure(t, nil, err)
}

}
Expand Down Expand Up @@ -2230,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
2 changes: 1 addition & 1 deletion status.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func status(configuration config.Configuration) {
currentBaseBranch, currentWipBranch := determineBranches(gitCurrentBranch(), gitBranches(), configuration)
say.Info("you are on wip branch " + currentWipBranch.String() + " (base branch " + currentBaseBranch.String() + ")")

sayLastCommitsList(currentBaseBranch.String(), currentWipBranch.String())
sayLastCommitsList(currentBaseBranch, currentWipBranch, configuration)
} else {
currentBaseBranch, _ := determineBranches(gitCurrentBranch(), gitBranches(), configuration)
say.Info("you are on base branch '" + currentBaseBranch.String() + "'")
Expand Down
Loading