2017-05-02 19:26:49 +03:00
|
|
|
// 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.
|
|
|
|
|
2016-07-12 21:56:12 +03:00
|
|
|
package gps
|
2016-04-20 03:17:02 +03:00
|
|
|
|
|
|
|
import (
|
2016-10-14 19:10:14 +03:00
|
|
|
"bytes"
|
2016-04-20 03:17:02 +03:00
|
|
|
"crypto/sha256"
|
2017-01-12 04:50:01 +03:00
|
|
|
"io"
|
2016-04-20 03:17:02 +03:00
|
|
|
"sort"
|
2017-03-12 08:18:22 +03:00
|
|
|
"strconv"
|
2017-03-11 01:43:16 +03:00
|
|
|
"strings"
|
2016-04-20 03:17:02 +03:00
|
|
|
)
|
|
|
|
|
2017-01-15 22:11:21 +03:00
|
|
|
// string headers used to demarcate sections in hash input creation
|
|
|
|
const (
|
|
|
|
hhConstraints = "-CONSTRAINTS-"
|
|
|
|
hhImportsReqs = "-IMPORTS/REQS-"
|
|
|
|
hhIgnores = "-IGNORES-"
|
|
|
|
hhOverrides = "-OVERRIDES-"
|
|
|
|
hhAnalyzer = "-ANALYZER-"
|
|
|
|
)
|
|
|
|
|
2016-07-27 19:23:37 +03:00
|
|
|
// HashInputs computes a hash digest of all data in SolveParams and the
|
|
|
|
// RootManifest that act as function inputs to Solve().
|
2016-04-20 03:17:02 +03:00
|
|
|
//
|
|
|
|
// The digest returned from this function is the same as the digest that would
|
2016-04-29 01:53:42 +03:00
|
|
|
// be included with a Solve() Result. As such, it's appropriate for comparison
|
|
|
|
// against the digest stored in a lock file, generated by a previous Solve(): if
|
|
|
|
// the digests match, then manifest and lock are in sync, and a Solve() is
|
|
|
|
// unnecessary.
|
2016-04-20 21:52:46 +03:00
|
|
|
//
|
|
|
|
// (Basically, this is for memoization.)
|
2017-01-11 03:24:58 +03:00
|
|
|
func (s *solver) HashInputs() (digest []byte) {
|
2017-01-12 04:50:01 +03:00
|
|
|
h := sha256.New()
|
|
|
|
s.writeHashingInputs(h)
|
2017-01-11 03:24:58 +03:00
|
|
|
|
2017-01-12 04:50:01 +03:00
|
|
|
hd := h.Sum(nil)
|
2017-01-11 03:24:58 +03:00
|
|
|
digest = hd[:]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-12 04:50:01 +03:00
|
|
|
func (s *solver) writeHashingInputs(w io.Writer) {
|
|
|
|
writeString := func(s string) {
|
2017-01-12 05:02:09 +03:00
|
|
|
// Skip zero-length string writes; it doesn't affect the real hash
|
|
|
|
// calculation, and keeps misleading newlines from showing up in the
|
|
|
|
// debug output.
|
|
|
|
if s != "" {
|
|
|
|
// All users of writeHashingInputs cannot error on Write(), so just
|
|
|
|
// ignore it
|
|
|
|
w.Write([]byte(s))
|
|
|
|
}
|
2017-01-12 04:50:01 +03:00
|
|
|
}
|
|
|
|
|
2017-01-15 22:11:21 +03:00
|
|
|
// We write "section headers" into the hash purely to ease scanning when
|
|
|
|
// debugging this input-constructing algorithm; as long as the headers are
|
|
|
|
// constant, then they're effectively a no-op.
|
|
|
|
writeString(hhConstraints)
|
|
|
|
|
2017-01-14 08:41:06 +03:00
|
|
|
// getApplicableConstraints will apply overrides, incorporate requireds,
|
|
|
|
// apply local ignores, drop stdlib imports, and finally trim out
|
|
|
|
// ineffectual constraints.
|
2017-05-18 04:13:59 +03:00
|
|
|
for _, pd := range s.rd.getApplicableConstraints(s.stdLibFn) {
|
2017-01-12 04:50:01 +03:00
|
|
|
writeString(string(pd.Ident.ProjectRoot))
|
|
|
|
writeString(pd.Ident.Source)
|
2017-04-15 07:50:12 +03:00
|
|
|
writeString(pd.Constraint.typedString())
|
2016-04-20 03:17:02 +03:00
|
|
|
}
|
|
|
|
|
2017-01-14 08:41:06 +03:00
|
|
|
// Write out each discrete import, including those derived from requires.
|
2017-01-15 22:11:21 +03:00
|
|
|
writeString(hhImportsReqs)
|
2017-05-18 04:13:59 +03:00
|
|
|
imports := s.rd.externalImportList(s.stdLibFn)
|
2017-01-14 08:41:06 +03:00
|
|
|
sort.Strings(imports)
|
|
|
|
for _, im := range imports {
|
|
|
|
writeString(im)
|
2016-06-21 08:30:41 +03:00
|
|
|
}
|
|
|
|
|
2017-01-14 08:41:06 +03:00
|
|
|
// Add ignores, skipping any that point under the current project root;
|
|
|
|
// those will have already been implicitly incorporated by the import
|
|
|
|
// lister.
|
2017-01-15 22:11:21 +03:00
|
|
|
writeString(hhIgnores)
|
2017-09-28 14:35:47 +03:00
|
|
|
|
2017-10-13 16:11:24 +03:00
|
|
|
ig := s.rd.ir.ToSlice()
|
2017-01-12 05:04:05 +03:00
|
|
|
sort.Strings(ig)
|
|
|
|
for _, igp := range ig {
|
2017-10-13 17:11:56 +03:00
|
|
|
// Typical prefix comparison checks will erroneously fail if the wildcard
|
|
|
|
// is present. Trim it off, if present.
|
|
|
|
tigp := strings.TrimSuffix(igp, "*")
|
|
|
|
if !strings.HasPrefix(tigp, s.rd.rpt.ImportRoot) || !isPathPrefixOrEqual(s.rd.rpt.ImportRoot, tigp) {
|
2017-10-13 16:11:24 +03:00
|
|
|
writeString(igp)
|
|
|
|
}
|
2016-07-01 05:03:11 +03:00
|
|
|
}
|
|
|
|
|
2017-01-14 08:41:06 +03:00
|
|
|
// Overrides *also* need their own special entry distinct from basic
|
|
|
|
// constraints, to represent the unique effects they can have on the entire
|
|
|
|
// solving process beyond root's immediate scope.
|
2017-01-15 22:11:21 +03:00
|
|
|
writeString(hhOverrides)
|
2017-01-12 08:17:37 +03:00
|
|
|
for _, pc := range s.rd.ovr.asSortedSlice() {
|
2017-01-12 04:50:01 +03:00
|
|
|
writeString(string(pc.Ident.ProjectRoot))
|
2016-12-17 21:39:04 +03:00
|
|
|
if pc.Ident.Source != "" {
|
2017-01-12 04:50:01 +03:00
|
|
|
writeString(pc.Ident.Source)
|
2016-07-27 19:23:37 +03:00
|
|
|
}
|
|
|
|
if pc.Constraint != nil {
|
2017-04-15 07:50:12 +03:00
|
|
|
writeString(pc.Constraint.typedString())
|
2016-07-27 19:23:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-15 22:11:21 +03:00
|
|
|
writeString(hhAnalyzer)
|
2017-06-14 19:56:28 +03:00
|
|
|
ai := s.rd.an.Info()
|
|
|
|
writeString(ai.Name)
|
|
|
|
writeString(strconv.Itoa(ai.Version))
|
2017-01-12 04:50:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// bytes.Buffer wrapper that injects newlines after each call to Write().
|
|
|
|
type nlbuf bytes.Buffer
|
|
|
|
|
|
|
|
func (buf *nlbuf) Write(p []byte) (n int, err error) {
|
|
|
|
n, _ = (*bytes.Buffer)(buf).Write(p)
|
|
|
|
(*bytes.Buffer)(buf).WriteByte('\n')
|
|
|
|
return n + 1, nil
|
2017-01-11 03:24:58 +03:00
|
|
|
}
|
2016-07-22 16:54:15 +03:00
|
|
|
|
2017-01-11 03:24:58 +03:00
|
|
|
// HashingInputsAsString returns the raw input data used by Solver.HashInputs()
|
|
|
|
// as a string.
|
|
|
|
//
|
|
|
|
// This is primarily intended for debugging purposes.
|
|
|
|
func HashingInputsAsString(s Solver) string {
|
|
|
|
ts := s.(*solver)
|
2017-01-12 04:50:01 +03:00
|
|
|
buf := new(nlbuf)
|
2017-01-11 03:24:58 +03:00
|
|
|
ts.writeHashingInputs(buf)
|
|
|
|
|
2017-01-12 04:50:01 +03:00
|
|
|
return (*bytes.Buffer)(buf).String()
|
2016-04-20 03:17:02 +03:00
|
|
|
}
|