using System;
using System.Collections;
using System.Collections.Generic;
using LibGit2Sharp.Core;
namespace LibGit2Sharp
{
///
/// Holds the result of the determination of the state of the working directory.
/// Only files that differ from the current index and/or commit will be considered.
///
public class RepositoryStatus : IEnumerable
{
private readonly List statusEntries = new List();
private readonly List added = new List();
private readonly List staged = new List();
private readonly List removed = new List();
private readonly List missing = new List();
private readonly List modified = new List();
private readonly List untracked = new List();
private readonly List ignored = new List();
private readonly bool isDirty;
private readonly IDictionary> dispatcher = Build();
private static IDictionary> Build()
{
return new Dictionary>
{
{ FileStatus.Untracked, (rs, s) => rs.untracked.Add(s) },
{ FileStatus.Modified, (rs, s) => rs.modified.Add(s) },
{ FileStatus.Missing, (rs, s) => rs.missing.Add(s) },
{ FileStatus.Added, (rs, s) => rs.added.Add(s) },
{ FileStatus.Staged, (rs, s) => rs.staged.Add(s) },
{ FileStatus.Removed, (rs, s) => rs.removed.Add(s) },
{ FileStatus.Ignored, (rs, s) => rs.ignored.Add(s) },
};
}
internal RepositoryStatus(Repository repo)
{
Ensure.Success(NativeMethods.git_status_foreach(repo.Handle, StateChanged, IntPtr.Zero));
isDirty = statusEntries.Count != 0;
}
private int StateChanged(FilePath filePath, uint state, IntPtr payload)
{
var gitStatus = (FileStatus)state;
statusEntries.Add(new StatusEntry(filePath.Native, gitStatus));
foreach (KeyValuePair> kvp in dispatcher)
{
if (!gitStatus.Has(kvp.Key))
{
continue;
}
kvp.Value(this, filePath.Native);
}
return 0;
}
///
/// Returns an enumerator that iterates through the collection.
///
/// An object that can be used to iterate through the collection.
public IEnumerator GetEnumerator()
{
return statusEntries.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();
}
///
/// List of files added to the index, which are not in the current commit
///
public IEnumerable Added
{
get { return added; }
}
///
/// List of files added to the index, which are already in the current commit with different content
///
public IEnumerable Staged
{
get { return staged; }
}
///
/// List of files removed from the index but are existent in the current commit
///
public IEnumerable Removed
{
get { return removed; }
}
///
/// List of files existent in the index but are missing in the working directory
///
public IEnumerable Missing
{
get { return missing; }
}
///
/// List of files with unstaged modifications. A file may be modified and staged at the same time if it has been modified after adding.
///
public IEnumerable Modified
{
get { return modified; }
}
///
/// List of files existing in the working directory but are neither tracked in the index nor in the current commit.
///
public IEnumerable Untracked
{
get { return untracked; }
}
///
/// List of files existing in the working directory that are ignored.
///
public IEnumerable Ignored
{
get { return ignored; }
}
///
/// True if the index or the working directory has been altered since the last commit. False otherwise.
///
public bool IsDirty
{
get { return isDirty; }
}
}
}