Exemple #1
0
 Comment CommentLine(FileDiff fileDiff, LineDiff lineDiff, string user, string comment)
 {
     return(new Comment()
     {
         File = fileDiff.Name, LineId = lineDiff.Id, User = user, Text = comment
     });
 }
        private static List <FileDiff> ParseFileDiffs(IRestResponse <EnterpriseDiffResponse> response)
        {
            var fileDiffs = new List <FileDiff>();

            foreach (var diff in response.Data.Diffs)
            {
                var fileDiff = new FileDiff
                {
                    From   = diff.Source?.String,
                    To     = diff.Destination?.String,
                    Chunks = new List <ChunkDiff>(),
                };

                fileDiff.Type = fileDiff.From == null
                    ? FileChangeType.Add
                    : fileDiff.To == null ? FileChangeType.Delete : FileChangeType.Modified;

                fileDiffs.Add(fileDiff);

                foreach (var diffHunk in diff.Hunks ?? new List <Hunk>())
                {
                    var chunkDiff = new ChunkDiff()
                    {
                        Changes = new List <LineDiff>(),
                    };
                    fileDiff.Chunks.Add(chunkDiff);

                    foreach (var segment in diffHunk.Segments)
                    {
                        foreach (var line in segment.Lines)
                        {
                            var ld = new LineDiff()
                            {
                                Content  = line.Text,
                                OldIndex = segment.Type == "ADDED" ? null : (int?)line.Source,
                                NewIndex = segment.Type == "REMOVED" ? null : (int?)line.Destination,
                                Type     = segment.Type == "ADDED"
                                    ? LineChangeType.Add
                                    : segment.Type == "REMOVED"
                                        ? LineChangeType.Delete
                                        : LineChangeType.Normal
                            };

                            chunkDiff.Changes.Add(ld);
                        }
                    }
                }


                fileDiff.Additions = fileDiff.Chunks.Sum(y => y.Changes.Count(z => z.Add));
                fileDiff.Deletions = fileDiff.Chunks.Sum(y => y.Changes.Count(z => z.Delete));
            }
            return(fileDiffs);
        }
Exemple #3
0
 private string WriteLine(LineDiff arg)
 {
     switch (arg.Type)
     {
         case LineDiffType.Added:
             return "+" + arg.Content;
         case LineDiffType.Removed:
             return "-" + arg.Content;
         default:
             return " " + arg.Content;
     }
 }
Exemple #4
0
        public LineDiff GetLineDiff(string text1, string text2)
        {
            if (text1 == text2)
            {
                return(new LineDiff(Operation.Equal, text1));
            }

            var interlineDiffs = Diff(text1, text2);

            SemanticCleanup(interlineDiffs);
            var diff = new LineDiff(Operation.Modify);

            foreach (var interlineDiff in interlineDiffs)
            {
                diff.Add(interlineDiff);
            }

            return(diff);
        }
Exemple #5
0
        internal static FileDiff ParseDiffChunk(IStringReader reader, ref ChangeSetDetail merge)
        {
            var diff = ParseDiffHeader(reader, merge);

            if (diff == null)
            {
                return null;
            }

            // Current diff range
            DiffRange currentRange = null;
            int? leftCounter = null;
            int? rightCounter = null;

            // Parse the file diff
            while (!reader.Done)
            {
                int? currentLeft = null;
                int? currentRight = null;
                string line = reader.ReadLine();

                if (line.Equals(@"\ No newline at end of file", StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                bool isDiffRange = line.StartsWith("@@", StringComparison.Ordinal);
                ChangeType? changeType = null;

                if (line.StartsWith("+", StringComparison.Ordinal))
                {
                    changeType = ChangeType.Added;
                    currentRight = ++rightCounter;
                    currentLeft = null;
                }
                else if (line.StartsWith("-", StringComparison.Ordinal))
                {
                    changeType = ChangeType.Deleted;
                    currentLeft = ++leftCounter;
                    currentRight = null;
                }
                else if (IsCommitHeader(line))
                {
                    reader.PutBack(line.Length);
                    merge = ParseCommitAndSummary(reader);
                }
                else
                {
                    if (!isDiffRange)
                    {
                        currentLeft = ++leftCounter;
                        currentRight = ++rightCounter;
                    }
                    changeType = ChangeType.None;
                }

                if (changeType != null)
                {
                    var lineDiff = new LineDiff(changeType.Value, line);
                    if (!isDiffRange)
                    {
                        lineDiff.LeftLine = currentLeft;
                        lineDiff.RightLine = currentRight;
                    }

                    diff.Lines.Add(lineDiff);
                }

                if (isDiffRange)
                {
                    // Parse the new diff range
                    currentRange = DiffRange.Parse(line.AsReader());
                    leftCounter = currentRange.LeftFrom - 1;
                    rightCounter = currentRange.RightFrom - 1;
                }
            }

            return diff;
        }
Exemple #6
0
        internal static FileDiff ParseDiffChunk(IStringReader reader, ref ChangeSetDetail merge)
        {
            var diff = ParseDiffHeader(reader, merge);

            if (diff == null)
            {
                return(null);
            }

            // Current diff range
            DiffRange currentRange = null;
            int?      leftCounter  = null;
            int?      rightCounter = null;

            // Parse the file diff
            while (!reader.Done)
            {
                int?   currentLeft  = null;
                int?   currentRight = null;
                string line         = reader.ReadLine();

                if (line.Equals(@"\ No newline at end of file", StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                bool       isDiffRange = line.StartsWith("@@", StringComparison.Ordinal);
                ChangeType?changeType  = null;

                if (line.StartsWith("+", StringComparison.Ordinal))
                {
                    changeType   = ChangeType.Added;
                    currentRight = ++rightCounter;
                    currentLeft  = null;
                }
                else if (line.StartsWith("-", StringComparison.Ordinal))
                {
                    changeType   = ChangeType.Deleted;
                    currentLeft  = ++leftCounter;
                    currentRight = null;
                }
                else if (IsCommitHeader(line))
                {
                    reader.PutBack(line.Length);
                    merge = ParseCommitAndSummary(reader);
                }
                else
                {
                    if (!isDiffRange)
                    {
                        currentLeft  = ++leftCounter;
                        currentRight = ++rightCounter;
                    }
                    changeType = ChangeType.None;
                }

                if (changeType != null)
                {
                    var lineDiff = new LineDiff(changeType.Value, line);
                    if (!isDiffRange)
                    {
                        lineDiff.LeftLine  = currentLeft;
                        lineDiff.RightLine = currentRight;
                    }

                    diff.Lines.Add(lineDiff);
                }

                if (isDiffRange)
                {
                    // Parse the new diff range
                    currentRange = DiffRange.Parse(line.AsReader());
                    leftCounter  = currentRange.LeftFrom - 1;
                    rightCounter = currentRange.RightFrom - 1;
                }
            }

            return(diff);
        }
Exemple #7
0
 private void button1_Click(object sender, EventArgs e)
 {
     Collection<MultiLineDiff> result = new LineDiff().Diff(textBoxOriginal.Text, textBoxNew.Text);
 }
Exemple #8
0
        private static LineDiff ReadLine(LineReader reader)
        {
            while (!reader.EndOfFile && String.IsNullOrWhiteSpace(reader.Current))
            {
                reader.NextLine();
            }
            if (reader.EndOfFile) { return null; }

            LineDiffType type;
            char typeChar = reader.Current[0];
            switch (typeChar)
            {
                case '+':
                    type = LineDiffType.Added;
                    break;
                case '-':
                    type = LineDiffType.Removed;
                    break;
                case ' ':
                    type = LineDiffType.Same;
                    break;
                default:
                    // Start of next hunk or file
                    return null;
            }
            LineDiff line = new LineDiff(type, reader.Current.Substring(1).TrimEnd('\n', '\r'));
            reader.NextLine();
            return line;
        }
Exemple #9
0
 static bool  MatchesLine(IAnnotation annotation, FileDiff fileDiff, LineDiff lineDiff)
 {
     return(annotation.File == fileDiff.Name && annotation.LineId == lineDiff.Id);
 }
Exemple #10
0
 public void ClearDiff()
 {
     Classification = DiffClassification.Unchanged;
     Patch          = null;
     Diff           = null;
 }
Exemple #11
0
 public void AssignDiff(DiffClassification classification, LinePatch patch, LineDiff diff)
 {
     Classification = classification;
     Patch          = patch;
     Diff           = diff;
 }