From 21723a3974bc47e3950708caf5cfdf4a65086efc Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Thu, 17 Nov 2016 21:29:55 -0500 Subject: [PATCH] *: fix comparison of missing keywords Adding another test validated from the FreeBSD workflow. Just because the keywords requested to be validated are not present in the manifest, it is not an error. Also, if the keywords from a new manifest are not present in a prior manifest, then only compare the common keywords. Fixes https://github.com/vbatts/go-mtree/issues/86 Signed-off-by: Vincent Batts --- check_test.go | 2 +- cmd/gomtree/main.go | 10 +++------- compare.go | 22 +++++++++++++--------- test/cli/0005.sh | 19 +++++++++++++++++++ 4 files changed, 36 insertions(+), 17 deletions(-) create mode 100644 test/cli/0005.sh diff --git a/check_test.go b/check_test.go index 89024dd..6627b53 100644 --- a/check_test.go +++ b/check_test.go @@ -69,7 +69,7 @@ func TestCheckKeywords(t *testing.T) { t.Fatal(err) } if len(res) != 1 { - t.Errorf("expected to get 1 delta on changed mtimes, but did not") + t.Fatal("expected to get 1 delta on changed mtimes, but did not") } if res[0].Type() != Modified { t.Errorf("expected to get modified delta on changed mtimes, but did not") diff --git a/cmd/gomtree/main.go b/cmd/gomtree/main.go index 8c940cb..0d3d488 100644 --- a/cmd/gomtree/main.go +++ b/cmd/gomtree/main.go @@ -184,10 +184,6 @@ func app() error { if (keyword == "time" && mtree.InKeywordSlice("tar_time", specKeywords)) || (keyword == "tar_time" && mtree.InKeywordSlice("time", specKeywords)) { continue } - - if !mtree.InKeywordSlice(keyword, specKeywords) { - return fmt.Errorf("cannot verify keywords not in mtree specification: %s\n", keyword) - } } } @@ -263,9 +259,9 @@ func app() error { if isTarSpec(specDh) || *flTar != "" { res = filterMissingKeywords(res) } - if len(res) > 0 { - return fmt.Errorf("unexpected missing keywords: %d", len(res)) - } + //if len(res) > 0 { + //return fmt.Errorf("unexpected missing keywords: %d", len(res)) + //} out := formatFunc(res) if _, err := os.Stdout.Write([]byte(out)); err != nil { diff --git a/compare.go b/compare.go index 7497b46..b2340b3 100644 --- a/compare.go +++ b/compare.go @@ -185,10 +185,16 @@ func compareEntry(oldEntry, newEntry Entry) ([]KeyDelta, error) { } diffs := map[Keyword]*stateT{} + oldKeys := oldEntry.AllKeys() + newKeys := newEntry.AllKeys() // Fill the map with the old keys first. - for _, kv := range oldEntry.AllKeys() { + for _, kv := range oldKeys { key := kv.Keyword() + // only add this diff if the new keys has this keyword + if key != "tar_time" && key != "time" && HasKeyword(newKeys, key) == emptyKV { + continue + } // Cannot take &kv because it's the iterator. copy := new(KeyVal) @@ -202,8 +208,12 @@ func compareEntry(oldEntry, newEntry Entry) ([]KeyDelta, error) { } // Then fill the new keys. - for _, kv := range newEntry.AllKeys() { + for _, kv := range newKeys { key := kv.Keyword() + // only add this diff if the old keys has this keyword + if key != "tar_time" && key != "time" && HasKeyword(oldKeys, key) == emptyKV { + continue + } // Cannot take &kv because it's the iterator. copy := new(KeyVal) @@ -319,12 +329,6 @@ func Compare(oldDh, newDh *DirectoryHierarchy, keys []Keyword) ([]InodeDelta, er New *Entry } - // Make dealing with the keys mapping easier. - keySet := map[Keyword]struct{}{} - for _, key := range keys { - keySet[key] = struct{}{} - } - // To deal with different orderings of the entries, use a path-keyed // map to make sure we don't start comparing unrelated entries. diffs := map[string]*stateT{} @@ -405,7 +409,7 @@ func Compare(oldDh, newDh *DirectoryHierarchy, keys []Keyword) ([]InodeDelta, er if keys != nil { var filterChanged []KeyDelta for _, keyDiff := range changed { - if _, ok := keySet[keyDiff.name]; ok { + if InKeywordSlice(keyDiff.name, keys) { filterChanged = append(filterChanged, keyDiff) } } diff --git a/test/cli/0005.sh b/test/cli/0005.sh new file mode 100644 index 0000000..48e025b --- /dev/null +++ b/test/cli/0005.sh @@ -0,0 +1,19 @@ +#!/bin/bash +set -e + +name=$(basename $0) +root=$1 +gomtree=$(readlink -f ${root}/gomtree) +t=$(mktemp -d /tmp/go-mtree.XXXXXX) + +echo "[${name}] Running in ${t}" + +pushd ${root} +mkdir -p ${t}/extract +git archive --format=tar HEAD^{tree} . | tar -C ${t}/extract/ -x + +${gomtree} -k sha1digest -c -p ${t}/extract/ > ${t}/${name}.mtree +${gomtree} -f ${t}/${name}.mtree -k md5digest -p ${t}/extract/ + +popd +rm -rf ${t}