From e1c269422ab209443b80f05095627c2795e32fd5 Mon Sep 17 00:00:00 2001 From: Mark Bartel Date: Mon, 2 Jul 2018 14:34:22 -0400 Subject: worktree: add test for correct tree sorting (issue #881) Signed-off-by: Mark Bartel --- worktree_commit_test.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/worktree_commit_test.go b/worktree_commit_test.go index 5575bca..2100626 100644 --- a/worktree_commit_test.go +++ b/worktree_commit_test.go @@ -8,9 +8,15 @@ import ( "gopkg.in/src-d/go-git.v4/plumbing/storer" "gopkg.in/src-d/go-git.v4/storage/memory" + "bytes" . "gopkg.in/check.v1" "gopkg.in/src-d/go-billy.v4/memfs" + "gopkg.in/src-d/go-billy.v4/osfs" "gopkg.in/src-d/go-billy.v4/util" + "gopkg.in/src-d/go-git.v4/storage/filesystem" + "io/ioutil" + "os" + "os/exec" ) func (s *WorktreeSuite) TestCommitInvalidOptions(c *C) { @@ -135,6 +141,54 @@ func (s *WorktreeSuite) TestRemoveAndCommitAll(c *C) { assertStorageStatus(c, s.Repository, 13, 11, 11, expected) } +func (s *WorktreeSuite) TestCommitTreeSort(c *C) { + path, err := ioutil.TempDir(os.TempDir(), "test-commit-tree-sort") + c.Assert(err, IsNil) + fs := osfs.New(path) + st, err := filesystem.NewStorage(fs) + c.Assert(err, IsNil) + r, err := Init(st, nil) + c.Assert(err, IsNil) + + r, err = Clone(memory.NewStorage(), memfs.New(), &CloneOptions{ + URL: path, + }) + + w, err := r.Worktree() + c.Assert(err, IsNil) + + mfs := w.Filesystem + + err = mfs.MkdirAll("delta", 0755) + c.Assert(err, IsNil) + + for _, p := range []string{"delta_last", "Gamma", "delta/middle", "Beta", "delta-first", "alpha"} { + util.WriteFile(mfs, p, []byte("foo"), 0644) + _, err = w.Add(p) + c.Assert(err, IsNil) + } + + _, err = w.Commit("foo\n", &CommitOptions{ + All: true, + Author: defaultSignature(), + }) + c.Assert(err, IsNil) + + err = r.Push(&PushOptions{}) + c.Assert(err, IsNil) + + cmd := exec.Command("git", "fsck") + cmd.Dir = path + cmd.Env = os.Environ() + buf := &bytes.Buffer{} + cmd.Stderr = buf + cmd.Stdout = buf + + err = cmd.Run() + + c.Assert(err, IsNil, Commentf("%s", buf.Bytes())) +} + func assertStorageStatus( c *C, r *Repository, treesCount, blobCount, commitCount int, head plumbing.Hash, -- cgit From 7ff71b5a66eea98983c610d33523041a5a829e2f Mon Sep 17 00:00:00 2001 From: Mark Bartel Date: Tue, 3 Jul 2018 17:20:57 -0400 Subject: worktree: sort the tree object. Fixes #881 Signed-off-by: Mark Bartel --- worktree_commit.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/worktree_commit.go b/worktree_commit.go index 5fa63ab..ee5cd82 100644 --- a/worktree_commit.go +++ b/worktree_commit.go @@ -11,6 +11,7 @@ import ( "gopkg.in/src-d/go-git.v4/storage" "gopkg.in/src-d/go-billy.v4" + "sort" ) // Commit stores the current contents of the index in a new commit along with @@ -162,7 +163,20 @@ func (h *buildTreeHelper) doBuildTree(e *index.Entry, parent, fullpath string) { h.trees[parent].Entries = append(h.trees[parent].Entries, te) } +type sortableEntries []object.TreeEntry + +func (sortableEntries) sortName(te object.TreeEntry) string { + if te.Mode == filemode.Dir { + return te.Name + "/" + } + return te.Name +} +func (se sortableEntries) Len() int { return len(se) } +func (se sortableEntries) Less(i int, j int) bool { return se.sortName(se[i]) < se.sortName(se[j]) } +func (se sortableEntries) Swap(i int, j int) { se[i], se[j] = se[j], se[i] } + func (h *buildTreeHelper) copyTreeToStorageRecursive(parent string, t *object.Tree) (plumbing.Hash, error) { + sort.Sort(sortableEntries(t.Entries)) for i, e := range t.Entries { if e.Mode != filemode.Dir && !e.Hash.IsZero() { continue -- cgit From f0c4318e7b8d5cbf91723c71a4ba20d1bd0cfaf5 Mon Sep 17 00:00:00 2001 From: Mark Bartel Date: Sat, 7 Jul 2018 23:20:05 -0400 Subject: worktree: address PR comments: sort imports appropriately Signed-off-by: Mark Bartel --- worktree_commit.go | 2 +- worktree_commit_test.go | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/worktree_commit.go b/worktree_commit.go index ee5cd82..f0e0b42 100644 --- a/worktree_commit.go +++ b/worktree_commit.go @@ -2,6 +2,7 @@ package git import ( "path" + "sort" "strings" "gopkg.in/src-d/go-git.v4/plumbing" @@ -11,7 +12,6 @@ import ( "gopkg.in/src-d/go-git.v4/storage" "gopkg.in/src-d/go-billy.v4" - "sort" ) // Commit stores the current contents of the index in a new commit along with diff --git a/worktree_commit_test.go b/worktree_commit_test.go index 2100626..5ca9b51 100644 --- a/worktree_commit_test.go +++ b/worktree_commit_test.go @@ -1,22 +1,22 @@ package git import ( + "bytes" + "io/ioutil" + "os" + "os/exec" "time" "gopkg.in/src-d/go-git.v4/plumbing" "gopkg.in/src-d/go-git.v4/plumbing/object" "gopkg.in/src-d/go-git.v4/plumbing/storer" "gopkg.in/src-d/go-git.v4/storage/memory" + "gopkg.in/src-d/go-git.v4/storage/filesystem" - "bytes" . "gopkg.in/check.v1" "gopkg.in/src-d/go-billy.v4/memfs" "gopkg.in/src-d/go-billy.v4/osfs" "gopkg.in/src-d/go-billy.v4/util" - "gopkg.in/src-d/go-git.v4/storage/filesystem" - "io/ioutil" - "os" - "os/exec" ) func (s *WorktreeSuite) TestCommitInvalidOptions(c *C) { -- cgit