问题
I’m trying to get some information about the commit-history by mining a git-repository. I’m using the package libgit2sharp.
So far, I got the commit-author, committer, sha-value, commit-date and the commit-message. My problem is to move through the repository-tree, to get the patch of all changed files of every single commit.
Does anybody solve this problem before, or can help me?
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;
}
}
}
Another trying is the following code. It shows the files and folder of the root-level, but I cant open a folder.
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);
}
}
回答1:
>(How) to get the patch of all changed files of every single commit?
Use the Diff.Compare<Patch>()
method by passing it the Tree
of each Commit
you're willing to compare.
Tree commitTree1 = repo.Lookup<Commit>("f8d44d7").Tree;
Tree commitTree2 = repo.Lookup<Commit>("7252fe2").Tree;
var patch = repo.Diff.Compare<Patch>(commitTree1, commitTree2);
One can find more usage details by taking a look at the test metthod CanCompareTwoVersionsOfAFileWithADiffOfTwoHunks() in the DiffTreeToTreeFixture.cs test suite.
>Another trying is the following code. It shows the files and folder of the root-level, but I cant open a folder.
Each TreeEntry
exposes a Target
property returning the pointed at GitObject
.
When the TargetType
is of type TreeEntryTargetType.Tree
, in order to retrieve this child Tree
, you'd have to use the following:
var subTree = (Tree)treeEntry.Target;
回答2:
Thanks for the answer!
Now I receive the patch between two commits. With the following code, the OutOfMemoryException is often thrown.
LibGit2Sharp.Commit lastCommit = commits.First();
repository.CommitCount = commits.Count();
foreach (LibGit2Sharp.Commit commit in commits)
if (commit.Sha != lastCommit.Sha)
{
Tree commitTree1 = repo.Lookup<LibGit2Sharp.Commit>(lastCommit.Sha).Tree;
Tree commitTree2 = repo.Lookup<LibGit2Sharp.Commit>(commit.Sha).Tree;
var patch = repo.Diff.Compare<Patch>(commitTree1, commitTree2);
// some value assigments
lastCommit = commit;
}
来源:https://stackoverflow.com/questions/24822582/how-to-get-the-patch-of-every-changed-file-of-a-single-commit