This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1271 from sdboyer/pkgtree-hidden-trimming
Trim hidden packages from (root) PackageTrees
- Loading branch information
Showing
21 changed files
with
870 additions
and
353 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
internal/gps/_testdata/src/varied_hidden/.onlyfromtests/a.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// 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 onlyfromtests | ||
|
||
import ( | ||
"sort" | ||
|
||
_ "varied/_secondorder" | ||
|
||
"github.com/golang/dep/internal/gps" | ||
) | ||
|
||
var ( | ||
M = sort.Strings | ||
_ = gps.Solve | ||
) |
15 changes: 15 additions & 0 deletions
15
internal/gps/_testdata/src/varied_hidden/.onlyfromtests/b.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// 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 onlyfromtests | ||
|
||
import ( | ||
"os" | ||
"sort" | ||
) | ||
|
||
var ( | ||
_ = sort.Strings | ||
_ = os.PathSeparator | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// 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 frommain | ||
|
||
import ( | ||
"sort" | ||
|
||
"github.com/golang/dep/internal/gps" | ||
) | ||
|
||
var ( | ||
M = sort.Strings | ||
_ = gps.Solve | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// 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 never | ||
|
||
import ( | ||
"sort" | ||
|
||
"github.com/golang/dep/internal/gps" | ||
) | ||
|
||
var ( | ||
M = sort.Strings | ||
_ = gps.Solve | ||
) |
11 changes: 11 additions & 0 deletions
11
internal/gps/_testdata/src/varied_hidden/_secondorder/secondorder.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// 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 secondorder | ||
|
||
import "hash" | ||
|
||
var ( | ||
H = hash.Hash | ||
) |
7 changes: 7 additions & 0 deletions
7
internal/gps/_testdata/src/varied_hidden/always/always_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// 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 always | ||
|
||
import _ "varied/.onlyfromtests" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// 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 dotslash | ||
|
||
import ( | ||
"../github.com/golang/dep/internal/gps" | ||
) | ||
|
||
var ( | ||
A = gps.Solver | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// 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 main | ||
|
||
import ( | ||
_ "varied/_frommain" | ||
"varied/simple" | ||
) | ||
|
||
var ( | ||
_ = simple.S | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// 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 main | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
var ( | ||
_ = http.Client | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// 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 simple | ||
|
||
import "varied/simple/testdata" | ||
|
||
var ( | ||
_ = testdata.H | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// 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 simple | ||
|
||
import ( | ||
"go/parser" | ||
|
||
"github.com/golang/dep/internal/gps" | ||
) | ||
|
||
var ( | ||
_ = parser.ParseFile | ||
S = gps.Prepare | ||
) |
7 changes: 7 additions & 0 deletions
7
internal/gps/_testdata/src/varied_hidden/simple/testdata/another.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// 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 testdata | ||
|
||
import _ "varied/dotdotslash" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
} |
Oops, something went wrong.