-
Notifications
You must be signed in to change notification settings - Fork 894
/
Copy pathPatchStats.cs
142 lines (127 loc) · 4.75 KB
/
PatchStats.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;
namespace LibGit2Sharp
{
/// <summary>
/// Holds summary information for a diff.
/// <para>The individual patches for each file can be accessed through the indexer of this class.</para>
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class PatchStats : IEnumerable<ContentChangeStats>, IDiffResult
{
private readonly IDictionary<FilePath, ContentChangeStats> changes = new Dictionary<FilePath, ContentChangeStats>();
private readonly int totalLinesAdded;
private readonly int totalLinesDeleted;
/// <summary>
/// For mocking.
/// </summary>
protected PatchStats()
{ }
internal unsafe PatchStats(DiffHandle diff)
{
using (diff)
{
int count = Proxy.git_diff_num_deltas(diff);
for (int i = 0; i < count; i++)
{
using (var patch = Proxy.git_patch_from_diff(diff, i))
{
var delta = Proxy.git_diff_get_delta(diff, i);
var pathPtr = delta->new_file.Path != null ? delta->new_file.Path : delta->old_file.Path;
var newFilePath = LaxFilePathMarshaler.FromNative(pathPtr);
var stats = Proxy.git_patch_line_stats(patch);
int added = stats.Item1;
int deleted = stats.Item2;
changes.Add(newFilePath, new ContentChangeStats(added, deleted));
totalLinesAdded += added;
totalLinesDeleted += deleted;
}
}
}
}
#region IEnumerable<ContentChanges> Members
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>An <see cref="IEnumerator{T}"/> object that can be used to iterate through the collection.</returns>
public virtual IEnumerator<ContentChangeStats> GetEnumerator()
{
return changes.Values.GetEnumerator();
}
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>An <see cref="IEnumerator"/> object that can be used to iterate through the collection.</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
/// <summary>
/// Gets the <see cref="ContentChangeStats"/> corresponding to the specified <paramref name="path"/>.
/// </summary>
/// <param name="path"></param>
public virtual ContentChangeStats this[string path]
{
get { return this[(FilePath)path]; }
}
private ContentChangeStats this[FilePath path]
{
get
{
ContentChangeStats stats;
if (changes.TryGetValue(path, out stats))
{
return stats;
}
return null;
}
}
/// <summary>
/// The total number of lines added in this diff.
/// </summary>
public virtual int TotalLinesAdded
{
get { return totalLinesAdded; }
}
/// <summary>
/// The total number of lines deleted in this diff.
/// </summary>
public virtual int TotalLinesDeleted
{
get { return totalLinesDeleted; }
}
private string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture,
"+{0} -{1}",
TotalLinesAdded,
TotalLinesDeleted);
}
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
// This doesn't do anything yet because it loads everything
// eagerly and disposes of the diff handle in the constructor.
}
}
}