什么是git日志路径的LibGit2Sharp等价物?

too*_*ays 9 c# git libgit2 libgit2sharp

如何获取包含特定文件的提交列表,即等效git log pathLibGit2Sharp.

它没有实施,还是有一种我失踪的方式?

Von*_*onC 5

LibGit2Sharp来自 C 库libgit2git log ...它一开始并没有包含在内;)

然而,LibGit2Sharp 有自己的git log功能:
它的页面git log涉及Filters,但 Filter 似乎并没有按路径过滤(详见“如何在查询引用时排除 stashes? ”)。
所以目前看来还没有实施。


dmc*_*mck 5

我正在努力使用 LibGit2Sharp 在我的应用程序中获得相同的功能。

我编写了下面的代码,它将列出包含该文件的所有提交。不包括 GitCommit 类,但它只是属性的集合。

我的目的是让代码只列出文件已更改的提交,类似于 SVN 日志,但我还没有编写那部分。

请注意,代码尚未优化,这只是我的初步尝试,但我希望它有用。

/// <summary>
/// Loads the history for a file
/// </summary>
/// <param name="filePath">Path to file</param>
/// <returns>List of version history</returns>
public List<IVersionHistory> LoadHistory(string filePath)
{
    LibGit2Sharp.Repository repo = new Repository(this.pathToRepo);

    string path = filePath.Replace(this.pathToRepo.Replace(System.IO.Path.DirectorySeparatorChar + ".git", string.Empty), string.Empty).Substring(1);
    List<IVersionHistory> list = new List<IVersionHistory>();

    foreach (Commit commit in repo.Head.Commits)
    {
        if (this.TreeContainsFile(commit.Tree, path) && list.Count(x => x.Date == commit.Author.When) == 0)
        {
            list.Add(new GitCommit() { Author = commit.Author.Name, Date = commit.Author.When, Message = commit.MessageShort} as IVersionHistory);
        }
    }

    return list;
}

/// <summary>
/// Checks a GIT tree to see if a file exists
/// </summary>
/// <param name="tree">The GIT tree</param>
/// <param name="filename">The file name</param>
/// <returns>true if file exists</returns>
private bool TreeContainsFile(Tree tree, string filename)
{
    if (tree.Any(x => x.Path == filename))
    {
        return true;
    }
    else
    {
        foreach (Tree branch in tree.Where(x => x.Type == GitObjectType.Tree).Select(x => x.Target as Tree))
        {
            if (this.TreeContainsFile(branch, filename))
            {
                return true;
            }
        }
    }

    return false;
}
Run Code Online (Sandbox Code Playgroud)


小智 5

每次树/blob 发生更改时,它都会获得新的 id 哈希值。您所需要的只是与父提交树/blob 项哈希进行比较:

var commits = repository.Commits
   .Where(c => c.Parents.Count() == 1 && c.Tree["file"] != null &&
      (c.Parents.FirstOrDefault().Tree["file"] == null ||
         c.Tree["file"].Target.Id !=
         c.Parents.FirstOrDefault().Tree["file"].Target.Id));
Run Code Online (Sandbox Code Playgroud)