fixing stuff...

This commit is contained in:
Marcell Mars 2021-03-25 02:01:04 +01:00
parent dbd30f60a9
commit 277d854f1f
4 changed files with 56 additions and 27 deletions

26
lastcommitlog.go Normal file
View file

@ -0,0 +1,26 @@
package main
import (
"fmt"
"os"
"path/filepath"
)
func writeLogToFile(logLines, path string) {
lastPublishCommitLog, err := os.Create(filepath.Join(path, "last-commit-log.txt"))
check(err)
defer lastPublishCommitLog.Close()
fmt.Fprintln(lastPublishCommitLog, logLines)
}
func writeLastCommitLog(logLines string, isBare bool, hugo *Hugo, hook *Hook) {
if isBare {
if hook.Publish {
writeLogToFile(logLines, filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName))
} else {
writeLogToFile(logLines, filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName, "_preview"))
}
} else {
writeLogToFile(logLines, filepath.Join(hugo.DestinationDir))
}
}

44
main.go
View file

@ -1,8 +1,10 @@
package main package main
import ( import (
"bufio"
"fmt" "fmt"
"log" "log"
"os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -60,7 +62,7 @@ func gitExePath(hook *Hook) string {
func main() { func main() {
startTime := time.Now() startTime := time.Now()
logLines := "~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\n" + startTime.Format(time.RFC822) + "\n" logLines := fmt.Sprintf("~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\nSANDPOINTS GITHOOK (%s)\n\n", version)
// init global struct/variables // init global struct/variables
var hook *Hook var hook *Hook
@ -75,13 +77,16 @@ func main() {
hook.Gogit = true hook.Gogit = true
hook.PublicHTMLPath = filepath.Join("/var", "www", "html", "sandpoints") hook.PublicHTMLPath = filepath.Join("/var", "www", "html", "sandpoints")
// hookExe, err := os.Executable() hookExe, err := os.Executable()
// check(err) check(err)
// hook.ExecutablePath = filepath.Dir(hookExe) hook.ExecutablePath = filepath.Dir(hookExe)
// hook.ExecutablePath = "/home/m/Downloads/SimpleSandpoints/.git/hooks" // 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/dev.git/hooks/post-receive.d"
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"
hookContext(hook) scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
scannerTxt := scanner.Text()
hookContext(hook, scannerTxt)
gitPath := gitExePath(hook) gitPath := gitExePath(hook)
giqi = &giq.Gogit{} giqi = &giq.Gogit{}
// enforce go-git if mocking hook.ExecutablePath // enforce go-git if mocking hook.ExecutablePath
@ -93,38 +98,39 @@ func main() {
git = giqi.GitStruct(hook.ExecutablePath, hook.Context, hook.Stdinput, gitPath) git = giqi.GitStruct(hook.ExecutablePath, hook.Context, hook.Stdinput, gitPath)
hook.Publish = git.Publish hook.Publish = git.Publish
hugo.SourceDir = git.RepoPath hugo.SourceDir = git.RepoPath
// hugo.DestinationDir =
if git.IsBare { if git.IsBare {
giqi.AddBareWorktree(git.RepoPath, git.IndexPath, git.Worktree, gitPath) giqi.AddBareWorktree(git.RepoPath, git.IndexPath, git.Worktree, gitPath)
defer giqi.RemoveBareWorktree(git.RepoPath, git.IndexPath, git.Worktree, gitPath) defer giqi.RemoveBareWorktree(git.RepoPath, git.IndexPath, git.Worktree, gitPath)
hugo.SourceDir = git.TmpRepoPath hugo.SourceDir = git.TmpRepoPath
// tree, err := exec.Command("/usr/bin/tree", git.TmpRepoPath).Output()
// check(err)
// fmt.Println(string(tree))
} }
hugoContext(hugo, git.RepoPath) hugoContext(hugo, git.RepoPath)
logs := hugoRender(hugo, hook.Offline) logs := hugoRender(hugo, hook.Offline)
// fmt.Println(logs)
logLines += fmt.Sprintf(logs) logLines += fmt.Sprintf(logs)
if git.IsBare { if git.IsBare {
cleanUpPublicHTML(hugo, hook) cleanUpPublicHTML(hugo, hook)
copyToPublicHTML(hugo, hook) copyToPublicHTML(hugo, hook)
logLines += fmt.Sprintln("~~~~~~~~~~~~~~~~~~~~~~~~~~~")
if hook.Publish { if hook.Publish {
// fmt.Println("Web site:", filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName)) logLines += fmt.Sprintf("Web site path:\n%s", filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName))
logLines += fmt.Sprintf("Web site: %s\n", filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName))
} else { } else {
// fmt.Println("Web site:", filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName, "_preview")) logLines += fmt.Sprintf("Web site path:\n%s", filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName, "_preview"))
logLines += fmt.Sprintf("Web site: %s\n", filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName, "_preview"))
} }
} else { } else {
// fmt.Println("Web site:", hugo.DestinationDir) logLines += fmt.Sprintf("Web site path:\n%s", hugo.DestinationDir)
logLines += fmt.Sprintf("Web site: %s\n", hugo.DestinationDir)
} }
durationMillseconds := int64(time.Since(startTime) / time.Millisecond) 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) 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", hugo)
logLines = logLines + fmt.Sprintf("%#v", hook)
writeLastCommitLog(logLines, git.IsBare, hugo, hook)
fmt.Println(logLines) fmt.Println(logLines)
} }

View file

@ -1,19 +1,16 @@
package main package main
import ( import (
"bufio"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
) )
func hookContext(hook *Hook) { func hookContext(hook *Hook, scannerTxt string) {
scanner := bufio.NewScanner(os.Stdin) if scannerTxt == "" {
scanner.Scan()
if scanner.Text() == "" {
hook.Context = "PostCommit" hook.Context = "PostCommit"
} else if strings.HasPrefix(scanner.Text(), "sandpoints-ext") { } else if strings.HasPrefix(scannerTxt, "sandpoints-ext") {
revs := strings.Fields(scanner.Text()) revs := strings.Fields(scannerTxt)
if revs[1] == "publish" { if revs[1] == "publish" {
hook.Publish = true hook.Publish = true
} }
@ -29,7 +26,7 @@ func hookContext(hook *Hook) {
} else { } else {
// it only handles gitea use case if not Local or sandpoints-ext // it only handles gitea use case if not Local or sandpoints-ext
hook.Context = "PostReceive" hook.Context = "PostReceive"
hook.Stdinput = scanner.Text() hook.Stdinput = scannerTxt
} }
} }

BIN
sphook

Binary file not shown.