Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
c6daf934
Commit
c6daf934
authored
6 years ago
by
Overbool
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(diff): modify diff logic and comment
License: MIT Signed-off-by:
Overbool
<
overbool.xu@gmail.com
>
parent
7cda005a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
25 deletions
+28
-25
dagutils/diff.go
dagutils/diff.go
+25
-22
test/sharness/t0052-object-diff.sh
test/sharness/t0052-object-diff.sh
+3
-3
No files found.
dagutils/diff.go
View file @
c6daf934
...
...
@@ -95,29 +95,23 @@ func ApplyChange(ctx context.Context, ds ipld.DAGService, nd *dag.ProtoNode, cs
}
// Diff returns a set of changes that transform node 'a' into node 'b'.
// It
supports two nodes forms: ProtoNode and RawNode. Be
ca
u
se
we treat
//
the
nodes
as IPLD nodes as long as possible and only conv
er
t
th
em
//
to ProtoNode when necessary: when we need to remove links, and at that point
//
(if they have links to remove) we know they are not raw nodes
.
// It
only traverses links in the following
case
s:
//
1. two
node
'
s
links number are great
er th
an 0.
//
2. both of two nodes are ProtoNode.
//
Otherwise, it compares the cid and emits a Mod change object
.
func
Diff
(
ctx
context
.
Context
,
ds
ipld
.
DAGService
,
a
,
b
ipld
.
Node
)
([]
*
Change
,
error
)
{
// Base case where both nodes are leaves, just compare
// their CIDs.
if
len
(
a
.
Links
())
==
0
&&
len
(
b
.
Links
())
==
0
{
if
a
.
Cid
()
.
Equals
(
b
.
Cid
())
{
return
[]
*
Change
{},
nil
}
return
[]
*
Change
{
{
Type
:
Mod
,
Before
:
a
.
Cid
(),
After
:
b
.
Cid
(),
},
},
nil
return
getChange
(
a
,
b
)
}
var
out
[]
*
Change
cleanA
:=
a
.
Copy
()
cleanB
:=
b
.
Copy
()
cleanA
,
okA
:=
a
.
Copy
()
.
(
*
dag
.
ProtoNode
)
cleanB
,
okB
:=
b
.
Copy
()
.
(
*
dag
.
ProtoNode
)
if
!
okA
||
!
okB
{
return
getChange
(
a
,
b
)
}
// strip out unchanged stuff
for
_
,
lnk
:=
range
a
.
Links
()
{
...
...
@@ -146,12 +140,8 @@ func Diff(ctx context.Context, ds ipld.DAGService, a, b ipld.Node) ([]*Change, e
out
=
append
(
out
,
subc
)
}
}
if
cleanA
,
ok
:=
cleanA
.
(
*
dag
.
ProtoNode
);
ok
{
cleanA
.
RemoveNodeLink
(
l
.
Name
)
}
if
cleanB
,
ok
:=
cleanB
.
(
*
dag
.
ProtoNode
);
ok
{
cleanB
.
RemoveNodeLink
(
l
.
Name
)
}
cleanA
.
RemoveNodeLink
(
l
.
Name
)
cleanB
.
RemoveNodeLink
(
l
.
Name
)
}
}
...
...
@@ -207,3 +197,16 @@ func MergeDiffs(a, b []*Change) ([]*Change, []Conflict) {
}
return
out
,
conflicts
}
func
getChange
(
a
,
b
ipld
.
Node
)
([]
*
Change
,
error
)
{
if
a
.
Cid
()
.
Equals
(
b
.
Cid
())
{
return
[]
*
Change
{},
nil
}
return
[]
*
Change
{
{
Type
:
Mod
,
Before
:
a
.
Cid
(),
After
:
b
.
Cid
(),
},
},
nil
}
This diff is collapsed.
Click to expand it.
test/sharness/t0052-object-diff.sh
View file @
c6daf934
...
...
@@ -58,19 +58,19 @@ test_expect_success "diff against self (single file) is empty" '
'
test_expect_success
"diff (raw-leaves) against self (single file) is empty"
'
ipfs object diff $SINGLE_FILE_RAW $SINGLE_FILE_RAW > diff_raw_out
ipfs object diff $SINGLE_FILE_RAW $SINGLE_FILE_RAW > diff_raw_out
&&
printf "" > diff_raw_exp &&
test_cmp diff_raw_exp diff_raw_out
'
test_expect_success
"diff against self (empty dir) is empty"
'
ipfs object diff $EMPTY_DIR $EMPTY_DIR > diff_out
ipfs object diff $EMPTY_DIR $EMPTY_DIR > diff_out
&&
printf "" > diff_exp &&
test_cmp diff_exp diff_out
'
test_expect_success
"diff (raw-leaves) against self (empty dir) is empty"
'
ipfs object diff $EMPTY_DIR_RAW $EMPTY_DIR_RAW > diff_raw_out
ipfs object diff $EMPTY_DIR_RAW $EMPTY_DIR_RAW > diff_raw_out
&&
printf "" > diff_raw_exp &&
test_cmp diff_raw_exp diff_raw_out
'
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment