MCPcopy
hub / github.com/wagoodman/dive / AddPath

Method AddPath

dive/filetree/file_tree.go:244–282  ·  view source on GitHub ↗

AddPath adds a new node to the tree with the given payload

(filepath string, data FileInfo)

Source from the content-addressed store, hash-verified

242
243// AddPath adds a new node to the tree with the given payload
244func (tree *FileTree) AddPath(filepath string, data FileInfo) (*FileNode, []*FileNode, error) {
245 filepath = path.Clean(filepath)
246 if filepath == "." {
247 return nil, nil, fmt.Errorf("cannot add relative path '%s'", filepath)
248 }
249 nodeNames := strings.Split(strings.Trim(filepath, "/"), "/")
250 node := tree.Root
251 addedNodes := make([]*FileNode, 0)
252 for idx, name := range nodeNames {
253 if name == "" {
254 continue
255 }
256 // find or create node
257 if node.Children[name] != nil {
258 node = node.Children[name]
259 } else {
260 // don't add paths that should be deleted
261 if strings.HasPrefix(name, doubleWhiteoutPrefix) {
262 return nil, addedNodes, nil
263 }
264
265 // don't attach the payload. The payload is destined for the
266 // Path's end node, not any intermediary node.
267 node = node.AddChild(name, FileInfo{})
268 addedNodes = append(addedNodes, node)
269
270 if node == nil {
271 // the child could not be added
272 return node, addedNodes, fmt.Errorf("could not add child node: '%s' (path:'%s')", name, filepath)
273 }
274 }
275
276 // attach payload to the last specified node
277 if idx == len(nodeNames)-1 {
278 node.Data.FileInfo = data
279 }
280 }
281 return node, addedNodes, nil
282}
283
284// RemovePath removes a node from the tree given its path.
285func (tree *FileTree) RemovePath(path string) error {

Callers 15

TestStringBetweenFunction · 0.95
TestAddRelativePathFunction · 0.95
TestAddPathFunction · 0.95
TestAddWhiteoutPathFunction · 0.95
TestRemovePathFunction · 0.95
TestStackFunction · 0.95
TestCopyFunction · 0.95
TestCompareWithNoChangesFunction · 0.95
TestCompareWithAddsFunction · 0.95
TestCompareWithChangesFunction · 0.95
TestCompareWithRemovesFunction · 0.95

Calls 1

AddChildMethod · 0.80

Tested by 15

TestStringBetweenFunction · 0.76
TestAddRelativePathFunction · 0.76
TestAddPathFunction · 0.76
TestAddWhiteoutPathFunction · 0.76
TestRemovePathFunction · 0.76
TestStackFunction · 0.76
TestCopyFunction · 0.76
TestCompareWithNoChangesFunction · 0.76
TestCompareWithAddsFunction · 0.76
TestCompareWithChangesFunction · 0.76
TestCompareWithRemovesFunction · 0.76