using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;
namespace LibGit2Sharp
{
///
/// Holds summary information for a diff.
/// The individual patches for each file can be accessed through the indexer of this class.
///
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class PatchStats : IEnumerable
{
private readonly IDictionary changes = new Dictionary();
private readonly int totalLinesAdded;
private readonly int totalLinesDeleted;
///
/// For mocking.
///
protected PatchStats()
{ }
internal PatchStats(DiffSafeHandle 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.NewFile.Path != IntPtr.Zero ? delta.NewFile.Path : delta.OldFile.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 Members
///
/// Returns an enumerator that iterates through the collection.
///
/// An object that can be used to iterate through the collection.
public virtual IEnumerator GetEnumerator()
{
return changes.Values.GetEnumerator();
}
///
/// Returns an enumerator that iterates through the collection.
///
/// An object that can be used to iterate through the collection.
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
///
/// Gets the corresponding to the specified .
///
///
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;
}
}
///
/// The total number of lines added in this diff.
///
public virtual int TotalLinesAdded
{
get { return totalLinesAdded; }
}
///
/// The total number of lines deleted in this diff.
///
public virtual int TotalLinesDeleted
{
get { return totalLinesDeleted; }
}
private string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture, "+{0} -{1}",
TotalLinesAdded, TotalLinesDeleted);
}
}
}
}