Skip to content

Commit

Permalink
Rename exclusion -> negation
Browse files Browse the repository at this point in the history
Exclusion rules modified with the word "exclusion" to represent the opposite (negated exclusion, or "inclusion") was a very misleading name. I renamed it throughout as "negation" and "negated"

This does not modify any public package API
  • Loading branch information
brandonc committed Dec 4, 2023
1 parent 2a0c05e commit 101bfff
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 37 deletions.
14 changes: 7 additions & 7 deletions internal/ignorefiles/ignorerules.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ type Ruleset struct {
}

// ExcludesResult is the result of matching a path against a Ruleset. A result
// is a Match if it matches a set of paths that are excluded by the rules in a
// is Excluded if it matches a set of paths that are excluded by the rules in a
// Ruleset. A matching result is Dominating if none of the rules that follow it
// contain an exclusion, implying that if the rule excludes a directory,
// contain a negation, implying that if the rule excludes a directory,
// everything below that directory may be ignored.
type ExcludesResult struct {
Match bool
Excluded bool
Dominating bool
}

Expand Down Expand Up @@ -92,20 +92,20 @@ func (r *Ruleset) Excludes(path string) (ExcludesResult, error) {
}
}
if match {
foundMatch = !rule.excluded
dominating = foundMatch && !rule.exclusionsAfter
foundMatch = !rule.negated
dominating = foundMatch && !rule.negationsAfter
}
}
return ExcludesResult{
Match: foundMatch,
Excluded: foundMatch,
Dominating: dominating,
}, retErr
}

// Includes is the inverse of [Ruleset.Excludes].
func (r *Ruleset) Includes(path string) (bool, error) {
result, err := r.Excludes(path)
return !result.Match, err
return !result.Excluded, err
}

