gitignore

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2026 License: MIT Imports: 6 Imported by: 0

README

gitignore

A Go library for matching paths against gitignore rules. Handles the full gitignore spec including negation patterns, ** globs, bracket expressions with POSIX character classes, directory-only patterns, and scoped patterns from nested .gitignore files.

import "github.com/git-pkgs/gitignore"

// Load patterns from .git/info/exclude and root .gitignore
m := gitignore.New("/path/to/repo")

// Add patterns from nested .gitignore files
m.AddFromFile("/path/to/repo/src/.gitignore", "src")

// Or add patterns directly
m.AddPatterns([]byte("*.log\nbuild/\n"), "")

// Check if a path is ignored (use trailing slash for directories)
m.Match("vendor/lib.go")  // true if matched
m.Match("vendor/")        // tests as directory

Paths passed to Match should use forward slashes and be relative to the repository root. Directory paths need a trailing slash so that directory-only patterns (written with a trailing / in .gitignore) work correctly.

Uses last-match-wins semantics, same as git.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Matcher

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

Matcher checks paths against gitignore rules collected from .gitignore files, .git/info/exclude, and any additional patterns. Patterns from subdirectory .gitignore files are scoped to paths within that directory.

Paths passed to Match should use forward slashes. Directory paths must have a trailing slash (e.g. "vendor/") so that directory-only patterns (those written with a trailing slash in .gitignore) match correctly.

func New

func New(root string) *Matcher

New creates a Matcher that reads patterns from the repository's .git/info/exclude and root .gitignore. The root parameter should be the repository working directory (containing .git/).

func (*Matcher) AddFromFile

func (m *Matcher) AddFromFile(absPath, relDir string)

AddFromFile reads a .gitignore file at the given absolute path and scopes its patterns to the given relative directory.

func (*Matcher) AddPatterns

func (m *Matcher) AddPatterns(data []byte, dir string)

AddPatterns parses gitignore pattern lines from data and scopes them to the given relative directory. Pass an empty dir for root-level patterns.

func (*Matcher) Match

func (m *Matcher) Match(relPath string) bool

Match returns true if the given path should be ignored. The path should be slash-separated and relative to the repository root. For directories, append a trailing slash (e.g. "vendor/"). Uses last-match-wins semantics: iterates patterns in reverse and returns on the first match.

Jump to

Keyboard shortcuts

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