From 1d949f1fb8597ad860a9247e551f2a2cea070d12 Mon Sep 17 00:00:00 2001 From: Tom Neuber Date: Tue, 30 Jul 2024 20:41:16 +0200 Subject: [PATCH] refactor(git): move commit exists function to project --- pkg/git/commit.go | 18 ------------------ pkg/git/project.go | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/pkg/git/commit.go b/pkg/git/commit.go index 49ed170..a4508c1 100644 --- a/pkg/git/commit.go +++ b/pkg/git/commit.go @@ -1,10 +1,8 @@ package git import ( - "errors" "fmt" "path/filepath" - "strings" "time" "github.com/ProtonMail/go-crypto/openpgp" @@ -95,22 +93,6 @@ func (c *Commit) Create(msg string) error { return nil } -func (c *Commit) Exists(uid string, id uint) bool { - commitIter, err := c.project.repository.Log(&git.LogOptions{}) - if err != nil { - return false - } - - err = commitIter.ForEach(func(commit *object.Commit) error { - if strings.Contains(commit.Message, fmt.Sprintf("Update %s", uid)) && - strings.Contains(commit.Message, fmt.Sprintf("version %d", id)) { - return errors.New("version already committed") - } - return nil - }) - return err != nil -} - func (c *Commit) addContent() error { file, err := c.project.worktree.Filesystem.Create(c.Filename) if err != nil { diff --git a/pkg/git/project.go b/pkg/git/project.go index 058f3df..512cf79 100644 --- a/pkg/git/project.go +++ b/pkg/git/project.go @@ -3,12 +3,15 @@ package git import ( "context" "errors" + "fmt" "io" + "strings" "github.com/go-git/go-billy/v5" "github.com/go-git/go-billy/v5/memfs" "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" + "github.com/go-git/go-git/v5/plumbing/object" "github.com/go-git/go-git/v5/plumbing/transport" "github.com/go-git/go-git/v5/plumbing/transport/http" "github.com/go-git/go-git/v5/storage/memory" @@ -133,6 +136,22 @@ func (p *Project) Clone(ctx context.Context) error { return nil } +func (p *Project) CommitExists(uid string, id uint) bool { + commitIter, err := p.repository.Log(&git.LogOptions{}) + if err != nil { + return false + } + + err = commitIter.ForEach(func(commit *object.Commit) error { + if strings.Contains(commit.Message, fmt.Sprintf("Update %s", uid)) && + strings.Contains(commit.Message, fmt.Sprintf("version %d", id)) { + return errors.New("version already committed") + } + return nil + }) + return err != nil +} + func (p *Project) HasChanges() bool { remoteBranchRef := plumbing.NewRemoteReferenceName("origin", p.Branch) remoteBranch, err := p.repository.Reference(remoteBranchRef, true)