Optimize .dockerignore when there are exclusions
| ... | ... |
@@ -582,10 +582,36 @@ func TarWithOptions(srcPath string, options *TarOptions) (io.ReadCloser, error) |
| 582 | 582 |
} |
| 583 | 583 |
|
| 584 | 584 |
if skip {
|
| 585 |
- if !exceptions && f.IsDir() {
|
|
| 585 |
+ // If we want to skip this file and its a directory |
|
| 586 |
+ // then we should first check to see if there's an |
|
| 587 |
+ // excludes pattern (eg !dir/file) that starts with this |
|
| 588 |
+ // dir. If so then we can't skip this dir. |
|
| 589 |
+ |
|
| 590 |
+ // Its not a dir then so we can just return/skip. |
|
| 591 |
+ if !f.IsDir() {
|
|
| 592 |
+ return nil |
|
| 593 |
+ } |
|
| 594 |
+ |
|
| 595 |
+ // No exceptions (!...) in patterns so just skip dir |
|
| 596 |
+ if !exceptions {
|
|
| 586 | 597 |
return filepath.SkipDir |
| 587 | 598 |
} |
| 588 |
- return nil |
|
| 599 |
+ |
|
| 600 |
+ dirSlash := relFilePath + string(filepath.Separator) |
|
| 601 |
+ |
|
| 602 |
+ for _, pat := range patterns {
|
|
| 603 |
+ if pat[0] != '!' {
|
|
| 604 |
+ continue |
|
| 605 |
+ } |
|
| 606 |
+ pat = pat[1:] + string(filepath.Separator) |
|
| 607 |
+ if strings.HasPrefix(pat, dirSlash) {
|
|
| 608 |
+ // found a match - so can't skip this dir |
|
| 609 |
+ return nil |
|
| 610 |
+ } |
|
| 611 |
+ } |
|
| 612 |
+ |
|
| 613 |
+ // No matching exclusion dir so just skip dir |
|
| 614 |
+ return filepath.SkipDir |
|
| 589 | 615 |
} |
| 590 | 616 |
|
| 591 | 617 |
if seen[relFilePath] {
|