ignore

package module
v0.0.0-...-525f6e1 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2021 License: MIT Imports: 4 Imported by: 334

README

go-gitignore

Build Status Coverage Status

A gitignore parser for Go

Install

go get github.com/sabhiram/go-gitignore

Usage

For a quick sample of how to use this library, check out the tests under ignore_test.go.

Documentation

Overview

ignore is a library which returns a new ignorer object which can test against various paths. This is particularly useful when trying to filter files based on a .gitignore document

The rules for parsing the input file are the same as the ones listed in the Git docs here: http://git-scm.com/docs/gitignore

The summarized version of the same has been copied here:

  1. A blank line matches no files, so it can serve as a separator for readability.
  2. A line starting with # serves as a comment. Put a backslash ("\") in front of the first hash for patterns that begin with a hash.
  3. Trailing spaces are ignored unless they are quoted with backslash ("\").
  4. An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn’t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined. Put a backslash ("\") in front of the first "!" for patterns that begin with a literal "!", for example, "\!important!.txt".
  5. If the pattern ends with a slash, it is removed for the purpose of the following description, but it would only find a match with a directory. In other words, foo/ will match a directory foo and paths underneath it, but will not match a regular file or a symbolic link foo (this is consistent with the way how pathspec works in general in Git).
  6. If the pattern does not contain a slash /, Git treats it as a shell glob pattern and checks for a match against the pathname relative to the location of the .gitignore file (relative to the toplevel of the work tree if not from a .gitignore file).
  7. Otherwise, Git treats the pattern as a shell glob suitable for consumption by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will not match a / in the pathname. For example, "Documentation/*.html" matches "Documentation/git.html" but not "Documentation/ppc/ppc.html" or "tools/perf/Documentation/perf.html".
  8. A leading slash matches the beginning of the pathname. For example, "/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.c".
  9. Two consecutive asterisks ("**") in patterns matched against full pathname may have special meaning: i. A leading "**" followed by a slash means match in all directories. For example, "** /foo" matches file or directory "foo" anywhere, the same as pattern "foo". "** /foo/bar" matches file or directory "bar" anywhere that is directly under directory "foo". ii. A trailing "/**" matches everything inside. For example, "abc/**" matches all files inside directory "abc", relative to the location of the .gitignore file, with infinite depth. iii. A slash followed by two consecutive asterisks then a slash matches zero or more directories. For example, "a/** /b" matches "a/b", "a/x/b", "a/x/y/b" and so on. iv. Other consecutive asterisks are considered invalid.

Index

Examples

Constants

View Source
const (
	Major = 1
	Minor = 1
	Patch = 0

	Version = "1.1.0"
)

WARNING: Auto generated version file. Do not edit this file by hand. WARNING: go get github.com/sabhiram/gover to manage this file. Version: 1.1.0

Variables

This section is empty.

Functions

This section is empty.

Types

type GitIgnore

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

GitIgnore wraps a list of ignore pattern.

func CompileIgnoreFile

func CompileIgnoreFile(fpath string) (*GitIgnore, error)

CompileIgnoreFile uses an ignore file as the input, parses the lines out of the file and invokes the CompileIgnoreLines method.

func CompileIgnoreFileAndLines

func CompileIgnoreFileAndLines(fpath string, lines ...string) (*GitIgnore, error)

CompileIgnoreFileAndLines accepts a ignore file as the input, parses the lines out of the file and invokes the CompileIgnoreLines method with additional lines.

func CompileIgnoreLines

func CompileIgnoreLines(lines ...string) *GitIgnore

CompileIgnoreLines accepts a variadic set of strings, and returns a GitIgnore instance which converts and appends the lines in the input to regexp.Regexp patterns held within the GitIgnore objects "patterns" field.

Example
ignoreObject := CompileIgnoreLines([]string{"node_modules", "*.out", "foo/*.c"}...)

// You can test the ignoreObject against various paths using the
// "MatchesPath()" interface method. This pretty much is up to
// the users interpretation. In the case of a ".gitignore" file,
// a "match" would indicate that a given path would be ignored.
fmt.Println(ignoreObject.MatchesPath("node_modules/test/foo.js"))
fmt.Println(ignoreObject.MatchesPath("node_modules2/test.out"))
fmt.Println(ignoreObject.MatchesPath("test/foo.js"))
Output:

true
true
false

func (*GitIgnore) MatchesPath

func (gi *GitIgnore) MatchesPath(f string) bool

MatchesPath returns true if the given GitIgnore structure would target a given path string `f`.

func (*GitIgnore) MatchesPathHow

func (gi *GitIgnore) MatchesPathHow(f string) (bool, *IgnorePattern)

MatchesPathHow returns true, `pattern` if the given GitIgnore structure would target a given path string `f`. The IgnorePattern has the Line, LineNo fields.

type IgnoreParser

type IgnoreParser interface {
	MatchesPath(f string) bool
	MatchesPathHow(f string) (bool, *IgnorePattern)
}

IgnoreParser is an interface with `MatchesPaths`.

type IgnorePattern

type IgnorePattern struct {
	Pattern *regexp.Regexp
	Negate  bool
	LineNo  int
	Line    string
}

IgnorePattern encapsulates a pattern and if it is a negated pattern.

Jump to

Keyboard shortcuts

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