Skip to content

Commit

Permalink
Merge pull request golang#120 from adg/sort
Browse files Browse the repository at this point in the history
Analyze packages in deterministic order when generating hash
  • Loading branch information
sdboyer committed Dec 15, 2016
2 parents ee2eb69 + ac9abb9 commit e79bb7c
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ func (s *solver) HashInputs() []byte {
}

// Write each of the packages, or the errors that were found for a
// particular subpath, into the hash.
// particular subpath, into the hash. We need to do this in a
// deterministic order, so expand and sort the map.
var pkgs []PackageOrErr
for _, perr := range s.rpt.Packages {
pkgs = append(pkgs, perr)
}
sort.Sort(sortPackageOrErr(pkgs))
for _, perr := range pkgs {
if perr.Err != nil {
buf.WriteString(perr.Err.Error())
} else {
Expand Down Expand Up @@ -85,3 +91,25 @@ func (s *solver) HashInputs() []byte {
hd := sha256.Sum256(buf.Bytes())
return hd[:]
}

type sortPackageOrErr []PackageOrErr

func (s sortPackageOrErr) Len() int { return len(s) }
func (s sortPackageOrErr) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

func (s sortPackageOrErr) Less(i, j int) bool {
a, b := s[i], s[j]
if a.Err != nil || b.Err != nil {
// Sort errors last.
if b.Err == nil {
return false
}
if a.Err == nil {
return true
}
// And then by string.
return a.Err.Error() < b.Err.Error()
}
// And finally, sort by import path.
return a.P.ImportPath < b.P.ImportPath
}

0 comments on commit e79bb7c

Please sign in to comment.