Skip to content

Commit

Permalink
Update git importer for new data models. Update web views for new dat…
Browse files Browse the repository at this point in the history
…a models.
  • Loading branch information
nasdf committed Feb 7, 2021
1 parent df4245e commit 7319448
Show file tree
Hide file tree
Showing 19 changed files with 101 additions and 79 deletions.
1 change: 1 addition & 0 deletions cmd/multi/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func daemonAction(c *cli.Context) error {
return err
}

// ensure any changes made offline will be published
if err := client.Authors().Publish(c.Context); err != nil {
return err
}
Expand Down
19 changes: 2 additions & 17 deletions cmd/multi/import.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package main

import (
"errors"
"os"
"path/filepath"

"github.com/multiverse-vcs/go-multiverse/rpc"
Expand Down Expand Up @@ -39,17 +37,6 @@ func importAction(c *cli.Context) error {
cli.ShowSubcommandHelpAndExit(c, 1)
}

name := c.Args().Get(0)

cwd, err := os.Getwd()
if err != nil {
return err
}

if _, err := FindConfig(cwd); err == nil {
return errors.New("repo already exists")
}

dir, err := filepath.Abs(c.String("dir"))
if err != nil {
return err
Expand All @@ -61,7 +48,7 @@ func importAction(c *cli.Context) error {
}

args := rpc.ImportArgs{
Name: name,
Name: c.Args().Get(0),
Type: c.String("type"),
URL: c.String("url"),
Dir: dir,
Expand All @@ -72,7 +59,5 @@ func importAction(c *cli.Context) error {
return err
}

config := NewConfig(cwd)
config.Name = name
return config.Save()
return nil
}
7 changes: 3 additions & 4 deletions data/author.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ import (

// Author contains info about a user.
type Author struct {
// Name is the human friendly name of the author.
Name string `json:"name"`
// Email is the email address of the author.
Email string `json:"email"`
// Repositories is a map of repositories.
Repositories map[string]cid.Cid `json:"repositories"`
// Metadata contains additional data.
Metadata map[string]string `json:"metadata"`
}

// GetAuthor returns the author with the given CID.
Expand Down Expand Up @@ -68,5 +66,6 @@ func AuthorFromCBOR(data []byte) (*Author, error) {
func NewAuthor() *Author {
return &Author{
Repositories: make(map[string]cid.Cid),
Metadata: make(map[string]string),
}
}
22 changes: 3 additions & 19 deletions data/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package data

import (
cbornode "github.com/ipfs/go-ipld-cbor"
"github.com/libp2p/go-libp2p-core/crypto"
)

// Record contains named record info.
Expand All @@ -25,26 +24,11 @@ func RecordFromCBOR(data []byte) (*Record, error) {
return &rec, nil
}

// NewRecord returns a signed record containing the given payload.
func NewRecord(payload []byte, sequence uint64, key crypto.PrivKey) (*Record, error) {
signature, err := key.Sign(payload)
if err != nil {
return nil, err
}

// NewRecord returns a record containing the given payload.
func NewRecord(payload []byte, sequence uint64, signature []byte) *Record {
return &Record{
Payload: payload,
Sequence: sequence,
Signature: signature,
}, nil
}

// Encode returns the record raw bytes.
func (r *Record) Encode() ([]byte, error) {
return cbornode.DumpObject(r)
}

// Verify returns true if the payload signature matches the given public key.
func (r *Record) Verify(key crypto.PubKey) (bool, error) {
return key.Verify(r.Payload, r.Signature)
}
}
7 changes: 1 addition & 6 deletions data/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@ import (

// Repository contains all versions of a project.
type Repository struct {
// Name is the human friendly name of the repo.
Name string `json:"name"`
// DefaultBranch is the base branch of the repo.
DefaultBranch string `json:"default_branch"`
// Description describes the project.
Description string `json:"description"`
// Branches is a map of names to commit CIDs.
Branches map[string]cid.Cid `json:"branches"`
// Tags is a map of names to commit CIDs.
Expand Down Expand Up @@ -71,9 +67,8 @@ func RepositoryFromCBOR(data []byte) (*Repository, error) {
}

// NewRepository returns a new repo.
func NewRepository(name string) *Repository {
func NewRepository() *Repository {
return &Repository{
Name: name,
Branches: make(map[string]cid.Cid),
Tags: make(map[string]cid.Cid),
Metadata: make(map[string]string),
Expand Down
14 changes: 11 additions & 3 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ func NewImporter(ctx context.Context, dag ipld.DAGService, repo *git.Repository,

// 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
Expand All @@ -92,9 +97,13 @@ func (i *importer) AddRepository() (cid.Cid, error) {
return cid.Cid{}, err
}

mrepo := data.NewRepository(i.name)
defaultBranch := string(head.Name())
defaultBranch = path.Base(defaultBranch)

mrepo := data.NewRepository()
mrepo.Branches = i.branches
mrepo.Tags = i.tags
mrepo.DefaultBranch = defaultBranch

return data.AddRepository(i.ctx, i.dag, mrepo)
}
Expand Down Expand Up @@ -154,13 +163,12 @@ func (i *importer) AddCommit(hash plumbing.Hash) (cid.Cid, error) {
}

mcommit := data.NewCommit(tree.Cid(), commit.Message, 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_author_date"] = commit.Author.When.Format(DateFormat)
mcommit.Metadata["git_committer_name"] = commit.Committer.Name
mcommit.Metadata["git_committer_email"] = commit.Committer.Email
mcommit.Metadata["git_committer_date"] = commit.Committer.When.Format(DateFormat)

id, err := data.AddCommit(i.ctx, i.dag, mcommit)
if err != nil {
Expand Down
6 changes: 1 addition & 5 deletions git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@ func TestImportFromURL(t *testing.T) {
t.Fatal("failed to import git repo")
}

repo, err := data.GetRepository(ctx, dag, id)
_, err = data.GetRepository(ctx, dag, id)
if err != nil {
t.Fatal("failed to get repo")
}

if repo.Name != "go-multiverse" {
t.Error("unexpected repo name")
}
}
2 changes: 1 addition & 1 deletion p2p/namesys.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (v validator) Validate(key string, value []byte) error {
return err
}

match, err := rec.Verify(pub)
match, err := pub.Verify(rec.Payload, rec.Signature)
if err != nil {
return err
}
Expand Down
9 changes: 6 additions & 3 deletions peer/authors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package peer

import (
"context"
"errors"

cbornode "github.com/ipfs/go-ipld-cbor"
"github.com/libp2p/go-libp2p-core/peer"
Expand All @@ -28,12 +29,14 @@ func (a *authors) Publish(ctx context.Context) error {
return err
}

rec, err := data.NewRecord(payload, a.config.Sequence, key)
signature, err := key.Sign(payload)
if err != nil {
return err
}

val, err := rec.Encode()
rec := data.NewRecord(payload, a.config.Sequence, signature)

val, err := cbornode.DumpObject(rec)
if err != nil {
return err
}
Expand All @@ -50,7 +53,7 @@ func (a *authors) Search(ctx context.Context, id peer.ID) (*data.Author, error)

val, ok := <-out
if !ok {
return nil, nil
return nil, errors.New("author not found")
}

rec, err := data.RecordFromCBOR(val)
Expand Down
12 changes: 10 additions & 2 deletions rpc/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ func (s *Service) CreateBranch(args *BranchArgs, reply *BranchReply) error {
cfg.Sequence++
cfg.Author.Repositories[args.Name] = id

return cfg.Save()
if err := cfg.Save(); err != nil {
return err
}

return s.client.Authors().Publish(ctx)
}

// DeleteBranch deletes an existing branch.
Expand Down Expand Up @@ -114,5 +118,9 @@ func (s *Service) DeleteBranch(args *BranchArgs, reply *BranchReply) error {
cfg.Sequence++
cfg.Author.Repositories[args.Name] = id

return cfg.Save()
if err := cfg.Save(); err != nil {
return err
}

return s.client.Authors().Publish(ctx)
}
8 changes: 4 additions & 4 deletions rpc/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ type CloneReply struct {
func (s *Service) Clone(args *CloneArgs, reply *CloneReply) error {
ctx := context.Background()

if args.Dir == "" {
return errors.New("dir is required")
}

repo, err := data.GetRepository(ctx, s.client, args.ID)
if err != nil {
return err
}

if args.Dir == "" {
args.Dir = repo.Name
}

id, ok := repo.Branches[args.Branch]
if !ok {
return errors.New("branch does not exist")
Expand Down
11 changes: 9 additions & 2 deletions rpc/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,21 @@ func (s *Service) Commit(args *CommitArgs, reply *CommitReply) error {
if err != nil {
return err
}

repo.Branches[args.Branch] = head
reply.Index = head

id, err = data.AddRepository(ctx, s.client, repo)
if err != nil {
return err
}

cfg.Sequence++
cfg.Author.Repositories[args.Name] = id

reply.Index = head
return cfg.Save()
if err := cfg.Save(); err != nil {
return err
}

return s.client.Authors().Publish(ctx)
}
8 changes: 7 additions & 1 deletion rpc/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ func (s *Service) Import(args *ImportArgs, reply *ImportReply) error {
return err
}

cfg.Sequence++
cfg.Author.Repositories[args.Name] = id
return cfg.Save()

if err := cfg.Save(); err != nil {
return err
}

return s.client.Authors().Publish(ctx)
}
10 changes: 8 additions & 2 deletions rpc/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,20 @@ func (s *Service) Init(args *InitArgs, reply *InitReply) error {
return errors.New("repo with name already exists")
}

repo := data.NewRepository(args.Name)
repo := data.NewRepository()
repo.DefaultBranch = args.Branch

id, err := data.AddRepository(ctx, s.client, repo)
if err != nil {
return err
}

cfg.Sequence++
cfg.Author.Repositories[args.Name] = id

return cfg.Save()
if err := cfg.Save(); err != nil {
return err
}

return s.client.Authors().Publish(ctx)
}
9 changes: 8 additions & 1 deletion rpc/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,25 @@ func (s *Service) Merge(args *MergeArgs, reply *MergeReply) error {
if err != nil {
return err
}

repo.Branches[args.Branch] = index
reply.Index = index

id, err = data.AddRepository(ctx, s.client, repo)
if err != nil {
return err
}

cfg.Sequence++
cfg.Author.Repositories[args.Name] = id

if err := cfg.Save(); err != nil {
return err
}

reply.Index = index
if err := s.client.Authors().Publish(ctx); err != nil {
return err
}

return unixfs.Write(ctx, s.client, args.Root, merge)
}
16 changes: 14 additions & 2 deletions rpc/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,15 @@ func (s *Service) CreateTag(args *TagArgs, reply *TagReply) error {
if err != nil {
return err
}

cfg.Sequence++
cfg.Author.Repositories[args.Name] = id

return cfg.Save()
if err := cfg.Save(); err != nil {
return err
}

return s.client.Authors().Publish(ctx)
}

// DeleteTag deletes an existing tag.
Expand Down Expand Up @@ -108,7 +114,13 @@ func (s *Service) DeleteTag(args *TagArgs, reply *TagReply) error {
if err != nil {
return err
}

cfg.Sequence++
cfg.Author.Repositories[args.Name] = id

return cfg.Save()
if err := cfg.Save(); err != nil {
return err
}

return s.client.Authors().Publish(ctx)
}
Loading

0 comments on commit 7319448

Please sign in to comment.