Skip to content

Commit

Permalink
pkgset: add transitive reduction
Browse files Browse the repository at this point in the history
Fixes #39
  • Loading branch information
egonelbre committed Dec 25, 2020
1 parent 0f0fb54 commit bab77ac
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
11 changes: 7 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (*ExprHelp) Usage() string {
# Basic operations
There are a few basic oprations specified for manipulating sets of packages.
a b c;
a b c;
a + b + c; add(a, b, c); or(a, b, c)
returns packages that are used by a or b
Expand Down Expand Up @@ -80,6 +80,9 @@ func (*ExprHelp) Usage() string {
reach(a, b);
lists packages from a that can reach a package in b
transitive(a);
a transitive reduction in package dependencies
# Tags and OS:
test=1(a);
Expand All @@ -94,17 +97,17 @@ func (*ExprHelp) Usage() string {
# Example expressions
github.com/loov/goda:deps
all dependencies for "github.com/loov/goda" package
all dependencies for "github.com/loov/goda" package
github.com/loov/goda/...:noroot
all dependencies for "github.com/loov/goda" sub-package
all dependencies for "github.com/loov/goda" sub-package
shared(github.com/loov/goda/pkgset, github.com/loov/goda/templates)
packages shared by "github.com/loov/goda/pkgset" and "github.com/loov/goda/templates"
github.com/loov/goda/...:noroot - golang.org/x/tools/...
all dependencies excluding golang.org/x/tools
reach(github.com/loov/goda/...:root, golang.org/x/tools/go/packages:root)
packages in github.com/loov/goda/ that use golang.org/x/tools/go/packages
`
Expand Down
7 changes: 7 additions & 0 deletions pkgset/calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ func Calc(parentContext context.Context, expr []string) (Set, error) {
args, err := evalArgs(ctx, e.Args)
return Reach(args[0], args[1]), err

case "transitive":
if len(e.Args) != 1 {
return nil, fmt.Errorf("reach requires one argument: %v", e)
}
args, err := evalArgs(ctx, e.Args)
return Transitive(args[0]), err

default:
return nil, fmt.Errorf("unknown func %v: %v", e.Name, e)
}
Expand Down
30 changes: 29 additions & 1 deletion pkgset/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (set Set) IncludeRecursive(p *packages.Package) {
func (set Set) Clone() Set {
r := make(Set, len(set))
for pid, p := range set {
r[pid] = p
r[pid] = p // TODO: make a deep clone
}
return r
}
Expand Down Expand Up @@ -187,6 +187,34 @@ func Reach(a, b Set) Set {
return result
}

// Transitive returns transitive reduction.
func Transitive(a Set) Set {
result := a.Clone()

var includeDeps func(p *packages.Package, r map[string]struct{})
includeDeps = func(p *packages.Package, r map[string]struct{}) {
for _, c := range p.Imports {
if _, visited := r[c.ID]; visited {
continue
}
r[c.ID] = struct{}{}
includeDeps(c, r)
}
}

for _, p := range result {
indirectDeps := make(map[string]struct{})
for _, c := range p.Imports {
includeDeps(c, indirectDeps)
}
for dep := range indirectDeps {
delete(p.Imports, dep)
}
}

return result
}

// Sources returns packages that don't have incoming edges
func Sources(a Set) Set {
incoming := map[string]int{}
Expand Down

0 comments on commit bab77ac

Please sign in to comment.