2020-02-15 22:13:50 +00:00
|
|
|
package dehub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"hash"
|
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
defaultHashHelperAlgo = sha256.New
|
|
|
|
)
|
|
|
|
|
|
|
|
type hashHelper struct {
|
2020-04-18 19:26:32 +00:00
|
|
|
hash hash.Hash
|
2020-02-15 22:13:50 +00:00
|
|
|
varintBuf []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// if h is nil it then defaultHashHelperAlgo will be used
|
|
|
|
func newHashHelper(h hash.Hash) *hashHelper {
|
|
|
|
if h == nil {
|
|
|
|
h = defaultHashHelperAlgo()
|
|
|
|
}
|
|
|
|
s := &hashHelper{
|
2020-04-18 19:26:32 +00:00
|
|
|
hash: h,
|
2020-02-15 22:13:50 +00:00
|
|
|
varintBuf: make([]byte, binary.MaxVarintLen64),
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2020-03-21 18:24:38 +00:00
|
|
|
func (s *hashHelper) sum(prefix []byte) []byte {
|
2020-04-18 19:26:32 +00:00
|
|
|
out := make([]byte, len(prefix), len(prefix)+s.hash.Size())
|
2020-03-21 18:24:38 +00:00
|
|
|
copy(out, prefix)
|
2020-04-18 19:26:32 +00:00
|
|
|
return s.hash.Sum(out)
|
2020-03-21 18:24:38 +00:00
|
|
|
}
|
|
|
|
|
2020-02-15 22:13:50 +00:00
|
|
|
func (s *hashHelper) writeUint(i uint64) {
|
|
|
|
n := binary.PutUvarint(s.varintBuf, i)
|
2020-04-18 19:26:32 +00:00
|
|
|
if _, err := s.hash.Write(s.varintBuf[:n]); err != nil {
|
|
|
|
panic(fmt.Sprintf("error writing %x to %T: %v", s.varintBuf[:n], s.hash, err))
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *hashHelper) writeStr(str string) {
|
|
|
|
s.writeUint(uint64(len(str)))
|
2020-04-18 19:26:32 +00:00
|
|
|
s.hash.Write([]byte(str))
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
|
2020-04-18 19:26:32 +00:00
|
|
|
func (s *hashHelper) writeChangedFiles(changedFiles []ChangedFile) {
|
|
|
|
sort.Slice(changedFiles, func(i, j int) bool {
|
|
|
|
return changedFiles[i].Path < changedFiles[j].Path
|
2020-02-15 22:13:50 +00:00
|
|
|
})
|
|
|
|
|
2020-04-18 19:26:32 +00:00
|
|
|
s.writeUint(uint64(len(changedFiles)))
|
|
|
|
for _, fileChanged := range changedFiles {
|
|
|
|
s.writeStr(fileChanged.Path)
|
|
|
|
s.hash.Write(fileChanged.FromMode.Bytes())
|
|
|
|
s.hash.Write(fileChanged.FromHash[:])
|
|
|
|
s.hash.Write(fileChanged.ToMode.Bytes())
|
|
|
|
s.hash.Write(fileChanged.ToHash[:])
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-21 18:24:38 +00:00
|
|
|
var (
|
|
|
|
changeHashVersion = []byte{0}
|
|
|
|
commentHashVersion = []byte{0}
|
|
|
|
)
|
2020-02-15 22:13:50 +00:00
|
|
|
|
|
|
|
// if h is nil it then defaultHashHelperAlgo will be used
|
2020-04-26 20:23:03 +00:00
|
|
|
func genChangeFingerprint(h hash.Hash, msg string, changedFiles []ChangedFile) []byte {
|
2020-02-15 22:13:50 +00:00
|
|
|
s := newHashHelper(h)
|
|
|
|
s.writeStr(msg)
|
2020-04-18 19:26:32 +00:00
|
|
|
s.writeChangedFiles(changedFiles)
|
2020-03-21 18:24:38 +00:00
|
|
|
return s.sum(changeHashVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
// if h is nil it then defaultHashHelperAlgo will be used
|
2020-04-26 20:23:03 +00:00
|
|
|
func genCommentFingerprint(h hash.Hash, comment string) []byte {
|
2020-03-21 18:24:38 +00:00
|
|
|
s := newHashHelper(h)
|
|
|
|
s.writeStr(comment)
|
|
|
|
return s.sum(commentHashVersion)
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|