Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6d49371

Browse files
committedMay 4, 2017
Add itemized feedback for dep resolution in init
- Compares manifest, lock and packageTree to separate direct and transitive dependencies. Based on the constraints, logs them as feedback. - Creates ConstraintFeedback struct to hold dep constraint data. - Adds GetUsingFeedback() and GetLockingFeedback() functions to generate feedback text.
1 parent 06f5960 commit 6d49371

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed
 

‎cmd/dep/init.go

+42
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
189189

190190
l.Memo = s.HashInputs()
191191

192+
// Log itemized feedback about the resolved dependencies
193+
itemizedFeedback(m, l, pkgT)
194+
192195
internal.Vlogf("Writing manifest and lock files.")
193196

194197
sw, err := dep.NewSafeWriter(m, nil, l, dep.VendorAlways)
@@ -239,6 +242,45 @@ func hasImportPathPrefix(s, prefix string) bool {
239242
return strings.HasPrefix(s, prefix+"/")
240243
}
241244

245+
// itemizedFeedback itemizes resolved dependencies and logs the solution as
246+
// feedback.
247+
func itemizedFeedback(m *dep.Manifest, l *dep.Lock, pkgT pkgtree.PackageTree) {
248+
// Algo:
249+
// loop through lock projects
250+
// 1. check in manifest for for same entry
251+
// 2. if exists in manifest, it's direct dep, goes to manifest & lock
252+
// 3. if not exists in manifest, check in pkgT
253+
// 3.a. if exists in pkgT, it's direct dep, hint constraint, lock only
254+
// 3.b. if not exists in pkgT, it's transitive dep, lock only
255+
// 4. log feedback
256+
for _, lock := range l.Projects() {
257+
r, v, _ := gps.VersionComponentStrings(lock.Version())
258+
projectPath := lock.Ident().ProjectRoot
259+
260+
cf := &internal.ConstraintFeedback{
261+
Version: v,
262+
Revision: r,
263+
ProjectPath: projectPath,
264+
}
265+
266+
if _, ok := m.Dependencies[projectPath]; ok {
267+
// Direct dependency
268+
cf.DependencyType = internal.DepTypeDirect
269+
cf.ConstraintType = internal.ConsTypeConstraint
270+
} else {
271+
if _, ok := pkgT.Packages[string(projectPath)]; ok {
272+
// Direct dependency, hint
273+
cf.DependencyType = internal.DepTypeDirect
274+
cf.ConstraintType = internal.ConsTypeHint
275+
} else {
276+
// Transitive dependency
277+
cf.DependencyType = internal.DepTypeTransitive
278+
}
279+
}
280+
cf.LogFeedback()
281+
}
282+
}
283+
242284
// getProjectPropertiesFromVersion takes a gps.Version and returns a proper
243285
// gps.ProjectProperties with Constraint value based on the provided version.
244286
func getProjectPropertiesFromVersion(v gps.Version) gps.ProjectProperties {

‎internal/internal.go

+45
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,21 @@ package internal
77
import (
88
"fmt"
99
"os"
10+
11+
"github.com/golang/dep/gps"
1012
)
1113

1214
// Verbose specifies if verbose logging is enabled.
1315
var Verbose bool
1416

17+
// Constraint types
18+
const ConsTypeConstraint = "constraint"
19+
const ConsTypeHint = "hint"
20+
21+
// Dependency types
22+
const DepTypeDirect = "direct dep"
23+
const DepTypeTransitive = "transitive dep"
24+
1525
func Logln(args ...interface{}) {
1626
fmt.Fprintln(os.Stderr, args...)
1727
}
@@ -27,3 +37,38 @@ func Vlogf(format string, args ...interface{}) {
2737
}
2838
Logf(format, args...)
2939
}
40+
41+
// ConstraintFeedback holds project constraint feedback data
42+
type ConstraintFeedback struct {
43+
Version, Revision, ConstraintType, DependencyType string
44+
ProjectPath gps.ProjectRoot
45+
}
46+
47+
// LogFeedback logs the feedback
48+
func (cf ConstraintFeedback) LogFeedback() {
49+
// "Using" feedback for direct dep
50+
if cf.DependencyType == DepTypeDirect {
51+
Logf(GetUsingFeedback(cf.Version, cf.ConstraintType, cf.DependencyType, string(cf.ProjectPath)))
52+
}
53+
// No "Locking" feedback for hints. "Locking" feedback only for constraint and
54+
// transitive dep
55+
if cf.ConstraintType != ConsTypeHint {
56+
Logf(GetLockingFeedback(cf.Version, cf.Revision, cf.DependencyType, string(cf.ProjectPath)))
57+
}
58+
}
59+
60+
// GetUsingFeedback returns dependency using feedback string.
61+
// Example:
62+
// Using ^1.0.0 as constraint for direct dep github.com/foo/bar
63+
// Using 1b8edb3 as hint for direct dep github.com/bar/baz
64+
func GetUsingFeedback(version, consType, depType, projectPath string) string {
65+
return fmt.Sprintf("Using %s as %s for %s %s", version, consType, depType, projectPath)
66+
}
67+
68+
// GetLockingFeedback returns dependency locking feedback string.
69+
// Example:
70+
// Locking in v1.1.4 (bc29b4f) for direct dep github.com/foo/bar
71+
// Locking in master (436f39d) for transitive dep github.com/baz/qux
72+
func GetLockingFeedback(version, revision, depType, projectPath string) string {
73+
return fmt.Sprintf("Locking in %s (%s) for %s %s", version, revision, depType, projectPath)
74+
}

‎internal/internal_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2016 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 internal
6+
7+
import (
8+
"testing"
9+
)
10+
11+
func TestGetConstraintString(t *testing.T) {
12+
cases := []struct {
13+
feedback string
14+
want string
15+
}{
16+
{
17+
feedback: GetUsingFeedback("^1.0.0", ConsTypeConstraint, DepTypeDirect, "github.com/foo/bar"),
18+
want: "Using ^1.0.0 as constraint for direct dep github.com/foo/bar",
19+
},
20+
{
21+
feedback: GetUsingFeedback("1b8edb3", ConsTypeHint, DepTypeDirect, "github.com/bar/baz"),
22+
want: "Using 1b8edb3 as hint for direct dep github.com/bar/baz",
23+
},
24+
{
25+
feedback: GetLockingFeedback("v1.1.4", "bc29b4f", DepTypeDirect, "github.com/foo/bar"),
26+
want: "Locking in v1.1.4 (bc29b4f) for direct dep github.com/foo/bar",
27+
},
28+
{
29+
feedback: GetLockingFeedback("master", "436f39d", DepTypeTransitive, "github.com/baz/qux"),
30+
want: "Locking in master (436f39d) for transitive dep github.com/baz/qux",
31+
},
32+
}
33+
34+
for _, c := range cases {
35+
if c.want != c.feedback {
36+
t.Fatalf("Feedbacks are not expected: \n\t(GOT) %v\n\t(WNT) %v", c.feedback, c.want)
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)
This repository has been archived.