Skip to content

Commit

Permalink
File, added size and reader is now a method
Browse files Browse the repository at this point in the history
  • Loading branch information
mcuadros committed Feb 22, 2016
1 parent 9c319b2 commit 07ca1ac
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 76 deletions.
16 changes: 10 additions & 6 deletions file.go
Expand Up @@ -2,7 +2,6 @@ package git

import (
"bytes"
"io"
"strings"

"gopkg.in/src-d/go-git.v3/core"
Expand All @@ -11,14 +10,18 @@ import (
// File represents git file objects.
type File struct {
Name string
io.Reader
Hash core.Hash
Blob
}

func newFile(name string, b *Blob) *File {
return &File{Name: name, Blob: *b}
}

// Contents returns the contents of a file as a string.
func (f *File) Contents() string {
buf := new(bytes.Buffer)
buf.ReadFrom(f)
buf.ReadFrom(f.Reader())

return buf.String()
}

Expand All @@ -31,6 +34,7 @@ func (f *File) Lines() []string {
if splits[len(splits)-1] == "" {
return splits[:len(splits)-1]
}

return splits
}

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

func (iter *FileIter) Next() (*File, error) {
for {
name, entry, obj, err := iter.w.Next()
name, _, obj, err := iter.w.Next()
if err != nil {
return nil, err
}
Expand All @@ -57,7 +61,7 @@ func (iter *FileIter) Next() (*File, error) {
blob := &Blob{}
blob.Decode(obj)

return &File{Name: name, Reader: blob.Reader(), Hash: entry.Hash}, nil
return newFile(name, blob), nil
}
}

Expand Down
2 changes: 1 addition & 1 deletion objects_test.go
Expand Up @@ -67,7 +67,7 @@ func (s *ObjectsSuite) TestParseTree(c *C) {
for f, err := iter.Next(); err == nil; f, err = iter.Next() {
count++
if f.Name == "go/example.go" {
content, _ := ioutil.ReadAll(f)
content, _ := ioutil.ReadAll(f.Reader())
c.Assert(content, HasLen, 2780)
}
}
Expand Down
2 changes: 1 addition & 1 deletion tree.go
Expand Up @@ -61,7 +61,7 @@ func (t *Tree) File(path string) (*File, error) {
blob := &Blob{}
blob.Decode(obj)

return &File{Name: path, Reader: blob.Reader(), Hash: *hash}, nil
return newFile(path, blob), nil
}

func (t *Tree) findHash(path string) (*core.Hash, error) {
Expand Down
222 changes: 154 additions & 68 deletions tree_test.go
Expand Up @@ -52,86 +52,172 @@ func (s *SuiteTree) TestFile(c *C) {
commit string // the commit to search for the file
path string // the path of the file to find
blobHash string // expected hash of the returned file
size int64 // expected size of the returned file
found bool // expected found value
}{
// use git ls-tree commit to get the hash of the blobs
{"https://github.com/tyba/git-fixture.git", "b029517f6300c2da0f4b651b8642506cd6aaf45d", "not-found",
"", false},
{"https://github.com/tyba/git-fixture.git", "b029517f6300c2da0f4b651b8642506cd6aaf45d", ".gitignore",
"32858aad3c383ed1ff0a0f9bdf231d54a00c9e88", true},
{"https://github.com/tyba/git-fixture.git", "b029517f6300c2da0f4b651b8642506cd6aaf45d", "LICENSE",
"c192bd6a24ea1ab01d78686e417c8bdc7c3d197f", true},

{"https://github.com/tyba/git-fixture.git", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", "not-found",
"", false},
{"https://github.com/tyba/git-fixture.git", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", ".gitignore",
"32858aad3c383ed1ff0a0f9bdf231d54a00c9e88", true},
{"https://github.com/tyba/git-fixture.git", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", "binary.jpg",
"d5c0f4ab811897cadf03aec358ae60d21f91c50d", true},
{"https://github.com/tyba/git-fixture.git", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", "LICENSE",
"c192bd6a24ea1ab01d78686e417c8bdc7c3d197f", true},

{"https://github.com/tyba/git-fixture.git", "35e85108805c84807bc66a02d91535e1e24b38b9", "binary.jpg",
"d5c0f4ab811897cadf03aec358ae60d21f91c50d", true},
{"https://github.com/tyba/git-fixture.git", "b029517f6300c2da0f4b651b8642506cd6aaf45d", "binary.jpg",
"", false},

{"https://github.com/tyba/git-fixture.git", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", "CHANGELOG",
"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", true},
{"https://github.com/tyba/git-fixture.git", "1669dce138d9b841a518c64b10914d88f5e488ea", "CHANGELOG",
"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", true},
{"https://github.com/tyba/git-fixture.git", "a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69", "CHANGELOG",
"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", true},
{"https://github.com/tyba/git-fixture.git", "35e85108805c84807bc66a02d91535e1e24b38b9", "CHANGELOG",
"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", false},
{"https://github.com/tyba/git-fixture.git", "b8e471f58bcbca63b07bda20e428190409c2db47", "CHANGELOG",
"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", true},
{"https://github.com/tyba/git-fixture.git", "b029517f6300c2da0f4b651b8642506cd6aaf45d", "CHANGELOG",
"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", false},

{
"https://github.com/tyba/git-fixture.git",
"b029517f6300c2da0f4b651b8642506cd6aaf45d", "not-found",
"", 0, false,
},
{
"https://github.com/tyba/git-fixture.git",
"b029517f6300c2da0f4b651b8642506cd6aaf45d", ".gitignore",
"32858aad3c383ed1ff0a0f9bdf231d54a00c9e88", 189, true,
},
{
"https://github.com/tyba/git-fixture.git",
"b029517f6300c2da0f4b651b8642506cd6aaf45d", "LICENSE",
"c192bd6a24ea1ab01d78686e417c8bdc7c3d197f", 1072, true,
},
{
"https://github.com/tyba/git-fixture.git",
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5", "not-found",
"", 0, false,
},
{
"https://github.com/tyba/git-fixture.git",
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5", ".gitignore",
"32858aad3c383ed1ff0a0f9bdf231d54a00c9e88", 189, true,
},
{
"https://github.com/tyba/git-fixture.git",
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5", "binary.jpg",
"d5c0f4ab811897cadf03aec358ae60d21f91c50d", 76110, true,
},
{
"https://github.com/tyba/git-fixture.git",
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5", "LICENSE",
"c192bd6a24ea1ab01d78686e417c8bdc7c3d197f", 1072, true,
},
{
"https://github.com/tyba/git-fixture.git",
"35e85108805c84807bc66a02d91535e1e24b38b9", "binary.jpg",
"d5c0f4ab811897cadf03aec358ae60d21f91c50d", 76110, true,
},
{
"https://github.com/tyba/git-fixture.git",
"b029517f6300c2da0f4b651b8642506cd6aaf45d", "binary.jpg",
"", 0, false,
},
{
"https://github.com/tyba/git-fixture.git",
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5", "CHANGELOG",
"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", 18, true,
},
{
"https://github.com/tyba/git-fixture.git",
"1669dce138d9b841a518c64b10914d88f5e488ea", "CHANGELOG",
"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", 18, true,
},
{
"https://github.com/tyba/git-fixture.git",
"a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69", "CHANGELOG",
"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", 18, true,
},
{
"https://github.com/tyba/git-fixture.git",
"35e85108805c84807bc66a02d91535e1e24b38b9", "CHANGELOG",
"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", 0, false,
},
{
"https://github.com/tyba/git-fixture.git",
"b8e471f58bcbca63b07bda20e428190409c2db47", "CHANGELOG",
"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", 18, true,
},
{
"https://github.com/tyba/git-fixture.git",
"b029517f6300c2da0f4b651b8642506cd6aaf45d", "CHANGELOG",
"d3ff53e0564a9f87d8e84b6e28e5060e517008aa", 0, false,
},
// git submodule
{"https://github.com/cpcs499/Final_Pres_P.git", "70bade703ce556c2c7391a8065c45c943e8b6bc3", "Final",
"", false},
{"https://github.com/cpcs499/Final_Pres_P.git", "70bade703ce556c2c7391a8065c45c943e8b6bc3", "Final/not-found",
"", false},

{"https://github.com/jamesob/desk.git", "d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "LICENSE",
"49c45e6cc893d6f5ebd5c9343fe4492360f339bf", true},
{"https://github.com/jamesob/desk.git", "d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "examples",
"", false},
{"https://github.com/jamesob/desk.git", "d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "examples/desk.sh",
"d9c7751138824cd2d539c23d5afe3f9d29836854", true},
{"https://github.com/jamesob/desk.git", "d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "examples/not-found",
"", false},
{"https://github.com/jamesob/desk.git", "d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "test/bashrc",
"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", true},
{"https://github.com/jamesob/desk.git", "d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "test/not-found",
"", false},

{"https://github.com/spinnaker/spinnaker.git", "b32b2aecae2cfca4840dd480f8082da206a538da", "etc/apache2/sites-available/spinnaker.conf",
"1d452c616be4fb16d2cc6b8a7e7a2208a6e64d2d", true},

{"https://github.com/alcortesm/binary-relations.git", "c44b5176e99085c8fe36fa27b045590a7b9d34c9", "Makefile",
"2dd2ad8c14de6612ed15813679a6554bad99330b", true},
{"https://github.com/alcortesm/binary-relations.git", "c44b5176e99085c8fe36fa27b045590a7b9d34c9", "src/binrels",
"", false},
{"https://github.com/alcortesm/binary-relations.git", "c44b5176e99085c8fe36fa27b045590a7b9d34c9", "src/map-slice",
"", false},
{"https://github.com/alcortesm/binary-relations.git", "c44b5176e99085c8fe36fa27b045590a7b9d34c9", "src/map-slice/map-slice.go",
"12431e98381dd5097e1a19fe53429c72ef1f328e", true},
{"https://github.com/alcortesm/binary-relations.git", "c44b5176e99085c8fe36fa27b045590a7b9d34c9", "src/map-slice/map-slice.go/not-found",
"", false},
{
"https://github.com/cpcs499/Final_Pres_P.git",
"70bade703ce556c2c7391a8065c45c943e8b6bc3", "Final",
"", 0, false,
},
{
"https://github.com/cpcs499/Final_Pres_P.git",
"70bade703ce556c2c7391a8065c45c943e8b6bc3", "Final/not-found",
"", 0, false,
},
{
"https://github.com/jamesob/desk.git",
"d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "LICENSE",
"49c45e6cc893d6f5ebd5c9343fe4492360f339bf", 1058, true,
},
{
"https://github.com/jamesob/desk.git",
"d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "examples",
"", 0, false,
},
{
"https://github.com/jamesob/desk.git",
"d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "examples/desk.sh",
"d9c7751138824cd2d539c23d5afe3f9d29836854", 265, true,
},
{
"https://github.com/jamesob/desk.git",
"d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "examples/not-found",
"", 0, false,
},
{
"https://github.com/jamesob/desk.git",
"d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "test/bashrc",
"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", 0, true,
},
{
"https://github.com/jamesob/desk.git",
"d4edaf0e8101fcea437ebd982d899fe2cc0f9f7b", "test/not-found",
"", 0, false,
},
{
"https://github.com/spinnaker/spinnaker.git",
"b32b2aecae2cfca4840dd480f8082da206a538da", "etc/apache2/sites-available/spinnaker.conf",
"1d452c616be4fb16d2cc6b8a7e7a2208a6e64d2d", 67, true,
},
{
"https://github.com/alcortesm/binary-relations.git",
"c44b5176e99085c8fe36fa27b045590a7b9d34c9", "Makefile",
"2dd2ad8c14de6612ed15813679a6554bad99330b", 1254, true,
},
{
"https://github.com/alcortesm/binary-relations.git",
"c44b5176e99085c8fe36fa27b045590a7b9d34c9", "src/binrels",
"", 0, false,
},
{
"https://github.com/alcortesm/binary-relations.git",
"c44b5176e99085c8fe36fa27b045590a7b9d34c9", "src/map-slice",
"", 0, false,
},
{
"https://github.com/alcortesm/binary-relations.git",
"c44b5176e99085c8fe36fa27b045590a7b9d34c9", "src/map-slice/map-slice.go",
"12431e98381dd5097e1a19fe53429c72ef1f328e", 179, true,
},
{
"https://github.com/alcortesm/binary-relations.git",
"c44b5176e99085c8fe36fa27b045590a7b9d34c9", "src/map-slice/map-slice.go/not-found",
"", 0, false,
},
} {
commit, err := s.repos[t.repo].Commit(core.NewHash(t.commit))
c.Assert(err, IsNil, Commentf("subtest %d: %v (%s)", i, err, t.commit))

tree := commit.Tree()
file, err := tree.File(t.path)
found := err == nil
c.Assert(found, Equals, t.found, Commentf("subtest %d, path=%s, commit=%s", i, t.path, t.commit))
if found {
c.Assert(file.Hash.String(), Equals, t.blobHash, Commentf("subtest %d, commit=%s, path=%s", i, t.commit, t.path))

comment := Commentf("subtest %d, path=%s, commit=%s", i, t.path, t.commit)
c.Assert(found, Equals, t.found, comment)
if !found {
continue
}

c.Assert(file.Size, Equals, t.size, comment)
c.Assert(file.Hash.String(), Equals, t.blobHash, comment)
}
}

Expand Down

0 comments on commit 07ca1ac

Please sign in to comment.