mirror of
				https://github.com/xiaoqidun/gitcz.git
				synced 2025-11-04 09:19:38 +08:00 
			
		
		
		
	Compare commits
	
		
			19 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 945c08d9c3 | |||
| 1364c50b90 | |||
| 9743118378 | |||
| d898a07ea9 | |||
| 5a879147b9 | |||
| 8aad2018ff | |||
| 85015449ca | |||
| 
						 | 
					36dcaa56a5 | ||
| 8e203b7689 | |||
| 8168b719fd | |||
| d36f61bc80 | |||
| b45c7a8e32 | |||
| 
						 | 
					55c015a566 | ||
| 00b8e2c4b6 | |||
| d4c9df855d | |||
| 10bffed6e5 | |||
| 23618d81ad | |||
| bf37f8a281 | |||
| ce68380357 | 
							
								
								
									
										24
									
								
								.drone.yml
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								.drone.yml
									
									
									
									
									
								
							@@ -1,10 +1,28 @@
 | 
			
		||||
kind: pipeline
 | 
			
		||||
type: docker
 | 
			
		||||
name: default
 | 
			
		||||
 | 
			
		||||
node:
 | 
			
		||||
  region: cn
 | 
			
		||||
steps:
 | 
			
		||||
  - name: generate
 | 
			
		||||
    pull: if-not-exists
 | 
			
		||||
    image: ccr.ccs.tencentyun.com/xiaoqidun/build
 | 
			
		||||
    commands:
 | 
			
		||||
      - build -f build.sh -i gitcz.go -o release/gitcz
 | 
			
		||||
  - name: build
 | 
			
		||||
    pull: if-not-exists
 | 
			
		||||
    image: golang:alpine
 | 
			
		||||
    image: ccr.ccs.tencentyun.com/xiaoqidun/goenv
 | 
			
		||||
    commands:
 | 
			
		||||
      - go build gitcz.go
 | 
			
		||||
      - ./build.sh
 | 
			
		||||
  - name: upload
 | 
			
		||||
    image: ccr.ccs.tencentyun.com/xiaoqidun/gocos
 | 
			
		||||
    settings:
 | 
			
		||||
      secret_id:
 | 
			
		||||
        from_secret: cos_secret_id
 | 
			
		||||
      secret_key:
 | 
			
		||||
        from_secret: cos_secret_key
 | 
			
		||||
      bucket_url:
 | 
			
		||||
        from_secret: cos_bucket_url
 | 
			
		||||
      source: release/
 | 
			
		||||
      target: product/gitcz
 | 
			
		||||
      strip_prefix: release/
 | 
			
		||||
							
								
								
									
										16
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								README.md
									
									
									
									
									
								
							@@ -1,26 +1,38 @@
 | 
			
		||||
# gitcz
 | 
			
		||||
 | 
			
		||||
Golang 版本 Git Commitizen,commit 规范工具
 | 
			
		||||
 | 
			
		||||
# 快速安装
 | 
			
		||||
go get -u github.com/xiaoqidun/gitcz
 | 
			
		||||
 | 
			
		||||
go install github.com/xiaoqidun/gitcz@latest
 | 
			
		||||
 | 
			
		||||
# 编译安装
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
git clone https://github.com/xiaoqidun/gitcz.git
 | 
			
		||||
cd gitcz
 | 
			
		||||
go build gitcz.go
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
# 手动安装
 | 
			
		||||
