Skip to content

Commit

Permalink
add encode path
Browse files Browse the repository at this point in the history
  • Loading branch information
willscott authored and rvagg committed Jul 29, 2021
1 parent 7997bfa commit 42dafa5
Show file tree
Hide file tree
Showing 10 changed files with 329 additions and 9 deletions.
9 changes: 9 additions & 0 deletions blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,12 @@ func DecodeBlob(na ipld.NodeAssembler, rd *bufio.Reader) error {

return na.AssignBytes(buf.Bytes())
}

func encodeBlob(n ipld.Node, w io.Writer) error {
bytes, err := n.AsBytes()
if err != nil {
return err
}
_, err = bufio.NewWriter(w).Write(bytes)
return err
}
53 changes: 53 additions & 0 deletions commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,56 @@ func decodeGpgSig(rd *bufio.Reader) (GpgSig, error) {

return &out, nil
}

func encodeCommit(n ipld.Node, w io.Writer) error {
c, ok := n.(Commit)
if !ok {
return fmt.Errorf("not a Commit: %T", n)
}

buf := new(bytes.Buffer)
fmt.Fprintf(buf, "commit %s\x00", c.DataSize.x)
fmt.Fprintf(buf, "tree %s\n", hex.EncodeToString(c.GitTree.sha()))
for _, p := range c.Parents.x {
fmt.Fprintf(buf, "parent %s\n", hex.EncodeToString(p.sha()))
}
fmt.Fprintf(buf, "author %s\n", c.Author.v.GitString())
fmt.Fprintf(buf, "committer %s\n", c.Committer.v.GitString())
if len(c.Encoding.v.x) > 0 {
fmt.Fprintf(buf, "encoding %s\n", c.Encoding.v.x)
}
for _, mtag := range c.MergeTag.x {
fmt.Fprintf(buf, "mergetag object %s\n", hex.EncodeToString(mtag.Object.sha()))
fmt.Fprintf(buf, " type %s\n", mtag.TagType.x)
fmt.Fprintf(buf, " tag %s\n", mtag.Tag.x)
fmt.Fprintf(buf, " tagger %s\n \n", mtag.Tagger.GitString())
fmt.Fprintf(buf, "%s", mtag.Text.x)
}
if c.Sig.m == schema.Maybe_Value {
fmt.Fprintln(buf, "gpgsig -----BEGIN PGP SIGNATURE-----")
fmt.Fprint(buf, c.Sig.v.x)
fmt.Fprintln(buf, " -----END PGP SIGNATURE-----")
}
for _, line := range c.Other.x {
fmt.Fprintln(buf, line.x)
}
fmt.Fprintf(buf, "\n%s", c.Message.x)

_, err := bufio.NewWriter(w).Write(buf.Bytes())
return err
}

func (p _PersonInfo) GitString() string {
f := "%s <%s>"
arg := []interface{}{p.Name.x, p.Email.x}
if p.Date.x != "" {
f = f + " %s"
arg = append(arg, p.Date.x)
}

if p.Timezone.x != "" {
f = f + " %s"
arg = append(arg, p.Timezone.x)
}
return fmt.Sprintf(f, arg...)
}
1 change: 1 addition & 0 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func main() {
schema.SpawnStructField("Tag", "String", false, false),
schema.SpawnStructField("Tagger", "PersonInfo", false, false),
schema.SpawnStructField("Text", "String", false, false),
schema.SpawnStructField("DataSize", "String", true, false),
}, schema.SpawnStructRepresentationMap(map[string]string{"Type": "TagType"})))
ts.Accumulate(schema.SpawnList("ListTag", "Tag", false))
ts.Accumulate(schema.SpawnList("ListParents", "Link", false)) //Todo: type 'Parents' links
Expand Down
16 changes: 16 additions & 0 deletions init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ipldgit

import (
"github.com/ipfs/go-cid"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
)

var (
_ cidlink.MulticodecDecoder = Decoder
_ cidlink.MulticodecEncoder = Encoder
)

func init() {
cidlink.RegisterMulticodecDecoder(cid.GitRaw, Decoder)
cidlink.RegisterMulticodecEncoder(cid.GitRaw, Encoder)
}
Loading

0 comments on commit 42dafa5

Please sign in to comment.