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 (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/go-git/go-billy/v5"
|
"github.com/go-git/go-billy/v5"
|
||||||
"github.com/go-git/go-billy/v5/memfs"
|
"github.com/go-git/go-billy/v5/memfs"
|
||||||
|
@ -41,9 +39,10 @@ func WithOutputWriter(o io.Writer) ProjectOption {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Project struct {
|
type Project struct {
|
||||||
Branch string
|
Branch string
|
||||||
Force bool
|
Force bool
|
||||||
RepoURL string
|
RepoURL string
|
||||||
|
CommitLogs map[string]*object.Commit
|
||||||
|
|
||||||
auth transport.AuthMethod
|
auth transport.AuthMethod
|
||||||
fs billy.Filesystem
|
fs billy.Filesystem
|
||||||
|
@ -56,6 +55,7 @@ type Project struct {
|
||||||
func NewProject(url string, options ...ProjectOption) *Project {
|
func NewProject(url string, options ...ProjectOption) *Project {
|
||||||
project := &Project{
|
project := &Project{
|
||||||
RepoURL: url,
|
RepoURL: url,
|
||||||
|
CommitLogs: make(map[string]*object.Commit),
|
||||||
fs: memfs.New(),
|
fs: memfs.New(),
|
||||||
storer: memory.NewStorage(),
|
storer: memory.NewStorage(),
|
||||||
repository: nil,
|
repository: nil,
|
||||||
|
@ -136,41 +136,40 @@ func (p *Project) Clone(ctx context.Context) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Project) CommitExists(uid string, id uint) bool {
|
func (p *Project) LoadLogs() error {
|
||||||
commitIter, err := p.repository.Log(&git.LogOptions{})
|
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 {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
err = commitIter.ForEach(func(commit *object.Commit) error {
|
remoteBranchRef, err := p.repository.Reference(plumbing.NewRemoteReferenceName("origin", p.Branch), true)
|
||||||
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)
|
|
||||||
if errors.Is(err, plumbing.ErrReferenceNotFound) {
|
if errors.Is(err, plumbing.ErrReferenceNotFound) {
|
||||||
return true
|
return true
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
localBranchRef, err := p.repository.Reference(plumbing.NewBranchReferenceName(p.Branch), true)
|
return localBranchRef.Hash() != remoteBranchRef.Hash()
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if localBranchRef.Hash() != remoteBranch.Hash() {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Project) Pull(ctx context.Context) error {
|
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)
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue