repos / pgit

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

commit
fe38cd8
parent
c0b0fe0
author
Eric Bower
date
2023-08-06 16:00:45 +0000 UTC
progress
6 files changed,  +105, -49
M config.yaml
+1, -0
1@@ -3,6 +3,7 @@ repos:
2   - path: /home/erock/dev/pico/pico
3     refs:
4       - main
5+      - v1.0.0
6     desc: pico services - prose.sh, lists.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
7   - path: /home/erock/dev/pico/pico-ops
8     refs:
M html/header.partial.tmpl
+3, -2
 1@@ -6,9 +6,10 @@
 2 
 3 <div class="text-lg">
 4   <a href="{{.Repo.SummaryURL}}">summary</a> |
 5+  <a href="{{.Repo.RefsURL}}">refs</a> |
 6+  <span class="font-bold">{{.Repo.RevName}}</span> |
 7   <a href="{{.Repo.TreeURL}}">tree</a> |
 8-  <a href="{{.Repo.LogURL}}">log</a> |
 9-  <a href="{{.Repo.RefsURL}}">refs</a>
10+  <a href="{{.Repo.LogURL}}">log</a>
11 </div>
12 
13 <div class="my">
M html/log.page.tmpl
+12, -3
 1@@ -5,9 +5,18 @@
 2 {{define "content"}}
 3   {{template "header" .}}
 4 
 5+  <div>
 6   {{range .Data.Log}}
 7-  <p>
 8-    {{.Author.When}} <a href="{{.URL}}">{{.Summary}}</a> {{.Author.Name}}
 9-  </p>
10+    <div class="box">
11+      <div>
12+        <a href="{{.URL}}">{{.SummaryStr}}</a>
13+      </div>
14+      <div>
15+        <span>{{.AuthorStr}}</span>
16+        <span>&nbsp;committed&nbsp;</span>
17+        <span>{{.WhenStr}}</span>
18+      </div>
19+    </div>
20   {{end}}
21+  </div>
22 {{end}}
M html/refs.page.tmpl
+8, -11
 1@@ -5,17 +5,14 @@
 2 {{define "content"}}
 3   {{template "header" .}}
 4 
 5-  <h2 class="text-md">branches</h2>
 6-  <ol>
 7-  {{range .Data.Branches}}
 8-    <li>{{.Refspec}}</li>
 9-  {{end}}
10-  </ol>
11-
12-  <h2 class="text-md">tags</h2>
13-  <ol>
14-  {{range .Data.Tags}}
15-    <li>{{.Refspec}}</li>
16+  <h2 class="text-md font-bold">refs</h2>
17+  <ul>
18+  {{range .Data.Refs}}
19+    {{if .URL}}
20+      <li><a href="{{.URL}}">{{.Refspec}}</a></li>
21+    {{else}}
22+      <li>{{.Refspec}}</li>
23+    {{end}}
24   {{end}}
25   </ol>
26 {{end}}
M html/tree.page.tmpl
+17, -19
 1@@ -5,24 +5,22 @@
 2 {{define "content"}}
 3   {{template "header" .}}
 4 
 5-  <table>
 6-    <tbody>
 7-    {{range .Data.Tree}}
 8-      <tr>
 9-        <td>
