public void When_Check_Branch_Is_Called_A_Comparison_With_Result_Yes_Always_Returns_Yes_Otherwise_The_Result_Status_Is_That_Of_The_Last_Comparison(ResultStatus status1, ResultStatus status2, ResultStatus expectedStatus)
        {
            /* Setup */
            _mockBranchComparer
            .Setup(m => m.Compare(_mainBranch, _mainBranch))
            .Returns(new ComparerResult {
                Status = status1
            });
            _mockBranchComparer
            .Setup(m => m.Compare(_mainBranch, _refBranch))
            .Returns(new ComparerResult {
                Status = status2
            });
            var selector = new ComparisonSelector(_mockBranchComparer.Object);

            /* Test */
            var result = selector.CheckBranch(_mainBranch, new List <Branch> {
                _mainBranch, _refBranch
            });

            /* Assert */
            Assert.That(result.Status, Is.EqualTo(expectedStatus));
        }
        public void When_Check_Branch_Is_Called_With_A_Branch_That_Has_Fewer_Commits_With_The_Same_Commit_Ids_As_Those_Of_Another_Branch_Then_The_Main_Branch_Should_Be_Deletable()
        {
            /* Setup */
            // First commit
            File.WriteAllText(Path.Combine(_repoPath, "file1.txt"), "File1's content...");
            _repo.Stage("file1.txt");
            _repo.Commit("First commit...");

            // Checkout new branch
            _repo.CreateBranch("newBranch1");
            _repo.Checkout("newBranch1");

            // Second commit
            File.WriteAllText(Path.Combine(_repoPath, "file2.txt"), "File2's content...");
            _repo.Stage("file2.txt");
            _repo.Commit("Second commit...");

            // Checkout new branch
            _repo.CreateBranch("newBranch2");
            _repo.Checkout("newBranch2");

            // Second commit
            File.WriteAllText(Path.Combine(_repoPath, "file3.txt"), "File3's content...");
            _repo.Stage("file3.txt");
            _repo.Commit("Third commit...");

            // Prepare for comparison
            var mainBranch = _repo.Branches["newBranch1"];
            var branches   = _repo.Branches;

            /* Test */
            var result = _selector.CheckBranch(new Gitcomparer.Core.Model.Branch(mainBranch), branches.Select(b => new Gitcomparer.Core.Model.Branch(b)).ToList());

            /* Assert */
            Assert.That(result.Status, Is.EqualTo(ResultStatus.Yes));
        }
Пример #3
0
        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();
        }