Skip to content

Commit cebec78

Browse files
committed
Merge pull request #11 from alcortesm/ignore-submodules
Ignore submodule dirs (empty directories without associated object)
2 parents da5ab9d + 3ba0360 commit cebec78

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

file_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func (s *SuiteFile) SetUpSuite(c *C) {
2222
packfile string
2323
}{
2424
{"https://github.com/tyba/git-fixture.git", "formats/packfile/fixtures/git-fixture.ofs-delta"},
25+
{"https://github.com/cpcs499/Final_Pres_P", "formats/packfile/fixtures/Final_Pres_P.ofs-delta"},
2526
}
2627
s.repos = make(map[string]*Repository, 0)
2728
for _, fixRepo := range fixtureRepos {
@@ -131,3 +132,31 @@ func (s *SuiteFile) TestLines(c *C) {
131132
"subtest %d: commit=%s, path=%s", i, t.commit, t.path))
132133
}
133134
}
135+
136+
var ignoreEmptyDirEntriesTests = []struct {
137+
repo string // the repo name as in localRepos
138+
commit string // the commit to search for the file
139+
}{
140+
{
141+
"https://github.com/cpcs499/Final_Pres_P",
142+
"70bade703ce556c2c7391a8065c45c943e8b6bc3",
143+
// the Final dir in this commit is empty
144+
},
145+
}
146+
147+
// It is difficult to assert that we are ignoring an (empty) dir as even
148+
// if we don't, no files will be found in it.
149+
//
150+
// At least this test has a high chance of panicking if
151+
// we don't ignore empty dirs.
152+
func (s *SuiteFile) TestIgnoreEmptyDirEntries(c *C) {
153+
for i, t := range ignoreEmptyDirEntriesTests {
154+
commit, err := s.repos[t.repo].Commit(core.NewHash(t.commit))
155+
c.Assert(err, IsNil, Commentf("subtest %d: %v (%s)", i, err, t.commit))
156+
157+
for file := range commit.Tree().Files() {
158+
_ = file.Contents()
159+
// this would probably panic if we are not ignoring empty dirs
160+
}
161+
}
162+
}

tree.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ func (t *Tree) Files() chan *File {
3939

4040
func (t *Tree) walkEntries(base string, ch chan *File) {
4141
for _, entry := range t.Entries {
42-
obj, _ := t.r.Storage.Get(entry.Hash)
42+
obj, ok := t.r.Storage.Get(entry.Hash)
43+
if !ok {
44+
continue // ignore entries without hash (= submodule dirs)
45+
}
46+
4347
if obj.Type() == core.TreeObject {
4448
tree := &Tree{r: t.r}
4549
tree.Decode(obj)

0 commit comments

Comments
 (0)