You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
146 lines
3.3 KiB
Go
146 lines
3.3 KiB
Go
package giq
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"math/rand"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
gogit "github.com/go-git/go-git/v5"
|
|
"github.com/go-git/go-git/v5/plumbing"
|
|
"github.com/go-git/go-git/v5/plumbing/object"
|
|
)
|
|
|
|
func (g *Gogit) Whoami() string {
|
|
return "I am Gogit!"
|
|
}
|
|
|
|
func isPublish(r gogit.Repository, prevCommit, lastCommit string) bool {
|
|
ret := false
|
|
pCommit, err := r.ResolveRevision(plumbing.Revision(prevCommit))
|
|
check(err)
|
|
lCommit, err := r.ResolveRevision(plumbing.Revision(lastCommit))
|
|
check(err)
|
|
// fileName := "README.md"
|
|
hashCommitRange, err := r.Log(&gogit.LogOptions{
|
|
From: *lCommit,
|
|
// FileName: &fileName,
|
|
// Order:
|
|
})
|
|
check(err)
|
|
hashCommitMap := make(map[string]bool)
|
|
err = hashCommitRange.ForEach(func(c *object.Commit) error {
|
|
// fmt.Println(c.Hash.String(), c.Message)
|
|
hashCommitMap[c.Hash.String()] = true
|
|
ok := strings.Contains(c.Message, "!publish!")
|
|
if ok {
|
|
ret = true
|
|
return io.EOF
|
|
} else if c.Hash == *pCommit {
|
|
// return storer.ErrStop
|
|
return io.EOF
|
|
}
|
|
return nil
|
|
})
|
|
|
|
if ret {
|
|
return true
|
|
}
|
|
|
|
fileName := "PUBLISH_TRIGGER.md"
|
|
fileNameCommitRange, err := r.Log(&gogit.LogOptions{
|
|
FileName: &fileName,
|
|
})
|
|
check(err)
|
|
err = fileNameCommitRange.ForEach(func(c *object.Commit) error {
|
|
_, ok := hashCommitMap[c.Hash.String()]
|
|
if ok {
|
|
// return storer.ErrStop
|
|
ret = true
|
|
return io.EOF
|
|
}
|
|
return nil
|
|
})
|
|
|
|
fileMkdName := "PUBLISH_TRIGGER.mkd"
|
|
fileMkdNameCommitRange, err := r.Log(&gogit.LogOptions{
|
|
FileName: &fileMkdName,
|
|
})
|
|
check(err)
|
|
err = fileMkdNameCommitRange.ForEach(func(c *object.Commit) error {
|
|
_, ok := hashCommitMap[c.Hash.String()]
|
|
if ok {
|
|
// return storer.ErrStop
|
|
ret = true
|
|
return io.EOF
|
|
}
|
|
return nil
|
|
})
|
|
|
|
// fmt.Println(hashCommitMap)
|
|
if ret {
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func (g *Gogit) AddBareWorktree(gitRepoPath, gitIndexPath, gitWorktree, gexe string) {
|
|
tmpRepoPath := filepath.Join(os.TempDir(), gitWorktree)
|
|
_, err := gogit.PlainClone(tmpRepoPath, false, &gogit.CloneOptions{
|
|
URL: gitRepoPath,
|
|
Depth: 1,
|
|
Tags: gogit.NoTags,
|
|
// NoCheckout: true,
|
|
})
|
|
check(err)
|
|
// fmt.Println("WORKTREE:", tmpRepoPath)
|
|
}
|
|
func (g *Gogit) RemoveBareWorktree(gitRepoPath, gitIndexPath, gitWorktree, gexe string) {
|
|
tmpRepoPath := filepath.Join(os.TempDir(), gitWorktree)
|
|
err := os.RemoveAll(tmpRepoPath)
|
|
check(err)
|
|
}
|
|
|
|
func (g *Gogit) GitStruct(hookPath, hookContext, hookStdinput, gexe string) Git {
|
|
var git Git
|
|
_ = gexe
|
|
|
|
repoPath, gitIsBare, err := detectGitPath(hookPath)
|
|
check(err)
|
|
git.IsBare = gitIsBare
|
|
|
|
r, err := gogit.PlainOpen(repoPath)
|
|
check(err)
|
|
|
|
gitPreviousCommit, err := r.ResolveRevision(plumbing.Revision("HEAD^"))
|
|
check(err)
|
|
headCommit, err := r.Head()
|
|
check(err)
|
|
|
|
if hookContext == "PostReceive" {
|
|
revs := strings.Fields(hookStdinput)
|
|
git.PreviousCommit = revs[0]
|
|
git.LastCommit = revs[1]
|
|
git.Branch = revs[2]
|
|
} else {
|
|
git.PreviousCommit = gitPreviousCommit.String()
|
|
git.LastCommit = headCommit.Hash().String()
|
|
git.Branch = headCommit.Name().Short()
|
|
}
|
|
|
|
git.Publish = isPublish(*r, git.PreviousCommit, git.LastCommit)
|
|
git.RepoPath = repoPath
|
|
|
|
git.IndexPath = filepath.Join("/", ".git", "index")
|
|
if git.IsBare {
|
|
git.IndexPath = filepath.Join("/", "index")
|
|
}
|
|
|
|
git.Worktree = fmt.Sprintf("sandpoints_repo_%06d", rand.Intn(100001))
|
|
git.TmpRepoPath = filepath.Join(os.TempDir(), git.Worktree)
|
|
return git
|
|
}
|