From f4512d3c6cd6765d0623c039febcabd1225ed908 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sun, 29 May 2011 10:38:21 -0700 Subject: [PATCH] blobref: add Sha1FromString helper and SizedBlobRef Equal/String --- lib/go/camli/blobref/blobref.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/go/camli/blobref/blobref.go b/lib/go/camli/blobref/blobref.go index a43eba5a5..f1a871f5d 100644 --- a/lib/go/camli/blobref/blobref.go +++ b/lib/go/camli/blobref/blobref.go @@ -48,6 +48,14 @@ type SizedBlobRef struct { Size int64 } +func (sb *SizedBlobRef) Equal(o SizedBlobRef) bool { + return sb.Size == o.Size && sb.BlobRef.String() == o.BlobRef.String() +} + +func (sb *SizedBlobRef) String() string { + return fmt.Sprintf("[%s %d bytes]", sb.BlobRef.String(), sb.Size) +} + type ReadSeekCloser interface { io.Reader io.Seeker @@ -122,8 +130,14 @@ func blobIfValid(hashname, digest string) *BlobRef { return newBlob(hashname, digest) } -func FromHash(name string, h hash.Hash) *BlobRef { - return newBlob(name, fmt.Sprintf("%x", h.Sum())) +func FromHash(hashfunc string, h hash.Hash) *BlobRef { + return newBlob(hashfunc, fmt.Sprintf("%x", h.Sum())) +} + +func Sha1FromString(s string) *BlobRef { + s1 := sha1.New() + s1.Write([]byte(s)) + return FromHash("sha1", s1) } // FromPattern takes a pattern and if it matches 's' with two exactly two valid