1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
package fetch
import (
"errors"
"fmt"
oid "lindenii.org/go/furgit/object/id"
"lindenii.org/go/furgit/object/tree"
"lindenii.org/go/furgit/object/tree/mode"
)
var ErrPathInvalid = errors.New("object/fetch: invalid tree path")
// PathNotFoundError indicates that one tree path segment was not found.
type PathNotFoundError struct {
Index int
Name []byte
}
func (err *PathNotFoundError) Error() string {
return fmt.Sprintf("object/fetch: tree entry %q not found at index %d", err.Name, err.Index)
}
// PathNotTreeError indicates that one intermediate path segment was not a tree.
type PathNotTreeError struct {
Index int
Name []byte
}
func (err *PathNotTreeError) Error() string {
return fmt.Sprintf("object/fetch: path segment %q at index %d is not a tree", err.Name, err.Index)
}
// Path resolves parts within the tree identified by root
// and returns the final tree entry.
//
// The root object may be any tree-ish object accepted by PeelToTree.
//
// parts must contain at least one path segment.
// Intermediate path segments must resolve to tree entries.
// The final entry is returned without loading its object.
// Path segments may not contain \x00.
//
// If your entry names are valid UTF-8
// and uses / solely as segment separators,
// it may be convenient to use TreeFS
// for an io/fs.FS-like interface.
//
// Labels: Life-Parent.
func (fetcher *Fetcher) Path(root oid.ObjectID, parts []string) (tree.Entry, error) {
if len(parts) == 0 {
return tree.Entry{}, ErrPathInvalid
}
current, err := fetcher.PeelToTree(root)
if err != nil {
return tree.Entry{}, err
}
for i, part := range parts {
if len(part) == 0 {
return tree.Entry{}, ErrPathInvalid
}
entry, ok := current.Object().Find(part)
if !ok {
return tree.Entry{}, &PathNotFoundError{
Index: i,
Name: append([]byte(nil), part...),
}
}
if i == len(parts)-1 {
return entry, nil
}
if entry.Mode != mode.Directory {
return tree.Entry{}, &PathNotTreeError{
Index: i,
Name: append([]byte(nil), part...),
}
}
current, err = fetcher.ExactTree(entry.ID)
if err != nil {
return tree.Entry{}, err
}
}
return tree.Entry{}, &PathNotFoundError{Index: len(parts) - 1}
}
|