repos / pgit

static site generator for git
git clone https://github.com/picosh/pgit.git

commit
5a7edb5
parent
78eb71e
author
Eric Bower
date
2024-03-19 20:11:44 +0000 UTC
refactor: use slog
3 files changed,  +33, -35
M go.mod
M go.sum
M go.mod
+1, -2
 1@@ -6,12 +6,11 @@ require (
 2 	github.com/alecthomas/chroma v0.10.0
 3 	github.com/dustin/go-humanize v1.0.0
 4 	github.com/gogs/git-module v1.6.0
 5-	go.uber.org/zap v1.25.0
 6 )
 7 
 8 require (
 9 	github.com/dlclark/regexp2 v1.7.0 // indirect
10 	github.com/mcuadros/go-version v0.0.0-20190308113854-92cdf37c5b75 // indirect
11-	go.uber.org/multierr v1.10.0 // indirect
12+	github.com/stretchr/testify v1.8.1 // indirect
13 	golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
14 )
M go.sum
+6, -6
 1@@ -1,6 +1,5 @@
 2 github.com/alecthomas/chroma v0.10.0 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbfjek=
 3 github.com/alecthomas/chroma v0.10.0/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s=
 4-github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
 5 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 6 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
 7 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 8@@ -16,14 +15,14 @@ github.com/mcuadros/go-version v0.0.0-20190308113854-92cdf37c5b75/go.mod h1:76rf
 9 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
