37 Star 407 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
git.go 1.50 KB
一键复制 编辑 原始数据 按行查看 历史
package git
import (
"bytes"
"fmt"
"net/url"
"os/exec"
"strings"
"github.com/pkg/errors"
)
func Clone(path, url, branch string) error {
return runcmd("git", "clone", "-b", branch, "--single-branch", url, path)
}
func Update(path, branch string) error {
if err := runcmd("git", "-C", path, "fetch"); err != nil {
return err
}
return runcmd("git", "-C", path, "checkout", fmt.Sprintf("origin/%s", branch))
}
func HeadCommit(path string) (string, error) {
cmd := exec.Command("git", "-C", path, "rev-parse", "HEAD")
output, err := cmd.Output()
return strings.Trim(string(output), "\n"), err
}
func RemoteBranchHeadCommit(url, branch string) (string, error) {
cmd := exec.Command("git", "ls-remote", url, branch)
output, err := cmd.CombinedOutput()
if err != nil {
return "", errors.Wrap(err, string(output))
}
parts := strings.Split(string(output), "\t")
return parts[0], nil
}
func IsValid(url string) bool {
err := runcmd("git", "ls-remote", url)
return err == nil
}
func runcmd(name string, arg ...string) error {
cmd := exec.Command(name, arg...)
bufErr := &bytes.Buffer{}
cmd.Stderr = bufErr
if err := cmd.Run(); err != nil {
return errors.Wrap(err, bufErr.String())
}
return nil
}
// FormatURL generates request url if is a private catalog
func FormatURL(pathURL, username, password string) string {
if len(username) > 0 && len(password) > 0 {
if u, err := url.Parse(pathURL); err == nil {
u.User = url.UserPassword(username, password)
return u.String()
}
}
return pathURL
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.0-rc2

搜索帮助