Commit dc0e5c7
Eric Bower
·
2026-05-16 20:26:44 -0400 EDT
parent ae904db
chore: support multiple parents on commit page
2 files changed,
+52,
-27
+9,
-1
1@@ -15,7 +15,15 @@
2 </div>
3
4 <div>
5- parent <a href="{{.ParentURL}}">{{.Parent}}</a>
6+ {{- $num := len .Parents -}}
7+ {{- if eq $num 1 -}}
8+ parent <a href="{{index .ParentURLs 0}}">{{index .Parents 0}}</a>
9+ {{- else -}}
10+ parents
11+ {{- range $i, $p := .Parents}}
12+ <a href="{{index $.ParentURLs $i}}">{{.}}</a>{{if lt (add $i 1) $num}}, {{end}}
13+ {{- end}}
14+ {{- end}}
15 </div>
16 </div>
17
M
main.go
+43,
-26
1@@ -33,6 +33,10 @@ var embedFS embed.FS
2 //go:embed static/*
3 var staticFS embed.FS
4
5+var funcMap = template.FuncMap{
6+ "add": func(a, b int) int { return a + b },
7+}
8+
9 type Config struct {
10 // required params
11 Outdir string
12@@ -114,7 +118,7 @@ type CommitData struct {
13 WhenStr string
14 AuthorStr string
15 ShortID string
16- ParentID string
17+ ParentIDs []string
18 Refs []*RefInfo
19 *git.Commit
20 }
21@@ -206,13 +210,13 @@ type FilePageData struct {
22
23 type CommitPageData struct {
24 *PageData
25- CommitMsg template.HTML
26- CommitID string
27- Commit *CommitData
28- Diff *DiffRender
29- Parent string
30- ParentURL template.URL
31- CommitURL template.URL
32+ CommitMsg template.HTML
33+ CommitID string
34+ Commit *CommitData
35+ Diff *DiffRender
36+ Parents []string
37+ ParentURLs []template.URL
38+ CommitURL template.URL
39 }
40
41 type RefPageData struct {
42@@ -375,7 +379,7 @@ func readmeFile(repo *Config) string {
43 }
44
45 func (c *Config) writeHtml(writeData *WriteData) {
46- ts, err := template.ParseFS(
47+ ts, err := template.New("").Funcs(funcMap).ParseFS(
48 embedFS,
49 writeData.Template,
50 "html/header.partial.tmpl",
51@@ -394,13 +398,14 @@ func (c *Config) writeHtml(writeData *WriteData) {
52 w, err := os.OpenFile(fp, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
53 bail(err)
54
55- err = ts.Execute(w, writeData.Data)
56+ tplName := filepath.Base(writeData.Template)
57+ err = ts.ExecuteTemplate(w, tplName, writeData.Data)
58 bail(err)
59 }
60
61 // writeHtmlIfChanged renders a template and only writes if content hash differs from existing file
62 func (c *Config) writeHtmlIfChanged(writeData *WriteData) {
63- ts, err := template.ParseFS(
64+ ts, err := template.New("").Funcs(funcMap).ParseFS(
65 embedFS,
66 writeData.Template,
67 "html/header.partial.tmpl",
68@@ -411,7 +416,8 @@ func (c *Config) writeHtmlIfChanged(writeData *WriteData) {
69
70 // render to buffer first
71 var buf bytes.Buffer
72- err = ts.Execute(&buf, writeData.Data)
73+ tplName := filepath.Base(writeData.Template)
74+ err = ts.ExecuteTemplate(&buf, tplName, writeData.Data)
75 bail(err)
76
77 dir := filepath.Join(c.Outdir, writeData.Subdir)
78@@ -613,14 +619,21 @@ func (c *Config) writeLogDiff(repo *git.Repository, pageData PageData, commit *C
79 }
80 rnd.Files = fls
81
82+ shortParents := make([]string, len(commit.ParentIDs))
83+ parentURLs := make([]template.URL, len(commit.ParentIDs))
84+ for i, pid := range commit.ParentIDs {
85+ shortParents[i] = getShortID(pid)
86+ parentURLs[i] = c.getCommitURL(pid)
87+ }
88+
89 commitData := &CommitPageData{
90- PageData: &pageData,
91- Commit: commit,
92- CommitID: getShortID(commitID),
93- Diff: rnd,
94- Parent: getShortID(commit.ParentID),
95- CommitURL: c.getCommitURL(commitID),
96- ParentURL: c.getCommitURL(commit.ParentID),
97+ PageData: &pageData,
98+ Commit: commit,
99+ CommitID: getShortID(commitID),
100+ Diff: rnd,
101+ Parents: shortParents,
102+ CommitURL: c.getCommitURL(commitID),
103+ ParentURLs: parentURLs,
104 }
105
106 c.writeHtml(&WriteData{
107@@ -1084,15 +1097,19 @@ func (c *Config) writeRevision(repo *git.Repository, pageData *PageData, refs []
108 }
109 }
110
111- parentSha, _ := commit.ParentID(0)
112- parentID := ""
113- if parentSha == nil {
114- parentID = commit.ID.String()
115- } else {
116- parentID = parentSha.String()
117+ parentIDs := []string{}
118+ for i := 0; ; i++ {
119+ sha, _ := commit.ParentID(i)
120+ if sha == nil {
121+ if len(parentIDs) == 0 {
122+ parentIDs = append(parentIDs, commit.ID.String())
123+ }
124+ break
125+ }
126+ parentIDs = append(parentIDs, sha.String())
127 }
128 logs = append(logs, &CommitData{
129- ParentID: parentID,
130+ ParentIDs: parentIDs,
131 URL: c.getCommitURL(commit.ID.String()),
132 ShortID: getShortID(commit.ID.String()),
133 SummaryStr: commit.Summary(),