Merge pull request #148 from cyphar/compare-improvements

compare: allow nil newDh and oldDh
This commit is contained in:
Vincent Batts 2018-05-31 12:30:39 -07:00 committed by GitHub
commit 1bcf4de08f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 30 deletions

View file

@ -1,9 +1,9 @@
language: go language: go
go: go:
- 1.x - 1.x
- 1.10.x
- 1.9.x
- 1.8.x - 1.8.x
- 1.7.x
- 1.6.3
sudo: false sudo: false

View file

@ -325,6 +325,10 @@ func compareEntry(oldEntry, newEntry Entry) ([]KeyDelta, error) {
// missing entries and the like). A missing or extra key is treated as a // missing entries and the like). A missing or extra key is treated as a
// Modified type. // Modified type.
// //
// If oldDh or newDh are empty, we assume they are a hierarchy that is
// completely empty. This is purely for helping callers create synthetic
// InodeDeltas.
//
// NB: The order of the parameters matters (old, new) because Extra and // NB: The order of the parameters matters (old, new) because Extra and
// Missing are considered as different discrepancy types. // Missing are considered as different discrepancy types.
func Compare(oldDh, newDh *DirectoryHierarchy, keys []Keyword) ([]InodeDelta, error) { func Compare(oldDh, newDh *DirectoryHierarchy, keys []Keyword) ([]InodeDelta, error) {
@ -338,7 +342,8 @@ func Compare(oldDh, newDh *DirectoryHierarchy, keys []Keyword) ([]InodeDelta, er
// map to make sure we don't start comparing unrelated entries. // map to make sure we don't start comparing unrelated entries.
diffs := map[string]*stateT{} diffs := map[string]*stateT{}
// First, iterate over the old hierarchy. // First, iterate over the old hierarchy. If nil, pretend it's empty.
if oldDh != nil {
for _, e := range oldDh.Entries { for _, e := range oldDh.Entries {
if e.Type == RelativeType || e.Type == FullType { if e.Type == RelativeType || e.Type == FullType {
path, err := e.Path() path, err := e.Path()
@ -357,8 +362,10 @@ func Compare(oldDh, newDh *DirectoryHierarchy, keys []Keyword) ([]InodeDelta, er
diffs[path].Old = copy diffs[path].Old = copy
} }
} }
}
// Then, iterate over the new hierarchy. // Then, iterate over the new hierarchy. If nil, pretend it's empty.
if newDh != nil {
for _, e := range newDh.Entries { for _, e := range newDh.Entries {
if e.Type == RelativeType || e.Type == FullType { if e.Type == RelativeType || e.Type == FullType {
path, err := e.Path() path, err := e.Path()
@ -377,6 +384,7 @@ func Compare(oldDh, newDh *DirectoryHierarchy, keys []Keyword) ([]InodeDelta, er
diffs[path].New = copy diffs[path].New = copy
} }
} }
}
// Now we compute the diff. // Now we compute the diff.
var results []InodeDelta var results []InodeDelta