BlueGit

timfa bluegit

Files

bluegit / internal / server / commitgraph.go

commitgraph.go

commitgraph.go 2.4 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 SVG template.HTML
20}
21
22func (s *Server) graphs() *gitgraph.Cache {
23 s.graphOnce.Do(func() {
24 s.graphCache = gitgraph.NewCache(gitgraph.DefaultMaxBytes)
25 })
26 return s.graphCache
27}
28
29func (s *Server) fillGraph(r *http.Request, v *View, repo store.Repo) {
30 g, err := s.repoGraph(r.Context(), repo)
31 if err != nil || g == nil || len(g.Rows) == 0 {
32 return
33 }
34 v.Graph = presentGraph(r.Context(), s, repo.ID, g, i18n.Parse(v.Lang))
35}
36
37func (s *Server) repoGraph(ctx context.Context, repo store.Repo) (*gitgraph.Graph, error) {
38 def := repo.DefaultBranch
39 // Sleutel op repo + hoofdbranch + limiet. De tip-stand zit niet in de
40 // lookup: die zou bij elke pageview git- of schijf-I/O vragen. Bij een
41 // push wist refsChanged de entry (Drop) en vult refreshCache hem opnieuw.
42 key := gitgraph.Key(repo.ID, nil, def, gitgraph.DefaultLimit)
43 return s.graphs().Get(ctx, key, def, func(ctx context.Context) ([]gitgraph.Commit, []gitgraph.Ref, error) {
44 return gitrepo.GraphHistory(ctx, s.Git, s.gdir(repo), gitgraph.DefaultLimit)
45 })
46}
47
48func presentGraph(ctx context.Context, s *Server, repoID int64, g *gitgraph.Graph, lang i18n.Lang) *GraphView {
49 rows := make([]gitgraph.Row, len(g.Rows))
50 copy(rows, g.Rows)
51 if s != nil {
52 in := make([]gitrepo.Commit, 0, len(rows))
53 for _, r := range rows {
54 in = append(in, gitrepo.Commit{
55 SHA: r.SHA, Short: r.Short, Author: r.Author, Email: r.Email,
56 Unix: r.Unix, Subject: r.Subject,
57 })
58 }
59 people := s.presentCommits(ctx, repoID, in, lang)
60 for i := range rows {
61 if i >= len(people) {
62 break
63 }
64 p := people[i]
65 rows[i].Avatar = p.Avatar
66 rows[i].Initial = p.Initial
67 rows[i].UserURL = p.UserURL
68 if rows[i].Initial == "" {
69 rows[i].Initial = initialOf(rows[i].Author)
70 }
71 }
72 }
73 for i := range rows {
74 if rows[i].Initial == "" {
75 rows[i].Initial = initialOf(rows[i].Author)
76 }
77 }
78 return &GraphView{
79 Rows: rows,
80 Lanes: g.Lanes,
81 Width: g.Width,
82 Height: g.Height,
83 SVG: template.HTML(g.SVG()),
84 }
85}
86
87func (s *Server) dropGraph(repo store.Repo) {
88 if s == nil {
89 return
90 }
91 s.graphs().Drop(repo.ID)
92}