Skip to content

Commit 0852833

Browse files
committed
fix: gitop checkout function
1 parent b2353da commit 0852833

File tree

3 files changed

+48
-27
lines changed

3 files changed

+48
-27
lines changed

cmd/gitlab-flow/commands_hotfix.go

+19-11
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ func getHotfixSubCommands() cli.Commands {
1818
// gitlab-flow hotfix start @title @desc
1919
func getHotfixStartSubCommand() *cli.Command {
2020
return &cli.Command{
21-
Name: "start",
22-
Usage: "open a hotfix branch and merge request to master",
23-
ArgsUsage: "@title @desc",
24-
Description: "@title title \n\t @desc description",
25-
Category: "hotfix",
21+
Name: "open",
22+
Usage: "open @title @description",
23+
ArgsUsage: "@title] [@description",
24+
Description: "open a hotfix branch and merge request to master. " +
25+
"\n@title title \n@desc description",
26+
Category: "hotfix",
2627
Action: func(c *cli.Context) error {
2728
log.
2829
WithFields(log.Fields{"args": c.Args().Slice()}).
@@ -33,11 +34,9 @@ func getHotfixStartSubCommand() *cli.Command {
3334
if title == "" {
3435
return errors.New("title could not be empty")
3536
}
36-
3737
if desc == "" {
3838
return errors.New("desc could not be empty")
3939
}
40-
4140
confPath := c.String("conf_path")
4241
debug := c.Bool("debug")
4342
return getFlow(confPath, debug).HotfixBegin(title, desc)
@@ -49,11 +48,20 @@ func getHotfixStartSubCommand() *cli.Command {
4948
// gitlab-flow hotfix release @title @desc
5049
func getHotfixFinishSubCommand() *cli.Command {
5150
return &cli.Command{
52-
Name: "release",
53-
Usage: "finish a hotfix",
54-
ArgsUsage: "@branchName",
55-
Description: "@title title",
51+
Name: "close",
52+
Usage: "close [-hb, --hotfix_branch_name `hotfixBranchName`]",
53+
ArgsUsage: "[-hb, --hotfix_branch_name `hotfixBranchName`]",
54+
Description: "close a hotfix development, then create a merge request into master",
5655
Category: "hotfix",
56+
Flags: []cli.Flag{
57+
&cli.StringFlag{
58+
Name: "hotfix_branch_name",
59+
Aliases: []string{"hb"},
60+
Value: "", // default current branch
61+
Usage: "-hb, --hotfix_branch_name", // be be overwritten
62+
Required: false,
63+
},
64+
},
5765
Action: func(c *cli.Context) error {
5866
log.
5967
WithFields(log.Fields{"args": c.Args().Slice()}).

internal/git-operator/git_operator_cmd.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func NewBasedCmd(dir string) IGitOperator {
2929
Cmd: "git",
3030
Dir: dir,
3131
fetchCmd: "fetch {arg}",
32-
checkoutCmd: "checkout {createFlag} {branch}",
32+
checkoutCmd: "checkout {createFlag}{branch}",
3333
currentBranchCmd: "rev-parse --abbrev-ref HEAD",
3434
}
3535
}
@@ -43,10 +43,6 @@ func (c operatorBasedCmd) run(dir string, cmd string, keyval ...string) error {
4343
func (c operatorBasedCmd) run1(dir string, cmdline string, keyval []string, verbose bool) ([]byte, error) {
4444
m := make(map[string]string)
4545
for i := 0; i < len(keyval); i += 2 {
46-
// FIXED: ignore empty val
47-
if keyval[i] == "" {
48-
continue
49-
}
5046
m[keyval[i]] = keyval[i+1]
5147
}
5248
args := strings.Fields(cmdline)

internal/git-operator/git_operator_test.go

+28-11
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,38 @@ import (
44
"os"
55
"path/filepath"
66
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
var (
12+
home, _ = os.UserHomeDir()
13+
repoPath = filepath.Join(home, "Projects", "opensource", "test")
714
)
815

9-
func Test_gitOp(t *testing.T) {
10-
home, _ := os.UserHomeDir()
11-
repoPath := filepath.Join(home, "Projects", "medlinker", "micro-server-template")
16+
func Test_gitOp_FetchOrigin(t *testing.T) {
17+
op := NewBasedCmd(repoPath)
18+
err := op.FetchOrigin()
19+
assert.Nil(t, err)
20+
}
21+
22+
func Test_gitOp_Checkout(t *testing.T) {
23+
op := NewBasedCmd(repoPath)
24+
err := op.Checkout("hotfix/hotfix-1", false)
25+
assert.Nil(t, err)
26+
}
1227

28+
func Test_gitOp_Checkout_create(t *testing.T) {
1329
op := NewBasedCmd(repoPath)
30+
err := op.Checkout("checkout-ddd", true)
31+
assert.Nil(t, err)
32+
}
1433

15-
if err := op.FetchOrigin(); err != nil {
16-
t.Error(err)
17-
t.FailNow()
18-
}
34+
func Test_gitOp_CurrentBranch(t *testing.T) {
35+
op := NewBasedCmd(repoPath)
1936

20-
if err := op.Checkout("test", false); err != nil {
21-
t.Error(err)
22-
t.FailNow()
23-
}
37+
b := "hotfix/hotfix-1"
38+
cb, err := op.CurrentBranch()
39+
assert.Nil(t, err)
40+
assert.Equal(t, b, cb)
2441
}

0 commit comments

Comments
 (0)