aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--clients/common/common.go12
-rw-r--r--clients/common/common_test.go19
-rw-r--r--clients/http/git_upload_pack_test.go5
-rw-r--r--formats/packfile/reader.go28
-rw-r--r--formats/packfile/reader_test.go14
-rw-r--r--internal/hash.go (renamed from common/hash.go)2
-rw-r--r--internal/hash_test.go (renamed from common/hash_test.go)2
-rw-r--r--internal/object.go (renamed from common/object.go)22
-rw-r--r--objects.go167
-rw-r--r--objects_test.go58
-rw-r--r--remote.go19
-rw-r--r--remote_test.go4
12 files changed, 202 insertions, 150 deletions
diff --git a/clients/common/common.go b/clients/common/common.go
index 5da0570..4204cb7 100644
--- a/clients/common/common.go
+++ b/clients/common/common.go
@@ -6,8 +6,10 @@ import (
"net/url"
"strings"
- "gopkg.in/sourcegraph/go-vcsurl.v1"
"gopkg.in/src-d/go-git.v2/formats/pktline"
+ "gopkg.in/src-d/go-git.v2/internal"
+
+ "gopkg.in/sourcegraph/go-vcsurl.v1"
)
const GitUploadPackServiceName = "git-upload-pack"
@@ -74,7 +76,7 @@ func (r Capabilities) SymbolicReference(sym string) string {
}
type RemoteHead struct {
- Id string
+ Id internal.Hash
Name string
}
@@ -134,12 +136,12 @@ func (r *GitUploadPackInfo) getRemoteHead(line string) *RemoteHead {
return nil
}
- return &RemoteHead{parts[0], parts[1]}
+ return &RemoteHead{internal.NewHash(parts[0]), parts[1]}
}
type GitUploadPackRequest struct {
- Want []string
- Have []string
+ Want []internal.Hash
+ Have []internal.Hash
}
func (r *GitUploadPackRequest) String() string {
diff --git a/clients/common/common_test.go b/clients/common/common_test.go
index 74492f0..43faf5b 100644
--- a/clients/common/common_test.go
+++ b/clients/common/common_test.go
@@ -7,6 +7,7 @@ import (
. "gopkg.in/check.v1"
"gopkg.in/src-d/go-git.v2/formats/pktline"
+ "gopkg.in/src-d/go-git.v2/internal"
)
func Test(t *testing.T) { TestingT(t) }
@@ -48,15 +49,25 @@ func (s *SuiteCommon) TestGitUploadPackInfo(c *C) {
ref := info.Capabilities.SymbolicReference("HEAD")
c.Assert(ref, Equals, "refs/heads/master")
- c.Assert(info.Refs[ref].Id, Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
+ c.Assert(info.Refs[ref].Id.String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
c.Assert(info.Refs[ref].Name, Equals, "refs/heads/master")
}
func (s *SuiteCommon) TestGitUploadPackRequest(c *C) {
r := &GitUploadPackRequest{
- Want: []string{"foo", "qux"},
- Have: []string{"bar"},
+ Want: []internal.Hash{
+ internal.NewHash("d82f291cde9987322c8a0c81a325e1ba6159684c"),
+ internal.NewHash("2b41ef280fdb67a9b250678686a0c3e03b0a9989"),
+ },
+ Have: []internal.Hash{
+ internal.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"),
+ },
}
- c.Assert(r.String(), Equals, "000dwant foo\n000dwant qux\n000dhave bar\n00000009done\n")
+ c.Assert(r.String(), Equals,
+ "0032want d82f291cde9987322c8a0c81a325e1ba6159684c\n"+
+ "0032want 2b41ef280fdb67a9b250678686a0c3e03b0a9989\n"+
+ "0032have 6ecf0ef2c2dffb796033e5a02219af86ec6584e5\n0000"+
+ "0009done\n",
+ )
}
diff --git a/clients/http/git_upload_pack_test.go b/clients/http/git_upload_pack_test.go
index 603cd1b..7234766 100644
--- a/clients/http/git_upload_pack_test.go
+++ b/clients/http/git_upload_pack_test.go
@@ -5,6 +5,7 @@ import (
. "gopkg.in/check.v1"
"gopkg.in/src-d/go-git.v2/clients/common"
+ "gopkg.in/src-d/go-git.v2/internal"
)
type SuiteRemote struct{}
@@ -41,7 +42,9 @@ func (s *SuiteRemote) TestFetch(c *C) {
c.Assert(r.Connect(RepositoryFixture), IsNil)
reader, err := r.Fetch(&common.GitUploadPackRequest{
- Want: []string{"6ecf0ef2c2dffb796033e5a02219af86ec6584e5"},
+ Want: []internal.Hash{
+ internal.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"),
+ },
})
c.Assert(err, IsNil)
diff --git a/formats/packfile/reader.go b/formats/packfile/reader.go
index 6ccf384..420b9cd 100644
--- a/formats/packfile/reader.go
+++ b/formats/packfile/reader.go
@@ -7,7 +7,7 @@ import (
"io"
"io/ioutil"
- "gopkg.in/src-d/go-git.v2/common"
+ "gopkg.in/src-d/go-git.v2/internal"
"github.com/klauspost/compress/zlib"
)
@@ -50,8 +50,8 @@ type Reader struct {
Format Format
r *trackingReader
- s common.ObjectStorage
- offsets map[int64]common.Hash
+ s internal.ObjectStorage
+ offsets map[int64]internal.Hash
}
// NewReader returns a new Reader that reads from a io.Reader
@@ -60,12 +60,12 @@ func NewReader(r io.Reader) *Reader {
MaxObjectsLimit: DefaultMaxObjectsLimit,
r: &trackingReader{r: r},
- offsets: make(map[int64]common.Hash, 0),
+ offsets: make(map[int64]internal.Hash, 0),
}
}
// Read reads the objects and stores it at the ObjectStorage
-func (r *Reader) Read(s common.ObjectStorage) (int64, error) {
+func (r *Reader) Read(s internal.ObjectStorage) (int64, error) {
r.s = s
if err := r.validateHeader(); err != nil {
if err == io.EOF {
@@ -144,7 +144,7 @@ func (r *Reader) readObjects(count uint32) error {
return nil
}
-func (r *Reader) newRAWObject() (common.Object, error) {
+func (r *Reader) newRAWObject() (internal.Object, error) {
raw := r.s.New()
var steps int64
@@ -153,7 +153,7 @@ func (r *Reader) newRAWObject() (common.Object, error) {
return nil, err
}
- typ := common.ObjectType((buf[0] >> 4) & 7)
+ typ := internal.ObjectType((buf[0] >> 4) & 7)
size := int64(buf[0] & 15)
steps++ // byte we just read to get `o.typ` and `o.size`
@@ -173,11 +173,11 @@ func (r *Reader) newRAWObject() (common.Object, error) {
var err error
switch raw.Type() {
- case common.REFDeltaObject:
+ case internal.REFDeltaObject:
err = r.readREFDelta(raw)
- case common.OFSDeltaObject:
+ case internal.OFSDeltaObject:
err = r.readOFSDelta(raw, steps)
- case common.CommitObject, common.TreeObject, common.BlobObject, common.TagObject:
+ case internal.CommitObject, internal.TreeObject, internal.BlobObject, internal.TagObject:
err = r.readObject(raw)
default:
err = InvalidObjectErr.n("tag %q", raw.Type)
@@ -186,8 +186,8 @@ func (r *Reader) newRAWObject() (common.Object, error) {
return raw, err
}
-func (r *Reader) readREFDelta(raw common.Object) error {
- var ref common.Hash
+func (r *Reader) readREFDelta(raw internal.Object) error {
+ var ref internal.Hash
if _, err := r.r.Read(ref[:]); err != nil {
return err
}
@@ -215,7 +215,7 @@ func (r *Reader) readREFDelta(raw common.Object) error {
return nil
}
-func (r *Reader) readOFSDelta(raw common.Object, steps int64) error {
+func (r *Reader) readOFSDelta(raw internal.Object, steps int64) error {
start := r.r.position
offset, err := decodeOffset(r.r, steps)
if err != nil {
@@ -246,7 +246,7 @@ func (r *Reader) readOFSDelta(raw common.Object, steps int64) error {
return nil
}
-func (r *Reader) readObject(raw common.Object) error {
+func (r *Reader) readObject(raw internal.Object) error {
return r.inflate(raw.Writer())
}
diff --git a/formats/packfile/reader_test.go b/formats/packfile/reader_test.go
index 14c092e..f5cc3e7 100644
--- a/formats/packfile/reader_test.go
+++ b/formats/packfile/reader_test.go
@@ -9,7 +9,7 @@ import (
"testing"
"time"
- "gopkg.in/src-d/go-git.v2/common"
+ "gopkg.in/src-d/go-git.v2/internal"
"github.com/dustin/go-humanize"
. "gopkg.in/check.v1"
@@ -29,7 +29,7 @@ func (s *ReaderSuite) TestReadPackfile(c *C) {
r := NewReader(d)
- storage := common.NewRAWObjectStorage()
+ storage := internal.NewRAWObjectStorage()
_, err := r.Read(storage)
c.Assert(err, IsNil)
@@ -63,7 +63,7 @@ func (s *ReaderSuite) testReadPackfileGitFixture(c *C, file string, f Format) {
r := NewReader(d)
r.Format = f
- storage := common.NewRAWObjectStorage()
+ storage := internal.NewRAWObjectStorage()
_, err = r.Read(storage)
c.Assert(err, IsNil)
@@ -99,10 +99,10 @@ func (s *ReaderSuite) testReadPackfileGitFixture(c *C, file string, f Format) {
})
}
-func AssertObjects(c *C, s *common.RAWObjectStorage, expects []string) {
+func AssertObjects(c *C, s *internal.RAWObjectStorage, expects []string) {
c.Assert(len(expects), Equals, len(s.Objects))
for _, expected := range expects {
- obtained, ok := s.Get(common.NewHash(expected))
+ obtained, ok := s.Get(internal.NewHash(expected))
c.Assert(ok, Equals, true)
c.Assert(obtained.Hash().String(), Equals, expected)
}
@@ -174,14 +174,14 @@ func (s *ReaderSuite) _TestMemoryREF(c *C) {
fmt.Println("time", time.Since(start))
}
-func readFromFile(c *C, file string, f Format) *common.RAWObjectStorage {
+func readFromFile(c *C, file string, f Format) *internal.RAWObjectStorage {
d, err := os.Open(file)
c.Assert(err, IsNil)
r := NewReader(d)
r.Format = f
- storage := common.NewRAWObjectStorage()
+ storage := internal.NewRAWObjectStorage()
_, err = r.Read(storage)
c.Assert(err, IsNil)
diff --git a/common/hash.go b/internal/hash.go
index 83844c7..db55b24 100644
--- a/common/hash.go
+++ b/internal/hash.go
@@ -1,4 +1,4 @@
-package common
+package internal
import (
"crypto/sha1"
diff --git a/common/hash_test.go b/internal/hash_test.go
index cee0c0f..222f3b4 100644
--- a/common/hash_test.go
+++ b/internal/hash_test.go
@@ -1,4 +1,4 @@
-package common
+package internal
import (
"testing"
diff --git a/common/object.go b/internal/object.go
index 60c44da..0f11140 100644
--- a/common/object.go
+++ b/internal/object.go
@@ -1,4 +1,4 @@
-package common
+package internal
import (
"bytes"
@@ -72,10 +72,18 @@ func (o *RAWObject) Write(p []byte) (n int, err error) {
type RAWObjectStorage struct {
Objects map[Hash]*RAWObject
+ Commits map[Hash]*RAWObject
+ Trees map[Hash]*RAWObject
+ Blobs map[Hash]*RAWObject
}
func NewRAWObjectStorage() *RAWObjectStorage {
- return &RAWObjectStorage{make(map[Hash]*RAWObject, 0)}
+ return &RAWObjectStorage{
+ Objects: make(map[Hash]*RAWObject, 0),
+ Commits: make(map[Hash]*RAWObject, 0),
+ Trees: make(map[Hash]*RAWObject, 0),
+ Blobs: make(map[Hash]*RAWObject, 0),
+ }
}
func (o *RAWObjectStorage) New() Object {
@@ -86,10 +94,20 @@ func (o *RAWObjectStorage) Set(obj Object) Hash {
h := obj.Hash()
o.Objects[h] = obj.(*RAWObject)
+ switch obj.Type() {
+ case CommitObject:
+ o.Commits[h] = o.Objects[h]
+ case TreeObject:
+ o.Trees[h] = o.Objects[h]
+ case BlobObject:
+ o.Blobs[h] = o.Objects[h]
+ }
+
return h
}
func (o *RAWObjectStorage) Get(h Hash) (Object, bool) {
obj, ok := o.Objects[h]
+
return obj, ok
}
diff --git a/objects.go b/objects.go
index 77bdc2a..a82a73b 100644
--- a/objects.go
+++ b/objects.go
@@ -1,155 +1,148 @@
package git
import (
+ "bufio"
"bytes"
- "encoding/hex"
"fmt"
+ "io"
+ "os"
"strconv"
"time"
- "gopkg.in/src-d/go-git.v2/common"
+ "gopkg.in/src-d/go-git.v2/internal"
)
-// Object generic object interface
-type Object interface {
- Type() common.ObjectType
- Hash() common.Hash
-}
-
// Commit points to a single tree, marking it as what the project looked like
// at a certain point in time. It contains meta-information about that point
// in time, such as a timestamp, the author of the changes since the last
// commit, a pointer to the previous commit(s), etc.
// http://schacon.github.io/gitbook/1_the_git_object_model.html
type Commit struct {
- Tree common.Hash
- Parents []common.Hash
+ Hash internal.Hash
+ Tree internal.Hash
+ Parents []internal.Hash
Author Signature
Committer Signature
Message string
- hash common.Hash
}
-// ParseCommit transform a byte slice into a Commit struct
-func ParseCommit(b []byte) (*Commit, error) {
- o := &Commit{hash: common.ComputeHash(common.CommitObject, b)}
+// Decode transform an internal.Object into a Blob struct
+func (c *Commit) Decode(o internal.Object) error {
+ c.Hash = o.Hash()
+ r := bufio.NewReader(o.Reader())
- lines := bytes.Split(b, []byte{'\n'})
- for i := range lines {
- if len(lines[i]) > 0 {
- var err error
+ var message bool
+ for {
+ line, err := r.ReadSlice('\n')
+ if err != nil && err != io.EOF {
+ return err
+ }
- split := bytes.SplitN(lines[i], []byte{' '}, 2)
+ line = bytes.TrimSpace(line)
+ if !message {
+ if len(line) == 0 {
+ message = true
+ continue
+ }
+
+ split := bytes.SplitN(line, []byte{' '}, 2)
switch string(split[0]) {
case "tree":
- _, err = hex.Decode(o.Tree[:], split[1])
+ c.Tree = internal.NewHash(string(split[1]))
case "parent":
- var h common.Hash
- _, err = hex.Decode(h[:], split[1])
- if err == nil {
- o.Parents = append(o.Parents, h)
- }
+ c.Parents = append(c.Parents, internal.NewHash(string(split[1])))
case "author":
- o.Author = ParseSignature(split[1])
+ c.Author = ParseSignature(split[1])
case "committer":
- o.Committer = ParseSignature(split[1])
- }
-
- if err != nil {
- return nil, err
+ c.Committer = ParseSignature(split[1])
}
} else {
- o.Message = string(bytes.Join(append(lines[i+1:]), []byte{'\n'}))
- break
+ c.Message += string(line) + "\n"
}
+ if err == io.EOF {
+ return nil
+ }
}
-
- return o, nil
-}
-
-// Type returns the object type
-func (o *Commit) Type() common.ObjectType {
- return common.CommitObject
-}
-
-// Hash returns the computed hash of the commit
-func (o *Commit) Hash() common.Hash {
- return o.hash
}
// Tree is basically like a directory - it references a bunch of other trees
// and/or blobs (i.e. files and sub-directories)
type Tree struct {
Entries []TreeEntry
- hash common.Hash
+ Hash internal.Hash
}
// TreeEntry represents a file
type TreeEntry struct {
Name string
- Hash common.Hash
+ Mode os.FileMode
+ Hash internal.Hash
}
-// ParseTree transform a byte slice into a Tree struct
-func ParseTree(b []byte) (*Tree, error) {
- o := &Tree{hash: common.ComputeHash(common.TreeObject, b)}
-
- if len(b) == 0 {
- return o, nil
+// Decode transform an internal.Object into a Tree struct
+func (t *Tree) Decode(o internal.Object) error {
+ t.Hash = o.Hash()
+ if o.Size() == 0 {
+ return nil
}
+ r := bufio.NewReader(o.Reader())
for {
- split := bytes.SplitN(b, []byte{0}, 2)
- split1 := bytes.SplitN(split[0], []byte{' '}, 2)
+ mode, err := r.ReadString(' ')
+ if err != nil {
+ if err == io.EOF {
+ break
+ }
- entry := TreeEntry{}
- entry.Name = string(split1[1])
- copy(entry.Hash[:], split[1][0:20])
+ return err
+ }
- o.Entries = append(o.Entries, entry)
+ fm, err := strconv.ParseInt(mode[:len(mode)-1], 8, 32)
+ if err != nil && err != io.EOF {
+ return err
+ }
- b = split[1][20:]
- if len(split[1]) == 20 {
- break
+ name, err := r.ReadString(0)
+ if err != nil && err != io.EOF {
+ return err
}
- }
- return o, nil
-}
+ var hash internal.Hash
+ _, err = r.Read(hash[:])
+ if err != nil && err != io.EOF {
+ return err
+ }
-// Type returns the object type
-func (o *Tree) Type() common.ObjectType {
- return common.TreeObject
-}
+ t.Entries = append(t.Entries, TreeEntry{
+ Hash: hash,
+ Mode: os.FileMode(fm),
+ Name: name[:len(name)-1],
+ })
+ }
-// Hash returns the computed hash of the tree
-func (o *Tree) Hash() common.Hash {
- return o.hash
+ return nil
}
// Blob is used to store file data - it is generally a file.
type Blob struct {
- Len int
- hash common.Hash
+ Hash internal.Hash
+ Size int64
+ obj internal.Object
}
-// ParseBlob transform a byte slice into a Blob struct
-func ParseBlob(b []byte) (*Blob, error) {
- return &Blob{
- Len: len(b),
- hash: common.ComputeHash(common.BlobObject, b),
- }, nil
-}
+// Decode transform an internal.Object into a Blob struct
+func (b *Blob) Decode(o internal.Object) error {
+ b.Hash = o.Hash()
+ b.Size = o.Size()
+ b.obj = o
-// Type returns the object type
-func (o *Blob) Type() common.ObjectType {
- return common.BlobObject
+ return nil
}
-// Hash returns the computed hash of the blob
-func (o *Blob) Hash() common.Hash {
- return o.hash
+// Reader returns a reader allow the access to the content of the blob
+func (b *Blob) Reader() io.Reader {
+ return b.obj.Reader()
}
// Signature represents an action signed by a person
diff --git a/objects_test.go b/objects_test.go
index 53da5f0..ec39d44 100644
--- a/objects_test.go
+++ b/objects_test.go
@@ -2,9 +2,11 @@ package git
import (
"encoding/base64"
+ "io/ioutil"
"time"
. "gopkg.in/check.v1"
+ "gopkg.in/src-d/go-git.v2/internal"
)
type ObjectsSuite struct{}
@@ -13,11 +15,17 @@ var _ = Suite(&ObjectsSuite{})
var CommitFixture = "dHJlZSBjMmQzMGZhOGVmMjg4NjE4ZjY1ZjZlZWQ2ZTE2OGUwZDUxNDg4NmY0CnBhcmVudCBiMDI5NTE3ZjYzMDBjMmRhMGY0YjY1MWI4NjQyNTA2Y2Q2YWFmNDVkCnBhcmVudCBiOGU0NzFmNThiY2JjYTYzYjA3YmRhMjBlNDI4MTkwNDA5YzJkYjQ3CmF1dGhvciBNw6F4aW1vIEN1YWRyb3MgPG1jdWFkcm9zQGdtYWlsLmNvbT4gMTQyNzgwMjQzNCArMDIwMApjb21taXR0ZXIgTcOheGltbyBDdWFkcm9zIDxtY3VhZHJvc0BnbWFpbC5jb20+IDE0Mjc4MDI0MzQgKzAyMDAKCk1lcmdlIHB1bGwgcmVxdWVzdCAjMSBmcm9tIGRyaXBvbGxlcy9mZWF0dXJlCgpDcmVhdGluZyBjaGFuZ2Vsb2c="
-func (s *ObjectsSuite) TestParseCommit(c *C) {
+func (s *ObjectsSuite) TestNewCommit(c *C) {
data, _ := base64.StdEncoding.DecodeString(CommitFixture)
- commit, err := ParseCommit(data)
- c.Assert(err, IsNil)
+ o := &internal.RAWObject{}
+ o.SetType(internal.CommitObject)
+ o.Writer().Write(data)
+
+ commit := &Commit{}
+ c.Assert(commit.Decode(o), IsNil)
+
+ c.Assert(commit.Hash.String(), Equals, "a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69")
c.Assert(commit.Tree.String(), Equals, "c2d30fa8ef288618f65f6eed6e168e0d514886f4")
c.Assert(commit.Parents, HasLen, 2)
c.Assert(commit.Parents[0].String(), Equals, "b029517f6300c2da0f4b651b8642506cd6aaf45d")
@@ -26,43 +34,43 @@ func (s *ObjectsSuite) TestParseCommit(c *C) {
c.Assert(commit.Author.Name, Equals, "Máximo Cuadros")
c.Assert(commit.Author.When.Unix(), Equals, int64(1427802434))
c.Assert(commit.Committer.Email, Equals, "mcuadros@gmail.com")
- c.Assert(commit.Message, Equals, "Merge pull request #1 from dripolles/feature\n\nCreating changelog")
-}
-
-func (s *ObjectsSuite) TestCommitHash(c *C) {
- data, _ := base64.StdEncoding.DecodeString(CommitFixture)
- commit, err := ParseCommit(data)
-
- c.Assert(err, IsNil)
- c.Assert(commit.Hash().String(), Equals, "a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69")
+ c.Assert(commit.Message, Equals, "Merge pull request #1 from dripolles/feature\n\nCreating changelog\n")
}
var TreeFixture = "MTAwNjQ0IC5naXRpZ25vcmUAMoWKrTw4PtH/Cg+b3yMdVKAMnogxMDA2NDQgQ0hBTkdFTE9HANP/U+BWSp+H2OhLbijlBg5RcAiqMTAwNjQ0IExJQ0VOU0UAwZK9aiTqGrAdeGhuQXyL3Hw9GX8xMDA2NDQgYmluYXJ5LmpwZwDVwPSrgRiXyt8DrsNYrmDSH5HFDTQwMDAwIGdvAKOXcadlH5f69ccuCCJNhX/DUTPbNDAwMDAganNvbgBah35qkGonQ61uRdmcF5NkKq+O2jQwMDAwIHBocABYavVn0Ltedx5JvdlDT14Pt20l+jQwMDAwIHZlbmRvcgDPSqOziXT7fYHzZ8CDD3141lq4aw=="
func (s *ObjectsSuite) TestParseTree(c *C) {
data, _ := base64.StdEncoding.DecodeString(TreeFixture)
- tree, err := ParseTree(data)
- c.Assert(err, IsNil)
+
+ o := &internal.RAWObject{}
+ o.SetType(internal.TreeObject)
+ o.SetSize(int64(len(data)))
+ o.Writer().Write(data)
+
+ tree := &Tree{}
+ c.Assert(tree.Decode(o), IsNil)
c.Assert(tree.Entries, HasLen, 8)
c.Assert(tree.Entries[0].Name, Equals, ".gitignore")
+ c.Assert(tree.Entries[0].Mode.String(), Equals, "-rw-r--r--")
c.Assert(tree.Entries[0].Hash.String(), Equals, "32858aad3c383ed1ff0a0f9bdf231d54a00c9e88")
}
-func (s *ObjectsSuite) TestTreeHash(c *C) {
- data, _ := base64.StdEncoding.DecodeString(TreeFixture)
- tree, err := ParseTree(data)
+func (s *ObjectsSuite) TestBlobHash(c *C) {
+ o := &internal.RAWObject{}
+ o.SetType(internal.BlobObject)
+ o.SetSize(3)
+ o.Writer().Write([]byte{'F', 'O', 'O'})
- c.Assert(err, IsNil)
- c.Assert(tree.Hash().String(), Equals, "a8d315b2b1c615d43042c3a62402b8a54288cf5c")
-}
+ blob := &Blob{}
+ c.Assert(blob.Decode(o), IsNil)
-func (s *ObjectsSuite) TestBlobHash(c *C) {
- blob, err := ParseBlob([]byte{'F', 'O', 'O'})
- c.Assert(err, IsNil)
+ c.Assert(blob.Size, Equals, int64(3))
+ c.Assert(blob.Hash.String(), Equals, "d96c7efbfec2814ae0301ad054dc8d9fc416c9b5")
- c.Assert(blob.Len, Equals, 3)
- c.Assert(blob.Hash().String(), Equals, "d96c7efbfec2814ae0301ad054dc8d9fc416c9b5")
+ data, err := ioutil.ReadAll(blob.Reader())
+ c.Assert(err, IsNil)
+ c.Assert(string(data), Equals, "FOO")
}
func (s *ObjectsSuite) TestParseSignature(c *C) {
diff --git a/remote.go b/remote.go
index 715d71e..7d21710 100644
--- a/remote.go
+++ b/remote.go
@@ -1,10 +1,12 @@
package git
import (
+ "fmt"
"io"
"gopkg.in/src-d/go-git.v2/clients"
"gopkg.in/src-d/go-git.v2/clients/common"
+ "gopkg.in/src-d/go-git.v2/internal"
)
type Remote struct {
@@ -58,7 +60,22 @@ func (r *Remote) Fetch(req *common.GitUploadPackRequest) (io.ReadCloser, error)
// FetchDefaultBranch returns a reader for the default branch
func (r *Remote) FetchDefaultBranch() (io.ReadCloser, error) {
+ ref, err := r.Ref(r.DefaultBranch())
+ if err != nil {
+ return nil, err
+ }
+
return r.Fetch(&common.GitUploadPackRequest{
- Want: []string{r.upInfo.Refs[r.DefaultBranch()].Id},
+ Want: []internal.Hash{ref},
})
}
+
+// Ref returns the Hash pointing the given refName
+func (r *Remote) Ref(refName string) (internal.Hash, error) {
+ ref, ok := r.upInfo.Refs[refName]
+ if !ok {
+ return internal.NewHash(""), fmt.Errorf("unable to find ref %q", refName)
+ }
+
+ return ref.Id, nil
+}
diff --git a/remote_test.go b/remote_test.go
index 5674407..5cb2e30 100644
--- a/remote_test.go
+++ b/remote_test.go
@@ -1,8 +1,8 @@
package git
import (
- "gopkg.in/src-d/go-git.v2/common"
"gopkg.in/src-d/go-git.v2/formats/packfile"
+ "gopkg.in/src-d/go-git.v2/internal"
. "gopkg.in/check.v1"
)
@@ -43,7 +43,7 @@ func (s *SuiteRemote) TestFetchDefaultBranch(c *C) {
pr := packfile.NewReader(reader)
- storage := common.NewRAWObjectStorage()
+ storage := internal.NewRAWObjectStorage()
_, err = pr.Read(storage)
c.Assert(err, IsNil)
c.Assert(storage.Objects, HasLen, 28)