var DefaultRuleset *Ruleset
Expand Down
30 changes: 15 additions & 15 deletions internal/ignorefiles/terraformignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ func readRules(input io.Reader) ([]rule, error) {
rule := rule{}
// Exclusions
if pattern[0] == '!' {
rule.excluded = true
rule.negated = true
pattern = pattern[1:]
// Mark all previous rules as having exclusions after it
// Mark all previous rules as having negations after it
for i := currentRuleIndex; i >= 0; i-- {
if rules[i].exclusionsAfter {
if rules[i].negationsAfter {
break
}
rules[i].exclusionsAfter = true
rules[i].negationsAfter = true
}
}
// If it is a directory, add ** so we catch descendants
Expand All @@ -67,11 +67,11 @@ func readRules(input io.Reader) ([]rule, error) {
}

type rule struct {
val string // the value of the rule itself
excluded bool // ! is present, an exclusion rule
exclusionsAfter bool // exclusion rules appear after this rule
dirs []string // directories of the rule
regex *regexp.Regexp // regular expression to match for the rule
val string // the value of the rule itself
negated bool // prefixed by !, a negated rule
negationsAfter bool // negatied rules appear after this rule
dirs []string // directories of the rule
regex *regexp.Regexp // regular expression to match for the rule
}

func (r *rule) match(path string) (bool, error) {
Expand Down Expand Up @@ -170,16 +170,16 @@ func (r *rule) compile() error {

var defaultExclusions = []rule{
{
val: strings.Join([]string{"**", ".git", "**"}, string(os.PathSeparator)),
excluded: false,
val: strings.Join([]string{"**", ".git", "**"}, string(os.PathSeparator)),
negated: false,
},
{
val: strings.Join([]string{"**", ".terraform", "**"}, string(os.PathSeparator)),
excluded: false,
val: strings.Join([]string{"**", ".terraform", "**"}, string(os.PathSeparator)),
negated: false,
},
{
val: strings.Join([]string{"**", ".terraform", "modules", "**"}, string(os.PathSeparator)),
excluded: true,
val: strings.Join([]string{"**", ".terraform", "modules", "**"}, string(os.PathSeparator)),
negated: true,
},
}

Expand Down
10 changes: 5 additions & 5 deletions internal/ignorefiles/terraformignore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func TestTerraformIgnore(t *testing.T) {
t.Errorf("invalid rule syntax when checking %s at index %d", p.path, i)
continue
}
if result.Match != p.match {
if result.Excluded != p.match {
t.Fatalf("%s at index %d should be %t", p.path, i, p.match)
}
}
Expand All @@ -133,14 +133,14 @@ func TestTerraformIgnoreNoExclusionOptimization(t *testing.T) {
t.Fatalf("Expected 7 rules, got %d", len(rs.rules))
}

// reflects that no exclusions follow the last rule
// reflects that no negations follow the last rule
afterValue := false
for i := len(rs.rules) - 1; i >= 0; i-- {
r := rs.rules[i]
if r.exclusionsAfter != afterValue {
if r.negationsAfter != afterValue {
t.Errorf("Expected exclusionsAfter to be %v at index %d", afterValue, i)
}
if r.excluded {
if r.negated {
afterValue = true
}
}
Expand All @@ -156,7 +156,7 @@ func TestTerraformIgnoreNoExclusionOptimization(t *testing.T) {
}
}

if actual, _ := rs.Excludes("src/baz/ignored"); !actual.Match {
if actual, _ := rs.Excludes("src/baz/ignored"); !actual.Excluded {
t.Errorf("Expected %q to be excluded, but it was included", "src/baz/ignored")
}

Expand Down
4 changes: 2 additions & 2 deletions internal/ignorefiles/testdata/archive-dir/.terraformignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# ignore a directory
terraform.d/
# exclude ignoring a directory at the root
# negate ignoring a directory at the root
!/terraform.d/
# ignore a file at a subpath
**/foo/bar.tf
Expand All @@ -17,4 +17,4 @@ bar/something-[a-z].txt
# ignore a file
boop.txt
# but not one at the current directory
!/boop.txt
!/boop.txt
4 changes: 2 additions & 2 deletions slug.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,14 @@ func (p *Packer) packWalkFn(root, src, dst string, tarW *tar.Writer, meta *Meta,
return nil
}

if r := matchIgnoreRules(subpath, ignoreRules); r.Match {
if r := matchIgnoreRules(subpath, ignoreRules); r.Excluded {
return nil
}

// Catch directories so we don't end up with empty directories,
// the files are ignored correctly
if info.IsDir() {
if r := matchIgnoreRules(subpath+string(os.PathSeparator), ignoreRules); r.Match {
if r := matchIgnoreRules(subpath+string(os.PathSeparator), ignoreRules); r.Excluded {
if r.Dominating {
return filepath.SkipDir
} else {
Expand Down
4 changes: 2 additions & 2 deletions sourcebundle/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ func packagePrepareWalkFn(root string, ignoreRules *ignorefiles.Ruleset) filepat
if err != nil {
return fmt.Errorf("invalid .terraformignore rules: %#w", err)
}
if ignored.Match {
if ignored.Excluded {
err := os.RemoveAll(absPath)
if err != nil {
return fmt.Errorf("failed to remove ignored file %s: %s", relPath, err)
Expand All @@ -652,7 +652,7 @@ func packagePrepareWalkFn(root string, ignoreRules *ignorefiles.Ruleset) filepat
if err != nil {
return fmt.Errorf("invalid .terraformignore rules: %#w", err)
}
if ignored.Match {
if ignored.Excluded {
err := os.RemoveAll(absPath)
if err != nil {
return fmt.Errorf("failed to remove ignored file %s: %s", relPath, err)
Expand Down
4 changes: 2 additions & 2 deletions testdata/archive-dir-no-external/.terraformignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# ignore a directory
terraform.d/
# exclude ignoring a directory at the root
# negate ignoring a directory at the root
!/terraform.d/
# ignore a file at a subpath
**/foo/bar.tf
Expand All @@ -17,4 +17,4 @@ bar/something-[a-z].txt
# ignore a file
boop.txt
# but not one at the current directory
!/boop.txt
!/boop.txt
4 changes: 2 additions & 2 deletions testdata/archive-dir/.terraformignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# ignore a directory
terraform.d/
# exclude ignoring a directory at the root
# negate ignoring a directory at the root
!/terraform.d/
# ignore a file at a subpath
**/foo/bar.tf
Expand All @@ -17,4 +17,4 @@ bar/something-[a-z].txt
# ignore a file
boop.txt
# but not one at the current directory
!/boop.txt
!/boop.txt

0 comments on commit 101bfff

Please sign in to comment.