ignore

package
v0.0.0-...-7447bd4 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package ignore implements gitignore-compatible file filtering.

It supports the full gitignore rule set: negation (!), directory-only (/), anchored (/prefix), recursive globs (**), and negation re-inclusion. Rules cascade via a parent chain — child rules take precedence.

Index

Constants

This section is empty.

Variables

View Source
var FileTypes = map[string][]string{
	"go":      {"*.go"},
	"rust":    {"*.rs"},
	"python":  {"*.py", "*.pyi"},
	"js":      {"*.js", "*.jsx", "*.mjs", "*.cjs"},
	"ts":      {"*.ts", "*.tsx", "*.mts", "*.cts"},
	"c":       {"*.c", "*.h"},
	"cpp":     {"*.cpp", "*.hpp", "*.cc", "*.hh", "*.cxx", "*.hxx"},
	"zig":     {"*.zig", "*.zon"},
	"java":    {"*.java"},
	"kotlin":  {"*.kt", "*.kts"},
	"scala":   {"*.scala", "*.sc"},
	"swift":   {"*.swift"},
	"csharp":  {"*.cs"},
	"ruby":    {"*.rb", "*.rake", "Rakefile", "Gemfile"},
	"php":     {"*.php", "*.phtml"},
	"lua":     {"*.lua"},
	"html":    {"*.html", "*.htm"},
	"css":     {"*.css", "*.scss", "*.sass", "*.less"},
	"md":      {"*.md", "*.markdown"},
	"json":    {"*.json", "*.jsonc", "*.json5"},
	"yaml":    {"*.yaml", "*.yml"},
	"toml":    {"*.toml"},
	"sql":     {"*.sql"},
	"sh":      {"*.sh", "*.bash", "*.zsh"},
	"fish":    {"*.fish"},
	"make":    {"Makefile", "*.mk", "GNUmakefile"},
	"docker":  {"Dockerfile", "*.dockerfile"},
	"proto":   {"*.proto"},
	"graphql": {"*.graphql", "*.gql"},
}

FileTypes maps type names to glob patterns.

Functions

This section is empty.

Types

type Config

type Config struct {
	// GlobIncludes is the list of glob patterns to always include.
	GlobIncludes []string
	// GlobExcludes is the list of glob patterns to always exclude.
	GlobExcludes []string
	// Types is the list of file types to include (e.g., "go").
	Types []string
	// TypesNot is the list of file types to exclude.
	TypesNot []string
	// NoIgnore disables .gitignore/.ignore file loading.
	NoIgnore bool
	// Hidden includes hidden files and directories.
	Hidden bool
}

Config holds ignore engine options.

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine manages ignore rules and glob filters for file traversal.

func NewEngine

func NewEngine(cfg Config) (*Engine, error)

NewEngine creates an ignore engine from the given config.

func NewEngineFS

func NewEngineFS(fsys fs.FS, cfg Config) (*Engine, error)

NewEngineFS creates an ignore engine from the given config and filesystem.

func (*Engine) GetIgnoreRules

func (e *Engine) GetIgnoreRules(dir string) []IgnoreRule

GetIgnoreRules returns the loaded ignore rules for a directory.

func (*Engine) LoadIgnoreFile

func (e *Engine) LoadIgnoreFile(dir string) (IgnoreContext, error)

LoadIgnoreFile reads .gitignore and .ignore files from the given directory. It returns an IgnoreContext that can be used for subsequent ShouldIgnore calls in this directory.

func (*Engine) ShouldIgnore

func (e *Engine) ShouldIgnore(path string, isDir bool, ctx ...IgnoreContext) bool

ShouldIgnore returns true if the path should be excluded from traversal. path MUST be cleaned and use forward slashes (normalized by Walker). If ctx is provided, it avoids redundant trie traversals.

type IgnoreContext

type IgnoreContext struct {
	// contains filtered or unexported fields
}

IgnoreContext holds the state for ignore lookups in a specific directory. It can be passed to ShouldIgnore to avoid redundant trie traversals.

type IgnoreRule

type IgnoreRule struct {
	// Pattern is the glob pattern to match.
	Pattern string
	// Negated is true if the pattern starts with !.
	Negated bool
	// DirectoryOnly is true if the pattern ends with /.
	DirectoryOnly bool
	// Anchored is true if the pattern starts with /.
	Anchored bool
	// Source is the path to the file that defined this rule.
	Source string
}

IgnoreRule is a single parsed gitignore rule.

func (*IgnoreRule) Match

func (r *IgnoreRule) Match(relPath string, isDir bool) bool

Match reports whether relPath matches this rule's pattern. Returns raw pattern match; negation is handled by the caller.

type IgnoreSet

type IgnoreSet struct {
	// Dir is the directory this ignore file lives in.
	Dir string
	// DirBase is the base name of the directory (cached for performance).
	DirBase string
	// Rules is the list of parsed rules from the ignore file.
	Rules []IgnoreRule
	// Parent is the parent directory's ignore set.
	Parent *IgnoreSet
}

IgnoreSet holds all ignore rules for one ignore file (.gitignore/.ignore). Rules are ordered top-to-bottom; the last match wins.

func (*IgnoreSet) IsIgnored

func (s *IgnoreSet) IsIgnored(relPath string, isDir bool) bool

IsIgnored checks relPath against this set and all ancestors. Returns true if the path should be ignored.

Within each set, last matching rule wins. Across the chain, the first set with a matching rule determines the result (child set takes precedence over parent).

type Node

type Node struct {
	Children sync.Map // map[string]*Node
	Set      *IgnoreSet
}

Node is a trie node for directory-based ignore set lookups.

Jump to

Keyboard shortcuts

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