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
Commit data rendering for main index
author
Ben Vogt <[email protected]>
date
2023-04-03 16:00:11
stats
2 file(s) changed, 50 insertions(+), 16 deletions(-)
files
index.template.html
main.go
  1diff --git a/index.template.html b/index.template.html
  2index 723ab9f..b6bdaf2 100644
  3--- a/index.template.html
  4+++ b/index.template.html
  5@@ -31,7 +31,17 @@
  6           </tr>
  7         </thead>
  8         <tbody>
  9+          {{ range .Commits }}
 10           <tr>
 11+            <td>{{ .Date }}</td>
 12+            <td><a href="commit/{{ .Hash }}">{{ .Message }}</a></td>
 13+            <td>{{ .Author }}</td>
 14+            <td align="right">1</td>
 15+            <td align="right">+2</td>
 16+            <td align="right">-0</td>
 17+          </tr>
 18+          {{ end }}
 19+          <!-- <tr>
 20             <td>2020-03-04 11:03</td>
 21             <td><a href="commit/commit_hash.html">update TODO</a></td>
 22             <td>benjvogt</td>
 23@@ -54,7 +64,7 @@
 24             <td align="right">1</td>
 25             <td align="right">+2</td>
 26             <td align="right">-3</td>
 27-          </tr>
 28+          </tr> -->
 29         </tbody>
 30       </table>
 31     </div>
 32diff --git a/main.go b/main.go
 33index 98e4399..06ba68a 100644
 34--- a/main.go
 35+++ b/main.go
 36@@ -46,42 +46,67 @@ func (tf *TrackedFile) SaveTemplate(t *template.Template) {
 37 	checkErr(err)
 38 }
 39 
 40+type GshrCommit struct {
 41+	Author  string
 42+	Date    string
 43+	Hash    string
 44+	Message string
 45+}
 46+
 47+type MainIndex struct {
 48+	Commits []GshrCommit
 49+}
 50+
 51+func (mi *MainIndex) SaveTemplate(t *template.Template) {
 52+	output, err := os.Create(path.Join(outputDir, "index.html"))
 53+	checkErr(err)
 54+	err = t.Execute(output, mi)
 55+	checkErr(err)
 56+}
 57+
 58 func main() {
 59 	outputDir = os.Getenv("OUTPUT_DIR")
 60 	cloningDir = os.Getenv("CLONING_DIR")
 61 	line("OUTPUT_DIR = %v", outputDir)
 62 	line("CLONING_DIR = %v", cloningDir)
 63-	CloneAndInfo()
 64+	r := CloneAndInfo()
 65+	BuildMainIndex(r)
 66 	BuildTrackedFiles()
 67-	BuildMainIndex()
 68 }
 69 
 70-func CloneAndInfo() {
 71+func CloneAndInfo() *git.Repository {
 72 	r, err := git.PlainClone(cloningDir, false, &git.CloneOptions{
 73 		URL: "/Users/bvogt/dev/src/ben/www",
 74 	})
 75 	checkErr(err)
 76+	return r
 77+}
 78 
 79+func BuildMainIndex(r *git.Repository) {
 80+	t, err := template.ParseFiles("index.template.html")
 81+	commits := make([]GshrCommit, 0)
 82 	ref, err := r.Head()
 83 	checkErr(err)
 84-
 85 	cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
 86 	checkErr(err)
 87 
 88 	err = cIter.ForEach(func(c *object.Commit) error {
 89 		fmt.Println(c)
 90+		commits = append(commits, GshrCommit{
 91+			Author:  c.Author.Email,
 92+			Message: c.Message,
 93+			Date:    c.Author.When.UTC().Format("2006-01-02 15:04:05"),
 94+			Hash:    c.Hash.String(),
 95+		})
 96 		return nil
 97 	})
 98-	checkErr(err)
 99-
100-}
101 
102-func BuildMainIndex() {
103-	t, err := template.ParseFiles("index.template.html")
104-	checkErr(err)
105-	output, err := os.Create(path.Join(outputDir, "index.html"))
106-	err = t.Execute(output, "")
107 	checkErr(err)
108+	m := MainIndex{
109+		Commits: commits,
110+	}
111+	m.SaveTemplate(t)
112+
113 }
114 
115 func BuildTrackedFiles() {