Merge pull request #862 from MichaelMure/entity-interface

Michael Muré created

generalized interface for an Entity

Change summary

cache/repo_cache_bug.go     |  2 +-
entities/bug/bug.go         |  8 ++++++--
entities/bug/bug_actions.go | 22 +++-------------------
entity/dag/entity.go        |  2 +-
entity/dag/interface.go     | 29 ++++++++++++++---------------
5 files changed, 25 insertions(+), 38 deletions(-)

Detailed changes

cache/repo_cache_bug.go 🔗

@@ -501,7 +501,7 @@ func (c *RepoCache) RemoveBug(prefix string) error {
 	}
 
 	c.muBug.Lock()
-	err = bug.RemoveBug(c.repo, b.Id())
+	err = bug.Remove(c.repo, b.Id())
 
 	delete(c.bugs, b.Id())
 	delete(c.bugExcerpts, b.Id())

entities/bug/bug.go 🔗

@@ -29,6 +29,10 @@ var def = dag.Definition{
 
 var ClockLoader = dag.ClockLoader(def)
 
+type Interface interface {
+	dag.Interface[*Snapshot, Operation]
+}
+
 // Bug holds the data of a bug thread, organized in a way close to
 // how it will be persisted inside Git. This is the data structure
 // used to merge two different version of the same Bug.
@@ -119,7 +123,7 @@ func (bug *Bug) Validate() error {
 	}
 
 	// Check that there is no more CreateOp op
-	for i, op := range bug.Operations() {
+	for i, op := range bug.Entity.Operations() {
 		if i == 0 {
 			continue
 		}
@@ -146,7 +150,7 @@ func (bug *Bug) Operations() []Operation {
 	return result
 }
 
-// Compile a bug in a easily usable snapshot
+// Compile a bug in an easily usable snapshot
 func (bug *Bug) Compile() *Snapshot {
 	snap := &Snapshot{
 		id:     bug.Id(),

entities/bug/bug_actions.go 🔗

@@ -1,8 +1,6 @@
 package bug
 
 import (
-	"github.com/pkg/errors"
-
 	"github.com/MichaelMure/git-bug/entities/identity"
 	"github.com/MichaelMure/git-bug/entity"
 	"github.com/MichaelMure/git-bug/entity/dag"
@@ -25,21 +23,7 @@ func Push(repo repository.Repo, remote string) (string, error) {
 // Note: an author is necessary for the case where a merge commit is created, as this commit will
 // have an author and may be signed if a signing key is available.
 func Pull(repo repository.ClockedRepo, resolvers entity.Resolvers, remote string, mergeAuthor identity.Interface) error {
-	_, err := Fetch(repo, remote)
-	if err != nil {
-		return err
-	}
-
-	for merge := range MergeAll(repo, resolvers, remote, mergeAuthor) {
-		if merge.Err != nil {
-			return merge.Err
-		}
-		if merge.Status == entity.MergeStatusInvalid {
-			return errors.Errorf("merge failure: %s", merge.Reason)
-		}
-	}
-
-	return nil
+	return dag.Pull(def, repo, resolvers, remote, mergeAuthor)
 }
 
 // MergeAll will merge all the available remote bug
@@ -68,7 +52,7 @@ func MergeAll(repo repository.ClockedRepo, resolvers entity.Resolvers, remote st
 	return out
 }
 
-// RemoveBug will remove a local bug from its entity.Id
-func RemoveBug(repo repository.ClockedRepo, id entity.Id) error {
+// Remove will remove a local bug from its entity.Id
+func Remove(repo repository.ClockedRepo, id entity.Id) error {
 	return dag.Remove(def, repo, id)
 }

entity/dag/entity.go 🔗

@@ -361,7 +361,7 @@ func (e *Entity) Validate() error {
 		return fmt.Errorf("entity has no operations")
 	}
 
-	// check if each operations are valid
+	// check if each operation are valid
 	for _, op := range e.ops {
 		if err := op.Validate(); err != nil {
 			return err

entities/bug/interface.go → entity/dag/interface.go 🔗

@@ -1,4 +1,4 @@
-package bug
+package dag
 
 import (
 	"github.com/MichaelMure/git-bug/entity"
@@ -6,35 +6,34 @@ import (
 	"github.com/MichaelMure/git-bug/util/lamport"
 )
 
-type Interface interface {
-	// Id returns the Bug identifier
-	Id() entity.Id
+// Interface define the extended interface of a dag.Entity
+type Interface[SnapT Snapshot, OpT Operation] interface {
+	entity.Interface
 
-	// Validate checks if the Bug data is valid
+	// Validate checks if the Entity data is valid
 	Validate() error
 
 	// Append an operation into the staging area, to be committed later
-	Append(op Operation)
+	Append(op OpT)
 
 	// Operations returns the ordered operations
-	Operations() []Operation
+	Operations() []OpT
 
-	// NeedCommit indicates that the in-memory state changed and need to be commit in the repository
+	// NeedCommit indicates that the in-memory state changed and need to be committed in the repository
 	NeedCommit() bool
 
 	// Commit writes the staging area in Git and move the operations to the packs
 	Commit(repo repository.ClockedRepo) error
 
-	// FirstOp lookup for the very first operation of the bug.
-	// For a valid Bug, this operation should be a CreateOp
-	FirstOp() Operation
+	// FirstOp lookup for the very first operation of the Entity.
+	FirstOp() OpT
 
-	// LastOp lookup for the very last operation of the bug.
-	// For a valid Bug, should never be nil
-	LastOp() Operation
+	// LastOp lookup for the very last operation of the Entity.
+	// For a valid Entity, should never be nil
+	LastOp() OpT
 
 	// Compile a bug in an easily usable snapshot
-	Compile() *Snapshot
+	Compile() SnapT
 
 	// CreateLamportTime return the Lamport time of creation
 	CreateLamportTime() lamport.Time