mirror of
https://github.com/go-gitea/gitea.git
synced 2026-12-03 21:32:25 -06:00
Markdown, org-mode and other inline-rendered markup files get a source/rendered toggle in commit, compare and pull request diffs, like images and CSV. Source stays the default, matching GitHub. The rendered diff is fetched when opened, and only offered when both the old and the new name are markup files. The rendered diff follows GitHub's rich diff: changed blocks get a colored bar, words are marked inline, formatting-only changes are underlined, code blocks are compared by line and tables by cell, with a whole replacement when the columns of a table or row change. Math, mermaid and emoji are shown as removed and added whole, and invisible characters are escaped like in the source diff. A diff that takes too long is reported like a suppressed source diff. Wiki diffs get no toggle, and the rendered diff tab in the web editor is left for a follow-up. | Change | Rendered diff | | --- | --- | | Source stays the default | <img width="600" alt="Source stays the default" src="https://github.com/user-attachments/assets/87b140a6-4228-4514-8806-6897aeace766" /> | | Paragraphs and words | <img width="600" alt="Paragraphs and words" src="https://github.com/user-attachments/assets/e9c3b969-fb54-43b8-a167-364a81959015" /> | | Formatting, links and tasks | <img width="600" alt="Formatting, links and tasks" src="https://github.com/user-attachments/assets/2582db39-8d28-44e1-bc19-1128f259f070" /> | | Lists | <img width="600" alt="Lists" src="https://github.com/user-attachments/assets/23868a95-da05-4c41-a329-b8a06b6a94ad" /> | | Tables | <img width="600" alt="Tables" src="https://github.com/user-attachments/assets/ac924a0c-33df-4d89-af2d-bb2d74309d17" /> | | Code blocks | <img width="600" alt="Code blocks" src="https://github.com/user-attachments/assets/4a756005-ebf8-418f-a5a5-50520ef2ac4f" /> | | Math and mermaid | <img width="600" alt="Math and mermaid" src="https://github.com/user-attachments/assets/d1c6c96f-e7f4-4ed2-9744-8677fda68bbe" /> | This supersedes https://github.com/go-gitea/gitea/pull/37167, whose review raised the block-level and math/mermaid cases. Fixes: https://github.com/go-gitea/gitea/issues/21633 Assisted-by: Claude Code:claude-opus-5-5 --------- Co-authored-by: bircni <bircni@icloud.com> Co-authored-by: silverwind <me@silverwind.io>
163 lines
5.2 KiB
Go
163 lines
5.2 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package gitdiff
|
|
|
|
import (
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.dev/modules/git"
|
|
"gitea.dev/modules/markup"
|
|
"gitea.dev/modules/setting"
|
|
|
|
_ "gitea.dev/modules/markup/markdown"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestGetDiffForRender(t *testing.T) {
|
|
repoDir := filepath.Join(t.TempDir(), "temp-repo")
|
|
require.NoError(t, git.InitRepositoryLocal(t.Context(), repoDir, false, git.Sha1ObjectFormat.Name(), ""))
|
|
|
|
contentLeft := strings.Repeat("a\n", 20) +
|
|
"mark1\n" +
|
|
strings.Repeat("b\n", 10) +
|
|
"mark2\n" +
|
|
strings.Repeat("c\n", 40) +
|
|
"mark3\n" +
|
|
strings.Repeat("a\n", 30)
|
|
contentRight := strings.Repeat("a\n", 20-1) +
|
|
"mark1-x\n" +
|
|
strings.Repeat("b\n", 10+3) +
|
|
"mark2-x\n" +
|
|
strings.Repeat("c\n", 40-5) +
|
|
"mark3-x\n" +
|
|
strings.Repeat("a\n", 30)
|
|
|
|
gitRepo, err := git.OpenRepositoryLocal(t.Context(), repoDir)
|
|
require.NoError(t, err)
|
|
defer gitRepo.Close()
|
|
|
|
require.NoError(t, git.ForceFastImport(t.Context(), gitRepo, []git.FastImportCommit{
|
|
{Ref: "refs/heads/b1", Files: []git.FastImportFile{{Path: "foo.txt", Content: contentLeft}}},
|
|
{Ref: "refs/heads/b2", Files: []git.FastImportFile{{Path: "foo.txt", Content: contentRight}}},
|
|
}))
|
|
|
|
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)
|
|
|
|
type section struct {
|
|
ExpandDirection string
|
|
|
|
LastLeftIdx int
|
|
LastRightIdx int
|
|
LeftIdx int
|
|
RightIdx int
|
|
LeftHunkSize int
|
|
RightHunkSize int
|
|
}
|
|
expectedSections := []section{
|
|
{
|
|
ExpandDirection: "up",
|
|
LeftIdx: 17,
|
|
RightIdx: 17,
|
|
LeftHunkSize: 8,
|
|
RightHunkSize: 7,
|
|
},
|
|
{
|
|
ExpandDirection: "single",
|
|
LastLeftIdx: 24,
|
|
LastRightIdx: 23,
|
|
LeftIdx: 29,
|
|
RightIdx: 28,
|
|
LeftHunkSize: 7,
|
|
RightHunkSize: 10,
|
|
},
|
|
{
|
|
ExpandDirection: "updown",
|
|
LastLeftIdx: 35,
|
|
LastRightIdx: 37,
|
|
LeftIdx: 65,
|
|
RightIdx: 67,
|
|
LeftHunkSize: 12,
|
|
RightHunkSize: 7,
|
|
},
|
|
{
|
|
ExpandDirection: "down",
|
|
LastLeftIdx: 76,
|
|
LastRightIdx: 73,
|
|
LeftIdx: 103, // left has 103 lines
|
|
RightIdx: 100, // right has 100 lines
|
|
},
|
|
}
|
|
for idx, exp := range expectedSections {
|
|
secLine := diff.Files[0].Sections[idx].Lines[0] // line 0 should be the "section info"
|
|
actual := section{
|
|
ExpandDirection: secLine.GetExpandDirection(),
|
|
LastLeftIdx: secLine.SectionInfo.LastLeftIdx,
|
|
LastRightIdx: secLine.SectionInfo.LastRightIdx,
|
|
LeftIdx: secLine.SectionInfo.LeftIdx,
|
|
RightIdx: secLine.SectionInfo.RightIdx,
|
|
LeftHunkSize: secLine.SectionInfo.LeftHunkSize,
|
|
RightHunkSize: secLine.SectionInfo.RightHunkSize,
|
|
}
|
|
assert.Equal(t, exp, actual, "idx=%d", idx)
|
|
}
|
|
}
|
|
|
|
func TestGetDiffForRenderRenamedFile(t *testing.T) {
|
|
markup.RefreshFileNamePatterns()
|
|
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}, {Path: "guide.md", Content: "# Guide\n"}, {Path: "notes.md", Content: "# Notes\n"}}},
|
|
{Ref: "refs/heads/b2", Files: []git.FastImportFile{{Path: "moved/sample.csv", Content: content}, {Path: "docs/guide.md", Content: "# Guide\n"}, {Path: "notes.txt", Content: "# Notes\n"}}},
|
|
}))
|
|
|
|
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,
|
|
EnableRichDiff: true,
|
|
})
|
|
require.NoError(t, err)
|
|
require.Len(t, diff.Files, 3)
|
|
assert.Equal(t, "/any/repo-link/rich_diff?after="+afterCommit.ID.String()+"&before="+beforeCommit.ID.String()+"&old_path=guide.md&path=docs%2Fguide.md", diff.Files[0].RichDiffLink)
|
|
assert.Empty(t, diff.Files[2].RichDiffLink)
|
|
|
|
// the CSV diff of a renamed file needs both blobs, otherwise it panics
|
|
diffFile := diff.Files[1]
|
|
assert.Equal(t, DiffFileRename, diffFile.Type)
|
|
assert.True(t, diffFile.IsBlobTypeCsv)
|
|
assert.NotNil(t, diffFile.LeftBlob)
|
|
assert.NotNil(t, diffFile.RightBlob)
|
|
}
|