Esempio n. 1
0
        public void BuildDiffViewWithMargin(FileSet set)
        {
            var basePatch = FourWayDiff.MakePatch(set.Previous, set.Previous);

            var classifiedPatches = FourWayDiff.ClassifyPatches(
                set.Previous, basePatch,
                set.Current, set.ReviewPatch
                );

            var currentLines  = LineList.SplitLines(set.Current);
            var previousLines = LineList.SplitLines(set.Previous);

            DiffView.AssignPatchesToLines(classifiedPatches, currentLines, previousLines);

            List <Line> cleared = new List <Line>();

            DiffView.RemoveDiffsFromIdenticalLines(currentLines, previousLines, cleared);

            Console.WriteLine("Cleared:");
            foreach (var line in cleared)
            {
                Console.WriteLine(line);
            }

            DumpLines(currentLines, previousLines);

            var hunks = DiffView.BuildHunks(currentLines, previousLines, true).ToList();

            Assert.That(hunks, Has.Count.EqualTo(set.ExpectedPatchesWithMargin.Count), "Proper number of hunks generated");

            AssertHunks(hunks, previousLines, currentLines, set.ExpectedPatchesWithMargin);
        }
Esempio n. 2
0
            public async Task <Result> Execute(GetFileDiff query)
            {
                var previousCommit = query.Hashes.PreviousHead;
                var currentCommit  = query.Hashes.CurrentHead;

                var previousBaseCommit = query.Hashes.PreviousBase;
                var currentBaseCommit  = query.Hashes.CurrentBase;

                var contents = (await new[]
                {
                    new { Commit = previousCommit, Path = query.OldPath },
                    new { Commit = currentCommit, Path = query.NewPath },
                    new { Commit = previousBaseCommit, Path = query.OldPath },
                    new { Commit = currentBaseCommit, Path = query.OldPath }
                }
                                .DistinctBy(x => x.Commit)
                                .Select(async c => new
                {
                    File = c,
                    Content = (await _api.GetFileContent(query.ReviewId.ProjectId, c.Commit, c.Path)).DecodeString()
                })
                                .WhenAll())
                               .ToDictionary(x => x.File.Commit, x => x.Content);

                if (contents[previousCommit] == "" && query.OldPath != query.NewPath)
                {
                    contents[previousCommit] =
                        (await _api.GetFileContent(query.ReviewId.ProjectId, previousCommit, query.NewPath)).DecodeString();
                }

                foreach (var content in contents)
                {
                    if (IsBinaryFile(content.Value))
                    {
                        return(HandleBinaryFile(contents[previousCommit], contents[currentCommit], previousBaseCommit, currentBaseCommit, previousCommit, currentCommit));
                    }
                }

                var basePatch   = FourWayDiff.MakePatch(contents[previousBaseCommit], contents[currentBaseCommit]);
                var reviewPatch = FourWayDiff.MakePatch(contents[previousCommit], contents[currentCommit]);

                var classifiedPatches = FourWayDiff.ClassifyPatches(
                    contents[currentBaseCommit], basePatch,
                    contents[currentCommit], reviewPatch
                    );

                var previousLines = LineList.SplitLines(contents[previousCommit]);
                var currentLines  = LineList.SplitLines(contents[currentCommit]);

                DiffView.AssignPatchesToLines(classifiedPatches, currentLines, previousLines);

                List <Line> cleared = new List <Line>();

                DiffView.RemoveDiffsFromIdenticalLines(currentLines, previousLines, cleared);

                HunkInfo MakeHunkInfo(DiffView.HunkInfo hunk)
                {
                    var lines = new List <LineInfo>();

                    foreach (var(previous, current) in hunk.Lines)
                    {
                        if (previous != null && current != null)
                        {
                            lines.Add(new LineInfo
                            {
                                Text           = previous.Text,
                                Classification = previous.Classification.ToString(),
                                Operation      = previous.Diff?.Operation.ToString() ?? "Equal"
                            });

                            continue;
                        }

                        if (previous != null)
                        {
                            lines.Add(new LineInfo
                            {
                                Text           = previous.Text,
                                Classification = previous.Classification.ToString(),
                                Operation      = previous.Diff?.Operation.ToString() ?? "Equal"
                            });
                        }

                        if (current != null)
                        {
                            lines.Add(new LineInfo
                            {
                                Text           = current.Text,
                                Classification = current.Classification.ToString(),
                                Operation      = current.Diff?.Operation.ToString() ?? "Equal"
                            });
                        }
                    }

                    return(new HunkInfo
                    {
                        OldPosition = new PatchPosition
                        {
                            Start = hunk.StartPrevious,
                            End = hunk.EndPrevious,
                            Length = hunk.LengthPrevious
                        },
                        NewPosition = new PatchPosition
                        {
                            Start = hunk.StartCurrent,
                            End = hunk.EndCurrent,
                            Length = hunk.LengthCurrent
                        },
                        Lines = lines
                    });
                }

                var baseHunks = DiffView.BuildHunks(currentLines, previousLines, true);
                var hunks     = baseHunks.Select(MakeHunkInfo).ToList();

                return(new Result
                {
                    Commits = new ReviewDebugInfo
                    {
                        Review = new RevisionDebugInfo
                        {
                            Previous = previousCommit,
                            Current = currentCommit
                        },
                        Base = new RevisionDebugInfo
                        {
                            Previous = previousBaseCommit,
                            Current = currentBaseCommit
                        }
                    },

                    Contents = new ReviewDebugInfo
                    {
                        Review = new RevisionDebugInfo
                        {
                            Previous = contents[previousCommit],
                            PreviousTotalLines = previousLines.Count,
                            Current = contents[currentCommit],
                            CurrentTotalLines = currentLines.Count
                        },
                        Base = new RevisionDebugInfo
                        {
                            Previous = contents[previousBaseCommit],
                            Current = contents[currentBaseCommit]
                        }
                    },

                    Hunks = hunks
                });
            }
