refactor(git): split / improve clone function
This commit is contained in:
parent
55c59e0164
commit
4aad919153
3 changed files with 189 additions and 88 deletions
|
@ -2,15 +2,13 @@ package git
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/ProtonMail/go-crypto/openpgp"
|
||||
"github.com/ProtonMail/go-crypto/openpgp/armor"
|
||||
"github.com/go-git/go-billy/v5"
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/go-git/go-git/v5/config"
|
||||
"github.com/go-git/go-git/v5/plumbing"
|
||||
"github.com/go-git/go-git/v5/plumbing/object"
|
||||
)
|
||||
|
||||
|
@ -36,10 +34,16 @@ func WithCommitter(name, email string) CommitOption {
|
|||
}
|
||||
}
|
||||
|
||||
func WithFileContent(content []byte, file billy.File) CommitOption {
|
||||
func WithFileContent(content []byte, filename, folder string) CommitOption {
|
||||
return func(c *Commit) {
|
||||
c.Content = content
|
||||
c.File = file
|
||||
c.Filename = filepath.Join(folder, fmt.Sprintf("%s.json", filename))
|
||||
}
|
||||
}
|
||||
|
||||
func WithSigner(signKey SignKey) CommitOption {
|
||||
return func(c *Commit) {
|
||||
c.signKey = signKey.entity
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -47,9 +51,11 @@ type Commit struct {
|
|||
Author *object.Signature
|
||||
Committer *object.Signature
|
||||
Content []byte
|
||||
File billy.File
|
||||
Filename string
|
||||
KeyFile string
|
||||
|
||||
project *Project
|
||||
signKey *openpgp.Entity
|
||||
}
|
||||
|
||||
func (p *Project) NewCommit(options ...CommitOption) *Commit {
|
||||
|
@ -63,64 +69,11 @@ func (p *Project) NewCommit(options ...CommitOption) *Commit {
|
|||
}
|
||||
|
||||
func (c *Commit) Create(msg string) error {
|
||||
var (
|
||||
signer *openpgp.Entity
|
||||
)
|
||||
|
||||
if c.project.KeyFile != "" {
|
||||
file, err := os.Open(c.project.KeyFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
block, err := armor.Decode(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
entityList, err := openpgp.ReadKeyRing(block.Body)
|
||||
if err != nil || len(entityList) < 1 {
|
||||
return err
|
||||
}
|
||||
|
||||
signer = entityList[0]
|
||||
}
|
||||
|
||||
worktree, err := c.project.repository.Worktree()
|
||||
if err != nil {
|
||||
if err := c.addContent(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = c.project.repository.Branch(c.project.Branch)
|
||||
if errors.Is(err, git.ErrBranchNotFound) {
|
||||
err = c.project.repository.CreateBranch(
|
||||
&config.Branch{Name: c.project.Branch, Remote: c.project.Branch},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = worktree.Checkout(&git.CheckoutOptions{
|
||||
Branch: plumbing.ReferenceName(c.project.Branch),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err = c.File.Write(c.Content); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = c.File.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err = worktree.Add(c.File.Name()); err != nil {
|
||||
if _, err := c.project.worktree.Add(c.Filename); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -130,25 +83,41 @@ func (c *Commit) Create(msg string) error {
|
|||
commitOpts.Committer = c.Committer
|
||||
}
|
||||
|
||||
if signer != nil {
|
||||
commitOpts.SignKey = signer
|
||||
if c.signKey != nil {
|
||||
commitOpts.SignKey = c.signKey
|
||||
}
|
||||
|
||||
_, err = worktree.Commit(msg, &commitOpts)
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Commit) Push() error {
|
||||
origin, err := c.project.repository.Remote("origin")
|
||||
_, err := c.project.worktree.Commit(msg, &commitOpts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pushOpts := git.PushOptions{}
|
||||
return nil
|
||||
}
|
||||
|
||||
if c.project.auth != nil {
|
||||
pushOpts.Auth = c.project.auth
|
||||
func (c *Commit) Exists(uid string, id uint) bool {
|
||||
commitIter, err := c.project.repository.Log(&git.LogOptions{})
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return origin.Push(&pushOpts)
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
_, err = file.Write(c.Content)
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue