SandpointsGitHook/main.go

141 lines
3.6 KiB
Go
Raw Normal View History

package main
import (
2021-03-24 18:01:04 -07:00
"bufio"
"fmt"
"log"
2021-03-24 18:01:04 -07:00
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
2021-03-23 06:48:12 -07:00
"time"
"git.sandpoints.org/Drawwell/SandpointsGitHook/giq"
)
var (
version = "21.06.01"
2021-03-23 06:48:12 -07:00
logLines = ""
)
func check(e error) {
if e != nil {
2021-03-23 06:48:12 -07:00
logLines += fmt.Sprintf("\nGit hook version: %s\n~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\n", version)
fmt.Println(logLines)
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() {
2021-03-23 06:48:12 -07:00
startTime := time.Now()
logLines := fmt.Sprintf("\n~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\nSANDPOINTS GITHOOK (%s)\n\n", version)
fmt.Println(logLines)
// 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")
2021-03-24 18:01:04 -07:00
hookExe, err := os.Executable()
check(err)
hook.ExecutablePath = filepath.Dir(hookExe)
2021-03-23 06:48:12 -07:00
// hook.ExecutablePath = "/home/m/Downloads/SimpleSandpoints/.git/hooks"
// hook.ExecutablePath = "/home/m/gitea-repositories/sandpoints/dev.git/hooks/post-receive.d"
2021-03-24 18:01:04 -07:00
// hook.ExecutablePath = "/home/m/gitea-repositories/sandpoints/simplesandpoints.git/hooks/post-receive.d"
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
scannerTxt := strings.TrimSpace(scanner.Text())
2021-03-24 18:01:04 -07:00
hookContext(hook, scannerTxt)
gitPath := gitExePath(hook)
giqi = &giq.Gogit{}
// enforce go-git if mocking hook.ExecutablePath
2021-03-23 06:48:12 -07:00
// 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
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
}
hugoContext(hugo, git.RepoPath)
logs := hugoRender(hugo, hook)
2021-03-23 06:48:12 -07:00
logLines += fmt.Sprintf(logs)
if git.IsBare {
cleanUpPublicHTML(hugo, hook)
copyToPublicHTML(hugo, hook)
2021-03-24 18:01:04 -07:00
logLines += fmt.Sprintln("~~~~~~~~~~~~~~~~~~~~~~~~~~~")
2021-03-23 06:48:12 -07:00
if hook.Publish {
2021-03-24 18:01:04 -07:00
logLines += fmt.Sprintf("Web site path:\n%s", filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName))
2021-03-23 06:48:12 -07:00
} else {
2021-03-24 18:01:04 -07:00
logLines += fmt.Sprintf("Web site path:\n%s", filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName, "_preview"))
2021-03-23 06:48:12 -07:00
}
} else {
2021-03-24 18:01:04 -07:00
logLines += fmt.Sprintf("Web site path:\n%s", hugo.DestinationDir)
2021-03-23 06:48:12 -07:00
}
2021-03-24 18:01:04 -07:00
2021-03-23 06:48:12 -07:00
durationMillseconds := int64(time.Since(startTime) / time.Millisecond)
2021-03-24 18:01:04 -07:00
logLines = logLines + fmt.Sprintf("\n~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\nTotal githook time: %d ms\n~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\n", durationMillseconds)
logLines = logLines + startTime.Format(time.RFC822) + "\n~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\n"
logLines = logLines + "\n####################################\n"
// logLines = logLines + fmt.Sprintf("%#v\n", hugo)
// logLines = logLines + fmt.Sprintf("%#v\n", hook)
// logLines = logLines + fmt.Sprintf("%#v", git)
2021-03-24 18:01:04 -07:00
writeLastCommitLog(logLines, git.IsBare, hugo, hook)
2021-03-23 06:48:12 -07:00
fmt.Println(logLines)
}