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"
|
|
|
|
"crypto/md5"
|
2014-09-04 20:29:53 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-04-02 19:03:24 +00:00
|
|
|
"runtime"
|
2014-09-04 20:29:53 +00:00
|
|
|
"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"
|
2017-04-01 09:58:06 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/osutil"
|
2015-08-06 09:29:25 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
2014-09-04 20:29:53 +00:00
|
|
|
)
|
|
|
|
|
2016-05-01 15:58:23 +00:00
|
|
|
const (
|
2016-05-04 07:15:56 +00:00
|
|
|
resultNotMatched Result = 0
|
|
|
|
resultInclude Result = 1 << iota
|
|
|
|
resultDeletable = 1 << iota
|
|
|
|
resultFoldCase = 1 << iota
|
2016-05-01 15:58:23 +00:00
|
|
|
)
|
|
|
|
|
2014-09-04 20:29:53 +00:00
|
|
|
type Pattern struct {
|
2016-05-01 15:58:23 +00:00
|
|
|
pattern string
|
|
|
|
match glob.Glob
|
|
|
|
result Result
|
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
|
2016-05-01 15:58:23 +00:00
|
|
|
if p.result&resultInclude != resultInclude {
|
2016-04-02 19:03:24 +00:00
|
|
|
ret = "!" + ret
|
2014-12-23 09:05:08 +00:00
|
|
|
}
|
2016-05-01 15:58:23 +00:00
|
|
|
if p.result&resultFoldCase == resultFoldCase {
|
2016-04-02 19:03:24 +00:00
|
|
|
ret = "(?i)" + ret
|
|
|
|
}
|
2016-05-01 15:58:23 +00:00
|
|
|
if p.result&resultDeletable == resultDeletable {
|
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
|
|
|
}
|
|
|
|
|
2016-05-01 15:58:23 +00:00
|
|
|
type Result uint8
|
2016-04-07 09:34:07 +00:00
|
|
|
|
|
|
|
func (r Result) IsIgnored() bool {
|
2016-05-01 15:58:23 +00:00
|
|
|
return r&resultInclude == resultInclude
|
2016-04-07 09:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r Result) IsDeletable() bool {
|
2016-05-01 15:58:23 +00:00
|
|
|
return r.IsIgnored() && r&resultDeletable == resultDeletable
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r Result) IsCaseFolded() bool {
|
|
|
|
return r&resultFoldCase == resultFoldCase
|
2016-04-07 09:34:07 +00:00
|
|
|
}
|
|
|
|
|
2014-10-12 21:35:15 +00:00
|
|
|
type Matcher struct {
|
2017-04-01 09:58:06 +00:00
|
|
|
lines []string
|
2014-12-23 09:05:08 +00:00
|
|
|
patterns []Pattern
|
|
|
|
withCache bool
|
|
|
|
matches *cache
|
|
|
|
curHash string
|
|
|
|
stop chan struct{}
|
2016-11-22 21:30:45 +00:00
|
|
|
modtimes map[string]time.Time
|
2014-12-23 09:05:08 +00:00
|
|
|
mut sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(withCache bool) *Matcher {
|
|
|
|
m := &Matcher{
|
|
|
|
withCache: withCache,
|
|
|
|
stop: make(chan struct{}),
|
2015-04-22 22:54:31 +00:00
|
|
|
mut: sync.NewMutex(),
|
2014-12-23 09:05:08 +00:00
|
|
|
}
|
|
|
|
if withCache {
|
|
|
|
go m.clean(2 * time.Hour)
|
|
|
|
}
|
|
|
|
return m
|
2014-10-12 21:35:15 +00:00
|
|
|
}
|
2014-09-04 20:29:53 +00:00
|
|
|
|
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()
|
|
|
|
|
|
|
|
if m.patternsUnchanged(file) {
|
|
|
|
return nil
|
|
|
|
}
|
2014-12-23 09:05:08 +00:00
|
|
|
|
|
|
|
fd, err := os.Open(file)
|
|
|
|
if err != nil {
|
2016-11-22 21:30:45 +00:00
|
|
|
m.parseLocked(&bytes.Buffer{}, file)
|
2014-12-23 09:05:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
|
2016-11-22 21:30:45 +00:00
|
|
|
info, err := fd.Stat()
|
|
|
|
if err != nil {
|
|
|
|
m.parseLocked(&bytes.Buffer{}, file)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.modtimes = map[string]time.Time{
|
|
|
|
file: info.ModTime(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.parseLocked(fd, file)
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
|
|
|
|
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-04-01 09:58:06 +00:00
|
|
|
lines, patterns, err := parseIgnoreFile(r, file, m.modtimes)
|
2014-12-23 09:05:08 +00:00
|
|
|
// Error is saved and returned at the end. We process the patterns
|
|
|
|
// (possibly blank) anyway.
|
|
|
|
|
|
|
|
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
|
2017-04-01 09:58:06 +00:00
|
|
|
m.lines = lines
|
2014-12-23 09:05:08 +00:00
|
|
|
m.patterns = patterns
|
|
|
|
if m.withCache {
|
|
|
|
m.matches = newCache(patterns)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 21:30:45 +00:00
|
|
|
// patternsUnchanged returns true if none of the files making up the loaded
|
|
|
|
// patterns have changed since last check.
|
|
|
|
func (m *Matcher) patternsUnchanged(file string) bool {
|
|
|
|
if _, ok := m.modtimes[file]; !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for filename, modtime := range m.modtimes {
|
|
|
|
info, err := os.Stat(filename)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !info.ModTime().Equal(modtime) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-04-07 09:34:07 +00:00
|
|
|
func (m *Matcher) Match(file string) (result Result) {
|
2017-05-01 16:58:08 +00:00
|
|
|
if m == nil || file == "." {
|
2016-05-04 07:15:56 +00:00
|
|
|
return resultNotMatched
|
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 {
|
2016-05-04 07:15:56 +00:00
|
|
|
return resultNotMatched
|
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
|
|
|
}
|
|
|
|
|
2014-12-02 22:13:03 +00:00
|
|
|
// Check all the patterns for a match.
|
2016-04-02 19:03:24 +00:00
|
|
|
file = filepath.ToSlash(file)
|
|
|
|
var lowercaseFile string
|
2014-10-12 21:35:15 +00:00
|
|
|
for _, pattern := range m.patterns {
|
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) {
|
2016-05-01 15:58:23 +00:00
|
|
|
return pattern.result
|
2016-04-02 19:03:24 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if pattern.match.Match(file) {
|
2016-05-01 15:58:23 +00:00
|
|
|
return pattern.result
|
2016-04-02 19:03:24 +00:00
|
|
|
}
|
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.
|
|
|
|
return resultNotMatched
|
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 {
|
2015-04-27 19:49:10 +00:00
|
|
|
if m == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-17 07:33:48 +00:00
|
|
|
// ShouldIgnore returns true when a file is temporary, internal or ignored
|
|
|
|
func (m *Matcher) ShouldIgnore(filename string) bool {
|
|
|
|
switch {
|
|
|
|
case IsTemporary(filename):
|
|
|
|
return true
|
|
|
|
|
|
|
|
case IsInternal(filename):
|
|
|
|
return true
|
|
|
|
|
|
|
|
case m.Match(filename).IsIgnored():
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-12-23 09:05:08 +00:00
|
|
|
func hashPatterns(patterns []Pattern) string {
|
|
|
|
h := md5.New()
|
|
|
|
for _, pat := range patterns {
|
|
|
|
h.Write([]byte(pat.String()))
|
|
|
|
h.Write([]byte("\n"))
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%x", h.Sum(nil))
|
|
|
|
}
|
|
|
|
|
2017-04-01 09:58:06 +00:00
|
|
|
func loadIgnoreFile(file string, modtimes map[string]time.Time) ([]string, []Pattern, error) {
|
2016-11-22 21:30:45 +00:00
|
|
|
if _, ok := modtimes[file]; ok {
|
2017-04-01 09:58:06 +00:00
|
|
|
return nil, nil, fmt.Errorf("multiple include of ignore file %q", file)
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fd, err := os.Open(file)
|
|
|
|
if err != nil {
|
2017-04-01 09:58:06 +00:00
|
|
|
return nil, nil, err
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
|
2016-11-22 21:30:45 +00:00
|
|
|
info, err := fd.Stat()
|
|
|
|
if err != nil {
|
2017-04-01 09:58:06 +00:00
|
|
|
return nil, nil, err
|
2016-11-22 21:30:45 +00:00
|
|
|
}
|
|
|
|
modtimes[file] = info.ModTime()
|
|
|
|
|
|
|
|
return parseIgnoreFile(fd, file, modtimes)
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
|
|
|
|
2017-04-01 09:58:06 +00:00
|
|
|
func parseIgnoreFile(fd io.Reader, currentFile string, modtimes map[string]time.Time) ([]string, []Pattern, error) {
|
|
|
|
var lines []string
|
2014-12-23 09:05:08 +00:00
|
|
|
var patterns []Pattern
|
2014-09-04 20:29:53 +00:00
|
|
|
|
2016-05-01 15:58:23 +00:00
|
|
|
defaultResult := resultInclude
|
|
|
|
if runtime.GOOS == "darwin" || runtime.GOOS == "windows" {
|
|
|
|
defaultResult |= resultFoldCase
|
|
|
|
}
|
|
|
|
|
2014-09-04 20:29:53 +00:00
|
|
|
addPattern := func(line string) error {
|
2016-04-02 19:03:24 +00:00
|
|
|
pattern := Pattern{
|
2016-05-23 23:32:08 +00:00
|
|
|
result: defaultResult,
|
2016-04-02 19:03:24 +00:00
|
|
|
}
|
|
|
|
|
2016-04-07 09:34:07 +00:00
|
|
|
// Allow prefixes to be specified in any order, but only once.
|
|
|
|
var seenPrefix [3]bool
|
|
|
|
|
|
|
|
for {
|
|
|
|
if strings.HasPrefix(line, "!") && !seenPrefix[0] {
|
|
|
|
seenPrefix[0] = true
|
|
|
|
line = line[1:]
|
2016-05-01 15:58:23 +00:00
|
|
|
pattern.result ^= resultInclude
|
2016-04-07 09:34:07 +00:00
|
|
|
} else if strings.HasPrefix(line, "(?i)") && !seenPrefix[1] {
|
|
|
|
seenPrefix[1] = true
|
2016-05-01 15:58:23 +00:00
|
|
|
pattern.result |= resultFoldCase
|
2016-04-07 09:34:07 +00:00
|
|
|
line = line[4:]
|
|
|
|
} else if strings.HasPrefix(line, "(?d)") && !seenPrefix[2] {
|
|
|
|
seenPrefix[2] = true
|
2016-05-01 15:58:23 +00:00
|
|
|
pattern.result |= resultDeletable
|
2016-04-07 09:34:07 +00:00
|
|
|
line = line[4:]
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
2016-04-05 06:35:51 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 15:58:23 +00:00
|
|
|
if pattern.result.IsCaseFolded() {
|
2016-04-05 06:35:51 +00:00
|
|
|
line = strings.ToLower(line)
|
2015-09-02 18:54:18 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 23:32:08 +00:00
|
|
|
pattern.pattern = line
|
|
|
|
|
2016-04-02 19:03:24 +00:00
|
|
|
var err error
|
2014-09-04 20:29:53 +00:00
|
|
|
if strings.HasPrefix(line, "/") {
|
|
|
|
// Pattern is rooted in the current dir only
|
2016-10-21 07:33:40 +00:00
|
|
|
pattern.match, err = glob.Compile(line[1:], '/')
|
2014-09-04 20:29:53 +00:00
|
|
|
if err != nil {
|
2016-05-06 15:45:11 +00:00
|
|
|
return fmt.Errorf("invalid pattern %q in ignore file (%v)", line, err)
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
2016-04-02 19:03:24 +00:00
|
|
|
patterns = append(patterns, pattern)
|
2014-09-04 20:29:53 +00:00
|
|
|
} else if strings.HasPrefix(line, "**/") {
|
|
|
|
// Add the pattern as is, and without **/ so it matches in current dir
|
2016-10-21 07:33:40 +00:00
|
|
|
pattern.match, err = glob.Compile(line, '/')
|
2014-09-04 20:29:53 +00:00
|
|
|
if err != nil {
|
2016-05-06 15:45:11 +00:00
|
|
|
return fmt.Errorf("invalid pattern %q in ignore file (%v)", line, err)
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
2016-04-02 19:03:24 +00:00
|
|
|
patterns = append(patterns, pattern)
|
2014-09-04 20:29:53 +00:00
|
|
|
|
2016-04-04 12:22:25 +00:00
|
|
|
line = line[3:]
|
|
|
|
pattern.pattern = line
|
2016-10-21 07:33:40 +00:00
|
|
|
pattern.match, err = glob.Compile(line, '/')
|
2014-09-04 20:29:53 +00:00
|
|
|
if err != nil {
|
2016-05-06 15:45:11 +00:00
|
|
|
return fmt.Errorf("invalid pattern %q in ignore file (%v)", line, err)
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
2016-04-02 19:03:24 +00:00
|
|
|
patterns = append(patterns, pattern)
|
2014-09-04 20:29:53 +00:00
|
|
|
} else if strings.HasPrefix(line, "#include ") {
|
2015-09-29 16:01:19 +00:00
|
|
|
includeRel := line[len("#include "):]
|
|
|
|
includeFile := filepath.Join(filepath.Dir(currentFile), includeRel)
|
2017-04-01 09:58:06 +00:00
|
|
|
includeLines, includePatterns, err := loadIgnoreFile(includeFile, modtimes)
|
2014-09-04 20:29:53 +00:00
|
|
|
if err != nil {
|
2015-09-29 16:01:19 +00:00
|
|
|
return fmt.Errorf("include of %q: %v", includeRel, err)
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
2017-04-01 09:58:06 +00:00
|
|
|
lines = append(lines, includeLines...)
|
|
|
|
patterns = append(patterns, includePatterns...)
|
2014-09-04 20:29:53 +00:00
|
|
|
} else {
|
|
|
|
// Path name or pattern, add it so it matches files both in
|
|
|
|
// current directory and subdirs.
|
2016-10-21 07:33:40 +00:00
|
|
|
pattern.match, err = glob.Compile(line, '/')
|
2014-09-04 20:29:53 +00:00
|
|
|
if err != nil {
|
2016-05-06 15:45:11 +00:00
|
|
|
return fmt.Errorf("invalid pattern %q in ignore file (%v)", line, err)
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
2016-04-02 19:03:24 +00:00
|
|
|
patterns = append(patterns, pattern)
|
2014-09-04 20:29:53 +00:00
|
|
|
|
2016-04-04 12:22:25 +00:00
|
|
|
line := "**/" + line
|
|
|
|
pattern.pattern = line
|
2016-10-21 07:33:40 +00:00
|
|
|
pattern.match, err = glob.Compile(line, '/')
|
2014-09-04 20:29:53 +00:00
|
|
|
if err != nil {
|
2016-05-06 15:45:11 +00:00
|
|
|
return fmt.Errorf("invalid pattern %q in ignore file (%v)", line, err)
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
2016-04-02 19:03:24 +00:00
|
|
|
patterns = append(patterns, pattern)
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(fd)
|
|
|
|
var err error
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := strings.TrimSpace(scanner.Text())
|
2017-04-01 09:58:06 +00:00
|
|
|
lines = append(lines, line)
|
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 {
|
2014-09-04 20:29:53 +00:00
|
|
|
case strings.HasPrefix(line, "#"):
|
|
|
|
err = addPattern(line)
|
|
|
|
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 {
|
2017-04-01 09:58:06 +00:00
|
|
|
return nil, 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
|
|
|
|
|
|
|
// IsInternal returns true if the file, as a path relative to the folder
|
|
|
|
// root, represents an internal file that should always be ignored. The file
|
|
|
|
// path must be clean (i.e., in canonical shortest form).
|
|
|
|
func IsInternal(file string) bool {
|
|
|
|
internals := []string{".stfolder", ".stignore", ".stversions"}
|
|
|
|
pathSep := string(os.PathSeparator)
|
|
|
|
for _, internal := range internals {
|
|
|
|
if file == internal {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(file, internal+pathSep) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2017-04-01 09:58:06 +00:00
|
|
|
|
|
|
|
// WriteIgnores is a convenience function to avoid code duplication
|
|
|
|
func WriteIgnores(path string, content []string) error {
|
|
|
|
fd, err := osutil.CreateAtomic(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, line := range content {
|
|
|
|
fmt.Fprintln(fd, line)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := fd.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
osutil.HideFile(path)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|