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.

141 lines
3.6 KiB

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