aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--commons/hash.go17
-rw-r--r--commons/hash_test.go12
-rw-r--r--packfile/objects.go19
-rw-r--r--packfile/objects_test.go5
4 files changed, 34 insertions, 19 deletions
diff --git a/commons/hash.go b/commons/hash.go
new file mode 100644
index 0000000..03fa53f
--- /dev/null
+++ b/commons/hash.go
@@ -0,0 +1,17 @@
+package commons
+
+import (
+ "crypto/sha1"
+ "fmt"
+ "strconv"
+)
+
+func GitHash(t string, b []byte) string {
+ h := []byte(t)
+ h = append(h, ' ')
+ h = strconv.AppendInt(h, int64(len(b)), 10)
+ h = append(h, 0)
+ h = append(h, b...)
+
+ return fmt.Sprintf("%x", sha1.Sum(h))
+}
diff --git a/commons/hash_test.go b/commons/hash_test.go
new file mode 100644
index 0000000..79a22ea
--- /dev/null
+++ b/commons/hash_test.go
@@ -0,0 +1,12 @@
+package commons
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestCalculateHash(t *testing.T) {
+ assert.Equal(t, "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", GitHash("blob", []byte("")))
+ assert.Equal(t, "8ab686eafeb1f44702738c8b0f24f2567c36da6d", GitHash("blob", []byte("Hello, World!\n")))
+}
diff --git a/packfile/objects.go b/packfile/objects.go
index a73b8c3..6ed0a3b 100644
--- a/packfile/objects.go
+++ b/packfile/objects.go
@@ -2,11 +2,12 @@ package packfile
import (
"bytes"
- "crypto/sha1"
"encoding/hex"
"fmt"
"strconv"
"time"
+
+ "github.com/tyba/srcd-crawler/clients/git/commons"
)
type Object interface {
@@ -30,7 +31,7 @@ type Commit struct {
}
func NewCommit(b []byte) (*Commit, error) {
- o := &Commit{hash: calculateHash("commit", b)}
+ o := &Commit{hash: commons.GitHash("commit", b)}
lines := bytes.Split(b, []byte{'\n'})
for i := range lines {
@@ -147,7 +148,7 @@ type TreeEntry struct {
}
func NewTree(body []byte) (*Tree, error) {
- o := &Tree{hash: calculateHash("tree", body)}
+ o := &Tree{hash: commons.GitHash("tree", body)}
if len(body) == 0 {
return o, nil
@@ -185,7 +186,7 @@ type Blob struct {
}
func NewBlob(b []byte) (*Blob, error) {
- return &Blob{Len: len(b), hash: calculateHash("blob", b)}, nil
+ return &Blob{Len: len(b), hash: commons.GitHash("blob", b)}, nil
}
func (o *Blob) Type() string {
@@ -196,14 +197,4 @@ func (o *Blob) Hash() string {
return o.hash
}
-func calculateHash(objType string, content []byte) string {
- header := []byte(objType)
- header = append(header, ' ')
- header = strconv.AppendInt(header, int64(len(content)), 10)
- header = append(header, 0)
- header = append(header, content...)
-
- return fmt.Sprintf("%x", sha1.Sum(header))
-}
-
type ContentCallback func(hash string, content []byte)
diff --git a/packfile/objects_test.go b/packfile/objects_test.go
index 07609e3..93c348b 100644
--- a/packfile/objects_test.go
+++ b/packfile/objects_test.go
@@ -7,11 +7,6 @@ import (
"github.com/stretchr/testify/assert"
)
-func TestCalculateHash(t *testing.T) {
- assert.Equal(t, "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", calculateHash("blob", []byte("")))
- assert.Equal(t, "8ab686eafeb1f44702738c8b0f24f2567c36da6d", calculateHash("blob", []byte("Hello, World!\n")))
-}
-
func TestSignature(t *testing.T) {
cases := map[string]Signature{
`Foo Bar <foo@bar.com> 1257894000 +0100`: {