Skip to content

Commit d585e20

Browse files
committedJun 20, 2015
allow patch add-link to add at a path
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
1 parent 370df8f commit d585e20

File tree

2 files changed

+63
-10
lines changed

2 files changed

+63
-10
lines changed
 

‎core/commands/object.go

+49-10
Original file line numberDiff line numberDiff line change
@@ -582,36 +582,75 @@ func addLinkCaller(req cmds.Request, root *dag.Node) (key.Key, error) {
582582
return "", err
583583
}
584584

585-
name := req.Arguments()[2]
585+
path := req.Arguments()[2]
586586
childk := key.B58KeyDecode(req.Arguments()[3])
587587

588-
newkey, err := addLink(req.Context().Context, nd.DAG, root, name, childk)
588+
parts := strings.Split(path, "/")
589+
590+
nnode, err := insertNodeAtPath(req.Context().Context, nd.DAG, root, parts, childk)
589591
if err != nil {
590592
return "", err
591593
}
592-
593-
return newkey, nil
594+
return nnode.Key()
594595
}
595596

596-
func addLink(ctx context.Context, ds dag.DAGService, root *dag.Node, childname string, childk key.Key) (key.Key, error) {
597+
func addLink(ctx context.Context, ds dag.DAGService, root *dag.Node, childname string, childk key.Key) (*dag.Node, error) {
597598
ctx, cancel := context.WithTimeout(ctx, time.Second*30)
598599
childnd, err := ds.Get(ctx, childk)
599600
if err != nil {
600601
cancel()
601-
return "", err
602+
return nil, err
602603
}
603604
cancel()
604605

605606
err = root.AddNodeLinkClean(childname, childnd)
606607
if err != nil {
607-
return "", err
608+
return nil, err
608609
}
609610

610-
newkey, err := ds.Add(root)
611+
_, err = ds.Add(root)
611612
if err != nil {
612-
return "", err
613+
return nil, err
613614
}
614-
return newkey, nil
615+
return root, nil
616+
}
617+
618+
func insertNodeAtPath(ctx context.Context, ds dag.DAGService, root *dag.Node, path []string, toinsert key.Key) (*dag.Node, error) {
619+
if len(path) == 1 {
620+
return addLink(ctx, ds, root, path[0], toinsert)
621+
}
622+
623+
child, err := root.GetNodeLink(path[0])
624+
if err != nil {
625+
return nil, err
626+
}
627+
628+
nd, err := child.GetNode(ctx, ds)
629+
if err != nil {
630+
return nil, err
631+
}
632+
633+
ndprime, err := insertNodeAtPath(ctx, ds, nd, path[1:], toinsert)
634+
if err != nil {
635+
return nil, err
636+
}
637+
638+
err = root.RemoveNodeLink(path[0])
639+
if err != nil {
640+
return nil, err
641+
}
642+
643+
err = root.AddNodeLinkClean(path[0], ndprime)
644+
if err != nil {
645+
return nil, err
646+
}
647+
648+
_, err = ds.Add(root)
649+
if err != nil {
650+
return nil, err
651+
}
652+
653+
return root, nil
615654
}
616655

617656
func nodeFromTemplate(template string) (*dag.Node, error) {

‎test/sharness/t0051-object.sh

+14
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,20 @@ test_object_cmd() {
100100
OUTPUT=$(ipfs object patch $EMPTY_DIR add-link foo $EMPTY_DIR)
101101
'
102102

103+
test_expect_success "multilayer ipfs patch works" '
104+
echo "hello world" > hwfile &&
105+
FILE=$(ipfs add -q hwfile) &&
106+
EMPTY=$(ipfs object new unixfs-dir) &&
107+
ONE=$(ipfs object patch $EMPTY add-link b $EMPTY) &&
108+
TWO=$(ipfs object patch $EMPTY add-link a $ONE) &&
109+
ipfs object patch $TWO add-link a/b/c $FILE > multi_patch
110+
'
111+
112+
test_expect_success "output looks good" '
113+
ipfs cat $(cat multi_patch)/a/b/c > hwfile_out &&
114+
test_cmp hwfile hwfile_out
115+
'
116+
103117
test_expect_success "should have created dir within a dir" '
104118
ipfs ls $OUTPUT > patched_output
105119
'

0 commit comments

Comments
 (0)
Please sign in to comment.