gshr
git static host repo -- generates static html for repos
git clone https://git.vogt.world/gshr.git
Log | Files | README.md | LICENSE
← Commit log
commit
message
Rough diff rendering
author
Ben Vogt <[email protected]>
date
2023-04-04 20:13:12
stats
3 file(s) changed, 43 insertions(+), 3 deletions(-)
files
README.md
commit.go
templates/commit.html
  1diff --git a/README.md b/README.md
  2index d8ac977..8ecd3cf 100644
  3--- a/README.md
  4+++ b/README.md
  5@@ -7,7 +7,5 @@ Git static host repo.
  6 Tasks to do:
  7 
  8 * Render full commit diff.
  9-* Better use of BaseURL.
 10-* Show plain-text files (like Makefile) if possible.
 11 * README and LICENSE linking in main page.
 12 * Multi-repo namespacing.
 13\ No newline at end of file
 14diff --git a/commit.go b/commit.go
 15index 8bac22d..c9a822b 100644
 16--- a/commit.go
 17+++ b/commit.go
 18@@ -1,14 +1,22 @@
 19 package main
 20 
 21 import (
 22+	"bytes"
 23+	"errors"
 24 	"html/template"
 25 	"os"
 26 	"path"
 27 
 28+	"github.com/alecthomas/chroma/formatters/html"
 29+	"github.com/alecthomas/chroma/lexers"
 30+	"github.com/alecthomas/chroma/styles"
 31 	"github.com/go-git/go-git/v5"
 32 	"github.com/go-git/go-git/v5/plumbing/object"
 33 )
 34 
 35+type FileDiff struct {
 36+}
 37+
 38 type CommitPage struct {
 39 	RepoData        RepoData
 40 	Author          string
 41@@ -19,6 +27,7 @@ type CommitPage struct {
 42 	FileChangeCount int
 43 	LinesAdded      int
 44 	LinesDeleted    int
 45+	DiffContent     template.HTML
 46 }
 47 
 48 func (c *CommitPage) Render(t *template.Template) {
 49@@ -38,6 +47,37 @@ func RenderAllCommitPages(r *git.Repository) {
 50 	cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
 51 	checkErr(err)
 52 	err = cIter.ForEach(func(c *object.Commit) error {
 53+		parent, err := c.Parent(0)
 54+		if err != nil && errors.Is(err, object.ErrParentNotFound) {
 55+			// ok
 56+		} else if err != nil {
 57+			checkErr(err)
 58+		}
 59+		diffContent := template.HTML("")
 60+		if parent != nil {
 61+			lexer := lexers.Match("x.diff")
 62+			if lexer == nil {
 63+				lexer = lexers.Fallback
 64+			}
 65+			style := styles.Get("borland")
 66+			if style == nil {
 67+				style = styles.Fallback
 68+			}
 69+			patch, err := c.Patch(parent)
 70+			checkErr(err)
 71+			patchString := patch.String()
 72+			iterator, err := lexer.Tokenise(nil, patchString)
 73+			formatter := html.New(
 74+				html.WithClasses(true),
 75+				html.WithLineNumbers(true),
 76+				html.LinkableLineNumbers(true, ""),
 77+			)
 78+			s := ""
 79+			buf := bytes.NewBufferString(s)
 80+			err = formatter.Format(buf, style, iterator)
 81+			checkErr(err)
 82+			diffContent = template.HTML(buf.String())
 83+		}
 84 		stats, err := c.Stats()
 85 		added := 0
 86 		deleted := 0
 87@@ -57,6 +97,7 @@ func RenderAllCommitPages(r *git.Repository) {
 88 			FileChangeCount: len(stats),
 89 			LinesAdded:      added,
 90 			LinesDeleted:    deleted,
 91+			DiffContent:     diffContent,
 92 		}).Render(t)
 93 		return nil
 94 	})
 95diff --git a/templates/commit.html b/templates/commit.html
 96index 215874d..8e7d723 100644
 97--- a/templates/commit.html
 98+++ b/templates/commit.html
 99@@ -44,7 +44,7 @@
100           </tr>
101         </tbody>
102       </table>
103-      <hr />
104+      {{ .DiffContent }}
105     </div>
106   </div>
107 </body>