Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix caching to respect .dockerignore #854

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions pkg/executor/composite_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,22 @@ func (s *CompositeCache) AddPath(p, context string) error {
if err != nil {
return err
}
if util.ExcludeFile(p, context) {
return os.ErrNotExist
}

if fi.Mode().IsDir() {
k, err := HashDir(p, context)
empty, k, err := hashDir(p, context)
if err != nil {
return err
}
s.keys = append(s.keys, k)

// Only add the hash of this directory to the key
// if there is any whitelisted content.
if !empty || !util.ExcludeFile(p, context) {
s.keys = append(s.keys, k)
}
return nil
}

if util.ExcludeFile(p, context) {
return nil
}
fh, err := util.CacheHasher()(p)
Expand All @@ -85,16 +91,14 @@ func (s *CompositeCache) AddPath(p, context string) error {
}

// HashDir returns a hash of the directory.
func HashDir(p, context string) (string, error) {
func hashDir(p, context string) (bool, string, error) {
sha := sha256.New()
empty := true
if err := filepath.Walk(p, func(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
exclude := util.ExcludeFile(path, context)
if fi.IsDir() && exclude {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still needed right so we can skip walking whitelisted directories?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this was onr if the bugs:
If the directory was ignored, but one the contained files was included via later rules in the ignore file, this would skip over this file completely.

The change now would still not include the directory in the cache key, but would include the contained file.

return filepath.SkipDir
}
if exclude {
return nil
}
Expand All @@ -106,10 +110,11 @@ func HashDir(p, context string) (string, error) {
if _, err := sha.Write([]byte(fileHash)); err != nil {
return err
}
empty = false
return nil
}); err != nil {
return "", err
return false, "", err
}

return fmt.Sprintf("%x", sha.Sum(nil)), nil
return empty, fmt.Sprintf("%x", sha.Sum(nil)), nil
}