-
Notifications
You must be signed in to change notification settings - Fork 27
/
clone.go
53 lines (39 loc) · 1.11 KB
/
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
package git
import (
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
"io/ioutil"
"os"
)
var cloneMasterFunc = cloneMaster
const repositoryDirPrefix = "oec"
func CloneMaster(url, privateKeyFilepath, passPhrase string) (repositoryPath string, err error) {
tmpDir, err := ioutil.TempDir("", repositoryDirPrefix)
if err != nil {
return "", err
}
err = cloneMasterFunc(tmpDir, url, privateKeyFilepath, passPhrase)
if err != nil {
os.RemoveAll(tmpDir)
return "", err
}
return tmpDir, nil
}
func cloneMaster(tmpDir, gitUrl, privateKeyFilepath, passPhrase string) error {
options := &git.CloneOptions{
URL: gitUrl,
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth, // todo restrict max depth
ReferenceName: plumbing.Master,
SingleBranch: true,
}
if privateKeyFilepath != "" {
auth, err := ssh.NewPublicKeysFromFile(ssh.DefaultUsername, privateKeyFilepath, passPhrase)
if err != nil {
return err
}
options.Auth = auth
}
_, err := git.PlainClone(tmpDir, false, options)
return err
}