10-          <a href="{{.URL}}">{{.Path}}</a>
11-        </td>
12-        <td>
13-          <a href="{{.CommitURL}}">{{.Desc}}</a>
14-        </td>
15-        <td>
16-          {{.When}}
17-        </td>
18-        <td class="mono font-bold">
19+  <div>
20+  {{range .Data.Tree}}
21+    <div class="flex justify-between">
22+      <div class="flex-1">
23+        <a href="{{.URL}}">{{.Path}}</a>
24+      </div>
25+
26+      <div class="flex">
27+        <div>
28+          <a href="{{.CommitURL}}">{{.When}}</a>
29+        </div>
30+        <div class="mono font-bold">
31           L{{.NumLines}}
32-        </td>
33-      </tr>
34-    {{end}}
35-    </tbody>
36-  </table>
37+        </div>
38+      </div>
39+    </div>
40+  {{end}}
41+  </div>
42 {{end}}
M main.go
+64, -14
  1@@ -38,10 +38,14 @@ type RepoData struct {
  2 	RefsURL    string
  3 	CloneURL   string
  4 	MaxCommits int
  5+	RevName string
  6 }
  7 
  8 type CommitData struct {
  9+	SummaryStr string
 10 	URL string
 11+	WhenStr string
 12+	AuthorStr string
 13 	*git.Commit
 14 }
 15 
 16@@ -58,12 +62,11 @@ type TreeItem struct {
 17 type PageData struct {
 18 	Repo     *RepoData
 19 	Log      []*CommitData
 20-	Branches []*git.Reference
 21-	Tags     []*git.Reference
 22 	Tree     []*TreeItem
 23 	Readme   template.HTML
 24 	Rev      *git.Reference
 25 	RevName  string
 26+	Refs     []*RefInfo
 27 }
 28 
 29 type CommitPageData struct {
 30@@ -378,16 +381,21 @@ func (c *Config) writeLogDiffs(repo *git.Repository, pageData *PageData) {
 31 	}
 32 }
 33 
 34+type RefInfo struct {
 35+	Refspec string
 36+	URL     template.URL
 37+}
 38+
 39 func (c *Config) writeRepo(config *RepoConfig) *BranchOutput {
 40 	repo, err := git.Open(config.Path)
 41 	bail(err)
 42 
 43 	name := repoName(config.Path)
 44 
 45-	heads, err := repo.ShowRef(git.ShowRefOptions{Heads: true, Tags: false})
 46+	refs, err := repo.ShowRef(git.ShowRefOptions{Heads: true, Tags: true})
 47 	bail(err)
 48 
 49-	rev := findDefaultBranch(config, heads)
 50+	rev := findDefaultBranch(config, refs)
 51 	if rev == nil {
 52 		bail(fmt.Errorf("no default branch found"))
 53 	}
 54@@ -402,24 +410,39 @@ func (c *Config) writeRepo(config *RepoConfig) *BranchOutput {
 55 		LogURL:     fmt.Sprintf("/%s/logs/%s/index.html", name, revName),
 56 		RefsURL:    fmt.Sprintf("/%s/refs.html", name),
 57 		CloneURL:   fmt.Sprintf("https://%s/%s.git", c.URL, name),
 58+		RevName: revName,
 59 	}
 60 
 61-	tags, _ := repo.ShowRef(git.ShowRefOptions{Heads: false, Tags: true})
 62-
 63+	refInfoMap := map[string]*RefInfo{}
 64 	var mainOutput *BranchOutput
 65 	claimed := false
 66 	for _, revn := range config.Refs {
 67-		for _, head := range heads {
 68+		for _, head := range refs {
 69 			_, headName := filepath.Split(head.Refspec)
 70 			if revn != headName {
 71 				continue
 72 			}
 73+			refInfoMap[head.ID] = &RefInfo{
 74+				Refspec: strings.TrimPrefix(head.Refspec, "refs/"),
 75+				URL:     template.URL(fmt.Sprintf("/%s/tree/%s/index.html", name, revn)),
 76+			}
 77+
 78+			branchRepo := &RepoData{
 79+				Name:       name,
 80+				Desc:       config.Desc,
 81+				MaxCommits: config.MaxCommits,
 82+				SummaryURL: fmt.Sprintf("/%s/index.html", name),
 83+				TreeURL:    fmt.Sprintf("/%s/tree/%s/index.html", name, revn),
 84+				LogURL:     fmt.Sprintf("/%s/logs/%s/index.html", name, revn),
 85+				RefsURL:    fmt.Sprintf("/%s/refs.html", name),
 86+				CloneURL:   fmt.Sprintf("https://%s/%s.git", c.URL, name),
 87+				RevName: revn,
 88+			}
 89+
 90 			data := &PageData{
 91-				Branches: heads,
 92-				Tags:     tags,
 93-				Rev:      head,
 94-				RevName:  headName,
 95-				Repo:     repoData,
 96+				Rev:     head,
 97+				RevName: headName,
 98+				Repo:    branchRepo,
 99 			}
100 
101 			branchOutput := c.writeBranch(repo, data)
102@@ -430,13 +453,37 @@ func (c *Config) writeRepo(config *RepoConfig) *BranchOutput {
103 		}
104 	}
105 
106+	for _, ref := range refs {
107+		if refInfoMap[ref.ID] != nil {
108+			continue
109+		}
110+
111+		refInfoMap[ref.ID] = &RefInfo{
112+			Refspec: strings.TrimPrefix(ref.Refspec, "refs/"),
113+		}
114+	}
115+
116+	refInfoList := []*RefInfo{}
117+	for _, val := range refInfoMap {
118+		refInfoList = append(refInfoList, val)
119+	}
120+	sort.Slice(refInfoList, func(i, j int) bool {
121+		urlI := refInfoList[i].URL
122+		urlJ := refInfoList[j].URL
123+		refI := refInfoList[i].Refspec
124+		refJ := refInfoList[j].Refspec
125+		if urlI == urlJ {
126+			return refI < refJ
127+		}
128+		return urlI > urlJ
129+	})
130+
131 	data := &PageData{
132-		Branches: heads,
133-		Tags:     tags,
134 		Rev:      rev,
135 		RevName:  revName,
136 		Repo:     repoData,
137 		Readme:   template.HTML(mainOutput.Readme),
138+		Refs:     refInfoList,
139 	}
140 	writeRefs(data)
141 	writeRootSummary(data)
142@@ -466,6 +513,9 @@ func (c *Config) writeBranch(repo *git.Repository, pageData *PageData) *BranchOu
143 
144 		logs = append(logs, &CommitData{
145 			URL:    commitURL(pageData.Repo.Name, commit.ID.String()),
146+			SummaryStr: commit.Summary(),
147+			AuthorStr: commit.Author.Name,
148+			WhenStr: timediff.TimeDiff(commit.Author.When),
149 			Commit: commit,
150 		})
151 	}