From f04d2813ba06783ce676d1b2b0ac0ad7169ff60e Mon Sep 17 00:00:00 2001 From: xiaoqidun Date: Fri, 31 Jul 2020 16:39:33 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E5=8F=91=E5=B8=83=E5=8F=98=E6=9B=B4):=20?= =?UTF-8?q?=E4=BD=BF=E7=94=A8github=E8=87=AA=E5=B8=A6=E7=9A=84=E5=8F=91?= =?UTF-8?q?=E8=A1=8C=E5=8A=9F=E8=83=BD=E6=94=BE=E4=BA=8C=E8=BF=9B=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + LICENSE | 21 ++++++ README.md | 26 +++++++ gitcz.go | 197 +++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 3 + 5 files changed, 248 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 gitcz.go create mode 100644 go.mod diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f11b75 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..25da588 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 xiaoqidun + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..a23b1ae --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +# gitcz +Golang版本Git Commitizen,commit规范工具 +# 快速安装 +go get -u github.com/xiaoqidun/gitcz +# 编译安装 +``` +git clone https://github.com/xiaoqidun/gitcz.git +cd gitcz +go build gitcz.go +``` +# 手动安装 +1. 根据系统架构下载为你编译好的[二进制文件](https://github.com/xiaoqidun/gitcz/releases) +2. 将下载好的二进制文件重命名为gitcz并保留后缀 +3. 把gitcz文件移动到系统PATH环境变量中的目录下 +4. windows外的系统需使用chmod命令赋予可执行权限 +# 使用说明 +```shell script +# 添加文件到本地仓库 +git add . +# 使用-amend参数可覆盖最近一次提交信息,等同于git commit --amend +gitcz +# 推送文件到远程仓库 +git push +``` +# 规范文档 +gitcz使用:[angular git commit规范](https://github.com/angular/angular.js/blob/master/DEVELOPERS.md#-git-commit-guidelines) \ No newline at end of file diff --git a/gitcz.go b/gitcz.go new file mode 100644 index 0000000..0977cd8 --- /dev/null +++ b/gitcz.go @@ -0,0 +1,197 @@ +package main + +import ( + "bufio" + "bytes" + "flag" + "fmt" + "io/ioutil" + "log" + "os" + "os/exec" + "strconv" + "strings" +) + +type CzType struct { + Type string + Message string +} + +type CzCommit struct { + Type *CzType + Scope *string + Subject *string + Body *string + Footer *string +} + +var StdinInput = bufio.NewReader(os.Stdin) + +var ( + InputTypePrompt = "选择或输入一个提交类型(必填): " + InputScopePrompt = "说明本次提交的影响范围(必填): " + InputSubjectPrompt = "对本次提交进行简短描述(必填): " +) + +var CzTypeList = []CzType{ + { + Type: "feat", + Message: "新的功能", + }, + { + Type: "fix", + Message: "修补错误", + }, + { + Type: "docs", + Message: "文档修改", + }, + { + Type: "style", + Message: "格式变化", + }, + { + Type: "refactor", + Message: "重构代码", + }, + { + Type: "perf", + Message: "性能提高", + }, + { + Type: "test", + Message: "测试用例", + }, + { + Type: "chore", + Message: "构建变动", + }, +} + +func main() { + amend := flag.Bool( + "amend", + false, + "覆盖上次提交信息", + ) + author := flag.Bool( + "author", + false, + "关于本软件开发者", + ) + flag.Parse() + if *author { + Author() + return + } + czCommit := &CzCommit{} + czCommit.Type = InputType() + czCommit.Scope = InputScope() + czCommit.Subject = InputSubject() + commit := GenerateCommit(czCommit) + if err := GitCommit(commit, *amend); err != nil { + log.Println(err) + } +} + +func Author() { + fmt.Println("welcome to our website https://aite.xyz/") + fmt.Println("----------------------------------------") + fmt.Println("腾讯扣扣:88966001") + fmt.Println("电子邮箱:xiaoqidun@gmail.com") + fmt.Println("----------------------------------------") + fmt.Println("Copyright (c) 2020 xiaoqidun@gmail.com") +} + +func NewLine() { + fmt.Println() +} + +func GitCommit(commit string, amend bool) (err error) { + tempFile, err := ioutil.TempFile("", "git_commit_") + if err != nil { + return + } + defer func() { + _ = tempFile.Close() + _ = os.Remove(tempFile.Name()) + }() + if _, err = tempFile.WriteString(commit); err != nil { + return + } + args := []string{"commit"} + if amend { + args = append(args, "--amend") + } + args = append(args, "-F", tempFile.Name()) + cmd := exec.Command("git", args...) + result, err := cmd.CombinedOutput() + if err != nil && !strings.ContainsAny(err.Error(), "exit status") { + return + } else { + fmt.Println(string(bytes.TrimSpace(result))) + } + return nil +} + +func InputType() *CzType { + typeNum := len(CzTypeList) + for i := 0; i < typeNum; i++ { + fmt.Printf("[%d] %s:\t%s\n", i+1, CzTypeList[i].Type, CzTypeList[i].Message) + } + fmt.Print(InputTypePrompt) + text, _ := StdinInput.ReadString('\n') + text = strings.TrimSpace(text) + selectId, err := strconv.Atoi(text) + if err == nil && (selectId > 0 && selectId <= typeNum) { + NewLine() + return &CzTypeList[selectId-1] + } + for i := 0; i < typeNum; i++ { + if text == CzTypeList[i].Type { + NewLine() + return &CzTypeList[i] + } + } + return InputType() +} + +func InputScope() *string { + fmt.Print(InputScopePrompt) + text, _ := StdinInput.ReadString('\n') + text = strings.TrimSpace(text) + if text != "" { + NewLine() + return &text + } + return InputScope() +} + +func InputSubject() *string { + fmt.Print(InputSubjectPrompt) + text, _ := StdinInput.ReadString('\n') + text = strings.TrimSpace(text) + if text != "" { + NewLine() + return &text + } + return InputScope() +} + +func GenerateCommit(czCommit *CzCommit) string { + commit := fmt.Sprintf( + "%s(%s): %s\n", + czCommit.Type.Type, + *czCommit.Scope, + *czCommit.Subject, + ) + if czCommit.Body != nil { + commit += *czCommit.Body + } + commit += "\n" + if czCommit.Footer != nil { + commit += *czCommit.Footer + } + return commit +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..0656e0c --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module gitcz + +go 1.13