如何获取包含特定文件的提交列表,即git log path
的等效LibGit2Sharp
。
它没有实施,还是有某种方式让我失踪?
答案 0 :(得分:3)
LibGit2Sharp来自C库libgit2 ...首先不包含git log
;)
然而,LibGit2Sharp有自己的git log
功能:
其page on git log
涉及Filters,但过滤器似乎不按路径过滤(详见“How to exclude stashes while querying refs?”)。
所以目前似乎没有实施。
答案 1 :(得分:3)
我正致力于使用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;
}
答案 2 :(得分:3)
每次更改树/ 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));
答案 3 :(得分:1)
非常类似于dmck的答案,但更新了
private bool TreeContainsFile(Tree tree, string filePath)
{
//filePath is the relative path of your file to the root directory
if (tree.Any(x => x.Path == filePath))
{
return true;
}
return tree.Where(x => x.GetType() == typeof (TreeEntry))
.Select(x => x.Target)
.OfType<Tree>()
.Any(branch => TreeContainsFile(branch, filePath));
}