forked from eth0izzle/shhgit
/
git.go
56 lines (45 loc) · 1.18 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
45
46
47
48
49
50
51
52
53
54
55
56
package core
import (
"context"
"strings"
"time"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
)
type GitResourceType int
const (
LOCAL_SOURCE GitResourceType = iota
GITHUB_SOURCE
GITHUB_COMMENT
GIST_SOURCE
BITBUCKET_SOURCE
GITLAB_SOURCE
)
type GitResource struct {
Id int64
Type GitResourceType
Url string
Ref string
}
func CloneRepository(session *Session, url string, ref string, dir string) (*git.Repository, error) {
timeout := time.Duration(*session.Options.CloneRepositoryTimeout) * time.Second
localCtx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
session.Log.Debug("[%s] Cloning %s in to %s", url, ref, strings.Replace(dir, *session.Options.TempDirectory, "", -1))
opts := &git.CloneOptions{
Depth: 1,
RecurseSubmodules: git.NoRecurseSubmodules,
URL: url,
SingleBranch: true,
Tags: git.NoTags,
}
if ref != "" {
opts.ReferenceName = plumbing.ReferenceName(ref)
}
repository, err := git.PlainCloneContext(localCtx, dir, false, opts)
if err != nil {
session.Log.Debug("[%s] Cloning failed: %s", url, err.Error())
return nil, err
}
return repository, nil
}