gitignore

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2025 License: MIT Imports: 3 Imported by: 0

README

gitignore

GitHub release Go Reference Go Report Card Build Status

.gitignore pattern matching for Go.

Translated from the original c code, and validated against a large number of cases, including fuzzy testing, all cross-checked with git check-ignore to ensure behavior matches Git.

Installation

go get github.com/idelchi/go-gitignore

Usage

package main

import (
    "fmt"

    gitignore "github.com/idelchi/go-gitignore"
)

func main() {
    // Pass patterns to construct the gitignorer
    gi := gitignore.New("*.log", "build/", "!important.log")

    // Pass a path as well as a boolean indicating if it's a directory or not
    fmt.Println(gi.Ignored("app.log", false))        // true
    fmt.Println(gi.Ignored("important.log", false))  // false
    fmt.Println(gi.Ignored("build/file.txt", false)) // true
}

Use .Match(...) to retrieve both ignore status and the pattern that matched.

Limitations

  • Expects relative input paths. Absolute paths are treated as non-ignored, regardless of if they resolve to the current workspace.

Documentation

Overview

Package gitignore implements Git-compatible .gitignore pattern matching.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GitIgnore

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

GitIgnore holds a sequence of compiled patterns. Construct with New or NewOptions. Matching semantics follow Git’s .gitignore rules (last match wins).

func New

func New(lines ...string) *GitIgnore

New compiles .gitignore-style lines using default Options.

func NewOptions added in v0.0.3

func NewOptions(opt Options, lines ...string) *GitIgnore

NewOptions compiles .gitignore-style lines with explicit options.

func (*GitIgnore) Append added in v0.0.3

func (g *GitIgnore) Append(lines ...string)

Append compiles and appends new patterns, preserving last-match-wins order.

func (*GitIgnore) Ignored

func (g *GitIgnore) Ignored(pathname string, isDir bool) bool

Ignored reports whether a relative path should be ignored. The caller must indicate if the path is a directory.

func (*GitIgnore) Match added in v0.0.3

func (g *GitIgnore) Match(pathname string, isDir bool) Match

Match returns a detailed match result, including the deciding pattern. If no rule directly matches but an ancestor directory is excluded, the ancestor’s pattern is returned.

func (*GitIgnore) Patterns

func (g *GitIgnore) Patterns() []string

Patterns returns the original patterns in their input order.

type Match added in v0.0.3

type Match struct {
	Ignored bool
	Pattern string
}

Match is a detailed result mirroring `git check-ignore -v` semantics. Pattern contains the deciding pattern (or "!pattern" for a rescuing negation), or is empty when no rule matched and no parent exclusion applies.

type Options added in v0.0.3

type Options struct {
	// CaseFold enables ASCII-only case-insensitive matching in the underlying wildmatch engine.
	CaseFold bool
}

Options defines matcher-wide behavior.

Directories

Path Synopsis
Package wildmatch implements Git's wildmatch.c semantics in Go.
Package wildmatch implements Git's wildmatch.c semantics in Go.

Jump to

Keyboard shortcuts

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