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
| ... | ... | @@ -15,7 +15,15 @@ | |
| 15 | 15 | </div> | |
| 16 | 16 | ||
| 17 | 17 | <div> | |
| 18 | - | parent <a href="{{.ParentURL}}">{{.Parent}}</a> | |
| 18 | + | {{- $num := len .Parents -}} | |
| 19 | + | {{- if eq $num 1 -}} | |
| 20 | + | parent <a href="{{index .ParentURLs 0}}">{{index .Parents 0}}</a> | |
| 21 | + | {{- else -}} | |
| 22 | + | parents | |
| 23 | + | {{- range $i, $p := .Parents}} | |
| 24 | + | <a href="{{index $.ParentURLs $i}}">{{.}}</a>{{if lt (add $i 1) $num}}, {{end}} | |
| 25 | + | {{- end}} | |
| 26 | + | {{- end}} | |
| 19 | 27 | </div> | |
| 20 | 28 | </div> | |
| 21 | 29 |
M
main.go
+43,
-26
| ... | ... | @@ -33,6 +33,10 @@ var embedFS embed.FS | |
| 33 | 33 | //go:embed static/* | |
| 34 | 34 | var staticFS embed.FS | |
| 35 | 35 | ||
| 36 | + | var funcMap = template.FuncMap{ | |
| 37 | + | "add": func(a, b int) int { return a + b }, | |
| 38 | + | } | |
| 39 | + | ||
| 36 | 40 | type Config struct { | |
| 37 | 41 | // required params | |
| 38 | 42 | Outdir string |
| ... | ... | @@ -114,7 +118,7 @@ type CommitData struct { | |
| 114 | 118 | WhenStr string | |
| 115 | 119 | AuthorStr string | |
| 116 | 120 | ShortID string | |
| 117 | - | ParentID string | |
| 121 | + | ParentIDs []string | |
| 118 | 122 | Refs []*RefInfo | |
| 119 | 123 | *git.Commit | |
| 120 | 124 | } |
| ... | ... | @@ -206,13 +210,13 @@ type FilePageData struct { | |
| 206 | 210 | ||
| 207 | 211 | type CommitPageData struct { | |
| 208 | 212 | *PageData | |
| 209 | - | CommitMsg template.HTML | |
| 210 | - | CommitID string | |
| 211 | - | Commit *CommitData | |
| 212 | - | Diff *DiffRender | |
| 213 | - | Parent string | |
| 214 | - | ParentURL template.URL | |
| 215 | - | CommitURL template.URL | |
| 213 | + | CommitMsg template.HTML | |
| 214 | + | CommitID string | |
| 215 | + | Commit *CommitData | |
| 216 | + | Diff *DiffRender | |
| 217 | + | Parents []string | |
| 218 | + | ParentURLs []template.URL | |
| 219 | + | CommitURL template.URL | |
| 216 | 220 | } | |
| 217 | 221 | ||
| 218 | 222 | type RefPageData struct { |
| ... | ... | @@ -375,7 +379,7 @@ func readmeFile(repo *Config) string { | |
| 375 | 379 | } | |
| 376 | 380 | ||
| 377 | 381 | func (c *Config) writeHtml(writeData *WriteData) { | |
| 378 | - | ts, err := template.ParseFS( | |
| 382 | + | ts, err := template.New("").Funcs(funcMap).ParseFS( | |
| 379 | 383 | embedFS, | |
| 380 | 384 | writeData.Template, | |
| 381 | 385 | "html/header.partial.tmpl", |
| ... | ... | @@ -394,13 +398,14 @@ func (c *Config) writeHtml(writeData *WriteData) { | |
| 394 | 398 | w, err := os.OpenFile(fp, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) | |
| 395 | 399 | bail(err) | |
| 396 | 400 | ||
| 397 | - | err = ts.Execute(w, writeData.Data) | |
| 401 | + | tplName := filepath.Base(writeData.Template) | |
| 402 | + | err = ts.ExecuteTemplate(w, tplName, writeData.Data) | |
| 398 | 403 | bail(err) | |
| 399 | 404 | } | |
| 400 | 405 | ||
| 401 | 406 | // writeHtmlIfChanged renders a template and only writes if content hash differs from existing file | |
| 402 | 407 | func (c *Config) writeHtmlIfChanged(writeData *WriteData) { | |
| 403 | - | ts, err := template.ParseFS( | |
| 408 | + | ts, err := template.New("").Funcs(funcMap).ParseFS( | |
| 404 | 409 | embedFS, | |
| 405 | 410 | writeData.Template, | |
| 406 | 411 | "html/header.partial.tmpl", |
| ... | ... | @@ -411,7 +416,8 @@ func (c *Config) writeHtmlIfChanged(writeData *WriteData) { | |
| 411 | 416 | ||
| 412 | 417 | // render to buffer first | |
| 413 | 418 | var buf bytes.Buffer | |
| 414 | - | err = ts.Execute(&buf, writeData.Data) | |
| 419 | + | tplName := filepath.Base(writeData.Template) | |
| 420 | + | err = ts.ExecuteTemplate(&buf, tplName, writeData.Data) | |
| 415 | 421 | bail(err) | |
| 416 | 422 | ||
| 417 | 423 | dir := filepath.Join(c.Outdir, writeData.Subdir) |
| ... | ... | @@ -613,14 +619,21 @@ func (c *Config) writeLogDiff(repo *git.Repository, pageData PageData, commit *C | |
| 613 | 619 | } | |
| 614 | 620 | rnd.Files = fls | |
| 615 | 621 | ||
| 622 | + | shortParents := make([]string, len(commit.ParentIDs)) | |
| 623 | + | parentURLs := make([]template.URL, len(commit.ParentIDs)) | |
| 624 | + | for i, pid := range commit.ParentIDs { | |
| 625 | + | shortParents[i] = getShortID(pid) | |
| 626 | + | parentURLs[i] = c.getCommitURL(pid) | |
| 627 | + | } | |
| 628 | + | ||
| 616 | 629 | commitData := &CommitPageData{ | |
| 617 | - | PageData: &pageData, | |
| 618 | - | Commit: commit, | |
| 619 | - | CommitID: getShortID(commitID), | |
| 620 | - | Diff: rnd, | |
| 621 | - | Parent: getShortID(commit.ParentID), | |
| 622 | - | CommitURL: c.getCommitURL(commitID), | |
| 623 | - | ParentURL: c.getCommitURL(commit.ParentID), | |
| 630 | + | PageData: &pageData, | |
| 631 | + | Commit: commit, | |
| 632 | + | CommitID: getShortID(commitID), | |
| 633 | + | Diff: rnd, | |
| 634 | + | Parents: shortParents, | |
| 635 | + | CommitURL: c.getCommitURL(commitID), | |
| 636 | + | ParentURLs: parentURLs, | |
| 624 | 637 | } | |
| 625 | 638 | ||
| 626 | 639 | c.writeHtml(&WriteData{ |
| ... | ... | @@ -1084,15 +1097,19 @@ func (c *Config) writeRevision(repo *git.Repository, pageData *PageData, refs [] | |
| 1084 | 1097 | } | |
| 1085 | 1098 | } | |
| 1086 | 1099 | ||
| 1087 | - | parentSha, _ := commit.ParentID(0) | |
| 1088 | - | parentID := "" | |
| 1089 | - | if parentSha == nil { | |
| 1090 | - | parentID = commit.ID.String() | |
| 1091 | - | } else { | |
| 1092 | - | parentID = parentSha.String() | |
| 1100 | + | parentIDs := []string{} | |
| 1101 | + | for i := 0; ; i++ { | |
| 1102 | + | sha, _ := commit.ParentID(i) | |
| 1103 | + | if sha == nil { | |
| 1104 | + | if len(parentIDs) == 0 { | |
| 1105 | + | parentIDs = append(parentIDs, commit.ID.String()) | |
| 1106 | + | } | |
| 1107 | + | break | |
| 1108 | + | } | |
| 1109 | + | parentIDs = append(parentIDs, sha.String()) | |
| 1093 | 1110 | } | |
| 1094 | 1111 | logs = append(logs, &CommitData{ | |
| 1095 | - | ParentID: parentID, | |
| 1112 | + | ParentIDs: parentIDs, | |
| 1096 | 1113 | URL: c.getCommitURL(commit.ID.String()), | |
| 1097 | 1114 | ShortID: getShortID(commit.ID.String()), | |
| 1098 | 1115 | SummaryStr: commit.Summary(), |