Get path of commit files with LibGit2Sharp

1

How do I get the path of my last commit files? I have this example method:

private static void RepoListFiles()
{
    if (!arguments.ContainsKey("repository"))
    {
        Error("O repositório ainda não foi informado.");
        return;
    }

    using (repo)
    {
        if (repo.Head.Commits.Count() < 1)
        {
            Error("Nenhum Commit existente.");
            return;
        }

        Commit commit = repo.Commits.First();

        Tree tree = repo.Lookup<Commit>(commit.Sha).Tree;

        Console.WriteLine("\nSha: " + commit.Sha);
        if (commit.Author.Name != "Unknow") Console.WriteLine("Autor: " + commit.Author.Name);
        if (commit.Committer.Name != "Unknow") Console.WriteLine("Commiter: " + commit.Committer.Name);
        Console.WriteLine("Data: " + commit.Author.When); //Commit-Date
        Console.WriteLine("Mensagem: " + commit.Message);
        Console.WriteLine("---------------------------------");
        arquivos = new List<string>();
        GetFiles(tree);
        /*foreach (TreeEntry treeEntry in tree)
        {
            Console.WriteLine("Path: "   + treeEntry.Path);
            Console.WriteLine("Name: "   + treeEntry.Name);
            Console.WriteLine("---------");
        }*/

        foreach (string file in arquivos)
        {
            Console.WriteLine(file);
        }

        Console.WriteLine("---------------------------------");

    }
}

static void CompareTrees()
{
    using (repo)
    {
        Tree commitTree = repo.Head.Tip.Tree;
        Tree parentCommitTree = repo.Head.Tip.Parents.Single().Tree;

        var changes = repo.Diff.Compare<TreeChanges>(parentCommitTree, commitTree);

        TreeEntryChanges treeEntryChanges = changes.Single(c => c.Path == "1.txt");

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

Program:

But the way out does not get me all the way.

Directory structure

c:/teste
| - octocat.txt
| - parentoctocat.txt
| - /outros
| | - octocatblue.txt
| | - octored.txt

In my commit has only the files c:/teste/outros/octored.txt and c:/teste/outros/octocatblue.txt added.

But in the output comes all the files.

How do I get only the files added in my commit :

/outros/octocatblue.txt
/outros/octored.txt

[Edited]
Note: I have already been able to get the full path of the file. But I only need the files I've added in commit as $ git add outros/octocatblue.txt .

    
asked by anonymous 12.05.2015 / 17:49

2 answers

1

The answer of Cigano answers my question, but it was very confusing for me and I had to research enough to be able to understand it, considering that I am still learning # gitlib2sharp . So I'm posting the bare and raw solution.

I created a method that compares trees by taking the main Commit :

Tree commitTree = repo.Head.Tip.Tree;

And another Commit :

Tree parentCommitTree = repo.Head.Tip.Parents.Single().Tree;

And comparing the difference:

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

Going through this difference foreach (var ptc in patch) I get the path ptc.Path and status ptc.Status (Added, Renamed, Deleted, Modified, etc ...).

Method:

static void CompareTrees()
{
    using (repo)
    {
        Tree commitTree = repo.Head.Tip.Tree; // Arvore principal
        Tree parentCommitTree = repo.Head.Tip.Parents.Single().Tree; // Arvore anerior

        var patch = repo.Diff.Compare<Patch>(parentCommitTree, commitTree); // Diferênça

        foreach (var ptc in patch)
        {
            Console.WriteLine(ptc.Status +" -> "+ptc.Path); // Status -> Caminho do arquivo
        }
    }
}
    
13.05.2015 / 16:41
1

Apparently, it is comparing the previous commit tree with the current one. Here I found a fixture used in the project to test two trees that can help :

    public void CanCompareACommitTreeAgainstItsParent()
    {
        var path = SandboxStandardTestRepoGitDir();
        using (var repo = new Repository(path))
        {
            Tree commitTree = repo.Head.Tip.Tree;
            Tree parentCommitTree = repo.Head.Tip.Parents.Single().Tree;

            var changes = repo.Diff.Compare<TreeChanges>(parentCommitTree, commitTree);

            Assert.Equal(1, changes.Count());
            Assert.Equal(1, changes.Added.Count());

            TreeEntryChanges treeEntryChanges = changes.Single(c => c.Path == "1.txt");

            var patch = repo.Diff.Compare<Patch>(parentCommitTree, commitTree);
            Assert.False(patch["1.txt"].IsBinaryComparison);

            Assert.Equal("1.txt", treeEntryChanges.Path);
            Assert.Equal(ChangeKind.Added, treeEntryChanges.Status);

            Assert.Equal(treeEntryChanges, changes.Added.Single());

            Assert.Equal(Mode.Nonexistent, treeEntryChanges.OldMode);
        }
    }
    
12.05.2015 / 21:57