//The function which can implement ImportTreeFromDirectory. private void CreateTreeFromDirectory(string path, IndexingNode parent) { var directoryInfo = new DirectoryInfo(path); foreach (var f in directoryInfo.GetFiles()) { var fileNameHash = HashHelper.HashString(f.Name); var contentHash = HashHelper.HashFile(Path.Combine(path, f.Name)); var subFileNode = new IndexingNode(fileNameHash, contentHash, parent); _allNodes.Add(subFileNode); parent.AddChild(subFileNode); Project.CreateObject(f, fileNameHash, contentHash, _tempMode); } foreach (var d in directoryInfo.GetDirectories()) { if (d.Name == Project.PrivateFolderName) { // do not index directory of ourself // TODO: handle nested LocalVersionControlSystem continue; } var directoryNameHash = HashHelper.HashString(d.Name); var subDirectoryNode = new IndexingNode(directoryNameHash, parent); _allNodes.Add(subDirectoryNode); parent.AddChild(subDirectoryNode); Project.CreateObject(d, directoryNameHash, _tempMode); CreateTreeFromDirectory(Path.Combine(path, d.Name), subDirectoryNode); } }
//The function which can implement ImportTreeFromIndexing. private void CreateTreeFromIndexing(string[] indexing, int curLine, IndexingNode parent, int curLayer) { while (curLine + 1 < indexing.Length) { //Update to the current line. curLine++; var line = indexing[curLine]; //Get the node information. var layer = line.Length / 131; if (layer != 1) { if (line.Substring((layer - 2) * 131, 131).Substring(1, 64) != parent.NameSHA256) { continue; } } if (layer != curLayer + 1) { continue; } line = line.Substring((layer - 1) * 131, 131); var nameHash = line.Substring(1, 64); var contentHash = line.Substring(67, 64); if (contentHash == ObjectHelper.EmptyZeroes) { var tempNode = new IndexingNode(nameHash, parent); _allNodes.Add(tempNode); parent.AddChild(tempNode); CreateTreeFromIndexing(indexing, curLine, tempNode, layer); } else { var tempNode = new IndexingNode(nameHash, contentHash, parent); _allNodes.Add(tempNode); parent.AddChild(tempNode); } } }
//Step 2: Insert node only in Tree 2 without conflicts public static void Insert(IndexingNode curNode, IndexingNode insertNode) { IndexingNode parent = insertNode.GetParent(); if (parent == null) { throw new Exception(); } if (curNode.NameSHA256.Equals(parent.NameSHA256, StringComparison.InvariantCulture)) { IndexingNode temp = new IndexingNode(insertNode.NameSHA256, insertNode.ContentSHA256, curNode); foreach (IndexingNode n in curNode.Children) { //Same name, different content if (n.NameSHA256.Equals(insertNode.NameSHA256, StringComparison.InvariantCulture) && !n.ContentSHA256.Equals(insertNode.ContentSHA256, StringComparison.InvariantCulture)) { UpdateNodeIn1.Add(n); UpdateNodeIn2.Add(temp); return; } if (n.NameSHA256.Equals(insertNode.NameSHA256, StringComparison.InvariantCulture) && n.ContentSHA256.Equals(insertNode.ContentSHA256, StringComparison.InvariantCulture)) { return; } } curNode.AddChild(temp); ResultIndexingNodes.Add(temp); return; } foreach (IndexingNode n in curNode.Children) { Insert(n, insertNode); } }