Skip to content

Commit 84684de

Browse files
author
mritd
committed
refactor(git): move const
move const Signed-off-by: mritd <[email protected]>
1 parent 1657533 commit 84684de

File tree

6 files changed

+76
-83
lines changed

6 files changed

+76
-83
lines changed

cmd/branch.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"github.com/mritd/gitflow-toolkit/consts"
54
"github.com/mritd/gitflow-toolkit/git"
65
"github.com/spf13/cobra"
76
)
@@ -18,7 +17,7 @@ Create a branch with a prefix of feat.`,
1817
if len(args) != 1 {
1918
_ = cmd.Help()
2019
} else {
21-
git.Checkout(consts.FEAT, args[0])
20+
git.Checkout(git.FEAT, args[0])
2221
}
2322
},
2423
}
@@ -36,7 +35,7 @@ Create a branch with a prefix of fix.`,
3635
if len(args) != 1 {
3736
_ = cmd.Help()
3837
} else {
39-
git.Checkout(consts.FIX, args[0])
38+
git.Checkout(git.FIX, args[0])
4039
}
4140
},
4241
}
@@ -54,7 +53,7 @@ Create a branch with a prefix of hotfix.`,
5453
if len(args) != 1 {
5554
_ = cmd.Help()
5655
} else {
57-
git.Checkout(consts.HOTFIX, args[0])
56+
git.Checkout(git.HOTFIX, args[0])
5857
}
5958
},
6059
}
@@ -72,7 +71,7 @@ Create a branch with a prefix of perf.`,
7271
if len(args) != 1 {
7372
_ = cmd.Help()
7473
} else {
75-
git.Checkout(consts.PERF, args[0])
74+
git.Checkout(git.PERF, args[0])
7675
}
7776
},
7877
}
@@ -90,7 +89,7 @@ Create a branch with a prefix of refactor.`,
9089
if len(args) != 1 {
9190
_ = cmd.Help()
9291
} else {
93-
git.Checkout(consts.REFACTOR, args[0])
92+
git.Checkout(git.REFACTOR, args[0])
9493
}
9594
},
9695
}
@@ -108,7 +107,7 @@ Create a branch with a prefix of test.`,
108107
if len(args) != 1 {
109108
_ = cmd.Help()
110109
} else {
111-
git.Checkout(consts.TEST, args[0])
110+
git.Checkout(git.TEST, args[0])
112111
}
113112
},
114113
}
@@ -126,7 +125,7 @@ Create a branch with a prefix of chore.`,
126125
if len(args) != 1 {
127126
_ = cmd.Help()
128127
} else {
129-
git.Checkout(consts.CHORE, args[0])
128+
git.Checkout(git.CHORE, args[0])
130129
}
131130

132131
},
@@ -145,7 +144,7 @@ Create a branch with a prefix of style.`,
145144
if len(args) != 1 {
146145
_ = cmd.Help()
147146
} else {
148-
git.Checkout(consts.STYLE, args[0])
147+
git.Checkout(git.STYLE, args[0])
149148
}
150149
},
151150
}
@@ -163,7 +162,7 @@ Create a branch with a prefix of docs.`,
163162
if len(args) != 1 {
164163
_ = cmd.Help()
165164
} else {
166-
git.Checkout(consts.DOCS, args[0])
165+
git.Checkout(git.DOCS, args[0])
167166
}
168167
},
169168
}

cmd/ci.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/mritd/gitflow-toolkit/git"
88

9-
"github.com/mritd/gitflow-toolkit/consts"
109
"github.com/spf13/cobra"
1110
)
1211

@@ -39,7 +38,7 @@ func NewCi() *cobra.Command {
3938
cm := &git.Message{Sob: git.GenSOB()}
4039

4140
if fastCommit {
42-
cm.Type = consts.FEAT
41+
cm.Type = git.FEAT
4342
cm.Scope = "Undefined"
4443
cm.Subject = git.InputSubject()
4544
git.Commit(cm)

consts/consts.go

Lines changed: 0 additions & 31 deletions
This file was deleted.

git/ci.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,19 @@ import (
77
"strings"
88
"text/template"
99

10-
"github.com/mritd/gitflow-toolkit/consts"
1110
"github.com/mritd/gitflow-toolkit/utils"
1211
"github.com/mritd/promptx"
1312
"github.com/pkg/errors"
1413
)
1514

1615
type TypeMessage struct {
17-
Type consts.CommitType
16+
Type CommitType
1817
ZHDescription string
1918
ENDescription string
2019
}
2120

2221
type Message struct {
23-
Type consts.CommitType
22+
Type CommitType
2423
Scope string
2524
Subject string
2625
Body string
@@ -29,18 +28,18 @@ type Message struct {
2928
}
3029

3130
// 选择提交类型
32-
func SelectCommitType() consts.CommitType {
31+
func SelectCommitType() CommitType {
3332

3433
commitTypes := []TypeMessage{
35-
{Type: consts.FEAT, ZHDescription: "新功能", ENDescription: "Introducing new features"},
36-
{Type: consts.FIX, ZHDescription: "修复 Bug", ENDescription: "Bug fix"},
37-
{Type: consts.DOCS, ZHDescription: "添加文档", ENDescription: "Writing docs"},
38-
{Type: consts.STYLE, ZHDescription: "调整格式", ENDescription: "Improving structure/format of the code"},
39-
{Type: consts.REFACTOR, ZHDescription: "重构代码", ENDescription: "Refactoring code"},
40-
{Type: consts.TEST, ZHDescription: "增加测试", ENDescription: "When adding missing tests"},
41-
{Type: consts.CHORE, ZHDescription: "CI/CD 变动", ENDescription: "Changing CI/CD"},
42-
{Type: consts.PERF, ZHDescription: "性能优化", ENDescription: "Improving performance"},
43-
{Type: consts.EXIT, ZHDescription: "退出", ENDescription: "Exit commit"},
34+
{Type: FEAT, ZHDescription: "新功能", ENDescription: "Introducing new features"},
35+
{Type: FIX, ZHDescription: "修复 Bug", ENDescription: "Bug fix"},
36+
{Type: DOCS, ZHDescription: "添加文档", ENDescription: "Writing docs"},
37+
{Type: STYLE, ZHDescription: "调整格式", ENDescription: "Improving structure/format of the code"},
38+
{Type: REFACTOR, ZHDescription: "重构代码", ENDescription: "Refactoring code"},
39+
{Type: TEST, ZHDescription: "增加测试", ENDescription: "When adding missing tests"},
40+
{Type: CHORE, ZHDescription: "CI/CD 变动", ENDescription: "Changing CI/CD"},
41+
{Type: PERF, ZHDescription: "性能优化", ENDescription: "Improving performance"},
42+
{Type: EXIT, ZHDescription: "退出", ENDescription: "Exit commit"},
4443
}
4544
cfg := &promptx.SelectConfig{
4645
ActiveTpl: "» {{ .Type | cyan }} ({{ .ENDescription | cyan }})",
@@ -61,7 +60,7 @@ func SelectCommitType() consts.CommitType {
6160

6261
idx := s.Run()
6362

64-
if commitTypes[idx].Type == consts.EXIT {
63+
if commitTypes[idx].Type == EXIT {
6564
fmt.Println("Talk is cheap. Show me the code.")
6665
os.Exit(0)
6766
}
@@ -151,7 +150,7 @@ func Commit(cm *Message) {
151150
cm.Body = cm.Subject
152151
}
153152

154-
t, err := template.New("commitMessage").Parse(consts.CommitTpl)
153+
t, err := template.New("commitMessage").Parse(CommitTpl)
155154
utils.CheckAndExit(err)
156155

157156
f, err := ioutil.TempFile("", "git-commit")

git/cm.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ import (
77
"regexp"
88
"strings"
99

10-
"github.com/mritd/gitflow-toolkit/consts"
1110
"github.com/mritd/gitflow-toolkit/utils"
1211
)
1312

1413
func CheckCommitMessage(file string) {
1514

16-
reg := regexp.MustCompile(consts.CommitMessagePattern)
15+
reg := regexp.MustCompile(CommitMessagePattern)
1716

1817
b, err := ioutil.ReadFile(file)
1918
utils.CheckAndExit(err)
@@ -23,15 +22,15 @@ func CheckCommitMessage(file string) {
2322
checkFailed()
2423
} else {
2524
switch commitTypes[0][1] {
26-
case string(consts.FEAT):
27-
case string(consts.FIX):
28-
case string(consts.DOCS):
29-
case string(consts.STYLE):
30-
case string(consts.REFACTOR):
31-
case string(consts.TEST):
32-
case string(consts.CHORE):
33-
case string(consts.PERF):
34-
case string(consts.HOTFIX):
25+
case string(FEAT):
26+
case string(FIX):
27+
case string(DOCS):
28+
case string(STYLE):
29+
case string(REFACTOR):
30+
case string(TEST):
31+
case string(CHORE):
32+
case string(PERF):
33+
case string(HOTFIX):
3534
default:
3635
if !strings.HasPrefix(string(b), "Merge branch") {
3736
checkFailed()

git/git.go

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,75 @@ import (
66
"github.com/mritd/gitflow-toolkit/utils"
77

88
"fmt"
9+
)
10+
11+
type CommitType string
12+
13+
type RepoType string
914

10-
"github.com/mritd/gitflow-toolkit/consts"
15+
const Cmd = "git"
16+
17+
const (
18+
FEAT CommitType = "feat"
19+
FIX CommitType = "fix"
20+
DOCS CommitType = "docs"
21+
STYLE CommitType = "style"
22+
REFACTOR CommitType = "refactor"
23+
TEST CommitType = "test"
24+
CHORE CommitType = "chore"
25+
PERF CommitType = "perf"
26+
HOTFIX CommitType = "hotfix"
27+
EXIT CommitType = "exit"
1128
)
1229

30+
const CommitTpl = `{{ .Type }}({{ .Scope }}): {{ .Subject }}
31+
32+
{{ .Body }}
33+
34+
{{ .Footer }}
35+
36+
{{ .Sob }}
37+
`
38+
39+
const CommitMessagePattern = `^(?:fixup!\s*)?(\w*)(\(([\w\$\.\*/-].*)\))?\: (.*)|^Merge\ branch(.*)`
40+
1341
func CheckGitProject() {
14-
utils.MustExecNoOut(consts.GitCmd, "rev-parse", "--show-toplevel")
42+
utils.MustExecNoOut(Cmd, "rev-parse", "--show-toplevel")
1543
}
1644

1745
func CheckStagedFiles() bool {
18-
output := utils.MustExecRtOut(consts.GitCmd, "diff", "--cached", "--name-only")
46+
output := utils.MustExecRtOut(Cmd, "diff", "--cached", "--name-only")
1947
return strings.TrimSpace(output) != ""
2048
}
2149

2250
func GetLastCommitInfo() *[]string {
23-
title := utils.MustExecRtOut(consts.GitCmd, "log", "-1", "--pretty=format:%s")
24-
desc := utils.MustExecRtOut(consts.GitCmd, "log", "-1", "--pretty=format:%b")
51+
title := utils.MustExecRtOut(Cmd, "log", "-1", "--pretty=format:%s")
52+
desc := utils.MustExecRtOut(Cmd, "log", "-1", "--pretty=format:%b")
2553
return &[]string{title, desc}
2654
}
2755

2856
func GetCurrentBranch() string {
2957
// 1.8+ git symbolic-ref --short HEAD
30-
return strings.TrimSpace(utils.MustExecRtOut(consts.GitCmd, "rev-parse", "--abbrev-ref", "HEAD"))
58+
return strings.TrimSpace(utils.MustExecRtOut(Cmd, "rev-parse", "--abbrev-ref", "HEAD"))
3159
}
3260

3361
func Rebase(sourceBranch string, targetBranch string) {
3462
fmt.Println("checkout branch:", targetBranch)
35-
utils.MustExec(consts.GitCmd, "checkout", targetBranch)
63+
utils.MustExec(Cmd, "checkout", targetBranch)
3664
fmt.Println("pull origin branch:", targetBranch)
37-
utils.MustExec(consts.GitCmd, "pull", "origin", targetBranch)
65+
utils.MustExec(Cmd, "pull", "origin", targetBranch)
3866
fmt.Println("checkout branch:", sourceBranch)
39-
utils.MustExec(consts.GitCmd, "checkout", sourceBranch)
67+
utils.MustExec(Cmd, "checkout", sourceBranch)
4068
fmt.Println("exec git rebase")
41-
utils.MustExec(consts.GitCmd, "rebase", targetBranch)
69+
utils.MustExec(Cmd, "rebase", targetBranch)
4270
fmt.Println("push", sourceBranch, "to origin")
43-
utils.MustExec(consts.GitCmd, "push", "origin", sourceBranch)
71+
utils.MustExec(Cmd, "push", "origin", sourceBranch)
4472
}
4573

46-
func Checkout(prefix consts.CommitType, branch string) {
47-
utils.MustExec(consts.GitCmd, "checkout", "-b", string(prefix)+"/"+branch)
74+
func Checkout(prefix CommitType, branch string) {
75+
utils.MustExec(Cmd, "checkout", "-b", string(prefix)+"/"+branch)
4876
}
4977

5078
func Push() {
51-
utils.MustExec(consts.GitCmd, "push", "origin", strings.TrimSpace(GetCurrentBranch()))
79+
utils.MustExec(Cmd, "push", "origin", strings.TrimSpace(GetCurrentBranch()))
5280
}

0 commit comments

Comments
 (0)