1. 根据系统架构下载为你编译好的[二进制文件](https://github.com/xiaoqidun/gitcz/releases)
 | 
			
		||||
 | 
			
		||||
1. 根据系统架构下载为你编译好的[二进制文件](https://aite.xyz/product/gitcz/)
 | 
			
		||||
2. 将下载好的二进制文件重命名为 gitcz 并保留后缀
 | 
			
		||||
3. 把 gitcz 文件移动到系统 PATH 环境变量中的目录下
 | 
			
		||||
4. windows 外的系统需使用 chmod 命令赋予可执行权限
 | 
			
		||||
 | 
			
		||||
# 使用说明
 | 
			
		||||
 | 
			
		||||
```shell script
 | 
			
		||||
# 添加文件到本地仓库
 | 
			
		||||
git add .
 | 
			
		||||
# 使用-amend参数可覆盖最近一次提交信息,等同于git commit --amend
 | 
			
		||||
# 使用-S参数可进行签署commit操作,等同于git commit -S
 | 
			
		||||
gitcz
 | 
			
		||||
# 推送文件到远程仓库
 | 
			
		||||
git push
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
# 规范文档
 | 
			
		||||
 | 
			
		||||
gitcz 使用:[angular git commit 规范](https://github.com/angular/angular.js/blob/master/DEVELOPERS.md#-git-commit-guidelines)
 | 
			
		||||
							
								
								
									
										76
									
								
								gitcz.go
									
									
									
									
									
								
							
							
						
						
									
										76
									
								
								gitcz.go
									
									
									
									
									
								
							@@ -5,8 +5,6 @@ import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"flag"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"log"
 | 
			
		||||
	"os"
 | 
			
		||||
	"os/exec"
 | 
			
		||||
	"strconv"
 | 
			
		||||
@@ -23,7 +21,8 @@ type CzCommit struct {
 | 
			
		||||
	Scope          *string
 | 
			
		||||
	Subject        *string
 | 
			
		||||
	Body           *string
 | 
			
		||||
	Footer  *string
 | 
			
		||||
	BreakingChange *string
 | 
			
		||||
	Closes         *string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var StdinInput = bufio.NewReader(os.Stdin)
 | 
			
		||||
@@ -32,6 +31,9 @@ var (
 | 
			
		||||
	InputTypePrompt           = "选择或输入一个提交类型(必填): "
 | 
			
		||||
	InputScopePrompt          = "说明本次提交的影响范围(必填): "
 | 
			
		||||
	InputSubjectPrompt        = "对本次提交进行简短描述(必填): "
 | 
			
		||||
	InputBodyPrompt           = "对本次提交进行完整描述(选填): "
 | 
			
		||||
	InputBreakingChangePrompt = "如果当前代码版本与上一版本不兼容,对变动、变动的理由及迁移的方法进行描述(选填): "
 | 
			
		||||
	InputClosesPrompt         = "如果本次提交针对某个issue,列出关闭的issues(选填): "
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var CzTypeList = []CzType{
 | 
			
		||||
@@ -75,6 +77,7 @@ func main() {
 | 
			
		||||
		false,
 | 
			
		||||
		"覆盖上次提交信息",
 | 
			
		||||
	)
 | 
			
		||||
	sign := flag.Bool("S", false, "对commit进行签名")
 | 
			
		||||
	author := flag.Bool(
 | 
			
		||||
		"author",
 | 
			
		||||
		false,
 | 
			
		||||
@@ -89,9 +92,12 @@ func main() {
 | 
			
		||||
	czCommit.Type = InputType()
 | 
			
		||||
	czCommit.Scope = InputScope()
 | 
			
		||||
	czCommit.Subject = InputSubject()
 | 
			
		||||
	czCommit.Body = InputBody()
 | 
			
		||||
	czCommit.BreakingChange = InputBreakingChange()
 | 
			
		||||
	czCommit.Closes = InputCloses()
 | 
			
		||||
	commit := GenerateCommit(czCommit)
 | 
			
		||||
	if err := GitCommit(commit, *amend); err != nil {
 | 
			
		||||
		log.Println(err)
 | 
			
		||||
	if err := GitCommit(commit, *amend, *sign); err != nil {
 | 
			
		||||
		fmt.Println(err)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -108,8 +114,8 @@ func NewLine() {
 | 
			
		||||
	fmt.Println()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func GitCommit(commit string, amend bool) (err error) {
 | 
			
		||||
	tempFile, err := ioutil.TempFile("", "git_commit_")
 | 
			
		||||
func GitCommit(commit string, amend bool, sign bool) (err error) {
 | 
			
		||||
	tempFile, err := os.CreateTemp("", "git_commit_")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
@@ -124,6 +130,9 @@ func GitCommit(commit string, amend bool) (err error) {
 | 
			
		||||
	if amend {
 | 
			
		||||
		args = append(args, "--amend")
 | 
			
		||||
	}
 | 
			
		||||
	if sign {
 | 
			
		||||
		args = append(args, "-S")
 | 
			
		||||
	}
 | 
			
		||||
	args = append(args, "-F", tempFile.Name())
 | 
			
		||||
	cmd := exec.Command("git", args...)
 | 
			
		||||
	result, err := cmd.CombinedOutput()
 | 
			
		||||
@@ -154,6 +163,7 @@ func InputType() *CzType {
 | 
			
		||||
			return &CzTypeList[i]
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	NewLine()
 | 
			
		||||
	return InputType()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -165,6 +175,7 @@ func InputScope() *string {
 | 
			
		||||
		NewLine()
 | 
			
		||||
		return &text
 | 
			
		||||
	}
 | 
			
		||||
	NewLine()
 | 
			
		||||
	return InputScope()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -176,22 +187,63 @@ func InputSubject() *string {
 | 
			
		||||
		NewLine()
 | 
			
		||||
		return &text
 | 
			
		||||
	}
 | 
			
		||||
	return InputScope()
 | 
			
		||||
	NewLine()
 | 
			
		||||
	return InputSubject()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func InputBody() *string {
 | 
			
		||||
	fmt.Print(InputBodyPrompt)
 | 
			
		||||
	text, _ := StdinInput.ReadString('\n')
 | 
			
		||||
	text = strings.TrimSpace(text)
 | 
			
		||||
	if text != "" {
 | 
			
		||||
		NewLine()
 | 
			
		||||
		return &text
 | 
			
		||||
	}
 | 
			
		||||
	NewLine()
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func InputBreakingChange() *string {
 | 
			
		||||
	fmt.Print(InputBreakingChangePrompt)
 | 
			
		||||
	text, _ := StdinInput.ReadString('\n')
 | 
			
		||||
	text = strings.TrimSpace(text)
 | 
			
		||||
	if text != "" {
 | 
			
		||||
		NewLine()
 | 
			
		||||
		return &text
 | 
			
		||||
	}
 | 
			
		||||
	NewLine()
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func InputCloses() *string {
 | 
			
		||||
	fmt.Print(InputClosesPrompt)
 | 
			
		||||
	text, _ := StdinInput.ReadString('\n')
 | 
			
		||||
	text = strings.TrimSpace(text)
 | 
			
		||||
	if text != "" {
 | 
			
		||||
		NewLine()
 | 
			
		||||
		return &text
 | 
			
		||||
	}
 | 
			
		||||
	NewLine()
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func GenerateCommit(czCommit *CzCommit) string {
 | 
			
		||||
	commit := fmt.Sprintf(
 | 
			
		||||
		"%s(%s): %s\n",
 | 
			
		||||
		"%s(%s): %s\n\n",
 | 
			
		||||
		czCommit.Type.Type,
 | 
			
		||||
		*czCommit.Scope,
 | 
			
		||||
		*czCommit.Subject,
 | 
			
		||||
	)
 | 
			
		||||
	if czCommit.Body != nil {
 | 
			
		||||
		commit += *czCommit.Body
 | 
			
		||||
		commit += "\n\n"
 | 
			
		||||
	}
 | 
			
		||||
	commit += "\n"
 | 
			
		||||
	if czCommit.Footer != nil {
 | 
			
		||||
		commit += *czCommit.Footer
 | 
			
		||||
	if czCommit.BreakingChange != nil {
 | 
			
		||||
		commit += "BREAKING CHANGE: " + *czCommit.BreakingChange
 | 
			
		||||
		commit += "\n\n"
 | 
			
		||||
	}
 | 
			
		||||
	if czCommit.Closes != nil {
 | 
			
		||||
		commit += "Closes fix " + *czCommit.Closes
 | 
			
		||||
	}
 | 
			
		||||
	return commit
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user