/// <summary> /// Construct and write tree out of index. /// </summary> /// <returns> SHA-1 of the constructed tree</returns> /// <exception cref="IOException"></exception> public ObjectId writeTree() { CheckWriteOk(); var writer = new ObjectWriter(Repository); var current = new Tree(Repository); var trees = new Stack <Tree>(); trees.Push(current); var prevName = new string[0]; foreach (Entry e in _entries.Values) { if (e.Stage != STAGE_0) { continue; } string[] newName = SplitDirPath(e.Name); int c = LongestCommonPath(prevName, newName); while (c < trees.Count - 1) { current.Id = writer.WriteTree(current); trees.Pop(); current = trees.Count == 0 ? null : trees.Peek(); } while (trees.Count < newName.Length) { if (!current.ExistsTree(newName[trees.Count - 1])) { current = new Tree(current, Constants.encode(newName[trees.Count - 1])); current.Parent.AddEntry(current); trees.Push(current); } else { current = (Tree)current.findTreeMember(newName[trees.Count - 1]); trees.Push(current); } } var ne = new FileTreeEntry(current, e.ObjectId, Constants.encode(newName[newName.Length - 1]), (e.Mode & FileMode.ExecutableFile.Bits) == FileMode.ExecutableFile.Bits); current.AddEntry(ne); } while (trees.Count != 0) { current.Id = writer.WriteTree(current); trees.Pop(); if (trees.Count != 0) { current = trees.Peek(); } } return(current.TreeId); }