MCPcopy Index your code
hub / github.com/devspace-sh/devspace / AddEdge

Method AddEdge

pkg/devspace/dependency/graph/graph.go:143–172  ·  view source on GitHub ↗

AddEdge adds a new edge from a node to a node and returns an error if it would result in a cyclic graph

(fromID string, toID string)

Source from the content-addressed store, hash-verified

141
142// AddEdge adds a new edge from a node to a node and returns an error if it would result in a cyclic graph
143func (g *Graph) AddEdge(fromID string, toID string) error {
144 from, ok := g.Nodes[fromID]
145 if !ok {
146 return errors.Errorf("fromID %s does not exist", fromID)
147 }
148 to, ok := g.Nodes[toID]
149 if !ok {
150 return errors.Errorf("toID %s does not exist", toID)
151 }
152
153 // Check if cyclic
154 path := findFirstPath(to, from)
155 if path != nil {
156 return &CyclicError{
157 path: path,
158 What: g.item,
159 }
160 }
161
162 // Check if there is already an edge
163 for _, child := range from.Childs {
164 if child.ID == to.ID {
165 return nil
166 }
167 }
168
169 from.Childs = append(from.Childs, to)
170 to.Parents = append(to.Parents, from)
171 return nil
172}
173
174// find first path from node to node with DFS
175func findFirstPath(from *Node, to *Node) []*Node {

Callers 4

InsertNodeAtMethod · 0.95
resolveRecursiveMethod · 0.80
TestGraphFunction · 0.80
insertVariableGraphMethod · 0.80

Calls 2

findFirstPathFunction · 0.85
ErrorfMethod · 0.45

Tested by 1

TestGraphFunction · 0.64