/// <summary> /// Sort the sub-branches and their descendents so they are ordered according /// to the given comparer. /// </summary> /// <param name="comparer">The comparer that orders the branches</param> public void Sort(BranchComparer comparer) { if (comparer == null || this.ChildBranches.Count == 0) { return; } // We're about to sort the children, so clear the last child flag this.ChildBranches[this.ChildBranches.Count - 1].IsLastChild = false; this.ChildBranches.Sort(comparer); this.ChildBranches[this.ChildBranches.Count - 1].IsLastChild = true; foreach (Branch br in this.ChildBranches) { br.Sort(comparer); } }
public void When_The_Main_Branch_And_The_Reference_Branch_Have_The_Same_Name_They_Are_The_Same_Branch_And_Thus_The_Main_Branch_Should_Not_Be_Deletable() { /* Setup */ var mainBranch = _mainBranch; var refBranch = _refBranch; refBranch.Name = _mainBranch.Name; var branchComparer = new BranchComparer(new List <ICommitComparer> { _mockComparer1.Object, _mockComparer2.Object }); /* Test */ var result = branchComparer.Compare(_mainBranch, _refBranch); /* Assert */ Assert.That(result.Status, Is.EqualTo(ResultStatus.No)); }
public void When_A_Comparer_Returns_A_Result_With_Status_Yes_Or_No_The_Subsequent_Comparer_Should_Never_Be_Called_Otherwise_Both_Comparers_Should_Be_Called_Once(ResultStatus status1, ResultStatus status2, int expectedNr1, int expectedNr2) { /* Setup */ _mockComparer1 .Setup(m => m.Compare(_mainBranch, _refBranch)) .Returns(new ComparerResult { Status = status1 }); _mockComparer2 .Setup(m => m.Compare(_mainBranch, _refBranch)) .Returns(new ComparerResult { Status = status2 }); var comparers = new List <ICommitComparer> { _mockComparer1.Object, _mockComparer2.Object }; var branchComparer = new BranchComparer(comparers); /* Test */ var result = branchComparer.Compare(_mainBranch, _refBranch); /* Assert */ if (expectedNr1 == 0) { _mockComparer1.Verify(m => m.Compare(_mainBranch, _refBranch), Times.Never); } else if (expectedNr1 == 1) { _mockComparer1.Verify(m => m.Compare(_mainBranch, _refBranch), Times.Once); } if (expectedNr2 == 0) { _mockComparer2.Verify(m => m.Compare(_mainBranch, _refBranch), Times.Never); } else if (expectedNr2 == 1) { _mockComparer2.Verify(m => m.Compare(_mainBranch, _refBranch), Times.Once); } }
public void When_A_Comparer_Returns_A_Result_With_Status_Yes_Or_No_This_Should_Be_The_Result_Status_Of_The_Compare_Otherwise_The_Result_Status_Should_Be_That_Of_The_Subsequent_Compare(ResultStatus status1, ResultStatus status2, ResultStatus expectedStatus) { /* Setup */ _mockComparer1 .Setup(m => m.Compare(_mainBranch, _refBranch)) .Returns(new ComparerResult { Status = status1 }); _mockComparer2 .Setup(m => m.Compare(_mainBranch, _refBranch)) .Returns(new ComparerResult { Status = status2 }); var comparers = new List <ICommitComparer> { _mockComparer1.Object, _mockComparer2.Object }; var branchComparer = new BranchComparer(comparers); /* Test */ var result = branchComparer.Compare(_mainBranch, _refBranch); /* Assert */ Assert.That(result.Status, Is.EqualTo(expectedStatus)); }
static void Main(string[] args) { Log.Logger = new LoggerConfiguration().MinimumLevel.Debug().WriteTo.RollingFile(@"C:\\Documents\\{Date}.txt").CreateLogger(); // Gets directories at basepath var basepath = ConfigurationManager.AppSettings["basePath"]; var repoFolders = Directory.GetDirectories(basepath).Select(r => new DirectoryInfo(r).Name).ToArray(); // Asks user to select repository from directories string selectedRepo; ConsoleTools.Select("Select repo", repoFolders, out selectedRepo); selectedRepo = basepath + "\\" + selectedRepo; // Sets selected repository Repository repo = null; try { // Gets selected repo (as LibGit2Sharp.Repository) with submitted path repo = new Repository(selectedRepo); } catch (RepositoryNotFoundException ex) { Log.Information(ex, ex.ToString(), true); return; } // Gets selected repo's branches (as Model.Branch) var branches = repo.Branches.Where(b => !b.IsRemote).Select(b => new Branch(b)).ToList(); var branchNames = branches.Select(b => b.Name).ToArray(); if (branches.Count >= 2) { // Asks user to select which comparison to run Enum compChoice; ConsoleTools.SelectEnum("Select comparison", CompChoices, out compChoice); // Starts comparison with list of branch comparers var branchComparer = new BranchComparer(BranchComparer.CommitComparers); var comparisonHelper = new ComparisonSelector(branchComparer); // If CheckAll if (compChoice.Equals(ComparisonOptions.CheckAll)) { // Runs comparison and gets the deletable branches var deletableBranches = comparisonHelper.CheckAllBranches(branches).Where(b => b.Status.Equals(ResultStatus.Yes)).Select(b => b.MainBranch); // Prints result message according to result status if (deletableBranches.Any()) { foreach (var branch in deletableBranches) { Console.WriteLine("It's safe to remove " + branch.Name + "."); } } else { Console.WriteLine("It's not safe to remove any of this repository's branches."); } } else { // Asks user to select and sets main branch string mainBranchName; ConsoleTools.Select("Select the branch of interest", branchNames, out mainBranchName); var mainBranch = new Branch(); foreach (var branch in branches) { if (branch.Name.Equals(mainBranchName)) { mainBranch = branch; } } ComparerResult compResult; // If CheckOne if (compChoice.Equals(ComparisonOptions.CheckOne)) { compResult = comparisonHelper.CheckBranch(mainBranch, branches); // Prints result message according to result status if (compResult.Status.Equals(ResultStatus.Yes)) { Console.WriteLine("It's safe to delete the branch " + mainBranchName + "."); } else { Console.WriteLine("It's not safe to delete the branch " + mainBranchName + "."); } } // If CompareTwo else { // Asks user to select reference branch string refBranchName; ConsoleTools.Select("Compare " + mainBranchName + " with reference branch", branchNames.Where(b => b != mainBranch.Name).ToArray(), out refBranchName); Branch refBranch = new Branch(); foreach (var branch in branches) { if (branch.Name.Equals(refBranchName)) { refBranch = branch; } } compResult = comparisonHelper.CompareBranches(mainBranch, refBranch); // Prints result message according to result status if (compResult.Status.Equals(ResultStatus.Yes)) { Console.WriteLine("It's safe to delete the branch " + mainBranchName + "."); } else { Console.WriteLine("It's not safe to delete the branch " + mainBranchName + "."); } } } } else { Console.WriteLine("This repository only contains 1 local branch and thus no comparison can be made."); } // TEST Console.ReadLine(); }