Browse Source

allow whitelist of directories using a regex match - DK

Signed-off-by: Andrew Kutta <andrew.kutta@gmail.com>
Andrew Kutta 6 years ago
parent
commit
3b871f7986
1 changed files with 26 additions and 1 deletions
  1. 26 1
      internal/plumbing/tree_diff.go

+ 26 - 1
internal/plumbing/tree_diff.go

@@ -2,6 +2,7 @@ package plumbing
 
 import (
 	"fmt"
+	"regexp"
 	"gopkg.in/src-d/enry.v1"
 	"io"
 	"log"
@@ -20,6 +21,7 @@ import (
 type TreeDiff struct {
 	core.NoopMerger
 	SkipDirs     []string
+	WhitelistDirs [] string
 	Languages    map[string]bool
 
 	previousTree *object.Tree
@@ -44,6 +46,11 @@ const (
 	ConfigTreeDiffLanguages = "TreeDiff.Languages"
 	// allLanguages denotes passing all files in.
 	allLanguages = "all"
+
+	// ConfigTreeDiffWhitelistedPrefixes is the name of the configuration option
+	// (TreeDiff.Configure()) which allows to set whitelisted path prefixes -
+	// directories or complete file names.
+	ConfigTreeDiffWhitelistPrefixes = "TreeDiff.WhitelistedPrefixes"
 )
 
 // defaultBlacklistedPrefixes is the list of file path prefixes which should be skipped by default.
@@ -96,7 +103,14 @@ func (treediff *TreeDiff) ListConfigurationOptions() []core.ConfigurationOption
 			"which disables this filter and lets all the files through.", allLanguages),
 		Flag:        "languages",
 		Type:        core.StringsConfigurationOption,
-		Default:     []string{allLanguages}},
+		Default:     []string{allLanguages} }, {
+	  Name:       ConfigTreeDiffWhitelistPrefixes,
+		Description: "List of whitelisted path prefixes (e.g. directories or specific files). " +
+			"Values are in the UNIX format (\"path/to/x\"). Values should *not* start with \"/\". " +
+			"Separated with commas \",\".",
+		Flag:        "whitelisted-prefixes",
+		Type:        core.StringsConfigurationOption,
+		Default:     []string{}},
 	}
 	return options[:]
 }
@@ -115,6 +129,10 @@ func (treediff *TreeDiff) Configure(facts map[string]interface{}) {
 		treediff.Languages = map[string]bool{}
 		treediff.Languages[allLanguages] = true
 	}
+
+	if val, exists := facts[ConfigTreeDiffWhitelistPrefixes].([]string); exists {
+		treediff.WhitelistDirs = val
+	}
 }
 
 // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
@@ -196,6 +214,13 @@ OUTER:
 				continue OUTER
 			}
 		}
+    for _, dir := range treediff.WhitelistDirs {
+			matchedTo, _ := regexp.MatchString(dir, change.To.Name)
+			matchedFrom, _ := regexp.MatchString(dir, change.From.Name)
+			if !matchedTo && !matchedFrom {
+				continue OUTER
+			}
+		}
 		var changeEntry object.ChangeEntry
 		if change.To.Tree == nil {
 			changeEntry = change.From