我遇到了另一个libgit2问题,非常感谢你的帮助。
我正在尝试检索文件历史记录,即更改此文件的提交列表。看起来这相当不寻常...据我所见,没有相应的函数。
我能想到的唯一方法是使用修订历史API迭代修订版本,检查附加到提交的树对象,并在其中搜索给定的文件,如果找到,则将提交添加到我的列表中;否则继续到下一个提交。
但是,我认为这并不是最优解...
也许还有其他方法,例如直接查看.git文件夹并从中获取所需信息?
非常感谢您的帮助!
我遇到了另一个libgit2问题,非常感谢你的帮助。
我正在尝试检索文件历史记录,即更改此文件的提交列表。看起来这相当不寻常...据我所见,没有相应的函数。
我能想到的唯一方法是使用修订历史API迭代修订版本,检查附加到提交的树对象,并在其中搜索给定的文件,如果找到,则将提交添加到我的列表中;否则继续到下一个提交。
但是,我认为这并不是最优解...
也许还有其他方法,例如直接查看.git文件夹并从中获取所需信息?
非常感谢您的帮助!
但我觉得这对我来说看起来不太理想...
您的方法是正确的。请注意,您将不得不解决以下问题:
或许还有其他方法,例如直接查看.git文件夹并在那里获取所需信息?
尽管理解.git文件夹布局总是一个值得花费时间的过程,但恐怕这不能帮助您解决此特定文件历史记录问题。
注意:这个问题非常接近于这个libgit2sharp问题:如何获得影响给定文件的最后一次提交?
拉取请求#963添加了这个特性。
自LibGit2Sharp.0.22.0-pre20150415174523
预发布NuGet包以来,它已经可用。
这主要是在libgit2的issues/495中跟进的。
虽然它在libgit2sharp(实现)中已经实现了(PR 963,用于里程碑22),但它仍然“up for grabs”在libgit2本身。
该问题记录在issues/3041:提供包装revwalk的日志功能中。
问题中提到的方法在这个libgit2sharp示例中使用,并可以适应使用libgit2的C语言。在3041解决之前,它仍然是当前的解决方法。
如果使用C#,这个功能已经被添加到LibGit2Sharp
0.22.0NuGet Package(Pull Request 963)。你可以这样做:
var fileHistory = repository.Commits.QueryBy(filePathRelativeToRepository);
foreach (var version in fileHistory)
{
// Get further details by inspecting version.Commit
}
/// <summary>
/// Gets the previous commit of the file.
/// </summary>
/// <param name="repository">The repository.</param>
/// <param name="filePathRelativeToRepository">The file path relative to repository.</param>
/// <param name="commitSha">The commit sha to start the search for the previous version from. If null, the latest commit of the file will be returned.</param>
/// <returns></returns>
private static Commit GetPreviousCommitOfFile(Repository repository, string filePathRelativeToRepository, string commitSha = null)
{
bool versionMatchesGivenVersion = false;
var fileHistory = repository.Commits.QueryBy(filePathRelativeToRepository);
foreach (var version in fileHistory)
{
// If they want the latest commit or we have found the "previous" commit that they were after, return it.
if (string.IsNullOrWhiteSpace(commitSha) || versionMatchesGivenVersion)
return version.Commit;
// If this commit version matches the version specified, we want to return the next commit in the list, as it will be the previous commit.
if (version.Commit.Sha.Equals(commitSha))
versionMatchesGivenVersion = true;
}
return null;
}