diff options
| author | Petter Rasmussen | 2016-02-03 21:39:18 +0100 |
|---|---|---|
| committer | Petter Rasmussen | 2016-02-03 21:39:18 +0100 |
| commit | a8c5402742a2c3c1b79febda5dac3897bc98ba6b (patch) | |
| tree | 7b04ef04fb0d8f1ec23045577ee8bbe18cc89772 | |
| parent | 658985444c127d43e17fb760b38174aa87d88258 (diff) | |
| download | gdrive-a8c5402742a2c3c1b79febda5dac3897bc98ba6b.tar.bz2 | |
.gdriveignore support
| -rw-r--r-- | drive/sync.go | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/drive/sync.go b/drive/sync.go index 7e809dc..7b6c344 100644 --- a/drive/sync.go +++ b/drive/sync.go @@ -5,9 +5,12 @@ import ( "os" "path/filepath" "github.com/gyuho/goraph/graph" + "github.com/sabhiram/go-git-ignore" "google.golang.org/api/drive/v3" ) +const DefaultIgnoreFile = ".gdriveignore" + func (self *Drive) prepareSyncFiles(localPath string, root *drive.File) (*syncFiles, error) { localCh := make(chan struct{files []*localFile; err error}) remoteCh := make(chan struct{files []*remoteFile; err error}) @@ -48,6 +51,12 @@ func prepareLocalFiles(root string) ([]*localFile, error) { return nil, err } + // Skip file if it is ignored by ignore file + shouldIgnore, err := prepareIgnorer(filepath.Join(absRootPath, DefaultIgnoreFile)) + if err != nil { + return nil, err + } + err = filepath.Walk(absRootPath, func(absPath string, info os.FileInfo, err error) error { if err != nil { return err @@ -63,6 +72,10 @@ func prepareLocalFiles(root string) ([]*localFile, error) { return err } + if shouldIgnore(relPath) { + return nil + } + files = append(files, &localFile{ absPath: absPath, relPath: relPath, @@ -376,3 +389,22 @@ func (self byRemotePathLength) Swap(i, j int) { func (self byRemotePathLength) Less(i, j int) bool { return pathLength(self[i].relPath) < pathLength(self[j].relPath) } + +type ignoreFunc func(string) bool + +func prepareIgnorer(path string) (ignoreFunc, error) { + acceptAll := func(string) bool { + return false + } + + if !fileExists(path) { + return acceptAll, nil + } + + ignorer, err := ignore.CompileIgnoreFile(path) + if err != nil { + return acceptAll, fmt.Errorf("Failed to prepare ignorer: %s", err) + } + + return ignorer.MatchesPath, nil +} |
