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

Commit 07ca1ac

Browse files
committed
File, added size and reader is now a method
1 parent 9c319b2 commit 07ca1ac

File tree

4 files changed

+166
-76
lines changed

4 files changed

+166
-76
lines changed

file.go

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

33
import (
44
"bytes"
5-
"io"
65
"strings"
76

87
"gopkg.in/src-d/go-git.v3/core"
@@ -11,14 +10,18 @@ import (
1110
// File represents git file objects.
1211
type File struct {
1312
Name string
14-
io.Reader
15-
Hash core.Hash
13+
Blob
14+
}
15+
16+
func newFile(name string, b *Blob) *File {
17+
return &File{Name: name, Blob: *b}
1618
}
1719

1820
// Contents returns the contents of a file as a string.
1921
func (f *File) Contents() string {
2022
buf := new(bytes.Buffer)
21-
buf.ReadFrom(f)
23+
buf.ReadFrom(f.Reader())
24+
2225
return buf.String()
2326
}
2427

@@ -31,6 +34,7 @@ func (f *File) Lines() []string {
3134
if splits[len(splits)-1] == "" {
3235
return splits[:len(splits)-1]
3336
}
37+
3438
return splits
3539
}
3640

@@ -44,7 +48,7 @@ func NewFileIter(r *Repository, t *Tree) *FileIter {
4448

4549
func (iter *FileIter) Next() (*File, error) {
4650
for {
47-
name, entry, obj, err := iter.w.Next()
51+
name, _, obj, err := iter.w.Next()
4852
if err != nil {
4953
return nil, err
5054
}
@@ -57,7 +61,7 @@ func (iter *FileIter) Next() (*File, error) {
5761
blob := &Blob{}
5862
blob.Decode(obj)
5963

60-
return &File{Name: name, Reader: blob.Reader(), Hash: entry.Hash}, nil
64+
return newFile(name, blob), nil
6165
}
6266
}
6367

objects_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (s *ObjectsSuite) TestParseTree(c *C) {
6767
for f, err := iter.Next(); err == nil; f, err = iter.Next() {
6868
count++
6969
if f.Name == "go/example.go" {
70-
content, _ := ioutil.ReadAll(f)
70+
content, _ := ioutil.ReadAll(f.Reader())
7171
c.Assert(content, HasLen, 2780)
7272
}
7373
}

tree.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (t *Tree) File(path string) (*File, error) {
6161
blob := &Blob{}
6262
blob.Decode(obj)
6363

64-
return &File{Name: path, Reader: blob.Reader(), Hash: *hash}, nil
64+
return newFile(path, blob), nil
6565
}
6666

6767
func (t *Tree) findHash(path string) (*core.Hash, error) {

tree_test.go

Lines changed: 154 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -52,86 +52,172 @@ func (s *SuiteTree) TestFile(c *C) {
5252
commit string // the commit to search for the file
5353
path string // the path of the file to find
5454
blobHash string // expected hash of the returned file
55+
size int64 // expected size of the returned file
5556
found bool // expected found value
5657
}{
5758
// use git ls-tree commit to get the hash of the blobs
58-
{"https://github.com/tyba/git-fixture.git", "b029517f6300c2da0f4b651b8642506cd6aaf45d", "not-found",
59-
"", false},
60-
{"https://github.com/tyba/git-fixture.git", "b029517f6300c2da0f4b651b8642506cd6aaf45d", ".gitignore",
61-
"32858aad3c383ed1ff0a0f9bdf231d54a00c9e88", true},
62-
{"https://github.com/tyba/git-fixture.git", "b029517f6300c2da0f4b651b8642506cd6aaf45d", "LICENSE",
63-
"c192bd6a24ea1ab01d78686e417c8bdc7c3d197f", true},
64-
65-
{"https://github.com/tyba/git-fixture.git", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", "not-found",
66-
"", false},
67-
{"https://github.com/tyba/git-fixture.git", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", ".gitignore",
68-
"32858aad3c383ed1ff0a0f9bdf231d54a00c9e88", true},
69-
{"https://github.com/tyba/git-fixture.git", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", "binary.jpg",
70-
"d5c0f4ab811897cadf03aec358ae60d21f91c50d", true},
71-
{"https://github.com/tyba/git-fixture.git", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", "LICENSE",
72-
"c192bd6a24ea1ab01d78686e417c8bdc7c3d197f", true},
73-
74-
{"https://github.com/tyba/git-fixture.git", "35e85108805c84807bc66a02d91535e1e24b38b9", "binary.jpg",
75-
"d5c0f4ab811897cadf03aec358ae60d21f91c50d", true},
76-
{"https://github.com/tyba/git-fixture.git", "b029517f6300c2da0f4b651b8642506cd6aaf45d", "binary.jpg",
77-
"", false},
78-
79-
{"https://github.com/tyba/git-fixture.git", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", "CHANGELOG",
80-
"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", true},
81-
{"https://github.com/tyba/git-fixture.git", "1669dce138d9b841a518c64b10914d88f5e488ea", "CHANGELOG",
82-
"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", true},
83-
{"https://github.com/tyba/git-fixture.git", "a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69", "CHANGELOG",
84-
"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", true},
85-
{"https://github.com/tyba/git-fixture.git", "35e85108805c84807bc66a02d91535e1e24b38b9", "CHANGELOG",
86-
"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", false},
87-
{"https://github.com/tyba/git-fixture.git", "b8e471f58bcbca63b07bda20e428190409c2db47", "CHANGELOG",
88-
"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", true},
89-
{"https://github.com/tyba/git-fixture.git", "b029517f6300c2da0f4b651b8642506cd6aaf45d", "CHANGELOG",
90-
"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", false},
91-
59+
{
60+
"https://github.com/tyba/git-fixture.git",
61+
"b029517f6300c2da0f4b651b8642506cd6aaf45d", "not-found",
62+
"", 0, false,
63+
},
64+
{
65+
"https://github.com/tyba/git-fixture.git",
66+
"b029517f6300c2da0f4b651b8642506cd6aaf45d", ".gitignore",
67+
"32858aad3c383ed1ff0a0f9bdf231d54a00c9e88", 189, true,
68+
},
69+
{
70+
"https://github.com/tyba/git-fixture.git",
71+
"b029517f6300c2da0f4b651b8642506cd6aaf45d", "LICENSE",
72+
"c192bd6a24ea1ab01d78686e417c8bdc7c3d197f", 1072, true,
73+
},
74+
{
75+
"https://github.com/tyba/git-fixture.git",
76+
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5", "not-found",
77+
"", 0, false,
78+
},
79+
{
80+
"https://github.com/tyba/git-fixture.git",
81+
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5", ".gitignore",
82+
"32858aad3c383ed1ff0a0f9bdf231d54a00c9e88", 189, true,
83+
},
84+
{
85+
"https://github.com/tyba/git-fixture.git",
86+
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5", "binary.jpg",
87+
"d5c0f4ab811897cadf03aec358ae60d21f91c50d", 76110, true,
88+
},
89+
{
90+
"https://github.com/tyba/git-fixture.git",
91+
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5", "LICENSE",
92+
"c192bd6a24ea1ab01d78686e417c8bdc7c3d197f", 1072, true,
93+
},
94+
{
95+
"https://github.com/tyba/git-fixture.git",
96+
"35e85108805c84807bc66a02d91535e1e24b38b9", "binary.jpg",
97+
"d5c0f4ab811897cadf03aec358ae60d21f91c50d", 76110, true,
98+
},
99+
{
100+
"https://github.com/tyba/git-fixture.git",
101+
"b029517f6300c2da0f4b651b8642506cd6aaf45d", "binary.jpg",
102+
"", 0, false,
103+
},
104+
{
105+
"https://github.com/tyba/git-fixture.git",
106+
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5", "CHANGELOG",
107+
"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", 18, true,
108+
},
109+
{
110+
"https://github.com/tyba/git-fixture.git",
111+
"1669dce138d9b841a518c64b10914d88f5e488ea", "CHANGELOG",
112+
"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", 18, true,
113+
},
114+
{
115+
"https://github.com/tyba/git-fixture.git",
116+
"a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69", "CHANGELOG",
117+
"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", 18, true,
118+
},
119+
{
120+
"https://github.com/tyba/git-fixture.git",
121+
"35e85108805c84807bc66a02d91535e1e24b38b9", "CHANGELOG",
122+
"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", 0, false,
123+
},
124+
{
125+
"https://github.com/tyba/git-fixture.git",
126+
"b8e471f58bcbca63b07bda20e428190409c2db47", "CHANGELOG",
127+
"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", 18, true,
128+
},
129+
{
130+
"https://github.com/tyba/git-fixture.git",
131+
"b029517f6300c2da0f4b651b8642506cd6aaf45d", "CHANGELOG",
132+
"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", 0, false,
133+
},
92134
// git submodule
93-
{"https://github.com/cpcs499/Final_Pres_P.git", "70bade703ce556c2c7391a8065c45c943e8b6bc3", "Final",
94-
"", false},
95-
{"https://github.com/cpcs499/Final_Pres_P.git", "70bade703ce556c2c7391a8065c45c943e8b6bc3", "Final/not-found",
96-
"", false},
97-
98-
{"https://github.com/jamesob/desk.git", "d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "LICENSE",
99-
"49c45e6cc893d6f5ebd5c9343fe4492360f339bf", true},
100-
{"https://github.com/jamesob/desk.git", "d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "examples",
101-
"", false},
102-
{"https://github.com/jamesob/desk.git", "d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "examples/desk.sh",
103-
"d9c7751138824cd2d539c23d5afe3f9d29836854", true},
104-
{"https://github.com/jamesob/desk.git", "d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "examples/not-found",
105-
"", false},
106-
{"https://github.com/jamesob/desk.git", "d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "test/bashrc",
107-
"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", true},
108-
{"https://github.com/jamesob/desk.git", "d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "test/not-found",
109-
"", false},
110-
111-
{"https://github.com/spinnaker/spinnaker.git", "b32b2aecae2cfca4840dd480f8082da206a538da", "etc/apache2/sites-available/spinnaker.conf",
112-
"1d452c616be4fb16d2cc6b8a7e7a2208a6e64d2d", true},
113-
114-
{"https://github.com/alcortesm/binary-relations.git", "c44b5176e99085c8fe36fa27b045590a7b9d34c9", "Makefile",
115-
"2dd2ad8c14de6612ed15813679a6554bad99330b", true},
116-
{"https://github.com/alcortesm/binary-relations.git", "c44b5176e99085c8fe36fa27b045590a7b9d34c9", "src/binrels",
117-
"", false},
118-
{"https://github.com/alcortesm/binary-relations.git", "c44b5176e99085c8fe36fa27b045590a7b9d34c9", "src/map-slice",
119-
"", false},
120-
{"https://github.com/alcortesm/binary-relations.git", "c44b5176e99085c8fe36fa27b045590a7b9d34c9", "src/map-slice/map-slice.go",
121-
"12431e98381dd5097e1a19fe53429c72ef1f328e", true},
122-
{"https://github.com/alcortesm/binary-relations.git", "c44b5176e99085c8fe36fa27b045590a7b9d34c9", "src/map-slice/map-slice.go/not-found",
123-
"", false},
135+
{
136+
"https://github.com/cpcs499/Final_Pres_P.git",
137+
"70bade703ce556c2c7391a8065c45c943e8b6bc3", "Final",
138+
"", 0, false,
139+
},
140+
{
141+
"https://github.com/cpcs499/Final_Pres_P.git",
142+
"70bade703ce556c2c7391a8065c45c943e8b6bc3", "Final/not-found",
143+
"", 0, false,
144+
},
145+
{
146+
"https://github.com/jamesob/desk.git",
147+
"d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "LICENSE",
148+
"49c45e6cc893d6f5ebd5c9343fe4492360f339bf", 1058, true,
149+
},
150+
{
151+
"https://github.com/jamesob/desk.git",
152+
"d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "examples",
153+
"", 0, false,
154+
},
155+
{
156+
"https://github.com/jamesob/desk.git",
157+
"d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "examples/desk.sh",
158+
"d9c7751138824cd2d539c23d5afe3f9d29836854", 265, true,
159+
},
160+
{
161+
"https://github.com/jamesob/desk.git",
162+
"d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "examples/not-found",
163+
"", 0, false,
164+
},
165+
{
166+
"https://github.com/jamesob/desk.git",
167+
"d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "test/bashrc",
168+
"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", 0, true,
169+
},
170+
{
171+
"https://github.com/jamesob/desk.git",
172+
"d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "test/not-found",
173+
"", 0, false,
174+
},
175+
{
176+
"https://github.com/spinnaker/spinnaker.git",
177+
"b32b2aecae2cfca4840dd480f8082da206a538da", "etc/apache2/sites-available/spinnaker.conf",
178+
"1d452c616be4fb16d2cc6b8a7e7a2208a6e64d2d", 67, true,
179+
},
180+
{
181+
"https://github.com/alcortesm/binary-relations.git",
182+
"c44b5176e99085c8fe36fa27b045590a7b9d34c9", "Makefile",
183+
"2dd2ad8c14de6612ed15813679a6554bad99330b", 1254, true,
184+
},
185+
{
186+
"https://github.com/alcortesm/binary-relations.git",
187+
"c44b5176e99085c8fe36fa27b045590a7b9d34c9", "src/binrels",
188+
"", 0, false,
189+
},
190+
{
191+
"https://github.com/alcortesm/binary-relations.git",
192+
"c44b5176e99085c8fe36fa27b045590a7b9d34c9", "src/map-slice",
193+
"", 0, false,
194+
},
195+
{
196+
"https://github.com/alcortesm/binary-relations.git",
197+
"c44b5176e99085c8fe36fa27b045590a7b9d34c9", "src/map-slice/map-slice.go",
198+
"12431e98381dd5097e1a19fe53429c72ef1f328e", 179, true,
199+
},
200+
{
201+
"https://github.com/alcortesm/binary-relations.git",
202+
"c44b5176e99085c8fe36fa27b045590a7b9d34c9", "src/map-slice/map-slice.go/not-found",
203+
"", 0, false,
204+
},
124205
} {
125206
commit, err := s.repos[t.repo].Commit(core.NewHash(t.commit))
126207
c.Assert(err, IsNil, Commentf("subtest %d: %v (%s)", i, err, t.commit))
127208

128209
tree := commit.Tree()
129210
file, err := tree.File(t.path)
130211
found := err == nil
131-
c.Assert(found, Equals, t.found, Commentf("subtest %d, path=%s, commit=%s", i, t.path, t.commit))
132-
if found {
133-
c.Assert(file.Hash.String(), Equals, t.blobHash, Commentf("subtest %d, commit=%s, path=%s", i, t.commit, t.path))
212+
213+
comment := Commentf("subtest %d, path=%s, commit=%s", i, t.path, t.commit)
214+
c.Assert(found, Equals, t.found, comment)
215+
if !found {
216+
continue
134217
}
218+
219+
c.Assert(file.Size, Equals, t.size, comment)
220+
c.Assert(file.Hash.String(), Equals, t.blobHash, comment)
135221
}
136222
}
137223

0 commit comments

Comments
 (0)