command/git.go

237 lines
5.2 KiB
Go
Raw Normal View History

2022-06-14 20:23:30 +08:00
// Package command ...
//
// Description : command ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2022-06-14 20:02
package command
import (
"os"
"git.zhangdeman.cn/zhangdeman/command/define"
)
2022-06-14 20:23:30 +08:00
// Git ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:09 2022/6/14
func Git(workDir string, gitCmdPath string) *git {
if len(gitCmdPath) == 0 {
2023-04-09 22:18:18 +08:00
gitCmdPath, _ = Check("git")
if len(gitCmdPath) == 0 {
gitCmdPath = "git"
}
2022-06-14 20:23:30 +08:00
}
return &git{
workDir: workDir,
gitCmdPath: gitCmdPath,
}
}
type git struct {
workDir string
gitCmdPath string
}
// Init 对应 git init 命令
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:10 2022/6/14
func (g *git) Init() *define.Result {
// 删除已有git信息
_ = Dir(g.workDir).Delete(".git")
return Execute(g.workDir, g.gitCmdPath, []string{"init"})
}
// RemoteAddOrigin 对应 git remote add origin xxxx 命令
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:20 2022/6/14
func (g *git) RemoteAddOrigin(gitRepoAddress string) *define.Result {
return Execute(g.workDir, g.gitCmdPath, []string{"remote", "add", "origin", gitRepoAddress})
}
// SetUpstreamOrigin 对应 git push --set-upstream origin xxx 命令
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:22 2022/6/14
func (g *git) SetUpstreamOrigin(origin string) *define.Result {
return Execute(g.workDir, g.gitCmdPath, []string{"push", "--set-upstream ", "origin", origin})
}
2022-06-14 20:30:14 +08:00
// Add 对应 git add xxx 命令
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:24 2022/6/14
func (g *git) Add(fileList ...string) *define.Result {
if len(fileList) == 0 {
2022-06-14 20:55:32 +08:00
fileList = []string{"."}
2022-06-14 20:30:14 +08:00
}
param := []string{"add"}
for _, item := range fileList {
param = append(param, item)
}
return Execute(g.workDir, g.gitCmdPath, param)
}
// Commit 对应 git commit -m 'xxxx'
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:26 2022/6/14
func (g *git) Commit(comment string) *define.Result {
if len(comment) == 0 {
comment = "add some code"
}
return Execute(g.workDir, g.gitCmdPath, []string{"commit", "-m", comment})
}
// Push 对应 git push
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:28 2022/6/14
func (g *git) Push(force bool) *define.Result {
param := []string{
"push",
}
if force {
param = append(param, "-f")
}
return Execute(g.workDir, g.gitCmdPath, param)
}
2022-06-14 20:36:50 +08:00
// AddGitignore 添加 .gitignore 文件
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:31 2022/6/14
func (g *git) AddGitignore(fileList ...string) *define.Result {
baseFileContent := `# Created by .ignore support plugin (hsz.mobi)
### Go template
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
*.xlsx
# Test binary, built with "go test -c"
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Dependency directories (remove the comment below to include it)
# vendor/
# goland的相关配置
.idea
# vscode 的相关配置
.vscode
# 构建后的产物目录
release
*_test.go
*.txt
`
for _, item := range fileList {
baseFileContent = baseFileContent + "\n" + item
}
// 将信息写入文件
return &define.Result{
WorkDir: g.workDir,
CmdPath: "",
Param: nil,
Error: File(g.workDir, ".gitignore").AutoCreate().SetClearHasContentForWrite().
SetOpenFlag(os.O_TRUNC, os.O_RDWR, os.O_CREATE).Write([]byte(baseFileContent)),
2022-06-14 20:36:50 +08:00
Output: nil,
StartTime: 0,
FinishTime: 0,
}
}
2022-06-14 20:44:07 +08:00
// ConfigAuthor 配置开发人员信息
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:40 2022/6/14
func (g *git) ConfigAuthor(name string, email string) *define.Result {
// git config user.name 白茶清欢
if len(name) > 0 {
param := []string{"config", "user.name", name}
if r := Execute(g.workDir, g.gitCmdPath, param); nil != r.Error {
return r
}
}
if len(email) > 0 {
param := []string{"config", "user.email", email}
if r := Execute(g.workDir, g.gitCmdPath, param); nil != r.Error {
return r
}
}
return nil
}
2022-06-27 14:11:14 +08:00
// GetAuthor ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:59 2023/1/12
func (g *git) GetAuthor(useGlobal bool) (string, string, error) {
var (
username string
email string
)
// git config [--global] user.name 白茶清欢
param := []string{"config"}
if useGlobal {
param = append(param, "--global")
}
nameParam := append(param, "user.name")
if r := Execute(g.workDir, g.gitCmdPath, nameParam); nil != r.Error {
return "", "", r.Error
} else {
username = string(r.Output)
}
mailParam := append(param, "user.email")
if r := Execute(g.workDir, g.gitCmdPath, mailParam); nil != r.Error {
return "", "", r.Error
} else {
email = string(r.Output)
}
return username, email, nil
}
2022-06-27 14:11:14 +08:00
// Clone 拉取 git 仓库代码
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:03 2022/6/27
func (g *git) Clone(repo string, saveDir string) *define.Result {
param := []string{
2022-06-27 16:31:27 +08:00
"clone",
2022-06-27 14:11:14 +08:00
repo,
}
if len(saveDir) > 0 {
param = append(param, saveDir)
}
return Execute(g.workDir, g.gitCmdPath, param)
}
2023-04-09 22:18:18 +08:00
// Version 获取版本信息
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 22:15 2023/4/9
func (g *git) Version() *define.Result {
return Execute(g.workDir, g.gitCmdPath, []string{"version"})
}