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

Commit 7676985

Browse files
committed
Improve itemized feedback algo
- Uses PackageTree.ToReachMap() to collect direct external deps. - Separates version into Version and LockedVersion in ConstraintFeedback. Version is the version in manifest and LockedVersion is repo version or branch of locked project. Version is used for "Using" feedback and "LockedVersion" is used for "Locking" feedback. - Checks revision to for valid SHA1 digest and trimps to 7 characters. - Removes "Cached" log message from getProjectData()'s syncDep.
1 parent 6d49371 commit 7676985

File tree

3 files changed

+64
-34
lines changed

3 files changed

+64
-34
lines changed

cmd/dep/init.go

+53-25
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
package main
66

77
import (
8+
"encoding/hex"
89
"flag"
9-
"fmt"
1010
"log"
1111
"os"
1212
"path/filepath"
@@ -246,37 +246,67 @@ func hasImportPathPrefix(s, prefix string) bool {
246246
// feedback.
247247
func itemizedFeedback(m *dep.Manifest, l *dep.Lock, pkgT pkgtree.PackageTree) {
248248
// 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
249+
// 1. collect direct external deps from PackageTree
250+
// 2. loop through lock projects
251+
// 2.a. check if the projects exists in direct deps list
252+
// 2.b. if it's a direct dep:
253+
// 2.b.i. if it has a version attached, constraint dep
254+
// 2.b.ii. if it has revision only, hint dep
255+
// 2.c. else it's a transitive dep
256+
// 2.d. log feedback
257+
258+
// Extract direct external deps
259+
rm, errmap := pkgT.ToReachMap(true, true, false, nil)
260+
for fail := range errmap {
261+
internal.Logf("Warning: %s", fail)
262+
}
263+
264+
directDeps := rm.Flatten(false)
265+
256266
for _, lock := range l.Projects() {
257-
r, v, _ := gps.VersionComponentStrings(lock.Version())
258-
projectPath := lock.Ident().ProjectRoot
267+
// Get locked revision, version and branch
268+
r, v, b := gps.VersionComponentStrings(lock.Version())
269+
270+
// Check if it's a valid SHA1 digest and trim to 7 characters.
271+
if len(r) == 40 {
272+
if _, err := hex.DecodeString(r); err == nil {
273+
// Valid SHA1 digest
274+
r = r[0:7]
275+
}
276+
}
277+
278+
// Get LockedVersion
279+
var ver string
280+
if v != "" {
281+
ver = v
282+
} else if b != "" {
283+
ver = b
284+
}
259285

260286
cf := &internal.ConstraintFeedback{
261-
Version: v,
262-
Revision: r,
263-
ProjectPath: projectPath,
287+
LockedVersion: ver,
288+
Revision: r,
289+
ProjectPath: string(lock.Ident().ProjectRoot),
264290
}
265291

266-
if _, ok := m.Dependencies[projectPath]; ok {
292+
// Get manifest version if available
293+
if pr, ok := m.Dependencies[lock.Ident().ProjectRoot]; ok {
294+
cf.Version = pr.Constraint.String()
295+
}
296+
297+
if contains(directDeps, cf.ProjectPath) {
267298
// Direct dependency
268299
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
300+
if cf.LockedVersion != "" {
301+
cf.ConstraintType = internal.ConsTypeConstraint
275302
} else {
276-
// Transitive dependency
277-
cf.DependencyType = internal.DepTypeTransitive
303+
cf.ConstraintType = internal.ConsTypeHint
278304
}
305+
} else {
306+
// Transitive dependency
307+
cf.DependencyType = internal.DepTypeTransitive
279308
}
309+
280310
cf.LogFeedback()
281311
}
282312
}
@@ -324,11 +354,9 @@ func getProjectData(ctx *dep.Ctx, pkgT pkgtree.PackageTree, cpr string, sm *gps.
324354
ondisk := make(map[gps.ProjectRoot]gps.Version)
325355

326356
syncDep := func(pr gps.ProjectRoot, sm *gps.SourceMgr) {
327-
message := "Cached"
328357
if err := sm.SyncSourceFor(gps.ProjectIdentifier{ProjectRoot: pr}); err != nil {
329-
message = "Unable to cache"
358+
internal.Logf("%s %s", "Unable to cache", pr)
330359
}
331-
fmt.Fprintf(os.Stderr, "%s %s\n", message, pr)
332360
}
333361

334362
rm, _ := pkgT.ToReachMap(true, true, false, nil)

internal/internal.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ package internal
77
import (
88
"fmt"
99
"os"
10-
11-
"github.com/golang/dep/gps"
1210
)
1311

1412
// Verbose specifies if verbose logging is enabled.
@@ -40,20 +38,24 @@ func Vlogf(format string, args ...interface{}) {
4038

4139
// ConstraintFeedback holds project constraint feedback data
4240
type ConstraintFeedback struct {
43-
Version, Revision, ConstraintType, DependencyType string
44-
ProjectPath gps.ProjectRoot
41+
Version, LockedVersion, Revision, ConstraintType, DependencyType, ProjectPath string
4542
}
4643

4744
// LogFeedback logs the feedback
4845
func (cf ConstraintFeedback) LogFeedback() {
4946
// "Using" feedback for direct dep
5047
if cf.DependencyType == DepTypeDirect {
51-
Logf(GetUsingFeedback(cf.Version, cf.ConstraintType, cf.DependencyType, string(cf.ProjectPath)))
48+
ver := cf.Version
49+
// revision as version for hint
50+
if cf.ConstraintType == ConsTypeHint {
51+
ver = cf.Revision
52+
}
53+
Logf(GetUsingFeedback(ver, cf.ConstraintType, cf.DependencyType, cf.ProjectPath))
5254
}
53-
// No "Locking" feedback for hints. "Locking" feedback only for constraint and
54-
// transitive dep
55+
// No "Locking" feedback for hints. "Locking" feedback only for constraint
56+
// and transitive dep
5557
if cf.ConstraintType != ConsTypeHint {
56-
Logf(GetLockingFeedback(cf.Version, cf.Revision, cf.DependencyType, string(cf.ProjectPath)))
58+
Logf(GetLockingFeedback(cf.LockedVersion, cf.Revision, cf.DependencyType, cf.ProjectPath))
5759
}
5860
}
5961

internal/internal_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2016 The Go Authors. All rights reserved.
1+
// Copyright 2017 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

0 commit comments

Comments
 (0)