Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# 5.4.4
- Feature: `mob start --create` now takes an optional branch name (#211). `mob start --create <branch>` creates that branch from the current branch's up-to-date remote, pushes it, and starts the mob session on it. If the branch already exists locally or on the remote, it is checked out instead, so everyone in the mob can run the same command.
- Feature: Release a Linux arm64 build (`mob_<version>_linux_arm64.tar.gz`). The install script and the AUR package `mobsh-bin` pick it up automatically on arm64 machines.

# 5.4.3
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ Basic Commands(Options):
[--include-uncommitted-changes|-i] Move uncommitted changes to wip branch
[--discard-uncommitted-changes|-d] Discard uncommitted changes
[--branch|-b <branch-postfix>] Set wip branch to 'mob/<base-branch>-<branch-postfix>'
[--create|-c] Create the remote branch
[--create|-c [<branch>]] Create the remote branch; with <branch>, create or switch to that base branch first
[--room <room-name>] Set room name for timer.mob.sh once
next
[--stay|-s] Stay on wip branch (default)
Expand Down
20 changes: 19 additions & 1 deletion configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ type Configuration struct {
NotifyMessage string // override with MOB_NOTIFY_MESSAGE
NextStay bool // override with MOB_NEXT_STAY
HandleUncommittedChanges string
StartCreate bool // override with MOB_START_CREATE variable
StartCreate bool // override with MOB_START_CREATE variable
StartCreateBranch string
StartJoin bool
StashName string // override with MOB_STASH_NAME
WipBranchQualifier string // override with MOB_WIP_BRANCH_QUALIFIER
Expand Down Expand Up @@ -152,6 +153,10 @@ func ParseArgs(args []string, configuration Configuration) (command string, para
newConfiguration.DoneSquash = SquashWip
case "--create", "-c":
newConfiguration.StartCreate = true
if i+1 < len(args) && isBranchName(args[i+1]) {
newConfiguration.StartCreateBranch = args[i+1]
i++ // skip consumed parameter
}
case "--join", "-j":
newConfiguration.StartJoin = true
case "--delete-remote-wip-branch":
Expand All @@ -174,6 +179,19 @@ func ParseArgs(args []string, configuration Configuration) (command string, para
return
}

// isBranchName reports whether an argument following --create / -c is a branch
// name: anything that is not another option and not a number (a number is the
// timer).
func isBranchName(arg string) bool {
if strings.HasPrefix(arg, "-") {
return false
}
if _, err := strconv.Atoi(arg); err == nil {
return false
}
return true
}

func GetDefaultConfiguration() Configuration {
voiceCommand := ""
notifyCommand := ""
Expand Down
56 changes: 56 additions & 0 deletions configuration/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,62 @@ func TestParseArgsStartCreateShort(t *testing.T) {
test.Equals(t, true, configuration.StartCreate)
}

func TestParseArgsStartCreateWithBranch(t *testing.T) {
configuration := GetDefaultConfiguration()

command, parameters, configuration := ParseArgs([]string{"mob", "start", "--create", "feature/story-1"}, configuration)

test.Equals(t, "start", command)
test.Equals(t, "", strings.Join(parameters, ""))
test.Equals(t, "feature/story-1", configuration.StartCreateBranch)
test.Equals(t, true, configuration.StartCreate)
}

func TestParseArgsStartCreateWithBranchAndTimer(t *testing.T) {
configuration := GetDefaultConfiguration()

command, parameters, configuration := ParseArgs([]string{"mob", "start", "-c", "feature/story-1", "10"}, configuration)

test.Equals(t, "start", command)
test.Equals(t, "10", strings.Join(parameters, ""))
test.Equals(t, "feature/story-1", configuration.StartCreateBranch)
test.Equals(t, true, configuration.StartCreate)
}

func TestParseArgsStartTimerThenCreateWithBranch(t *testing.T) {
configuration := GetDefaultConfiguration()

command, parameters, configuration := ParseArgs([]string{"mob", "start", "10", "-c", "feature/story-1"}, configuration)

test.Equals(t, "start", command)
test.Equals(t, "10", strings.Join(parameters, ""))
test.Equals(t, "feature/story-1", configuration.StartCreateBranch)
test.Equals(t, true, configuration.StartCreate)
}

func TestParseArgsStartCreateWithNumberKeepsTimer(t *testing.T) {
configuration := GetDefaultConfiguration()

command, parameters, configuration := ParseArgs([]string{"mob", "start", "-c", "10"}, configuration)

test.Equals(t, "start", command)
test.Equals(t, "10", strings.Join(parameters, ""))
test.Equals(t, "", configuration.StartCreateBranch)
test.Equals(t, true, configuration.StartCreate)
}

func TestParseArgsStartCreateFollowedByOption(t *testing.T) {
configuration := GetDefaultConfiguration()

command, parameters, configuration := ParseArgs([]string{"mob", "start", "-c", "--branch", "green"}, configuration)

test.Equals(t, "start", command)
test.Equals(t, "", strings.Join(parameters, ""))
test.Equals(t, "", configuration.StartCreateBranch)
test.Equals(t, true, configuration.StartCreate)
test.Equals(t, "green", configuration.WipBranchQualifier)
}

func TestParseArgsStartJoin(t *testing.T) {
configuration := GetDefaultConfiguration()

Expand Down
2 changes: 1 addition & 1 deletion help/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Basic Commands with Options:
[--include-uncommitted-changes|-i] Move uncommitted changes to wip branch
[--discard-uncommitted-changes|-d] Discard uncommitted changes
[--branch|-b <branch-postfix>] Set wip branch to 'mob/<base-branch>` + configuration.WipBranchQualifierSeparator + `<branch-postfix>'
[--create|-c] Create the remote branch
[--create|-c [<branch>]] Create the remote branch; with <branch>, create or switch to that base branch first
[--join|-j] Join existing wip branch
[--room <room-name>] Set room name for timer.mob.sh once
next
Expand Down
115 changes: 107 additions & 8 deletions mob.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,12 @@ func execute(command string, parameter []string, configuration config.Configurat

switch command {
case "s", "start":
if containsAny(args, "--create", "-c") && len(parameter) > 1 {
say.Error("too many arguments: " + strings.Join(parameter, " "))
say.Fix("to create a branch and set a timer, use", configuration.Mob("start --create <branch> <timer>"))
exit.Exit(1)
return
}
err := start(configuration)
if !isMobProgramming(configuration) || err != nil {
exit.Exit(1)
Expand Down Expand Up @@ -505,6 +511,22 @@ func start(configuration config.Configuration) error {
}

git("fetch", configuration.RemoteName, "--prune")

if configuration.StartCreateBranch != "" {
if err := validateStartBranch(configuration); err != nil {
return err
}
if uncommittedChanges && configuration.HandleUncommittedChanges == config.DiscardChanges {
git("reset", "--hard")
}
if uncommittedChanges && configuration.HandleUncommittedChanges == config.IncludeChanges {
if err := stashUncommittedChanges(configuration); err != nil {
return err
}
}
switchToStartBranch(configuration)
}

currentBranch := gitCurrentBranch()
currentBaseBranch, currentWipBranch := determineBranches(currentBranch, gitLocalAndRemoteBranches(configuration), configuration)

Expand All @@ -531,18 +553,18 @@ func start(configuration config.Configuration) error {
git("reset", "--hard")
}

if uncommittedChanges && configuration.HandleUncommittedChanges == config.IncludeChanges {
if silentgit("ls-tree", "-r", "HEAD", "--full-name", "--name-only", ".") == "" {
say.Error("cannot start; current working dir is an uncommitted subdir")
say.Fix("to fix this, go to the parent directory and try again", "cd ..")
return errors.New("cannot start; current working dir is an uncommitted subdir")
if uncommittedChanges && configuration.HandleUncommittedChanges == config.IncludeChanges && hasUncommittedChanges() {
if err := stashUncommittedChanges(configuration); err != nil {
return err
}
git("stash", "push", "--include-untracked", "--message", configuration.StashName)
say.Info("uncommitted changes were stashed. If an error occurs later on, you can recover them with 'git stash pop'.")
}

if !isMobProgramming(configuration) {
git("merge", "FETCH_HEAD", "--ff-only")
if configuration.StartCreateBranch != "" {
git("merge", currentBaseBranch.remote(configuration).String(), "--ff-only")
} else {
git("merge", "FETCH_HEAD", "--ff-only")
}
}

if currentWipBranch.hasRemoteBranch(configuration) {
Expand Down Expand Up @@ -587,6 +609,9 @@ func sayFixUncommittedChanges(configuration config.Configuration) {
}

func createFix(configuration config.Configuration) string {
if configuration.StartCreateBranch != "" {
return " --create " + configuration.StartCreateBranch
}
if configuration.StartCreate {
return " --create"
}
Expand Down Expand Up @@ -615,6 +640,80 @@ func containsAny(list []string, elements ...string) bool {
return false
}

// validateStartBranch runs every check that can refuse the branch given to
// --create before start() touches the working tree (stash or reset).
func validateStartBranch(configuration config.Configuration) error {
branch := newBranch(configuration.StartCreateBranch)
currentBranch := gitCurrentBranch()

if branch.IsWipBranch(configuration) {
say.Error("cannot create branch " + branch.String() + " because it is a wip branch")
return errors.New("cannot create branch because it is a wip branch")
}

if branch == currentBranch {
return nil
}

if branch.hasLocalBranch() {
if branch.hasRemoteBranch(configuration) && branch.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+" "+branch.String())
return errors.New("cannot start; unpushed changes on base branch must be pushed upstream")
}
return nil
}

if branch.hasRemoteBranch(configuration) {
return nil
}

if currentBranch.IsWipBranch(configuration) {
say.Error("cannot create branch " + branch.String() + " while on wip branch " + currentBranch.String())
say.Fix("To finish the current session, use", configuration.Mob("done"))
return errors.New("cannot create branch while on wip branch")
}

if !currentBranch.hasRemoteBranch(configuration) {
say.Error("cannot create branch " + branch.String() + " because remote branch " + currentBranch.remote(configuration).String() + " is missing")
say.Fix("To push the current branch first, use", "git push "+configuration.RemoteName+" "+currentBranch.String()+" --set-upstream")
return errors.New("cannot create branch because the current branch has no remote branch")
}

return nil
}

func switchToStartBranch(configuration config.Configuration) {
branch := newBranch(configuration.StartCreateBranch)
currentBranch := gitCurrentBranch()

if branch == currentBranch {
return
}

if branch.hasLocalBranch() {
say.Info("switching to existing branch " + branch.String())
git("checkout", branch.String())
} else if branch.hasRemoteBranch(configuration) {
say.Info("checking out branch " + branch.String() + " from " + branch.remote(configuration).String())
git("checkout", "-b", branch.String(), branch.remote(configuration).String())
} else {
say.Info("creating branch " + branch.String() + " from " + currentBranch.remote(configuration).String())
git("checkout", "-b", branch.String(), "--no-track", currentBranch.remote(configuration).String())
}
}

func stashUncommittedChanges(configuration config.Configuration) error {
if silentgit("ls-tree", "-r", "HEAD", "--full-name", "--name-only", ".") == "" {
say.Error("cannot start; current working dir is an uncommitted subdir")
say.Fix("to fix this, go to the parent directory and try again", "cd ..")
return errors.New("cannot start; current working dir is an uncommitted subdir")
}
git("stash", "push", "--include-untracked", "--message", configuration.StashName)
say.Info("uncommitted changes were stashed. If an error occurs later on, you can recover them with 'git stash pop'.")
return nil
}

func createRemoteBranch(configuration config.Configuration, currentBaseBranch Branch) {
if !currentBaseBranch.hasRemoteBranch(configuration) && configuration.StartCreate {
git("push", configuration.RemoteName, currentBaseBranch.String(), "--set-upstream")
Expand Down
Loading
Loading