calling git as gitea's hook needs too much git config envs
This commit is contained in:
parent
161fd486f9
commit
e85820d9f7
5 changed files with 100 additions and 86 deletions
59
giq/giq.go
59
giq/giq.go
|
@ -1,6 +1,11 @@
|
|||
package giq
|
||||
|
||||
import "log"
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type Giqi interface {
|
||||
Whoami() string
|
||||
|
@ -30,3 +35,55 @@ func check(e error) {
|
|||
panic(e)
|
||||
}
|
||||
}
|
||||
|
||||
func isGitDir(path string) (bool, error) {
|
||||
markers := []string{"HEAD", "objects", "refs"}
|
||||
|
||||
for _, marker := range markers {
|
||||
_, err := os.Stat(filepath.Join(path, marker))
|
||||
if err == nil {
|
||||
continue
|
||||
}
|
||||
if !os.IsNotExist(err) {
|
||||
return false, err
|
||||
} else {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func detectGitPath(path string) (string, error) {
|
||||
path, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
for {
|
||||
fi, err := os.Stat(filepath.Join(path, ".git"))
|
||||
if err == nil {
|
||||
if !fi.IsDir() {
|
||||
return "", fmt.Errorf(".git exist but is not a directory")
|
||||
}
|
||||
return filepath.Join(path, ".git"), nil
|
||||
}
|
||||
if !os.IsNotExist(err) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
ok, err := isGitDir(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if ok {
|
||||
return path, nil
|
||||
}
|
||||
|
||||
if parent := filepath.Dir(path); parent == path {
|
||||
return "", fmt.Errorf(".git not found")
|
||||
} else {
|
||||
path = parent
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
69
giq/gogit.go
69
giq/gogit.go
|
@ -18,58 +18,6 @@ func (g *Gogit) Whoami() string {
|
|||
return "I am Gogit!"
|
||||
}
|
||||
|
||||
func detectGitPath(path string) (string, error) {
|
||||
path, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
for {
|
||||
fi, err := os.Stat(filepath.Join(path, ".git"))
|
||||
if err == nil {
|
||||
if !fi.IsDir() {
|
||||
return "", fmt.Errorf(".git exist but is not a directory")
|
||||
}
|
||||
return filepath.Join(path, ".git"), nil
|
||||
}
|
||||
if !os.IsNotExist(err) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
ok, err := isGitDir(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if ok {
|
||||
return path, nil
|
||||
}
|
||||
|
||||
if parent := filepath.Dir(path); parent == path {
|
||||
return "", fmt.Errorf(".git not found")
|
||||
} else {
|
||||
path = parent
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func isGitDir(path string) (bool, error) {
|
||||
markers := []string{"HEAD", "objects", "refs"}
|
||||
|
||||
for _, marker := range markers {
|
||||
_, err := os.Stat(filepath.Join(path, marker))
|
||||
if err == nil {
|
||||
continue
|
||||
}
|
||||
if !os.IsNotExist(err) {
|
||||
return false, err
|
||||
} else {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func isPublish(r gogit.Repository, prevCommit, lastCommit string) bool {
|
||||
ret := false
|
||||
pCommit, err := r.ResolveRevision(plumbing.Revision(prevCommit))
|
||||
|
@ -166,20 +114,19 @@ func (g *Gogit) GitStruct(hookPath, hookContext, hookStdinput, gexe string) Git
|
|||
headCommit, err := r.Head()
|
||||
check(err)
|
||||
|
||||
prevCommit := gitPreviousCommit.String()
|
||||
lastCommit := headCommit.Hash().String()
|
||||
|
||||
if hookContext == "PostReceive" {
|
||||
revs := strings.Fields(hookStdinput)
|
||||
prevCommit = revs[0]
|
||||
lastCommit = revs[1]
|
||||
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, prevCommit, lastCommit)
|
||||
git.Publish = isPublish(*r, git.PreviousCommit, git.LastCommit)
|
||||
git.RepoPath = repoPath
|
||||
git.PreviousCommit = prevCommit
|
||||
git.LastCommit = lastCommit
|
||||
git.Branch = headCommit.Name().Short()
|
||||
|
||||
git.IndexPath = filepath.Join("/", ".git", "index")
|
||||
if git.IsBare {
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"math/rand"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
@ -74,35 +75,40 @@ func isPublished(gitRepoPath, gitIndexPath, prevCommit, lastCommit string) bool
|
|||
func (g *Sysgit) GitStruct(hookPath, hookContext, hookStdinput, gexe string) Git {
|
||||
var git Git
|
||||
|
||||
repositoryPath, err := exec.Command(gexe, "-C", hookPath, "rev-parse", "--git-dir").Output()
|
||||
gitBase, err := detectGitPath(hookPath)
|
||||
check(err)
|
||||
repoPath := stringifyOutput(repositoryPath)
|
||||
gitBareOutput, err := exec.Command(gexe, "-C", repoPath, "rev-parse", "--is-bare-repository").Output()
|
||||
check(err)
|
||||
gitBare := stringifyOutput(gitBareOutput)
|
||||
if gitBare == "true" {
|
||||
git.IsBare = true
|
||||
} else {
|
||||
|
||||
repoPath := gitBase
|
||||
git.IsBare = true
|
||||
|
||||
base := path.Base(gitBase)
|
||||
if base == ".git" {
|
||||
git.IsBare = false
|
||||
repoPath = strings.Replace(stringifyOutput(repositoryPath), ".git", "", 1)
|
||||
check(err)
|
||||
repoPath = strings.Replace(gitBase, ".git", "", 1)
|
||||
}
|
||||
|
||||
gitLastCommit, err := exec.Command(gexe, "-C", repoPath, "rev-parse", "HEAD").Output()
|
||||
check(err)
|
||||
gitPreviousCommit, err := exec.Command(gexe, "-C", repoPath, "rev-parse", "HEAD^").Output()
|
||||
check(err)
|
||||
gitBranch, err := exec.Command(gexe, "-C", repoPath, "branch", "-a", "--contains", "HEAD").Output()
|
||||
check(err)
|
||||
if hookContext == "PostReceive" {
|
||||
revs := strings.Fields(hookStdinput)
|
||||
git.PreviousCommit = revs[0]
|
||||
git.LastCommit = revs[1]
|
||||
git.Branch = revs[2]
|
||||
} else {
|
||||
o, err := exec.Command(gexe, "-C", repoPath, "rev-parse", "HEAD").Output()
|
||||
check(err)
|
||||
git.LastCommit = stringifyOutput(o)
|
||||
o, err = exec.Command(gexe, "-C", repoPath, "rev-parse", "HEAD^").Output()
|
||||
check(err)
|
||||
git.PreviousCommit = stringifyOutput(o)
|
||||
o, err = exec.Command(gexe, "-C", repoPath, "branch", "-a", "--contains", "HEAD").Output()
|
||||
check(err)
|
||||
git.Branch = strings.Replace(stringifyOutput(o), "* ", "", 1)
|
||||
}
|
||||
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
git.Worktree = fmt.Sprintf("sandpoints_repo_%06d", rand.Intn(100001))
|
||||
git.TmpRepoPath = filepath.Join(os.TempDir(), git.Worktree)
|
||||
|
||||
git.RepoPath = repoPath
|
||||
git.LastCommit = stringifyOutput(gitLastCommit)
|
||||
git.PreviousCommit = stringifyOutput(gitPreviousCommit)
|
||||
git.Branch = strings.Replace(stringifyOutput(gitBranch), "* ", "", 1)
|
||||
git.IndexPath = filepath.Join("/", ".git", "index")
|
||||
if git.IsBare {
|
||||
git.IndexPath = filepath.Join("/", "index")
|
||||
|
|
8
main.go
8
main.go
|
@ -8,13 +8,14 @@ import (
|
|||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.sandpoints.org/Drawwell/SandpointsGitHook/giq"
|
||||
)
|
||||
|
||||
var (
|
||||
version = "21.04.01"
|
||||
version = "21.04.03"
|
||||
logLines = ""
|
||||
)
|
||||
|
||||
|
@ -86,7 +87,8 @@ func main() {
|
|||
// hook.ExecutablePath = "/home/m/gitea-repositories/sandpoints/simplesandpoints.git/hooks/post-receive.d"
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
scanner.Scan()
|
||||
scannerTxt := scanner.Text()
|
||||
scannerTxt := strings.TrimSpace(scanner.Text())
|
||||
|
||||
hookContext(hook, scannerTxt)
|
||||
gitPath := gitExePath(hook)
|
||||
giqi = &giq.Gogit{}
|
||||
|
@ -108,7 +110,7 @@ func main() {
|
|||
|
||||
hugoContext(hugo, git.RepoPath)
|
||||
|
||||
logs := hugoRender(hugo, hook.Offline)
|
||||
logs := hugoRender(hugo, hook)
|
||||
logLines += fmt.Sprintf(logs)
|
||||
if git.IsBare {
|
||||
cleanUpPublicHTML(hugo, hook)
|
||||
|
|
|
@ -29,12 +29,14 @@ func hugoContext(hugo *Hugo, gitRepoPath string) {
|
|||
hugo.DestinationDir = filepath.Join(hugo.SourceDir, "public")
|
||||
}
|
||||
|
||||
func hugoRender(hugo *Hugo, hookOffline bool) string {
|
||||
func hugoRender(hugo *Hugo, hook *Hook) string {
|
||||
// fmt.Println("hugo", "-s", hugo.SourceDir, "-d", hugo.DestinationDir, "--templateMetrics")
|
||||
logs := cage.Start()
|
||||
hugoCommand := []string{"-s", hugo.SourceDir, "-d", hugo.DestinationDir, "--templateMetrics"}
|
||||
if hookOffline {
|
||||
if hook.Offline {
|
||||
hugoCommand = append(hugoCommand, []string{"-e", "offline"}...)
|
||||
} else if hook.Context == "PostReceive" {
|
||||
hugoCommand = append(hugoCommand, []string{"-e", "gitea"}...)
|
||||
}
|
||||
goHugo := commands.Execute(hugoCommand)
|
||||
cage.Stop(logs)
|
||||
|
|
Loading…
Add table
Reference in a new issue