using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;
namespace LibGit2Sharp
{
///
/// Holds the result of a diff between two trees.
/// Changes at the granularity of the file can be obtained through the different sub-collections , and .
/// To obtain the actual patch of the diff, use the class when calling Compare..
///
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class TreeChanges : IEnumerable
{
private readonly List changes = new List();
private readonly List added = new List();
private readonly List deleted = new List();
private readonly List modified = new List();
private readonly List typeChanged = new List();
private readonly List unmodified = new List();
private readonly List renamed = new List();
private readonly List copied = new List();
private readonly IDictionary> fileDispatcher = Build();
private static IDictionary> Build()
{
return new Dictionary>
{
{ ChangeKind.Modified, (de, d) => de.modified.Add(d) },
{ ChangeKind.Deleted, (de, d) => de.deleted.Add(d) },
{ ChangeKind.Added, (de, d) => de.added.Add(d) },
{ ChangeKind.TypeChanged, (de, d) => de.typeChanged.Add(d) },
{ ChangeKind.Unmodified, (de, d) => de.unmodified.Add(d) },
{ ChangeKind.Renamed, (de, d) => de.renamed.Add(d) },
{ ChangeKind.Copied, (de, d) => de.copied.Add(d) },
};
}
///
/// Needed for mocking purposes.
///
protected TreeChanges()
{ }
internal TreeChanges(DiffSafeHandle diff)
{
Proxy.git_diff_foreach(diff, FileCallback, null, null);
}
private int FileCallback(GitDiffDelta delta, float progress, IntPtr payload)
{
AddFileChange(delta);
return 0;
}
private void AddFileChange(GitDiffDelta delta)
{
var treeEntryChanges = new TreeEntryChanges(delta);
fileDispatcher[treeEntryChanges.Status](this, treeEntryChanges);
changes.Add(treeEntryChanges);
}
#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.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
///
/// List of that have been been added.
///
public virtual IEnumerable Added
{
get { return added; }
}
///
/// List of that have been deleted.
///
public virtual IEnumerable Deleted
{
get { return deleted; }
}
///
/// List of that have been modified.
///
public virtual IEnumerable Modified
{
get { return modified; }
}
///
/// List of which type have been changed.
///
public virtual IEnumerable TypeChanged
{
get { return typeChanged; }
}
///
/// List of which have been renamed
///
public virtual IEnumerable Renamed
{
get { return renamed; }
}
///
/// List of which have been copied
///
public virtual IEnumerable Copied
{
get { return copied; }
}
///
/// List of which are unmodified
///
public virtual IEnumerable Unmodified
{
get { return unmodified; }
}
private string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture,
"+{0} ~{1} -{2} \u00B1{3} R{4} C{5}",
Added.Count(), Modified.Count(), Deleted.Count(),
TypeChanged.Count(), Renamed.Count(), Copied.Count());
}
}
}
}