10 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
11 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
12+github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
13+github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
14 github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
15 github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
16+github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
17+github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
18 github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
19-go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
20-go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
21-go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
22-go.uber.org/zap v1.25.0 h1:4Hvk6GtkucQ790dqmj7l1eEnRdKm3k3ZUrUMS2d5+5c=
23-go.uber.org/zap v1.25.0/go.mod h1:JIAUzQIH94IC4fOJQm7gMmBJP5k7wQfdcnYdPoEXJYk=
24+github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
25 golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
26 golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
27 golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
28@@ -31,3 +30,4 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
29 gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
30 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
31 gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
32+gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
M main.go
+26, -27
  1@@ -14,6 +14,7 @@ import (
  2 	"strings"
  3 	"sync"
  4 	"unicode/utf8"
  5+	"log/slog"
  6 
  7 	"github.com/alecthomas/chroma"
  8 	formatterHtml "github.com/alecthomas/chroma/formatters/html"
  9@@ -21,7 +22,6 @@ import (
 10 	"github.com/alecthomas/chroma/styles"
 11 	"github.com/dustin/go-humanize"
 12 	git "github.com/gogs/git-module"
 13-	"go.uber.org/zap"
 14 )
 15 
 16 //go:embed html/*.tmpl static/*
 17@@ -60,7 +60,7 @@ type Config struct {
 18 	// pretty name for the repo
 19 	RepoName string
 20 	// logger
 21-	Logger *zap.SugaredLogger
 22+	Logger *slog.Logger
 23 	// chroma style
 24 	Theme *chroma.Style
 25 }
 26@@ -320,7 +320,7 @@ func (c *Config) writeHtml(writeData *WriteData) {
 27 	bail(err)
 28 
 29 	fp := filepath.Join(dir, writeData.Filename)
 30-	c.Logger.Infof("writing (%s)", fp)
 31+	c.Logger.Info("writing", "filepath", fp)
 32 
 33 	w, err := os.OpenFile(fp, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
 34 	bail(err)
 35@@ -342,7 +342,7 @@ func (c *Config) copyStatic(dir string) error {
 36 		w, err := efs.ReadFile(infp)
 37 		bail(err)
 38 		fp := filepath.Join(c.Outdir, e.Name())
 39-		c.Logger.Infof("writing (%s)", fp)
 40+		c.Logger.Info("writing", "filepath", fp)
 41 		os.WriteFile(fp, w, 0755)
 42 	}
 43 
 44@@ -350,7 +350,7 @@ func (c *Config) copyStatic(dir string) error {
 45 }
 46 
 47 func (c *Config) writeRootSummary(data *PageData, readme template.HTML) {
 48-	c.Logger.Infof("writing root html (%s)", c.RepoPath)
 49+	c.Logger.Info("writing root html", "repoPath", c.RepoPath)
 50 	c.writeHtml(&WriteData{
 51 		Filename: "index.html",
 52 		Template: "html/summary.page.tmpl",
 53@@ -362,7 +362,7 @@ func (c *Config) writeRootSummary(data *PageData, readme template.HTML) {
 54 }
 55 
 56 func (c *Config) writeTree(data *PageData, tree *TreeRoot) {
 57-	c.Logger.Infof("writing tree (%s)", tree.Path)
 58+	c.Logger.Info("writing tree", "treePath", tree.Path)
 59 	c.writeHtml(&WriteData{
 60 		Filename: "index.html",
 61 		Subdir:   tree.Path,
 62@@ -375,7 +375,7 @@ func (c *Config) writeTree(data *PageData, tree *TreeRoot) {
 63 }
 64 
 65 func (c *Config) writeLog(data *PageData, logs []*CommitData) {
 66-	c.Logger.Infof("writing log file (%s)", data.RevData.Name())
 67+	c.Logger.Info("writing log file", "revision", data.RevData.Name())
 68 	c.writeHtml(&WriteData{
 69 		Filename: "index.html",
 70 		Subdir:   getLogBaseDir(data.RevData),
 71@@ -389,7 +389,7 @@ func (c *Config) writeLog(data *PageData, logs []*CommitData) {
 72 }
 73 
 74 func (c *Config) writeRefs(data *PageData, refs []*RefInfo) {
 75-	c.Logger.Infof("writing refs (%s)", c.RepoPath)
 76+	c.Logger.Info("writing refs", "repoPath", c.RepoPath)
 77 	c.writeHtml(&WriteData{
 78 		Filename: "refs.html",
 79 		Template: "html/refs.page.tmpl",
 80@@ -444,7 +444,7 @@ func (c *Config) writeLogDiff(repo *git.Repository, pageData *PageData, commit *
 81 	c.Mutex.RUnlock()
 82 
 83 	if hasCommit {
 84-		c.Logger.Infof("(%s) commit file already generated, skipping", getShortID(commitID))
 85+		c.Logger.Info("commit file already generated, skipping", "commitID", getShortID(commitID))
 86 		return
 87 	} else {
 88 		c.Mutex.Lock()
 89@@ -576,7 +576,7 @@ func getShortID(id string) string {
 90 }
 91 
 92 func (c *Config) writeRepo() *BranchOutput {
 93-	c.Logger.Infof("Writing repo (%s)", c.RepoPath)
 94+	c.Logger.Info("writing repo", "repoPath", c.RepoPath)
 95 	repo, err := git.Open(c.RepoPath)
 96 	bail(err)
 97 
 98@@ -656,7 +656,7 @@ func (c *Config) writeRepo() *BranchOutput {
 99 	})
100 
101 	for _, revData := range revs {
102-		c.Logger.Infof("Writing revision (%s)", revData.Name())
103+		c.Logger.Info("writing revision", "revision", revData.Name())
104 		data := &PageData{
105 			Repo:     c,
106 			RevData:  revData,
107@@ -858,6 +858,10 @@ func (tw *TreeWalker) walk(tree *git.Tree, curpath string) {
108 			return true
109 		}
110 
111+		if !treeEntries[i].IsDir && treeEntries[j].IsDir {
112+			return false
113+		}
114+
115 		return nameI < nameJ
116 	})
117 
118@@ -883,10 +887,10 @@ func (tw *TreeWalker) walk(tree *git.Tree, curpath string) {
119 }
120 
121 func (c *Config) writeRevision(repo *git.Repository, pageData *PageData, refs []*RefInfo) *BranchOutput {
122-	c.Logger.Infof(
123-		"compiling (%s) revision (%s)",
124-		c.RepoName,
125-		pageData.RevData.Name(),
126+	c.Logger.Info(
127+		"compiling revision",
128+		"repoName", c.RepoName,
129+		"revision", pageData.RevData.Name(),
130 	)
131 
132 	output := &BranchOutput{}
133@@ -998,10 +1002,10 @@ func (c *Config) writeRevision(repo *git.Repository, pageData *PageData, refs []
134 
135 	wg.Wait()
136 
137-	c.Logger.Infof(
138-		"compilation complete (%s) branch (%s)",
139-		c.RepoName,
140-		pageData.RevData.Name(),
141+	c.Logger.Info(
142+		"compilation complete branch",
143+		"repoName", c.RepoName,
144+		"revision", pageData.RevData.Name(),
145 	)
146 
147 	output.Readme = readme
148@@ -1029,12 +1033,7 @@ func main() {
149 
150 	theme := styles.Get(*themeFlag)
151 
152-	lg, err := zap.NewProduction()
153-	if err != nil {
154-		bail(err)
155-	}
156-
157-	logger := lg.Sugar()
158+	logger := slog.Default()
159 
160 	label := repoName(repoPath)
161 	if *labelFlag != "" {
162@@ -1060,7 +1059,7 @@ func main() {
163 		MaxCommits:         *maxCommitsFlag,
164 		HideTreeLastCommit: *hideTreeLastCommitFlag,
165 	}
166-	config.Logger.Infof("%+v", config)
167+	config.Logger.Info("config", "config", config)
168 
169 	if len(revs) == 0 {
170 		bail(fmt.Errorf("you must provide --revs"))
171@@ -1070,5 +1069,5 @@ func main() {
172 	config.copyStatic("static")
173 
174 	url := filepath.Join("/", "index.html")
175-	config.Logger.Info(url)
176+	config.Logger.Info("root url", "url", url)
177 }