Documentation
¶
Overview ¶
Package dirdiff provides utilities for computing unified diffs between two directory trees.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DiffOption ¶
type DiffOption func(*diffConfig)
DiffOption is a functional option for configuring DiffDirs behavior.
func WithContextLines ¶
func WithContextLines(n int) DiffOption
WithContextLines sets the number of unchanged context lines shown around each changed hunk. The default is 3, matching the conventional diff -u behavior. Negative values are clamped to 0.
func WithMaxBinaryScanBytes ¶
func WithMaxBinaryScanBytes(n int) DiffOption
WithMaxBinaryScanBytes sets the maximum number of bytes to scan when detecting binary content. Only the first N bytes of each file are checked for NUL bytes. The default is 8192. Values less than 1 are clamped to 1.
type DiffResult ¶
type DiffResult struct {
// Files contains one entry for each file that differs between the two directories.
Files []FileDiff `json:"files"`
}
DiffResult holds the complete diff between two directory trees.
func DiffDirs ¶
func DiffDirs(fs opctx.FS, dirA, dirB string, opts ...DiffOption) (*DiffResult, error)
DiffDirs computes a recursive unified diff between two directory trees on the provided filesystem. dirA is treated as the "original" (before) tree and dirB as the "modified" (after) tree. Regular files are compared by content, symlinks by target path, and other special entries by existence only. The returned DiffResult contains one FileDiff entry per changed file, sorted by relative path.
Note: all regular files are read entirely into memory for diffing. This is designed for small-to-moderate file trees (spec files, patches, overlay sources) — not for diffing large binary archives.
func (*DiffResult) ColorString ¶
func (r *DiffResult) ColorString() string
ColorString renders the complete diff result with ANSI color codes for terminal display. File headers (--- / +++) are bold, hunk headers (@@ ... @@) are cyan, added lines (+) are green, removed lines (-) are red, and context lines are uncolored. Returns an empty string when there are no differences.
func (*DiffResult) String ¶
func (r *DiffResult) String() string
String renders the complete diff result as a single unified diff string. Returns an empty string when there are no differences.
type FileDiff ¶
type FileDiff struct {
// Path is the file path relative to the compared directory roots.
Path string
// Status indicates whether the file was modified, added, removed, or type-changed.
Status FileStatus
// IsBinary is true when the file appears to contain binary content. When true,
// [UnifiedDiff] will be empty and [Message] will contain a descriptive message.
IsBinary bool
// UnifiedDiff holds the complete unified diff text for this file, including
// file headers (--- / +++) and hunk headers (@@ ... @@). Empty for binary files
// and special files where [Message] is set instead.
UnifiedDiff string
// Message holds a human-readable description for non-diffable entries (binary files,
// special files, empty files). Empty for normal text diffs where [UnifiedDiff] carries the diff content.
Message string
}
FileDiff holds the diff information for a single file.
func (FileDiff) MarshalJSON ¶
MarshalJSON implements json.Marshaler for FileDiff. It serializes the file diff as a structured JSON object, parsing the [UnifiedDiff] text into per-line change records.
type FileStatus ¶
type FileStatus string
FileStatus describes whether a file was added, removed, modified, or changed type relative to the original (pre-overlay) directory.
const ( // FileStatusModified indicates a file exists in both directories but with different content. FileStatusModified FileStatus = "modified" // FileStatusAdded indicates a file exists only in the "after" (overlaid) directory. FileStatusAdded FileStatus = "added" // FileStatusRemoved indicates a file exists only in the "before" (original) directory. FileStatusRemoved FileStatus = "removed" // FileStatusTypeChanged indicates a file exists in both directories but changed its // filesystem type (e.g., from symlink to regular file or vice versa). FileStatusTypeChanged FileStatus = "type-changed" )