generated from kyma-project/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathgo2git.go
224 lines (187 loc) · 5.61 KB
/
go2git.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package git
import (
"crypto/md5"
"fmt"
"os"
"path"
"strings"
git2go "github.com/libgit2/git2go/v34"
"github.com/pkg/errors"
"go.uber.org/zap"
)
const (
tempDir = "/tmp"
branchRefPattern = "refs/remotes/origin"
)
type GitClient interface {
LastCommit(options Options) (string, error)
Clone(path string, options Options) (string, error)
}
var _ GitClient = &git2GoClient{}
type GitClientFactory struct {
}
func (f GitClientFactory) GetGitClient(logger *zap.SugaredLogger) GitClient {
return NewGit2Go(logger)
}
type Options struct {
URL string
Reference string
Auth *AuthOptions
}
type cloner interface {
git2goClone(url, outputPath string, remoteCallbacks git2go.RemoteCallbacks) (*git2go.Repository, error)
}
type fetcher interface {
git2goFetch(url, outputPath string, remoteCallbacks git2go.RemoteCallbacks) (*git2go.Repository, error)
}
type git2GoClient struct {
cloner
fetcher
}
func NewGit2Go(logger *zap.SugaredLogger) *git2GoClient {
return &git2GoClient{
cloner: &git2goCloner{},
fetcher: &git2goFetcher{logger: logger},
}
}
func mkRepoDir(options Options) (string, error) {
nameHash := md5.Sum([]byte(options.URL))
repoPath := path.Join(tempDir, fmt.Sprintf("%x", nameHash))
err := os.MkdirAll(repoPath, 0700)
return repoPath, err
}
func (g *git2GoClient) LastCommit(options Options) (string, error) {
//commit
_, err := git2go.NewOid(options.Reference)
if err == nil {
return options.Reference, nil
}
// TODO: This is NOT thread safe. If we ever decide to go with more than one worker, we need to refactor this. But for now it's fine.
repoDir, err := mkRepoDir(options)
if err != nil {
return "", errors.Wrap(err, "while creating temporary directory")
}
defer os.RemoveAll(repoDir)
repo, err := g.fetchRepo(options, repoDir)
if err != nil {
return "", errors.Wrap(err, "while fetching the repository")
}
defer repo.Free()
//branch
ref, err := g.lookupBranch(repo, options.Reference)
if err == nil {
defer ref.Free()
return ref.Target().String(), nil
}
if !git2go.IsErrorCode(err, git2go.ErrorCodeNotFound) {
return "", errors.Wrap(err, "while lookup branch")
}
//tag
commit, err := g.lookupTag(repo, options.Reference)
if err != nil {
return "", errors.Wrap(err, "while lookup tag")
}
defer commit.Free()
return commit.Id().String(), nil
}
func (g *git2GoClient) Clone(path string, options Options) (string, error) {
repo, err := g.cloneRepo(options, path)
if err != nil {
return "", errors.Wrap(err, "while cloning the repository")
}
defer repo.Free()
oid, err := git2go.NewOid(options.Reference)
if err != nil {
return "", errors.Wrap(err, "while creating oid from reference")
}
commit, err := repo.LookupCommit(oid)
if err != nil {
return "", errors.Wrap(err, "while lookup for commit")
}
defer commit.Free()
err = repo.ResetToCommit(commit, git2go.ResetHard, &git2go.CheckoutOptions{})
if err != nil {
return "", errors.Wrap(err, "while resetting to commit")
}
ref, err := repo.Head()
if err != nil {
return "", errors.Wrap(err, "while getting head")
}
defer ref.Free()
return ref.Target().String(), nil
}
func (g *git2GoClient) cloneRepo(opts Options, path string) (*git2go.Repository, error) {
authCallbacks, err := GetAuth(opts.Auth)
if err != nil {
return nil, errors.Wrap(err, "while getting authentication opts")
}
return g.git2goClone(opts.URL, path, authCallbacks)
}
func (g *git2GoClient) fetchRepo(opts Options, path string) (*git2go.Repository, error) {
authCallbacks, err := GetAuth(opts.Auth)
if err != nil {
return nil, errors.Wrap(err, "while getting authentication opts")
}
return g.git2goFetch(opts.URL, path, authCallbacks)
}
func (g *git2GoClient) lookupBranch(repo *git2go.Repository, branchName string) (*git2go.Reference, error) {
iter, err := repo.NewReferenceIterator()
if err != nil {
return nil, errors.Wrap(err, "while creating reference iterator")
}
defer iter.Free()
for {
item, err := iter.Next()
if err != nil {
if git2go.IsErrorCode(err, git2go.ErrorCodeIterOver) {
return nil, git2go.MakeGitError2(int(git2go.ErrorCodeNotFound))
}
return nil, errors.Wrap(err, "while listing reference")
}
if g.isBranch(item, branchName) {
return item, nil
}
defer item.Free()
}
}
func (g *git2GoClient) isBranch(ref *git2go.Reference, branchName string) bool {
if strings.Contains(ref.Name(), branchRefPattern) {
splittedName := strings.Split(ref.Name(), "/")
if len(splittedName) < 4 {
return false
}
return splittedName[3] == branchName
}
return false
}
/*
Some repositories like bitbucket set tags in different way.
The tag has reference to object, not to commit.
Using this reference we can checkout head to it. From head we can extract commit id.
This method will also works with repositories like GitLab in which the tag is reference to the commit.
The reference has the same id as commit and won't produce errors
*/
func (g *git2GoClient) lookupTag(repo *git2go.Repository, tagName string) (*git2go.Commit, error) {
ref, err := repo.References.Dwim(tagName)
if err != nil {
if git2go.IsErrorCode(err, git2go.ErrorCodeNotFound) {
return nil, err
}
return nil, errors.Wrap(err, "while creating dwim from tag name")
}
defer ref.Free()
if err = repo.SetHeadDetached(ref.Target()); err != nil {
return nil, errors.Wrapf(err, "while checkout to ref: %s", ref.Target().String())
}
head, err := repo.Head()
if err != nil {
return nil, errors.Wrap(err, "while getting head")
}
defer head.Free()
commit, err := repo.LookupCommit(head.Target())
if err != nil {
return nil, errors.Wrap(err, "while getting commit from head")
}
defer commit.Free()
return commit, nil
}