-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-clone.go
87 lines (82 loc) · 1.8 KB
/
git-clone.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package utils
import (
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/sirupsen/logrus"
"io/ioutil"
"os"
)
func AuthClone(GitTmpDir, GitDir, AppName, Url, gitUser, GitPasswd string) error {
// Tempdir to clone the repository
_, err := ioutil.TempDir(GitTmpDir, AppName)
if err != nil {
logrus.Error(err)
return err
}
GitDirAppName := GitDir + "/" + AppName
err = os.RemoveAll(GitDirAppName) // clean up
if err != nil {
logrus.Error("git目标目录删除失败")
return err
}
// Clones the repository into the given dir, just as a normal LinuxCMD clone does
_, err = git.PlainClone(GitDirAppName, false, &git.CloneOptions{
URL: Url,
Auth: &http.BasicAuth{
Username: gitUser,
Password: GitPasswd,
},
})
if err != nil {
logrus.Error(err)
return err
}
return nil
}
func IsDir(path string) bool {
s, err := os.Stat(path)
if err != nil {
return false
}
return s.IsDir()
}
func Clone(GitTmpDir, GitDir, AppName, Url string) error {
// Tempdir to clone the repository
var err error
isOk := IsDir(GitTmpDir)
if isOk != true {
var err error
err = os.MkdirAll(GitTmpDir, 0755)
if err != nil {
logrus.Error(err)
}
}
isOk = IsDir(GitDir)
if isOk != true {
var err error
err = os.MkdirAll(GitDir, 0755)
if err != nil {
logrus.Error(err)
}
}
_, err = ioutil.TempDir(GitTmpDir, AppName)
if err != nil {
logrus.Error(err)
return err
}
GitDirAppName := GitDir + "/" + AppName
err = os.RemoveAll(GitDirAppName) // clean up
if err != nil {
logrus.Error("git目标目录删除失败")
return err
}
_, err = git.PlainClone(GitDirAppName, false, &git.CloneOptions{
URL: Url,
Progress: os.Stdout, //如果想关闭输出,请注释这行
})
if err != nil {
logrus.Error(err)
return err
}
return nil
}