-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
fetch.go
123 lines (101 loc) · 2.86 KB
/
fetch.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package git
import (
"fmt"
"os"
"strings"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-billy/v5/osfs"
gogit "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/storage/memory"
)
func NewRemote(srcUrl string) (*GitRepo, error) {
rc := &config.RemoteConfig{
Name: "origin",
URLs: []string{
"https://" + srcUrl,
},
}
lo := &gogit.ListOptions{}
if strings.Contains(srcUrl, "github.com") && os.Getenv("GITHUB_TOKEN") != "" {
lo.Auth = &http.BasicAuth{
Username: "github-token", // yes, this can be anything except an empty string
Password: os.Getenv("GITHUB_TOKEN"),
}
// co.URL = "git@" + strings.Replace(srcUrl, "/", ":", 1)
}
// fmt.Println("URL:", rc.URLs[0])
// Clones the repository into the worktree (fs) and storer all the .git
// content into the storer
st := memory.NewStorage()
remote := gogit.NewRemote(st, rc)
return &GitRepo{
Store: st,
Remote: remote,
ListOptions: lo,
}, nil
}
func CloneLocalRepo(location string) (*GitRepo, error) {
fs := osfs.New(location)
// Only returning the Billy FS in this case
return &GitRepo{
FS: fs,
}, nil
}
func CloneRepoRef(srcUrl string, ref *plumbing.Reference) (*GitRepo, error) {
co := &gogit.CloneOptions{
URL: "https://" + srcUrl,
SingleBranch: true,
ReferenceName: ref.Name(),
}
fmt.Println("cloning:", co.URL, ref)
if strings.Contains(srcUrl, "github.com") && os.Getenv("GITHUB_TOKEN") != "" {
co.Auth = &http.BasicAuth{
Username: "github-token", // yes, this can be anything except an empty string
Password: os.Getenv("GITHUB_TOKEN"),
}
}
// Clones the repository into the worktree (fs) and storer all the .git
// content into the storer
st := memory.NewStorage()
fs := memfs.New()
r, err := gogit.Clone(st, fs, co)
if err != nil {
return nil, err
}
return &GitRepo{
Store: st,
FS: fs,
Repo: r,
}, nil
}
// FetchGit clone the repository inside FS.
// If private flag is set, it will look for netrc credentials, fallbacking to SSH
func FetchGit(FS billy.Filesystem, remote, owner, repo, tag string, private bool) error {
gco := &gogit.CloneOptions{
URL: fmt.Sprintf("https://%s/%s/%s", remote, owner, repo),
Depth: 1,
}
if tag != "v0.0.0" {
gco.ReferenceName = plumbing.NewTagReferenceName(tag)
gco.SingleBranch = true
}
if private {
if netrc, err := NetrcCredentials(remote); err != nil {
gco.Auth = &http.BasicAuth{
Username: netrc.Login,
Password: netrc.Password,
}
} else if ssh, err := SSHCredentials(remote); err != nil {
gco.Auth = ssh.Keys
gco.URL = fmt.Sprintf("%s@%s:%s/%s", ssh.User, remote, owner, repo)
} else {
return err
}
}
_, err := gogit.Clone(memory.NewStorage(), FS, gco)
return err
}