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
HasGoFiles returns true if the diff contains any Go files (excluding test files unless includeTests is true).
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 ¶
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.
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 ¶
AddedLines returns just the added lines across all hunks of a file, in order.
func (File) DeletedLines ¶ added in v0.0.21
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.