gshr
git static host repo -- generates static html for repos
git clone https://git.vogt.world/gshr.git
Log | Files | README.md | LICENSE
← All files
name: log.go
-rw-r--r--
1631
 1package main
 2
 3import (
 4	"html/template"
 5	"os"
 6	"path"
 7
 8	"github.com/go-git/go-git/v5"
 9	"github.com/go-git/go-git/v5/plumbing/object"
10)
11
12type LogPageCommit struct {
13	Author          string
14	Date            string
15	Hash            string
16	Message         string
17	FileChangeCount int
18	LinesAdded      int
19	LinesDeleted    int
20}
21
22type LogPage struct {
23	RepoData   repoData
24	HasReadMe  bool
25	ReadMePath string
26	Commits    []LogPageCommit
27}
28
29func (l *LogPage) renderPage(t *template.Template) {
30	debug("log page for '%v'", l.RepoData.Name)
31	output, err := os.Create(path.Join(args.OutputDir, l.RepoData.Name, "log.html"))
32	checkErr(err)
33	err = t.Execute(output, l)
34	checkErr(err)
35}
36
37func renderLogPage(data repoData, r *git.Repository) {
38	t, err := template.ParseFS(htmlTemplates, "template.log.html", "template.partials.html")
39	checkErr(err)
40	commits := make([]LogPageCommit, 0)
41	ref, err := r.Head()
42	checkErr(err)
43	cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
44	checkErr(err)
45	err = cIter.ForEach(func(c *object.Commit) error {
46		stats, err := c.Stats()
47		added := 0
48		deleted := 0
49		for i := 0; i < len(stats); i++ {
50			stat := stats[i]
51			added += stat.Addition
52			deleted += stat.Deletion
53		}
54		checkErr(err)
55		commits = append(commits, LogPageCommit{
56			Author:          c.Author.Name,
57			Message:         c.Message,
58			Date:            c.Author.When.UTC().Format("2006-01-02 15:04:05"),
59			Hash:            c.Hash.String(),
60			FileChangeCount: len(stats),
61			LinesAdded:      added,
62			LinesDeleted:    deleted,
63		})
64		return nil
65	})
66	checkErr(err)
67	(&LogPage{
68		RepoData: data,
69		Commits:  commits,
70	}).renderPage(t)
71}