Esempio n. 3
0
        public void DiffViewIsCorrect(FileSet set)
        {
            var basePatch = FourWayDiff.MakePatch(set.Previous, set.Previous);

            var classifiedPatches = FourWayDiff.ClassifyPatches(
                set.Previous, basePatch,
                set.Current, set.ReviewPatch
                );

            var currentLines  = LineList.SplitLines(set.Current);
            var previousLines = LineList.SplitLines(set.Previous);

            DiffView.AssignPatchesToLines(classifiedPatches, currentLines, previousLines);

            List <Line> cleared = new List <Line>();

            DiffView.RemoveDiffsFromIdenticalLines(currentLines, previousLines, cleared);

            DumpLines(currentLines, previousLines);

            var previousIndex = 0;
            var currentIndex  = 0;

            while (previousIndex < previousLines.Count && currentIndex < currentLines.Count)
            {
                void NextPrevious()
                {
                    if (previousIndex < previousLines.Count)
                    {
                        previousIndex++;
                    }
                }

                void NextCurrent()
                {
                    if (currentIndex < currentLines.Count)
                    {
                        currentIndex++;
                    }
                }

                var previousLine = previousLines[previousIndex];
                var currentLine  = currentLines[currentIndex];

                if (!previousLine.IsNoChange)
                {
                    NextPrevious();
                }

                if (!currentLine.IsNoChange)
                {
                    NextCurrent();
                }

                if (previousLine.IsNoChange && currentLine.IsNoChange)
                {
                    Assert.That(previousLine.Text, Is.EqualTo(currentLine.Text), () =>
                    {
                        return($"Equal lines does not have the same text\nPrevious: {previousIndex,5} {previousLine}\nCurrent:  {currentIndex,5} {currentLine}");
                    });
                    NextPrevious();
                    NextCurrent();
                }
            }
        }