style(git): preload commit logs & adjust changes check
This commit is contained in:
parent
453041b4b2
commit
638bb23b90
1 changed files with 31 additions and 30 deletions
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue