-
Notifications
You must be signed in to change notification settings - Fork 892
/
Copy pathContentChanges.cs
145 lines (120 loc) · 4.51 KB
/
ContentChanges.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using LibGit2Sharp.Core;
namespace LibGit2Sharp
{
/// <summary>
/// Holds the changes between two <see cref="Blob"/>s.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class ContentChanges
{
private readonly StringBuilder patchBuilder = new StringBuilder();
/// <summary>
/// Needed for mocking purposes.
/// </summary>
protected ContentChanges()
{ }
internal unsafe ContentChanges(Repository repo, Blob oldBlob, Blob newBlob, GitDiffOptions options)
{
Proxy.git_diff_blobs(repo.Handle,
oldBlob != null ? oldBlob.Id : null,
newBlob != null ? newBlob.Id : null,
options,
FileCallback,
HunkCallback,
LineCallback);
}
internal ContentChanges(bool isBinaryComparison)
{
this.IsBinaryComparison = isBinaryComparison;
}
internal void AppendToPatch(string patch)
{
patchBuilder.Append(patch);
}
/// <summary>
/// The number of lines added.
/// </summary>
public virtual int LinesAdded { get; internal set; }
/// <summary>
/// The number of lines deleted.
/// </summary>
public virtual int LinesDeleted { get; internal set; }
/// <summary>
/// The list of added lines.
/// </summary>
public virtual List<Line> AddedLines { get; } = new List<Line>();
/// <summary>
/// The list of deleted lines.
/// </summary>
public virtual List<Line> DeletedLines { get; } = new List<Line>();
/// <summary>
/// The patch corresponding to these changes.
/// </summary>
public virtual string Patch
{
get { return patchBuilder.ToString(); }
}
/// <summary>
/// Determines if at least one side of the comparison holds binary content.
/// </summary>
public virtual bool IsBinaryComparison { get; private set; }
private unsafe int FileCallback(git_diff_delta* delta, float progress, IntPtr payload)
{
IsBinaryComparison = delta->flags.HasFlag(GitDiffFlags.GIT_DIFF_FLAG_BINARY);
if (!IsBinaryComparison)
{
return 0;
}
AppendToPatch("Binary content differ\n");
return 0;
}
private unsafe int HunkCallback(git_diff_delta* delta, GitDiffHunk hunk, IntPtr payload)
{
string decodedContent = LaxUtf8Marshaler.FromBuffer(hunk.Header, (int)hunk.HeaderLen);
AppendToPatch(decodedContent);
return 0;
}
private unsafe int LineCallback(git_diff_delta* delta, GitDiffHunk hunk, GitDiffLine line, IntPtr payload)
{
string decodedContent = LaxUtf8Marshaler.FromNative(line.content, (int)line.contentLen);
string prefix;
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;
case GitDiffLineOrigin.GIT_DIFF_LINE_CONTEXT:
prefix = Encoding.ASCII.GetString(new[] { (byte)line.lineOrigin });
break;
default:
prefix = string.Empty;
break;
}
AppendToPatch(prefix);
AppendToPatch(decodedContent);
return 0;
}
private string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture,
@"{{+{0}, -{1}}}",
LinesAdded,
LinesDeleted);
}
}
}
}