[GITEA] Simplify blame handler
- Remove unnecessary checks for `ctx.Repo.TreePath`, because it will
already early-return if it's empty.
- Simplify `performBlame` to extract the repoPath from the context.
- Don't render the topics, as they are not shown in the blame
page (there's a condition in the template for the blame page).
- Fix that `performBlame` doesn't call `NotFound`, it should instead
only return the error.
- Fix that the error handlings call `ServerError` instead of `NotFound`.
- Simplify `bypass-blame-ignore` to use `ctx.FormBool`.
- Remove `TreeLink`, `HasParentPath` and `ParentPath` as it's not used
in the blame template.
- Inline `BranchLink` and `RawFileLink` string operations.
- Move around `NumLines` to make it clear the error is handled.
- Less code, less things the code does, more readable and thus more
maintanable!
(cherry picked from commit e02baca55c
)
This commit is contained in:
parent
2824ae4cf2
commit
74e00620ca
1 changed files with 17 additions and 46 deletions
|
@ -8,7 +8,6 @@ import (
|
||||||
gotemplate "html/template"
|
gotemplate "html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
user_model "code.gitea.io/gitea/models/user"
|
user_model "code.gitea.io/gitea/models/user"
|
||||||
|
@ -39,32 +38,15 @@ type blameRow struct {
|
||||||
|
|
||||||
// RefBlame render blame page
|
// RefBlame render blame page
|
||||||
func RefBlame(ctx *context.Context) {
|
func RefBlame(ctx *context.Context) {
|
||||||
fileName := ctx.Repo.TreePath
|
if ctx.Repo.TreePath == "" {
|
||||||
if len(fileName) == 0 {
|
ctx.NotFound("No file specified", nil)
|
||||||
ctx.NotFound("Blame FileName", nil)
|
|
||||||
return
|
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 {
|
|
||||||
treeLink += "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
|
|
||||||
}
|
|
||||||
|
|
||||||
var treeNames []string
|
|
||||||
paths := make([]string, 0, 5)
|
paths := make([]string, 0, 5)
|
||||||
if len(ctx.Repo.TreePath) > 0 {
|
treeNames := strings.Split(ctx.Repo.TreePath, "/")
|
||||||
treeNames = strings.Split(ctx.Repo.TreePath, "/")
|
for i := range treeNames {
|
||||||
for i := range treeNames {
|
paths = append(paths, strings.Join(treeNames[:i+1], "/"))
|
||||||
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.
|
// Get current entry user currently looking at.
|
||||||
|
@ -73,47 +55,35 @@ func RefBlame(ctx *context.Context) {
|
||||||
HandleGitError(ctx, "Repo.Commit.GetTreeEntryByPath", err)
|
HandleGitError(ctx, "Repo.Commit.GetTreeEntryByPath", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
blob := entry.Blob()
|
blob := entry.Blob()
|
||||||
|
|
||||||
ctx.Data["Paths"] = paths
|
|
||||||
ctx.Data["TreeLink"] = treeLink
|
|
||||||
ctx.Data["TreeNames"] = treeNames
|
|
||||||
ctx.Data["BranchLink"] = branchLink
|
|
||||||
|
|
||||||
ctx.Data["RawFileLink"] = rawLink + "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
|
|
||||||
ctx.Data["PageIsViewCode"] = true
|
ctx.Data["PageIsViewCode"] = true
|
||||||
|
|
||||||
ctx.Data["IsBlame"] = true
|
ctx.Data["IsBlame"] = true
|
||||||
|
|
||||||
|
ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()
|
||||||
|
ctx.Data["RawFileLink"] = ctx.Repo.RepoLink + "/raw/" + ctx.Repo.BranchNameSubURL() + "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
|
||||||
|
ctx.Data["Paths"] = paths
|
||||||
|
ctx.Data["TreeNames"] = treeNames
|
||||||
|
|
||||||
ctx.Data["FileSize"] = blob.Size()
|
ctx.Data["FileSize"] = blob.Size()
|
||||||
ctx.Data["FileName"] = blob.Name()
|
ctx.Data["FileName"] = blob.Name()
|
||||||
|
|
||||||
ctx.Data["NumLines"], err = blob.GetBlobLineCount()
|
|
||||||
ctx.Data["NumLinesSet"] = true
|
ctx.Data["NumLinesSet"] = true
|
||||||
|
ctx.Data["NumLines"], err = blob.GetBlobLineCount()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.NotFound("GetBlobLineCount", err)
|
ctx.ServerError("GetBlobLineCount", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
bypassBlameIgnore, _ := strconv.ParseBool(ctx.FormString("bypass-blame-ignore"))
|
result, err := performBlame(ctx, ctx.Repo.Commit, ctx.Repo.TreePath, ctx.FormBool("bypass-blame-ignore"))
|
||||||
|
|
||||||
result, err := performBlame(ctx, ctx.Repo.Repository.RepoPath(), ctx.Repo.Commit, fileName, bypassBlameIgnore)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.NotFound("CreateBlameReader", err)
|
ctx.ServerError("performBlame", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.Data["UsesIgnoreRevs"] = result.UsesIgnoreRevs
|
ctx.Data["UsesIgnoreRevs"] = result.UsesIgnoreRevs
|
||||||
ctx.Data["FaultyIgnoreRevsFile"] = result.FaultyIgnoreRevsFile
|
ctx.Data["FaultyIgnoreRevsFile"] = result.FaultyIgnoreRevsFile
|
||||||
|
|
||||||
// Get Topics of this repo
|
|
||||||
renderRepoTopics(ctx)
|
|
||||||
if ctx.Written() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
commitNames := processBlameParts(ctx, result.Parts)
|
commitNames := processBlameParts(ctx, result.Parts)
|
||||||
if ctx.Written() {
|
if ctx.Written() {
|
||||||
return
|
return
|
||||||
|
@ -130,12 +100,13 @@ type blameResult struct {
|
||||||
FaultyIgnoreRevsFile bool
|
FaultyIgnoreRevsFile bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func performBlame(ctx *context.Context, repoPath string, commit *git.Commit, file string, bypassBlameIgnore bool) (*blameResult, error) {
|
func performBlame(ctx *context.Context, commit *git.Commit, file string, bypassBlameIgnore bool) (*blameResult, error) {
|
||||||
|
repoPath := ctx.Repo.Repository.RepoPath()
|
||||||
objectFormat, err := ctx.Repo.GitRepo.GetObjectFormat()
|
objectFormat, err := ctx.Repo.GitRepo.GetObjectFormat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.NotFound("CreateBlameReader", err)
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
blameReader, err := git.CreateBlameReader(ctx, objectFormat, repoPath, commit, file, bypassBlameIgnore)
|
blameReader, err := git.CreateBlameReader(ctx, objectFormat, repoPath, commit, file, bypassBlameIgnore)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
Loading…
Add table
Reference in a new issue