Search code examples
libgit2sharp

Unable to get added file names from the commit in libgit2sharp


I have come up with following code to identify changed files between the latest and the previous commit. This logic shows the name of the changed file. However it doesn't log the name of the "Added" file. I'm using GitLab repo to test this. Please advice how to resolve this.

    static void Main(string[] args)
    {            
        using (var repo = new Repository(@"C:\xxxx\source\repos\SampleApp"))
        {
            //Get the brach
            var branch = repo.Branches.Where(b => !b.IsRemote && b.IsCurrentRepositoryHead).FirstOrDefault();

            //Get the latest commit
            var latestCommit = branch.Commits.ElementAt(0);
            Console.WriteLine(string.Format("Latest Commit: {0}-{1}", latestCommit.MessageShort, latestCommit.Committer.When));

            //Get the previous comit
            var previousCommit = branch.Commits.ElementAt(1);
            Console.WriteLine(string.Format("Previous Commit: {0}-{1}", previousCommit.MessageShort, previousCommit.Committer.When));

            //Get the change set
            var changeSet = repo.Diff.Compare<TreeChanges>(latestCommit.Tree, previousCommit.Tree);

            var modifiedFiles = changeSet.Modified;                
            var addedFiles = changeSet.Added;

            //Print names of modified files
            foreach (var m in modifiedFiles)
            {
                Console.WriteLine("Modified: " + m.Path);
            }

            //Print names of added files
            foreach (var m in addedFiles)
            {
                Console.WriteLine("Added: " + m.Path);
            }
            Console.Read();     
        }
    }
}

Thanks in advance.

Update: The issue was with the below line where it gets the change set. I used old tree and new tree parameters other way. I have swapped those and now it works fine.

//Get the change set
var changeSet = repo.Diff.Compare<TreeChanges>(previousCommit.Tree, latestCommit.Tree);

Solution

  • Below is the fully functiong code.

    static void Main(string[] args)
            {            
                using (var repo = new Repository(@"C:\xxxx\source\repos\SampleApp"))
                {
                    //Get the brach
                    var branch = repo.Branches.Where(b => !b.IsRemote && b.IsCurrentRepositoryHead).FirstOrDefault();
    
                    //Get the latest commit
                    var latestCommit = branch.Commits.ElementAt(0);
                    Console.WriteLine(string.Format("Latest Commit: {0}-{1}", latestCommit.MessageShort, latestCommit.Committer.When));
    
                    //Get the previous comit
                    var previousCommit = branch.Commits.ElementAt(1);
                    Console.WriteLine(string.Format("Previous Commit: {0}-{1}", previousCommit.MessageShort, previousCommit.Committer.When));
    
                    //Get the change set
                    var changeSet = repo.Diff.Compare<TreeChanges>(previousCommit.Tree, latestCommit.Tree);
    
                    var modifiedFiles = changeSet.Modified;                
                    var addedFiles = changeSet.Added;
    
                    //Print names of modified files
                    foreach (var m in modifiedFiles)
                    {
                        Console.WriteLine("Modified: " + m.Path);
                    }
    
                    //Print names of added files
                    foreach (var m in addedFiles)
                    {
                        Console.WriteLine("Added: " + m.Path);
                    }
                    Console.Read();     
                }
            }