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