chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 13:00:08 +08:00
commit 6345505628
1516 changed files with 473769 additions and 0 deletions
+214
View File
@@ -0,0 +1,214 @@
// Package diff computes a line-level diff between two versions of a file and
// renders it as a unified diff, with added/removed line counts. It is a pure
// leaf package: the writer tools use it to preview a pending change (the new
// content a write_file / edit_file / multi_edit would produce) without touching
// disk, so a front-end can show an approval card or a changed-files panel before
// the call runs.
package diff
import (
"strconv"
"strings"
udiff "github.com/aymanbagabas/go-udiff"
)
// Kind classifies what a change does to a file's existence, so a UI can label
// it ("new file", "modified", "deleted") without diffing to find out.
type Kind string
const (
// Create is a write to a path that did not previously exist.
Create Kind = "create"
// Modify edits or overwrites an existing file.
Modify Kind = "modify"
// Delete empties an existing file to nothing.
Delete Kind = "delete"
)
// Change is a previewed (not-yet-applied) edit to one file: the before/after
// text, the unified diff between them, and the line tallies a UI shows as
// "+N / -M". Binary is set when either side looks non-textual, in which case
// Diff is left empty (a byte-diff would be noise).
type Change struct {
Path string `json:"path"`
Kind Kind `json:"kind"`
OldText string `json:"old_text"`
NewText string `json:"new_text"`
Added int `json:"added"` // lines present in new but not old
Removed int `json:"removed"` // lines present in old but not new
Diff string `json:"diff"` // unified diff; "" when Binary
Binary bool `json:"binary"`
Mode string `json:"mode,omitempty"` // optional render mode metadata
Hunks int `json:"hunks,omitempty"` // unified diff hunk count when rendered
}
type OutputMode string
const (
OutputModePatch OutputMode = "patch"
OutputModePreview OutputMode = "preview"
)
type BuildOptions struct {
ContextLines int
OldLabel string
NewLabel string
Mode OutputMode
}
// defaultContext is how many unchanged lines surround each change in the
// unified diff, matching the conventional `diff -u` default.
const defaultContext = 3
// maxDiffEdits caps the changed window we will send to an exact line diff. A
// near-total rewrite of a large file is unreadable and can make LCS/Myers-style
// algorithms allocate heavily, so we fall back to tallies before calling the
// third-party renderer.
const maxDiffEdits = 2000
// Build computes the Change from old to new text for path. kind is supplied by
// the caller (it knows whether the file existed); Build fills the diff and the
// line tallies. When either side contains a NUL byte the file is treated as
// binary: the tallies are left zero and Diff is empty.
func Build(path, oldText, newText string, kind Kind) Change {
return BuildWithOptions(path, oldText, newText, kind, BuildOptions{ContextLines: -1})
}
// BuildWithOptions computes a Change like Build, with explicit render options
// for callers that need preview-style labels or non-default context lines.
func BuildWithOptions(path, oldText, newText string, kind Kind, opts BuildOptions) Change {
opts = normalizeBuildOptions(path, opts)
c := Change{Path: path, Kind: kind, OldText: oldText, NewText: newText}
if opts.Mode != "" && opts.Mode != OutputModePatch {
c.Mode = string(opts.Mode)
}
if isBinary(oldText) || isBinary(newText) {
c.Binary = true
return c
}
if oldText == newText {
return c // no-op change; empty diff, zero tallies
}
oldLines, _ := splitLines(oldText)
newLines, _ := splitLines(newText)
if exactDiffTooLarge(oldLines, newLines) {
c.Added, c.Removed = approxTally(oldLines, newLines)
c.Diff = "(diff omitted: change too large to render — +" + itoa(c.Added) + " / -" + itoa(c.Removed) + " lines)"
return c
}
edits := udiff.Lines(oldText, newText)
c.Added, c.Removed = tallyEdits(oldText, edits)
diff, err := udiff.ToUnified(opts.OldLabel, opts.NewLabel, oldText, edits, opts.ContextLines)
if err != nil {
c.Added, c.Removed = approxTally(oldLines, newLines)
c.Diff = "(diff omitted: failed to render — +" + itoa(c.Added) + " / -" + itoa(c.Removed) + " lines)"
return c
}
c.Diff = diff
c.Hunks = countUnifiedHunks(diff)
return c
}
func normalizeBuildOptions(path string, opts BuildOptions) BuildOptions {
if opts.ContextLines < 0 {
opts.ContextLines = defaultContext
}
if opts.Mode == "" {
opts.Mode = OutputModePatch
}
if opts.OldLabel == "" {
prefix := "a/"
if opts.Mode == OutputModePreview {
prefix = "before/"
}
opts.OldLabel = prefix + path
}
if opts.NewLabel == "" {
prefix := "b/"
if opts.Mode == OutputModePreview {
prefix = "after/"
}
opts.NewLabel = prefix + path
}
return opts
}
func countUnifiedHunks(diff string) int {
return strings.Count(diff, "\n@@ ")
}
func exactDiffTooLarge(oldLines, newLines []string) bool {
return changedWindowSize(oldLines, newLines) > maxDiffEdits
}
func changedWindowSize(oldLines, newLines []string) int {
start := 0
for start < len(oldLines) && start < len(newLines) && oldLines[start] == newLines[start] {
start++
}
oldEnd := len(oldLines)
newEnd := len(newLines)
for oldEnd > start && newEnd > start && oldLines[oldEnd-1] == newLines[newEnd-1] {
oldEnd--
newEnd--
}
return (oldEnd - start) + (newEnd - start)
}
func tallyEdits(oldText string, edits []udiff.Edit) (added, removed int) {
for _, edit := range edits {
if edit.Start >= 0 && edit.End >= edit.Start && edit.End <= len(oldText) {
removed += logicalLineCount(oldText[edit.Start:edit.End])
}
added += logicalLineCount(edit.New)
}
return added, removed
}
func logicalLineCount(s string) int {
lines, _ := splitLines(s)
return len(lines)
}
// approxTally counts added/removed lines by multiset difference — order-
// insensitive but O(n+m), used when the exact diff is skipped for being too large.
func approxTally(oldLines, newLines []string) (added, removed int) {
counts := make(map[string]int, len(oldLines))
for _, l := range oldLines {
counts[l]++
}
for _, l := range newLines {
if counts[l] > 0 {
counts[l]--
} else {
added++
}
}
for _, c := range counts {
removed += c
}
return added, removed
}
// isBinary reports whether s looks non-textual. A NUL byte never appears in
// UTF-8 text, so it is a cheap, reliable signal — the same heuristic git uses.
func isBinary(s string) bool { return strings.IndexByte(s, 0) >= 0 }
// splitLines breaks s into lines without their terminators and reports whether
// the text ended with a newline. An empty string yields no lines. A trailing
// newline does not produce a spurious empty final line.
func splitLines(s string) (lines []string, endsWithNewline bool) {
if s == "" {
return nil, true // vacuously: no missing-newline marker for empty content
}
endsWithNewline = strings.HasSuffix(s, "\n")
if endsWithNewline {
s = s[:len(s)-1]
}
return strings.Split(s, "\n"), endsWithNewline
}
func itoa(n int) string { return strconv.Itoa(n) }
+182
View File
@@ -0,0 +1,182 @@
package diff
import (
"strings"
"testing"
)
// --- splitLines ---
func TestSplitLinesEmpty(t *testing.T) {
lines, eol := splitLines("")
if len(lines) != 0 {
t.Errorf("empty: got %d lines", len(lines))
}
if !eol {
t.Error("empty should report endsWithNewline=true")
}
}
func TestSplitLinesNoTrailingNewline(t *testing.T) {
lines, eol := splitLines("a\nb")
if len(lines) != 2 {
t.Errorf("got %d lines", len(lines))
}
if lines[0] != "a" || lines[1] != "b" {
t.Errorf("lines = %v", lines)
}
if eol {
t.Error("should report no trailing newline")
}
}
func TestSplitLinesTrailingNewline(t *testing.T) {
lines, eol := splitLines("a\nb\n")
if len(lines) != 2 {
t.Errorf("got %d lines", len(lines))
}
if !eol {
t.Error("should report trailing newline")
}
}
func TestSplitLinesSingleLine(t *testing.T) {
lines, eol := splitLines("hello")
if len(lines) != 1 || lines[0] != "hello" {
t.Errorf("lines = %v", lines)
}
if eol {
t.Error("single line without newline should report false")
}
}
func TestSplitLinesSingleLineWithNewline(t *testing.T) {
lines, eol := splitLines("hello\n")
if len(lines) != 1 || lines[0] != "hello" {
t.Errorf("lines = %v", lines)
}
if !eol {
t.Error("single line with newline should report true")
}
}
// --- isBinary ---
func TestIsBinaryEmpty(t *testing.T) {
if isBinary("") {
t.Error("empty string is not binary")
}
}
func TestIsBinaryText(t *testing.T) {
if isBinary("hello world\nline 2") {
t.Error("plain text is not binary")
}
}
func TestIsBinaryNUL(t *testing.T) {
if !isBinary("hello\x00world") {
t.Error("string with NUL should be binary")
}
}
func TestIsBinaryNULAtEnd(t *testing.T) {
if !isBinary("hello\x00") {
t.Error("NUL at end should be binary")
}
}
// --- Build edge cases ---
func TestBuildBothEmpty(t *testing.T) {
c := Build("empty.txt", "", "", Modify)
if c.Diff != "" || c.Added != 0 || c.Removed != 0 {
t.Errorf("both empty should be no-op: %+v", c)
}
}
func TestBuildEmptyOldNonEmptyNew(t *testing.T) {
c := Build("new.txt", "", "line1\nline2\n", Create)
if c.Added != 2 || c.Removed != 0 {
t.Errorf("added/removed = %d/%d", c.Added, c.Removed)
}
if c.Kind != Create {
t.Errorf("kind = %q", c.Kind)
}
}
func TestBuildNonEmptyOldEmptyNew(t *testing.T) {
c := Build("del.txt", "line1\nline2\n", "", Delete)
if c.Added != 0 || c.Removed != 2 {
t.Errorf("added/removed = %d/%d", c.Added, c.Removed)
}
}
func TestBuildCRLF(t *testing.T) {
old := "line1\r\nline2\r\n"
neu := "line1\r\nLINE2\r\n"
c := Build("crlf.txt", old, neu, Modify)
// CRLF should be handled (split on \n, \r stays in line content).
if c.Added != 1 || c.Removed != 1 {
t.Errorf("added/removed = %d/%d, want 1/1", c.Added, c.Removed)
}
}
func TestBuildWhitespaceOnly(t *testing.T) {
old := "a\tb\n"
neu := "a b\n"
c := Build("ws.txt", old, neu, Modify)
if c.Added != 1 || c.Removed != 1 {
t.Errorf("added/removed = %d/%d", c.Added, c.Removed)
}
}
func TestBuildLargeFile(t *testing.T) {
// Build a large file with one changed line in the middle.
var oldB, newB strings.Builder
for i := 0; i < 1000; i++ {
oldB.WriteString("line\n")
if i == 500 {
newB.WriteString("CHANGED\n")
} else {
newB.WriteString("line\n")
}
}
c := Build("large.txt", oldB.String(), newB.String(), Modify)
if c.Added != 1 || c.Removed != 1 {
t.Errorf("added/removed = %d/%d, want 1/1", c.Added, c.Removed)
}
}
// --- Kind constants ---
func TestKindConstants(t *testing.T) {
if Create != "create" {
t.Errorf("Create = %q", Create)
}
if Modify != "modify" {
t.Errorf("Modify = %q", Modify)
}
if Delete != "delete" {
t.Errorf("Delete = %q", Delete)
}
}
// --- itoa ---
func TestItoa(t *testing.T) {
cases := []struct {
n int
want string
}{
{0, "0"},
{1, "1"},
{42, "42"},
{1000, "1000"},
}
for _, c := range cases {
if got := itoa(c.n); got != c.want {
t.Errorf("itoa(%d) = %q, want %q", c.n, got, c.want)
}
}
}
+168
View File
@@ -0,0 +1,168 @@
package diff
import (
"strings"
"testing"
)
func TestBuild_NoChange(t *testing.T) {
c := Build("f.txt", "a\nb\n", "a\nb\n", Modify)
if c.Diff != "" || c.Added != 0 || c.Removed != 0 {
t.Fatalf("identical content should yield empty diff, got %+v", c)
}
}
func TestBuild_Create(t *testing.T) {
c := Build("new.txt", "", "hello\nworld\n", Create)
if c.Added != 2 || c.Removed != 0 {
t.Fatalf("added/removed = %d/%d, want 2/0", c.Added, c.Removed)
}
if !strings.Contains(c.Diff, "@@ -0,0 +1,2 @@") {
t.Fatalf("create hunk header missing:\n%s", c.Diff)
}
if !strings.Contains(c.Diff, "+hello") || !strings.Contains(c.Diff, "+world") {
t.Fatalf("added lines missing:\n%s", c.Diff)
}
}
func TestBuild_DeleteAll(t *testing.T) {
c := Build("gone.txt", "x\ny\n", "", Delete)
if c.Added != 0 || c.Removed != 2 {
t.Fatalf("added/removed = %d/%d, want 0/2", c.Added, c.Removed)
}
if !strings.Contains(c.Diff, "@@ -1,2 +0,0 @@") {
t.Fatalf("delete hunk header missing:\n%s", c.Diff)
}
}
func TestBuild_ModifyMiddle(t *testing.T) {
old := "1\n2\n3\n4\n5\n"
neu := "1\n2\nThree\n4\n5\n"
c := Build("m.txt", old, neu, Modify)
if c.Added != 1 || c.Removed != 1 {
t.Fatalf("added/removed = %d/%d, want 1/1", c.Added, c.Removed)
}
if !strings.Contains(c.Diff, "-3") || !strings.Contains(c.Diff, "+Three") {
t.Fatalf("expected -3/+Three:\n%s", c.Diff)
}
// 3 lines of context each side, but the file only has 2 before and 2 after.
if !strings.Contains(c.Diff, " 1") || !strings.Contains(c.Diff, " 5") {
t.Fatalf("context lines missing:\n%s", c.Diff)
}
}
func TestBuildWithOptionsPreviewLabelsAndContext(t *testing.T) {
old := "1\n2\n3\n4\n5\n"
neu := "1\n2\nThree\n4\n5\n"
c := BuildWithOptions("m.txt", old, neu, Modify, BuildOptions{ContextLines: 0, Mode: OutputModePreview})
if c.Mode != string(OutputModePreview) {
t.Fatalf("Mode = %q", c.Mode)
}
if !strings.Contains(c.Diff, "--- before/m.txt") || !strings.Contains(c.Diff, "+++ after/m.txt") {
t.Fatalf("preview labels missing:\n%s", c.Diff)
}
if strings.Contains(c.Diff, " 1") || strings.Contains(c.Diff, " 5") {
t.Fatalf("zero-context diff leaked unchanged context:\n%s", c.Diff)
}
if c.Hunks != 1 {
t.Fatalf("Hunks = %d, want 1:\n%s", c.Hunks, c.Diff)
}
}
func TestBuildWithOptionsCustomLabels(t *testing.T) {
c := BuildWithOptions("x.txt", "old\n", "new\n", Modify, BuildOptions{
ContextLines: 1,
OldLabel: "left",
NewLabel: "right",
})
if !strings.Contains(c.Diff, "--- left") || !strings.Contains(c.Diff, "+++ right") {
t.Fatalf("custom labels missing:\n%s", c.Diff)
}
}
func TestBuild_Prepend(t *testing.T) {
c := Build("p.txt", "b\nc\n", "a\nb\nc\n", Modify)
if c.Added != 1 || c.Removed != 0 {
t.Fatalf("added/removed = %d/%d, want 1/0", c.Added, c.Removed)
}
if !strings.Contains(c.Diff, "@@ -1,2 +1,3 @@") {
t.Fatalf("prepend header wrong:\n%s", c.Diff)
}
}
func TestBuild_TwoSeparateHunks(t *testing.T) {
// Two changes far enough apart (>2*context) to form distinct hunks.
old := "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\n"
neu := "a\nB\nc\nd\ne\nf\ng\nh\ni\nj\nK\nl\n"
c := Build("two.txt", old, neu, Modify)
if got := strings.Count(c.Diff, "@@ "); got != 2 {
t.Fatalf("expected 2 hunks, got %d:\n%s", got, c.Diff)
}
if c.Added != 2 || c.Removed != 2 {
t.Fatalf("added/removed = %d/%d, want 2/2", c.Added, c.Removed)
}
}
func TestBuild_AdjacentChangesMerge(t *testing.T) {
// Changes within 2*context collapse into one hunk.
old := "a\nb\nc\nd\ne\n"
neu := "a\nB\nc\nD\ne\n"
c := Build("adj.txt", old, neu, Modify)
if got := strings.Count(c.Diff, "@@ "); got != 1 {
t.Fatalf("expected 1 merged hunk, got %d:\n%s", got, c.Diff)
}
}
func TestBuild_NoNewlineAtEOF(t *testing.T) {
c := Build("nonl.txt", "a\nb", "a\nc", Modify)
if !strings.Contains(c.Diff, "\\ No newline at end of file") {
t.Fatalf("expected no-newline marker:\n%s", c.Diff)
}
}
func TestBuild_Binary(t *testing.T) {
c := Build("bin", "ok\n", "bad\x00data", Modify)
if !c.Binary {
t.Fatal("expected Binary=true")
}
if c.Diff != "" || c.Added != 0 || c.Removed != 0 {
t.Fatalf("binary change should carry no textual diff, got %+v", c)
}
}
// TestBuild_MinimalEditScript checks the third-party line diff keeps the same
// user-visible contract: a single line inserted into a run of identical lines
// must not be rendered as a block delete+insert.
func TestBuild_MinimalEditScript(t *testing.T) {
old := "x\nx\nx\n"
neu := "x\nx\ny\nx\n"
c := Build("min.txt", old, neu, Modify)
if c.Added != 1 || c.Removed != 0 {
t.Fatalf("added/removed = %d/%d, want 1/0 (minimal):\n%s", c.Added, c.Removed, c.Diff)
}
}
func TestChangedWindowSize(t *testing.T) {
oldLines, _ := splitLines("a\nb\nc\nd\ne\n")
newLines, _ := splitLines("a\nb\nC\nd\ne\n")
if got := changedWindowSize(oldLines, newLines); got != 2 {
t.Fatalf("single changed line window = %d, want 2", got)
}
oldLines, _ = splitLines("a\nb\nc\nd\ne\n")
newLines, _ = splitLines("a\nB\nc\nD\ne\n")
if got := changedWindowSize(oldLines, newLines); got != 6 {
t.Fatalf("separate changed lines window = %d, want 6", got)
}
}
func TestExactDiffTooLarge(t *testing.T) {
oldLines := make([]string, maxDiffEdits+1)
newLines := make([]string, maxDiffEdits+1)
for i := range oldLines {
oldLines[i] = "old"
newLines[i] = "new"
}
if !exactDiffTooLarge(oldLines, newLines) {
t.Fatal("large changed window should skip exact diff")
}
}
+71
View File
@@ -0,0 +1,71 @@
package diff
import (
"fmt"
"runtime"
"strings"
"testing"
"time"
)
// TestLargeRewriteBoundedCost proves a full rewrite of a large file no longer
// pays O(N²): the edit-distance cap skips the line-by-line render, so memory and
// time stay bounded while the tallies and an omitted-diff marker still report the
// change. Before the cap this allocated ~565 MB for 3000 lines (≈6 GB at 10k).
func TestLargeRewriteBoundedCost(t *testing.T) {
var oldB, newB strings.Builder
const n = 6000
for i := 0; i < n; i++ {
fmt.Fprintf(&oldB, "old line %d\n", i)
fmt.Fprintf(&newB, "totally different new line %d\n", i)
}
var m0, m1 runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&m0)
start := time.Now()
c := Build("big.txt", oldB.String(), newB.String(), Modify)
elapsed := time.Since(start)
runtime.ReadMemStats(&m1)
allocMB := float64(m1.TotalAlloc-m0.TotalAlloc) / (1 << 20)
t.Logf("2×%d-line rewrite: %v, %.1f MB, +%d/-%d", n, elapsed, allocMB, c.Added, c.Removed)
if allocMB > 150 {
t.Errorf("allocated %.1f MB — the edit-distance cap should bound this", allocMB)
}
if elapsed > time.Second {
t.Errorf("took %v — should be bounded", elapsed)
}
if c.Added != n || c.Removed != n {
t.Errorf("tallies wrong: +%d/-%d, want +%d/-%d", c.Added, c.Removed, n, n)
}
if !strings.Contains(c.Diff, "too large") {
t.Errorf("expected an omitted-diff marker, got %q", c.Diff)
}
}
// TestSmallEditOnLargeFileStillDiffs proves the cap doesn't punish the common
// case: a tiny edit in a big file converges far below the cap, so it keeps a real
// line-by-line diff regardless of file size.
func TestSmallEditOnLargeFileStillDiffs(t *testing.T) {
var oldB strings.Builder
const n = 8000
for i := 0; i < n; i++ {
fmt.Fprintf(&oldB, "line %d\n", i)
}
old := oldB.String()
updated := strings.Replace(old, "line 4000\n", "line 4000 EDITED\n", 1)
c := Build("big.txt", old, updated, Modify)
if c.Added != 1 || c.Removed != 1 {
t.Errorf("tallies = +%d/-%d, want +1/-1", c.Added, c.Removed)
}
if !strings.Contains(c.Diff, "line 4000 EDITED") || strings.Contains(c.Diff, "too large") {
t.Errorf("small edit on a large file should keep a real diff, got %q", firstN(c.Diff, 200))
}
}
func firstN(s string, n int) string {
if len(s) > n {
return s[:n]
}
return s
}