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

Commit 916755f

Browse files
committed
Improved support for Blob objects
1 parent 5e100bd commit 916755f

File tree

5 files changed

+96
-20
lines changed

5 files changed

+96
-20
lines changed

commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type Commit struct {
2929
}
3030

3131
func (c *Commit) Tree() *Tree {
32-
tree, _ := c.r.Tree(c.tree)
32+
tree, _ := c.r.Tree(c.tree) // FIXME: Return error as well?
3333
return tree
3434
}
3535

common_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func unpackFixtures(c *C, fixtures ...[]packedFixture) map[string]*Repository {
7171
c.Assert(err, IsNil)
7272

7373
r := packfile.NewReader(d)
74-
r.Format = packfile.OFSDeltaFormat // TODO: how to know the format of a pack file ahead of time?
74+
r.Format = packfile.OFSDeltaFormat // This is hardcoded because we don't have a good way to sniff the format
7575

7676
_, err = r.Read(repos[fixture.url].Storage)
7777
c.Assert(err, IsNil)

repository.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ func (r *Repository) Commits() *CommitIter {
110110
return NewCommitIter(r, r.Storage.Iter(core.CommitObject))
111111
}
112112

113-
// Tag returns a tag with the given hash.
114-
func (r *Repository) Tag(h core.Hash) (*Tag, error) {
113+
// Tree return the tree with the given hash
114+
func (r *Repository) Tree(h core.Hash) (*Tree, error) {
115115
obj, err := r.Storage.Get(h)
116116
if err != nil {
117117
if err == core.ObjectNotFoundErr {
@@ -120,18 +120,26 @@ func (r *Repository) Tag(h core.Hash) (*Tag, error) {
120120
return nil, err
121121
}
122122

123-
tag := &Tag{r: r}
124-
return tag, tag.Decode(obj)
123+
tree := &Tree{r: r}
124+
return tree, tree.Decode(obj)
125125
}
126126

127-
// Tags returns a TagIter that can step through all of the annotated tags
128-
// in the repository.
129-
func (r *Repository) Tags() *TagIter {
130-
return NewTagIter(r, r.Storage.Iter(core.TagObject))
127+
// Blob returns the blob with the given hash
128+
func (r *Repository) Blob(h core.Hash) (*Blob, error) {
129+
obj, err := r.Storage.Get(h)
130+
if err != nil {
131+
if err == core.ObjectNotFoundErr {
132+
return nil, ObjectNotFoundErr
133+
}
134+
return nil, err
135+
}
136+
137+
blob := &Blob{}
138+
return blob, blob.Decode(obj)
131139
}
132140

133-
// Tree return the tree with the given hash
134-
func (r *Repository) Tree(h core.Hash) (*Tree, error) {
141+
// Tag returns a tag with the given hash.
142+
func (r *Repository) Tag(h core.Hash) (*Tag, error) {
135143
obj, err := r.Storage.Get(h)
136144
if err != nil {
137145
if err == core.ObjectNotFoundErr {
@@ -140,6 +148,12 @@ func (r *Repository) Tree(h core.Hash) (*Tree, error) {
140148
return nil, err
141149
}
142150

143-
tree := &Tree{r: r}
144-
return tree, tree.Decode(obj)
151+
tag := &Tag{r: r}
152+
return tag, tag.Decode(obj)
153+
}
154+
155+
// Tags returns a TagIter that can step through all of the annotated tags
156+
// in the repository.
157+
func (r *Repository) Tags() *TagIter {
158+
return NewTagIter(r, r.Storage.Iter(core.TagObject))
145159
}

tag.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,41 @@ func (t *Tag) Decode(o core.Object) error {
7878
}
7979

8080
// Commit returns the commit pointed to by the tag. If the tag points to a
81-
// different type of object an error will be returned.
81+
// different type of object ErrUnsupportedObject will be returned.
8282
func (t *Tag) Commit() (*Commit, error) {
83+
if t.Type != core.CommitObject {
84+
return nil, ErrUnsupportedObject
85+
}
8386
return t.r.Commit(t.object)
8487
}
8588

86-
// Tree returns the tree pointed to by the tag. If the tag points to a
87-
// different type of object an error will be returned.
89+
// Tree returns the tree pointed to by the tag. If the tag points to a commit
90+
// object the tree of that commit will be returned. If the tag does not point
91+
// to a commit or tree object ErrUnsupportedObject will be returned.
8892
func (t *Tag) Tree() (*Tree, error) {
8993
// TODO: If the tag is of type commit, follow the commit to its tree?
90-
return t.r.Tree(t.object)
94+
switch t.Type {
95+
case core.CommitObject:
96+
commit, err := t.r.Commit(t.object)
97+
if err != nil {
98+
return nil, err
99+
}
100+
return commit.Tree(), nil
101+
case core.TreeObject:
102+
return t.r.Tree(t.object)
103+
default:
104+
return nil, ErrUnsupportedObject
105+
}
91106
}
92107

93-
// TODO: Add support for retrieving blobs? We don't have a type for that
94-
// currently.
108+
// Blob returns the blob pointed to by the tag. If the tag points to a
109+
// different type of object ErrUnsupportedObject will be returned.
110+
func (t *Tag) Blob() (*Blob, error) {
111+
if t.Type != core.BlobObject {
112+
return nil, ErrUnsupportedObject
113+
}
114+
return t.r.Blob(t.object)
115+
}
95116

96117
// Object returns the object pointed to by the tag.
97118
func (t *Tag) Object() (core.Object, error) {
@@ -132,4 +153,5 @@ func (iter *TagIter) Next() (*Tag, error) {
132153

133154
// Close releases any resources used by the iterator.
134155
func (iter *TagIter) Close() {
156+
iter.Close()
135157
}

tag_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ var tagTests = []struct {
4141
"95ee6e6c750ded1f4dc5499bad730ce3f58c6c3a": {"2c748387f5e9c35d001de3c9ba3072d0b3f10a72", core.CommitObject, "v0.4.0", "cfieber", "[email protected]", "2015-11-15T18:18:51Z", "Release of 0.4.0\n\n- 2c748387f5e9c35d001de3c9ba3072d0b3f10a72: Create LICENSE.txt\n- 0c6968e24796a67fa602c94d7a82fd4fd375ec59: Create AUTHORS\n- 3ce7b902a51bac2f10994f7d1f251b616c975e54: Drop trailing slash.\n"},
4242
"8b6002b614b454d45bafbd244b127839421f92ff": {"65e37611b1ff9cb589e3060507427a9a2645907e", core.CommitObject, "v0.3.0", "cfieber", "[email protected]", "2015-11-15T18:07:03Z", "Release of 0.3.0\n\n- 65e37611b1ff9cb589e3060507427a9a2645907e: need java plugin for manifest generation\n- bc02440df2ff95a014a7b3cb11b98c3a2bded777: newer gradle plugin\n- 791bcd1592828d9d5d16e83f3a825fb08b0ba22d: Don't need to rename the service.\n"},
4343
}},
44+
// TODO: Add fixture with tagged trees
45+
// TODO: Add fixture with tagged blobs
4446
}
4547

4648
type SuiteTag struct {
@@ -72,6 +74,44 @@ func (s *SuiteTag) TestCommit(c *C) {
7274
}
7375
}
7476

77+
func (s *SuiteTag) TestTree(c *C) {
78+
for _, t := range tagTests {
79+
r, ok := s.repos[t.repo]
80+
c.Assert(ok, Equals, true)
81+
k := 0
82+
for hash, expected := range t.tags {
83+
if expected.Type != core.TreeObject {
84+
continue
85+
}
86+
tag, err := r.Tag(core.NewHash(hash))
87+
c.Assert(err, IsNil)
88+
tree, err := tag.Tree()
89+
c.Assert(err, IsNil)
90+
c.Assert(tree.Hash.String(), Equals, expected.Object)
91+
k++
92+
}
93+
}
94+
}
95+
96+
func (s *SuiteTag) TestBlob(c *C) {
97+
for _, t := range tagTests {
98+
r, ok := s.repos[t.repo]
99+
c.Assert(ok, Equals, true)
100+
k := 0
101+
for hash, expected := range t.tags {
102+
if expected.Type != core.BlobObject {
103+
continue
104+
}
105+
tag, err := r.Tag(core.NewHash(hash))
106+
c.Assert(err, IsNil)
107+
blob, err := tag.Blob()
108+
c.Assert(err, IsNil)
109+
c.Assert(blob.Hash.String(), Equals, expected.Object)
110+
k++
111+
}
112+
}
113+
}
114+
75115
func testTagExpected(c *C, tag *Tag, expected expectedTag, comment string) {
76116
when, err := time.Parse(time.RFC3339, expected.When)
77117
c.Assert(err, IsNil)

0 commit comments

Comments
 (0)