Skip to content

Commit

Permalink
internal/loghash: move (soon-to-be-) shared code to its own package
Browse files Browse the repository at this point in the history
Change-Id: Id8c7d5752449b0bb82a2b21157cb067f6a8be18b
Reviewed-on: https://go-review.googlesource.com/10871
Reviewed-by: Brad Fitzpatrick <[email protected]>
  • Loading branch information
adg committed Jun 9, 2015
1 parent 777a5bf commit f825c79
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
7 changes: 3 additions & 4 deletions app/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package build
import (
"bytes"
"compress/gzip"
"crypto/sha1"
"errors"
"fmt"
"io"
Expand All @@ -24,6 +23,8 @@ import (
"appengine/datastore"

"cache"

"golang.org/x/build/internal/loghash"
)

const (
Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions internal/loghash/loghash.go
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit f825c79

Please sign in to comment.