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

Commit 7ff71b5

Browse files
committed
worktree: sort the tree object. Fixes #881
Signed-off-by: Mark Bartel <[email protected]>
1 parent e1c2694 commit 7ff71b5

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

worktree_commit.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"gopkg.in/src-d/go-git.v4/storage"
1212

1313
"gopkg.in/src-d/go-billy.v4"
14+
"sort"
1415
)
1516

1617
// 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) {
162163
h.trees[parent].Entries = append(h.trees[parent].Entries, te)
163164
}
164165

166+
type sortableEntries []object.TreeEntry
167+
168+
func (sortableEntries) sortName(te object.TreeEntry) string {
169+
if te.Mode == filemode.Dir {
170+
return te.Name + "/"
171+
}
172+
return te.Name
173+
}
174+
func (se sortableEntries) Len() int { return len(se) }
175+
func (se sortableEntries) Less(i int, j int) bool { return se.sortName(se[i]) < se.sortName(se[j]) }
176+
func (se sortableEntries) Swap(i int, j int) { se[i], se[j] = se[j], se[i] }
177+
165178
func (h *buildTreeHelper) copyTreeToStorageRecursive(parent string, t *object.Tree) (plumbing.Hash, error) {
179+
sort.Sort(sortableEntries(t.Entries))
166180
for i, e := range t.Entries {
167181
if e.Mode != filemode.Dir && !e.Hash.IsZero() {
168182
continue

0 commit comments

Comments
 (0)