如何获得单个提交的每个更改文件的补丁? [英] How to get the patch of every changed file of a single commit?

查看:185
本文介绍了如何获得单个提交的每个更改文件的补丁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过挖掘git-repository来获取关于commit-history的一些信息。我使用的是包libgit2sharp。



到目前为止,我得到了commit-author,committer,sha-value,commit-date和commit-message。我的问题是在存储库树中移动,以获得每个提交的所有已更改文件的补丁。



以前是否有人解决过这个问题,或者可以帮助我?

 使用(var repo = new Repository(@path \to\.git))
{
var commits = repo.Commits;
提交lastCommit = commits.Last();

foreach(提交中提交提交)
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);

树树= commit.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);
}
}


解决方案

>(How)获取每个提交的所有已更改文件的补丁?
$ b 使用 Diff.Compare< ;通过传递每个 Commit 来修补>()愿意进行比较。

 树commitTree1 = repo.Lookup< Commit>(f8d44d7)。 
Tree commitTree2 = repo.Lookup< Commit>(7252fe2)。

var patch = repo.Diff.Compare< Patch>(commitTree1,commitTree2);

通过查看测试方法,可以找到更多的使用细节 CanCompareTwoVersionsOfAFileWithADiffOfTwoHunks() 中的 DiffTreeToTreeFixture.cs 测试套件。



>另一个尝试是以下代码。它显示了根级别的文件和文件夹,但是我无法打开一个文件夹。

每个 TreeEntry 暴露了一个目标属性,它返回 GitObject



TargetType 类型为 TreeEntryTargetType.Tree 时,为了检索此子<$ c $

  var subTree =(Tree) ,您必须使用以下内容: treeEntry.Target; 


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);
        }
   }

解决方案

>(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;

这篇关于如何获得单个提交的每个更改文件的补丁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