Skip to content

Commit 571b195

Browse files
authored
Allow git to say what branches are allowed (#40)
1 parent 5929579 commit 571b195

File tree

4 files changed

+30
-66
lines changed

4 files changed

+30
-66
lines changed

cmd/newBranch/newBranch.go

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package newBranch
22

33
import (
44
"fmt"
5-
"regexp"
65

76
"github.com/emmahsax/go-git-helper/internal/commandline"
87
"github.com/emmahsax/go-git-helper/internal/executor"
@@ -27,7 +26,7 @@ func NewCommand() *cobra.Command {
2726
Args: cobra.MaximumNArgs(1),
2827
DisableFlagsInUseLine: true,
2928
RunE: func(cmd *cobra.Command, args []string) error {
30-
newNewBranch(determineBranch(args, debug), debug, executor.NewExecutor(debug)).execute()
29+
newNewBranch(determineBranch(args), debug, executor.NewExecutor(debug)).execute()
3130
return nil
3231
},
3332
}
@@ -45,45 +44,33 @@ func newNewBranch(branch string, debug bool, executor executor.ExecutorInterface
4544
}
4645
}
4746

48-
func determineBranch(args []string, debug bool) string {
47+
func determineBranch(args []string) string {
4948
if len(args) == 0 {
50-
return getValidBranch()
49+
return askForBranch()
5150
} else {
52-
if !isValidBranch(args[0]) {
53-
fmt.Println("--- Invalid branch provided ---")
54-
return getValidBranch()
55-
} else {
56-
return args[0]
57-
}
51+
return args[0]
5852
}
5953
}
6054

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

66-
func getValidBranch() string {
67-
var branch string
59+
func (nb *NewBranch) execute() {
60+
fmt.Println("Attempting to create a new branch:", nb.Branch)
61+
g := git.NewGit(nb.Debug, nb.Executor)
62+
g.Pull()
6863

6964
for {
70-
branch = commandline.AskOpenEndedQuestion("New branch name", false)
71-
72-
if isValidBranch(branch) {
65+
err := g.CreateBranch(nb.Branch)
66+
if err == nil {
7367
break
7468
}
7569

7670
fmt.Println("--- Invalid branch ---")
71+
nb.Branch = askForBranch()
7772
}
7873

79-
return branch
80-
}
81-
82-
func (nb *NewBranch) execute() {
83-
fmt.Println("Attempting to create a new branch:", nb.Branch)
84-
g := git.NewGit(nb.Debug, nb.Executor)
85-
g.Pull()
86-
g.CreateBranch(nb.Branch)
8774
g.Checkout(nb.Branch)
8875
g.PushBranch(nb.Branch)
8976
}

cmd/newBranch/newBranch_test.go

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,8 @@ func Test_determineBranch(t *testing.T) {
2424
args []string
2525
branch string
2626
}{
27-
{args: []string{}, branch: "hello-world"},
28-
{args: []string{"hello-world"}, branch: "hello-world"},
29-
{args: []string{"hello_world"}, branch: "hello_world"},
30-
{args: []string{"hello world"}, branch: "hello-world"},
31-
{args: []string{"hello_world!"}, branch: "hello-world"},
32-
{args: []string{"hello_world?"}, branch: "hello-world"},
33-
{args: []string{"#HelloWorld"}, branch: "hello-world"},
34-
{args: []string{"hello-world*"}, branch: "hello-world"},
27+
{args: []string{}, branch: "hello-something-or-other"},
28+
{args: []string{"hello-world"}, branch: ""},
3529
}
3630

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

47-
o := determineBranch(test.args, true)
41+
o := determineBranch(test.args)
4842

49-
if o != test.branch {
50-
t.Errorf("branch should be %s, but was %s", test.branch, o)
43+
if o == test.branch {
44+
continue
5145
}
52-
}
53-
}
5446

55-
func Test_isValidBranch(t *testing.T) {
56-
tests := []struct {
57-
branch string
58-
valid bool
59-
}{
60-
{branch: "hello-world", valid: true},
61-
{branch: "hello_world", valid: true},
62-
{branch: "hello world", valid: false},
63-
{branch: "hello_world!", valid: false},
64-
{branch: "hello_world?", valid: false},
65-
{branch: "#HelloWorld", valid: false},
66-
{branch: "hello-world*", valid: false},
67-
}
68-
69-
for _, test := range tests {
70-
o := isValidBranch(test.branch)
71-
72-
if o != test.valid {
73-
t.Errorf("branch %s should be %v, but wasn't", test.branch, test.valid)
47+
if len(test.args) > 0 && o == test.args[0] {
48+
continue
7449
}
50+
51+
t.Errorf("branch should be %s, but was %s", test.branch, o)
7552
}
7653
}
7754

78-
func Test_getValidBranch(t *testing.T) {
55+
func Test_askForBranch(t *testing.T) {
7956
tests := []struct {
8057
branch string
81-
valid bool
8258
}{
83-
{branch: "hello-world", valid: true},
59+
{branch: "hello-world"},
8460
}
8561

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

96-
o := getValidBranch()
72+
o := askForBranch()
9773

9874
if o != test.branch {
9975
t.Errorf("branch should be %s, but was %s", "hello-world", o)

internal/git/git.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,13 @@ func (g *Git) CleanDeletedBranches() {
5656
}
5757
}
5858

59-
func (g *Git) CreateBranch(branch string) {
59+
func (g *Git) CreateBranch(branch string) error {
6060
_, err := g.Executor.Exec("waitAndStdout", "git", "branch", "--no-track", branch)
6161
if err != nil {
62-
utils.HandleError(err, g.Debug, nil)
63-
return
62+
return err
6463
}
64+
65+
return nil
6566
}
6667

6768
func (g *Git) CreateEmptyCommit() {

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121

2222
var (
2323
packageOwner = "emmahsax"
24-
packageVersion = "0.0.5"
24+
packageVersion = "0.0.6"
2525
packageRepository = "go-git-helper"
2626
)
2727

0 commit comments

Comments
 (0)