Search code examples
c#gitlibgit2libgit2sharp

Libgit2Sharp : Get files associated with the initial (first) commit


How can I retrieve files that were part of the very first (initial) commit of a repository?

I'm currently using the following to find out files that are part of a commit (and it works). However, since the method need two parameters, what should I pass to get the files that are part of the fist commit? Or is there another method I need to use?

    repo.Diff.Compare<TreeChanges>(repo.Commits.ElementAt(i).Tree, repo.Commits.ElementAt(i + 1).Tree)

Thanks!


Solution

  • I was able to achieve my requirement using the following:

                        //The tree object corresponding to the first commit in the repo
                        Tree firstCommit = repo.Lookup<Tree>(repo.Commits.ElementAt(i).Tree.Sha);
                        //The tree object corresponding to the last commit in the repo
                        Tree lastCommit = repo.Lookup<Tree>(repo.Commits.ElementAt(0).Tree.Sha);
    
    
                        var changes = repo.Diff.Compare<TreeChanges>(lastCommit, firstCommit);
                        foreach (var item in changes)
                        {
                            if (item.Status != ChangeKind.Deleted)
                            {
                                //...This object (i.e. item) corresponds to a file that was part of the first (initial) commit...
                            }
                        }
    

    Let me know if there is a better way...