Commit 56966ad

Nick Faro committed on
Keep commit counts fresh on static pages
commit 56966ada2aa6ef257ff6efee580f6a018169186f parent 42337f7
4 changed files +28−8
Modifiedinternal/site/navigation.go +14−1
@@ -5,6 +5,7 @@ import (
5 "encoding/json" 5 "encoding/json"
6 "path" 6 "path"
7 "sort" 7 "sort"
8 "strconv"
8 ) 9 )
9 10
10 type repositoryNavigation struct { 11 type repositoryNavigation struct {
@@ -21,6 +22,15 @@ type fileNavigationEntry struct {
21 URL string `json:"url"` 22 URL string `json:"url"`
22 } 23 }
23 24
25 func branchStylePage(branch string, commitCount int) Page {
26 return Page{
27 Route: path.Join(".dumbforge/branches", branchRouteSegment(branch)+".css"),
28 Body: []byte(`[data-commit-count]::before { content: "` + strconv.Itoa(commitCount) + `"; }` + "\n"),
29 ContentType: "text/css; charset=utf-8",
30 Cache: siteCache,
31 }
32 }
33
24 func (g *Generator) SetBranches(branches []string, defaultBranch string) { 34 func (g *Generator) SetBranches(branches []string, defaultBranch string) {
25 seen := map[string]struct{}{} 35 seen := map[string]struct{}{}
26 g.Branches = g.Branches[:0] 36 g.Branches = g.Branches[:0]
@@ -102,7 +112,10 @@ func (g *Generator) RepositoryNavigationBuild(deletedBranches []string) (Build,
102 Cache: siteCache, 112 Cache: siteCache,
103 }}} 113 }}}
104 for _, branch := range deletedBranches { 114 for _, branch := range deletedBranches {
105 build.Deletes = append(build.Deletes, path.Join(".dumbforge/files", branchRouteSegment(branch)+".json")) 115 build.Deletes = append(build.Deletes,
116 path.Join(".dumbforge/files", branchRouteSegment(branch)+".json"),
117 path.Join(".dumbforge/branches", branchRouteSegment(branch)+".css"),
118 )
106 } 119 }
107 return build, nil 120 return build, nil
108 } 121 }
Modifiedinternal/site/site.go +3−1
@@ -117,6 +117,7 @@ type pageData struct {
117 CommitGroups []CommitGroup 117 CommitGroups []CommitGroup
118 ViewedCommit CommitView 118 ViewedCommit CommitView
119 BranchesURL string 119 BranchesURL string
120 BranchStyleURL string
120 FilesURL string 121 FilesURL string
121 DefaultBranch string 122 DefaultBranch string
122 GitHubImportURL string 123 GitHubImportURL string
@@ -209,7 +210,7 @@ func (g *Generator) BuildBranch(ctx context.Context, branch, oldOID, newOID stri
209 if err != nil { 210 if err != nil {
210 return Build{}, err 211 return Build{}, err
211 } 212 }
212 result.Pages = append(result.Pages, commitsPage) 213 result.Pages = append(result.Pages, commitsPage, branchStylePage(branch, summary.Count))
213 214
214 if oldOID == "" { 215 if oldOID == "" {
215 filesPage, err := g.filesPage(ctx, branch, newOID) 216 filesPage, err := g.filesPage(ctx, branch, newOID)
@@ -709,6 +710,7 @@ func (g *Generator) pageData(kind, title, branch string, summary branchSummary)
709 CommitsURL: summary.CommitsURL, 710 CommitsURL: summary.CommitsURL,
710 CommitGroups: summary.Groups, 711 CommitGroups: summary.Groups,
711 BranchesURL: routeURL(g.BaseURL, ".dumbforge/repository.json"), 712 BranchesURL: routeURL(g.BaseURL, ".dumbforge/repository.json"),
713 BranchStyleURL: routeURL(g.BaseURL, path.Join(".dumbforge/branches", branchRouteSegment(branch)+".css")),
712 FilesURL: routeURL(g.BaseURL, path.Join(".dumbforge/files", branchRouteSegment(branch)+".json")), 714 FilesURL: routeURL(g.BaseURL, path.Join(".dumbforge/files", branchRouteSegment(branch)+".json")),
713 DefaultBranch: g.DefaultBranch, 715 DefaultBranch: g.DefaultBranch,
714 GitHubImportURL: "https://github.com/new/import", 716 GitHubImportURL: "https://github.com/new/import",
Modifiedinternal/site/site_test.go +7−3
@@ -50,6 +50,9 @@ func TestFullAndIncrementalBuild(t *testing.T) {
50 assertPage(t, full, "", `absolute >= 604800`) 50 assertPage(t, full, "", `absolute >= 604800`)
51 assertPage(t, full, "", ".dumbforge/assets/gabriel-mark.svg") 51 assertPage(t, full, "", ".dumbforge/assets/gabriel-mark.svg")
52 assertPage(t, full, "", `.dumbforge/assets/site.css?v=`+generator.AssetVersion()) 52 assertPage(t, full, "", `.dumbforge/assets/site.css?v=`+generator.AssetVersion())
53 assertPage(t, full, "", `.dumbforge/branches/main.css`)
54 assertPage(t, full, ".dumbforge/branches/main.css", `content: "1"`)
55 assertPage(t, full, "", `class="counter" data-commit-count`)
53 assertPage(t, full, ".dumbforge/assets/site.css", "@media (prefers-color-scheme: light)") 56 assertPage(t, full, ".dumbforge/assets/site.css", "@media (prefers-color-scheme: light)")
54 assertPage(t, full, ".dumbforge/assets/site.css", ".commit-list-message { overflow: hidden; color: var(--text);") 57 assertPage(t, full, ".dumbforge/assets/site.css", ".commit-list-message { overflow: hidden; color: var(--text);")
55 assertPage(t, full, "", `<span class="header-owner">owner</span><span>/</span>`) 58 assertPage(t, full, "", `<span class="header-owner">owner</span><span>/</span>`)
@@ -106,8 +109,8 @@ func TestFullAndIncrementalBuild(t *testing.T) {
106 if err != nil { 109 if err != nil {
107 t.Fatal(err) 110 t.Fatal(err)
108 } 111 }
109 if len(incremental.Pages) != 6 { 112 if len(incremental.Pages) != 7 {
110 t.Fatalf("incremental build generated %d pages, want 6", len(incremental.Pages)) 113 t.Fatalf("incremental build generated %d pages, want 7", len(incremental.Pages))
111 } 114 }
112 for _, page := range incremental.Pages { 115 for _, page := range incremental.Pages {
113 if strings.HasPrefix(page.Route, ".dumbforge/assets/") || page.Route == ".dumbforge/site-version" { 116 if strings.HasPrefix(page.Route, ".dumbforge/assets/") || page.Route == ".dumbforge/site-version" {
@@ -119,6 +122,7 @@ func TestFullAndIncrementalBuild(t *testing.T) {
119 assertPage(t, incremental, "tree/main/src", `commit/`+second+`" title="change one file">change one file</a>`) 122 assertPage(t, incremental, "tree/main/src", `commit/`+second+`" title="change one file">change one file</a>`)
120 assertPage(t, incremental, "", `commit/`+first+`" title="initial">initial</a>`) 123 assertPage(t, incremental, "", `commit/`+first+`" title="initial">initial</a>`)
121 assertPage(t, incremental, "commits/main", "change one file") 124 assertPage(t, incremental, "commits/main", "change one file")
125 assertPage(t, incremental, ".dumbforge/branches/main.css", `content: "2"`)
122 assertPage(t, incremental, "commit/"+second, `class="diff-code old deletion`) 126 assertPage(t, incremental, "commit/"+second, `class="diff-code old deletion`)
123 assertPage(t, incremental, "commit/"+second, `class="diff-kd"`) 127 assertPage(t, incremental, "commit/"+second, `class="diff-kd"`)
124 assertPage(t, incremental, "commit/"+second, `changed`) 128 assertPage(t, incremental, "commit/"+second, `changed`)
@@ -223,7 +227,7 @@ func TestRepositoryNavigationBuild(t *testing.T) {
223 } 227 }
224 assertPage(t, build, ".dumbforge/repository.json", `"defaultBranch":"main"`) 228 assertPage(t, build, ".dumbforge/repository.json", `"defaultBranch":"main"`)
225 assertPage(t, build, ".dumbforge/repository.json", `"branches":["main","topic"]`) 229 assertPage(t, build, ".dumbforge/repository.json", `"branches":["main","topic"]`)
226 if len(build.Deletes) != 1 || build.Deletes[0] != ".dumbforge/files/old.json" { 230 if len(build.Deletes) != 2 || build.Deletes[0] != ".dumbforge/files/old.json" || build.Deletes[1] != ".dumbforge/branches/old.css" {
227 t.Fatalf("navigation deletes = %#v", build.Deletes) 231 t.Fatalf("navigation deletes = %#v", build.Deletes)
228 } 232 }
229 } 233 }
Modifiedinternal/site/template.go +4−3
@@ -10,6 +10,7 @@ const pageTemplate = `<!doctype html>
10 <link rel="icon" type="image/png" sizes="256x256" href="{{.BaseURL}}/.dumbforge/assets/gabriel-mark.png"> 10 <link rel="icon" type="image/png" sizes="256x256" href="{{.BaseURL}}/.dumbforge/assets/gabriel-mark.png">
11 <link rel="icon" type="image/svg+xml" href="{{.BaseURL}}/.dumbforge/assets/gabriel-mark.svg"> 11 <link rel="icon" type="image/svg+xml" href="{{.BaseURL}}/.dumbforge/assets/gabriel-mark.svg">
12 <link rel="stylesheet" href="{{.BaseURL}}/.dumbforge/assets/site.css?v={{.AssetVersion}}"> 12 <link rel="stylesheet" href="{{.BaseURL}}/.dumbforge/assets/site.css?v={{.AssetVersion}}">
13 <link rel="stylesheet" href="{{.BranchStyleURL}}">
13 </head> 14 </head>
14 <body data-base-url="{{.BaseURL}}" data-route-kind="{{.Kind}}" data-route-path="{{.Path}}" data-current-branch="{{.Branch}}" data-default-branch="{{.DefaultBranch}}" data-branches-url="{{.BranchesURL}}" data-files-url="{{.FilesURL}}"> 15 <body data-base-url="{{.BaseURL}}" data-route-kind="{{.Kind}}" data-route-path="{{.Path}}" data-current-branch="{{.Branch}}" data-default-branch="{{.DefaultBranch}}" data-branches-url="{{.BranchesURL}}" data-files-url="{{.FilesURL}}">
15 <header class="topbar"> 16 <header class="topbar">
@@ -21,7 +22,7 @@ const pageTemplate = `<!doctype html>
21 <section class="repo-nav"> 22 <section class="repo-nav">
22 <nav class="tabs" aria-label="Repository"> 23 <nav class="tabs" aria-label="Repository">
23 <a class="tab {{if and (ne .Kind "commits") (ne .Kind "commit")}}active{{end}}" href="{{.BaseURL}}">{{icon "code"}} Code</a> 24 <a class="tab {{if and (ne .Kind "commits") (ne .Kind "commit")}}active{{end}}" href="{{.BaseURL}}">{{icon "code"}} Code</a>
24 <a class="tab {{if or (eq .Kind "commits") (eq .Kind "commit")}}active{{end}}" href="{{.CommitsURL}}">{{icon "history"}} Commits <span class="counter">{{.CommitCount}}</span></a> 25 <a class="tab {{if or (eq .Kind "commits") (eq .Kind "commit")}}active{{end}}" href="{{.CommitsURL}}">{{icon "history"}} Commits <span class="counter" data-commit-count></span></a>
25 </nav> 26 </nav>
26 </section> 27 </section>
27 28
@@ -97,7 +98,7 @@ const pageTemplate = `<!doctype html>
97 {{template "branch-menu" .}} 98 {{template "branch-menu" .}}
98 <span class="toolbar-spacer"></span> 99 <span class="toolbar-spacer"></span>
99 {{template "go-file-button" .}} 100 {{template "go-file-button" .}}
100 <span class="history-total">{{icon "history"}} {{.CommitCount}} commits</span> 101 <span class="history-total">{{icon "history"}} <span data-commit-count></span> commits</span>
101 </div> 102 </div>
102 <div class="history-heading"><h1>Commit history</h1><p>Browse every commit reachable from <strong>{{.Branch}}</strong>.</p></div> 103 <div class="history-heading"><h1>Commit history</h1><p>Browse every commit reachable from <strong>{{.Branch}}</strong>.</p></div>
103 {{range .CommitGroups}}<section class="commit-group"> 104 {{range .CommitGroups}}<section class="commit-group">
@@ -583,7 +584,7 @@ const pageTemplate = `<!doctype html>
583 <div class="latest-commit"><strong>{{.Commit.Author}}</strong><a class="commit-message" href="{{.Commit.URL}}">{{.Commit.Message}}</a></div> 584 <div class="latest-commit"><strong>{{.Commit.Author}}</strong><a class="commit-message" href="{{.Commit.URL}}">{{.Commit.Message}}</a></div>
584 <a class="commit-hash" href="{{.Commit.URL}}">{{.Commit.ShortOID}}</a> 585 <a class="commit-hash" href="{{.Commit.URL}}">{{.Commit.ShortOID}}</a>
585 <time datetime="{{.Commit.DateTime}}" data-smart-time>{{.Commit.Date}}</time> 586 <time datetime="{{.Commit.DateTime}}" data-smart-time>{{.Commit.Date}}</time>
586 <a class="commit-count-link" href="{{.CommitsURL}}">{{icon "history"}}<strong>{{.CommitCount}}</strong> commits</a> 587 <a class="commit-count-link" href="{{.CommitsURL}}">{{icon "history"}}<strong data-commit-count></strong> commits</a>
587 </div> 588 </div>
588 <div class="tree-rows"> 589 <div class="tree-rows">
589 {{if .ParentURL}}<div class="tree-row parent-row"><a class="file-icon parent-icon" href="{{.ParentURL}}" aria-label="Parent directory">{{icon "back"}}</a><a class="file-name" href="{{.ParentURL}}">..</a><span></span><span></span></div>{{end}} 590 {{if .ParentURL}}<div class="tree-row parent-row"><a class="file-icon parent-icon" href="{{.ParentURL}}" aria-label="Parent directory">{{icon "back"}}</a><a class="file-name" href="{{.ParentURL}}">..</a><span></span><span></span></div>{{end}}