From e02baca55c0a3ed6a806f276c8e3cf2995a88967 Mon Sep 17 00:00:00 2001 From: Gusted Date: Thu, 18 Jan 2024 16:48:48 +0100 Subject: [PATCH] [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! --- routers/web/repo/blame.go | 63 +++++++++++---------------------------- 1 file changed, 17 insertions(+), 46 deletions(-) diff --git a/routers/web/repo/blame.go b/routers/web/repo/blame.go index d414779a14..fa9dd8a08c 100644 --- a/routers/web/repo/blame.go +++ b/routers/web/repo/blame.go @@ -8,7 +8,6 @@ import ( gotemplate "html/template" "net/http" "net/url" - "strconv" "strings" user_model "code.gitea.io/gitea/models/user" @@ -39,32 +38,15 @@ type blameRow struct { // RefBlame render blame page func RefBlame(ctx *context.Context) { - fileName := ctx.Repo.TreePath - if len(fileName) == 0 { - ctx.NotFound("Blame FileName", nil) + if ctx.Repo.TreePath == "" { + ctx.NotFound("No file specified", 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 { - treeLink += "/" + util.PathEscapeSegments(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] - } + treeNames := strings.Split(ctx.Repo.TreePath, "/") + for i := range treeNames { + paths = append(paths, strings.Join(treeNames[:i+1], "/")) } // Get current entry user currently looking at. @@ -73,47 +55,35 @@ func RefBlame(ctx *context.Context) { HandleGitError(ctx, "Repo.Commit.GetTreeEntryByPath", err) return } - 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["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["FileName"] = blob.Name() - ctx.Data["NumLines"], err = blob.GetBlobLineCount() ctx.Data["NumLinesSet"] = true - + ctx.Data["NumLines"], err = blob.GetBlobLineCount() if err != nil { - ctx.NotFound("GetBlobLineCount", err) + ctx.ServerError("GetBlobLineCount", err) return } - bypassBlameIgnore, _ := strconv.ParseBool(ctx.FormString("bypass-blame-ignore")) - - result, err := performBlame(ctx, ctx.Repo.Repository.RepoPath(), ctx.Repo.Commit, fileName, bypassBlameIgnore) + result, err := performBlame(ctx, ctx.Repo.Commit, ctx.Repo.TreePath, ctx.FormBool("bypass-blame-ignore")) if err != nil { - ctx.NotFound("CreateBlameReader", err) + ctx.ServerError("performBlame", err) return } ctx.Data["UsesIgnoreRevs"] = result.UsesIgnoreRevs ctx.Data["FaultyIgnoreRevsFile"] = result.FaultyIgnoreRevsFile - // Get Topics of this repo - renderRepoTopics(ctx) - if ctx.Written() { - return - } - commitNames := processBlameParts(ctx, result.Parts) if ctx.Written() { return @@ -130,12 +100,13 @@ type blameResult struct { 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() if err != nil { - ctx.NotFound("CreateBlameReader", err) return nil, err } + blameReader, err := git.CreateBlameReader(ctx, objectFormat, repoPath, commit, file, bypassBlameIgnore) if err != nil { return nil, err