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

Commit

Permalink
file, added field Mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mcuadros committed Apr 22, 2016
1 parent b35fc29 commit b08327b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
10 changes: 6 additions & 4 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package git

import (
"bytes"
"os"
"strings"
)

// File represents git file objects.
type File struct {
Name string
Mode os.FileMode
Blob
}

func newFile(name string, b *Blob) *File {
return &File{Name: name, Blob: *b}
func newFile(name string, m os.FileMode, b *Blob) *File {
return &File{Name: name, Mode: m, Blob: *b}
}

// Contents returns the contents of a file as a string.
Expand Down Expand Up @@ -57,13 +59,13 @@ func NewFileIter(r *Repository, t *Tree) *FileIter {

func (iter *FileIter) Next() (*File, error) {
for {
name, _, obj, err := iter.w.Next()
name, entry, obj, err := iter.w.Next()
if err != nil {
return nil, err
}

if blob, ok := obj.(*Blob); ok {
return newFile(name, blob), nil
return newFile(name, entry.Mode, blob), nil
}
}
}
Expand Down
1 change: 1 addition & 0 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func (s *SuiteFile) TestIter(c *C) {
expected := t.files[k]
file, err := iter.Next()
c.Assert(err, IsNil, Commentf("subtest %d, iter %d, err=%v", i, k, err))
c.Assert(file.Mode.String(), Equals, "-rw-r--r--")
c.Assert(file.Hash.IsZero(), Equals, false)
c.Assert(file.Hash, Equals, file.ID())
c.Assert(file.Name, Equals, expected.Name, Commentf("subtest %d, iter %d, name=%s, expected=%s", i, k, file.Name, expected.Hash))
Expand Down
15 changes: 5 additions & 10 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ type TreeEntry struct {
// File returns the hash of the file identified by the `path` argument.
// The path is interpreted as relative to the tree receiver.
func (t *Tree) File(path string) (*File, error) {
hash, err := t.findHash(path)
e, err := t.findEntry(path)
if err != nil {
return nil, ErrFileNotFound
}

obj, err := t.r.Storage.Get(*hash)
obj, err := t.r.Storage.Get(e.Hash)
if err != nil {
if err == core.ObjectNotFoundErr {
return nil, ErrFileNotFound // a git submodule
Expand All @@ -61,10 +61,10 @@ func (t *Tree) File(path string) (*File, error) {
blob := &Blob{}
blob.Decode(obj)

return newFile(path, blob), nil
return newFile(path, e.Mode, blob), nil
}

func (t *Tree) findHash(path string) (*core.Hash, error) {
func (t *Tree) findEntry(path string) (*TreeEntry, error) {
pathParts := strings.Split(path, "/")

var tree *Tree
Expand All @@ -75,12 +75,7 @@ func (t *Tree) findHash(path string) (*core.Hash, error) {
}
}

entry, err := tree.entry(pathParts[0])
if err != nil {
return nil, err
}

return &entry.Hash, nil
return tree.entry(pathParts[0])
}

var errDirNotFound = errors.New("directory not found")
Expand Down
1 change: 1 addition & 0 deletions tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ func (s *SuiteTree) TestFiles(c *C) {
iter := tree.Files()
defer iter.Close()
for file, err := iter.Next(); err == nil; file, err = iter.Next() {
c.Assert(file.Mode.String(), Equals, "-rw-r--r--")
output = append(output, file.Name)
}
sort.Strings(output)
Expand Down

0 comments on commit b08327b

Please sign in to comment.