Skip to content

Commit

Permalink
Allow git to say what branches are allowed (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmahsax authored Mar 16, 2024
1 parent 5929579 commit 571b195
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 66 deletions.
39 changes: 13 additions & 26 deletions cmd/newBranch/newBranch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package newBranch

import (
"fmt"
"regexp"

"github.com/emmahsax/go-git-helper/internal/commandline"
"github.com/emmahsax/go-git-helper/internal/executor"
Expand All @@ -27,7 +26,7 @@ func NewCommand() *cobra.Command {
Args: cobra.MaximumNArgs(1),
DisableFlagsInUseLine: true,
RunE: func(cmd *cobra.Command, args []string) error {
newNewBranch(determineBranch(args, debug), debug, executor.NewExecutor(debug)).execute()
newNewBranch(determineBranch(args), debug, executor.NewExecutor(debug)).execute()
return nil
},
}
Expand All @@ -45,45 +44,33 @@ func newNewBranch(branch string, debug bool, executor executor.ExecutorInterface
}
}

func determineBranch(args []string, debug bool) string {
func determineBranch(args []string) string {
if len(args) == 0 {
return getValidBranch()
return askForBranch()
} else {
if !isValidBranch(args[0]) {
fmt.Println("--- Invalid branch provided ---")
return getValidBranch()
} else {
return args[0]
}
return args[0]
}
}

func isValidBranch(branch string) bool {
validPattern := "^[a-zA-Z0-9-_]+$"
return regexp.MustCompile(validPattern).MatchString(branch)
func askForBranch() string {
return commandline.AskOpenEndedQuestion("New branch name", false)
}

func getValidBranch() string {
var branch string
func (nb *NewBranch) execute() {
fmt.Println("Attempting to create a new branch:", nb.Branch)
g := git.NewGit(nb.Debug, nb.Executor)
g.Pull()

for {
branch = commandline.AskOpenEndedQuestion("New branch name", false)

if isValidBranch(branch) {
err := g.CreateBranch(nb.Branch)
if err == nil {
break
}

fmt.Println("--- Invalid branch ---")
nb.Branch = askForBranch()
}

return branch
}

func (nb *NewBranch) execute() {
fmt.Println("Attempting to create a new branch:", nb.Branch)
g := git.NewGit(nb.Debug, nb.Executor)
g.Pull()
g.CreateBranch(nb.Branch)
g.Checkout(nb.Branch)
g.PushBranch(nb.Branch)
}
48 changes: 12 additions & 36 deletions cmd/newBranch/newBranch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,8 @@ func Test_determineBranch(t *testing.T) {
args []string
branch string
}{
{args: []string{}, branch: "hello-world"},
{args: []string{"hello-world"}, branch: "hello-world"},
{args: []string{"hello_world"}, branch: "hello_world"},
{args: []string{"hello world"}, branch: "hello-world"},
{args: []string{"hello_world!"}, branch: "hello-world"},
{args: []string{"hello_world?"}, branch: "hello-world"},
{args: []string{"#HelloWorld"}, branch: "hello-world"},
{args: []string{"hello-world*"}, branch: "hello-world"},
{args: []string{}, branch: "hello-something-or-other"},
{args: []string{"hello-world"}, branch: ""},
}

originalAskOpenEndedQuestion := commandline.AskOpenEndedQuestion
Expand All @@ -44,43 +38,25 @@ func Test_determineBranch(t *testing.T) {
return test.branch
}

o := determineBranch(test.args, true)
o := determineBranch(test.args)

if o != test.branch {
t.Errorf("branch should be %s, but was %s", test.branch, o)
if o == test.branch {
continue
}
}
}

func Test_isValidBranch(t *testing.T) {
tests := []struct {
branch string
valid bool
}{
{branch: "hello-world", valid: true},
{branch: "hello_world", valid: true},
{branch: "hello world", valid: false},
{branch: "hello_world!", valid: false},
{branch: "hello_world?", valid: false},
{branch: "#HelloWorld", valid: false},
{branch: "hello-world*", valid: false},
}

for _, test := range tests {
o := isValidBranch(test.branch)

if o != test.valid {
t.Errorf("branch %s should be %v, but wasn't", test.branch, test.valid)
if len(test.args) > 0 && o == test.args[0] {
continue
}

t.Errorf("branch should be %s, but was %s", test.branch, o)
}
}

func Test_getValidBranch(t *testing.T) {
func Test_askForBranch(t *testing.T) {
tests := []struct {
branch string
valid bool
}{
{branch: "hello-world", valid: true},
{branch: "hello-world"},
}

originalAskOpenEndedQuestion := commandline.AskOpenEndedQuestion
Expand All @@ -93,7 +69,7 @@ func Test_getValidBranch(t *testing.T) {
return test.branch
}

o := getValidBranch()
o := askForBranch()

if o != test.branch {
t.Errorf("branch should be %s, but was %s", "hello-world", o)
Expand Down
7 changes: 4 additions & 3 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ func (g *Git) CleanDeletedBranches() {
}
}

func (g *Git) CreateBranch(branch string) {
func (g *Git) CreateBranch(branch string) error {
_, err := g.Executor.Exec("waitAndStdout", "git", "branch", "--no-track", branch)
if err != nil {
utils.HandleError(err, g.Debug, nil)
return
return err
}

return nil
}

func (g *Git) CreateEmptyCommit() {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

var (
packageOwner = "emmahsax"
packageVersion = "0.0.5"
packageVersion = "0.0.6"
packageRepository = "go-git-helper"
)

Expand Down

0 comments on commit 571b195

Please sign in to comment.