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

Commit a9bd7dd

Browse files
authored
Merge branch 'master' into patch-1
2 parents 43c2a99 + 91e47ee commit a9bd7dd

File tree

5 files changed

+39
-21
lines changed

5 files changed

+39
-21
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ BUG FIXES:
66
* Adaptively recover from dirty and corrupted git repositories in cache (#1279).
77
* Suppress git password prompts in more places (#1357).
88
* Fix `-no-vendor` flag for `ensure -update` (#1361).
9+
* Validate `git ls-remote` output and ignore all malformed lines (#1379)
910

1011
IMPROVEMENTS:
1112

cmd/dep/init.go

+20-19
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,19 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
8585
root = filepath.Join(ctx.WorkingDir, args[0])
8686
}
8787
if err := os.MkdirAll(root, os.FileMode(0777)); err != nil {
88-
return errors.Wrapf(err, "unable to create directory %s", root)
88+
return errors.Wrapf(err, "init failed: unable to create a directory at %s", root)
8989
}
9090
}
9191

9292
var err error
9393
p := new(dep.Project)
9494
if err = p.SetRoot(root); err != nil {
95-
return errors.Wrap(err, "NewProject")
95+
return errors.Wrapf(err, "init failed: unable to set the root project to %s", root)
9696
}
9797

9898
ctx.GOPATH, err = ctx.DetectProjectGOPATH(p)
9999
if err != nil {
100-
return errors.Wrapf(err, "ctx.DetectProjectGOPATH")
100+
return errors.Wrapf(err, "init failed: unable to detect the containing GOPATH")
101101
}
102102

103103
mf := filepath.Join(root, dep.ManifestName)
@@ -106,30 +106,30 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
106106

107107
mok, err := fs.IsRegular(mf)
108108
if err != nil {
109-
return err
109+
return errors.Wrapf(err, "init failed: unable to check for an existing manifest at %s", mf)
110110
}
111111
if mok {
112-
return errors.Errorf("manifest already exists: %s", mf)
112+
return errors.Errorf("init aborted: manifest already exists at %s", mf)
113113
}
114114
// Manifest file does not exist.
115115

116116
lok, err := fs.IsRegular(lf)
117117
if err != nil {
118-
return err
118+
return errors.Wrapf(err, "init failed: unable to check for an existing lock at %s", lf)
119119
}
120120
if lok {
121-
return errors.Errorf("invalid state: manifest %q does not exist, but lock %q does", mf, lf)
121+
return errors.Errorf("invalid aborted: lock already exists at %s", lf)
122122
}
123123

124124
ip, err := ctx.ImportForAbs(root)
125125
if err != nil {
126-
return errors.Wrap(err, "root project import")
126+
return errors.Wrapf(err, "init failed: unable to determine the import path for the root project %s", root)
127127
}
128128
p.ImportRoot = gps.ProjectRoot(ip)
129129

130130
sm, err := ctx.SourceManager()
131131
if err != nil {
132-
return errors.Wrap(err, "getSourceManager")
132+
return errors.Wrap(err, "init failed: unable to create a source manager")
133133
}
134134
sm.UseDefaultSignalHandling()
135135
defer sm.Release()
@@ -139,7 +139,7 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
139139
}
140140
pkgT, directDeps, err := getDirectDependencies(sm, p)
141141
if err != nil {
142-
return err
142+
return errors.Wrap(err, "init failed: unable to determine direct dependencies")
143143
}
144144
if ctx.Verbose {
145145
ctx.Out.Printf("Checked %d directories for packages.\nFound %d direct dependencies.\n", len(pkgT.Packages), len(directDeps))
@@ -149,14 +149,14 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
149149
rootAnalyzer := newRootAnalyzer(cmd.skipTools, ctx, directDeps, sm)
150150
p.Manifest, p.Lock, err = rootAnalyzer.InitializeRootManifestAndLock(root, p.ImportRoot)
151151
if err != nil {
152-
return err
152+
return errors.Wrap(err, "init failed: unable to prepare an initial manifest and lock for the solver")
153153
}
154154

155155
if cmd.gopath {
156156
gs := newGopathScanner(ctx, directDeps, sm)
157157
err = gs.InitializeRootManifestAndLock(p.Manifest, p.Lock)
158158
if err != nil {
159-
return err
159+
return errors.Wrap(err, "init failed: unable to scan the GOPATH for dependencies")
160160
}
161161
}
162162

@@ -176,17 +176,18 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
176176
}
177177

178178
if err := ctx.ValidateParams(sm, params); err != nil {
179-
return err
179+
return errors.Wrapf(err, "init failed: validation of solve parameters failed")
180180
}
181181

182182
s, err := gps.Prepare(params, sm)
183183
if err != nil {
184-
return errors.Wrap(err, "prepare solver")
184+
return errors.Wrap(err, "init failed: unable to prepare the solver")
185185
}
186186

187187
soln, err := s.Solve(context.TODO())
188188
if err != nil {
189-
return handleAllTheFailuresOfTheWorld(err)
189+
err = handleAllTheFailuresOfTheWorld(err)
190+
return errors.Wrap(err, "init failed: unable to solve the dependency graph")
190191
}
191192
p.Lock = dep.LockFromSolution(soln)
192193

@@ -196,31 +197,31 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
196197
// to generate the final lock memo.
197198
s, err = gps.Prepare(params, sm)
198199
if err != nil {
199-
return errors.Wrap(err, "prepare solver")
200+
return errors.Wrap(err, "init failed: unable to recalculate the lock digest")
200201
}
201202

202203
p.Lock.SolveMeta.InputsDigest = s.HashInputs()
203204

204205
// Pass timestamp (yyyyMMddHHmmss format) as suffix to backup name.
205206
vendorbak, err := dep.BackupVendor(vpath, time.Now().Format("20060102150405"))
206207
if err != nil {
207-
return err
208+
return errors.Wrap(err, "init failed: first backup vendor/, delete it, and then retry the previous command: failed to backup existing vendor directory")
208209
}
209210
if vendorbak != "" {
210211
ctx.Err.Printf("Old vendor backed up to %v", vendorbak)
211212
}
212213

213214
sw, err := dep.NewSafeWriter(p.Manifest, nil, p.Lock, dep.VendorAlways)
214215
if err != nil {
215-
return err
216+
return errors.Wrap(err, "init failed: unable to create a SafeWriter")
216217
}
217218

218219
logger := ctx.Err
219220
if !ctx.Verbose {
220221
logger = log.New(ioutil.Discard, "", 0)
221222
}
222223
if err := sw.Write(root, sm, !cmd.noExamples, logger); err != nil {
223-
return errors.Wrap(err, "safe write of manifest and lock")
224+
return errors.Wrap(err, "init failed: unable to write the manifest, lock and vendor directory to disk")
224225
}
225226

226227
return nil

cmd/dep/testdata/harness_tests/init/manifest-exists/testcase.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"commands": [
33
["init"]
44
],
5-
"error-expected": "manifest already exists:",
5+
"error-expected": "init aborted: manifest already exists",
66
"vendor-final": []
77
}

