| OLD | NEW |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. | 1 // Copyright 2017 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package spec | 5 package spec |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "crypto/sha256" | 8 "crypto/sha256" |
| 9 "encoding/hex" | 9 "encoding/hex" |
| 10 "fmt" | 10 "fmt" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 D("name", pkg.Name). | 38 D("name", pkg.Name). |
| 39 Err() | 39 Err() |
| 40 } | 40 } |
| 41 } | 41 } |
| 42 | 42 |
| 43 return nil | 43 return nil |
| 44 } | 44 } |
| 45 | 45 |
| 46 // Hash hashes the contents of the supplied "spec" and returns the result as | 46 // Hash hashes the contents of the supplied "spec" and returns the result as |
| 47 // a hex-encoded string. | 47 // a hex-encoded string. |
| 48 func Hash(spec *vpython.Spec) string { | 48 // |
| 49 // If not empty, the contents of extra are prefixed to hash string. This can |
| 50 // be used to factor additional influences into the spec hash. |
| 51 func Hash(spec *vpython.Spec, extra string) string { |
| 49 data, err := proto.Marshal(spec) | 52 data, err := proto.Marshal(spec) |
| 50 if err != nil { | 53 if err != nil { |
| 51 panic(fmt.Errorf("failed to marshal proto: %v", err)) | 54 panic(fmt.Errorf("failed to marshal proto: %v", err)) |
| 52 } | 55 } |
| 53 | 56 |
| 54 mustWrite := func(v int, err error) { | 57 mustWrite := func(v int, err error) { |
| 55 if err != nil { | 58 if err != nil { |
| 56 panic(fmt.Errorf("impossible: %s", err)) | 59 panic(fmt.Errorf("impossible: %s", err)) |
| 57 } | 60 } |
| 58 } | 61 } |
| 59 | 62 |
| 60 hash := sha256.New() | 63 hash := sha256.New() |
| 64 if extra != "" { |
| 65 mustWrite(fmt.Fprintf(hash, "%s:", extra)) |
| 66 } |
| 61 mustWrite(fmt.Fprintf(hash, "%s:", vpython.Version)) | 67 mustWrite(fmt.Fprintf(hash, "%s:", vpython.Version)) |
| 62 mustWrite(hash.Write(data)) | 68 mustWrite(hash.Write(data)) |
| 63 return hex.EncodeToString(hash.Sum(nil)) | 69 return hex.EncodeToString(hash.Sum(nil)) |
| 64 } | 70 } |
| 65 | 71 |
| 66 type specPackageSlice []*vpython.Spec_Package | 72 type specPackageSlice []*vpython.Spec_Package |
| 67 | 73 |
| 68 func (s specPackageSlice) Len() int { return len(s) } | 74 func (s specPackageSlice) Len() int { return len(s) } |
| 69 func (s specPackageSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } | 75 func (s specPackageSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
| 70 | 76 |
| 71 func (s specPackageSlice) Less(i, j int) bool { | 77 func (s specPackageSlice) Less(i, j int) bool { |
| 72 return sortby.Chain{ | 78 return sortby.Chain{ |
| 73 func(i, j int) bool { return s[i].Name < s[j].Name }, | 79 func(i, j int) bool { return s[i].Name < s[j].Name }, |
| 74 func(i, j int) bool { return s[i].Version < s[j].Version }, | 80 func(i, j int) bool { return s[i].Version < s[j].Version }, |
| 75 }.Use(i, j) | 81 }.Use(i, j) |
| 76 } | 82 } |
| OLD | NEW |