gitignore

package
v0.36.1 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package gitignore matches paths against .gitignore rules.

It exists so the indexer can skip what the repository already declares as noise. A .gitignore is the one place a project has already written down which files are generated, vendored or private, and re-deriving that list as indexer excludes means maintaining the same knowledge twice — badly, since only one of the two copies is under review.

The implementation follows gitignore(5) rather than approximating it, because the approximations fail in the direction that matters: a pattern silently not matching indexes a build directory, and the person who wrote the pattern has no reason to suspect it. What is deliberately not implemented is everything outside the working tree — the global core.excludesFile, .git/info/exclude, and the index itself. A file tracked by git before it was ignored is still ignored here, because the file on disk is what this package can see and what the user pointed at.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddPattern added in v0.28.0

func AddPattern(dir, pattern string) bool

AddPattern appends pattern to the .gitignore in dir if one exists and the pattern is not already present. It returns true when the file was changed.

If dir has no .gitignore, nothing happens and false is returned: ogcode does not create a .gitignore on its own — it only extends one the project already keeps. A project with no .gitignore is left that way.

Types

type Matcher

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

Matcher answers whether a path is ignored, consulting every .gitignore between the root and the path itself.

Files are read lazily and cached, so a walk pays for each .gitignore once however many paths it covers.

func New

func New(root string) *Matcher

New returns a Matcher rooted at dir. No files are read until the first match.

func (*Matcher) Match

func (m *Matcher) Match(path string, isDir bool) bool

Match reports whether path is ignored. path may be absolute or relative to the root; isDir must say whether it names a directory, because a pattern ending in "/" matches only directories.

The rules of precedence are git's: within one file the last matching pattern decides, and a .gitignore deeper in the tree overrides a shallower one. So the search runs outermost-first and keeps the last verdict rather than stopping at the first — which is what lets a nested file re-include something its parent excluded.

Jump to

Keyboard shortcuts

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