如何获取包含特定文件的提交列表,即git log path
的LibGit2Sharp
等效项。
它是否尚未实现,或者有什么我缺少的方法?
最佳答案
我正在尝试使用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;
}
关于c# - LibGit2Sharp等效于git日志路径是什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13122138/