WIP: sort entries by full path name before compare

This commit is contained in:
Vincent Batts 2018-08-14 00:02:21 -04:00
parent a072d6d6e7
commit 29603ae478
2 changed files with 30 additions and 0 deletions

View file

@ -3,6 +3,7 @@ package mtree
import (
"encoding/json"
"fmt"
"sort"
"strconv"
)
@ -348,6 +349,7 @@ func Compare(thisDh, thatDh *DirectoryHierarchy, keys []Keyword) ([]InodeDelta,
// First, iterate over the old hierarchy. If nil, pretend it's empty.
if thisDh != nil {
sort.Sort(byName(thisDh.Entries))
for _, e := range thisDh.Entries {
if e.Type == RelativeType || e.Type == FullType {
path, err := e.Path()
@ -370,6 +372,7 @@ func Compare(thisDh, thatDh *DirectoryHierarchy, keys []Keyword) ([]InodeDelta,
// Then, iterate over the new hierarchy. If nil, pretend it's empty.
if thatDh != nil {
sort.Sort(byName(thatDh.Entries))
for _, e := range thatDh.Entries {
if e.Type == RelativeType || e.Type == FullType {
path, err := e.Path()

View file

@ -14,6 +14,33 @@ func (bp byPos) Len() int { return len(bp) }
func (bp byPos) Less(i, j int) bool { return bp[i].Pos < bp[j].Pos }
func (bp byPos) Swap(i, j int) { bp[i], bp[j] = bp[j], bp[i] }
type byName []Entry
func (bp byName) Len() int { return len(bp) }
func (bp byName) Less(i, j int) bool {
var iParent string
var jParent string
var err error
if bp[i].Parent != nil {
iParent, err = bp[i].Parent.Path()
if err != nil {
return false
}
}
iName := filepath.Clean(filepath.Join(iParent, bp[i].Name))
if bp[j].Parent != nil {
jParent, err = bp[j].Parent.Path()
if err != nil {
return false
}
}
jName := filepath.Clean(filepath.Join(jParent, bp[j].Name))
return iName < jName
}
func (bp byName) Swap(i, j int) { bp[i], bp[j] = bp[j], bp[i] }
// Entry is each component of content in the mtree spec file
type Entry struct {
Parent *Entry // up