| 1 | package git |
| 2 | |
| 3 | import ( |
| 4 | "compress/zlib" |
| 5 | "io" |
| 6 | |
| 7 | "github.com/ipfs/kubo/plugin" |
| 8 | |
| 9 | // Note that depending on this package registers it's multicodec encoder and decoder. |
| 10 | git "github.com/ipfs/go-ipld-git" |
| 11 | "github.com/ipld/go-ipld-prime" |
| 12 | "github.com/ipld/go-ipld-prime/multicodec" |
| 13 | mc "github.com/multiformats/go-multicodec" |
| 14 | ) |
| 15 | |
| 16 | // Plugins is exported list of plugins that will be loaded. |
| 17 | var Plugins = []plugin.Plugin{ |
| 18 | &gitPlugin{}, |
| 19 | } |
| 20 | |
| 21 | type gitPlugin struct{} |
| 22 | |
| 23 | var _ plugin.PluginIPLD = (*gitPlugin)(nil) |
| 24 | |
| 25 | func (*gitPlugin) Name() string { |
| 26 | return "ipld-git" |
| 27 | } |
| 28 | |
| 29 | func (*gitPlugin) Version() string { |
| 30 | return "0.0.1" |
| 31 | } |
| 32 | |
| 33 | func (*gitPlugin) Init(_ *plugin.Environment) error { |
| 34 | return nil |
| 35 | } |
| 36 | |
| 37 | func (*gitPlugin) Register(reg multicodec.Registry) error { |
| 38 | // register a custom identifier in the reserved range for import of "zlib-encoded git objects." |
| 39 | reg.RegisterDecoder(uint64(mc.ReservedStart+mc.GitRaw), decodeZlibGit) |
| 40 | reg.RegisterEncoder(uint64(mc.GitRaw), git.Encode) |
| 41 | reg.RegisterDecoder(uint64(mc.GitRaw), git.Decode) |
| 42 | return nil |
| 43 | } |
| 44 | |
| 45 | func decodeZlibGit(na ipld.NodeAssembler, r io.Reader) error { |
| 46 | rc, err := zlib.NewReader(r) |
| 47 | if err != nil { |
| 48 | return err |
| 49 | } |
| 50 | |
| 51 | defer rc.Close() |
| 52 | |
| 53 | return git.Decode(na, rc) |
| 54 | } |