gitignore-go – git.sr.ht/~jamesponddotco/gitignore-go Index | Files | Directories

package gitignore

import "git.sr.ht/~jamesponddotco/gitignore-go"

Package gitignore provides functionality to parse .gitignore files and match paths against the rules defined in those files.

The package tries to implement the gitignore specification as defined in the git documentation. It supports all standard gitignore features including pattern negation, directory-specific patterns, and wildcards.

Basic usage:

matcher, err := gitignore.New("/givePath/to/.gitignore")
if err != nil {
	// Handle error
}

if matcher.Match("givePath/to/file.txt") {
	// Path is ignored
}

The package provides two ways to create a matcher:

  1. From a file using New().
  2. From a slice of pattern strings using NewFromLines().

Index

Constants

const ErrRegexCompile xerrors.Error = "failed to compile regex"

ErrRegexCompile is returned when an error occurs while compiling regular expressions when parsing a .gitignore file.

Types

type File

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

File represents a .gitignore file and provides the functionality to match paths against its rules.

func New

func New(path string) (*File, error)

New creates a new File instance from a given .gitignore file.

func NewFromLines

func NewFromLines(lines []string) (*File, error)

NewFromLines creates a new File instance from a list of strings. Useful when patterns are available in memory rather than in a file or for testing.

func (*File) Match

func (f *File) Match(path string) bool

Match checks if the given path matches any of the .gitignore rules, and return true if the path should be ignored according to the rules.

The path is normalized to use forward slashes (/) regardless of the operating system.

Source Files

doc.go file.go

Directories

PathSynopsis
internal
Version
v1.0.0 (latest)
Published
Dec 14, 2024
Platform
linux/amd64
Imports
7 packages
Last checked
4 months ago

Tools for package owners.