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

Commit

Permalink
Improve itemized feedback algo
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
darkowlzz committed May 4, 2017
1 parent 6d49371 commit 7222809
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 34 deletions.
78 changes: 53 additions & 25 deletions cmd/dep/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
package main

import (
"encoding/hex"
"flag"
"fmt"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -246,37 +246,67 @@ func hasImportPathPrefix(s, prefix string) bool {
// feedback.
func itemizedFeedback(m *dep.Manifest, l *dep.Lock, pkgT pkgtree.PackageTree) {
// Algo:
// loop through lock projects
// 1. check in manifest for for same entry
// 2. if exists in manifest, it's direct dep, goes to manifest & lock
// 3. if not exists in manifest, check in pkgT
// 3.a. if exists in pkgT, it's direct dep, hint constraint, lock only
// 3.b. if not exists in pkgT, it's transitive dep, lock only
// 4. log feedback
// 1. collect direct external deps from PackageTree
// 2. loop through lock projects
// 2.a. check if the projects exists in direct deps list
// 2.b. if it's a direct dep:
// 2.b.i. if it has a version attached, constraint dep
// 2.b.ii. if it has revision only, hint dep
// 2.c. else it's a transitive dep
// 2.d. log feedback

// Extract direct external deps
rm, errmap := pkgT.ToReachMap(true, true, false, nil)
for fail := range errmap {
internal.Logf("Warning: %s", fail)
}

directDeps := rm.Flatten(false)

for _, lock := range l.Projects() {
r, v, _ := gps.VersionComponentStrings(lock.Version())
projectPath := lock.Ident().ProjectRoot
// Get locked revision, version and branch
r, v, b := gps.VersionComponentStrings(lock.Version())

// Check if it's a valid SHA1 digest and trim to 7 characters.
if len(r) == 40 {
if _, err := hex.DecodeString(r); err == nil {
// Valid SHA1 digest
r = r[0:7]
}
}

// Get LockedVersion
var ver string
if v != "" {
ver = v
} else if b != "" {
ver = b
}

cf := &internal.ConstraintFeedback{
Version: v,
Revision: r,
ProjectPath: projectPath,
LockedVersion: ver,
Revision: r,
ProjectPath: string(lock.Ident().ProjectRoot),
}

if _, ok := m.Dependencies[projectPath]; ok {
// Get manifest version if available
if pp, ok := m.Dependencies[lock.Ident().ProjectRoot]; ok {
cf.Version = pp.Constraint.String()
}

if contains(directDeps, cf.ProjectPath) {
// Direct dependency
cf.DependencyType = internal.DepTypeDirect
cf.ConstraintType = internal.ConsTypeConstraint
} else {
if _, ok := pkgT.Packages[string(projectPath)]; ok {
// Direct dependency, hint
cf.DependencyType = internal.DepTypeDirect
cf.ConstraintType = internal.ConsTypeHint
if cf.LockedVersion != "" {
cf.ConstraintType = internal.ConsTypeConstraint
} else {
// Transitive dependency
cf.DependencyType = internal.DepTypeTransitive
cf.ConstraintType = internal.ConsTypeHint
}
} else {
// Transitive dependency
cf.DependencyType = internal.DepTypeTransitive
}

cf.LogFeedback()
}
}
Expand Down Expand Up @@ -324,11 +354,9 @@ func getProjectData(ctx *dep.Ctx, pkgT pkgtree.PackageTree, cpr string, sm *gps.
ondisk := make(map[gps.ProjectRoot]gps.Version)

syncDep := func(pr gps.ProjectRoot, sm *gps.SourceMgr) {
message := "Cached"
if err := sm.SyncSourceFor(gps.ProjectIdentifier{ProjectRoot: pr}); err != nil {
message = "Unable to cache"
internal.Logf("%s %s", "Unable to cache", pr)
}
fmt.Fprintf(os.Stderr, "%s %s\n", message, pr)
}

rm, _ := pkgT.ToReachMap(true, true, false, nil)
Expand Down
18 changes: 10 additions & 8 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ package internal
import (
"fmt"
"os"

"github.com/golang/dep/gps"
)

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

// ConstraintFeedback holds project constraint feedback data
type ConstraintFeedback struct {
Version, Revision, ConstraintType, DependencyType string
ProjectPath gps.ProjectRoot
Version, LockedVersion, Revision, ConstraintType, DependencyType, ProjectPath string
}

// LogFeedback logs the feedback
func (cf ConstraintFeedback) LogFeedback() {
// "Using" feedback for direct dep
if cf.DependencyType == DepTypeDirect {
Logf(GetUsingFeedback(cf.Version, cf.ConstraintType, cf.DependencyType, string(cf.ProjectPath)))
ver := cf.Version
// revision as version for hint
if cf.ConstraintType == ConsTypeHint {
ver = cf.Revision
}
Logf(GetUsingFeedback(ver, cf.ConstraintType, cf.DependencyType, cf.ProjectPath))
}
// No "Locking" feedback for hints. "Locking" feedback only for constraint and
// transitive dep
// No "Locking" feedback for hints. "Locking" feedback only for constraint
// and transitive dep
if cf.ConstraintType != ConsTypeHint {
Logf(GetLockingFeedback(cf.Version, cf.Revision, cf.DependencyType, string(cf.ProjectPath)))
Logf(GetLockingFeedback(cf.LockedVersion, cf.Revision, cf.DependencyType, cf.ProjectPath))
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/internal_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 The Go Authors. All rights reserved.
// 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.

Expand Down

0 comments on commit 7222809

Please sign in to comment.