forked from michenriksen/gitrob
-
Notifications
You must be signed in to change notification settings - Fork 16
/
git.go
44 lines (39 loc) · 1.07 KB
/
git.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
package gitlab
import (
"fmt"
"github.com/codeEmitter/gitrob/common"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
"gopkg.in/src-d/go-git.v4/storage/memory"
"io/ioutil"
)
func CloneRepository(cloneConfig *common.CloneConfiguration) (*git.Repository, string, error) {
cloneOptions := &git.CloneOptions{
URL: *cloneConfig.Url,
Depth: *cloneConfig.Depth,
ReferenceName: plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", *cloneConfig.Branch)),
SingleBranch: true,
Tags: git.NoTags,
Auth: &http.BasicAuth{
Username: *cloneConfig.Username,
Password: *cloneConfig.Token,
},
}
var repository *git.Repository
var err error
var dir string
if !*cloneConfig.InMemClone {
dir, err = ioutil.TempDir("", "gitrob")
if err != nil {
return nil, "", err
}
repository, err = git.PlainClone(dir, false, cloneOptions)
} else {
repository, err = git.Clone(memory.NewStorage(), nil, cloneOptions)
}
if err != nil {
return nil, dir, err
}
return repository, dir, nil
}