refactor(git): move commit exists function to project

This commit is contained in:
Tom Neuber 2024-07-30 20:41:16 +02:00
parent d46a9f0883
commit 1d949f1fb8
Signed by: tom
GPG key ID: F17EFE4272D89FF6
2 changed files with 19 additions and 18 deletions

View file

@ -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 {

View file

@ -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)