Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'LibGit2Sharp/ContentChanges.cs')
-rw-r--r--LibGit2Sharp/ContentChanges.cs57
1 files changed, 56 insertions, 1 deletions
diff --git a/LibGit2Sharp/ContentChanges.cs b/LibGit2Sharp/ContentChanges.cs
index cd16a99c..23a90c56 100644
--- a/LibGit2Sharp/ContentChanges.cs
+++ b/LibGit2Sharp/ContentChanges.cs
@@ -1,4 +1,6 @@
-using System.Text;
+using System;
+using System.Text;
+using LibGit2Sharp.Core;
namespace LibGit2Sharp
{
@@ -13,6 +15,59 @@ namespace LibGit2Sharp
{
}
+ internal ContentChanges(Repository repo, Blob oldBlob, Blob newBlob, GitDiffOptions options)
+ {
+ using (var osw1 = new ObjectSafeWrapper(oldBlob.Id, repo))
+ using (var osw2 = new ObjectSafeWrapper(newBlob.Id, repo))
+ {
+ Ensure.Success(NativeMethods.git_diff_blobs(osw1.ObjectPtr, osw2.ObjectPtr, options, IntPtr.Zero, null, HunkCallback, LineCallback));
+ }
+ }
+
+ private static string NativeToString(IntPtr content, IntPtr contentlen)
+ {
+ return ((Utf8Marshaler)(Utf8Marshaler.GetInstance(string.Empty))).NativeToString(content, contentlen.ToInt32());
+ }
+
+ private int HunkCallback(IntPtr data, GitDiffDelta delta, GitDiffRange range, IntPtr header, IntPtr headerlen)
+ {
+ string decodedContent = NativeToString(header, headerlen);
+
+ PatchBuilder.AppendFormat("{0}", decodedContent);
+ return 0;
+ }
+
+ private int LineCallback(IntPtr data, GitDiffDelta delta, GitDiffRange range, GitDiffLineOrigin lineorigin, IntPtr content, IntPtr contentlen)
+ {
+ string decodedContent = NativeToString(content, contentlen);
+
+ string prefix;
+
+ switch (lineorigin)
+ {
+ case GitDiffLineOrigin.GIT_DIFF_LINE_ADDITION:
+ LinesAdded++;
+ prefix = Encoding.ASCII.GetString(new[] { (byte)lineorigin });
+ break;
+
+ case GitDiffLineOrigin.GIT_DIFF_LINE_DELETION:
+ LinesDeleted++;
+ prefix = Encoding.ASCII.GetString(new[] { (byte)lineorigin });
+ break;
+
+ case GitDiffLineOrigin.GIT_DIFF_LINE_CONTEXT:
+ prefix = Encoding.ASCII.GetString(new[] { (byte)lineorigin });
+ break;
+
+ default:
+ prefix = string.Empty;
+ break;
+ }
+
+ PatchBuilder.AppendFormat("{0}{1}", prefix, decodedContent);
+ return 0;
+ }
+
/// <summary>
/// The number of lines added.
/// </summary>