2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-09-04 20:29:53 +00:00
|
|
|
|
|
|
|
package ignore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2014-12-23 09:05:08 +00:00
|
|
|
"bytes"
|
2020-06-18 09:04:00 +00:00
|
|
|
"errors"
|
2014-09-04 20:29:53 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2014-12-23 09:05:08 +00:00
|
|
|
"time"
|
2014-09-04 20:29:53 +00:00
|
|
|
|
2016-04-02 19:03:24 +00:00
|
|
|
"github.com/gobwas/glob"
|
2019-11-23 15:20:54 +00:00
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2024-01-13 17:58:23 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/ignore/ignoreresult"
|
2017-04-01 09:58:06 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/osutil"
|
2021-03-17 21:22:49 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/sha256"
|
2015-08-06 09:29:25 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
2014-09-04 20:29:53 +00:00
|
|
|
)
|
|
|
|
|
2020-06-18 09:04:00 +00:00
|
|
|
// A ParseError signifies an error with contents of an ignore file,
|
|
|
|
// including I/O errors on included files. An I/O error on the root level
|
|
|
|
// ignore file is not a ParseError.
|
|
|
|
type ParseError struct {
|
|
|
|
inner error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ParseError) Error() string {
|
|
|
|
return fmt.Sprintf("parse error: %v", e.inner)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ParseError) Unwrap() error {
|
|
|
|
return e.inner
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsParseError(err error) bool {
|
|
|
|
var e *ParseError
|
|
|
|
return errors.As(err, &e)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseError(err error) error {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &ParseError{err}
|
|
|
|
}
|
|
|
|
|
2014-09-04 20:29:53 +00:00
|
|
|
type Pattern struct {
|
2016-05-01 15:58:23 +00:00
|
|
|
pattern string
|
|
|
|
match glob.Glob
|
2024-01-13 17:58:23 +00:00
|
|
|
result ignoreresult.R
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
|
|
|
|
2014-12-23 09:05:08 +00:00
|
|
|
func (p Pattern) String() string {
|
2016-04-02 19:03:24 +00:00
|
|
|
ret := p.pattern
|
2024-01-13 17:58:23 +00:00
|
|
|
if !p.result.IsIgnored() {
|
2016-04-02 19:03:24 +00:00
|
|
|
ret = "!" + ret
|
2014-12-23 09:05:08 +00:00
|
|
|
}
|
2024-01-13 17:58:23 +00:00
|
|
|
if p.result.IsCaseFolded() {
|
2016-04-02 19:03:24 +00:00
|
|
|
ret = "(?i)" + ret
|
|
|
|
}
|
2024-01-13 17:58:23 +00:00
|
|
|
if p.result.IsDeletable() {
|
2016-04-07 09:34:07 +00:00
|
|
|
ret = "(?d)" + ret
|
|
|
|
}
|
2016-04-02 19:03:24 +00:00
|
|
|
return ret
|
2014-12-23 09:05:08 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 07:37:41 +00:00
|
|
|
func (p Pattern) allowsSkippingIgnoredDirs() bool {
|
|
|
|
if p.result.IsIgnored() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if p.pattern[0] != '/' {
|
|
|
|
return false
|
|
|
|
}
|
lib/ignore: Optimise ignoring directories for filesystem watcher (fixes #9339) (#9340)
This improves the ignore handling so that directories can be fully
ignored (skipped in the watcher) in more cases. Specifically, where the
previous rule was that any complex `!`-pattern would disable skipping
directories, the new rule is that only matches on patterns *after* such
a `!`-pattern disable skipping. That is, the following now does the
intuitive thing:
```
/foo
/bar
!whatever
*
```
- `/foo/**` and `/bar/**` are completely skipped, since there is no
chance anything underneath them could ever be not-ignored
- `!whatever` toggles the "can't skip directories any more" flag
- Anything that matches `*` can't skip directories, because it's
possible we can have `whatever` match something deeper.
To enable this, some refactoring was necessary:
- The "can skip dirs" flag is now a property of the match result, not of
the pattern set as a whole.
- That meant returning a boolean is not good enough, we need to actually
return the entire `Result` (or, like, two booleans but that seemed
uglier and more annoying to use)
- `ShouldIgnore(string) boolean` went away with
`Match(string).IsIgnored()` being the obvious replacement (API
simplification!)
- The watcher then needed to import the `ignore` package (for the
`Result` type), but `fs` imports the watcher and `ignore` imports `fs`.
That's a cycle, so I broke out `Result` into a package of its own so
that it can be safely imported everywhere in things like `type Matcher
interface { Match(string) result.Result }`. There's a fair amount of
stuttering in `result.Result` and maybe we should go with something like
`ignoreresult.R` or so, leaving this open for discussion.
Tests refactored to suit, I think this change is in fact quite well
covered by the existing ones...
Also some noise because a few of the changed files were quite old and
got the `gofumpt` treatment by my editor. Sorry not sorry.
---------
Co-authored-by: Simon Frei <freisim93@gmail.com>
2024-01-15 10:13:22 +00:00
|
|
|
// A "/**" at the end is allowed and doesn't have any bearing on the
|
|
|
|
// below checks; remove it before checking.
|
|
|
|
pattern := strings.TrimSuffix(p.pattern, "/**")
|
|
|
|
if len(pattern) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if strings.Contains(pattern[1:], "/") {
|
2019-11-26 07:37:41 +00:00
|
|
|
return false
|
|
|
|
}
|
2020-04-07 08:23:38 +00:00
|
|
|
// Double asterisk everywhere in the path except at the end is bad
|
lib/ignore: Optimise ignoring directories for filesystem watcher (fixes #9339) (#9340)
This improves the ignore handling so that directories can be fully
ignored (skipped in the watcher) in more cases. Specifically, where the
previous rule was that any complex `!`-pattern would disable skipping
directories, the new rule is that only matches on patterns *after* such
a `!`-pattern disable skipping. That is, the following now does the
intuitive thing:
```
/foo
/bar
!whatever
*
```
- `/foo/**` and `/bar/**` are completely skipped, since there is no
chance anything underneath them could ever be not-ignored
- `!whatever` toggles the "can't skip directories any more" flag
- Anything that matches `*` can't skip directories, because it's
possible we can have `whatever` match something deeper.
To enable this, some refactoring was necessary:
- The "can skip dirs" flag is now a property of the match result, not of
the pattern set as a whole.
- That meant returning a boolean is not good enough, we need to actually
return the entire `Result` (or, like, two booleans but that seemed
uglier and more annoying to use)
- `ShouldIgnore(string) boolean` went away with
`Match(string).IsIgnored()` being the obvious replacement (API
simplification!)
- The watcher then needed to import the `ignore` package (for the
`Result` type), but `fs` imports the watcher and `ignore` imports `fs`.
That's a cycle, so I broke out `Result` into a package of its own so
that it can be safely imported everywhere in things like `type Matcher
interface { Match(string) result.Result }`. There's a fair amount of
stuttering in `result.Result` and maybe we should go with something like
`ignoreresult.R` or so, leaving this open for discussion.
Tests refactored to suit, I think this change is in fact quite well
covered by the existing ones...
Also some noise because a few of the changed files were quite old and
got the `gofumpt` treatment by my editor. Sorry not sorry.
---------
Co-authored-by: Simon Frei <freisim93@gmail.com>
2024-01-15 10:13:22 +00:00
|
|
|
return !strings.Contains(strings.TrimSuffix(pattern, "**"), "**")
|
2019-11-26 07:37:41 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 10:27:12 +00:00
|
|
|
// The ChangeDetector is responsible for determining if files have changed
|
|
|
|
// on disk. It gets told to Remember() files (name and modtime) and will
|
|
|
|
// then get asked if a file has been Seen() (i.e., Remember() has been
|
|
|
|
// called on it) and if any of the files have Changed(). To forget all
|
|
|
|
// files, call Reset().
|
|
|
|
type ChangeDetector interface {
|
2017-08-22 06:45:00 +00:00
|
|
|
Remember(fs fs.Filesystem, name string, modtime time.Time)
|
|
|
|
Seen(fs fs.Filesystem, name string) bool
|
2017-06-11 10:27:12 +00:00
|
|
|
Changed() bool
|
|
|
|
Reset()
|
|
|
|
}
|
|
|
|
|
2014-10-12 21:35:15 +00:00
|
|
|
type Matcher struct {
|
lib/ignore: Optimise ignoring directories for filesystem watcher (fixes #9339) (#9340)
This improves the ignore handling so that directories can be fully
ignored (skipped in the watcher) in more cases. Specifically, where the
previous rule was that any complex `!`-pattern would disable skipping
directories, the new rule is that only matches on patterns *after* such
a `!`-pattern disable skipping. That is, the following now does the
intuitive thing:
```
/foo
/bar
!whatever
*
```
- `/foo/**` and `/bar/**` are completely skipped, since there is no
chance anything underneath them could ever be not-ignored
- `!whatever` toggles the "can't skip directories any more" flag
- Anything that matches `*` can't skip directories, because it's
possible we can have `whatever` match something deeper.
To enable this, some refactoring was necessary:
- The "can skip dirs" flag is now a property of the match result, not of
the pattern set as a whole.
- That meant returning a boolean is not good enough, we need to actually
return the entire `Result` (or, like, two booleans but that seemed
uglier and more annoying to use)
- `ShouldIgnore(string) boolean` went away with
`Match(string).IsIgnored()` being the obvious replacement (API
simplification!)
- The watcher then needed to import the `ignore` package (for the
`Result` type), but `fs` imports the watcher and `ignore` imports `fs`.
That's a cycle, so I broke out `Result` into a package of its own so
that it can be safely imported everywhere in things like `type Matcher
interface { Match(string) result.Result }`. There's a fair amount of
stuttering in `result.Result` and maybe we should go with something like
`ignoreresult.R` or so, leaving this open for discussion.
Tests refactored to suit, I think this change is in fact quite well
covered by the existing ones...
Also some noise because a few of the changed files were quite old and
got the `gofumpt` treatment by my editor. Sorry not sorry.
---------
Co-authored-by: Simon Frei <freisim93@gmail.com>
2024-01-15 10:13:22 +00:00
|
|
|
fs fs.Filesystem
|
|
|
|
lines []string // exact lines read from .stignore
|
|
|
|
patterns []Pattern // patterns including those from included files
|
|
|
|
withCache bool
|
|
|
|
matches *cache
|
|
|
|
curHash string
|
|
|
|
stop chan struct{}
|
|
|
|
changeDetector ChangeDetector
|
|
|
|
mut sync.Mutex
|
2017-06-11 10:27:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// An Option can be passed to New()
|
|
|
|
type Option func(*Matcher)
|
|
|
|
|
|
|
|
// WithCache enables or disables lookup caching. The default is disabled.
|
|
|
|
func WithCache(v bool) Option {
|
|
|
|
return func(m *Matcher) {
|
|
|
|
m.withCache = v
|
|
|
|
}
|
2014-12-23 09:05:08 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 10:27:12 +00:00
|
|
|
// WithChangeDetector sets a custom ChangeDetector. The default is to simply
|
|
|
|
// use the on disk modtime for comparison.
|
|
|
|
func WithChangeDetector(cd ChangeDetector) Option {
|
|
|
|
return func(m *Matcher) {
|
|
|
|
m.changeDetector = cd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
func New(fs fs.Filesystem, opts ...Option) *Matcher {
|
2014-12-23 09:05:08 +00:00
|
|
|
m := &Matcher{
|
lib/ignore: Optimise ignoring directories for filesystem watcher (fixes #9339) (#9340)
This improves the ignore handling so that directories can be fully
ignored (skipped in the watcher) in more cases. Specifically, where the
previous rule was that any complex `!`-pattern would disable skipping
directories, the new rule is that only matches on patterns *after* such
a `!`-pattern disable skipping. That is, the following now does the
intuitive thing:
```
/foo
/bar
!whatever
*
```
- `/foo/**` and `/bar/**` are completely skipped, since there is no
chance anything underneath them could ever be not-ignored
- `!whatever` toggles the "can't skip directories any more" flag
- Anything that matches `*` can't skip directories, because it's
possible we can have `whatever` match something deeper.
To enable this, some refactoring was necessary:
- The "can skip dirs" flag is now a property of the match result, not of
the pattern set as a whole.
- That meant returning a boolean is not good enough, we need to actually
return the entire `Result` (or, like, two booleans but that seemed
uglier and more annoying to use)
- `ShouldIgnore(string) boolean` went away with
`Match(string).IsIgnored()` being the obvious replacement (API
simplification!)
- The watcher then needed to import the `ignore` package (for the
`Result` type), but `fs` imports the watcher and `ignore` imports `fs`.
That's a cycle, so I broke out `Result` into a package of its own so
that it can be safely imported everywhere in things like `type Matcher
interface { Match(string) result.Result }`. There's a fair amount of
stuttering in `result.Result` and maybe we should go with something like
`ignoreresult.R` or so, leaving this open for discussion.
Tests refactored to suit, I think this change is in fact quite well
covered by the existing ones...
Also some noise because a few of the changed files were quite old and
got the `gofumpt` treatment by my editor. Sorry not sorry.
---------
Co-authored-by: Simon Frei <freisim93@gmail.com>
2024-01-15 10:13:22 +00:00
|
|
|
fs: fs,
|
|
|
|
stop: make(chan struct{}),
|
|
|
|
mut: sync.NewMutex(),
|
2014-12-23 09:05:08 +00:00
|
|
|
}
|
2017-06-11 10:27:12 +00:00
|
|
|
for _, opt := range opts {
|
|
|
|
opt(m)
|
|
|
|
}
|
|
|
|
if m.changeDetector == nil {
|
2017-08-22 06:45:00 +00:00
|
|
|
m.changeDetector = newModtimeChecker()
|
2017-06-11 10:27:12 +00:00
|
|
|
}
|
|
|
|
if m.withCache {
|
2014-12-23 09:05:08 +00:00
|
|
|
go m.clean(2 * time.Hour)
|
|
|
|
}
|
|
|
|
return m
|
2014-10-12 21:35:15 +00:00
|
|
|
}
|
2014-09-04 20:29:53 +00:00
|
|
|
|
2020-06-18 09:04:00 +00:00
|
|
|
// Load and parse a file. The returned error may be of type *ParseError in
|
|
|
|
// which case a file was loaded from disk but the patterns could not be
|
|
|
|
// parsed. In this case the contents of the file are nonetheless available
|
|
|
|
// in the Lines() method.
|
2014-12-23 09:05:08 +00:00
|
|
|
func (m *Matcher) Load(file string) error {
|
2016-11-22 21:30:45 +00:00
|
|
|
m.mut.Lock()
|
|
|
|
defer m.mut.Unlock()
|
|
|
|
|
2017-08-22 06:45:00 +00:00
|
|
|
if m.changeDetector.Seen(m.fs, file) && !m.changeDetector.Changed() {
|
2016-11-22 21:30:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
2014-12-23 09:05:08 +00:00
|
|
|
|
2021-09-07 22:11:16 +00:00
|
|
|
fd, info, err := loadIgnoreFile(m.fs, file)
|
2014-12-23 09:05:08 +00:00
|
|
|
if err != nil {
|
2019-02-02 11:16:27 +00:00
|
|
|
m.parseLocked(&bytes.Buffer{}, file)
|
2014-12-23 09:05:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
|
2017-06-11 10:27:12 +00:00
|
|
|
m.changeDetector.Reset()
|
2016-11-22 21:30:45 +00:00
|
|
|
|
2018-05-08 21:37:13 +00:00
|
|
|
err = m.parseLocked(fd, file)
|
|
|
|
// If we failed to parse, don't cache, as next time Load is called
|
|
|
|
// we'll pretend it's all good.
|
|
|
|
if err == nil {
|
|
|
|
m.changeDetector.Remember(m.fs, file, info.ModTime())
|
|
|
|
}
|
|
|
|
return err
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 09:04:00 +00:00
|
|
|
// Load and parse an io.Reader. See Load() for notes on the returned error.
|
2014-12-23 09:05:08 +00:00
|
|
|
func (m *Matcher) Parse(r io.Reader, file string) error {
|
|
|
|
m.mut.Lock()
|
|
|
|
defer m.mut.Unlock()
|
2016-11-22 21:30:45 +00:00
|
|
|
return m.parseLocked(r, file)
|
|
|
|
}
|
2014-12-23 09:05:08 +00:00
|
|
|
|
2016-11-22 21:30:45 +00:00
|
|
|
func (m *Matcher) parseLocked(r io.Reader, file string) error {
|
2017-09-04 12:46:19 +00:00
|
|
|
lines, patterns, err := parseIgnoreFile(m.fs, r, file, m.changeDetector, make(map[string]struct{}))
|
2014-12-23 09:05:08 +00:00
|
|
|
// Error is saved and returned at the end. We process the patterns
|
|
|
|
// (possibly blank) anyway.
|
|
|
|
|
2018-01-20 07:52:57 +00:00
|
|
|
m.lines = lines
|
|
|
|
|
2014-12-23 09:05:08 +00:00
|
|
|
newHash := hashPatterns(patterns)
|
|
|
|
if newHash == m.curHash {
|
|
|
|
// We've already loaded exactly these patterns.
|
|
|
|
return err
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
2014-12-23 09:05:08 +00:00
|
|
|
|
|
|
|
m.curHash = newHash
|
|
|
|
m.patterns = patterns
|
|
|
|
if m.withCache {
|
|
|
|
m.matches = newCache(patterns)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
|
|
|
|
2024-01-13 17:58:23 +00:00
|
|
|
// Match matches the patterns plus temporary and internal files.
|
|
|
|
func (m *Matcher) Match(file string) (result ignoreresult.R) {
|
|
|
|
switch {
|
|
|
|
case fs.IsTemporary(file):
|
lib/ignore: Optimise ignoring directories for filesystem watcher (fixes #9339) (#9340)
This improves the ignore handling so that directories can be fully
ignored (skipped in the watcher) in more cases. Specifically, where the
previous rule was that any complex `!`-pattern would disable skipping
directories, the new rule is that only matches on patterns *after* such
a `!`-pattern disable skipping. That is, the following now does the
intuitive thing:
```
/foo
/bar
!whatever
*
```
- `/foo/**` and `/bar/**` are completely skipped, since there is no
chance anything underneath them could ever be not-ignored
- `!whatever` toggles the "can't skip directories any more" flag
- Anything that matches `*` can't skip directories, because it's
possible we can have `whatever` match something deeper.
To enable this, some refactoring was necessary:
- The "can skip dirs" flag is now a property of the match result, not of
the pattern set as a whole.
- That meant returning a boolean is not good enough, we need to actually
return the entire `Result` (or, like, two booleans but that seemed
uglier and more annoying to use)
- `ShouldIgnore(string) boolean` went away with
`Match(string).IsIgnored()` being the obvious replacement (API
simplification!)
- The watcher then needed to import the `ignore` package (for the
`Result` type), but `fs` imports the watcher and `ignore` imports `fs`.
That's a cycle, so I broke out `Result` into a package of its own so
that it can be safely imported everywhere in things like `type Matcher
interface { Match(string) result.Result }`. There's a fair amount of
stuttering in `result.Result` and maybe we should go with something like
`ignoreresult.R` or so, leaving this open for discussion.
Tests refactored to suit, I think this change is in fact quite well
covered by the existing ones...
Also some noise because a few of the changed files were quite old and
got the `gofumpt` treatment by my editor. Sorry not sorry.
---------
Co-authored-by: Simon Frei <freisim93@gmail.com>
2024-01-15 10:13:22 +00:00
|
|
|
return ignoreresult.IgnoreAndSkip
|
2024-01-13 17:58:23 +00:00
|
|
|
|
|
|
|
case fs.IsInternal(file):
|
lib/ignore: Optimise ignoring directories for filesystem watcher (fixes #9339) (#9340)
This improves the ignore handling so that directories can be fully
ignored (skipped in the watcher) in more cases. Specifically, where the
previous rule was that any complex `!`-pattern would disable skipping
directories, the new rule is that only matches on patterns *after* such
a `!`-pattern disable skipping. That is, the following now does the
intuitive thing:
```
/foo
/bar
!whatever
*
```
- `/foo/**` and `/bar/**` are completely skipped, since there is no
chance anything underneath them could ever be not-ignored
- `!whatever` toggles the "can't skip directories any more" flag
- Anything that matches `*` can't skip directories, because it's
possible we can have `whatever` match something deeper.
To enable this, some refactoring was necessary:
- The "can skip dirs" flag is now a property of the match result, not of
the pattern set as a whole.
- That meant returning a boolean is not good enough, we need to actually
return the entire `Result` (or, like, two booleans but that seemed
uglier and more annoying to use)
- `ShouldIgnore(string) boolean` went away with
`Match(string).IsIgnored()` being the obvious replacement (API
simplification!)
- The watcher then needed to import the `ignore` package (for the
`Result` type), but `fs` imports the watcher and `ignore` imports `fs`.
That's a cycle, so I broke out `Result` into a package of its own so
that it can be safely imported everywhere in things like `type Matcher
interface { Match(string) result.Result }`. There's a fair amount of
stuttering in `result.Result` and maybe we should go with something like
`ignoreresult.R` or so, leaving this open for discussion.
Tests refactored to suit, I think this change is in fact quite well
covered by the existing ones...
Also some noise because a few of the changed files were quite old and
got the `gofumpt` treatment by my editor. Sorry not sorry.
---------
Co-authored-by: Simon Frei <freisim93@gmail.com>
2024-01-15 10:13:22 +00:00
|
|
|
return ignoreresult.IgnoreAndSkip
|
2024-01-13 17:58:23 +00:00
|
|
|
|
|
|
|
case file == ".":
|
|
|
|
return ignoreresult.NotIgnored
|
2015-04-27 19:49:10 +00:00
|
|
|
}
|
|
|
|
|
2014-12-23 09:05:08 +00:00
|
|
|
m.mut.Lock()
|
|
|
|
defer m.mut.Unlock()
|
|
|
|
|
2014-10-12 21:35:15 +00:00
|
|
|
if len(m.patterns) == 0 {
|
2024-01-13 17:58:23 +00:00
|
|
|
return ignoreresult.NotIgnored
|
2014-10-12 21:35:15 +00:00
|
|
|
}
|
|
|
|
|
2014-12-02 22:13:03 +00:00
|
|
|
if m.matches != nil {
|
|
|
|
// Check the cache for a known result.
|
|
|
|
res, ok := m.matches.get(file)
|
2014-10-12 21:35:15 +00:00
|
|
|
if ok {
|
2014-12-02 22:13:03 +00:00
|
|
|
return res
|
2014-10-12 21:35:15 +00:00
|
|
|
}
|
2014-12-02 22:13:03 +00:00
|
|
|
|
|
|
|
// Update the cache with the result at return time
|
|
|
|
defer func() {
|
|
|
|
m.matches.set(file, result)
|
|
|
|
}()
|
2014-10-12 21:35:15 +00:00
|
|
|
}
|
|
|
|
|
lib/ignore: Optimise ignoring directories for filesystem watcher (fixes #9339) (#9340)
This improves the ignore handling so that directories can be fully
ignored (skipped in the watcher) in more cases. Specifically, where the
previous rule was that any complex `!`-pattern would disable skipping
directories, the new rule is that only matches on patterns *after* such
a `!`-pattern disable skipping. That is, the following now does the
intuitive thing:
```
/foo
/bar
!whatever
*
```
- `/foo/**` and `/bar/**` are completely skipped, since there is no
chance anything underneath them could ever be not-ignored
- `!whatever` toggles the "can't skip directories any more" flag
- Anything that matches `*` can't skip directories, because it's
possible we can have `whatever` match something deeper.
To enable this, some refactoring was necessary:
- The "can skip dirs" flag is now a property of the match result, not of
the pattern set as a whole.
- That meant returning a boolean is not good enough, we need to actually
return the entire `Result` (or, like, two booleans but that seemed
uglier and more annoying to use)
- `ShouldIgnore(string) boolean` went away with
`Match(string).IsIgnored()` being the obvious replacement (API
simplification!)
- The watcher then needed to import the `ignore` package (for the
`Result` type), but `fs` imports the watcher and `ignore` imports `fs`.
That's a cycle, so I broke out `Result` into a package of its own so
that it can be safely imported everywhere in things like `type Matcher
interface { Match(string) result.Result }`. There's a fair amount of
stuttering in `result.Result` and maybe we should go with something like
`ignoreresult.R` or so, leaving this open for discussion.
Tests refactored to suit, I think this change is in fact quite well
covered by the existing ones...
Also some noise because a few of the changed files were quite old and
got the `gofumpt` treatment by my editor. Sorry not sorry.
---------
Co-authored-by: Simon Frei <freisim93@gmail.com>
2024-01-15 10:13:22 +00:00
|
|
|
// Check all the patterns for a match. Track whether the patterns so far
|
|
|
|
// allow skipping matched directories or not. As soon as we hit an
|
|
|
|
// exclude pattern (with some exceptions), we can't skip directories
|
|
|
|
// anymore.
|
2016-04-02 19:03:24 +00:00
|
|
|
file = filepath.ToSlash(file)
|
|
|
|
var lowercaseFile string
|
lib/ignore: Optimise ignoring directories for filesystem watcher (fixes #9339) (#9340)
This improves the ignore handling so that directories can be fully
ignored (skipped in the watcher) in more cases. Specifically, where the
previous rule was that any complex `!`-pattern would disable skipping
directories, the new rule is that only matches on patterns *after* such
a `!`-pattern disable skipping. That is, the following now does the
intuitive thing:
```
/foo
/bar
!whatever
*
```
- `/foo/**` and `/bar/**` are completely skipped, since there is no
chance anything underneath them could ever be not-ignored
- `!whatever` toggles the "can't skip directories any more" flag
- Anything that matches `*` can't skip directories, because it's
possible we can have `whatever` match something deeper.
To enable this, some refactoring was necessary:
- The "can skip dirs" flag is now a property of the match result, not of
the pattern set as a whole.
- That meant returning a boolean is not good enough, we need to actually
return the entire `Result` (or, like, two booleans but that seemed
uglier and more annoying to use)
- `ShouldIgnore(string) boolean` went away with
`Match(string).IsIgnored()` being the obvious replacement (API
simplification!)
- The watcher then needed to import the `ignore` package (for the
`Result` type), but `fs` imports the watcher and `ignore` imports `fs`.
That's a cycle, so I broke out `Result` into a package of its own so
that it can be safely imported everywhere in things like `type Matcher
interface { Match(string) result.Result }`. There's a fair amount of
stuttering in `result.Result` and maybe we should go with something like
`ignoreresult.R` or so, leaving this open for discussion.
Tests refactored to suit, I think this change is in fact quite well
covered by the existing ones...
Also some noise because a few of the changed files were quite old and
got the `gofumpt` treatment by my editor. Sorry not sorry.
---------
Co-authored-by: Simon Frei <freisim93@gmail.com>
2024-01-15 10:13:22 +00:00
|
|
|
canSkipDir := true
|
2014-10-12 21:35:15 +00:00
|
|
|
for _, pattern := range m.patterns {
|
lib/ignore: Optimise ignoring directories for filesystem watcher (fixes #9339) (#9340)
This improves the ignore handling so that directories can be fully
ignored (skipped in the watcher) in more cases. Specifically, where the
previous rule was that any complex `!`-pattern would disable skipping
directories, the new rule is that only matches on patterns *after* such
a `!`-pattern disable skipping. That is, the following now does the
intuitive thing:
```
/foo
/bar
!whatever
*
```
- `/foo/**` and `/bar/**` are completely skipped, since there is no
chance anything underneath them could ever be not-ignored
- `!whatever` toggles the "can't skip directories any more" flag
- Anything that matches `*` can't skip directories, because it's
possible we can have `whatever` match something deeper.
To enable this, some refactoring was necessary:
- The "can skip dirs" flag is now a property of the match result, not of
the pattern set as a whole.
- That meant returning a boolean is not good enough, we need to actually
return the entire `Result` (or, like, two booleans but that seemed
uglier and more annoying to use)
- `ShouldIgnore(string) boolean` went away with
`Match(string).IsIgnored()` being the obvious replacement (API
simplification!)
- The watcher then needed to import the `ignore` package (for the
`Result` type), but `fs` imports the watcher and `ignore` imports `fs`.
That's a cycle, so I broke out `Result` into a package of its own so
that it can be safely imported everywhere in things like `type Matcher
interface { Match(string) result.Result }`. There's a fair amount of
stuttering in `result.Result` and maybe we should go with something like
`ignoreresult.R` or so, leaving this open for discussion.
Tests refactored to suit, I think this change is in fact quite well
covered by the existing ones...
Also some noise because a few of the changed files were quite old and
got the `gofumpt` treatment by my editor. Sorry not sorry.
---------
Co-authored-by: Simon Frei <freisim93@gmail.com>
2024-01-15 10:13:22 +00:00
|
|
|
if canSkipDir && !pattern.allowsSkippingIgnoredDirs() {
|
|
|
|
canSkipDir = false
|
|
|
|
}
|
|
|
|
|
|
|
|
res := pattern.result
|
|
|
|
if canSkipDir {
|
|
|
|
res = res.WithSkipDir()
|
|
|
|
}
|
2016-05-01 15:58:23 +00:00
|
|
|
if pattern.result.IsCaseFolded() {
|
2016-04-02 19:03:24 +00:00
|
|
|
if lowercaseFile == "" {
|
|
|
|
lowercaseFile = strings.ToLower(file)
|
|
|
|
}
|
|
|
|
if pattern.match.Match(lowercaseFile) {
|
lib/ignore: Optimise ignoring directories for filesystem watcher (fixes #9339) (#9340)
This improves the ignore handling so that directories can be fully
ignored (skipped in the watcher) in more cases. Specifically, where the
previous rule was that any complex `!`-pattern would disable skipping
directories, the new rule is that only matches on patterns *after* such
a `!`-pattern disable skipping. That is, the following now does the
intuitive thing:
```
/foo
/bar
!whatever
*
```
- `/foo/**` and `/bar/**` are completely skipped, since there is no
chance anything underneath them could ever be not-ignored
- `!whatever` toggles the "can't skip directories any more" flag
- Anything that matches `*` can't skip directories, because it's
possible we can have `whatever` match something deeper.
To enable this, some refactoring was necessary:
- The "can skip dirs" flag is now a property of the match result, not of
the pattern set as a whole.
- That meant returning a boolean is not good enough, we need to actually
return the entire `Result` (or, like, two booleans but that seemed
uglier and more annoying to use)
- `ShouldIgnore(string) boolean` went away with
`Match(string).IsIgnored()` being the obvious replacement (API
simplification!)
- The watcher then needed to import the `ignore` package (for the
`Result` type), but `fs` imports the watcher and `ignore` imports `fs`.
That's a cycle, so I broke out `Result` into a package of its own so
that it can be safely imported everywhere in things like `type Matcher
interface { Match(string) result.Result }`. There's a fair amount of
stuttering in `result.Result` and maybe we should go with something like
`ignoreresult.R` or so, leaving this open for discussion.
Tests refactored to suit, I think this change is in fact quite well
covered by the existing ones...
Also some noise because a few of the changed files were quite old and
got the `gofumpt` treatment by my editor. Sorry not sorry.
---------
Co-authored-by: Simon Frei <freisim93@gmail.com>
2024-01-15 10:13:22 +00:00
|
|
|
return res
|
2016-04-02 19:03:24 +00:00
|
|
|
}
|
2022-07-28 17:14:22 +00:00
|
|
|
} else if pattern.match.Match(file) {
|
lib/ignore: Optimise ignoring directories for filesystem watcher (fixes #9339) (#9340)
This improves the ignore handling so that directories can be fully
ignored (skipped in the watcher) in more cases. Specifically, where the
previous rule was that any complex `!`-pattern would disable skipping
directories, the new rule is that only matches on patterns *after* such
a `!`-pattern disable skipping. That is, the following now does the
intuitive thing:
```
/foo
/bar
!whatever
*
```
- `/foo/**` and `/bar/**` are completely skipped, since there is no
chance anything underneath them could ever be not-ignored
- `!whatever` toggles the "can't skip directories any more" flag
- Anything that matches `*` can't skip directories, because it's
possible we can have `whatever` match something deeper.
To enable this, some refactoring was necessary:
- The "can skip dirs" flag is now a property of the match result, not of
the pattern set as a whole.
- That meant returning a boolean is not good enough, we need to actually
return the entire `Result` (or, like, two booleans but that seemed
uglier and more annoying to use)
- `ShouldIgnore(string) boolean` went away with
`Match(string).IsIgnored()` being the obvious replacement (API
simplification!)
- The watcher then needed to import the `ignore` package (for the
`Result` type), but `fs` imports the watcher and `ignore` imports `fs`.
That's a cycle, so I broke out `Result` into a package of its own so
that it can be safely imported everywhere in things like `type Matcher
interface { Match(string) result.Result }`. There's a fair amount of
stuttering in `result.Result` and maybe we should go with something like
`ignoreresult.R` or so, leaving this open for discussion.
Tests refactored to suit, I think this change is in fact quite well
covered by the existing ones...
Also some noise because a few of the changed files were quite old and
got the `gofumpt` treatment by my editor. Sorry not sorry.
---------
Co-authored-by: Simon Frei <freisim93@gmail.com>
2024-01-15 10:13:22 +00:00
|
|
|
return res
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-02 22:13:03 +00:00
|
|
|
|
2016-05-04 07:15:56 +00:00
|
|
|
// Default to not matching.
|
2024-01-13 17:58:23 +00:00
|
|
|
return ignoreresult.NotIgnored
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
|
|
|
|
2017-04-01 09:58:06 +00:00
|
|
|
// Lines return a list of the unprocessed lines in .stignore at last load
|
|
|
|
func (m *Matcher) Lines() []string {
|
|
|
|
m.mut.Lock()
|
|
|
|
defer m.mut.Unlock()
|
|
|
|
return m.lines
|
|
|
|
}
|
|
|
|
|
2016-04-02 19:03:24 +00:00
|
|
|
// Patterns return a list of the loaded patterns, as they've been parsed
|
2014-11-08 21:12:18 +00:00
|
|
|
func (m *Matcher) Patterns() []string {
|
2014-12-23 09:05:08 +00:00
|
|
|
m.mut.Lock()
|
|
|
|
defer m.mut.Unlock()
|
|
|
|
|
2014-11-08 21:12:18 +00:00
|
|
|
patterns := make([]string, len(m.patterns))
|
|
|
|
for i, pat := range m.patterns {
|
2014-12-23 09:05:08 +00:00
|
|
|
patterns[i] = pat.String()
|
2014-11-08 21:12:18 +00:00
|
|
|
}
|
|
|
|
return patterns
|
|
|
|
}
|
|
|
|
|
2018-03-17 15:49:12 +00:00
|
|
|
func (m *Matcher) String() string {
|
|
|
|
return fmt.Sprintf("Matcher/%v@%p", m.Patterns(), m)
|
|
|
|
}
|
|
|
|
|
2014-12-23 09:05:08 +00:00
|
|
|
func (m *Matcher) Hash() string {
|
|
|
|
m.mut.Lock()
|
|
|
|
defer m.mut.Unlock()
|
|
|
|
return m.curHash
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Matcher) Stop() {
|
|
|
|
close(m.stop)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Matcher) clean(d time.Duration) {
|
|
|
|
t := time.NewTimer(d / 2)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-m.stop:
|
|
|
|
return
|
|
|
|
case <-t.C:
|
|
|
|
m.mut.Lock()
|
|
|
|
if m.matches != nil {
|
|
|
|
m.matches.clean(d)
|
|
|
|
}
|
|
|
|
t.Reset(d / 2)
|
|
|
|
m.mut.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func hashPatterns(patterns []Pattern) string {
|
2021-03-17 21:22:49 +00:00
|
|
|
h := sha256.New()
|
2014-12-23 09:05:08 +00:00
|
|
|
for _, pat := range patterns {
|
2019-02-02 11:16:27 +00:00
|
|
|
h.Write([]byte(pat.String()))
|
|
|
|
h.Write([]byte("\n"))
|
2014-12-23 09:05:08 +00:00
|
|
|
}
|
|
|
|
return fmt.Sprintf("%x", h.Sum(nil))
|
|
|
|
}
|
|
|
|
|
2021-09-07 22:11:16 +00:00
|
|
|
func loadIgnoreFile(fs fs.Filesystem, file string) (fs.File, fs.FileInfo, error) {
|
2017-09-04 12:46:19 +00:00
|
|
|
fd, err := fs.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return fd, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := fd.Stat()
|
|
|
|
if err != nil {
|
|
|
|
fd.Close()
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
|
|
|
|
2017-09-04 12:46:19 +00:00
|
|
|
return fd, info, err
|
|
|
|
}
|
|
|
|
|
2018-01-17 16:56:53 +00:00
|
|
|
func loadParseIncludeFile(filesystem fs.Filesystem, file string, cd ChangeDetector, linesSeen map[string]struct{}) ([]Pattern, error) {
|
2017-08-22 06:45:00 +00:00
|
|
|
// Allow escaping the folders filesystem.
|
|
|
|
// TODO: Deprecate, somehow?
|
|
|
|
if filesystem.Type() == fs.FilesystemTypeBasic {
|
|
|
|
uri := filesystem.URI()
|
|
|
|
joined := filepath.Join(uri, file)
|
2018-11-22 10:16:45 +00:00
|
|
|
if !fs.IsParent(joined, uri) {
|
2017-08-22 06:45:00 +00:00
|
|
|
filesystem = fs.NewFilesystem(filesystem.Type(), filepath.Dir(joined))
|
|
|
|
file = filepath.Base(joined)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-04 12:46:19 +00:00
|
|
|
if cd.Seen(filesystem, file) {
|
2023-04-20 13:00:55 +00:00
|
|
|
return nil, errors.New("multiple include")
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
|
|
|
|
2021-09-07 22:11:16 +00:00
|
|
|
fd, info, err := loadIgnoreFile(filesystem, file)
|
2016-11-22 21:30:45 +00:00
|
|
|
if err != nil {
|
2023-04-20 13:00:55 +00:00
|
|
|
// isNotExist is considered "ok" in a sense of that a folder doesn't have to act
|
|
|
|
// upon it. This is because it is allowed for .stignore to not exist. However,
|
|
|
|
// included ignore files are not allowed to be missing and these errors should be
|
2024-01-13 17:58:23 +00:00
|
|
|
// acted upon on. So we don't preserve the error chain here and manually set an
|
2023-04-20 13:00:55 +00:00
|
|
|
// error instead, if the file is missing.
|
|
|
|
if fs.IsNotExist(err) {
|
|
|
|
err = errors.New("file not found")
|
|
|
|
}
|
2018-01-17 16:56:53 +00:00
|
|
|
return nil, err
|
2016-11-22 21:30:45 +00:00
|
|
|
}
|
2017-09-04 12:46:19 +00:00
|
|
|
defer fd.Close()
|
2016-11-22 21:30:45 +00:00
|
|
|
|
2017-08-22 06:45:00 +00:00
|
|
|
cd.Remember(filesystem, file, info.ModTime())
|
2017-06-11 10:27:12 +00:00
|
|
|
|
2018-01-17 16:56:53 +00:00
|
|
|
_, patterns, err := parseIgnoreFile(filesystem, fd, file, cd, linesSeen)
|
|
|
|
return patterns, err
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 07:37:41 +00:00
|
|
|
func parseLine(line string) ([]Pattern, error) {
|
|
|
|
pattern := Pattern{
|
2024-01-13 17:58:23 +00:00
|
|
|
result: ignoreresult.Ignored,
|
2016-05-01 15:58:23 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 07:37:41 +00:00
|
|
|
// Allow prefixes to be specified in any order, but only once.
|
|
|
|
var seenPrefix [3]bool
|
2016-04-02 19:03:24 +00:00
|
|
|
|
2019-11-26 07:37:41 +00:00
|
|
|
for {
|
|
|
|
if strings.HasPrefix(line, "!") && !seenPrefix[0] {
|
|
|
|
seenPrefix[0] = true
|
|
|
|
line = line[1:]
|
2024-01-13 17:58:23 +00:00
|
|
|
pattern.result = pattern.result.ToggleIgnored()
|
2019-11-26 07:37:41 +00:00
|
|
|
} else if strings.HasPrefix(line, "(?i)") && !seenPrefix[1] {
|
|
|
|
seenPrefix[1] = true
|
2024-01-13 17:58:23 +00:00
|
|
|
pattern.result = pattern.result.WithFoldCase()
|
2019-11-26 07:37:41 +00:00
|
|
|
line = line[4:]
|
|
|
|
} else if strings.HasPrefix(line, "(?d)") && !seenPrefix[2] {
|
|
|
|
seenPrefix[2] = true
|
2024-01-13 17:58:23 +00:00
|
|
|
pattern.result = pattern.result.WithDeletable()
|
2019-11-26 07:37:41 +00:00
|
|
|
line = line[4:]
|
|
|
|
} else {
|
|
|
|
break
|
2016-04-05 06:35:51 +00:00
|
|
|
}
|
2019-11-26 07:37:41 +00:00
|
|
|
}
|
2016-04-05 06:35:51 +00:00
|
|
|
|
2020-01-30 08:58:44 +00:00
|
|
|
if line == "" {
|
2020-06-18 09:04:00 +00:00
|
|
|
return nil, parseError(errors.New("missing pattern"))
|
2020-01-30 08:58:44 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 07:37:41 +00:00
|
|
|
if pattern.result.IsCaseFolded() {
|
|
|
|
line = strings.ToLower(line)
|
|
|
|
}
|
|
|
|
|
|
|
|
pattern.pattern = line
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if strings.HasPrefix(line, "/") {
|
|
|
|
// Pattern is rooted in the current dir only
|
|
|
|
pattern.match, err = glob.Compile(line[1:], '/')
|
2020-06-18 09:04:00 +00:00
|
|
|
return []Pattern{pattern}, parseError(err)
|
2019-11-26 07:37:41 +00:00
|
|
|
}
|
|
|
|
patterns := make([]Pattern, 2)
|
|
|
|
if strings.HasPrefix(line, "**/") {
|
|
|
|
// Add the pattern as is, and without **/ so it matches in current dir
|
|
|
|
pattern.match, err = glob.Compile(line, '/')
|
|
|
|
if err != nil {
|
2020-06-18 09:04:00 +00:00
|
|
|
return nil, parseError(err)
|
2015-09-02 18:54:18 +00:00
|
|
|
}
|
2019-11-26 07:37:41 +00:00
|
|
|
patterns[0] = pattern
|
2015-09-02 18:54:18 +00:00
|
|
|
|
2019-11-26 07:37:41 +00:00
|
|
|
line = line[3:]
|
2016-05-23 23:32:08 +00:00
|
|
|
pattern.pattern = line
|
2019-11-26 07:37:41 +00:00
|
|
|
pattern.match, err = glob.Compile(line, '/')
|
|
|
|
if err != nil {
|
2020-06-18 09:04:00 +00:00
|
|
|
return nil, parseError(err)
|
2019-11-26 07:37:41 +00:00
|
|
|
}
|
|
|
|
patterns[1] = pattern
|
|
|
|
return patterns, nil
|
|
|
|
}
|
|
|
|
// Path name or pattern, add it so it matches files both in
|
|
|
|
// current directory and subdirs.
|
|
|
|
pattern.match, err = glob.Compile(line, '/')
|
|
|
|
if err != nil {
|
2020-06-18 09:04:00 +00:00
|
|
|
return nil, parseError(err)
|
2019-11-26 07:37:41 +00:00
|
|
|
}
|
|
|
|
patterns[0] = pattern
|
2016-05-23 23:32:08 +00:00
|
|
|
|
2019-11-26 07:37:41 +00:00
|
|
|
line = "**/" + line
|
|
|
|
pattern.pattern = line
|
|
|
|
pattern.match, err = glob.Compile(line, '/')
|
|
|
|
if err != nil {
|
2020-06-18 09:04:00 +00:00
|
|
|
return nil, parseError(err)
|
2019-11-26 07:37:41 +00:00
|
|
|
}
|
|
|
|
patterns[1] = pattern
|
|
|
|
return patterns, nil
|
|
|
|
}
|
2014-09-04 20:29:53 +00:00
|
|
|
|
2019-11-26 07:37:41 +00:00
|
|
|
func parseIgnoreFile(fs fs.Filesystem, fd io.Reader, currentFile string, cd ChangeDetector, linesSeen map[string]struct{}) ([]string, []Pattern, error) {
|
|
|
|
var patterns []Pattern
|
2014-09-04 20:29:53 +00:00
|
|
|
|
2019-11-26 07:37:41 +00:00
|
|
|
addPattern := func(line string) error {
|
|
|
|
newPatterns, err := parseLine(line)
|
|
|
|
if err != nil {
|
2020-06-18 09:04:00 +00:00
|
|
|
return fmt.Errorf("invalid pattern %q in ignore file: %w", line, err)
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
2019-11-26 07:37:41 +00:00
|
|
|
patterns = append(patterns, newPatterns...)
|
2014-09-04 20:29:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(fd)
|
2020-06-18 09:04:00 +00:00
|
|
|
var lines []string
|
2014-09-04 20:29:53 +00:00
|
|
|
for scanner.Scan() {
|
|
|
|
line := strings.TrimSpace(scanner.Text())
|
2017-04-01 09:58:06 +00:00
|
|
|
lines = append(lines, line)
|
2020-06-18 09:04:00 +00:00
|
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
for _, line := range lines {
|
2017-09-04 12:46:19 +00:00
|
|
|
if _, ok := linesSeen[line]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
linesSeen[line] = struct{}{}
|
2014-09-04 20:29:53 +00:00
|
|
|
switch {
|
|
|
|
case line == "":
|
|
|
|
continue
|
2014-09-16 21:22:21 +00:00
|
|
|
case strings.HasPrefix(line, "//"):
|
|
|
|
continue
|
2016-04-02 19:03:24 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 07:11:16 +00:00
|
|
|
line = filepath.ToSlash(line)
|
2016-04-02 19:03:24 +00:00
|
|
|
switch {
|
2018-01-17 16:56:53 +00:00
|
|
|
case strings.HasPrefix(line, "#include"):
|
2019-08-30 09:36:31 +00:00
|
|
|
fields := strings.SplitN(line, " ", 2)
|
|
|
|
if len(fields) != 2 {
|
2020-06-18 09:04:00 +00:00
|
|
|
err = parseError(errors.New("failed to parse #include line: no file?"))
|
2019-08-30 09:36:31 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
includeRel := strings.TrimSpace(fields[1])
|
|
|
|
if includeRel == "" {
|
2020-06-18 09:04:00 +00:00
|
|
|
err = parseError(errors.New("failed to parse #include line: no file?"))
|
2019-08-30 09:36:31 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2018-01-17 16:56:53 +00:00
|
|
|
includeFile := filepath.Join(filepath.Dir(currentFile), includeRel)
|
|
|
|
var includePatterns []Pattern
|
|
|
|
if includePatterns, err = loadParseIncludeFile(fs, includeFile, cd, linesSeen); err == nil {
|
|
|
|
patterns = append(patterns, includePatterns...)
|
2018-05-08 21:37:13 +00:00
|
|
|
} else {
|
|
|
|
// Wrap the error, as if the include does not exist, we get a
|
|
|
|
// IsNotExists(err) == true error, which we use to check
|
2022-08-23 13:44:11 +00:00
|
|
|
// existence of the .stignore file, and just end up assuming
|
2018-05-08 21:37:13 +00:00
|
|
|
// there is none, rather than a broken include.
|
2020-06-18 09:04:00 +00:00
|
|
|
err = parseError(fmt.Errorf("failed to load include file %s: %w", includeFile, err))
|
2018-01-17 16:56:53 +00:00
|
|
|
}
|
2014-09-04 20:29:53 +00:00
|
|
|
case strings.HasSuffix(line, "/**"):
|
|
|
|
err = addPattern(line)
|
|
|
|
case strings.HasSuffix(line, "/"):
|
2016-10-03 23:12:55 +00:00
|
|
|
err = addPattern(line + "**")
|
2014-09-04 20:29:53 +00:00
|
|
|
default:
|
|
|
|
err = addPattern(line)
|
|
|
|
if err == nil {
|
|
|
|
err = addPattern(line + "/**")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-06-18 09:04:00 +00:00
|
|
|
return lines, nil, err
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-01 09:58:06 +00:00
|
|
|
return lines, patterns, nil
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
2016-12-01 14:00:11 +00:00
|
|
|
|
2017-04-01 09:58:06 +00:00
|
|
|
// WriteIgnores is a convenience function to avoid code duplication
|
2017-08-19 14:36:56 +00:00
|
|
|
func WriteIgnores(filesystem fs.Filesystem, path string, content []string) error {
|
2019-12-02 07:19:02 +00:00
|
|
|
if len(content) == 0 {
|
|
|
|
err := filesystem.Remove(path)
|
|
|
|
if fs.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
fd, err := osutil.CreateAtomicFilesystem(filesystem, path)
|
2017-04-01 09:58:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-22 08:38:24 +00:00
|
|
|
wr := osutil.LineEndingsWriter(fd)
|
2017-04-01 09:58:06 +00:00
|
|
|
for _, line := range content {
|
2021-11-22 08:38:24 +00:00
|
|
|
fmt.Fprintln(wr, line)
|
2017-04-01 09:58:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := fd.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-02 11:16:27 +00:00
|
|
|
filesystem.Hide(path)
|
2017-04-01 09:58:06 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2017-06-11 10:27:12 +00:00
|
|
|
|
2017-08-22 06:45:00 +00:00
|
|
|
type modtimeCheckerKey struct {
|
|
|
|
fs fs.Filesystem
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2017-06-11 10:27:12 +00:00
|
|
|
// modtimeChecker is the default implementation of ChangeDetector
|
|
|
|
type modtimeChecker struct {
|
2017-08-22 06:45:00 +00:00
|
|
|
modtimes map[modtimeCheckerKey]time.Time
|
2017-06-11 10:27:12 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 06:45:00 +00:00
|
|
|
func newModtimeChecker() *modtimeChecker {
|
2017-06-11 10:27:12 +00:00
|
|
|
return &modtimeChecker{
|
2017-08-22 06:45:00 +00:00
|
|
|
modtimes: map[modtimeCheckerKey]time.Time{},
|
2017-06-11 10:27:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 06:45:00 +00:00
|
|
|
func (c *modtimeChecker) Remember(fs fs.Filesystem, name string, modtime time.Time) {
|
|
|
|
c.modtimes[modtimeCheckerKey{fs, name}] = modtime
|
2017-06-11 10:27:12 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 06:45:00 +00:00
|
|
|
func (c *modtimeChecker) Seen(fs fs.Filesystem, name string) bool {
|
|
|
|
_, ok := c.modtimes[modtimeCheckerKey{fs, name}]
|
2017-06-11 10:27:12 +00:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *modtimeChecker) Reset() {
|
2017-08-22 06:45:00 +00:00
|
|
|
c.modtimes = map[modtimeCheckerKey]time.Time{}
|
2017-06-11 10:27:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *modtimeChecker) Changed() bool {
|
2017-08-22 06:45:00 +00:00
|
|
|
for key, modtime := range c.modtimes {
|
|
|
|
info, err := key.fs.Stat(key.name)
|
2017-06-11 10:27:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if !info.ModTime().Equal(modtime) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|