dag: diff: check CIDs in base case when comparing nodes
Fixes #4591. License: MIT Signed-off-by: Lucas Molas <schomatis@gmail.com>
Lucas Molas committed
Mar 5, 2018 at 12:00 UTC
90acbfaae7b22e0abce84e94f8316a7d60487958
2 files changed
+22
-1
merkledag/utils/diff.go
+5
@@ -95,7 +95,12 @@ func ApplyChange(ctx context.Context, ds ipld.DAGService, nd *dag.ProtoNode, cs
95
96
// Diff returns a set of changes that transform node 'a' into node 'b'
97
func Diff(ctx context.Context, ds ipld.DAGService, a, b ipld.Node) ([]*Change, error) {
98
+ // Base case where both nodes are leaves, just compare
99
+ // their CIDs.
100
if len(a.Links()) == 0 && len(b.Links()) == 0 {
101
+ if a.Cid().Equals(b.Cid()) {
102
+ return []*Change{}, nil
103
+ }
104
return []*Change{
105
&Change{
106
Type: Mod,
test/sharness/t0052-object-diff.sh
+17
-1
@@ -20,7 +20,11 @@ test_expect_success "create some objects for testing diffs" '
20
echo "nested" > foo/baz/dog &&
21
C=$(ipfs add -r -q foo | tail -n1)
22
echo "changed" > foo/bar &&
23
- D=$(ipfs add -r -q foo | tail -n1)
23
+ D=$(ipfs add -r -q foo | tail -n1) &&
24
+ echo "" > single_file &&
25
+ SINGLE_FILE=$(ipfs add -r -q single_file | tail -n1) &&
26
+ mkdir empty_dir
27
+ EMPTY_DIR=$(ipfs add -r -q empty_dir | tail -n1)
28
'
29
30
test_expect_success "diff against self is empty" '
@@ -32,6 +36,18 @@ test_expect_success "identity diff output looks good" '
36
test_cmp diff_exp diff_out
37
'
38
39
+test_expect_success "diff against self (single file) is empty" '
40
+ ipfs object diff $SINGLE_FILE $SINGLE_FILE > diff_out
41
+ printf "" > diff_exp &&
42
+ test_cmp diff_exp diff_out
43
+'
44
+
45
+test_expect_success "diff against self (empty dir) is empty" '
46
+ ipfs object diff $EMPTY_DIR $EMPTY_DIR > diff_out
47
+ printf "" > diff_exp &&
48
+ test_cmp diff_exp diff_out
49
+'
50
+
51
test_expect_success "diff added link works" '
52
ipfs object diff $A $B > diff_out
53
'