.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.
go get github.com/idelchi/go-gitignorepackage 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.
- Expects relative input paths. Absolute paths are treated as non-ignored, regardless of if they resolve to the current workspace.