repos / pgit

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

commit
173794d
parent
9185313
author
Eric Bower
date
2023-08-14 20:33:58 +0000 UTC
refactor: use reference name for URLs
2 files changed,  +83, -49
M html/header.partial.tmpl
+1, -1
1@@ -10,7 +10,7 @@
2 <nav class="text-lg">
3   <a href="{{.SiteURLs.SummaryURL}}">summary</a> |
4   <a href="{{.SiteURLs.RefsURL}}">refs</a> |
5-  <span class="font-bold">{{.RevData.RevName}}</span> |
6+  <span class="font-bold">{{.RevData.Name}}</span> |
7   <a href="{{.RevData.TreeURL}}">tree</a> |
8   <a href="{{.RevData.LogURL}}">log</a>
9 </nav>
M main.go
+82, -48
  1@@ -13,7 +13,6 @@ import (
  2 	"path/filepath"
  3 	"sort"
  4 	"strings"
  5-	"time"
  6 	"unicode/utf8"
  7 
  8 	"github.com/alecthomas/chroma"
  9@@ -70,12 +69,31 @@ type Config struct {
 10 	Theme *chroma.Style
 11 }
 12 
 13+type RevInfo interface {
 14+	ID() string
 15+	Name() string
 16+}
 17+
 18 // revision data
 19 type RevData struct {
 20-	ID      string
 21-	RevName string
 22-	TreeURL template.URL
 23-	LogURL  template.URL
 24+	id   string
 25+	name string
 26+}
 27+
 28+func (r *RevData) ID() string {
 29+	return r.id
 30+}
 31+
 32+func (r *RevData) Name() string {
 33+	return r.name
 34+}
 35+
 36+func (r *RevData) TreeURL() template.URL {
 37+	return getTreeURL(r)
 38+}
 39+
 40+func (r *RevData) LogURL() template.URL {
 41+	return getLogsURL(r)
 42 }
 43 
 44 type TagData struct {
 45@@ -283,7 +301,7 @@ func readmeFile(repo *Config) string {
 46 	return strings.ToLower(repo.Readme)
 47 }
 48 
 49-func walkTree(tree *git.Tree, commitID string, curpath string, aggregate []*TreeItem) []*TreeItem {
 50+func walkTree(tree *git.Tree, revData *RevData, curpath string, aggregate []*TreeItem) []*TreeItem {
 51 	entries, err := tree.Entries()
 52 	bail(err)
 53 
 54@@ -292,7 +310,7 @@ func walkTree(tree *git.Tree, commitID string, curpath string, aggregate []*Tree
 55 		typ := entry.Type()
 56 		if typ == git.ObjectTree {
 57 			re, _ := tree.Subtree(entry.Name())
 58-			aggregate = walkTree(re, commitID, fname, aggregate)
 59+			aggregate = walkTree(re, revData, fname, aggregate)
 60 		}
 61 
 62 		if entry.Type() == git.ObjectBlob {
 63@@ -300,7 +318,7 @@ func walkTree(tree *git.Tree, commitID string, curpath string, aggregate []*Tree
 64 				Size:  toPretty(entry.Size()),
 65 				Path:  fname,
 66 				Entry: entry,
 67-				URL:   template.URL(filepath.Join("/", "tree", commitID, "item", fname)),
 68+				URL:   template.URL(getFileURL(revData, fname)),
 69 			})
 70 		}
 71 	}
 72@@ -352,7 +370,7 @@ func (c *Config) writeRootSummary(data *PageData, readme template.HTML) {
 73 func (c *Config) writeTree(data *PageData, tree []*TreeItem) {
 74 	c.writeHtml(&WriteData{
 75 		Filename: "index.html",
 76-		Subdir:   filepath.Join("tree", getShortID(data.RevData.ID)),
 77+		Subdir:   getTreeBaseDir(data.RevData),
 78 		Template: "html/tree.page.tmpl",
 79 		Data: &TreePageData{
 80 			PageData: data,
 81@@ -364,7 +382,7 @@ func (c *Config) writeTree(data *PageData, tree []*TreeItem) {
 82 func (c *Config) writeLog(data *PageData, logs []*CommitData) {
 83 	c.writeHtml(&WriteData{
 84 		Filename: "index.html",
 85-		Subdir:   filepath.Join("logs", getShortID(data.RevData.ID)),
 86+		Subdir:   getLogBaseDir(data.RevData),
 87 		Template: "html/log.page.tmpl",
 88 		Data: &LogPageData{
 89 			PageData: data,
 90@@ -416,7 +434,7 @@ func (c *Config) writeHTMLTreeFiles(pageData *PageData, tree []*TreeItem) string
 91 				Contents: template.HTML(contents),
 92 				Path:     file.Path,
 93 			},
 94-			Subdir: filepath.Join("tree", getShortID(pageData.RevData.ID), "item", d),
 95+			Subdir: getFileURL(pageData.RevData, d),
 96 		})
 97 	}
 98 	return readme
 99@@ -442,7 +460,7 @@ func (c *Config) writeLogDiffs(repo *git.Repository, pageData *PageData, logs []
100 			pt := ancestors[0]
101 			parent = &CommitData{
102 				Commit: pt,
103-				URL:    c.getCommitURL(pt.ID.String()),
104+				URL:    getCommitURL(pt.ID.String()),
105 			}
106 		}
107 		parentID := parent.ID.String()
108@@ -492,8 +510,8 @@ func (c *Config) writeLogDiffs(repo *git.Repository, pageData *PageData, logs []
109 			CommitID:  getShortID(commitID),
110 			Diff:      rnd,
111 			Parent:    getShortID(parentID),
112-			CommitURL: c.getCommitURL(commitID),
113-			ParentURL: c.getCommitURL(parentID),
114+			CommitURL: getCommitURL(commitID),
115+			ParentURL: getCommitURL(parentID),
116 		}
117 
118 		c.writeHtml(&WriteData{
119@@ -505,27 +523,47 @@ func (c *Config) writeLogDiffs(repo *git.Repository, pageData *PageData, logs []
120 	}
121 }
122 
123-func (c *Config) getSummaryURL() template.URL {
124+func getSummaryURL() template.URL {
125 	url := "/index.html"
126 	return template.URL(url)
127 }
128 
129-func (c *Config) getRefsURL() template.URL {
130+func getRefsURL() template.URL {
131 	url := "/refs.html"
132 	return template.URL(url)
133 }
134 
135-func (c *Config) getTreeURL(revn string) template.URL {
136-	url := fmt.Sprintf("/tree/%s/index.html", revn)
137+func getRevIDForURL(info RevInfo) string {
138+	return info.Name()
139+}
140+
141+func getTreeBaseDir(info RevInfo) string {
142+	subdir := getRevIDForURL(info)
143+	return filepath.Join("/", "tree", subdir)
144+}
145+
146+func getLogBaseDir(info RevInfo) string {
147+	subdir := getRevIDForURL(info)
148+	return filepath.Join("/", "logs", subdir)
149+}
150+
151+func getFileURL(info RevInfo, fname string) string {
152+	return filepath.Join(getTreeBaseDir(info), "item", fname)
153+}
154+
155+func getTreeURL(info RevInfo) template.URL {
156+	dir := getTreeBaseDir(info)
157+	url := filepath.Join(dir, "index.html")
158 	return template.URL(url)
159 }
160 
161-func (c *Config) getLogsURL(revn string) template.URL {
162-	url := fmt.Sprintf("/logs/%s/index.html", revn)
163+func getLogsURL(info RevInfo) template.URL {
164+	dir := getLogBaseDir(info)
165+	url := filepath.Join(dir, "index.html")
166 	return template.URL(url)
167 }
168 
169-func (c *Config) getCommitURL(commitID string) template.URL {
170+func getCommitURL(commitID string) template.URL {
171 	url := fmt.Sprintf("/commits/%s.html", commitID)
172 	return template.URL(url)
173 }
174@@ -534,8 +572,8 @@ func (c *Config) getURLs() *SiteURLs {
175 	return &SiteURLs{
176 		HomeURL:    c.HomeURL,
177 		CloneURL:   c.CloneURL,
178-		RefsURL:    c.getRefsURL(),
179-		SummaryURL: c.getSummaryURL(),
180+		RefsURL:    getRefsURL(),
181+		SummaryURL: getSummaryURL(),
182 	}
183 }
184 
185@@ -567,11 +605,10 @@ func (c *Config) writeRepo() *BranchOutput {
186 		}
187 
188 		data := &RevData{
189-			ID:      fullRevID,
190-			RevName: revName,
191-			TreeURL: c.getTreeURL(revID),
192-			LogURL:  c.getLogsURL(revID),
193+			id:   fullRevID,
194+			name: revName,
195 		}
196+
197 		if first == nil {
198 			first = data
199 		}
200@@ -586,10 +623,10 @@ func (c *Config) writeRepo() *BranchOutput {
201 	mainOutput := &BranchOutput{}
202 	claimed := false
203 	for _, revData := range revs {
204-		refInfoMap[revData.RevName] = &RefInfo{
205-			ID:      revData.ID,
206-			Refspec: revData.RevName,
207-			URL:     revData.TreeURL,
208+		refInfoMap[revData.Name()] = &RefInfo{
209+			ID:      revData.ID(),
210+			Refspec: revData.Name(),
211+			URL:     revData.TreeURL(),
212 		}
213 	}
214 
215@@ -640,9 +677,8 @@ func (c *Config) writeRepo() *BranchOutput {
216 	// use the first revision in our list to generate
217 	// the root summary, logs, and tree the user can click
218 	revData := &RevData{
219-		TreeURL: c.getTreeURL(getShortID(first.ID)),
220-		LogURL:  c.getLogsURL(getShortID(first.ID)),
221-		RevName: first.RevName,
222+		id:   first.ID(),
223+		name: first.Name(),
224 	}
225 
226 	data := &PageData{
227@@ -659,7 +695,7 @@ func (c *Config) writeRevision(repo *git.Repository, pageData *PageData, refs []
228 	c.Logger.Infof(
229 		"compiling (%s) revision (%s)",
230 		c.RepoName,
231-		pageData.RevData.RevName,
232+		pageData.RevData.Name(),
233 	)
234 
235 	output := &BranchOutput{}
236@@ -668,7 +704,7 @@ func (c *Config) writeRevision(repo *git.Repository, pageData *PageData, refs []
237 		pageSize = 5000
238 	}
239 
240-	commits, err := repo.CommitsByPage(pageData.RevData.ID, 0, pageSize)
241+	commits, err := repo.CommitsByPage(pageData.RevData.ID(), 0, pageSize)
242 	bail(err)
243 
244 	logs := []*CommitData{}
245@@ -685,21 +721,21 @@ func (c *Config) writeRevision(repo *git.Repository, pageData *PageData, refs []
246 		}
247 
248 		logs = append(logs, &CommitData{
249-			URL:        c.getCommitURL(commit.ID.String()),
250+			URL:        getCommitURL(commit.ID.String()),
251 			ShortID:    getShortID(commit.ID.String()),
252 			SummaryStr: commit.Summary(),
253 			AuthorStr:  commit.Author.Name,
254-			WhenStr:    commit.Author.When.Format(time.RFC822),
255+			WhenStr:    commit.Author.When.Format("02 Jan 06"),
256 			Commit:     commit,
257 			Refs:       tags,
258 		})
259 	}
260 
261-	tree, err := repo.LsTree(pageData.RevData.ID)
262+	tree, err := repo.LsTree(pageData.RevData.ID())
263 	bail(err)
264 
265 	entries := []*TreeItem{}
266-	treeEntries := walkTree(tree, getShortID(pageData.RevData.ID), "", entries)
267+	treeEntries := walkTree(tree, pageData.RevData, "", entries)
268 	for _, entry := range treeEntries {
269 		entry.Path = strings.TrimPrefix(entry.Path, "/")
270 
271@@ -708,7 +744,7 @@ func (c *Config) writeRevision(repo *git.Repository, pageData *PageData, refs []
272 		if pageData.Repo.HideTreeLastCommit {
273 			c.Logger.Info("skipping finding last commit for each file")
274 		} else {
275-			lastCommits, err = repo.RevList([]string{pageData.RevData.ID}, git.RevListOptions{
276+			lastCommits, err = repo.RevList([]string{pageData.RevData.ID()}, git.RevListOptions{
277 				Path:           entry.Path,
278 				CommandOptions: git.CommandOptions{Args: []string{"-1"}},
279 			})
280@@ -718,14 +754,12 @@ func (c *Config) writeRevision(repo *git.Repository, pageData *PageData, refs []
281 			if len(lastCommits) > 0 {
282 				lc = lastCommits[0]
283 			}
284-			entry.CommitURL = c.getCommitURL(lc.ID.String())
285+			entry.CommitURL = getCommitURL(lc.ID.String())
286 			entry.Summary = lc.Summary()
287-			entry.When = lc.Author.When.Format(time.RFC822)
288+			entry.When = lc.Author.When.Format("02 Jan 06")
289 		}
290-		fpath := filepath.Join(
291-			"/tree",
292-			getShortID(pageData.RevData.ID),
293-			"item",
294+		fpath := getFileURL(
295+			pageData.RevData,
296 			fmt.Sprintf("%s.html", entry.Path),
297 		)
298 		entry.URL = template.URL(fpath)
299@@ -734,7 +768,7 @@ func (c *Config) writeRevision(repo *git.Repository, pageData *PageData, refs []
300 	c.Logger.Infof(
301 		"compilation complete (%s) branch (%s)",
302 		c.RepoName,
303-		pageData.RevData.RevName,
304+		pageData.RevData.Name(),
305 	)
306 
307 	c.writeLog(pageData, logs)