Skip to content

Commit cb39e77

Browse files
authored
Merge pull request #20 from bk2204/sha256
Add support for SHA-256
2 parents b4f6b4d + 202cc02 commit cb39e77

33 files changed

+644
-387
lines changed

backend.go

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,36 @@ package gitobj
22

33
import (
44
"bufio"
5+
"hash"
56
"io"
67
"os"
78
"path"
89
"regexp"
910
"strconv"
1011
"strings"
1112

12-
"github.com/git-lfs/gitobj/pack"
13-
"github.com/git-lfs/gitobj/storage"
13+
"github.com/git-lfs/gitobj/v2/pack"
14+
"github.com/git-lfs/gitobj/v2/storage"
1415
)
1516

16-
// NewFilesystemBackend initializes a new filesystem-based backend.
17-
func NewFilesystemBackend(root, tmp string) (storage.Backend, error) {
18-
return NewFilesystemBackendWithAlternates(root, tmp, "")
19-
}
20-
21-
// NewFilesystemBackendWithAlternates initializes a new filesystem-based
22-
// backend, optionally with additional alternates as specified in the
17+
// NewFilesystemBackend initializes a new filesystem-based backend,
18+
// optionally with additional alternates as specified in the
2319
// `alternates` variable. The syntax is that of the Git environment variable
24-
// GIT_ALTERNATE_OBJECT_DIRECTORIES.
25-
func NewFilesystemBackendWithAlternates(root, tmp, alternates string) (storage.Backend, error) {
20+
// GIT_ALTERNATE_OBJECT_DIRECTORIES. The hash algorithm used is specified by
21+
// the algo parameter.
22+
func NewFilesystemBackend(root, tmp, alternates string, algo hash.Hash) (storage.Backend, error) {
2623
fsobj := newFileStorer(root, tmp)
27-
packs, err := pack.NewStorage(root)
24+
packs, err := pack.NewStorage(root, algo)
2825
if err != nil {
2926
return nil, err
3027
}
3128

32-
storage, err := findAllBackends(fsobj, packs, root)
29+
storage, err := findAllBackends(fsobj, packs, root, algo)
3330
if err != nil {
3431
return nil, err
3532
}
3633

37-
storage, err = addAlternatesFromEnvironment(storage, alternates)
34+
storage, err = addAlternatesFromEnvironment(storage, alternates, algo)
3835
if err != nil {
3936
return nil, err
4037
}
@@ -45,7 +42,7 @@ func NewFilesystemBackendWithAlternates(root, tmp, alternates string) (storage.B
4542
}, nil
4643
}
4744

48-
func findAllBackends(mainLoose *fileStorer, mainPacked *pack.Storage, root string) ([]storage.Storage, error) {
45+
func findAllBackends(mainLoose *fileStorer, mainPacked *pack.Storage, root string, algo hash.Hash) ([]storage.Storage, error) {
4946
storage := make([]storage.Storage, 2)
5047
storage[0] = mainLoose
5148
storage[1] = mainPacked
@@ -61,7 +58,7 @@ func findAllBackends(mainLoose *fileStorer, mainPacked *pack.Storage, root strin
6158

6259
scanner := bufio.NewScanner(f)
6360
for scanner.Scan() {
64-
storage, err = addAlternateDirectory(storage, scanner.Text())
61+
storage, err = addAlternateDirectory(storage, scanner.Text(), algo)
6562
if err != nil {
6663
return nil, err
6764
}
@@ -74,24 +71,24 @@ func findAllBackends(mainLoose *fileStorer, mainPacked *pack.Storage, root strin
7471
return storage, nil
7572
}
7673

77-
func addAlternateDirectory(s []storage.Storage, dir string) ([]storage.Storage, error) {
74+
func addAlternateDirectory(s []storage.Storage, dir string, algo hash.Hash) ([]storage.Storage, error) {
7875
s = append(s, newFileStorer(dir, ""))
79-
pack, err := pack.NewStorage(dir)
76+
pack, err := pack.NewStorage(dir, algo)
8077
if err != nil {
8178
return s, err
8279
}
8380
s = append(s, pack)
8481
return s, nil
8582
}
8683

87-
func addAlternatesFromEnvironment(s []storage.Storage, env string) ([]storage.Storage, error) {
84+
func addAlternatesFromEnvironment(s []storage.Storage, env string, algo hash.Hash) ([]storage.Storage, error) {
8885
if len(env) == 0 {
8986
return s, nil
9087
}
9188

9289
for _, dir := range splitAlternateString(env, alternatesSeparator) {
9390
var err error
94-
s, err = addAlternateDirectory(s, dir)
91+
s, err = addAlternateDirectory(s, dir, algo)
9592
if err != nil {
9693
return nil, err
9794
}

blob.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package gitobj
33
import (
44
"bytes"
55
"fmt"
6+
"hash"
67
"io"
78
"os"
89
)
@@ -75,7 +76,7 @@ func (b *Blob) Type() ObjectType { return BlobObjectType }
7576
// stream, which is always zero.
7677
//
7778
// If any errors are encountered while reading the blob, they will be returned.
78-
func (b *Blob) Decode(r io.Reader, size int64) (n int, err error) {
79+
func (b *Blob) Decode(hash hash.Hash, r io.Reader, size int64) (n int, err error) {
7980
b.Size = size
8081
b.Contents = io.LimitReader(r, size)
8182

blob_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gitobj
22

33
import (
44
"bytes"
5+
"crypto/sha1"
56
"errors"
67
"io/ioutil"
78
"strings"
@@ -48,7 +49,7 @@ func TestBlobDecoding(t *testing.T) {
4849
from := strings.NewReader(contents)
4950

5051
b := new(Blob)
51-
n, err := b.Decode(from, int64(len(contents)))
52+
n, err := b.Decode(sha1.New(), from, int64(len(contents)))
5253

5354
assert.Equal(t, 0, n)
5455
assert.Nil(t, err)

commit.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"bytes"
66
"encoding/hex"
77
"fmt"
8+
"hash"
89
"io"
910
"strings"
1011
"time"
@@ -91,12 +92,12 @@ func (c *Commit) Type() ObjectType { return CommitObjectType }
9192
//
9293
// If any error was encountered along the way, that will be returned, along with
9394
// the number of bytes read up to that point.
94-
func (c *Commit) Decode(from io.Reader, size int64) (n int, err error) {
95+
func (c *Commit) Decode(hash hash.Hash, from io.Reader, size int64) (n int, err error) {
9596
var finishedHeaders bool
9697
var messageParts []string
9798

9899
s := bufio.NewScanner(from)
99-
s.Buffer(nil, 1024 * 1024)
100+
s.Buffer(nil, 1024*1024)
100101
for s.Scan() {
101102
text := s.Text()
102103
n = n + len(text+"\n")

commit_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gitobj
22

33
import (
44
"bytes"
5+
"crypto/sha1"
56
"encoding/hex"
67
"fmt"
78
"io"
@@ -77,7 +78,7 @@ func TestCommitDecoding(t *testing.T) {
7778
flen := from.Len()
7879

7980
commit := new(Commit)
80-
n, err := commit.Decode(from, int64(flen))
81+
n, err := commit.Decode(sha1.New(), from, int64(flen))
8182

8283
assert.Nil(t, err)
8384
assert.Equal(t, flen, n)
@@ -107,7 +108,7 @@ func TestCommitDecodingWithEmptyName(t *testing.T) {
107108
flen := from.Len()
108109

109110
commit := new(Commit)
110-
n, err := commit.Decode(from, int64(flen))
111+
n, err := commit.Decode(sha1.New(), from, int64(flen))
111112

112113
assert.Nil(t, err)
113114
assert.Equal(t, flen, n)
@@ -118,10 +119,10 @@ func TestCommitDecodingWithEmptyName(t *testing.T) {
118119
}
119120

120121
func TestCommitDecodingWithLargeCommitMessage(t *testing.T) {
121-
message := "This message text is, with newline, exactly 64 characters long. ";
122+
message := "This message text is, with newline, exactly 64 characters long. "
122123
// This message will be exactly 1 MiB in size when part of the commit.
123-
longMessage := strings.Repeat(message, (1024 * 1024 / 64) - 1)
124-
longMessage += strings.TrimSpace(message);
124+
longMessage := strings.Repeat(message, (1024*1024/64)-1)
125+
longMessage += strings.TrimSpace(message)
125126

126127
author := &Signature{Name: "", Email: "john@example.com", When: time.Now()}
127128
committer := &Signature{Name: "", Email: "jane@example.com", When: time.Now()}
@@ -138,7 +139,7 @@ func TestCommitDecodingWithLargeCommitMessage(t *testing.T) {
138139
flen := from.Len()
139140

140141
commit := new(Commit)
141-
n, err := commit.Decode(from, int64(flen))
142+
n, err := commit.Decode(sha1.New(), from, int64(flen))
142143

143144
assert.Nil(t, err)
144145
assert.Equal(t, flen, n)
@@ -164,7 +165,7 @@ func TestCommitDecodingWithMessageKeywordPrefix(t *testing.T) {
164165
flen := from.Len()
165166

166167
commit := new(Commit)
167-
n, err := commit.Decode(from, int64(flen))
168+
n, err := commit.Decode(sha1.New(), from, int64(flen))
168169

169170
assert.NoError(t, err)
170171
assert.Equal(t, flen, n)
@@ -191,7 +192,7 @@ func TestCommitDecodingWithWhitespace(t *testing.T) {
191192
flen := from.Len()
192193

193194
commit := new(Commit)
194-
n, err := commit.Decode(from, int64(flen))
195+
n, err := commit.Decode(sha1.New(), from, int64(flen))
195196

196197
assert.NoError(t, err)
197198
assert.Equal(t, flen, n)
@@ -221,7 +222,7 @@ func TestCommitDecodingMultilineHeader(t *testing.T) {
221222
flen := from.Len()
222223

223224
commit := new(Commit)
224-
n, err := commit.Decode(from, int64(flen))
225+
n, err := commit.Decode(sha1.New(), from, int64(flen))
225226

226227
require.Nil(t, err)
227228
require.Equal(t, flen, n)

file_storer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"os"
99
"path/filepath"
1010

11-
"github.com/git-lfs/gitobj/errors"
11+
"github.com/git-lfs/gitobj/v2/errors"
1212
)
1313

1414
// fileStorer implements the storer interface by writing to the .git/objects

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/git-lfs/gitobj
1+
module github.com/git-lfs/gitobj/v2
22

33
require (
44
github.com/davecgh/go-spew v1.1.1 // indirect

memory_storer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"io"
77
"sync"
88

9-
"github.com/git-lfs/gitobj/errors"
9+
"github.com/git-lfs/gitobj/v2/errors"
1010
)
1111

1212
// memoryStorer is an implementation of the storer interface that holds data for

memory_storer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"strings"
99
"testing"
1010

11-
"github.com/git-lfs/gitobj/errors"
11+
"github.com/git-lfs/gitobj/v2/errors"
1212
"github.com/stretchr/testify/assert"
1313
)
1414

object.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package gitobj
22

3-
import "io"
3+
import (
4+
"hash"
5+
"io"
6+
)
47

58
// Object is an interface satisfied by any concrete type that represents a loose
69
// Git object.
@@ -29,7 +32,7 @@ type Object interface {
2932
//
3033
// If an(y) error was encountered, it should be returned immediately,
3134
// along with the number of bytes read up to that point.
32-
Decode(from io.Reader, size int64) (n int, err error)
35+
Decode(hash hash.Hash, from io.Reader, size int64) (n int, err error)
3336

3437
// Type returns the ObjectType constant that represents an instance of
3538
// the implementing type.

0 commit comments

Comments
 (0)