【发布时间】:2014-07-18 10:15:33
【问题描述】:
我试图通过挖掘一个 git 存储库来获取有关提交历史的一些信息。我正在使用包 libgit2sharp。
到目前为止,我得到了提交作者、提交者、sha-value、提交日期和提交消息。我的问题是遍历存储库树,以获取每个提交的所有更改文件的补丁。
以前有人解决过这个问题吗,或者可以帮助我吗?
using (var repo = new Repository(@"path\to\.git"))
{
var commits = repo.Commits;
Commit lastCommit = commits.Last();
foreach (Commit commit in commits)
if (commit.Sha != lastCommit.Sha)
{
Console.WriteLine(commit.Sha);
Console.WriteLine(commit.Author.Name);
Console.WriteLine(commit.Committer.Name);
Console.WriteLine(commit.Author.When); //Commit-Date
Console.WriteLine(commit.Message);
Tree tree = commit.Tree;
Tree parentCommitTree = lastCommit.Tree;
TreeChanges changes = repo.Diff.Compare<TreeChanges>(parentCommitTree, tree);
foreach (TreeEntryChanges treeEntryChanges in changes)
{
ObjectId oldcontenthash = treeEntryChanges.OldOid;
ObjectId newcontenthash = treeEntryChanges.Oid;
}
}
}
另一个尝试是下面的代码。它显示了根级的文件和文件夹,但我无法打开文件夹。
foreach(TreeEntry treeEntry in tree)
{
// Blob blob1 = (Blob)treeEntry.Target;
var targettype = treeEntry.TargetType;
if (targettype == TreeEntryTargetType.Blob)
{
string filename = treeEntry.Name;
string path = treeEntry.Path;
string sha = treeEntry.Target.Sha;
var filemode = treeEntry.Mode;
Console.WriteLine(filename);
Console.WriteLine(path);
}
else if (targettype == TreeEntryTargetType.Tree)
{
Console.WriteLine("Folder: " + treeEntry.Name);
}
}
【问题讨论】:
标签: git libgit2sharp