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.

131 lines
3.4 KiB
Go

package main
import (
"fmt"
"log"
"os/exec"
"path/filepath"
"runtime"
"time"
"git.sandpoints.org/Drawwell/SandpointsGitHook/giq"
)
var (
version = "21.03.04"
logLines = ""
)
func check(e error) {
if e != nil {
logLines += fmt.Sprintf("\nGit hook version: %s\n~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\n", version)
log.Fatal(e)
panic(e)
}
}
type Hugo struct {
CatalogName string
CatalogPrefix string
SourceDir string
DestinationDir string
PublicHTMLName string
}
type Hook struct {
Context string
Publish bool
Offline bool
Gogit bool
Stdinput string
ExecutablePath string
PublicHTMLPath string
}
func gitExePath(hook *Hook) string {
gexe := "git"
if runtime.GOOS == "windows" {
gexe = "git.exe"
}
gpath, err := exec.LookPath(gexe)
check(err)
if gpath != "" {
hook.Gogit = false
return gpath
} else {
hook.Gogit = true
return ""
}
}
func main() {
startTime := time.Now()
logLines := "~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\n" + startTime.Format(time.RFC822) + "\n"
// init global struct/variables
var hook *Hook
var hugo *Hugo
var git giq.Git
var giqi giq.Giqi
hook = new(Hook)
hugo = new(Hugo)
hook.Publish = false
hook.Gogit = true
hook.PublicHTMLPath = filepath.Join("/var", "www", "html", "sandpoints")
// hookExe, err := os.Executable()
// check(err)
// hook.ExecutablePath = filepath.Dir(hookExe)
// hook.ExecutablePath = "/home/m/Downloads/SimpleSandpoints/.git/hooks"
// hook.ExecutablePath = "/home/m/gitea-repositories/sandpoints/dev.git/hooks/post-receive.d"
hook.ExecutablePath = "/home/m/gitea-repositories/sandpoints/simplesandpoints.git/hooks/post-receive.d"
hookContext(hook)
gitPath := gitExePath(hook)
giqi = &giq.Gogit{}
// enforce go-git if mocking hook.ExecutablePath
// if hook.Gogit {
// DEFAULT but enforce sysgit if mocking hook.ExecutablePath
if !hook.Gogit {
giqi = &giq.Sysgit{}
}
git = giqi.GitStruct(hook.ExecutablePath, hook.Context, hook.Stdinput, gitPath)
hook.Publish = git.Publish
hugo.SourceDir = git.RepoPath
// hugo.DestinationDir =
if git.IsBare {
giqi.AddBareWorktree(git.RepoPath, git.IndexPath, git.Worktree, gitPath)
defer giqi.RemoveBareWorktree(git.RepoPath, git.IndexPath, git.Worktree, gitPath)
hugo.SourceDir = git.TmpRepoPath
// tree, err := exec.Command("/usr/bin/tree", git.TmpRepoPath).Output()
// check(err)
// fmt.Println(string(tree))
}
hugoContext(hugo, git.RepoPath)
logs := hugoRender(hugo, hook.Offline)
// fmt.Println(logs)
logLines += fmt.Sprintf(logs)
if git.IsBare {
cleanUpPublicHTML(hugo, hook)
copyToPublicHTML(hugo, hook)
if hook.Publish {
// fmt.Println("Web site:", filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName))
logLines += fmt.Sprintf("Web site: %s\n", filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName))
} else {
// fmt.Println("Web site:", filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName, "_preview"))
logLines += fmt.Sprintf("Web site: %s\n", filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName, "_preview"))
}
} else {
// fmt.Println("Web site:", hugo.DestinationDir)
logLines += fmt.Sprintf("Web site: %s\n", hugo.DestinationDir)
}
durationMillseconds := int64(time.Since(startTime) / time.Millisecond)
logLines = logLines + fmt.Sprintf("Total processing time: %d ms\n\nGit hook version: %s\n~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\n", durationMillseconds, version)
fmt.Println(logLines)
}