Skip to content

Commit

Permalink
worktree: sort the tree object. Fixes src-d#881
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Bartel <github@spottybenny.ca>
  • Loading branch information
spottybenny committed Jul 3, 2018
1 parent e1c2694 commit 7ff71b5
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions worktree_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7ff71b5

Please sign in to comment.