Skip to content

Commit 13ca9f4

Browse files
committed
fix: return the shortest, completely resolved path in the resolve command
fixes #5703 License: MIT Signed-off-by: Steven Allen <[email protected]>
1 parent e3c6776 commit 13ca9f4

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

core/commands/resolve.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,7 @@ Resolve the value of an IPFS DAG path:
134134
return err
135135
}
136136

137-
if rp.Remainder() != "" {
138-
// TODO: js expects this error. Instead of fixing this
139-
// error, we should fix #5703.
140-
return fmt.Errorf("found non-link at given path")
141-
}
142-
143-
return cmds.EmitOnce(res, &ncmd.ResolvedPath{Path: path.Path("/" + rp.Namespace() + "/" + rp.Cid().String())})
137+
return cmds.EmitOnce(res, &ncmd.ResolvedPath{Path: path.Path(rp.Simplify().String())})
144138
},
145139
Encoders: cmds.EncoderMap{
146140
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, rp *ncmd.ResolvedPath) error {

core/coreapi/interface/path.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ type ResolvedPath interface {
8989
// For more examples see the documentation of Cid() method
9090
Remainder() string
9191

92+
// Simplify fully reduces the path to one that traverses no links.
93+
Simplify() ResolvedPath
94+
9295
Path
9396
}
9497

@@ -174,3 +177,16 @@ func (p *resolvedPath) Root() cid.Cid {
174177
func (p *resolvedPath) Remainder() string {
175178
return p.remainder
176179
}
180+
181+
func (p *resolvedPath) Simplify() ResolvedPath {
182+
if p.cid.Equals(p.root) {
183+
return p
184+
}
185+
186+
simplified := "/" + p.Namespace() + "/" + p.cid.String()
187+
if p.remainder != "" {
188+
simplified += "/" + p.remainder
189+
}
190+
191+
return NewResolvedPath(ipfspath.Path(simplified), p.cid, p.cid, p.remainder)
192+
}

test/sharness/t0160-resolve.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ test_resolve_cmd() {
6262
test_resolve "/ipfs/$a_hash/b" "/ipfs/$b_hash"
6363
test_resolve "/ipfs/$a_hash/b/c" "/ipfs/$c_hash"
6464
test_resolve "/ipfs/$b_hash/c" "/ipfs/$c_hash"
65+
test_resolve "/ipld/$dag_hash/a/b/c" "/ipld/$dag_hash/a/b/c"
66+
test_resolve "/ipld/$dag_hash/a/b" "/ipld/$dag_hash/a/b"
6567

6668
test_resolve_setup_name "/ipfs/$a_hash"
6769
test_resolve "/ipns/$id_hash" "/ipfs/$a_hash"

0 commit comments

Comments
 (0)