private void AddBranches( IBranchesKnowledge branchesKnowledge, ICommitsData commitsData, IBranchPickingOptions branchPickingOptions, ITree tree) { // Branches in the writing order. IBranch[] branchesOrdered = branchesKnowledge.EnumerateBranchesInLogicalOrder().ToArray(); IBranchPicker branchPicker = _branchPickerFactory.CreateBranchPicker(branchPickingOptions); // Filter them so that we get only those we want to write. IBranch[] branchesFiltered = branchesOrdered.Where(b => branchPicker.ShouldBePicked(b.Label)).ToArray(); var commitsSet = new HashSet <ICommit>(commitsData); foreach (IBranch b in branchesFiltered) { ICommit tip = commitsData.GetByHash(b.Tip); var hashesInBranch = new List <IHash>(commitsSet.Count / 3); IEnumerable <ICommit> upTheTree = commitsData.EnumerateUpTheHistoryFrom(tip); foreach (ICommit commit in upTheTree) { if (!commitsSet.Contains(commit)) { break; } hashesInBranch.Add(commit.Hash); commitsSet.Remove(commit); } hashesInBranch.Reverse(); tree.AddBranch(b, hashesInBranch); } }