From 29603ae478e48f994b81d9abd5a398a494e67b26 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Tue, 14 Aug 2018 00:02:21 -0400 Subject: [PATCH] WIP: sort entries by full path name before compare --- compare.go | 3 +++ entry.go | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/compare.go b/compare.go index 5549e24..daa5676 100644 --- a/compare.go +++ b/compare.go @@ -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() diff --git a/entry.go b/entry.go index fc8c1c9..d983afc 100644 --- a/entry.go +++ b/entry.go @@ -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