mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-26 06:16:56 +09:00
fix(diff): load both blobs of renamed files for diff rendering (#39969)
When opening a pull request that renamed a CSV file: the compare page fails with a 500. Fix: load the left blob for every file that was not added, and the right blob (from `Name`) for every file that was not deleted. That matches what the templates did before #38517. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
2 files changed
+40
-3
No files matched your search
@@ -522,13 +522,13 @@ func (diffFile *DiffFile) prepareDiffRenderDetail(ctx context.Context, gitRepo *
|
||||
// * for "text" type: need to read up to "highlight limit size" to do full-file-highlighting
|
||||
contentLimit := util.Iif(diffFile.IsBin, typesniffer.SniffContentSize, MaxFullFileHighlightSizeLimit)
|
||||
var leftBlobType, rightBlobType typesniffer.SniffedType
|
||||
if (diffFile.Type == DiffFileDel || diffFile.Type == DiffFileChange) && leftCommit != nil {
|
||||
if diffFile.Type != DiffFileAdd && leftCommit != nil {
|
||||
c := getCommitFileBlobAndLimitedContent(ctx, gitRepo, leftCommit, diffFile.OldName, contentLimit)
|
||||
diffFile.LeftBlob, diffFile.LeftBlobSize, ret.leftLineCount, ret.leftContent = c.gitBlob, c.blobSize, c.lineCount, c.limitedContent
|
||||
leftBlobType = typesniffer.DetectContentType(ret.leftContent.buf.Bytes())
|
||||
}
|
||||
if (diffFile.Type == DiffFileAdd || diffFile.Type == DiffFileChange) && rightCommit != nil {
|
||||
c := getCommitFileBlobAndLimitedContent(ctx, gitRepo, rightCommit, diffFile.OldName, contentLimit)
|
||||
if diffFile.Type != DiffFileDel && rightCommit != nil {
|
||||
c := getCommitFileBlobAndLimitedContent(ctx, gitRepo, rightCommit, diffFile.Name, contentLimit)
|
||||
diffFile.RightBlob, diffFile.RightBlobSize, ret.rightLineCount, ret.rightContent = c.gitBlob, c.blobSize, c.lineCount, c.limitedContent
|
||||
rightBlobType = typesniffer.DetectContentType(ret.rightContent.buf.Bytes())
|
||||
}
|
||||
|
||||
@@ -116,3 +116,40 @@ func TestGetDiffForRender(t *testing.T) {
|
||||
assert.Equal(t, exp, actual, "idx=%d", idx)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDiffForRenderRenamedFile(t *testing.T) {
|
||||
repoDir := filepath.Join(t.TempDir(), "temp-repo")
|
||||
require.NoError(t, git.InitRepositoryLocal(t.Context(), repoDir, false, git.Sha1ObjectFormat.Name(), ""))
|
||||
|
||||
gitRepo, err := git.OpenRepositoryLocal(t.Context(), repoDir)
|
||||
require.NoError(t, err)
|
||||
defer gitRepo.Close()
|
||||
|
||||
content := "id,name\n1,Alice\n2,Bob\n"
|
||||
require.NoError(t, git.ForceFastImport(t.Context(), gitRepo, []git.FastImportCommit{
|
||||
{Ref: "refs/heads/b1", Files: []git.FastImportFile{{Path: "original/sample.csv", Content: content}}},
|
||||
{Ref: "refs/heads/b2", Files: []git.FastImportFile{{Path: "moved/sample.csv", Content: content}}},
|
||||
}))
|
||||
|
||||
beforeCommit, err := gitRepo.GetBranchCommit(t.Context(), "b1")
|
||||
require.NoError(t, err)
|
||||
afterCommit, err := gitRepo.GetBranchCommit(t.Context(), "b2")
|
||||
require.NoError(t, err)
|
||||
|
||||
diff, err := GetDiffForRender(t.Context(), "/any/repo-link", gitRepo, &DiffOptions{
|
||||
BeforeCommitID: beforeCommit.ID.String(),
|
||||
AfterCommitID: afterCommit.ID.String(),
|
||||
MaxLines: setting.Git.MaxGitDiffLines,
|
||||
MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters,
|
||||
MaxFiles: setting.Git.MaxGitDiffFiles,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, diff.Files, 1)
|
||||
|
||||
// the CSV diff of a renamed file needs both blobs, otherwise it panics
|
||||
diffFile := diff.Files[0]
|
||||
assert.Equal(t, DiffFileRename, diffFile.Type)
|
||||
assert.True(t, diffFile.IsBlobTypeCsv)
|
||||
assert.NotNil(t, diffFile.LeftBlob)
|
||||
assert.NotNil(t, diffFile.RightBlob)
|
||||
}
|
||||
Reference in new issue
Block a user