From 58ee9fdc4a217ea145ec60dd524953fff7d91743 Mon Sep 17 00:00:00 2001 From: Gusted Date: Sun, 18 Aug 2024 23:26:41 +0200 Subject: [PATCH] feat: Improve diff being generated Add `DiffCleanupSemantic` into the mix when generated diffs (PR review, commit view and issue/comment history). This avoids trying to produce a optimal diff and tries to reduce the amount of edits, by combing them into larger edits, which is nicer and easier to 'look at'. There's no need for a perfect minimal diff, as the output isn't being parsed by a computer, it's parsed by people. Ref: https://codeberg.org/forgejo/forgejo/issues/4996 --- routers/web/repo/issue_content_history.go | 1 + services/gitdiff/highlightdiff.go | 1 + 2 files changed, 2 insertions(+) diff --git a/routers/web/repo/issue_content_history.go b/routers/web/repo/issue_content_history.go index 31d2de6d53..16b250abda 100644 --- a/routers/web/repo/issue_content_history.go +++ b/routers/web/repo/issue_content_history.go @@ -154,6 +154,7 @@ func GetContentHistoryDetail(ctx *context.Context) { dmp := diffmatchpatch.New() // `checklines=false` makes better diff result diff := dmp.DiffMain(prevHistoryContentText, history.ContentText, false) + diff = dmp.DiffCleanupSemantic(diff) diff = dmp.DiffCleanupEfficiency(diff) // use chroma to render the diff html diff --git a/services/gitdiff/highlightdiff.go b/services/gitdiff/highlightdiff.go index 99313c5f36..c72959ea16 100644 --- a/services/gitdiff/highlightdiff.go +++ b/services/gitdiff/highlightdiff.go @@ -97,6 +97,7 @@ func (hcd *HighlightCodeDiff) diffWithHighlight(filename, language, codeA, codeB convertedCodeB := hcd.ConvertToPlaceholders(string(highlightCodeB)) diffs := diffMatchPatch.DiffMain(convertedCodeA, convertedCodeB, true) + diffs = diffMatchPatch.DiffCleanupSemantic(diffs) diffs = diffMatchPatch.DiffCleanupEfficiency(diffs) for i := range diffs {