diff

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

README

Unified Diff Parser

This package implements a parser for unified diff format (git/perforce style).

Supported Syntax

  • Git-style headers:

    • diff --git a/oldpath b/newpath
    • --- oldpath or --- /dev/null (new files)
    • +++ newpath
    • new file mode XXXX
  • Unified diff hunks:

    • @@ -oldStart,oldLen +newStart,newLen @@
    • Context lines (space-prefixed)
    • Removed lines (- prefix)
    • Added lines (+ prefix)

Line Classification

Added lines (+) are classified as either:

  • Modified: when they immediately follow removed lines (-) in the same hunk
  • Added: when they appear without preceding removed lines

Limitations

  • Only tracks new-file line numbers (post-change state)
  • Treats renames as modifications (uses NewPath only)
  • Requires valid hunk headers
  • No binary file support
  • Expects UTF-8 encoded text files

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Normalize

func Normalize(p string) string

Normalize converts a diff path into a normalized form: - Converts backslashes to forward slashes first - Strips "a/" and "b/" prefixes (git-style) - only once - Cleans the path (removes "." components, etc.)

Types

type DiffData

type DiffData struct {
	Files []FileDiff
}

DiffData represents a complete diff, potentially containing multiple files

func Parse

func Parse(path string, logger *slog.Logger) (*DiffData, error)

Parse reads a unified diff file and returns a structured representation. It handles standard git diffs, file additions, modifications, and hunks. It relies on internal/filereader to handle character encoding (UTF-16/BOM).

type FileDiff

type FileDiff struct {
	OldPath string // Original file path, "/dev/null" for new files
	NewPath string // New file path
	Kind    string // "added", "modified", or "renamed"
	Hunks   []Hunk
}

FileDiff represents changes to a single file

type Hunk

type Hunk struct {
	// Original file line information
	OldStart int // Starting line in old file
	OldLines int // Number of lines from old file

	// New file line information
	NewStart int // Starting line in new file
	NewLines int // Number of lines in new file

	// Line offsets from NewStart for added/modified lines
	AddedLineOffsets    []int // Lines that were added (no corresponding removal)
	ModifiedLineOffsets []int // Lines that replace removed lines
	// contains filtered or unexported fields
}

Hunk represents a single diff hunk with line offsets

Jump to

Keyboard shortcuts

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