|
| 1 | +// Package git contains methods for importing Git repositories. |
| 2 | +package git |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "io/ioutil" |
| 7 | + |
| 8 | + "github.com/go-git/go-git/v5" |
| 9 | + "github.com/go-git/go-git/v5/plumbing" |
| 10 | + "github.com/go-git/go-git/v5/plumbing/filemode" |
| 11 | + "github.com/go-git/go-git/v5/plumbing/object" |
| 12 | + "github.com/go-git/go-git/v5/storage/memory" |
| 13 | + "github.com/ipfs/go-cid" |
| 14 | + ipld "github.com/ipfs/go-ipld-format" |
| 15 | + "github.com/ipfs/go-merkledag" |
| 16 | + ufsio "github.com/ipfs/go-unixfs/io" |
| 17 | + "github.com/multiverse-vcs/go-multiverse/data" |
| 18 | + "github.com/multiverse-vcs/go-multiverse/unixfs" |
| 19 | +) |
| 20 | + |
| 21 | +// DateFormat is the format to store dates in. |
| 22 | +const DateFormat = "Mon Jan 02 15:04:05 2006 -0700" |
| 23 | + |
| 24 | +// importer adds objects from a git repo to a dag. |
| 25 | +type importer struct { |
| 26 | + ctx context.Context |
| 27 | + dag ipld.DAGService |
| 28 | + name string |
| 29 | + repo *git.Repository |
| 30 | + branches map[string]cid.Cid |
| 31 | + tags map[string]cid.Cid |
| 32 | +} |
| 33 | + |
| 34 | +// ImportFromFS is a helper to import a git repo from a directory. |
| 35 | +func ImportFromFS(ctx context.Context, dag ipld.DAGService, name, dir string) (cid.Cid, error) { |
| 36 | + repo, err := git.PlainOpen(dir) |
| 37 | + if err != nil { |
| 38 | + return cid.Cid{}, err |
| 39 | + } |
| 40 | + |
| 41 | + importer := NewImporter(ctx, dag, repo, name) |
| 42 | + return importer.AddRepository() |
| 43 | +} |
| 44 | + |
| 45 | +// ImportFromURL is a helper to import a git repo from a url. |
| 46 | +func ImportFromURL(ctx context.Context, dag ipld.DAGService, name, url string) (cid.Cid, error) { |
| 47 | + opts := git.CloneOptions{ |
| 48 | + URL: url, |
| 49 | + } |
| 50 | + |
| 51 | + repo, err := git.Clone(memory.NewStorage(), nil, &opts) |
| 52 | + if err != nil { |
| 53 | + return cid.Cid{}, err |
| 54 | + } |
| 55 | + |
| 56 | + importer := NewImporter(ctx, dag, repo, name) |
| 57 | + return importer.AddRepository() |
| 58 | +} |
| 59 | + |
| 60 | +// NewImporter returns an importer for the given repo. |
| 61 | +func NewImporter(ctx context.Context, dag ipld.DAGService, repo *git.Repository, name string) *importer { |
| 62 | + return &importer{ |
| 63 | + ctx: ctx, |
| 64 | + dag: dag, |
| 65 | + name: name, |
| 66 | + repo: repo, |
| 67 | + branches: make(map[string]cid.Cid), |
| 68 | + tags: make(map[string]cid.Cid), |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// AddRepository adds all branches and tags to the dag. |
| 73 | +func (i *importer) AddRepository() (cid.Cid, error) { |
| 74 | + tags, err := i.repo.Tags() |
| 75 | + if err != nil { |
| 76 | + return cid.Cid{}, err |
| 77 | + } |
| 78 | + |
| 79 | + branches, err := i.repo.Branches() |
| 80 | + if err != nil { |
| 81 | + return cid.Cid{}, err |
| 82 | + } |
| 83 | + |
| 84 | + if err := branches.ForEach(i.AddBranch); err != nil { |
| 85 | + return cid.Cid{}, err |
| 86 | + } |
| 87 | + |
| 88 | + if err := tags.ForEach(i.AddTag); err != nil { |
| 89 | + return cid.Cid{}, err |
| 90 | + } |
| 91 | + |
| 92 | + mrepo := data.NewRepository(i.name) |
| 93 | + mrepo.Branches = i.branches |
| 94 | + mrepo.Tags = i.tags |
| 95 | + |
| 96 | + return data.AddRepository(i.ctx, i.dag, mrepo) |
| 97 | +} |
| 98 | + |
| 99 | +// AddBranch adds the branch with the given ref to the dag. |
| 100 | +func (i *importer) AddBranch(ref *plumbing.Reference) error { |
| 101 | + id, err := i.AddCommit(ref.Hash()) |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + i.branches[string(ref.Name())] = id |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +// AddTag adds the tag with the given ref to the dag. |
| 111 | +func (i *importer) AddTag(ref *plumbing.Reference) error { |
| 112 | + id, err := i.AddCommit(ref.Hash()) |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + |
| 117 | + i.tags[string(ref.Name())] = id |
| 118 | + return nil |
| 119 | +} |
| 120 | + |
| 121 | +// AddCommit adds the commit with the given hash to the dag. |
| 122 | +func (i *importer) AddCommit(hash plumbing.Hash) (cid.Cid, error) { |
| 123 | + commit, err := i.repo.CommitObject(hash) |
| 124 | + if err != nil { |
| 125 | + return cid.Cid{}, err |
| 126 | + } |
| 127 | + |
| 128 | + var parents []cid.Cid |
| 129 | + for _, h := range commit.ParentHashes { |
| 130 | + parent, err := i.AddCommit(h) |
| 131 | + if err != nil { |
| 132 | + return cid.Cid{}, err |
| 133 | + } |
| 134 | + |
| 135 | + parents = append(parents, parent) |
| 136 | + } |
| 137 | + |
| 138 | + tree, err := i.AddTree(commit.TreeHash) |
| 139 | + if err != nil { |
| 140 | + return cid.Cid{}, err |
| 141 | + } |
| 142 | + |
| 143 | + mcommit := data.NewCommit(tree.Cid(), commit.Message, parents...) |
| 144 | + mcommit.Metadata["git_hash"] = hash.String() |
| 145 | + mcommit.Metadata["git_author_name"] = commit.Author.Name |
| 146 | + mcommit.Metadata["git_author_email"] = commit.Author.Email |
| 147 | + mcommit.Metadata["git_author_date"] = commit.Author.When.Format(DateFormat) |
| 148 | + mcommit.Metadata["git_committer_name"] = commit.Committer.Name |
| 149 | + mcommit.Metadata["git_committer_email"] = commit.Committer.Email |
| 150 | + mcommit.Metadata["git_committer_date"] = commit.Committer.When.Format(DateFormat) |
| 151 | + |
| 152 | + return data.AddCommit(i.ctx, i.dag, mcommit) |
| 153 | +} |
| 154 | + |
| 155 | +// AddTree adds the tree with the given hash to the dag. |
| 156 | +func (i *importer) AddTree(hash plumbing.Hash) (ipld.Node, error) { |
| 157 | + tree, err := i.repo.TreeObject(hash) |
| 158 | + if err != nil { |
| 159 | + return nil, err |
| 160 | + } |
| 161 | + |
| 162 | + dir := ufsio.NewDirectory(i.dag) |
| 163 | + for _, entry := range tree.Entries { |
| 164 | + subnode, err := i.AddTreeEntry(entry) |
| 165 | + if err != nil { |
| 166 | + return nil, err |
| 167 | + } |
| 168 | + |
| 169 | + if err := dir.AddChild(i.ctx, entry.Name, subnode); err != nil { |
| 170 | + return nil, err |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + node, err := dir.GetNode() |
| 175 | + if err != nil { |
| 176 | + return nil, err |
| 177 | + } |
| 178 | + |
| 179 | + if err := i.dag.Add(i.ctx, node); err != nil { |
| 180 | + return nil, err |
| 181 | + } |
| 182 | + |
| 183 | + return node, nil |
| 184 | +} |
| 185 | + |
| 186 | +// AddTreeEntry adds the tree entry with the given hash to the dag. |
| 187 | +func (i *importer) AddTreeEntry(entry object.TreeEntry) (ipld.Node, error) { |
| 188 | + if entry.Mode == filemode.Dir { |
| 189 | + return i.AddTree(entry.Hash) |
| 190 | + } |
| 191 | + |
| 192 | + blob, err := i.repo.BlobObject(entry.Hash) |
| 193 | + if err != nil { |
| 194 | + return nil, err |
| 195 | + } |
| 196 | + |
| 197 | + r, err := blob.Reader() |
| 198 | + if err != nil { |
| 199 | + return nil, err |
| 200 | + } |
| 201 | + defer r.Close() |
| 202 | + |
| 203 | + if entry.Mode.IsFile() { |
| 204 | + return unixfs.Chunk(i.ctx, i.dag, r) |
| 205 | + } |
| 206 | + |
| 207 | + target, err := ioutil.ReadAll(r) |
| 208 | + if err != nil { |
| 209 | + return nil, err |
| 210 | + } |
| 211 | + |
| 212 | + node := merkledag.NodeWithData(target) |
| 213 | + if err := i.dag.Add(i.ctx, node); err != nil { |
| 214 | + return nil, err |
| 215 | + } |
| 216 | + |
| 217 | + return node, nil |
| 218 | +} |
0 commit comments