gps/prune.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func PruneProject(baseDir string, lp LockedProject, options PruneOptions, logger
7777

7878
if (options & PruneNestedVendorDirs) != 0 {
7979
if err := pruneNestedVendorDirs(projectDir); err != nil {
80-
return err
80+
return errors.Wrapf(err, "failed to prune nested vendor directories")
8181
}
8282
}
8383

gps/vcs_source.go

+16
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"os"
1212
"path/filepath"
13+
"regexp"
1314
"strings"
1415

1516
"github.com/Masterminds/semver"
@@ -117,6 +118,10 @@ func (bs *baseVCSSource) exportRevisionTo(ctx context.Context, r Revision, to st
117118
return fs.CopyDir(bs.repo.LocalPath(), to)
118119
}
119120

121+
var (
122+
gitHashRE = regexp.MustCompile(`^[a-f0-9]{40}$`)
123+
)
124+
120125
// gitSource is a generic git repository implementation that should work with
121126
// all standard git remotes.
122127
type gitSource struct {
@@ -238,6 +243,10 @@ func (s *gitSource) exportRevisionTo(ctx context.Context, rev Revision, to strin
238243
return nil
239244
}
240245

246+
func (s *gitSource) isValidHash(hash []byte) bool {
247+
return gitHashRE.Match(hash)
248+
}
249+
241250
func (s *gitSource) listVersions(ctx context.Context) (vlist []PairedVersion, err error) {
242251
r := s.repo
243252

@@ -298,6 +307,13 @@ func (s *gitSource) listVersions(ctx context.Context) (vlist []PairedVersion, er
298307
vlist = make([]PairedVersion, len(all))
299308
for _, pair := range all {
300309
var v PairedVersion
310+
// Valid `git ls-remote` output should start with hash, be at least
311+
// 45 chars long and 40th character should be '\t'
312+
//
313+
// See: https://github.com/golang/dep/pull/1160#issuecomment-328843519
314+
if len(pair) < 45 || pair[40] != '\t' || !s.isValidHash(pair[:40]) {
315+
continue
316+
}
301317
if string(pair[41:]) == "HEAD" {
302318
// If HEAD is present, it's always first
303319
headrev = Revision(pair[:40])

0 commit comments

Comments
 (0)