Skip to content
Piyush Rungta edited this page Jun 23, 2024 · 2 revisions

Git

$git cat-file {sha}:{filename}

LibGit2Sharp

repo.Head.Tip[{FilePathToContentFrom}].Target as Blob;
using (var content = new StreamReader(blob.GetContentStream(), Encoding.UTF8))
{
   commitContent = content.ReadToEnd();
}

From StackOverFlow

Complete example:

using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using LibGit2Sharp;

namespace libgitblob
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            var repo = new Repository ("/your/repo/path");
            foreach (var item in repo.RetrieveStatus()) {
                if (item.State == FileStatus.Modified) {
                    var blob = repo.Head.Tip[item.FilePath].Target as Blob;
                    string commitContent;
                    using (var content = new StreamReader(blob.GetContentStream(), Encoding.UTF8))
                    {
                        commitContent = content.ReadToEnd();
                    }
                    string workingContent;
                    using (var content = new StreamReader(repo.Info.WorkingDirectory + Path.DirectorySeparatorChar + item.FilePath, Encoding.UTF8))
                    {
                        workingContent = content.ReadToEnd();
                    }
                    Console.WriteLine ("\n\n~~~~ Original file ~~~~");
                    Console.WriteLine(commitContent);
                    Console.WriteLine ("\n\n~~~~ Current file ~~~~");
                    Console.WriteLine(workingContent);
                }
            }
        }
    }
}
Clone this wiki locally