How to detect pending changes in libgit2sharp?

The following works for me:

///DEPRECATED - see comment from @derptastic
public bool HasUncommittedChanges
{
    get
    {
        using (var repo = new Repository(repositoryRoot))
        {
            RepositoryStatus status = repo.RetrieveStatus();
            return status.IsDirty;
        }
    }
}

Thanks to @Derptastic for the link to LibGit2Sharp Wiki


You can use repository.Diff.Compare().

    /// <summary>
    ///   Show changes between the working directory and the index.
    /// </summary>
    /// <param name = "paths">The list of paths (either files or directories) that should be compared.</param>
    /// <returns>A <see cref = "TreeChanges"/> containing the changes between the working directory and the index.</returns>
    public virtual TreeChanges Compare(IEnumerable<string> paths = null)

Passing no paths at all should give all changes.


The following lines of code will provide the filename and the state of that file.

foreach (var item in repo1.RetrieveStatus())
{
  Console.WriteLine(item.FilePath);
  Console.WriteLine(item.State);
}