From f825c792c1dd1ef4d0224b6465fa7dbd99411291 Mon Sep 17 00:00:00 2001 From: Andrew Gerrand Date: Tue, 9 Jun 2015 15:17:19 -0700 Subject: [PATCH] internal/loghash: move (soon-to-be-) shared code to its own package Change-Id: Id8c7d5752449b0bb82a2b21157cb067f6a8be18b Reviewed-on: https://go-review.googlesource.com/10871 Reviewed-by: Brad Fitzpatrick --- app/build/build.go | 7 +++---- internal/loghash/loghash.go | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 internal/loghash/loghash.go diff --git a/app/build/build.go b/app/build/build.go index 3b52a7c025..bd13080a75 100644 --- a/app/build/build.go +++ b/app/build/build.go @@ -9,7 +9,6 @@ package build import ( "bytes" "compress/gzip" - "crypto/sha1" "errors" "fmt" "io" @@ -24,6 +23,8 @@ import ( "appengine/datastore" "cache" + + "golang.org/x/build/internal/loghash" ) const ( @@ -863,13 +864,11 @@ func (l *Log) Text() ([]byte, error) { } func PutLog(c appengine.Context, text string) (hash string, err error) { - h := sha1.New() - io.WriteString(h, text) b := new(bytes.Buffer) z, _ := gzip.NewWriterLevel(b, gzip.BestCompression) io.WriteString(z, text) z.Close() - hash = fmt.Sprintf("%x", h.Sum(nil)) + hash = loghash.New(text) key := datastore.NewKey(c, "Log", hash, 0, nil) _, err = datastore.Put(c, key, &Log{b.Bytes()}) return diff --git a/internal/loghash/loghash.go b/internal/loghash/loghash.go new file mode 100644 index 0000000000..d47e6fc860 --- /dev/null +++ b/internal/loghash/loghash.go @@ -0,0 +1,20 @@ +// Copyright 2015 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. + +// Package loghash provides the shared information for computing +// a log hash (as in https://build.golang.org/log/HASH). +package loghash + +import ( + "crypto/sha1" + "fmt" + "io" +) + +// New returns a new hash for the given log text. +func New(text string) (hash string) { + h := sha1.New() + io.WriteString(h, text) + return fmt.Sprintf("%x", h.Sum(nil)) +}