fix(object): add Diff() comment and test case
License: MIT Signed-off-by: Overbool <overbool.xu@gmail.com>
Overbool committed
Sep 17, 2018 at 11:24 UTC
5b0a94895f87bfc4c8ff686846b5d4989d33b399
2 files changed
+48
-14
dagutils/diff.go
+6
-2
@@ -94,7 +94,11 @@ func ApplyChange(ctx context.Context, ds ipld.DAGService, nd *dag.ProtoNode, cs
94
return e.Finalize(ctx, ds)
95
}
96
97
-// Diff returns a set of changes that transform node 'a' into node 'b'
97
+// Diff returns a set of changes that transform node 'a' into node 'b'.
98
+// It supports two nodes forms: ProtoNode and RawNode. Because we treats
99
+// the nodes as IPLD nodes as long as possible and only convert them
100
+// to ProtoNode when necessary: when we need to remove links, and at that point
101
+// (if they have links to remove) we know they are not raw nodes.
102
func Diff(ctx context.Context, ds ipld.DAGService, a, b ipld.Node) ([]*Change, error) {
103
// Base case where both nodes are leaves, just compare
104
// their CIDs.
@@ -103,7 +107,7 @@ func Diff(ctx context.Context, ds ipld.DAGService, a, b ipld.Node) ([]*Change, e
107
return []*Change{}, nil
108
}
109
return []*Change{
106
- &Change{
110
+ {
111
Type: Mod,
112
Before: a.Cid(),
113
After: b.Cid(),
test/sharness/t0052-object-diff.sh
+42
-12
@@ -15,82 +15,112 @@ test_expect_success "create some objects for testing diffs" '
15
echo "stuff" > foo/bar &&
16
mkdir foo/baz &&
17
A=$(ipfs add -r -q foo | tail -n1) &&
18
+ AR=$(ipfs add --raw-leaves -r -q foo | tail -n1) &&
19
echo "more things" > foo/cat &&
20
B=$(ipfs add -r -q foo | tail -n1) &&
21
+ BR=$(ipfs add --raw-leaves -r -q foo | tail -n1) &&
22
echo "nested" > foo/baz/dog &&
23
C=$(ipfs add -r -q foo | tail -n1)
24
+ CR=$(ipfs add --raw-leaves -r -q foo | tail -n1)
25
echo "changed" > foo/bar &&
26
D=$(ipfs add -r -q foo | tail -n1) &&
27
+ DR=$(ipfs add --raw-leaves -r -q foo | tail -n1) &&
28
echo "" > single_file &&
29
SINGLE_FILE=$(ipfs add -r -q single_file | tail -n1) &&
30
+ SINGLE_FILE_RAW=$(ipfs add --raw-leaves -r -q single_file | tail -n1) &&
31
mkdir empty_dir
32
EMPTY_DIR=$(ipfs add -r -q empty_dir | tail -n1)
33
+ EMPTY_DIR_RAW=$(ipfs add --raw-leaves -r -q empty_dir | tail -n1)
34
'
35
36
test_expect_success "diff against self is empty" '
31
- ipfs object diff $A $A > diff_out
37
+ ipfs object diff $A $A > diff_out &&
38
+ ipfs object diff $AR $AR > diff_raw_out
39
'
40
41
test_expect_success "identity diff output looks good" '
42
printf "" > diff_exp &&
43
+ printf "" > diff_raw_exp &&
44
test_cmp diff_exp diff_out
45
+ test_cmp diff_raw_exp diff_raw_out
46
'
47
48
test_expect_success "diff against self (single file) is empty" '
40
- ipfs object diff $SINGLE_FILE $SINGLE_FILE > diff_out
49
+ ipfs object diff $SINGLE_FILE $SINGLE_FILE > diff_out &&
50
+ ipfs object diff $SINGLE_FILE_RAW $SINGLE_FILE_RAW > diff_raw_out
51
printf "" > diff_exp &&
42
- test_cmp diff_exp diff_out
52
+ printf "" > diff_raw_exp &&
53
+ test_cmp diff_exp diff_out &&
54
+ test_cmp diff_raw_exp diff_raw_out
55
'
56
57
test_expect_success "diff against self (empty dir) is empty" '
58
ipfs object diff $EMPTY_DIR $EMPTY_DIR > diff_out
59
+ ipfs object diff $EMPTY_DIR_RAW $EMPTY_DIR_RAW > diff_raw_out
60
printf "" > diff_exp &&
48
- test_cmp diff_exp diff_out
61
+ printf "" > diff_raw_exp &&
62
+ test_cmp diff_exp diff_out &&
63
+ test_cmp diff_raw_exp diff_raw_out
64
'
65
66
test_expect_success "diff added link works" '
67
ipfs object diff $A $B > diff_out
68
+ ipfs object diff $AR $BR > diff_raw_out
69
'
70
71
test_expect_success "diff added link looks right" '
72
echo + QmUSvcqzhdfYM1KLDbM76eLPdS9ANFtkJvFuPYeZt73d7A \"cat\" > diff_exp &&
57
- test_cmp diff_exp diff_out
73
+ echo + zb2rhmWNFDCdMjJoCZPE5b5NuU38yoRzRmEtfzb4exxk3R8g4 \"cat\" > diff_raw_exp &&
74
+ test_cmp diff_exp diff_out &&
75
+ test_cmp diff_raw_exp diff_raw_out
76
'
77
78
test_expect_success "verbose diff added link works" '
79
ipfs object diff -v $A $B > diff_out
80
+ ipfs object diff -v $AR $BR > diff_raw_out
81
'
82
83
test_expect_success "verbose diff added link looks right" '
84
echo Added new link \"cat\" pointing to QmUSvcqzhdfYM1KLDbM76eLPdS9ANFtkJvFuPYeZt73d7A. > diff_exp &&
66
- test_cmp diff_exp diff_out
85
+ echo Added new link \"cat\" pointing to zb2rhmWNFDCdMjJoCZPE5b5NuU38yoRzRmEtfzb4exxk3R8g4. > diff_raw_exp &&
86
+ test_cmp diff_exp diff_out &&
87
+ test_cmp diff_raw_exp diff_raw_out
88
'
89
90
test_expect_success "diff removed link works" '
70
- ipfs object diff -v $B $A > diff_out
91
+ ipfs object diff -v $B $A > diff_out &&
92
+ ipfs object diff -v $BR $AR > diff_raw_out
93
'
94
95
test_expect_success "diff removed link looks right" '
96
echo Removed link \"cat\" \(was QmUSvcqzhdfYM1KLDbM76eLPdS9ANFtkJvFuPYeZt73d7A\). > diff_exp &&
75
- test_cmp diff_exp diff_out
97
+ echo Removed link \"cat\" \(was zb2rhmWNFDCdMjJoCZPE5b5NuU38yoRzRmEtfzb4exxk3R8g4\). > diff_raw_exp &&
98
+ test_cmp diff_exp diff_out &&
99
+ test_cmp diff_raw_exp diff_raw_out
100
'
101
102
test_expect_success "diff nested add works" '
79
- ipfs object diff -v $B $C > diff_out
103
+ ipfs object diff -v $B $C > diff_out &&
104
+ ipfs object diff -v $BR $CR > diff_raw_out
105
'
106
107
test_expect_success "diff looks right" '
108
echo Added new link \"baz/dog\" pointing to QmdNJQUTZuDpsUcec7YDuCfRfvw1w4J13DCm7YcU4VMZdS. > diff_exp &&
84
- test_cmp diff_exp diff_out
109
+ echo Added new link \"baz/dog\" pointing to zb2rhaM8wjDfi8A22dEqk89raWtViq8pjxvKQu2eaKtWhYKgE. > diff_raw_exp &&
110
+ test_cmp diff_exp diff_out &&
111
+ test_cmp diff_raw_exp diff_raw_out
112
'
113
114
test_expect_success "diff changed link works" '
88
- ipfs object diff -v $C $D > diff_out
115
+ ipfs object diff -v $C $D > diff_out &&
116
+ ipfs object diff -v $CR $DR > diff_raw_out
117
'
118
119
test_expect_success "diff looks right" '
120
echo Changed \"bar\" from QmNgd5cz2jNftnAHBhcRUGdtiaMzb5Rhjqd4etondHHST8 to QmRfFVsjSXkhFxrfWnLpMae2M4GBVsry6VAuYYcji5MiZb. > diff_exp &&
93
- test_cmp diff_exp diff_out
121
+ echo Changed \"bar\" from zb2rhdUECGnPgMJNgmghaMKdqqGdpTe9GmEJiPna488ThfLBz to zb2rhfEA1M13SPoeayrsPcKhCezgMQPjguGFLH56G8qQ2qpDn. > diff_raw_exp &&
122
+ test_cmp diff_exp diff_out &&
123
+ test_cmp diff_raw_exp diff_raw_out
124
'
125
126
test_done