You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
3.4 KiB

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "runtime"
  10. "time"
  11. "git.sandpoints.org/Drawwell/SandpointsGitHook/giq"
  12. )
  13. var (
  14. version = "21.03.04"
  15. logLines = ""
  16. )
  17. func check(e error) {
  18. if e != nil {
  19. logLines += fmt.Sprintf("\nGit hook version: %s\n~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\n", version)
  20. log.Fatal(e)
  21. panic(e)
  22. }
  23. }
  24. type Hugo struct {
  25. CatalogName string
  26. CatalogPrefix string
  27. SourceDir string
  28. DestinationDir string
  29. PublicHTMLName string
  30. }
  31. type Hook struct {
  32. Context string
  33. Publish bool
  34. Offline bool
  35. Gogit bool
  36. Stdinput string
  37. ExecutablePath string
  38. PublicHTMLPath string
  39. }
  40. func gitExePath(hook *Hook) string {
  41. gexe := "git"
  42. if runtime.GOOS == "windows" {
  43. gexe = "git.exe"
  44. }
  45. gpath, err := exec.LookPath(gexe)
  46. check(err)
  47. if gpath != "" {
  48. hook.Gogit = false
  49. return gpath
  50. } else {
  51. hook.Gogit = true
  52. return ""
  53. }
  54. }
  55. func main() {
  56. startTime := time.Now()
  57. logLines := fmt.Sprintf("~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\nSANDPOINTS GITHOOK (%s)\n\n", version)
  58. // init global struct/variables
  59. var hook *Hook
  60. var hugo *Hugo
  61. var git giq.Git
  62. var giqi giq.Giqi
  63. hook = new(Hook)
  64. hugo = new(Hugo)
  65. hook.Publish = false
  66. hook.Gogit = true
  67. hook.PublicHTMLPath = filepath.Join("/var", "www", "html", "sandpoints")
  68. hookExe, err := os.Executable()
  69. check(err)
  70. hook.ExecutablePath = filepath.Dir(hookExe)
  71. // hook.ExecutablePath = "/home/m/Downloads/SimpleSandpoints/.git/hooks"
  72. // hook.ExecutablePath = "/home/m/gitea-repositories/sandpoints/dev.git/hooks/post-receive.d"
  73. // hook.ExecutablePath = "/home/m/gitea-repositories/sandpoints/simplesandpoints.git/hooks/post-receive.d"
  74. scanner := bufio.NewScanner(os.Stdin)
  75. scanner.Scan()
  76. scannerTxt := scanner.Text()
  77. hookContext(hook, scannerTxt)
  78. gitPath := gitExePath(hook)
  79. giqi = &giq.Gogit{}
  80. // enforce go-git if mocking hook.ExecutablePath
  81. // if hook.Gogit {
  82. // DEFAULT but enforce sysgit if mocking hook.ExecutablePath
  83. if !hook.Gogit {
  84. giqi = &giq.Sysgit{}
  85. }
  86. git = giqi.GitStruct(hook.ExecutablePath, hook.Context, hook.Stdinput, gitPath)
  87. hook.Publish = git.Publish
  88. hugo.SourceDir = git.RepoPath
  89. if git.IsBare {
  90. giqi.AddBareWorktree(git.RepoPath, git.IndexPath, git.Worktree, gitPath)
  91. defer giqi.RemoveBareWorktree(git.RepoPath, git.IndexPath, git.Worktree, gitPath)
  92. hugo.SourceDir = git.TmpRepoPath
  93. }
  94. hugoContext(hugo, git.RepoPath)
  95. logs := hugoRender(hugo, hook.Offline)
  96. logLines += fmt.Sprintf(logs)
  97. if git.IsBare {
  98. cleanUpPublicHTML(hugo, hook)
  99. copyToPublicHTML(hugo, hook)
  100. logLines += fmt.Sprintln("~~~~~~~~~~~~~~~~~~~~~~~~~~~")
  101. if hook.Publish {
  102. logLines += fmt.Sprintf("Web site path:\n%s", filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName))
  103. } else {
  104. logLines += fmt.Sprintf("Web site path:\n%s", filepath.Join(hook.PublicHTMLPath, hugo.PublicHTMLName, "_preview"))
  105. }
  106. } else {
  107. logLines += fmt.Sprintf("Web site path:\n%s", hugo.DestinationDir)
  108. }
  109. durationMillseconds := int64(time.Since(startTime) / time.Millisecond)
  110. logLines = logLines + fmt.Sprintf("\n~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\nTotal githook time: %d ms\n~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\n", durationMillseconds)
  111. logLines = logLines + startTime.Format(time.RFC822) + "\n~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\n"
  112. logLines = logLines + "\n####################################\n"
  113. logLines = logLines + fmt.Sprintf("%#v", hugo)
  114. logLines = logLines + fmt.Sprintf("%#v", hook)
  115. writeLastCommitLog(logLines, git.IsBare, hugo, hook)
  116. fmt.Println(logLines)
  117. }