/NoGo

A .gitignore parsing lib in pure Go

Primary LanguageGoMIT LicenseMIT

NoGo test CodeQL

A .gitignore parser for Go.

Features

  • parsing .gitignore files
  • loading file trees with several .gitignore files
  • fs.WalkDir WalkDirFunc implementation (and afero.Walk (see below))
  • customizable ignore filename (instead of .gitignore)
  • full compatibility with git
    As far as I could test it, it handles .gitignore files the same way as git.
    If you find an inconsistency with git, please create a new Issue.
    The goal is to provide the exact same .gitignore handling.

Stability

Note that this lib is currently beta and therefore may introduce breaking changes. However I don't think much will change.

Usage

n := nogo.New(nogo.DotGitRule)
if err := n.AddFromFS(wdfs, ".gitignore"); err != nil {
    panic(err)
}

match := n.Match(toSearch, isDir)
fmt.Println(match)

There is also an alternative MatchBecause method which returns also the causing rule if you need some context.

There exists a predefined rule to ignore any .git folder automatically.

n := nogo.New(nogo.DotGitRule)
if err := n.AddFromFS(wdfs, ".gitignore"); err != nil {
    panic(err)
}

Walk

NoGo can be used with fs.WalkDir. Just see the example walk. If you need to use another Walk function, you can build your own wrapper using the NoGo.WalkFunc function.

I intentionally did not include an afero walk to avoid a new dependency just because of afero-compatibility. However, you can easily build your own.
You can find an example for afero in the documentation of NoGo.WalkFunc.