From 4c074099b8f305758ee88b319c7e0d32c2412038 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Thu, 25 Aug 2022 11:19:43 +0200 Subject: [PATCH] generalized interface for an Entity --- cache/repo_cache_bug.go | 2 +- entities/bug/bug.go | 8 +++++-- entities/bug/bug_actions.go | 22 +++-------------- entity/dag/entity.go | 2 +- {entities/bug => entity/dag}/interface.go | 29 +++++++++++------------ 5 files changed, 25 insertions(+), 38 deletions(-) rename {entities/bug => entity/dag}/interface.go (59%) diff --git a/cache/repo_cache_bug.go b/cache/repo_cache_bug.go index 4522f1bcbdefd55c3708c688f2aa8e24bef2ceff..dc2b271cd84455f7c46a48f5467e844ef19590f3 100644 --- a/cache/repo_cache_bug.go +++ b/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()) diff --git a/entities/bug/bug.go b/entities/bug/bug.go index 1140c543aea359c7662cb993df01848f1bf27169..b0f46c0b88fb65c102c9e700fb216441bfa002e4 100644 --- a/entities/bug/bug.go +++ b/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(), diff --git a/entities/bug/bug_actions.go b/entities/bug/bug_actions.go index 864c2052b9d5d0e59ab6e0fd8591abdf8da3786d..c25b92437cb467f9ba8aecd714c961f041154754 100644 --- a/entities/bug/bug_actions.go +++ b/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) } diff --git a/entity/dag/entity.go b/entity/dag/entity.go index 8b56127444cc116ab511939da8a166b3fb554fbb..ca674ad79d78454ea7625a209658af63ad8df994 100644 --- a/entity/dag/entity.go +++ b/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 diff --git a/entities/bug/interface.go b/entity/dag/interface.go similarity index 59% rename from entities/bug/interface.go rename to entity/dag/interface.go index 2ae31fd1287064c798b1aad7c250a15081adc5f9..613f60e60240bf3eb1da0688affa5ab72342b9c7 100644 --- a/entities/bug/interface.go +++ b/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