From 5cb1037cb75fee013c0b657c585263148b52f1cf Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 17 Sep 2022 18:53:04 +0800 Subject: [PATCH] Fix CSV diff for added/deleted files (#21189) (#21193) Backport #21189 Fixes #21184 Regression of #19552 Instead of using `GetBlobByPath`, use the already existing instances. --- routers/web/repo/compare.go | 36 +++++++++++++++---------------- templates/repo/diff/box.tmpl | 2 +- templates/repo/diff/csv_diff.tmpl | 2 +- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 5c46882f3d..903a2e8ede 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -113,17 +113,17 @@ func setCsvCompareContext(ctx *context.Context) { Error string } - ctx.Data["CreateCsvDiff"] = func(diffFile *gitdiff.DiffFile, baseCommit, headCommit *git.Commit) CsvDiffResult { - if diffFile == nil || baseCommit == nil || headCommit == nil { + ctx.Data["CreateCsvDiff"] = func(diffFile *gitdiff.DiffFile, baseBlob, headBlob *git.Blob) CsvDiffResult { + if diffFile == nil { return CsvDiffResult{nil, ""} } errTooLarge := errors.New(ctx.Locale.Tr("repo.error.csv.too_large")) - csvReaderFromCommit := func(ctx *markup.RenderContext, c *git.Commit) (*csv.Reader, io.Closer, error) { - blob, err := c.GetBlobByPath(diffFile.Name) - if err != nil { - return nil, nil, err + csvReaderFromCommit := func(ctx *markup.RenderContext, blob *git.Blob) (*csv.Reader, io.Closer, error) { + if blob == nil { + // It's ok for blob to be nil (file added or deleted) + return nil, nil, nil } if setting.UI.CSV.MaxFileSize != 0 && setting.UI.CSV.MaxFileSize < blob.Size() { @@ -139,28 +139,28 @@ func setCsvCompareContext(ctx *context.Context) { return csvReader, reader, err } - baseReader, baseBlobCloser, err := csvReaderFromCommit(&markup.RenderContext{Ctx: ctx, RelativePath: diffFile.OldName}, baseCommit) + baseReader, baseBlobCloser, err := csvReaderFromCommit(&markup.RenderContext{Ctx: ctx, RelativePath: diffFile.OldName}, baseBlob) if baseBlobCloser != nil { defer baseBlobCloser.Close() } - if err == errTooLarge { - return CsvDiffResult{nil, err.Error()} - } if err != nil { - log.Error("CreateCsvDiff error whilst creating baseReader from file %s in commit %s in %s: %v", diffFile.Name, baseCommit.ID.String(), ctx.Repo.Repository.Name, err) - return CsvDiffResult{nil, "unable to load file from base commit"} + if err == errTooLarge { + return CsvDiffResult{nil, err.Error()} + } + log.Error("error whilst creating csv.Reader from file %s in base commit %s in %s: %v", diffFile.Name, baseBlob.ID.String(), ctx.Repo.Repository.Name, err) + return CsvDiffResult{nil, "unable to load file"} } - headReader, headBlobCloser, err := csvReaderFromCommit(&markup.RenderContext{Ctx: ctx, RelativePath: diffFile.Name}, headCommit) + headReader, headBlobCloser, err := csvReaderFromCommit(&markup.RenderContext{Ctx: ctx, RelativePath: diffFile.Name}, headBlob) if headBlobCloser != nil { defer headBlobCloser.Close() } - if err == errTooLarge { - return CsvDiffResult{nil, err.Error()} - } if err != nil { - log.Error("CreateCsvDiff error whilst creating headReader from file %s in commit %s in %s: %v", diffFile.Name, headCommit.ID.String(), ctx.Repo.Repository.Name, err) - return CsvDiffResult{nil, "unable to load file from head commit"} + if err == errTooLarge { + return CsvDiffResult{nil, err.Error()} + } + log.Error("error whilst creating csv.Reader from file %s in head commit %s in %s: %v", diffFile.Name, headBlob.ID.String(), ctx.Repo.Repository.Name, err) + return CsvDiffResult{nil, "unable to load file"} } sections, err := gitdiff.CreateCsvDiff(diffFile, baseReader, headReader) diff --git a/templates/repo/diff/box.tmpl b/templates/repo/diff/box.tmpl index 2fbc5b0e10..98669ce0cb 100644 --- a/templates/repo/diff/box.tmpl +++ b/templates/repo/diff/box.tmpl @@ -153,7 +153,7 @@ {{if $isImage}} {{template "repo/diff/image_diff" dict "file" . "root" $ "blobBase" $blobBase "blobHead" $blobHead}} {{else}} - {{template "repo/diff/csv_diff" dict "file" . "root" $}} + {{template "repo/diff/csv_diff" dict "file" . "root" $ "blobBase" $blobBase "blobHead" $blobHead}} {{end}} diff --git a/templates/repo/diff/csv_diff.tmpl b/templates/repo/diff/csv_diff.tmpl index a92ef79301..0f46da306e 100644 --- a/templates/repo/diff/csv_diff.tmpl +++ b/templates/repo/diff/csv_diff.tmpl @@ -1,6 +1,6 @@ - {{$result := call .root.CreateCsvDiff .file .root.BaseCommit .root.HeadCommit}} + {{$result := call .root.CreateCsvDiff .file .blobBase .blobHead}} {{if $result.Error}}
{{$result.Error}}
{{else if $result.Sections}}