2019-04-20 08:17:00 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html"
|
|
|
|
gotemplate "html/template"
|
2021-04-05 21:00:52 +05:30
|
|
|
"net/http"
|
2019-04-20 08:17:00 +05:30
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/highlight"
|
2020-12-04 00:16:11 +05:30
|
|
|
"code.gitea.io/gitea/modules/templates"
|
2019-08-15 20:16:21 +05:30
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2019-04-20 08:17:00 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tplBlame base.TplName = "repo/home"
|
|
|
|
)
|
|
|
|
|
2021-06-28 04:43:20 +05:30
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-04-20 08:17:00 +05:30
|
|
|
// RefBlame render blame page
|
|
|
|
func RefBlame(ctx *context.Context) {
|
|
|
|
fileName := ctx.Repo.TreePath
|
|
|
|
if len(fileName) == 0 {
|
|
|
|
ctx.NotFound("Blame FileName", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
userName := ctx.Repo.Owner.Name
|
|
|
|
repoName := ctx.Repo.Repository.Name
|
|
|
|
commitID := ctx.Repo.CommitID
|
|
|
|
|
|
|
|
branchLink := ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()
|
|
|
|
treeLink := branchLink
|
|
|
|
rawLink := ctx.Repo.RepoLink + "/raw/" + ctx.Repo.BranchNameSubURL()
|
|
|
|
|
|
|
|
if len(ctx.Repo.TreePath) > 0 {
|
|
|
|
treeLink += "/" + ctx.Repo.TreePath
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
ctx.NotFoundOrServerError("Repo.Commit.GetTreeEntryByPath", git.IsErrNotExist, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
blob := entry.Blob()
|
|
|
|
|
|
|
|
ctx.Data["Paths"] = paths
|
|
|
|
ctx.Data["TreeLink"] = treeLink
|
|
|
|
ctx.Data["TreeNames"] = treeNames
|
|
|
|
ctx.Data["BranchLink"] = branchLink
|
2020-07-01 03:04:03 +05:30
|
|
|
|
2019-04-20 08:17:00 +05:30
|
|
|
ctx.Data["RawFileLink"] = rawLink + "/" + ctx.Repo.TreePath
|
|
|
|
ctx.Data["PageIsViewCode"] = true
|
|
|
|
|
|
|
|
ctx.Data["IsBlame"] = true
|
|
|
|
|
|
|
|
ctx.Data["FileSize"] = blob.Size()
|
|
|
|
ctx.Data["FileName"] = blob.Name()
|
|
|
|
|
2020-06-16 00:09:39 +05:30
|
|
|
ctx.Data["NumLines"], err = blob.GetBlobLineCount()
|
|
|
|
if err != nil {
|
|
|
|
ctx.NotFound("GetBlobLineCount", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:48:11 +05:30
|
|
|
blameReader, err := git.CreateBlameReader(ctx, models.RepoPath(userName, repoName), commitID, fileName)
|
2019-04-20 08:17:00 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.NotFound("CreateBlameReader", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer blameReader.Close()
|
|
|
|
|
2019-06-26 23:45:26 +05:30
|
|
|
blameParts := make([]git.BlamePart, 0)
|
2019-04-20 08:17:00 +05:30
|
|
|
|
|
|
|
for {
|
|
|
|
blamePart, err := blameReader.NextPart()
|
|
|
|
if err != nil {
|
|
|
|
ctx.NotFound("NextPart", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if blamePart == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
blameParts = append(blameParts, *blamePart)
|
|
|
|
}
|
|
|
|
|
2021-06-28 04:43:20 +05:30
|
|
|
// Get Topics of this repo
|
|
|
|
renderRepoTopics(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
commitNames, previousCommits := processBlameParts(ctx, blameParts)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
renderBlame(ctx, blameParts, commitNames, previousCommits)
|
|
|
|
|
|
|
|
ctx.HTML(http.StatusOK, tplBlame)
|
|
|
|
}
|
|
|
|
|
2021-08-09 23:38:51 +05:30
|
|
|
func processBlameParts(ctx *context.Context, blameParts []git.BlamePart) (map[string]*models.UserCommit, map[string]string) {
|
2021-06-28 04:43:20 +05:30
|
|
|
// store commit data by SHA to look up avatar info etc
|
2021-08-09 23:38:51 +05:30
|
|
|
commitNames := make(map[string]*models.UserCommit)
|
2021-06-28 04:43:20 +05:30
|
|
|
// previousCommits contains links from SHA to parent SHA,
|
|
|
|
// if parent also contains the current TreePath.
|
|
|
|
previousCommits := make(map[string]string)
|
|
|
|
// and as blameParts can reference the same commits multiple
|
|
|
|
// times, we cache the lookup work locally
|
2021-08-09 23:38:51 +05:30
|
|
|
commits := make([]*git.Commit, 0, len(blameParts))
|
2021-06-28 04:43:20 +05:30
|
|
|
commitCache := map[string]*git.Commit{}
|
|
|
|
commitCache[ctx.Repo.Commit.ID.String()] = ctx.Repo.Commit
|
2019-04-20 08:17:00 +05:30
|
|
|
|
|
|
|
for _, part := range blameParts {
|
|
|
|
sha := part.Sha
|
|
|
|
if _, ok := commitNames[sha]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-06-28 04:43:20 +05:30
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
commitCache[sha] = commit
|
|
|
|
}
|
|
|
|
|
|
|
|
// find parent commit
|
|
|
|
if commit.ParentCount() > 0 {
|
|
|
|
psha := commit.Parents[0]
|
|
|
|
previousCommit, ok := commitCache[psha.String()]
|
|
|
|
if !ok {
|
|
|
|
previousCommit, _ = commit.Parent(0)
|
|
|
|
if previousCommit != nil {
|
|
|
|
commitCache[psha.String()] = previousCommit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// only store parent commit ONCE, if it has the file
|
|
|
|
if previousCommit != nil {
|
|
|
|
if haz1, _ := previousCommit.HasFile(ctx.Repo.TreePath); haz1 {
|
|
|
|
previousCommits[commit.ID.String()] = previousCommit.ID.String()
|
|
|
|
}
|
2019-04-20 08:17:00 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-09 23:38:51 +05:30
|
|
|
commits = append(commits, commit)
|
2019-04-20 08:17:00 +05:30
|
|
|
}
|
|
|
|
|
2021-06-28 04:43:20 +05:30
|
|
|
// populate commit email addresses to later look up avatars.
|
2021-08-09 23:38:51 +05:30
|
|
|
for _, c := range models.ValidateCommitsWithEmails(commits) {
|
2019-04-20 08:17:00 +05:30
|
|
|
commitNames[c.ID.String()] = c
|
|
|
|
}
|
|
|
|
|
2021-06-28 04:43:20 +05:30
|
|
|
return commitNames, previousCommits
|
2019-04-20 08:17:00 +05:30
|
|
|
}
|
|
|
|
|
2021-08-09 23:38:51 +05:30
|
|
|
func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames map[string]*models.UserCommit, previousCommits map[string]string) {
|
2019-04-20 08:17:00 +05:30
|
|
|
repoLink := ctx.Repo.RepoLink
|
|
|
|
|
2019-06-13 01:11:28 +05:30
|
|
|
var lines = make([]string, 0)
|
2021-06-28 04:43:20 +05:30
|
|
|
rows := make([]*blameRow, 0)
|
2019-04-20 08:17:00 +05:30
|
|
|
|
|
|
|
var i = 0
|
2021-06-28 04:43:20 +05:30
|
|
|
var commitCnt = 0
|
|
|
|
for _, part := range blameParts {
|
2019-04-20 08:17:00 +05:30
|
|
|
for index, line := range part.Lines {
|
|
|
|
i++
|
|
|
|
lines = append(lines, line)
|
|
|
|
|
2021-06-28 04:43:20 +05:30
|
|
|
br := &blameRow{
|
|
|
|
RowNumber: i,
|
2019-04-20 08:17:00 +05:30
|
|
|
}
|
2021-06-28 04:43:20 +05:30
|
|
|
|
2019-04-20 08:17:00 +05:30
|
|
|
commit := commitNames[part.Sha]
|
2021-06-28 04:43:20 +05:30
|
|
|
previousSha := previousCommits[part.Sha]
|
2019-04-20 08:17:00 +05:30
|
|
|
if index == 0 {
|
2021-06-28 04:43:20 +05:30
|
|
|
// Count commit number
|
|
|
|
commitCnt++
|
|
|
|
|
2019-04-20 08:17:00 +05:30
|
|
|
// User avatar image
|
2019-08-15 20:16:21 +05:30
|
|
|
commitSince := timeutil.TimeSinceUnix(timeutil.TimeStamp(commit.Author.When.Unix()), ctx.Data["Lang"].(string))
|
2020-12-04 00:16:11 +05:30
|
|
|
|
|
|
|
var avatar string
|
2019-04-20 08:17:00 +05:30
|
|
|
if commit.User != nil {
|
2020-12-04 00:16:11 +05:30
|
|
|
avatar = string(templates.Avatar(commit.User, 18, "mr-3"))
|
2019-04-20 08:17:00 +05:30
|
|
|
} else {
|
2020-12-04 00:16:11 +05:30
|
|
|
avatar = string(templates.AvatarByEmail(commit.Author.Email, commit.Author.Name, 18, "mr-3"))
|
2019-04-20 08:17:00 +05:30
|
|
|
}
|
2020-12-04 00:16:11 +05:30
|
|
|
|
2021-06-28 04:43:20 +05:30
|
|
|
br.Avatar = gotemplate.HTML(avatar)
|
|
|
|
br.RepoLink = repoLink
|
|
|
|
br.PartSha = part.Sha
|
|
|
|
br.PreviousSha = previousSha
|
|
|
|
br.PreviousShaURL = fmt.Sprintf("%s/blame/commit/%s/%s", repoLink, previousSha, ctx.Repo.TreePath)
|
|
|
|
br.CommitURL = fmt.Sprintf("%s/commit/%s", repoLink, part.Sha)
|
|
|
|
br.CommitMessage = html.EscapeString(commit.CommitMessage)
|
|
|
|
br.CommitSince = commitSince
|
2019-04-20 08:17:00 +05:30
|
|
|
}
|
|
|
|
|
2019-05-12 01:57:39 +05:30
|
|
|
if i != len(lines)-1 {
|
2019-04-20 08:17:00 +05:30
|
|
|
line += "\n"
|
|
|
|
}
|
2020-07-01 03:04:03 +05:30
|
|
|
fileName := fmt.Sprintf("%v", ctx.Data["FileName"])
|
|
|
|
line = highlight.Code(fileName, line)
|
2021-06-28 04:43:20 +05:30
|
|
|
|
|
|
|
br.Code = gotemplate.HTML(line)
|
|
|
|
rows = append(rows, br)
|
2019-04-20 08:17:00 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-28 04:43:20 +05:30
|
|
|
ctx.Data["BlameRows"] = rows
|
|
|
|
ctx.Data["CommitCnt"] = commitCnt
|
2019-04-20 08:17:00 +05:30
|
|
|
}
|