Skip to content

Commit

Permalink
Merge pull request #24 from WanderaOrg/folder-sync
Browse files Browse the repository at this point in the history
Delete obsolete files in target directory
  • Loading branch information
coufalja authored Mar 4, 2019
2 parents 12f4b04 + a07f822 commit dade091
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,13 +474,15 @@ func newFolderUploader(o UploaderOptions) (Uploader, error) {
}

func (u *folderUploader) Upload(commitID string, iter FileIter) error {
filesToKeep := make(map[string]bool)
err := iter.ForEach(func(file *object.File) error {
if filterFile(file, u.includes, u.excludes) {
src := path.Join(u.sourcePath, file.Name)
if _, err := os.Lstat(src); err == nil {
src, _ = filepath.Abs(src)
}
dst := path.Join(u.name, file.Name)
filesToKeep[dst] = true

source, err := os.Open(src)
if err != nil {
Expand Down Expand Up @@ -518,6 +520,16 @@ func (u *folderUploader) Upload(commitID string, iter FileIter) error {
return nil
})

err = filepath.Walk(u.name, func(path string, info os.FileInfo, err error) error {
if _, exists := filesToKeep[path]; !info.IsDir() && !exists {
err := os.Remove(path)
if err != nil {
return err
}
}
return nil
})

return err
}

Expand Down

0 comments on commit dade091

Please sign in to comment.