aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing
diff options
context:
space:
mode:
Diffstat (limited to 'plumbing')
-rw-r--r--plumbing/format/index/encoder.go9
-rw-r--r--plumbing/format/index/encoder_test.go10
-rw-r--r--plumbing/format/index/index.go13
-rw-r--r--plumbing/object/file.go2
-rw-r--r--plumbing/object/file_test.go28
-rw-r--r--plumbing/object/tree.go5
-rw-r--r--plumbing/object/tree_test.go41
7 files changed, 98 insertions, 10 deletions
diff --git a/plumbing/format/index/encoder.go b/plumbing/format/index/encoder.go
index e5de135..bdb10c1 100644
--- a/plumbing/format/index/encoder.go
+++ b/plumbing/format/index/encoder.go
@@ -6,6 +6,7 @@ import (
"errors"
"hash"
"io"
+ "sort"
"time"
"srcd.works/go-git.v4/utils/binary"
@@ -61,6 +62,8 @@ func (e *Encoder) encodeHeader(idx *Index) error {
}
func (e *Encoder) encodeEntries(idx *Index) error {
+ sort.Sort(byName(idx.Entries))
+
for _, entry := range idx.Entries {
if err := e.encodeEntry(&entry); err != nil {
return err
@@ -139,3 +142,9 @@ func (e *Encoder) padEntry(wrote int) error {
func (e *Encoder) encodeFooter() error {
return binary.Write(e.w, e.hash.Sum(nil))
}
+
+type byName []Entry
+
+func (l byName) Len() int { return len(l) }
+func (l byName) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
+func (l byName) Less(i, j int) bool { return l[i].Name < l[j].Name }
diff --git a/plumbing/format/index/encoder_test.go b/plumbing/format/index/encoder_test.go
index 914ee26..a6a0ea2 100644
--- a/plumbing/format/index/encoder_test.go
+++ b/plumbing/format/index/encoder_test.go
@@ -26,6 +26,11 @@ func (s *IndexSuite) TestEncode(c *C) {
}, {
CreatedAt: time.Now(),
ModifiedAt: time.Now(),
+ Name: "bar",
+ Size: 82,
+ }, {
+ CreatedAt: time.Now(),
+ ModifiedAt: time.Now(),
Name: strings.Repeat(" ", 20),
Size: 82,
}},
@@ -42,6 +47,11 @@ func (s *IndexSuite) TestEncode(c *C) {
c.Assert(err, IsNil)
c.Assert(idx, DeepEquals, output)
+
+ c.Assert(output.Entries[0].Name, Equals, strings.Repeat(" ", 20))
+ c.Assert(output.Entries[1].Name, Equals, "bar")
+ c.Assert(output.Entries[2].Name, Equals, "foo")
+
}
func (s *IndexSuite) TestEncodeUnsuportedVersion(c *C) {
diff --git a/plumbing/format/index/index.go b/plumbing/format/index/index.go
index e5dc178..a95dba2 100644
--- a/plumbing/format/index/index.go
+++ b/plumbing/format/index/index.go
@@ -36,9 +36,14 @@ const (
// in the worktree, having information about the working files. Changes in
// worktree are detected using this Index. The Index is also used during merges
type Index struct {
- Version uint32
- Entries []Entry
- Cache *Tree
+ // Version is index version
+ Version uint32
+ // Entries collection of entries represented by this Index. The order of
+ // this collection is not guaranteed
+ Entries []Entry
+ // Cache represents the 'Cached tree' extension
+ Cache *Tree
+ // ResolveUndo represents the 'Resolve undo' extension
ResolveUndo *ResolveUndo
}
@@ -84,7 +89,7 @@ type TreeEntry struct {
// Path component (relative to its parent directory)
Path string
// Entries is the number of entries in the index that is covered by the tree
- // this entry represents
+ // this entry represents.
Entries int
// Trees is the number that represents the number of subtrees this tree has
Trees int
diff --git a/plumbing/object/file.go b/plumbing/object/file.go
index 35e7f24..4866777 100644
--- a/plumbing/object/file.go
+++ b/plumbing/object/file.go
@@ -81,7 +81,7 @@ func (iter *FileIter) Next() (*File, error) {
return nil, err
}
- if entry.Mode.IsDir() {
+ if entry.Mode.IsDir() || entry.Mode == SubmoduleMode {
continue
}
diff --git a/plumbing/object/file_test.go b/plumbing/object/file_test.go
index 4c8bbb6..ff01c9f 100644
--- a/plumbing/object/file_test.go
+++ b/plumbing/object/file_test.go
@@ -247,3 +247,31 @@ func (s *FileSuite) TestFileIter(c *C) {
c.Assert(count, Equals, 1)
}
+
+func (s *FileSuite) TestFileIterSubmodule(c *C) {
+ dotgit := fixtures.ByURL("https://github.com/git-fixtures/submodule.git").One().DotGit()
+ st, err := filesystem.NewStorage(dotgit)
+
+ c.Assert(err, IsNil)
+
+ hash := plumbing.NewHash("a692ec699bff9117c1ed91752afbb7d9d272ebef")
+ commit, err := GetCommit(st, hash)
+ c.Assert(err, IsNil)
+
+ tree, err := commit.Tree()
+ c.Assert(err, IsNil)
+
+ expected := []string{
+ ".gitmodules",
+ }
+
+ var count int
+ i := tree.Files()
+ i.ForEach(func(f *File) error {
+ c.Assert(f.Name, Equals, expected[count])
+ count++
+ return nil
+ })
+
+ c.Assert(count, Equals, 1)
+}
diff --git a/plumbing/object/tree.go b/plumbing/object/tree.go
index 3bcd80a..27d8578 100644
--- a/plumbing/object/tree.go
+++ b/plumbing/object/tree.go
@@ -375,11 +375,6 @@ func (w *TreeWalker) Next() (name string, entry TreeEntry, err error) {
return
}
- if entry.Mode == SubmoduleMode {
- err = nil
- continue
- }
-
if entry.Mode.IsDir() {
obj, err = GetTree(w.s, entry.Hash)
}
diff --git a/plumbing/object/tree_test.go b/plumbing/object/tree_test.go
index be721b9..8ea31bb 100644
--- a/plumbing/object/tree_test.go
+++ b/plumbing/object/tree_test.go
@@ -4,7 +4,10 @@ import (
"io"
"os"
+ fixtures "github.com/src-d/go-git-fixtures"
+
"srcd.works/go-git.v4/plumbing"
+ "srcd.works/go-git.v4/storage/filesystem"
. "gopkg.in/check.v1"
"srcd.works/go-git.v4/plumbing/storer"
@@ -262,6 +265,44 @@ func (s *TreeSuite) TestTreeWalkerNextNonRecursive(c *C) {
c.Assert(count, Equals, 8)
}
+func (s *TreeSuite) TestTreeWalkerNextSubmodule(c *C) {
+ dotgit := fixtures.ByURL("https://github.com/git-fixtures/submodule.git").One().DotGit()
+ st, err := filesystem.NewStorage(dotgit)
+ c.Assert(err, IsNil)
+
+ hash := plumbing.NewHash("a692ec699bff9117c1ed91752afbb7d9d272ebef")
+ commit, err := GetCommit(st, hash)
+ c.Assert(err, IsNil)
+
+ tree, err := commit.Tree()
+ c.Assert(err, IsNil)
+
+ expected := []string{
+ ".gitmodules",
+ "basic",
+ "itself",
+ }
+
+ var count int
+ walker := NewTreeWalker(tree, true)
+ defer walker.Close()
+
+ for {
+ name, entry, err := walker.Next()
+ if err == io.EOF {
+ break
+ }
+
+ c.Assert(err, IsNil)
+ c.Assert(entry, NotNil)
+ c.Assert(name, Equals, expected[count])
+
+ count++
+ }
+
+ c.Assert(count, Equals, 3)
+}
+
var treeWalkerExpects = []struct {
Path, Mode, Name, Hash, Tree string
}{{