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
|
package giq
|
||||||
|
|
||||||
import "log"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
type Giqi interface {
|
type Giqi interface {
|
||||||
Whoami() string
|
Whoami() string
|
||||||
|
@ -30,3 +35,55 @@ func check(e error) {
|
||||||
panic(e)
|
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!"
|
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 {
|
func isPublish(r gogit.Repository, prevCommit, lastCommit string) bool {
|
||||||
ret := false
|
ret := false
|
||||||
pCommit, err := r.ResolveRevision(plumbing.Revision(prevCommit))
|
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()
|
headCommit, err := r.Head()
|
||||||
check(err)
|
check(err)
|
||||||
|
|
||||||
prevCommit := gitPreviousCommit.String()
|
|
||||||
lastCommit := headCommit.Hash().String()
|
|
||||||
|
|
||||||
if hookContext == "PostReceive" {
|
if hookContext == "PostReceive" {
|
||||||
revs := strings.Fields(hookStdinput)
|
revs := strings.Fields(hookStdinput)
|
||||||
prevCommit = revs[0]
|
git.PreviousCommit = revs[0]
|
||||||
lastCommit = revs[1]
|
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.RepoPath = repoPath
|
||||||
git.PreviousCommit = prevCommit
|
|
||||||
git.LastCommit = lastCommit
|
|
||||||
git.Branch = headCommit.Name().Short()
|
|
||||||
|
|
||||||
git.IndexPath = filepath.Join("/", ".git", "index")
|
git.IndexPath = filepath.Join("/", ".git", "index")
|
||||||
if git.IsBare {
|
if git.IsBare {
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -74,35 +75,40 @@ func isPublished(gitRepoPath, gitIndexPath, prevCommit, lastCommit string) bool
|
||||||
func (g *Sysgit) GitStruct(hookPath, hookContext, hookStdinput, gexe string) Git {
|
func (g *Sysgit) GitStruct(hookPath, hookContext, hookStdinput, gexe string) Git {
|
||||||
var git Git
|
var git Git
|
||||||
|
|
||||||
repositoryPath, err := exec.Command(gexe, "-C", hookPath, "rev-parse", "--git-dir").Output()
|
gitBase, err := detectGitPath(hookPath)
|
||||||
check(err)
|
check(err)
|
||||||
repoPath := stringifyOutput(repositoryPath)
|
|
||||||
gitBareOutput, err := exec.Command(gexe, "-C", repoPath, "rev-parse", "--is-bare-repository").Output()
|
repoPath := gitBase
|
||||||
check(err)
|
git.IsBare = true
|
||||||
gitBare := stringifyOutput(gitBareOutput)
|
|
||||||
if gitBare == "true" {
|
base := path.Base(gitBase)
|
||||||
git.IsBare = true
|
if base == ".git" {
|
||||||
} else {
|
|
||||||
git.IsBare = false
|
git.IsBare = false
|
||||||
repoPath = strings.Replace(stringifyOutput(repositoryPath), ".git", "", 1)
|
repoPath = strings.Replace(gitBase, ".git", "", 1)
|
||||||
check(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gitLastCommit, err := exec.Command(gexe, "-C", repoPath, "rev-parse", "HEAD").Output()
|
if hookContext == "PostReceive" {
|
||||||
check(err)
|
revs := strings.Fields(hookStdinput)
|
||||||
gitPreviousCommit, err := exec.Command(gexe, "-C", repoPath, "rev-parse", "HEAD^").Output()
|
git.PreviousCommit = revs[0]
|
||||||
check(err)
|
git.LastCommit = revs[1]
|
||||||
gitBranch, err := exec.Command(gexe, "-C", repoPath, "branch", "-a", "--contains", "HEAD").Output()
|
git.Branch = revs[2]
|
||||||
check(err)
|
} 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())
|
rand.Seed(time.Now().UnixNano())
|
||||||
git.Worktree = fmt.Sprintf("sandpoints_repo_%06d", rand.Intn(100001))
|
git.Worktree = fmt.Sprintf("sandpoints_repo_%06d", rand.Intn(100001))
|
||||||
git.TmpRepoPath = filepath.Join(os.TempDir(), git.Worktree)
|
git.TmpRepoPath = filepath.Join(os.TempDir(), git.Worktree)
|
||||||
|
|
||||||
git.RepoPath = repoPath
|
git.RepoPath = repoPath
|
||||||
git.LastCommit = stringifyOutput(gitLastCommit)
|
|
||||||
git.PreviousCommit = stringifyOutput(gitPreviousCommit)
|
|
||||||
git.Branch = strings.Replace(stringifyOutput(gitBranch), "* ", "", 1)
|
|
||||||
git.IndexPath = filepath.Join("/", ".git", "index")
|
git.IndexPath = filepath.Join("/", ".git", "index")
|
||||||
if git.IsBare {
|
if git.IsBare {
|
||||||
git.IndexPath = filepath.Join("/", "index")
|
git.IndexPath = filepath.Join("/", "index")
|
||||||
|
|
8
main.go
8
main.go
|
@ -8,13 +8,14 @@ import (
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.sandpoints.org/Drawwell/SandpointsGitHook/giq"
|
"git.sandpoints.org/Drawwell/SandpointsGitHook/giq"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
version = "21.04.01"
|
version = "21.04.03"
|
||||||
logLines = ""
|
logLines = ""
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -86,7 +87,8 @@ func main() {
|
||||||
// hook.ExecutablePath = "/home/m/gitea-repositories/sandpoints/simplesandpoints.git/hooks/post-receive.d"
|
// hook.ExecutablePath = "/home/m/gitea-repositories/sandpoints/simplesandpoints.git/hooks/post-receive.d"
|
||||||
scanner := bufio.NewScanner(os.Stdin)
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
scanner.Scan()
|
scanner.Scan()
|
||||||
scannerTxt := scanner.Text()
|
scannerTxt := strings.TrimSpace(scanner.Text())
|
||||||
|
|
||||||
hookContext(hook, scannerTxt)
|
hookContext(hook, scannerTxt)
|
||||||
gitPath := gitExePath(hook)
|
gitPath := gitExePath(hook)
|
||||||
giqi = &giq.Gogit{}
|
giqi = &giq.Gogit{}
|
||||||
|
@ -108,7 +110,7 @@ func main() {
|
||||||
|
|
||||||
hugoContext(hugo, git.RepoPath)
|
hugoContext(hugo, git.RepoPath)
|
||||||
|
|
||||||
logs := hugoRender(hugo, hook.Offline)
|
logs := hugoRender(hugo, hook)
|
||||||
logLines += fmt.Sprintf(logs)
|
logLines += fmt.Sprintf(logs)
|
||||||
if git.IsBare {
|
if git.IsBare {
|
||||||
cleanUpPublicHTML(hugo, hook)
|
cleanUpPublicHTML(hugo, hook)
|
||||||
|
|
|
@ -29,12 +29,14 @@ func hugoContext(hugo *Hugo, gitRepoPath string) {
|
||||||
hugo.DestinationDir = filepath.Join(hugo.SourceDir, "public")
|
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")
|
// fmt.Println("hugo", "-s", hugo.SourceDir, "-d", hugo.DestinationDir, "--templateMetrics")
|
||||||
logs := cage.Start()
|
logs := cage.Start()
|
||||||
hugoCommand := []string{"-s", hugo.SourceDir, "-d", hugo.DestinationDir, "--templateMetrics"}
|
hugoCommand := []string{"-s", hugo.SourceDir, "-d", hugo.DestinationDir, "--templateMetrics"}
|
||||||
if hookOffline {
|
if hook.Offline {
|
||||||
hugoCommand = append(hugoCommand, []string{"-e", "offline"}...)
|
hugoCommand = append(hugoCommand, []string{"-e", "offline"}...)
|
||||||
|
} else if hook.Context == "PostReceive" {
|
||||||
|
hugoCommand = append(hugoCommand, []string{"-e", "gitea"}...)
|
||||||
}
|
}
|
||||||
goHugo := commands.Execute(hugoCommand)
|
goHugo := commands.Execute(hugoCommand)
|
||||||
cage.Stop(logs)
|
cage.Stop(logs)
|
||||||
|
|
Loading…
Add table
Reference in a new issue