代码拉取完成,页面将自动刷新
package utils
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"syscall"
)
// Config for git command
type Config struct {
Bin string // default "git"
}
// Client of git command
type Client interface {
CanExec() error
Exec(string, ...string) (string, error)
InsideWorkTree() error
}
type clientImpl struct {
config *Config
}
// NewGit git command client
func NewGit(config *Config) Client {
bin := "git"
if config != nil {
if config.Bin != "" {
bin = config.Bin
}
}
return &clientImpl{
config: &Config{
Bin: bin,
},
}
}
// CanExec check whether the git command is executable
func (client *clientImpl) CanExec() error {
_, err := exec.LookPath(client.config.Bin)
if err != nil {
return fmt.Errorf("\"%s\" does not exists", client.config.Bin)
}
return nil
}
// Exec executes the git command
func (client *clientImpl) Exec(subcmd string, args ...string) (string, error) {
arr := append([]string{subcmd}, args...)
var out bytes.Buffer
cmd := exec.Command(client.config.Bin, arr...)
cmd.Stdout = &out
cmd.Stderr = ioutil.Discard
err := cmd.Run()
var exitError *exec.ExitError
if errors.As(err, &exitError) {
if waitStatus, ok := exitError.Sys().(syscall.WaitStatus); ok {
if waitStatus.ExitStatus() != 0 {
return "", err
}
}
}
return strings.TrimRight(strings.TrimSpace(out.String()), "\000"), nil
}
// InsideWorkTree check whether the current working directory is inside the git repository
func (client *clientImpl) InsideWorkTree() error {
out, err := client.Exec("rev-parse", "--is-inside-work-tree")
if err != nil {
return err
}
if out != "true" {
cwd, err := os.Getwd()
if err != nil {
return err
}
return fmt.Errorf("\"%s\" is no git repository", cwd)
}
return nil
}
var (
git = NewGit(nil)
)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。