Commit cce1481

Eric Bower  ·  2026-05-11 23:35:11 -0400 EDT
parent a36f4fa
feat: only write commit if file is missing

feat: only write tree files if shasum is different
1 files changed,  +58, -4
+58, -4
  1@@ -2,10 +2,12 @@ package main
  2 
  3 import (
  4 	"bytes"
  5+	"crypto/sha256"
  6 	"embed"
  7 	"flag"
  8 	"fmt"
  9 	"html/template"
 10+	"io"
 11 	"log/slog"
 12 	"math"
 13 	"os"
 14@@ -332,6 +334,47 @@ func (c *Config) writeHtml(writeData *WriteData) {
 15 	bail(err)
 16 }
 17 
 18+// writeHtmlIfChanged renders a template and only writes if content hash differs from existing file
 19+func (c *Config) writeHtmlIfChanged(writeData *WriteData) {
 20+	ts, err := template.ParseFS(
 21+		embedFS,
 22+		writeData.Template,
 23+		"html/header.partial.tmpl",
 24+		"html/footer.partial.tmpl",
 25+		"html/base.layout.tmpl",
 26+	)
 27+	bail(err)
 28+
 29+	// render to buffer first
 30+	var buf bytes.Buffer
 31+	err = ts.Execute(&buf, writeData.Data)
 32+	bail(err)
 33+
 34+	dir := filepath.Join(c.Outdir, writeData.Subdir)
 35+	err = os.MkdirAll(dir, os.ModePerm)
 36+	bail(err)
 37+
 38+	fp := filepath.Join(dir, writeData.Filename)
 39+
 40+	// compute hash of new content
 41+	newHash := sha256.Sum256(buf.Bytes())
 42+
 43+	// compare with existing file
 44+	if f, err := os.Open(fp); err == nil {
 45+		defer f.Close()
 46+		h := sha256.New()
 47+		if _, err := io.Copy(h, f); err == nil {
 48+			if bytes.Equal(h.Sum(nil), newHash[:]) {
 49+				return // unchanged, skip
 50+			}
 51+		}
 52+	}
 53+
 54+	c.Logger.Info("writing", "filepath", fp)
 55+	err = os.WriteFile(fp, buf.Bytes(), 0644)
 56+	bail(err)
 57+}
 58+
 59 func (c *Config) copyStatic(dir string) error {
 60 	entries, err := staticFS.ReadDir(dir)
 61 	bail(err)
 62@@ -405,6 +448,7 @@ func (c *Config) writeRefs(data *PageData, refs []*RefInfo) {
 63 }
 64 
 65 func (c *Config) writeHTMLTreeFile(pageData *PageData, treeItem *TreeItem) string {
 66+	d := filepath.Dir(treeItem.Path)
 67 	readme := ""
 68 	b, err := treeItem.Entry.Blob().Bytes()
 69 	bail(err)
 70@@ -419,15 +463,13 @@ func (c *Config) writeHTMLTreeFile(pageData *PageData, treeItem *TreeItem) strin
 71 		bail(err)
 72 	}
 73 
 74-	d := filepath.Dir(treeItem.Path)
 75-
 76 	nameLower := strings.ToLower(treeItem.Entry.Name())
 77 	summary := readmeFile(pageData.Repo)
 78 	if d == "." && nameLower == summary {
 79 		readme = contents
 80 	}
 81 
 82-	c.writeHtml(&WriteData{
 83+	c.writeHtmlIfChanged(&WriteData{
 84 		Filename: fmt.Sprintf("%s.html", treeItem.Entry.Name()),
 85 		Template: "html/file.page.tmpl",
 86 		Data: &FilePageData{
 87@@ -450,12 +492,22 @@ func (c *Config) writeLogDiff(repo *git.Repository, pageData *PageData, commit *
 88 	if hasCommit {
 89 		c.Logger.Info("commit file already generated, skipping", "commitID", getShortID(commitID))
 90 		return
 91-	} else {
 92+	}
 93+
 94+	// skip if output file already exists from a previous run
 95+	commitPath := filepath.Join(c.Outdir, "commits", commitID+".html")
 96+	if _, err := os.Stat(commitPath); err == nil {
 97+		c.Logger.Info("commit file exists, skipping", "commitID", getShortID(commitID))
 98 		c.Mutex.Lock()
 99 		c.Cache[commitID] = true
100 		c.Mutex.Unlock()
101+		return
102 	}
103 
104+	c.Mutex.Lock()
105+	c.Cache[commitID] = true
106+	c.Mutex.Unlock()
107+
108 	diff, err := repo.Diff(commitID, 0, 0, 0, git.DiffOptions{})
109 	bail(err)
110 
111@@ -1138,3 +1190,5 @@ func main() {
112 	url := filepath.Join("/", "index.html")
113 	config.Logger.Info("root url", "url", url)
114 }
115+
116+// modified