using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using LibGit2Sharp.Core;
namespace LibGit2Sharp
{
///
/// The collection of s in a
/// index due to a
/// previously performed merge operation.
///
public class ConflictCollection : IEnumerable
{
private readonly Repository repo;
///
/// Needed for mocking purposes.
///
protected ConflictCollection()
{ }
internal ConflictCollection(Repository repo)
{
this.repo = repo;
}
///
/// Gets the for the
/// specified relative path.
///
/// The relative path to query
/// A that represents the conflict for this file.
public virtual Conflict this[string path]
{
get
{
return Proxy.git_index_conflict_get(repo.Index.Handle, repo, path);
}
}
#region IEnumerable Members
private List AllConflicts()
{
var list = new List();
IndexEntry ancestor = null, ours = null, theirs = null;
string currentPath = null;
foreach (IndexEntry entry in repo.Index)
{
if (entry.StageLevel == StageLevel.Staged)
{
continue;
}
if (currentPath != null && !entry.Path.Equals(currentPath, StringComparison.Ordinal))
{
list.Add(new Conflict(ancestor, ours, theirs));
ancestor = null;
ours = null;
theirs = null;
}
currentPath = entry.Path;
switch (entry.StageLevel)
{
case StageLevel.Ancestor:
ancestor = entry;
break;
case StageLevel.Ours:
ours = entry;
break;
case StageLevel.Theirs:
theirs = entry;
break;
default:
throw new InvalidOperationException(string.Format(
CultureInfo.InvariantCulture,
"Entry '{0}' bears an unexpected StageLevel '{1}'",
entry.Path, entry.StageLevel));
}
}
if (currentPath != null)
{
list.Add(new Conflict(ancestor, ours, theirs));
}
return list;
}
///
/// Returns an enumerator that iterates through the collection.
///
/// An object that can be used to iterate through the collection.
public virtual IEnumerator GetEnumerator()
{
return AllConflicts().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
}
}