diff

package
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 21, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

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, dependency-free 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.

The line diff uses Myers' O(ND) algorithm — the same shortest-edit-script approach git uses — so the hunks read the way a developer expects rather than a naive longest-common-subsequence walk.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Change

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"`
}

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).

func Build

func Build(path, oldText, newText string, kind Kind) Change

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.

type Kind

type Kind string

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.

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"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL