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