提交 bf7da852 作者: Jeromy

basic implementation of object diff

License: MIT
Signed-off-by: 's avatarJeromy <jeromyj@gmail.com>
上级 b0a613fd
package objectcmd
import (
"bytes"
"fmt"
"io"
cmds "github.com/ipfs/go-ipfs/commands"
dagutils "github.com/ipfs/go-ipfs/merkledag/utils"
path "github.com/ipfs/go-ipfs/path"
)
var ObjectDiffCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "takes a diff of the two given objects",
ShortDescription: `
`,
},
Arguments: []cmds.Argument{
cmds.StringArg("obj_a", true, false, "object to diff against"),
cmds.StringArg("obj_b", true, false, "object to diff"),
},
Run: func(req cmds.Request, res cmds.Response) {
node, err := req.InvocContext().GetNode()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
a := req.Arguments()[0]
b := req.Arguments()[1]
pa, err := path.ParsePath(a)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
pb, err := path.ParsePath(b)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
ctx := req.Context()
obj_a, err := node.Resolver.ResolvePath(ctx, pa)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
obj_b, err := node.Resolver.ResolvePath(ctx, pb)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
changes, err := dagutils.Diff(ctx, node.DAG, obj_a, obj_b)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
res.SetOutput(changes)
},
Type: []*dagutils.Change{},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
changes := res.Output().([]*dagutils.Change)
buf := new(bytes.Buffer)
for _, change := range changes {
switch change.Type {
case dagutils.Add:
fmt.Fprintf(buf, "added new link %q pointing to %s\n", change.Path, change.After)
case dagutils.Mod:
fmt.Fprintf(buf, "changed %q from %s to %s\n", change.Path, change.Before, change.After)
case dagutils.Remove:
fmt.Fprintf(buf, "removed link %q (was %s)\n", change.Path, change.Before)
}
}
return buf, nil
},
},
}
......@@ -48,13 +48,14 @@ var ObjectCmd = &cmds.Command{
'ipfs object' is a plumbing command used to manipulate DAG objects
directly.`,
Synopsis: `
ipfs object data <key> - Outputs raw bytes in an object
ipfs object get <key> - Get the DAG node named by <key>
ipfs object links <key> - Outputs links pointed to by object
ipfs object new <template> - Create new ipfs objects
ipfs object patch <args> - Create new object from old ones
ipfs object put <data> - Stores input, outputs its key
ipfs object stat <key> - Outputs statistics of object
ipfs object data <key> - Outputs raw bytes in an object
ipfs object get <key> - Get the DAG node named by <key>
ipfs object links <key> - Outputs links pointed to by object
ipfs object new <template> - Create new ipfs objects
ipfs object patch <args> - Create new object from old ones
ipfs object put <data> - Stores input, outputs its key
ipfs object stat <key> - Outputs statistics of object
ipfs object diff <key1> <key2> - Diffs two given objects
`,
},
......@@ -66,6 +67,7 @@ ipfs object stat <key> - Outputs statistics of object
"patch": ObjectPatchCmd,
"put": ObjectPutCmd,
"stat": ObjectStatCmd,
"diff": ObjectDiffCmd,
},
}
......
......@@ -75,17 +75,25 @@ func ApplyChange(ctx context.Context, ds dag.DAGService, nd *dag.Node, cs []*Cha
return e.Finalize(ds)
}
func Diff(ctx context.Context, ds dag.DAGService, a, b *dag.Node) []*Change {
func Diff(ctx context.Context, ds dag.DAGService, a, b *dag.Node) ([]*Change, error) {
if len(a.Links) == 0 && len(b.Links) == 0 {
ak, _ := a.Key()
bk, _ := b.Key()
ak, err := a.Key()
if err != nil {
return nil, err
}
bk, err := b.Key()
if err != nil {
return nil, err
}
return []*Change{
&Change{
Type: Mod,
Before: ak,
After: bk,
},
}
}, nil
}
var out []*Change
......@@ -99,9 +107,20 @@ func Diff(ctx context.Context, ds dag.DAGService, a, b *dag.Node) []*Change {
if bytes.Equal(l.Hash, lnk.Hash) {
// no change... ignore it
} else {
anode, _ := lnk.GetNode(ctx, ds)
bnode, _ := l.GetNode(ctx, ds)
sub := Diff(ctx, ds, anode, bnode)
anode, err := lnk.GetNode(ctx, ds)
if err != nil {
return nil, err
}
bnode, err := l.GetNode(ctx, ds)
if err != nil {
return nil, err
}
sub, err := Diff(ctx, ds, anode, bnode)
if err != nil {
return nil, err
}
for _, subc := range sub {
subc.Path = path.Join(lnk.Name, subc.Path)
......@@ -128,7 +147,7 @@ func Diff(ctx context.Context, ds dag.DAGService, a, b *dag.Node) []*Change {
})
}
return out
return out, nil
}
type Conflict struct {
......
#!/bin/sh
#
# Copyright (c) 2016 Jeromy Johnson
# MIT Licensed; see the LICENSE file in this repository.
#
test_description="Test object diff command"
. lib/test-lib.sh
test_init_ipfs
test_expect_success "create some objects for testing diffs" '
mkdir foo &&
echo "stuff" > foo/bar &&
mkdir foo/baz &&
A=$(ipfs add -r -q foo | tail -n1) &&
echo "more things" > foo/cat &&
B=$(ipfs add -r -q foo | tail -n1) &&
echo "nested" > foo/baz/dog &&
C=$(ipfs add -r -q foo | tail -n1)
echo "changed" > foo/bar &&
D=$(ipfs add -r -q foo | tail -n1)
'
test_expect_success "diff against self is empty" '
ipfs object diff $A $A > diff_out
'
test_expect_success "identity diff output looks good" '
printf "" > diff_exp &&
test_cmp diff_exp diff_out
'
test_expect_success "diff added link works" '
ipfs object diff $A $B > diff_out
'
test_expect_success "diff added link looks right" '
echo added new link \"cat\" pointing to QmUSvcqzhdfYM1KLDbM76eLPdS9ANFtkJvFuPYeZt73d7A > diff_exp &&
test_cmp diff_exp diff_out
'
test_expect_success "diff removed link works" '
ipfs object diff $B $A > diff_out
'
test_expect_success "diff removed link looks right" '
echo removed link \"cat\" \(was QmUSvcqzhdfYM1KLDbM76eLPdS9ANFtkJvFuPYeZt73d7A\) > diff_exp &&
test_cmp diff_exp diff_out
'
test_expect_success "diff nested add works" '
ipfs object diff $B $C > diff_out
'
test_expect_success "diff looks right" '
echo added new link \"baz/dog\" pointing to QmdNJQUTZuDpsUcec7YDuCfRfvw1w4J13DCm7YcU4VMZdS > diff_exp &&
test_cmp diff_exp diff_out
'
test_expect_success "diff changed link works" '
ipfs object diff $C $D > diff_out
'
test_expect_success "diff looks right" '
echo changed \"bar\" from QmNgd5cz2jNftnAHBhcRUGdtiaMzb5Rhjqd4etondHHST8 to QmRfFVsjSXkhFxrfWnLpMae2M4GBVsry6VAuYYcji5MiZb > diff_exp &&
test_cmp diff_exp diff_out
'
test_done
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论