Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 98f625e

Browse files
authored
Merge pull request #1156 from darkowlzz/recursive-ignore
Wildcard ignores
2 parents 07298e2 + 276ead9 commit 98f625e

File tree

18 files changed

+420
-2
lines changed

18 files changed

+420
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
NEW FEATURES:
44

5+
* Wildcard ignore support. (#1156)
56
* Disable SourceManager lock by setting `DEPNOLOCK` environment variable.
67
(#1206)
78

cmd/dep/testdata/harness_tests/ensure/pkg-ignored/wildcard-ignore/final/Gopkg.lock

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ignored = ["github.com/golang/notexist/samples*"]
2+
3+
[[constraint]]
4+
branch = "master"
5+
name = "github.com/sdboyer/deptest"

cmd/dep/testdata/harness_tests/ensure/pkg-ignored/wildcard-ignore/initial/Gopkg.lock

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ignored = ["github.com/golang/notexist/samples*"]
2+
3+
[[constraint]]
4+
branch = "master"
5+
name = "github.com/sdboyer/deptest"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
_ "github.com/sdboyer/deptest"
9+
)
10+
11+
func main() {
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package samples
6+
7+
import _ "github.com/sdboyer/deptestdos"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package subsamples
6+
7+
import _ "github.com/sdboyer/dep-test"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"commands": [
3+
["ensure"]
4+
],
5+
"error-expected": "",
6+
"vendor-final": [
7+
"github.com/sdboyer/deptest"
8+
]
9+
}

docs/Gopkg.toml.md

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ You might also try [virtualgo](https://github.com/GetStream/vg), which installs
3636
ignored = ["github.com/user/project/badpkg"]
3737
```
3838

39+
Use wildcard character (*) to define a package prefix to be ignored. Use this
40+
to ignore any package and their subpackages.
41+
```toml
42+
ignored = ["github.com/user/project/badpkg*"]
43+
```
44+
3945
**Use this for:** preventing a package and any of that package's unique
4046
dependencies from being installed.
4147

internal/gps/hash.go

+16
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"strings"
1414
)
1515

16+
const wcIgnoreSuffix = "*"
17+
1618
// string headers used to demarcate sections in hash input creation
1719
const (
1820
hhConstraints = "-CONSTRAINTS-"
@@ -81,10 +83,24 @@ func (s *solver) writeHashingInputs(w io.Writer) {
8183
writeString(hhIgnores)
8284
ig := make([]string, 0, len(s.rd.ig))
8385
for pkg := range s.rd.ig {
86+
// Skip wildcard ignore. They are handled separately below.
87+
if strings.HasSuffix(pkg, wcIgnoreSuffix) {
88+
continue
89+
}
90+
8491
if !strings.HasPrefix(pkg, s.rd.rpt.ImportRoot) || !isPathPrefixOrEqual(s.rd.rpt.ImportRoot, pkg) {
8592
ig = append(ig, pkg)
8693
}
8794
}
95+
96+
// Add wildcard ignores to ignore list.
97+
if s.rd.igpfx != nil {
98+
s.rd.igpfx.Walk(func(s string, v interface{}) bool {
99+
ig = append(ig, s+"*")
100+
return false
101+
})
102+
}
103+
88104
sort.Strings(ig)
89105

90106
for _, igp := range ig {

internal/gps/hash_test.go

+91
Original file line numberDiff line numberDiff line change
@@ -594,3 +594,94 @@ func diffHashingInputs(s Solver, wnt []string) string {
594594
tw.Flush()
595595
return buf.String()
596596
}
597+
598+
func TestHashInputsIneffectualWildcardIgs(t *testing.T) {
599+
fix := basicFixtures["shared dependency with overlapping constraints"]
600+
601+
rm := fix.rootmanifest().(simpleRootManifest).dup()
602+
603+
params := SolveParameters{
604+
RootDir: string(fix.ds[0].n),
605+
RootPackageTree: fix.rootTree(),
606+
Manifest: rm,
607+
ProjectAnalyzer: naiveAnalyzer{},
608+
stdLibFn: func(string) bool { return false },
609+
mkBridgeFn: overrideMkBridge,
610+
}
611+
612+
cases := []struct {
613+
name string
614+
ignoreMap map[string]bool
615+
elems []string
616+
}{
617+
{
618+
name: "no wildcard ignores",
619+
elems: []string{
620+
hhConstraints,
621+
"a",
622+
"sv-1.0.0",
623+
"b",
624+
"sv-1.0.0",
625+
hhImportsReqs,
626+
"a",
627+
"b",
628+
hhIgnores,
629+
hhOverrides,
630+
hhAnalyzer,
631+
"naive-analyzer",
632+
"1",
633+
},
634+
},
635+
{
636+
name: "different wildcard ignores",
637+
ignoreMap: map[string]bool{
638+
"foobar*": true,
639+
"foobarbaz*": true,
640+
"foozapbar*": true,
641+
},
642+
elems: []string{
643+
hhConstraints,
644+
"a",
645+
"sv-1.0.0",
646+
"b",
647+
"sv-1.0.0",
648+
hhImportsReqs,
649+
"a",
650+
"b",
651+
hhIgnores,
652+
"foobar*",
653+
"foozapbar*",
654+
hhOverrides,
655+
hhAnalyzer,
656+
"naive-analyzer",
657+
"1",
658+
},
659+
},
660+
}
661+
662+
for _, c := range cases {
663+
t.Run(c.name, func(t *testing.T) {
664+
665+
rm.ig = c.ignoreMap
666+
667+
params.Manifest = rm
668+
669+
s, err := Prepare(params, newdepspecSM(fix.ds, nil))
670+
if err != nil {
671+
t.Fatalf("Unexpected error while prepping solver: %s", err)
672+
}
673+
674+
dig := s.HashInputs()
675+
h := sha256.New()
676+
677+
for _, v := range c.elems {
678+
h.Write([]byte(v))
679+
}
680+
correct := h.Sum(nil)
681+
682+
if !bytes.Equal(dig, correct) {
683+
t.Errorf("Hashes are not equal. Inputs:\n%s", diffHashingInputs(s, c.elems))
684+
}
685+
})
686+
}
687+
}

internal/gps/pkgtree/pkgtree.go

+55
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ import (
1818
"strconv"
1919
"strings"
2020
"unicode"
21+
22+
"github.com/armon/go-radix"
2123
)
2224

25+
// wildcard ignore suffix
26+
const wcIgnoreSuffix = "*"
27+
2328
// Package represents a Go package. It contains a subset of the information
2429
// go/build.Package does.
2530
type Package struct {
@@ -550,6 +555,9 @@ func (t PackageTree) ToReachMap(main, tests, backprop bool, ignore map[string]bo
550555
ignore = make(map[string]bool)
551556
}
552557

558+
// Radix tree for ignore prefixes.
559+
xt := CreateIgnorePrefixTree(ignore)
560+
553561
// world's simplest adjacency list
554562
workmap := make(map[string]wm)
555563

@@ -572,6 +580,14 @@ func (t PackageTree) ToReachMap(main, tests, backprop bool, ignore map[string]bo
572580
continue
573581
}
574582

583+
if xt != nil {
584+
// Skip ignored packages prefix matches
585+
_, _, ok := xt.LongestPrefix(ip)
586+
if ok {
587+
continue
588+
}
589+
}
590+
575591
// TODO (kris-nova) Disable to get staticcheck passing
576592
//imps = imps[:0]
577593

@@ -1026,3 +1042,42 @@ func uniq(a []string) []string {
10261042
}
10271043
return a[:i]
10281044
}
1045+
1046+
// CreateIgnorePrefixTree takes a set of strings to be ignored and returns a
1047+
// trie consisting of strings prefixed with wildcard ignore suffix (*).
1048+
func CreateIgnorePrefixTree(ig map[string]bool) *radix.Tree {
1049+
var xt *radix.Tree
1050+
1051+
// Create a sorted list of all the ignores to have a proper order in
1052+
// ignores parsing.
1053+
sortedIgnores := make([]string, len(ig))
1054+
for k := range ig {
1055+
sortedIgnores = append(sortedIgnores, k)
1056+
}
1057+
sort.Strings(sortedIgnores)
1058+
1059+
for _, i := range sortedIgnores {
1060+
// Skip global ignore.
1061+
if i == "*" {
1062+
continue
1063+
}
1064+
1065+
// Check if it's a recursive ignore.
1066+
if strings.HasSuffix(i, wcIgnoreSuffix) {
1067+
// Create trie if it doesn't exists.
1068+
if xt == nil {
1069+
xt = radix.New()
1070+
}
1071+
// Check if it is ineffectual.
1072+
_, _, ok := xt.LongestPrefix(i)
1073+
if ok {
1074+
// Skip ineffectual wildcard ignore.
1075+
continue
1076+
}
1077+
// Create the ignore prefix and insert in the radix tree.
1078+
xt.Insert(i[:len(i)-len(wcIgnoreSuffix)], true)
1079+
}
1080+
}
1081+
1082+
return xt
1083+
}

0 commit comments

Comments
 (0)