diff --git a/slug.go b/slug.go index 4f6ca94..be0c66e 100644 --- a/slug.go +++ b/slug.go @@ -149,14 +149,21 @@ func packWalkFn(root, src, dst string, tarW *tar.Writer, meta *Meta, dereference return nil } - if m := matchIgnoreRule(subpath, ignoreRules); m { - return nil + if m, r := matchIgnoreRule(subpath, ignoreRules); m && !(r.val == "**/*" && info.IsDir()) { + if info.IsDir() { + return filepath.SkipDir + } else { + return nil + } } // Catch directories so we don't end up with empty directories, // the files are ignored correctly if info.IsDir() { - if m := matchIgnoreRule(subpath+string(os.PathSeparator), ignoreRules); m { + if m, r := matchIgnoreRule(subpath+string(os.PathSeparator), ignoreRules); m { + if r.val != "**/*" { // Represents the `*` rule + return filepath.SkipDir + } return nil } } diff --git a/slug_test.go b/slug_test.go index 20a611f..ae9c052 100644 --- a/slug_test.go +++ b/slug_test.go @@ -75,7 +75,7 @@ func TestPack(t *testing.T) { // except for the .terraform/modules subdirectory. for _, file := range fileList { if strings.HasPrefix(file, ".terraform"+string(filepath.Separator)) && - !strings.HasPrefix(file, filepath.Clean(".terraform/modules")) { + !strings.HasPrefix(file, filepath.Clean(".terraform/modules")) && file != ".terraform"+string(filepath.Separator) { t.Fatalf("unexpected .terraform content: %s", file) } } diff --git a/terraformignore.go b/terraformignore.go index 4f478be..29748a4 100644 --- a/terraformignore.go +++ b/terraformignore.go @@ -74,22 +74,28 @@ func readRules(input io.Reader) []rule { return rules } -func matchIgnoreRule(path string, rules []rule) bool { - matched := false +func matchIgnoreRule(path string, rules []rule) (bool, *rule) { + var matched *rule = nil path = filepath.FromSlash(path) - for _, rule := range rules { + for idx := range rules { + rule := &rules[idx] match, _ := rule.match(path) if match { - matched = !rule.excluded + if rule.excluded { + return false, rule + } + if rule.val != "**/*" || matched == nil { + matched = rule + } } } - if matched { + if matched != nil { debug(true, path, "Skipping excluded path:", path) } - return matched + return matched != nil, matched } type rule struct { @@ -200,9 +206,13 @@ var defaultExclusions = []rule{ excluded: false, }, { - val: filepath.Join("**", ".terraform", "**"), + val: filepath.Join("**", ".terraform", "?**"), excluded: false, }, + { + val: filepath.Join("**", ".terraform", "modules"), + excluded: true, + }, { val: filepath.Join("**", ".terraform", "modules", "**"), excluded: true, diff --git a/terraformignore_test.go b/terraformignore_test.go index a5a13ac..1063c1a 100644 --- a/terraformignore_test.go +++ b/terraformignore_test.go @@ -7,7 +7,7 @@ import ( func TestTerraformIgnore(t *testing.T) { // path to directory without .terraformignore p := parseIgnoreFile("testdata/external-dir") - if len(p) != 3 { + if len(p) != 4 { t.Fatal("A directory without .terraformignore should get the default patterns") } @@ -22,6 +22,10 @@ func TestTerraformIgnore(t *testing.T) { paths := []file{ { path: ".terraform/", + match: false, + }, + { + path: ".terraform/plugins", match: true, }, { @@ -106,7 +110,7 @@ func TestTerraformIgnore(t *testing.T) { }, } for i, p := range paths { - match := matchIgnoreRule(p.path, ignoreRules) + match, _ := matchIgnoreRule(p.path, ignoreRules) if match != p.match { t.Fatalf("%s at index %d should be %t", p.path, i, p.match) }