This commit is contained in:
anqiansong
2022-03-05 22:52:32 +08:00
committed by GitHub
parent 08a8bd7ef7
commit 68a81fea8a
11 changed files with 95 additions and 20 deletions

View File

@@ -12,7 +12,7 @@ import (
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
)
func CloneIntoGitHome(url string) (dir string, err error) {
func CloneIntoGitHome(url string, branch string) (dir string, err error) {
gitHome, err := pathx.GetGitHome()
if err != nil {
return "", err
@@ -21,6 +21,9 @@ func CloneIntoGitHome(url string) (dir string, err error) {
ext := filepath.Ext(url)
repo := strings.TrimSuffix(filepath.Base(url), ext)
dir = filepath.Join(gitHome, repo)
if pathx.FileExists(dir) {
os.RemoveAll(dir)
}
path, err := env.LookPath("git")
if err != nil {
return "", err
@@ -28,7 +31,12 @@ func CloneIntoGitHome(url string) (dir string, err error) {
if !env.CanExec() {
return "", fmt.Errorf("os %q can not call 'exec' command", runtime.GOOS)
}
cmd := exec.Command(path, "clone", url, dir)
args := []string{"clone"}
if len(branch) > 0 {
args = append(args, "-b", branch)
}
args = append(args, url, dir)
cmd := exec.Command(path, args...)
cmd.Env = os.Environ()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr