Skip to content

added lines and deleted lines in content changes #1790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions LibGit2Sharp/ContentChanges.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Text;
Expand All @@ -22,6 +23,9 @@ protected ContentChanges()

internal unsafe ContentChanges(Repository repo, Blob oldBlob, Blob newBlob, GitDiffOptions options)
{
AddedLines = new List<Line>();
DeletedLines = new List<Line>();

Proxy.git_diff_blobs(repo.Handle,
oldBlob != null ? oldBlob.Id : null,
newBlob != null ? newBlob.Id : null,
Expand Down Expand Up @@ -51,6 +55,17 @@ internal void AppendToPatch(string patch)
/// </summary>
public virtual int LinesDeleted { get; internal set; }

/// <summary>
/// Lis of all lines added.
/// </summary>
public virtual List<Line> AddedLines { get; internal set; }

/// <summary>
/// List of all lines deleted.
/// </summary>
public virtual List<Line> DeletedLines { get; internal set; }


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the extra line

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still need to clean up this extra line break.

/// <summary>
/// The patch corresponding to these changes.
/// </summary>
Expand Down Expand Up @@ -95,11 +110,13 @@ private unsafe int LineCallback(git_diff_delta* delta, GitDiffHunk hunk, GitDiff
switch (line.lineOrigin)
{
case GitDiffLineOrigin.GIT_DIFF_LINE_ADDITION:
AddedLines.Add(new Line(line.NewLineNo, decodedContent));
LinesAdded++;
prefix = Encoding.ASCII.GetString(new[] { (byte)line.lineOrigin });
break;

case GitDiffLineOrigin.GIT_DIFF_LINE_DELETION:
DeletedLines.Add(new Line(line.OldLineNo, decodedContent));
LinesDeleted++;
prefix = Encoding.ASCII.GetString(new[] { (byte)line.lineOrigin });
break;
Expand Down
28 changes: 28 additions & 0 deletions LibGit2Sharp/Line.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace LibGit2Sharp
{
/// <summary>
/// Represents a Line with line number and content.
/// </summary>
public struct Line
{
/// <summary>
/// Points to the number of the original line in the blob
/// </summary>
public int LineNumber { get; }

/// <summary>
/// This content of the line in the original blob
/// </summary>
public String Content { get; }

internal Line(int lineNumber, string content)
{
LineNumber = lineNumber;
Content = content;
}
}
}
7 changes: 7 additions & 0 deletions LibGit2Sharp/Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ private unsafe int PrintCallBack(git_diff_delta* delta, GitDiffHunk hunk, GitDif
PatchEntryChanges currentChange = this[filePath];
string prefix = string.Empty;

string decodedContent = LaxUtf8Marshaler.FromNative(line.content, (int)line.contentLen);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this when this appears to be the same as the patchPart declaration on line 59?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should able to be removed. You can just use the existing patchPart instead.


currentChange.AddedLines = new List<Line>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these needed? Seems like this should be already handled in the ContentChanges constructor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added this in case the contructor that calls LineCallback on ContentChanges is not the constructor that is utilized by Patch. This way these values are always set. After fixing the declaration of the lists and adding tests. This does work. I would like to get your input on whether or not you think this is a viable option.

currentChange.DeletedLines = new List<Line>();

switch (line.lineOrigin)
{
case GitDiffLineOrigin.GIT_DIFF_LINE_CONTEXT:
Expand All @@ -77,12 +82,14 @@ private unsafe int PrintCallBack(git_diff_delta* delta, GitDiffHunk hunk, GitDif
case GitDiffLineOrigin.GIT_DIFF_LINE_ADDITION:
linesAdded++;
currentChange.LinesAdded++;
currentChange.AddedLines.Add(new Line(line.NewLineNo, decodedContent));
prefix = "+";
break;

case GitDiffLineOrigin.GIT_DIFF_LINE_DELETION:
linesDeleted++;
currentChange.LinesDeleted++;
currentChange.DeletedLines.Add(new Line(line.OldLineNo, decodedContent));
prefix = "-";
break;
}
Expand Down