Repository navigation
Expand file tree
/
Copy pathdirectory.go
More file actions
69 lines (64 loc) · 2.25 KB
/
Copy pathdirectory.go
File metadata and controls
69 lines (64 loc) · 2.25 KB
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
package codegraph
import (
"context"
"fmt"
"path"
)
// DirectoryID identifies a directory by its snapshot-relative path; "." is root.
func DirectoryID(path string) string { return "directory:" + path }
// DirectoryNode is a detached typed view of the same Directory node in Graph.
// Its existence records an ancestor of supplied material, not complete membership.
type DirectoryNode struct{ Node }
func (g *Graph) Directory(path string) (DirectoryNode, bool) {
n, ok := g.Node(DirectoryID(path))
if !ok || n.Kind != DirectoryNodeKind {
return DirectoryNode{}, false
}
return DirectoryNode{Node: n}, true
}
// +why=Path structure must not use contains: that relation defines semantic namespace ownership.
// +spec=Every published Document has its ancestor directories, including parse failures and opaque gitlinks; tags match each path independently.
func (b *Builder) publishDocumentStructure(ctx context.Context, nodes map[string]Node, relations map[string]Relation) error {
var documents []Node
for _, n := range nodes {
if n.Kind == DocumentNodeKind {
documents = append(documents, n)
}
}
evidence := 0
for _, r := range relations {
evidence += len(r.Evidence)
}
for _, n := range documents {
n.Path = n.Location.Path
n.Tags = b.tagMatcher.Match(n.Path)
nodes[n.ID] = n
child, parent := n.ID, path.Dir(n.Path)
for {
if err := ctx.Err(); err != nil {
return err
}
id := DirectoryID(parent)
_, exists := nodes[id]
if !exists {
if len(nodes) >= b.opts.MaxNodes {
return fmt.Errorf("%w: directory nodes", ErrBuildBudget)
}
nodes[id] = Node{ID: id, Kind: DirectoryNodeKind, Name: path.Base(parent), Path: parent, Tags: b.tagMatcher.Match(parent)}
}
edgeID := identity(child, id, InDirectory)
if len(relations) >= b.opts.MaxRelations || evidence >= b.opts.MaxEvidence {
return fmt.Errorf("%w: directory relations or evidence", ErrBuildBudget)
}
relations[edgeID] = Relation{ID: edgeID, Source: child, Target: id, Kind: InDirectory,
Confidence: Exact, Evidence: []Evidence{{Basis: "document_path", Confidence: Exact}}}
evidence++
// An existing directory already has its complete ancestor chain.
if exists || parent == "." {
break
}
child, parent = id, path.Dir(parent)
}
}
return nil
}