Esempio n. 1
0
        public void Execute()
        {
            var index   = 1;
            var commits = _Git.GetTotalCommits().Each(x => x.Index = index++);

            Print(commits);

            int n;

            while ((n = ReadIgnore(commits.Length)) != 0)
            {
                switch (n)
                {
                case -1:
                {
                    Print(commits);
                    break;
                }

                case -2:
                {
                    Console.WriteLine("your input is not valid.");
                    break;
                }

                default:
                {
                    index   = 1;
                    commits = commits.Remove(x => x.Index == n).Each(x => x.Index = index++);
                    Print(commits);
                    break;
                }
                }
            }

            var files = new File[0];

            index = 0;
            Console.Write($"\r1st phrase: {index++}/{commits.Length}");

            foreach (var commit in commits.OrderBy(x => x.Date))
            {
                var changes = _Git.GetCommitDetail(commit.Id);
                foreach (var change in changes)
                {
                    var exists = files.FirstOrDefault(x => x.Path.EqualsIgnoreCase(change.Path));

                    if (exists == null)
                    {
                        var file = new File()
                        {
                            InitialCommit = commit,
                            LatestCommit  = commit,
                            InitialAction = change.Action,
                            LatestAction  = change.Action,
                            Path          = change.Path,
                        };
                        files = files.Append(file);
                    }
                    else
                    {
                        exists.LatestCommit = commit;
                        exists.LatestAction = change.Action;
                    }
                }

                Console.Write($"\r1st phrase: {index++}/{commits.Length}");
            }

            // A -> A -> A: N/A
            // A -> A -> M: N/A
            // A -> A -> D: N/A
            // A -> M -> A: Compare
            // A -> M -> M: Compare
            // A -> M -> D: Delete
            // A -> D -> A: Compare
            // A -> D -> M: Compare
            // A -> D -> D: Delete
            // M -> A -> A: N/A
            // M -> A -> M: N/A
            // M -> A -> D: N/A
            // M -> M -> A: Compare
            // M -> M -> M: Compare
            // M -> M -> D: Delete
            // M -> D -> A: Compare
            // M -> D -> M: Compare
            // M -> D -> D: Delete
            // . -> A -> A: Add
            // . -> A -> M: Add
            // . -> A -> D: Ignore

            index = 0;
            ConsoleHelper.ClearCurrentLine();

            foreach (var file in files)
            {
                Console.Write($"\r2nd phrase: {index++}/{files.Length}");

                if ((file.InitialAction == "M" && file.LatestAction == "A") ||
                    (file.InitialAction == "M" && file.LatestAction == "M") ||
                    (file.InitialAction == "D" && file.LatestAction == "A") ||
                    (file.InitialAction == "D" && file.LatestAction == "M"))
                {
                    var commit = _Git.GetFileHistory(file.Path, file.InitialCommit);
                    if (commit != null)
                    {
                        file.HistoryCommit = commit;

                        var historyContent = _Git.GetFileContent(file.HistoryCommit.Id, file.Path);
                        var latestContent  = _Git.GetFileContent(file.LatestCommit.Id, file.Path);

                        if (historyContent == latestContent)
                        {
                            file.Mark = "I";
                            continue;
                        }
                    }

                    file.Mark = "M";
                    continue;
                }

                if ((file.InitialAction == "M" && file.LatestAction == "D") ||
                    (file.InitialAction == "D" && file.LatestAction == "D"))
                {
                    var commit = _Git.GetFileHistory(file.Path, file.InitialCommit);
                    if (commit != null)
                    {
                        file.HistoryCommit = commit;
                    }

                    file.Mark = "D";
                    continue;
                }

                if ((file.InitialAction == "A" && file.LatestAction == "A") ||
                    (file.InitialAction == "A" && file.LatestAction == "M"))
                {
                    file.Mark = "A";
                    continue;
                }

                if (file.InitialAction == "A" && file.LatestAction == "D")
                {
                    file.Mark = "I";
                    continue;
                }
            }

            ConsoleHelper.ClearCurrentLine();

            index = 1;
            var compares = files.Where(x => x.Mark != "I").OrderBy(x => x.Mark).ThenBy(x => x.Path).ToArray().Each(x => x.Index = index++);

            // print results
            Print(compares);

            // wait input from user
            while ((n = ReadCompare(compares.Length)) != 0)
            {
                switch (n)
                {
                case -1:
                {
                    Print(compares);
                    break;
                }

                case -2:
                {
                    Console.WriteLine("your input is not valid.");
                    break;
                }

                default:
                {
                    var compare = compares.FirstOrDefault(x => x.Index == n);
                    if (compare.Mark == "M" && compare.HistoryCommit != null)
                    {
                        Console.WriteLine($"compare {compare.HistoryCommit.Id} to {compare.LatestCommit.Id}");

                        if (!"./tempfiles".IsFolder())
                        {
                            Directory.CreateDirectory("./tempfiles".FullPath());
                        }

                        var history = $"./tempfiles/{compare.HistoryCommit.Id.Substring(0, 6)}-{compare.FileName}".FullPath();
                        using (var outputStream = new FileStream(history, FileMode.Create, FileAccess.Write))
                        {
                            using (var writer = new StreamWriter(outputStream))
                            {
                                writer.Write(_Git.GetFileContent(compare.HistoryCommit.Id, compare.Path));
                            }
                        }

                        var latest = $"./tempfiles/{compare.LatestCommit.Id.Substring(0, 6)}-{compare.FileName}".FullPath();
                        using (var outputStream = new FileStream(latest, FileMode.Create, FileAccess.Write))
                        {
                            using (var writer = new StreamWriter(outputStream))
                            {
                                writer.Write(_Git.GetFileContent(compare.LatestCommit.Id, compare.Path));
                            }
                        }

                        var config = DependencyContainer.Resolve <IConfig>();

                        new ProcessHelper(config.CompareTool).Add(config.CompareToolParams ?? string.Empty).Add($"\"{history}\"").Add($"\"{latest}\"").Execute();
                    }
                    else if (compare.Mark == "A")
                    {
                        Console.WriteLine($"{compare.Path} Added.");

                        if (!"./tempfiles".IsFolder())
                        {
                            Directory.CreateDirectory("./tempfiles".FullPath());
                        }

                        var history = "./tempfiles/empty".FullPath();
                        if (!history.IsFile())
                        {
                            using (var outputStream = new FileStream(history, FileMode.Create, FileAccess.Write)) { }
                        }

                        var latest = $"./tempfiles/{compare.LatestCommit.Id.Substring(0, 6)}-{compare.FileName}".FullPath();
                        using (var outputStream = new FileStream(latest, FileMode.Create, FileAccess.Write))
                        {
                            using (var writer = new StreamWriter(outputStream))
                            {
                                writer.Write(_Git.GetFileContent(compare.LatestCommit.Id, compare.Path));
                            }
                        }

                        var config = DependencyContainer.Resolve <IConfig>();

                        new ProcessHelper(config.CompareTool).Add(config.CompareToolParams ?? string.Empty).Add($"\"{history}\"").Add($"\"{latest}\"").Execute();
                    }
                    else if (compare.Mark == "D" && compare.HistoryCommit != null)
                    {
                        Console.WriteLine($"{compare.Path} Deleted.");

                        if (!"./tempfiles".IsFolder())
                        {
                            Directory.CreateDirectory("./tempfiles".FullPath());
                        }

                        var history = $"./tempfiles/{compare.HistoryCommit.Id.Substring(0, 6)}-{compare.FileName}".FullPath();
                        using (var outputStream = new FileStream(history, FileMode.Create, FileAccess.Write))
                        {
                            using (var writer = new StreamWriter(outputStream))
                            {
                                writer.Write(_Git.GetFileContent(compare.HistoryCommit.Id, compare.Path));
                            }
                        }

                        var latest = "./tempfiles/empty".FullPath();
                        if (!latest.IsFile())
                        {
                            using (var outputStream = new FileStream(latest, FileMode.Create, FileAccess.Write)) { }
                        }

                        var config = DependencyContainer.Resolve <IConfig>();

                        new ProcessHelper(config.CompareTool).Add(config.CompareToolParams ?? string.Empty).Add($"\"{history}\"").Add($"\"{latest}\"").Execute();
                    }

                    break;
                }
                }
            }
        }