Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 465cba7

Browse files
committed
Extract GitHash function
1 parent b9471b1 commit 465cba7

File tree

4 files changed

+34
-19
lines changed

4 files changed

+34
-19
lines changed

commons/hash.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package commons
2+
3+
import (
4+
"crypto/sha1"
5+
"fmt"
6+
"strconv"
7+
)
8+
9+
func GitHash(t string, b []byte) string {
10+
h := []byte(t)
11+
h = append(h, ' ')
12+
h = strconv.AppendInt(h, int64(len(b)), 10)
13+
h = append(h, 0)
14+
h = append(h, b...)
15+
16+
return fmt.Sprintf("%x", sha1.Sum(h))
17+
}

commons/hash_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package commons
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestCalculateHash(t *testing.T) {
10+
assert.Equal(t, "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", GitHash("blob", []byte("")))
11+
assert.Equal(t, "8ab686eafeb1f44702738c8b0f24f2567c36da6d", GitHash("blob", []byte("Hello, World!\n")))
12+
}

packfile/objects.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package packfile
22

33
import (
44
"bytes"
5-
"crypto/sha1"
65
"encoding/hex"
76
"fmt"
87
"strconv"
98
"time"
9+
10+
"github.com/tyba/srcd-crawler/clients/git/commons"
1011
)
1112

1213
type Object interface {
@@ -30,7 +31,7 @@ type Commit struct {
3031
}
3132

3233
func NewCommit(b []byte) (*Commit, error) {
33-
o := &Commit{hash: calculateHash("commit", b)}
34+
o := &Commit{hash: commons.GitHash("commit", b)}
3435

3536
lines := bytes.Split(b, []byte{'\n'})
3637
for i := range lines {
@@ -147,7 +148,7 @@ type TreeEntry struct {
147148
}
148149

149150
func NewTree(body []byte) (*Tree, error) {
150-
o := &Tree{hash: calculateHash("tree", body)}
151+
o := &Tree{hash: commons.GitHash("tree", body)}
151152

152153
if len(body) == 0 {
153154
return o, nil
@@ -185,7 +186,7 @@ type Blob struct {
185186
}
186187

187188
func NewBlob(b []byte) (*Blob, error) {
188-
return &Blob{Len: len(b), hash: calculateHash("blob", b)}, nil
189+
return &Blob{Len: len(b), hash: commons.GitHash("blob", b)}, nil
189190
}
190191

191192
func (o *Blob) Type() string {
@@ -196,14 +197,4 @@ func (o *Blob) Hash() string {
196197
return o.hash
197198
}
198199

199-
func calculateHash(objType string, content []byte) string {
200-
header := []byte(objType)
201-
header = append(header, ' ')
202-
header = strconv.AppendInt(header, int64(len(content)), 10)
203-
header = append(header, 0)
204-
header = append(header, content...)
205-
206-
return fmt.Sprintf("%x", sha1.Sum(header))
207-
}
208-
209200
type ContentCallback func(hash string, content []byte)

packfile/objects_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ import (
77
"github.com/stretchr/testify/assert"
88
)
99

10-
func TestCalculateHash(t *testing.T) {
11-
assert.Equal(t, "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", calculateHash("blob", []byte("")))
12-
assert.Equal(t, "8ab686eafeb1f44702738c8b0f24f2567c36da6d", calculateHash("blob", []byte("Hello, World!\n")))
13-
}
14-
1510
func TestSignature(t *testing.T) {
1611
cases := map[string]Signature{
1712
`Foo Bar <[email protected]> 1257894000 +0100`: {

0 commit comments

Comments
 (0)