Get the path of a commit files with Libgit2sharp

Asked

Viewed 86 times

1

How to get the file path of my last commit? 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:

Execução do programa

But the exit doesn’t bring me all the way.

Structure of the directory

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 on output comes all the files.

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

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

[Edited]
Note: I already managed to get the full path of the file. However I only need the files I added to commit as $ git add outros/octocatblue.txt.

2 answers

1

Apparently it’s comparing the previous commit tree to 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);
        }
    }
  • So man, I don’t know much about C#, this "Assert object" doesn’t have in my project, it must be something from their test. That 1.txt is the file he’s comparing?

  • I updated my code and print there on the question. I couldn’t test this "example" because I didn’t understand it, I don’t know if this 1.txt is the file being compared, or if it is the file from where it stores the result.

  • @Kaduamaral Assert is a set of sets for comparison. It is used to write unit tests. 1.txt is any file he has versioned and is checking for changes.

1


The reply of the Gypsy answers my question, but she was very confused for me and I had to do a lot of research to understand her, taking into account that I am still learning and the library . That’s why I’m posting the raw solution.


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

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

Walking through this gap foreach (var ptc in patch) I get the way ptc.Path and the 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
        }
    }
}
  • Why did you mark my answer as accepted? Your answer is better.

  • 1

    @Gypsy omorrisonmendez I answered after I had already accepted and has a minimum time for you to accept it

Browser other questions tagged

You are not signed in. Login or sign up in order to post.