github.com/ianlewis/go-gitignore@v0.1.1-0.20231110021210-4a0f15cbd56f/README.md (about)

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