From 7319448ffac69daa8e6ca2679d8640660d0519fc Mon Sep 17 00:00:00 2001 From: Keenan Nemetz Date: Sat, 6 Feb 2021 17:41:43 -0800 Subject: [PATCH] Update git importer for new data models. Update web views for new data models. --- cmd/multi/daemon.go | 1 + cmd/multi/import.go | 19 ++----------------- data/author.go | 7 +++---- data/record.go | 22 +++------------------- data/repository.go | 7 +------ git/git.go | 14 +++++++++++--- git/git_test.go | 6 +----- p2p/namesys.go | 2 +- peer/authors.go | 9 ++++++--- rpc/branch.go | 12 ++++++++++-- rpc/clone.go | 8 ++++---- rpc/commit.go | 11 +++++++++-- rpc/import.go | 8 +++++++- rpc/init.go | 10 ++++++++-- rpc/merge.go | 9 ++++++++- rpc/tag.go | 16 ++++++++++++++-- web/html/author.html | 5 +++-- web/html/tree.html | 10 +++++----- web/view.go | 4 ++++ 19 files changed, 101 insertions(+), 79 deletions(-) diff --git a/cmd/multi/daemon.go b/cmd/multi/daemon.go index 50313c1..0282b30 100644 --- a/cmd/multi/daemon.go +++ b/cmd/multi/daemon.go @@ -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 } diff --git a/cmd/multi/import.go b/cmd/multi/import.go index 281008a..ebc999a 100644 --- a/cmd/multi/import.go +++ b/cmd/multi/import.go @@ -1,8 +1,6 @@ package main import ( - "errors" - "os" "path/filepath" "github.com/multiverse-vcs/go-multiverse/rpc" @@ -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 @@ -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, @@ -72,7 +59,5 @@ func importAction(c *cli.Context) error { return err } - config := NewConfig(cwd) - config.Name = name - return config.Save() + return nil } diff --git a/data/author.go b/data/author.go index 9cbb1d2..8affb0f 100644 --- a/data/author.go +++ b/data/author.go @@ -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. @@ -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), } } diff --git a/data/record.go b/data/record.go index 39ff291..9dcb02d 100644 --- a/data/record.go +++ b/data/record.go @@ -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. @@ -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) + } } diff --git a/data/repository.go b/data/repository.go index 7e4b601..30b6035 100644 --- a/data/repository.go +++ b/data/repository.go @@ -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. @@ -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), diff --git a/git/git.go b/git/git.go index a45abb9..7a05533 100644 --- a/git/git.go +++ b/git/git.go @@ -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 @@ -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) } @@ -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 { diff --git a/git/git_test.go b/git/git_test.go index d3796fc..9bb7ced 100644 --- a/git/git_test.go +++ b/git/git_test.go @@ -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") - } } diff --git a/p2p/namesys.go b/p2p/namesys.go index f27ba21..01d24cf 100644 --- a/p2p/namesys.go +++ b/p2p/namesys.go @@ -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 } diff --git a/peer/authors.go b/peer/authors.go index d1e940d..d4312df 100644 --- a/peer/authors.go +++ b/peer/authors.go @@ -2,6 +2,7 @@ package peer import ( "context" + "errors" cbornode "github.com/ipfs/go-ipld-cbor" "github.com/libp2p/go-libp2p-core/peer" @@ -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 } @@ -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) diff --git a/rpc/branch.go b/rpc/branch.go index 60569dd..12f28dd 100644 --- a/rpc/branch.go +++ b/rpc/branch.go @@ -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. @@ -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) } diff --git a/rpc/clone.go b/rpc/clone.go index f876af7..186a714 100644 --- a/rpc/clone.go +++ b/rpc/clone.go @@ -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") diff --git a/rpc/commit.go b/rpc/commit.go index 51d2dd6..1581759 100644 --- a/rpc/commit.go +++ b/rpc/commit.go @@ -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) } diff --git a/rpc/import.go b/rpc/import.go index 4207472..470248c 100644 --- a/rpc/import.go +++ b/rpc/import.go @@ -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) } diff --git a/rpc/init.go b/rpc/init.go index 4e6d3e5..05d0283 100644 --- a/rpc/init.go +++ b/rpc/init.go @@ -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) } diff --git a/rpc/merge.go b/rpc/merge.go index a4e3ed4..6476f43 100644 --- a/rpc/merge.go +++ b/rpc/merge.go @@ -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) } diff --git a/rpc/tag.go b/rpc/tag.go index 2cb0cbb..613656f 100644 --- a/rpc/tag.go +++ b/rpc/tag.go @@ -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. @@ -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) } diff --git a/web/html/author.html b/web/html/author.html index beafde2..ae65f7e 100644 --- a/web/html/author.html +++ b/web/html/author.html @@ -2,8 +2,9 @@ {{ range $i, $v := .List }}

- - {{ $v.Name }} + {{ $name := (index $.Keys $i) }} + + {{ $name }}

diff --git a/web/html/tree.html b/web/html/tree.html index ac44fe9..069ec34 100644 --- a/web/html/tree.html +++ b/web/html/tree.html @@ -20,7 +20,7 @@