1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-28 00:39:31 +08:00

Merge pull request #2562 from ipfs/feat/object-diff

implement object diff command
This commit is contained in:
Jeromy Johnson
2016-04-18 12:53:48 -07:00
4 changed files with 237 additions and 15 deletions

View File

@ -0,0 +1,121 @@
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: `
ipfs object diff is a command used to show the differences between
two ipfs objects.`,
LongDescription: `
ipfs object diff is a command used to show the differences between
two ipfs objects.
Example:
$ ls foo
bar baz/ giraffe
$ ipfs add -r foo
...
added QmegHcnrPgMwC7tBiMxChD54fgQMBUecNw9nE9UUU4x1bz foo
$ OBJ_A=QmegHcnrPgMwC7tBiMxChD54fgQMBUecNw9nE9UUU4x1bz
$ echo "different content" > foo/bar
$ ipfs add -r foo
...
added QmcmRptkSPWhptCttgHg27QNDmnV33wAJyUkCnAvqD3eCD foo
$ OBJ_B=QmcmRptkSPWhptCttgHg27QNDmnV33wAJyUkCnAvqD3eCD
$ ipfs object diff -v $OBJ_A $OBJ_B
changed "bar" from QmNgd5cz2jNftnAHBhcRUGdtiaMzb5Rhjqd4etondHHST8 to QmRfFVsjSXkhFxrfWnLpMae2M4GBVsry6VAuYYcji5MiZb
`,
},
Arguments: []cmds.Argument{
cmds.StringArg("obj_a", true, false, "object to diff against"),
cmds.StringArg("obj_b", true, false, "object to diff"),
},
Options: []cmds.Option{
cmds.BoolOption("verbose", "v", "Produce verbose output"),
},
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) {
verbose, _, _ := res.Request().Option("v").Bool()
changes := res.Output().([]*dagutils.Change)
buf := new(bytes.Buffer)
for _, change := range changes {
if verbose {
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)
}
} else {
switch change.Type {
case dagutils.Add:
fmt.Fprintf(buf, "+ %s %q\n", change.After, change.Path)
case dagutils.Mod:
fmt.Fprintf(buf, "~ %s %s %q\n", change.Before, change.After, change.Path)
case dagutils.Remove:
fmt.Fprintf(buf, "- %s %q\n", change.Before, change.Path)
}
}
}
return buf, nil
},
},
}

View File

@ -49,6 +49,7 @@ var ObjectCmd = &cmds.Command{
directly.`,
Synopsis: `
ipfs object data <key> - Outputs raw bytes in an object
ipfs object diff <key1> <key2> - Diffs two given objects
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
@ -60,6 +61,7 @@ ipfs object stat <key> - Outputs statistics of object
Subcommands: map[string]*cmds.Command{
"data": ObjectDataCmd,
"diff": ObjectDiffCmd,
"get": ObjectGetCmd,
"links": ObjectLinksCmd,
"new": ObjectNewCmd,

View File

@ -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 {

View File

@ -0,0 +1,80 @@
#!/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 + QmUSvcqzhdfYM1KLDbM76eLPdS9ANFtkJvFuPYeZt73d7A \"cat\" > diff_exp &&
test_cmp diff_exp diff_out
'
test_expect_success "verbose diff added link works" '
ipfs object diff -v $A $B > diff_out
'
test_expect_success "verbose 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 -v $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 -v $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 -v $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