2020-08-06 04:04:08 -04:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-08-06 04:04:08 -04:00
|
|
|
|
|
|
|
package gitgraph
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 08:37:34 -05:00
|
|
|
"context"
|
2020-08-06 04:04:08 -04:00
|
|
|
"fmt"
|
2021-04-17 05:27:25 -04:00
|
|
|
"strings"
|
2024-11-09 21:59:40 -05:00
|
|
|
"time"
|
2020-11-08 12:21:54 -05:00
|
|
|
|
2021-12-10 03:14:24 -05:00
|
|
|
asymkey_model "code.gitea.io/gitea/models/asymkey"
|
2021-09-24 07:32:56 -04:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-12 11:51:54 -04:00
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2021-12-09 20:27:50 -05:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 04:49:20 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-11-08 12:21:54 -05:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-08-06 04:04:08 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewGraph creates a basic graph
|
|
|
|
func NewGraph() *Graph {
|
|
|
|
graph := &Graph{}
|
|
|
|
graph.relationCommit = &Commit{
|
|
|
|
Row: -1,
|
|
|
|
Column: -1,
|
|
|
|
}
|
|
|
|
graph.Flows = map[int64]*Flow{}
|
|
|
|
return graph
|
|
|
|
}
|
|
|
|
|
|
|
|
// Graph represents a collection of flows
|
|
|
|
type Graph struct {
|
|
|
|
Flows map[int64]*Flow
|
|
|
|
Commits []*Commit
|
|
|
|
MinRow int
|
|
|
|
MinColumn int
|
|
|
|
MaxRow int
|
|
|
|
MaxColumn int
|
|
|
|
relationCommit *Commit
|
|
|
|
}
|
|
|
|
|
|
|
|
// Width returns the width of the graph
|
|
|
|
func (graph *Graph) Width() int {
|
|
|
|
return graph.MaxColumn - graph.MinColumn + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Height returns the height of the graph
|
|
|
|
func (graph *Graph) Height() int {
|
|
|
|
return graph.MaxRow - graph.MinRow + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddGlyph adds glyph to flows
|
|
|
|
func (graph *Graph) AddGlyph(row, column int, flowID int64, color int, glyph byte) {
|
|
|
|
flow, ok := graph.Flows[flowID]
|
|
|
|
if !ok {
|
|
|
|
flow = NewFlow(flowID, color, row, column)
|
|
|
|
graph.Flows[flowID] = flow
|
|
|
|
}
|
|
|
|
flow.AddGlyph(row, column, glyph)
|
|
|
|
|
|
|
|
if row < graph.MinRow {
|
|
|
|
graph.MinRow = row
|
|
|
|
}
|
|
|
|
if row > graph.MaxRow {
|
|
|
|
graph.MaxRow = row
|
|
|
|
}
|
|
|
|
if column < graph.MinColumn {
|
|
|
|
graph.MinColumn = column
|
|
|
|
}
|
|
|
|
if column > graph.MaxColumn {
|
|
|
|
graph.MaxColumn = column
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddCommit adds a commit at row, column on flowID with the provided data
|
|
|
|
func (graph *Graph) AddCommit(row, column int, flowID int64, data []byte) error {
|
|
|
|
commit, err := NewCommit(row, column, data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
commit.Flow = flowID
|
|
|
|
graph.Commits = append(graph.Commits, commit)
|
|
|
|
|
|
|
|
graph.Flows[flowID].Commits = append(graph.Flows[flowID].Commits, commit)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-08 12:21:54 -05:00
|
|
|
// LoadAndProcessCommits will load the git.Commits for each commit in the graph,
|
|
|
|
// the associate the commit with the user author, and check the commit verification
|
|
|
|
// before finally retrieving the latest status
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 08:37:34 -05:00
|
|
|
func (graph *Graph) LoadAndProcessCommits(ctx context.Context, repository *repo_model.Repository, gitRepo *git.Repository) error {
|
2020-11-08 12:21:54 -05:00
|
|
|
var err error
|
|
|
|
var ok bool
|
|
|
|
|
2021-11-24 04:49:20 -05:00
|
|
|
emails := map[string]*user_model.User{}
|
2020-11-08 12:21:54 -05:00
|
|
|
keyMap := map[string]bool{}
|
|
|
|
|
|
|
|
for _, c := range graph.Commits {
|
|
|
|
if len(c.Rev) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c.Commit, err = gitRepo.GetCommit(c.Rev)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GetCommit: %s Error: %w", c.Rev, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Commit.Author != nil {
|
|
|
|
email := c.Commit.Author.Email
|
|
|
|
if c.User, ok = emails[email]; !ok {
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 08:37:34 -05:00
|
|
|
c.User, _ = user_model.GetUserByEmail(ctx, email)
|
2020-11-08 12:21:54 -05:00
|
|
|
emails[email] = c.User
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 08:37:34 -05:00
|
|
|
c.Verification = asymkey_model.ParseCommitWithSignature(ctx, c.Commit)
|
2020-11-08 12:21:54 -05:00
|
|
|
|
2021-12-10 03:14:24 -05:00
|
|
|
_ = asymkey_model.CalculateTrustStatus(c.Verification, repository.GetTrustModel(), func(user *user_model.User) (bool, error) {
|
2023-09-29 08:12:54 -04:00
|
|
|
return repo_model.IsOwnerMemberCollaborator(ctx, repository, user.ID)
|
2021-12-10 03:14:24 -05:00
|
|
|
}, &keyMap)
|
2020-11-08 12:21:54 -05:00
|
|
|
|
2023-09-25 09:17:37 -04:00
|
|
|
statuses, _, err := git_model.GetLatestCommitStatus(ctx, repository.ID, c.Commit.ID.String(), db.ListOptions{})
|
2020-11-08 12:21:54 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Error("GetLatestCommitStatus: %v", err)
|
|
|
|
} else {
|
2022-06-12 11:51:54 -04:00
|
|
|
c.Status = git_model.CalcCommitStatus(statuses)
|
2020-11-08 12:21:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-06 04:04:08 -04:00
|
|
|
// NewFlow creates a new flow
|
|
|
|
func NewFlow(flowID int64, color, row, column int) *Flow {
|
|
|
|
return &Flow{
|
|
|
|
ID: flowID,
|
|
|
|
ColorNumber: color,
|
|
|
|
MinRow: row,
|
|
|
|
MinColumn: column,
|
|
|
|
MaxRow: row,
|
|
|
|
MaxColumn: column,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flow represents a series of glyphs
|
|
|
|
type Flow struct {
|
|
|
|
ID int64
|
|
|
|
ColorNumber int
|
|
|
|
Glyphs []Glyph
|
|
|
|
Commits []*Commit
|
|
|
|
MinRow int
|
|
|
|
MinColumn int
|
|
|
|
MaxRow int
|
|
|
|
MaxColumn int
|
|
|
|
}
|
|
|
|
|
|
|
|
// Color16 wraps the color numbers around mod 16
|
|
|
|
func (flow *Flow) Color16() int {
|
|
|
|
return flow.ColorNumber % 16
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddGlyph adds glyph at row and column
|
|
|
|
func (flow *Flow) AddGlyph(row, column int, glyph byte) {
|
|
|
|
if row < flow.MinRow {
|
|
|
|
flow.MinRow = row
|
|
|
|
}
|
|
|
|
if row > flow.MaxRow {
|
|
|
|
flow.MaxRow = row
|
|
|
|
}
|
|
|
|
if column < flow.MinColumn {
|
|
|
|
flow.MinColumn = column
|
|
|
|
}
|
|
|
|
if column > flow.MaxColumn {
|
|
|
|
flow.MaxColumn = column
|
|
|
|
}
|
|
|
|
|
|
|
|
flow.Glyphs = append(flow.Glyphs, Glyph{
|
|
|
|
row,
|
|
|
|
column,
|
|
|
|
glyph,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-09 09:49:37 -04:00
|
|
|
// Glyph represents a coordinate and glyph
|
2020-08-06 04:04:08 -04:00
|
|
|
type Glyph struct {
|
|
|
|
Row int
|
|
|
|
Column int
|
|
|
|
Glyph byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// RelationCommit represents an empty relation commit
|
|
|
|
var RelationCommit = &Commit{
|
|
|
|
Row: -1,
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCommit creates a new commit from a provided line
|
|
|
|
func NewCommit(row, column int, line []byte) (*Commit, error) {
|
2020-11-08 12:21:54 -05:00
|
|
|
data := bytes.SplitN(line, []byte("|"), 5)
|
|
|
|
if len(data) < 5 {
|
2020-08-06 04:04:08 -04:00
|
|
|
return nil, fmt.Errorf("malformed data section on line %d with commit: %s", row, string(line))
|
|
|
|
}
|
2024-11-09 21:59:40 -05:00
|
|
|
// Format is a slight modifcation from RFC1123Z
|
|
|
|
t, err := time.Parse("Mon, _2 Jan 2006 15:04:05 -0700", string(data[2]))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not parse date of commit: %w", err)
|
|
|
|
}
|
2020-08-06 04:04:08 -04:00
|
|
|
return &Commit{
|
|
|
|
Row: row,
|
|
|
|
Column: column,
|
|
|
|
// 0 matches git log --pretty=format:%d => ref names, like the --decorate option of git-log(1)
|
2020-11-08 12:21:54 -05:00
|
|
|
Refs: newRefsFromRefNames(data[0]),
|
2020-08-06 04:04:08 -04:00
|
|
|
// 1 matches git log --pretty=format:%H => commit hash
|
|
|
|
Rev: string(data[1]),
|
2024-11-09 21:59:40 -05:00
|
|
|
// 2 matches git log --pretty=format:%aD => author date, RFC2822 style
|
|
|
|
Date: t,
|
2020-11-08 12:21:54 -05:00
|
|
|
// 3 matches git log --pretty=format:%h => abbreviated commit hash
|
|
|
|
ShortRev: string(data[3]),
|
|
|
|
// 4 matches git log --pretty=format:%s => subject
|
|
|
|
Subject: string(data[4]),
|
2020-08-06 04:04:08 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-11-08 12:21:54 -05:00
|
|
|
func newRefsFromRefNames(refNames []byte) []git.Reference {
|
|
|
|
refBytes := bytes.Split(refNames, []byte{',', ' '})
|
|
|
|
refs := make([]git.Reference, 0, len(refBytes))
|
|
|
|
for _, refNameBytes := range refBytes {
|
|
|
|
if len(refNameBytes) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
refName := string(refNameBytes)
|
2021-04-17 05:27:25 -04:00
|
|
|
if strings.HasPrefix(refName, "tag: ") {
|
|
|
|
refName = strings.TrimPrefix(refName, "tag: ")
|
2021-10-17 15:47:12 -04:00
|
|
|
} else {
|
2021-04-17 05:27:25 -04:00
|
|
|
refName = strings.TrimPrefix(refName, "HEAD -> ")
|
2020-11-08 12:21:54 -05:00
|
|
|
}
|
|
|
|
refs = append(refs, git.Reference{
|
|
|
|
Name: refName,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return refs
|
|
|
|
}
|
|
|
|
|
2024-05-09 09:49:37 -04:00
|
|
|
// Commit represents a commit at coordinate X, Y with the data
|
2020-08-06 04:04:08 -04:00
|
|
|
type Commit struct {
|
2020-11-08 12:21:54 -05:00
|
|
|
Commit *git.Commit
|
2021-11-24 04:49:20 -05:00
|
|
|
User *user_model.User
|
2024-03-04 13:11:42 -05:00
|
|
|
Verification *asymkey_model.ObjectVerification
|
2022-06-12 11:51:54 -04:00
|
|
|
Status *git_model.CommitStatus
|
2020-11-08 12:21:54 -05:00
|
|
|
Flow int64
|
|
|
|
Row int
|
|
|
|
Column int
|
|
|
|
Refs []git.Reference
|
|
|
|
Rev string
|
2024-11-09 21:59:40 -05:00
|
|
|
Date time.Time
|
2020-11-08 12:21:54 -05:00
|
|
|
ShortRev string
|
|
|
|
Subject string
|
2020-08-06 04:04:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnlyRelation returns whether this a relation only commit
|
|
|
|
func (c *Commit) OnlyRelation() bool {
|
|
|
|
return c.Row == -1
|
|
|
|
}
|