diff --git a/lastcommitlog.go b/lastcommitlog.go new file mode 100644 index 00000000..0678c134 --- /dev/null +++ b/lastcommitlog.go @@ -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)) + } +} diff --git a/main.go b/main.go index ca516320..1b6fb11e 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,10 @@ package main import ( + "bufio" "fmt" "log" + "os" "os/exec" "path/filepath" "runtime" @@ -60,7 +62,7 @@ func gitExePath(hook *Hook) string { func main() { startTime := time.Now() - logLines := "~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\n" + startTime.Format(time.RFC822) + "\n" + logLines := fmt.Sprintf("~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\nSANDPOINTS GITHOOK (%s)\n\n", version) // init global struct/variables var hook *Hook @@ -75,13 +77,16 @@ func main() { hook.Gogit = true hook.PublicHTMLPath = filepath.Join("/var", "www", "html", "sandpoints") - // hookExe, err := os.Executable() - // check(err) - // hook.ExecutablePath = filepath.Dir(hookExe) + 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) + // hook.ExecutablePath = "/home/m/gitea-repositories/sandpoints/simplesandpoints.git/hooks/post-receive.d" + scanner := bufio.NewScanner(os.Stdin) + scanner.Scan() + scannerTxt := scanner.Text() + hookContext(hook, scannerTxt) gitPath := gitExePath(hook) giqi = &giq.Gogit{} // enforce go-git if mocking hook.ExecutablePath @@ -93,38 +98,39 @@ func main() { 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) + + logLines += fmt.Sprintln("~~~~~~~~~~~~~~~~~~~~~~~~~~~") 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)) + logLines += fmt.Sprintf("Web site path:\n%s", 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")) + logLines += fmt.Sprintf("Web site path:\n%s", filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName, "_preview")) } } else { - // fmt.Println("Web site:", hugo.DestinationDir) - logLines += fmt.Sprintf("Web site: %s\n", hugo.DestinationDir) + logLines += fmt.Sprintf("Web site path:\n%s", 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) + 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) } diff --git a/metahook.go b/metahook.go index eebb6c28..bac6f38c 100644 --- a/metahook.go +++ b/metahook.go @@ -1,19 +1,16 @@ package main import ( - "bufio" "os" "path/filepath" "strings" ) -func hookContext(hook *Hook) { - scanner := bufio.NewScanner(os.Stdin) - scanner.Scan() - if scanner.Text() == "" { +func hookContext(hook *Hook, scannerTxt string) { + if scannerTxt == "" { hook.Context = "PostCommit" - } else if strings.HasPrefix(scanner.Text(), "sandpoints-ext") { - revs := strings.Fields(scanner.Text()) + } else if strings.HasPrefix(scannerTxt, "sandpoints-ext") { + revs := strings.Fields(scannerTxt) if revs[1] == "publish" { hook.Publish = true } @@ -29,7 +26,7 @@ func hookContext(hook *Hook) { } else { // it only handles gitea use case if not Local or sandpoints-ext hook.Context = "PostReceive" - hook.Stdinput = scanner.Text() + hook.Stdinput = scannerTxt } } diff --git a/sphook b/sphook index 6bfad6a8..f1602d8f 100755 Binary files a/sphook and b/sphook differ