From 638bb23b903403cbb7922c27df21d1f4b6e7b660 Mon Sep 17 00:00:00 2001 From: Tom Neuber Date: Mon, 19 Aug 2024 14:10:46 +0200 Subject: [PATCH] style(git): preload commit logs & adjust changes check --- pkg/git/project.go | 61 +++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/pkg/git/project.go b/pkg/git/project.go index 512cf79..789deb8 100644 --- a/pkg/git/project.go +++ b/pkg/git/project.go @@ -3,9 +3,7 @@ package git import ( "context" "errors" - "fmt" "io" - "strings" "github.com/go-git/go-billy/v5" "github.com/go-git/go-billy/v5/memfs" @@ -41,9 +39,10 @@ func WithOutputWriter(o io.Writer) ProjectOption { } type Project struct { - Branch string - Force bool - RepoURL string + Branch string + Force bool + RepoURL string + CommitLogs map[string]*object.Commit auth transport.AuthMethod fs billy.Filesystem @@ -56,6 +55,7 @@ type Project struct { func NewProject(url string, options ...ProjectOption) *Project { project := &Project{ RepoURL: url, + CommitLogs: make(map[string]*object.Commit), fs: memfs.New(), storer: memory.NewStorage(), repository: nil, @@ -136,41 +136,40 @@ func (p *Project) Clone(ctx context.Context) error { return nil } -func (p *Project) CommitExists(uid string, id uint) bool { +func (p *Project) LoadLogs() error { commitIter, err := p.repository.Log(&git.LogOptions{}) + if err != nil { + return err + } + + return commitIter.ForEach(func(c *object.Commit) error { + p.CommitLogs[c.Message] = c + return nil + }) +} + +func (p *Project) CommitExists(commitmsg string) bool { + if _, ok := p.CommitLogs[commitmsg]; ok { + return true + } + + return false +} + +func (p *Project) HasChanges() bool { + localBranchRef, err := p.repository.Head() 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) + remoteBranchRef, err := p.repository.Reference(plumbing.NewRemoteReferenceName("origin", p.Branch), true) if errors.Is(err, plumbing.ErrReferenceNotFound) { return true } else if err != nil { return false } - localBranchRef, err := p.repository.Reference(plumbing.NewBranchReferenceName(p.Branch), true) - if err != nil { - return false - } - - if localBranchRef.Hash() != remoteBranch.Hash() { - return true - } - - return false + return localBranchRef.Hash() != remoteBranchRef.Hash() } func (p *Project) Pull(ctx context.Context) error { @@ -187,7 +186,9 @@ func (p *Project) Pull(ctx context.Context) error { } err := p.worktree.PullContext(ctx, &pullOpts) - if !errors.Is(err, plumbing.ErrReferenceNotFound) && err != nil { + if !errors.Is(err, plumbing.ErrReferenceNotFound) && + !errors.Is(err, git.NoErrAlreadyUpToDate) && + err != nil { return err }