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.
This commit is contained in:
wxiaoguang 2022-09-17 18:53:04 +08:00 committed by GitHub
parent 2dcea782c5
commit 5cb1037cb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 20 deletions

View File

@ -113,17 +113,17 @@ func setCsvCompareContext(ctx *context.Context) {
Error string Error string
} }
ctx.Data["CreateCsvDiff"] = func(diffFile *gitdiff.DiffFile, baseCommit, headCommit *git.Commit) CsvDiffResult { ctx.Data["CreateCsvDiff"] = func(diffFile *gitdiff.DiffFile, baseBlob, headBlob *git.Blob) CsvDiffResult {
if diffFile == nil || baseCommit == nil || headCommit == nil { if diffFile == nil {
return CsvDiffResult{nil, ""} return CsvDiffResult{nil, ""}
} }
errTooLarge := errors.New(ctx.Locale.Tr("repo.error.csv.too_large")) errTooLarge := errors.New(ctx.Locale.Tr("repo.error.csv.too_large"))
csvReaderFromCommit := func(ctx *markup.RenderContext, c *git.Commit) (*csv.Reader, io.Closer, error) { csvReaderFromCommit := func(ctx *markup.RenderContext, blob *git.Blob) (*csv.Reader, io.Closer, error) {
blob, err := c.GetBlobByPath(diffFile.Name) if blob == nil {
if err != nil { // It's ok for blob to be nil (file added or deleted)
return nil, nil, err return nil, nil, nil
} }
if setting.UI.CSV.MaxFileSize != 0 && setting.UI.CSV.MaxFileSize < blob.Size() { 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 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 { if baseBlobCloser != nil {
defer baseBlobCloser.Close() defer baseBlobCloser.Close()
} }
if err != nil {
if err == errTooLarge { if err == errTooLarge {
return CsvDiffResult{nil, err.Error()} return CsvDiffResult{nil, err.Error()}
} }
if err != nil { 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)
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"}
return CsvDiffResult{nil, "unable to load file from base commit"}
} }
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 { if headBlobCloser != nil {
defer headBlobCloser.Close() defer headBlobCloser.Close()
} }
if err != nil {
if err == errTooLarge { if err == errTooLarge {
return CsvDiffResult{nil, err.Error()} return CsvDiffResult{nil, err.Error()}
} }
if err != nil { 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)
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"}
return CsvDiffResult{nil, "unable to load file from head commit"}
} }
sections, err := gitdiff.CreateCsvDiff(diffFile, baseReader, headReader) sections, err := gitdiff.CreateCsvDiff(diffFile, baseReader, headReader)

View File

@ -153,7 +153,7 @@
{{if $isImage}} {{if $isImage}}
{{template "repo/diff/image_diff" dict "file" . "root" $ "blobBase" $blobBase "blobHead" $blobHead}} {{template "repo/diff/image_diff" dict "file" . "root" $ "blobBase" $blobBase "blobHead" $blobHead}}
{{else}} {{else}}
{{template "repo/diff/csv_diff" dict "file" . "root" $}} {{template "repo/diff/csv_diff" dict "file" . "root" $ "blobBase" $blobBase "blobHead" $blobHead}}
{{end}} {{end}}
</table> </table>
</div> </div>

View File

@ -1,6 +1,6 @@
<tr> <tr>
<td> <td>
{{$result := call .root.CreateCsvDiff .file .root.BaseCommit .root.HeadCommit}} {{$result := call .root.CreateCsvDiff .file .blobBase .blobHead}}
{{if $result.Error}} {{if $result.Error}}
<div class="ui center">{{$result.Error}}</div> <div class="ui center">{{$result.Error}}</div>
{{else if $result.Sections}} {{else if $result.Sections}}