2019-04-19 22:47:00 -04:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-04-19 22:47:00 -04:00
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
gotemplate "html/template"
|
2021-04-05 11:30:52 -04:00
|
|
|
"net/http"
|
2021-11-16 13:18:25 -05:00
|
|
|
"net/url"
|
2023-09-16 13:42:34 -04:00
|
|
|
"strconv"
|
2019-04-19 22:47:00 -04:00
|
|
|
"strings"
|
|
|
|
|
2021-11-24 04:49:20 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2022-01-06 20:18:52 -05:00
|
|
|
"code.gitea.io/gitea/modules/charset"
|
2019-04-19 22:47:00 -04:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/highlight"
|
2021-11-17 15:37:00 -05:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2024-04-01 09:11:30 -04:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-12-03 13:46:11 -05:00
|
|
|
"code.gitea.io/gitea/modules/templates"
|
2021-11-16 13:18:25 -05:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2024-02-27 02:12:22 -05:00
|
|
|
"code.gitea.io/gitea/services/context"
|
2024-02-14 13:50:31 -05:00
|
|
|
files_service "code.gitea.io/gitea/services/repository/files"
|
2019-04-19 22:47:00 -04:00
|
|
|
)
|
|
|
|
|
2021-06-27 19:13:20 -04:00
|
|
|
type blameRow struct {
|
|
|
|
RowNumber int
|
|
|
|
Avatar gotemplate.HTML
|
|
|
|
RepoLink string
|
|
|
|
PartSha string
|
|
|
|
PreviousSha string
|
|
|
|
PreviousShaURL string
|
|
|
|
IsFirstCommit bool
|
|
|
|
CommitURL string
|
|
|
|
CommitMessage string
|
|
|
|
CommitSince gotemplate.HTML
|
|
|
|
Code gotemplate.HTML
|
2022-08-13 14:32:34 -04:00
|
|
|
EscapeStatus *charset.EscapeStatus
|
2021-06-27 19:13:20 -04:00
|
|
|
}
|
|
|
|
|
2019-04-19 22:47:00 -04:00
|
|
|
// RefBlame render blame page
|
|
|
|
func RefBlame(ctx *context.Context) {
|
|
|
|
fileName := ctx.Repo.TreePath
|
|
|
|
if len(fileName) == 0 {
|
|
|
|
ctx.NotFound("Blame FileName", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
branchLink := ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()
|
|
|
|
treeLink := branchLink
|
|
|
|
rawLink := ctx.Repo.RepoLink + "/raw/" + ctx.Repo.BranchNameSubURL()
|
|
|
|
|
|
|
|
if len(ctx.Repo.TreePath) > 0 {
|
2021-11-16 13:18:25 -05:00
|
|
|
treeLink += "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
|
2019-04-19 22:47:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var treeNames []string
|
|
|
|
paths := make([]string, 0, 5)
|
|
|
|
if len(ctx.Repo.TreePath) > 0 {
|
|
|
|
treeNames = strings.Split(ctx.Repo.TreePath, "/")
|
|
|
|
for i := range treeNames {
|
|
|
|
paths = append(paths, strings.Join(treeNames[:i+1], "/"))
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Data["HasParentPath"] = true
|
|
|
|
if len(paths)-2 >= 0 {
|
|
|
|
ctx.Data["ParentPath"] = "/" + paths[len(paths)-1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get current entry user currently looking at.
|
|
|
|
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
|
|
|
|
if err != nil {
|
2023-09-29 03:42:39 -04:00
|
|
|
HandleGitError(ctx, "Repo.Commit.GetTreeEntryByPath", err)
|
2019-04-19 22:47:00 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
blob := entry.Blob()
|
|
|
|
|
|
|
|
ctx.Data["Paths"] = paths
|
|
|
|
ctx.Data["TreeLink"] = treeLink
|
|
|
|
ctx.Data["TreeNames"] = treeNames
|
|
|
|
ctx.Data["BranchLink"] = branchLink
|
2020-06-30 17:34:03 -04:00
|
|
|
|
2021-11-16 13:18:25 -05:00
|
|
|
ctx.Data["RawFileLink"] = rawLink + "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
|
2019-04-19 22:47:00 -04:00
|
|
|
ctx.Data["PageIsViewCode"] = true
|
|
|
|
|
|
|
|
ctx.Data["IsBlame"] = true
|
|
|
|
|
2024-04-01 09:11:30 -04:00
|
|
|
fileSize := blob.Size()
|
|
|
|
ctx.Data["FileSize"] = fileSize
|
2019-04-19 22:47:00 -04:00
|
|
|
ctx.Data["FileName"] = blob.Name()
|
|
|
|
|
2024-04-01 09:11:30 -04:00
|
|
|
if fileSize >= setting.UI.MaxDisplayFileSize {
|
|
|
|
ctx.Data["IsFileTooLarge"] = true
|
|
|
|
ctx.HTML(http.StatusOK, tplRepoHome)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-15 14:39:39 -04:00
|
|
|
ctx.Data["NumLines"], err = blob.GetBlobLineCount()
|
|
|
|
if err != nil {
|
|
|
|
ctx.NotFound("GetBlobLineCount", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-16 13:42:34 -04:00
|
|
|
bypassBlameIgnore, _ := strconv.ParseBool(ctx.FormString("bypass-blame-ignore"))
|
|
|
|
|
|
|
|
result, err := performBlame(ctx, ctx.Repo.Repository.RepoPath(), ctx.Repo.Commit, fileName, bypassBlameIgnore)
|
2019-04-19 22:47:00 -04:00
|
|
|
if err != nil {
|
|
|
|
ctx.NotFound("CreateBlameReader", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-16 13:42:34 -04:00
|
|
|
ctx.Data["UsesIgnoreRevs"] = result.UsesIgnoreRevs
|
|
|
|
ctx.Data["FaultyIgnoreRevsFile"] = result.FaultyIgnoreRevsFile
|
2019-04-19 22:47:00 -04:00
|
|
|
|
2021-06-27 19:13:20 -04:00
|
|
|
// Get Topics of this repo
|
|
|
|
renderRepoTopics(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-30 20:26:52 -05:00
|
|
|
commitNames := processBlameParts(ctx, result.Parts)
|
2021-06-27 19:13:20 -04:00
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-30 20:26:52 -05:00
|
|
|
renderBlame(ctx, result.Parts, commitNames)
|
2021-06-27 19:13:20 -04:00
|
|
|
|
2023-04-14 15:29:05 -04:00
|
|
|
ctx.HTML(http.StatusOK, tplRepoHome)
|
2021-06-27 19:13:20 -04:00
|
|
|
}
|
|
|
|
|
2023-09-16 13:42:34 -04:00
|
|
|
type blameResult struct {
|
2023-12-21 16:48:18 -05:00
|
|
|
Parts []*git.BlamePart
|
2023-09-16 13:42:34 -04:00
|
|
|
UsesIgnoreRevs bool
|
|
|
|
FaultyIgnoreRevsFile bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func performBlame(ctx *context.Context, repoPath string, commit *git.Commit, file string, bypassBlameIgnore bool) (*blameResult, error) {
|
2024-02-24 01:55:19 -05:00
|
|
|
objectFormat := ctx.Repo.GetObjectFormat()
|
|
|
|
|
2023-12-13 16:02:00 -05:00
|
|
|
blameReader, err := git.CreateBlameReader(ctx, objectFormat, repoPath, commit, file, bypassBlameIgnore)
|
2023-09-16 13:42:34 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r := &blameResult{}
|
|
|
|
if err := fillBlameResult(blameReader, r); err != nil {
|
|
|
|
_ = blameReader.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = blameReader.Close()
|
|
|
|
if err != nil {
|
|
|
|
if len(r.Parts) == 0 && r.UsesIgnoreRevs {
|
|
|
|
// try again without ignored revs
|
|
|
|
|
2023-12-13 16:02:00 -05:00
|
|
|
blameReader, err = git.CreateBlameReader(ctx, objectFormat, repoPath, commit, file, true)
|
2023-09-16 13:42:34 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r := &blameResult{
|
|
|
|
FaultyIgnoreRevsFile: true,
|
|
|
|
}
|
|
|
|
if err := fillBlameResult(blameReader, r); err != nil {
|
|
|
|
_ = blameReader.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, blameReader.Close()
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func fillBlameResult(br *git.BlameReader, r *blameResult) error {
|
|
|
|
r.UsesIgnoreRevs = br.UsesIgnoreRevs()
|
|
|
|
|
2023-12-21 16:48:18 -05:00
|
|
|
previousHelper := make(map[string]*git.BlamePart)
|
|
|
|
|
|
|
|
r.Parts = make([]*git.BlamePart, 0, 5)
|
2023-09-16 13:42:34 -04:00
|
|
|
for {
|
|
|
|
blamePart, err := br.NextPart()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("BlameReader.NextPart failed: %w", err)
|
|
|
|
}
|
|
|
|
if blamePart == nil {
|
|
|
|
break
|
|
|
|
}
|
2023-12-21 16:48:18 -05:00
|
|
|
|
|
|
|
if prev, ok := previousHelper[blamePart.Sha]; ok {
|
|
|
|
if blamePart.PreviousSha == "" {
|
|
|
|
blamePart.PreviousSha = prev.PreviousSha
|
|
|
|
blamePart.PreviousPath = prev.PreviousPath
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
previousHelper[blamePart.Sha] = blamePart
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Parts = append(r.Parts, blamePart)
|
2023-09-16 13:42:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-21 16:48:18 -05:00
|
|
|
func processBlameParts(ctx *context.Context, blameParts []*git.BlamePart) map[string]*user_model.UserCommit {
|
2021-06-27 19:13:20 -04:00
|
|
|
// store commit data by SHA to look up avatar info etc
|
2021-11-24 04:49:20 -05:00
|
|
|
commitNames := make(map[string]*user_model.UserCommit)
|
2021-06-27 19:13:20 -04:00
|
|
|
// and as blameParts can reference the same commits multiple
|
|
|
|
// times, we cache the lookup work locally
|
2021-08-09 14:08:51 -04:00
|
|
|
commits := make([]*git.Commit, 0, len(blameParts))
|
2021-06-27 19:13:20 -04:00
|
|
|
commitCache := map[string]*git.Commit{}
|
|
|
|
commitCache[ctx.Repo.Commit.ID.String()] = ctx.Repo.Commit
|
2019-04-19 22:47:00 -04:00
|
|
|
|
|
|
|
for _, part := range blameParts {
|
|
|
|
sha := part.Sha
|
|
|
|
if _, ok := commitNames[sha]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-06-27 19:13:20 -04:00
|
|
|
// find the blamePart commit, to look up parent & email address for avatars
|
|
|
|
commit, ok := commitCache[sha]
|
|
|
|
var err error
|
|
|
|
if !ok {
|
|
|
|
commit, err = ctx.Repo.GitRepo.GetCommit(sha)
|
|
|
|
if err != nil {
|
|
|
|
if git.IsErrNotExist(err) {
|
|
|
|
ctx.NotFound("Repo.GitRepo.GetCommit", err)
|
|
|
|
} else {
|
|
|
|
ctx.ServerError("Repo.GitRepo.GetCommit", err)
|
|
|
|
}
|
2023-11-30 20:26:52 -05:00
|
|
|
return nil
|
2021-06-27 19:13:20 -04:00
|
|
|
}
|
|
|
|
commitCache[sha] = commit
|
|
|
|
}
|
|
|
|
|
2021-08-09 14:08:51 -04:00
|
|
|
commits = append(commits, commit)
|
2019-04-19 22:47:00 -04:00
|
|
|
}
|
|
|
|
|
2021-06-27 19:13:20 -04:00
|
|
|
// populate commit email addresses to later look up avatars.
|
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
|
|
|
for _, c := range user_model.ValidateCommitsWithEmails(ctx, commits) {
|
2019-04-19 22:47:00 -04:00
|
|
|
commitNames[c.ID.String()] = c
|
|
|
|
}
|
|
|
|
|
2023-11-30 20:26:52 -05:00
|
|
|
return commitNames
|
2019-04-19 22:47:00 -04:00
|
|
|
}
|
|
|
|
|
2023-12-21 16:48:18 -05:00
|
|
|
func renderBlame(ctx *context.Context, blameParts []*git.BlamePart, commitNames map[string]*user_model.UserCommit) {
|
2019-04-19 22:47:00 -04:00
|
|
|
repoLink := ctx.Repo.RepoLink
|
|
|
|
|
2024-02-14 13:50:31 -05:00
|
|
|
language, err := files_service.TryGetContentLanguage(ctx.Repo.GitRepo, ctx.Repo.CommitID, ctx.Repo.TreePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to get file language for %-v:%s. Error: %v", ctx.Repo.Repository, ctx.Repo.TreePath, err)
|
2021-11-17 15:37:00 -05:00
|
|
|
}
|
2024-02-14 13:50:31 -05:00
|
|
|
|
2022-01-20 12:46:10 -05:00
|
|
|
lines := make([]string, 0)
|
2021-06-27 19:13:20 -04:00
|
|
|
rows := make([]*blameRow, 0)
|
2022-08-13 14:32:34 -04:00
|
|
|
escapeStatus := &charset.EscapeStatus{}
|
2019-04-19 22:47:00 -04:00
|
|
|
|
2022-11-19 06:08:06 -05:00
|
|
|
var lexerName string
|
|
|
|
|
2023-08-09 23:19:39 -04:00
|
|
|
avatarUtils := templates.NewAvatarUtils(ctx)
|
2022-01-20 12:46:10 -05:00
|
|
|
i := 0
|
|
|
|
commitCnt := 0
|
2021-06-27 19:13:20 -04:00
|
|
|
for _, part := range blameParts {
|
2019-04-19 22:47:00 -04:00
|
|
|
for index, line := range part.Lines {
|
|
|
|
i++
|
|
|
|
lines = append(lines, line)
|
|
|
|
|
2021-06-27 19:13:20 -04:00
|
|
|
br := &blameRow{
|
|
|
|
RowNumber: i,
|
2019-04-19 22:47:00 -04:00
|
|
|
}
|
2021-06-27 19:13:20 -04:00
|
|
|
|
2019-04-19 22:47:00 -04:00
|
|
|
commit := commitNames[part.Sha]
|
|
|
|
if index == 0 {
|
2021-06-27 19:13:20 -04:00
|
|
|
// Count commit number
|
|
|
|
commitCnt++
|
|
|
|
|
2019-04-19 22:47:00 -04:00
|
|
|
// User avatar image
|
2024-11-04 06:30:00 -05:00
|
|
|
commitSince := templates.TimeSince(commit.Author.When)
|
2020-12-03 13:46:11 -05:00
|
|
|
|
|
|
|
var avatar string
|
2019-04-19 22:47:00 -04:00
|
|
|
if commit.User != nil {
|
2024-03-24 08:14:03 -04:00
|
|
|
avatar = string(avatarUtils.Avatar(commit.User, 18))
|
2019-04-19 22:47:00 -04:00
|
|
|
} else {
|
Migrate margin and padding helpers to tailwind (#30043)
This will conclude the refactor of 1:1 class replacements to tailwind,
except `gt-hidden`. Commands ran:
```bash
perl -p -i -e 's#gt-(p|m)([lrtbxy])?-0#tw-$1$2-0#g' {web_src/js,templates,routers,services}/**/*
perl -p -i -e 's#gt-(p|m)([lrtbxy])?-1#tw-$1$2-0.5#g' {web_src/js,templates,routers,services}/**/*
perl -p -i -e 's#gt-(p|m)([lrtbxy])?-2#tw-$1$2-1#g' {web_src/js,templates,routers,services}/**/*
perl -p -i -e 's#gt-(p|m)([lrtbxy])?-3#tw-$1$2-2#g' {web_src/js,templates,routers,services}/**/*
perl -p -i -e 's#gt-(p|m)([lrtbxy])?-4#tw-$1$2-4#g' {web_src/js,templates,routers,services}/**/*
perl -p -i -e 's#gt-(p|m)([lrtbxy])?-5#tw-$1$2-8#g' {web_src/js,templates,routers,services}/**/*
```
2024-03-24 12:42:49 -04:00
|
|
|
avatar = string(avatarUtils.AvatarByEmail(commit.Author.Email, commit.Author.Name, 18, "tw-mr-2"))
|
2019-04-19 22:47:00 -04:00
|
|
|
}
|
2020-12-03 13:46:11 -05:00
|
|
|
|
2021-06-27 19:13:20 -04:00
|
|
|
br.Avatar = gotemplate.HTML(avatar)
|
|
|
|
br.RepoLink = repoLink
|
|
|
|
br.PartSha = part.Sha
|
2023-11-30 20:26:52 -05:00
|
|
|
br.PreviousSha = part.PreviousSha
|
|
|
|
br.PreviousShaURL = fmt.Sprintf("%s/blame/commit/%s/%s", repoLink, url.PathEscape(part.PreviousSha), util.PathEscapeSegments(part.PreviousPath))
|
2021-11-16 13:18:25 -05:00
|
|
|
br.CommitURL = fmt.Sprintf("%s/commit/%s", repoLink, url.PathEscape(part.Sha))
|
2021-10-31 04:25:24 -04:00
|
|
|
br.CommitMessage = commit.CommitMessage
|
2021-06-27 19:13:20 -04:00
|
|
|
br.CommitSince = commitSince
|
2019-04-19 22:47:00 -04:00
|
|
|
}
|
|
|
|
|
2019-05-11 16:27:39 -04:00
|
|
|
if i != len(lines)-1 {
|
2019-04-19 22:47:00 -04:00
|
|
|
line += "\n"
|
|
|
|
}
|
2020-06-30 17:34:03 -04:00
|
|
|
fileName := fmt.Sprintf("%v", ctx.Data["FileName"])
|
2022-11-19 06:08:06 -05:00
|
|
|
line, lexerNameForLine := highlight.Code(fileName, language, line)
|
|
|
|
|
|
|
|
// set lexer name to the first detected lexer. this is certainly suboptimal and
|
|
|
|
// we should instead highlight the whole file at once
|
|
|
|
if lexerName == "" {
|
|
|
|
lexerName = lexerNameForLine
|
|
|
|
}
|
2021-06-27 19:13:20 -04:00
|
|
|
|
2023-12-17 09:38:54 -05:00
|
|
|
br.EscapeStatus, br.Code = charset.EscapeControlHTML(line, ctx.Locale)
|
2021-06-27 19:13:20 -04:00
|
|
|
rows = append(rows, br)
|
2022-01-06 20:18:52 -05:00
|
|
|
escapeStatus = escapeStatus.Or(br.EscapeStatus)
|
2019-04-19 22:47:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-06 20:18:52 -05:00
|
|
|
ctx.Data["EscapeStatus"] = escapeStatus
|
2021-06-27 19:13:20 -04:00
|
|
|
ctx.Data["BlameRows"] = rows
|
|
|
|
ctx.Data["CommitCnt"] = commitCnt
|
2022-11-19 06:08:06 -05:00
|
|
|
ctx.Data["LexerName"] = lexerName
|
2019-04-19 22:47:00 -04:00
|
|
|
}
|