/
git.go
263 lines (214 loc) · 5.87 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Package git contains methods for importing Git repositories.
package git
import (
"context"
"io"
"os"
"path"
"path/filepath"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/filemode"
"github.com/go-git/go-git/v5/plumbing/object"
cid "github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
merkledag "github.com/ipfs/go-merkledag"
ufs "github.com/ipfs/go-unixfs"
ufsio "github.com/ipfs/go-unixfs/io"
"github.com/multiverse-vcs/go-multiverse/pkg/dag"
mobject "github.com/multiverse-vcs/go-multiverse/pkg/object"
)
// DateFormat is the format to store dates in.
const DateFormat = "Mon Jan 02 15:04:05 2006 -0700"
// importer adds objects from a git repo to a dag.
type importer struct {
ctx context.Context
dag ipld.DAGService
name string
repo *git.Repository
objects map[string]cid.Cid
branches map[string]cid.Cid
tags map[string]cid.Cid
}
// ImportFromURL is a helper to import a git repo from a url.
func ImportFromURL(ctx context.Context, dag ipld.DAGService, name, url string) (cid.Cid, error) {
dir := filepath.Join(os.TempDir(), "multi_git_import_"+name)
defer os.RemoveAll(dir)
opts := git.CloneOptions{
URL: url,
}
_, err := git.PlainClone(dir, false, &opts)
if err != nil {
return cid.Cid{}, err
}
return ImportFromFS(ctx, dag, name, dir)
}
// ImportFromFS is a helper to import a git repo from a directory.
func ImportFromFS(ctx context.Context, dag ipld.DAGService, name, dir string) (cid.Cid, error) {
dot := filepath.Join(dir, ".git")
repo, err := git.PlainOpen(dot)
if err != nil {
return cid.Cid{}, err
}
importer := NewImporter(ctx, dag, repo, name)
return importer.AddRepository()
}
// NewImporter returns an importer for the given repo.
func NewImporter(ctx context.Context, dag ipld.DAGService, repo *git.Repository, name string) *importer {
return &importer{
ctx: ctx,
dag: dag,
name: name,
repo: repo,
objects: make(map[string]cid.Cid),
branches: make(map[string]cid.Cid),
tags: make(map[string]cid.Cid),
}
}
// AddRepository adds all branches and tags to the dag.
func (i *importer) AddRepository() (cid.Cid, error) {
head, err := i.repo.Head()
if err != nil {
return cid.Cid{}, err
}
tags, err := i.repo.Tags()
if err != nil {
return cid.Cid{}, err
}
branches, err := i.repo.Branches()
if err != nil {
return cid.Cid{}, err
}
if err := branches.ForEach(i.AddBranch); err != nil {
return cid.Cid{}, err
}
if err := tags.ForEach(i.AddTag); err != nil {
return cid.Cid{}, err
}
defaultBranch := string(head.Name())
defaultBranch = path.Base(defaultBranch)
mrepo := mobject.NewRepository()
mrepo.Branches = i.branches
mrepo.Tags = i.tags
mrepo.DefaultBranch = defaultBranch
return mobject.AddRepository(i.ctx, i.dag, mrepo)
}
// AddBranch adds the branch with the given ref to the dag.
func (i *importer) AddBranch(ref *plumbing.Reference) error {
id, err := i.AddCommit(ref.Hash())
if err != nil {
return err
}
name := string(ref.Name())
name = path.Base(name)
i.branches[name] = id
return nil
}
// AddTag adds the tag with the given ref to the dag.
func (i *importer) AddTag(ref *plumbing.Reference) error {
id, err := i.AddCommit(ref.Hash())
if err != nil {
return err
}
name := string(ref.Name())
name = path.Base(name)
i.tags[name] = id
return nil
}
// AddCommit adds the commit with the given hash to the dag.
func (i *importer) AddCommit(hash plumbing.Hash) (cid.Cid, error) {
if id, ok := i.objects[hash.String()]; ok {
return id, nil
}
commit, err := i.repo.CommitObject(hash)
if err != nil {
return cid.Cid{}, err
}
var parents []cid.Cid
for _, h := range commit.ParentHashes {
parent, err := i.AddCommit(h)
if err != nil {
return cid.Cid{}, err
}
parents = append(parents, parent)
}
tree, err := i.AddTree(commit.TreeHash)
if err != nil {
return cid.Cid{}, err
}
mcommit := mobject.NewCommit()
mcommit.Tree = tree.Cid()
mcommit.Message = commit.Message
mcommit.Parents = parents
mcommit.Date = commit.Committer.When
mcommit.Metadata["git_hash"] = hash.String()
mcommit.Metadata["git_author_name"] = commit.Author.Name
mcommit.Metadata["git_author_email"] = commit.Author.Email
mcommit.Metadata["git_committer_name"] = commit.Committer.Name
mcommit.Metadata["git_committer_email"] = commit.Committer.Email
id, err := mobject.AddCommit(i.ctx, i.dag, mcommit)
if err != nil {
return cid.Cid{}, err
}
i.objects[hash.String()] = id
return id, nil
}
// AddTree adds the tree with the given hash to the dag.
func (i *importer) AddTree(hash plumbing.Hash) (ipld.Node, error) {
if id, ok := i.objects[hash.String()]; ok {
return i.dag.Get(i.ctx, id)
}
tree, err := i.repo.TreeObject(hash)
if err != nil {
return nil, err
}
dir := ufsio.NewDirectory(i.dag)
for _, entry := range tree.Entries {
subnode, err := i.AddTreeEntry(entry)
if err != nil {
return nil, err
}
if err := dir.AddChild(i.ctx, entry.Name, subnode); err != nil {
return nil, err
}
}
node, err := dir.GetNode()
if err != nil {
return nil, err
}
if err := i.dag.Add(i.ctx, node); err != nil {
return nil, err
}
i.objects[hash.String()] = node.Cid()
return node, nil
}
// AddTreeEntry adds the tree entry with the given hash to the dag.
func (i *importer) AddTreeEntry(entry object.TreeEntry) (ipld.Node, error) {
switch entry.Mode {
case filemode.Dir:
return i.AddTree(entry.Hash)
case filemode.Submodule:
return ufs.EmptyDirNode(), nil
}
blob, err := i.repo.BlobObject(entry.Hash)
if err != nil {
return nil, err
}
r, err := blob.Reader()
if err != nil {
return nil, err
}
defer r.Close()
if entry.Mode.IsFile() {
return dag.Chunk(i.ctx, i.dag, r)
}
target, err := io.ReadAll(r)
if err != nil {
return nil, err
}
node := merkledag.NodeWithData(target)
if err := i.dag.Add(i.ctx, node); err != nil {
return nil, err
}
return node, nil
}