Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在LibGit2Sharp中从提交中修改/添加/删除文件_C#_Git_Libgit2sharp - Fatal编程技术网

C# 在LibGit2Sharp中从提交中修改/添加/删除文件

C# 在LibGit2Sharp中从提交中修改/添加/删除文件,c#,git,libgit2sharp,C#,Git,Libgit2sharp,我使用此方法,从上次提交中获取文件: static void GetFiles(Tree t, String dir = "") { foreach (TreeEntry treeEntry in t) { if (treeEntry.TargetType == TreeEntryTargetType.Tree) { Tree tr = repo.Lookup<Tree>(treeEntry.

我使用此方法,从上次提交中获取文件:

static void GetFiles(Tree t, String dir = "")
{
    foreach (TreeEntry treeEntry in t)
    {
        if (treeEntry.TargetType == TreeEntryTargetType.Tree)
        {
            Tree tr = repo.Lookup<Tree>(treeEntry.Target.Sha);
            GetFiles(tr, dir + "/" + treeEntry.Name);
        }
        else
        {
            string caminho = dir + "/" + treeEntry.Path;
            arquivos.Add(caminho);
        }
        
    }
    return;
}
上次提交时修改了此文件:

c:/teste
| - /outros
| | - octocatblue.txt <- This modified
| | - octored.txt <- This new

由于git在文件系统中存储数据的方式,git提交是提交中包含的所有文件的快照。然后
对象返回存储库中所有文件的状态


我认为您必须在这次提交的树和上一次提交的树之间进行区分,我认为应该使用方法
Repository.diff.Compare()

您能帮我更改方法
GetFiles
吗?
c:/teste
| - /outros
| | - octocatblue.txt <- This modified
| | - octored.txt <- This new
static void CompareTrees()
{
    using (repo)
    {
        Tree commitTree = repo.Head.Tip.Tree; // Main Tree
        Tree parentCommitTree = repo.Head.Tip.Parents.First().Tree; // Secondary Tree

        var patch = repo.Diff.Compare<Patch>(parentCommitTree, commitTree); // Difference

        foreach (var ptc in patch)
        {
            Console.WriteLine(ptc.Status +" -> "+ptc.Path); // Status -> File Path
        }
    }
}