Skip to content

Commit a26bf59

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 0182659 commit a26bf59

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

core/commands/resolve.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,8 @@ Resolve the value of an IPFS DAG path:
135135
return err
136136
}
137137

138-
c := rp.Cid()
139-
140-
return cmds.EmitOnce(res, &ncmd.ResolvedPath{Path: path.FromCid(c)})
138+
rp = coreiface.Simplify(rp)
139+
return cmds.EmitOnce(res, &ncmd.ResolvedPath{Path: path.Path(rp)})
141140
},
142141
Encoders: cmds.EncoderMap{
143142
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {

core/coreapi/interface/path.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,22 @@ func IpldPath(c cid.Cid) ResolvedPath {
125125
}
126126
}
127127

128+
// Simplify takes a ResolvedPath and returns a *new* ResovledPath that's as
129+
// short as possible. In the new resolved path, `p.Cid()` will be the same as
130+
// `p.Root()`.
131+
func Simplify(p ResolvedPath) ResolvedPath {
132+
c := p.Cid()
133+
if c == p.Root() {
134+
return p
135+
}
136+
simplified := "/" + p.Namespace() + "/" + c.String()
137+
remainder := p.Remainder()
138+
if remainder != "" {
139+
simplified += "/" + remainder
140+
}
141+
return NewResolvedPath(ipfspath.Path(simplified), c, c, remainder)
142+
}
143+
128144
// ParsePath parses string path to a Path
129145
func ParsePath(p string) (Path, error) {
130146
pp, err := ipfspath.ParsePath(p)

test/sharness/t0160-resolve.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ test_expect_success "resolve: prepare files" '
1414
c_hash=$(ipfs add -q -r a/b/c | tail -n1)
1515
'
1616

17+
test_expect_success "resolve: prepare dag" '
18+
dag_hash=$(ipfs dag put <<<"{\"a\": {\"b\": {\"c\": \"asdfasdfasdf\"}}}")
19+
'
20+
1721
test_resolve_setup_name() {
1822
ref=$1
1923

@@ -58,6 +62,8 @@ test_resolve_cmd() {
5862
test_resolve "/ipfs/$a_hash/b" "/ipfs/$b_hash"
5963
test_resolve "/ipfs/$a_hash/b/c" "/ipfs/$c_hash"
6064
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"
6167

6268
test_resolve_setup_name "/ipfs/$a_hash"
6369
test_resolve "/ipns/$id_hash" "/ipfs/$a_hash"

0 commit comments

Comments
 (0)