如何检测libgit2sharp中的待处理更改?

21

7
您可以查看 RepositoryStatus status = repo.Index.RetrieveStatus(); 方法的更多信息,详见单元测试 - nemesv
1
@nemesv 这真的应该是一个答案... 它真的很好 :) - Joel Martinez
2
看起来 repo.Index.RetrieveStatus() 已经被弃用,建议使用 repo.RetrieveStatus()。 - Jason Koopmans
3个回答

19

以下方法适用于我:

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

感谢 @Derptastic 提供的链接到 LibGit2Sharp Wiki


1
这在一些仓库中给了我错误的结果,因为 Git 本身没有发现未提交的更改,但是 repo.isDirty 返回 true。如果你想要一个简单的布尔检查来判断 Git diff(或者本质上是“我有未提交的更改吗”),可以参考官方文档 - https://github.com/libgit2/libgit2sharp/wiki/git-diff - repo.Diff.Compare<TreeChanges>().Count > 0 - Derptastic

7
以下代码将提供文件名和该文件的状态。
foreach (var item in repo1.RetrieveStatus())
{
  Console.WriteLine(item.FilePath);
  Console.WriteLine(item.State);
}    

6
您可以使用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)

没有传递任何路径应该显示所有更改。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接