forked from golang/dep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ignored_ruleset.go
111 lines (94 loc) · 2.7 KB
/
ignored_ruleset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pkgtree
import (
"sort"
"strings"
"github.com/armon/go-radix"
)
// IgnoredRuleset comprises a set of rules for ignoring import paths. It can
// manage both literal and prefix-wildcard matches.
type IgnoredRuleset struct {
t *radix.Tree
}
// NewIgnoredRuleset processes a set of strings into an IgnoredRuleset. Strings
// that end in "*" are treated as wildcards, where any import path with a
// matching prefix will be ignored. IgnoredRulesets are immutable once created.
//
// Duplicate and redundant (i.e. a literal path that has a prefix of a wildcard
// path) declarations are discarded. Consequently, it is possible that the
// returned IgnoredRuleset may have a smaller Len() than the input slice.
func NewIgnoredRuleset(ig []string) *IgnoredRuleset {
if len(ig) == 0 {
return &IgnoredRuleset{}
}
ir := &IgnoredRuleset{
t: radix.New(),
}
// Sort the list of all the ignores in order to ensure that wildcard
// precedence is recorded correctly in the trie.
sort.Strings(ig)
for _, i := range ig {
// Skip global ignore and empty string.
if i == "*" || i == "" {
continue
}
_, wildi, has := ir.t.LongestPrefix(i)
// We may not always have a value here, but if we do, then it's a bool.
wild, _ := wildi.(bool)
// Check if it's a wildcard ignore.
if strings.HasSuffix(i, "*") {
// Check if it is ineffectual.
if has && wild {
// Skip ineffectual wildcard ignore.
continue
}
// Create the ignore prefix and insert in the radix tree.
ir.t.Insert(i[:len(i)-1], true)
} else if !has || !wild {
ir.t.Insert(i, false)
}
}
if ir.t.Len() == 0 {
ir.t = nil
}
return ir
}
// IsIgnored indicates whether the provided path should be ignored, according to
// the ruleset.
func (ir *IgnoredRuleset) IsIgnored(path string) bool {
if path == "" || ir == nil || ir.t == nil {
return false
}
prefix, wildi, has := ir.t.LongestPrefix(path)
return has && (wildi.(bool) || path == prefix)
}
// Len indicates the number of rules in the ruleset.
func (ir *IgnoredRuleset) Len() int {
if ir == nil || ir.t == nil {
return 0
}
return ir.t.Len()
}
// ToSlice converts the contents of the IgnoredRuleset to a string slice.
//
// This operation is symmetrically dual to NewIgnoredRuleset.
func (ir *IgnoredRuleset) ToSlice() []string {
irlen := ir.Len()
if irlen == 0 {
return nil
}
items := make([]string, 0, irlen)
ir.t.Walk(func(s string, v interface{}) bool {
if s != "" {
if v.(bool) {
items = append(items, s+"*")
} else {
items = append(items, s)
}
}
return false
})
return items
}