[英]How to retrieve the history of a file?
我得到了另一个libgit2问题,非常感谢你的帮助。
我正在尝试检索文件历史记录,即更改此文件的提交列表。 而且它似乎非常传统......据我所见,没有任何功能。
我能想到的唯一方法是使用版本控制API迭代修订版本,检查附加到提交的树对象并在那里搜索给定文件,如果找到,则将提交添加到我的列表中,否则继续下一次提交。
但它对我来说似乎不是最理想的......
可能是有任何其他方法,例如,直接查看.git文件夹并获取所需信息吗?
提前谢谢了!
但它对我来说似乎不是最理想的......
你的方法是正确的。 要小心你必须要反对:
可能是有任何其他方法,例如,直接查看.git文件夹并获取所需信息吗?
虽然理解.git文件夹布局总是花费很多时间,但我担心这不会帮助您解决这个特定的文件历史问题。
注意:这个问题与libgit2sharp问题非常接近: 如何获取影响给定文件的最后一次提交?
拉请求#963添加了这个功能。
自LibGit2Sharp.0.22.0-pre20150415174523
预发布NuGet包以来可用。
这主要是在libgit2的问题/ 495中。
即使它是在libgit2sharp ( PR 963 , 里程碑22 )中实现的,它仍然在libgit2本身“抢夺”。
问题/ 3041中记录了该问题:提供包装revwalk
日志功能 。
问题中提到的方法在这个libgit2sharp示例中使用 ,可以使用libgit2适应C语言。 在3041号决议之前,它仍然是当前的解决方法。
如果使用C#,则此功能已添加到LibGit2Sharp
0.22.0 NuGet包 ( Pull Request 963 )。 您可以执行以下操作:
var fileHistory = repository.Commits.QueryBy(filePathRelativeToRepository);
foreach (var version in fileHistory)
{
// Get further details by inspecting version.Commit
}
在我的Diff All Files VS Extension (它是开源的,所以你可以查看代码),我需要获得一个文件的先前提交,所以我可以看到在给定的提交中对文件做了哪些更改。 这是我检索文件先前提交的方式:
/// <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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.