【发布时间】:2019-10-15 08:23:45
【问题描述】:
我想从本地存储库中获取创建或更改特定文件的所有提交。到目前为止,我们的应用程序通过将文件名放入提交消息中,然后在创建历史记录时搜索具有该名称的提交消息来处理此问题。如果可能的话,我希望有一个不涉及提交消息的更清洁的解决方案。
这是我迄今为止尝试过的:
private List<Commit> GetAllCommitsForFile(string repositoryPath, string filename)
{
using (var repo = new Repository(repositoryPath))
{
List<LogEntry> logs = repo.Commits.QueryBy(Path.Combine(RepositoryPath, filename)).ToList();
List<Commit> commits = new List<Commit>();
foreach(LogEntry log in logs)
{
commits.Add(log.Commit);
}
return commits;
}
}
QueryBy-method 的描述说:
返回存储库的提交列表,表示文件重命名后的历史记录。
对我来说,这听起来正是我想要的。然而,当我只传递文件名时,我得到一个 KeyNotFoundException,当我将它与存储库路径结合时,我得到一个空列表。
【问题讨论】:
-
“当我只传递文件名时,我得到一个异常” - 它告诉你什么?
-
@CodeCaster 已编辑,抱歉懒惰。
-
它有消息吗?你的历史是什么样的,你能创建一个minimal reproducible example吗?另请参阅github.com/libgit2/libgit2sharp/issues/1410
-
嗯,消息说:“给定的密钥不在目录中”,这里没有新信息。至于历史:有几个提交,每个提交创建一个或两个文件。因此,对于复制,您可以将几个文件提交到一个新的 repo,然后在其中一个上运行上述方法......
标签: c# git libgit2sharp