diff

package
v0.0.21 Latest Latest
Warning

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

Go to latest
Published: May 25, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package diff provides AST analysis capabilities for Go files in the diff. This enables semantic rules that can track cross-function type information.

Package diff parses unified git diffs into a structured form that rules can query. Only the subset of the format that git actually emits is supported: standard headers, hunk headers, add/delete/context lines, new-file and deleted-file markers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HasGoFiles added in v0.0.21

func HasGoFiles(d *Diff, includeTests bool) bool

HasGoFiles returns true if the diff contains any Go files (excluding test files unless includeTests is true).

func IsGoFile added in v0.0.21

func IsGoFile(path string, includeTests bool) bool

IsGoFile returns true if the path ends with .go and is not a test file (unless includeTests is true).

func ParseIgnoreFile

func ParseIgnoreFile(r io.Reader) ([]string, error)

ParseIgnoreFile reads a .slopgateignore file and returns the list of glob patterns. Blank lines and lines starting with '#' are ignored. Trailing whitespace is trimmed.

Types

type AnalysisResult added in v0.0.21

type AnalysisResult struct {
	// Files maps file path -> analysis. Only Go files that could be parsed
	// are included. Files that failed to parse have Error set.
	Files map[string]*FileAnalysis
	// GoFiles is the list of Go file paths in the diff, in order.
	GoFiles []string
}

AnalysisResult holds the AST analysis for all modified Go files in the diff.

func LoadASTAnalysis added in v0.0.21

func LoadASTAnalysis(d *Diff) *AnalysisResult

LoadASTAnalysis parses the added/changed Go files in the diff using the standard library's go/parser and go/types. This enables AST-aware rules that can track cross-function type information.

The AnalysisResult is keyed by the new file path (Path field of each File).

If a file cannot be parsed, it is included with a non-nil Error field so rules can handle partial failures gracefully.

type Diff

type Diff struct {
	Files []File

	RepoRoot         string // optional absolute worktree root for rules that inspect files
	Staged           bool   // true when the diff came from git diff --cached
	SnapshotRef      string // optional git snapshot source for the new side, e.g. HEAD or :
	SnapshotWorktree bool   // true when the new-side snapshot matches the live worktree
	// contains filtered or unexported fields
}

Diff is the top-level parsed representation of a unified diff.

func FilterIgnored

func FilterIgnored(d *Diff, patterns []string) *Diff

FilterIgnored returns a copy of d with any File whose path matches one of the glob patterns removed. Patterns follow the syntax of filepath.Match, with one extension: a leading "**/" matches any number of leading path segments — so "**/foo_test.go" matches "pkg/rules/foo_test.go" and "foo_test.go" alike.

If patterns is empty or nil, the input Diff is returned unchanged.

func Parse

func Parse(r io.Reader) (*Diff, error)

Parse reads a unified diff from r and returns a Diff. It is forgiving: unknown header lines are ignored. An empty input yields an empty Diff with no error.

func (*Diff) AllFiles added in v0.0.21

func (d *Diff) AllFiles() []File

AllFiles returns both active and ignored files so meta-rules can reason about companion test changes without re-exposing ignored files to normal scanners.

type File

type File struct {
	Path     string // new path (or old path if deleted)
	OldPath  string // path in the old revision
	IsNew    bool
	IsDelete bool
	Hunks    []Hunk
}

File represents a single file's changes within a diff.

func (File) AddedLines

func (f File) AddedLines() []Line

AddedLines returns just the added lines across all hunks of a file, in order.

func (File) DeletedLines added in v0.0.21

func (f File) DeletedLines() []Line

DeletedLines returns just the deleted lines across all hunks of a file, in order.

type FileAnalysis added in v0.0.21

type FileAnalysis struct {
	Path  string
	AST   *ast.File
	Types *types.Package
	Info  *types.Info
	// Error is the error encountered during AST/type parsing, if any.
	Error error
}

FileAnalysis holds the AST and type information for a single Go file.

type Hunk

type Hunk struct {
	OldStart int
	OldLines int
	NewStart int
	NewLines int
	Lines    []Line
}

Hunk is a single @@ ... @@ block.

type Line

type Line struct {
	Kind      LineKind
	Content   string // text of the line, without the leading +/- space
	NewLineNo int    // line number in the new file (0 for Delete)
	OldLineNo int    // line number in the old file (0 for Add)
}

Line is one unified-diff line within a hunk.

type LineKind

type LineKind int

LineKind tags each line within a hunk.

const (
	LineContext LineKind = iota
	LineAdd
	LineDelete
)

Jump to

Keyboard shortcuts

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