BlueGit

timfa bluegit

Files

bluegit / internal / server / commitgraph.go

commitgraph.go

commitgraph.go 2.9 KiB Download Raw
1package server
2
3import (
4 "context"
5 "html/template"
6 "net/http"
7
8 "gitlab.com/Timfa/bluegit/internal/gitgraph"
9 "gitlab.com/Timfa/bluegit/internal/gitrepo"
10 "gitlab.com/Timfa/bluegit/internal/i18n"
11 "gitlab.com/Timfa/bluegit/internal/store"
12)
13
14// GraphView is de commitgrafiek op de project-overzichtspagina.
15type GraphView struct {
16 Rows []gitgraph.Row
17 Lanes int
18 Width, Height int
19 TextX, PanelWidth int
20 RefW int
21 SVG template.HTML
22}
23
24func (s *Server) graphs() *gitgraph.Cache {
25 s.graphOnce.Do(func() {
26 s.graphCache = gitgraph.NewCache(gitgraph.DefaultMaxBytes)
27 })
28 return s.graphCache
29}
30
31func (s *Server) fillGraph(r *http.Request, v *View, repo store.Repo) {
32 g, err := s.repoGraph(r.Context(), repo)
33 if err != nil || g == nil || len(g.Rows) == 0 {
34 return
35 }
36 v.Graph = presentGraph(r.Context(), s, repo, g, i18n.Parse(v.Lang))
37}
38
39func (s *Server) repoGraph(ctx context.Context, repo store.Repo) (*gitgraph.Graph, error) {
40 def := repo.DefaultBranch
41 // Sleutel op repo + hoofdbranch + limiet. De tip-stand zit niet in de
42 // lookup: die zou bij elke pageview git- of schijf-I/O vragen. Bij een
43 // push wist refsChanged de entry (Drop) en vult refreshCache hem opnieuw.
44 key := gitgraph.Key(repo.ID, nil, def, gitgraph.DefaultLimit)
45 return s.graphs().Get(ctx, key, def, func(ctx context.Context) ([]gitgraph.Commit, []gitgraph.Ref, error) {
46 return gitrepo.GraphHistory(ctx, s.Git, s.gdir(repo), gitgraph.DefaultLimit)
47 })
48}
49
50func presentGraph(ctx context.Context, s *Server, repo store.Repo, g *gitgraph.Graph, lang i18n.Lang) *GraphView {
51 rows := make([]gitgraph.Row, len(g.Rows))
52 copy(rows, g.Rows)
53 if s != nil {
54 in := make([]gitrepo.Commit, 0, len(rows))
55 for _, r := range rows {
56 in = append(in, gitrepo.Commit{
57 SHA: r.SHA, Short: r.Short, Author: r.Author, Email: r.Email,
58 Unix: r.Unix, Subject: r.Subject,
59 })
60 }
61 people := s.presentCommits(ctx, repo.ID, in, lang)
62 for i := range rows {
63 if i >= len(people) {
64 break
65 }
66 p := people[i]
67 rows[i].Avatar = p.Avatar
68 rows[i].Initial = p.Initial
69 rows[i].UserURL = p.UserURL
70 if rows[i].Initial == "" {
71 rows[i].Initial = initialOf(rows[i].Author)
72 }
73 }
74 }
75 for i := range rows {
76 if rows[i].Initial == "" {
77 rows[i].Initial = initialOf(rows[i].Author)
78 }
79 }
80 // Kopie met avatars: de cache bewaart de kale layout, de SVG
81 // krijgt hier de knopen (image/clipPath) op de lane-x.
82 draw := *g
83 draw.Rows = rows
84 if repo.OwnerPath != "" && repo.Name != "" {
85 draw.CommitBase = "/" + repo.FullPath() + "/-/commit/"
86 }
87 return &GraphView{
88 Rows: rows,
89 Lanes: g.Lanes,
90 Width: g.Width,
91 Height: g.Height,
92 TextX: g.TextX,
93 PanelWidth: g.PanelWidth,
94 RefW: gitgraph.RefW,
95 SVG: template.HTML(draw.SVG()),
96 }
97}
98
99func (s *Server) dropGraph(repo store.Repo) {
100 if s == nil {
101 return
102 }
103 s.graphs().Drop(repo.ID)
104}