github.com/muesli/go-gitignore@v0.0.0-20200714020803-ff91c85188b2/README.md (about) 1 # go-gitignore 2 3 Package `go-gitignore` provides an interface for parsing `.gitignore` files, 4 either individually, or within a repository, and 5 matching paths against the retrieved patterns. Path matching is done using 6 [fnmatch](https://github.com/danwakefield/fnmatch) as specified by 7 [git](https://git-scm.com/docs/gitignore), with 8 support for recursive matching via the `**` pattern. 9 10 ```go 11 import "github.com/denormal/go-gitignore" 12 13 // match a file against a particular .gitignore 14 ignore, err := gitignore.NewFromFile("/my/.gitignore") 15 if err != nil { 16 panic(err) 17 } 18 match := ignore.Match("/my/file/to.check") 19 if match != nil { 20 if match.Ignore() { 21 return true 22 } 23 } 24 25 // or match against a repository 26 // - here we match a directory path relative to the repository 27 ignore, err := gitignore.NewRepository( "/my/git/repository" ) 28 if err != nil { 29 panic(err) 30 } 31 match := ignore.Relative("src/examples", true) 32 if match != nil { 33 if match.Include() { 34 fmt.Printf( 35 "include src/examples/ because of pattern %q at %s", 36 match, match.Position(), 37 ) 38 } 39 } 40 41 // if it's not important whether a path matches, but whether it is 42 // ignored or included... 43 if ignore.Ignore("src/test") { 44 fmt.Println("ignore src/test") 45 } else if ignore.Include("src/github.com") { 46 fmt.Println("include src/github.com") 47 } 48 ``` 49 50 For more information see `godoc github.com/denormal/go-gitignore`. 51 52 ## Patterns 53 54 `go-gitignore` supports the same `.gitignore` pattern format and matching rules as defined by [git](https://git-scm.com/docs/gitignore): 55 56 * A blank line matches no files, so it can serve as a separator for readability. 57 58 * A line starting with `#` serves as a comment. Put a backslash `\` in front of the first hash for patterns that begin with a hash. 59 60 * Trailing spaces are ignored unless they are quoted with backslash `\`. 61 62 * 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`. 63 64 * 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). 65 66 * 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). 67 68 * 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`. 69 70 * A leading slash matches the beginning of the pathname. For example, `/*.c` matches `cat-file.c` but not `mozilla-sha1/sha1.c`. 71 72 Two consecutive asterisks `**` in patterns matched against full pathname may have special meaning: 73 74 * 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`. 75 76 * 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. 77 78 * 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. 79 80 * Other consecutive asterisks are considered invalid. 81 82 ## Installation 83 84 `go-gitignore` can be installed using the standard Go approach: 85 86 ```go 87 go get github.com/denormal/go-gitignore 88 ``` 89 90 ## License 91 92 Copyright (c) 2016 Denormal Limited 93 94 [MIT License](LICENSE)